
#include "delay.h"
#include "cache_drv.h"


static volatile u32 ticks10us=0;
static volatile u32 ticks=0;
volatile u32 tickSecs=0;

void SysTick_Handler1(void)
{
	if(ticks10us)
		ticks10us--;
	if(uartCountDown)
		uartCountDown--;

	ticks++;
	if(ticks%100000)
	{
		tickSecs++;		
	}
	
	
}
void InitTickTime(void)
{
	ticks10us=0xffffffff;
}
u32 GetTickTime(void)
{
	return (0xffffffff-ticks10us);
}
void InitSysTick(void)
{
	/* g_sys_clk / 1000    1ms左右中断一次
	 * g_sys_clk / 100000	 10us左右中断一次
	 * g_sys_clk / 1000000 1us左右中断一次
	 */
	if (SysTick_Config(g_core_clk/100000))	
	{ 
		/* Capture error */ 
		while (1);
	}
	// 关闭滴答定时器  
	//SysTick->CTRL &= ~ SysTick_CTRL_ENABLE_Msk;
}

void DelayMS(volatile uint32_t delaymS)
{
		volatile uint32_t k = 0;
    __IO uint32_t counter_ms;
    
    if(ICACHE->CACHE_CCR & ENCACHE)
    {
        counter_ms = g_sys_clk/6000;
    }
    else
    {
        counter_ms = g_sys_clk/36000;
    }
		
	//Added by Jason.Zhao 2021/8/17 10:34:8
	delaymS*=2;
	while(delaymS --)
	{
		while(k < counter_ms)k ++;
		k = 0;
	}
}
void delay(volatile uint32_t time)
{
	while(time--);
}

void Delay_ms(volatile uint32_t time)
{    
   volatile u32 i=0;  
   while(time--)
   {
      i=163000;    //自己定义
      while(i--) ;    
   }
}

void Delay_us(volatile uint32_t time)
{
	volatile u32 i=0;
	while(time--)
	{
		i=163;        //自己定义
		while(i--) ; 
	}
}


