#include "delay.h"

//-----------------------------------------------------------------
#ifdef SOFT_DELAY

void delay_init ( u8 SYSCLK )
{}

#if 1
void delay_us ( u32 Number )
{
    u32 i = 0;

    while ( Number-- )
    {
        i = 12;

        while ( i-- );      //12
    }
}

void delay_ms ( u16 Number )
{
    u32 i;

    while ( Number-- )
    {
        i = 12000;

        while ( i-- );   //12000
    }
}

#else
unsigned char MULTIPLE8M = 9;
void delay_us ( u32 usNum )
{
    unsigned short kk, i;

    for ( i = usNum; i > 0; i-- )
    {
        kk = 2 * MULTIPLE8M - 1; //0x080003AA 4C06 LDR r4,[pc,#24] ; @0x080003C4 2clk

        do
        {
            //0x080003AC BF00 NOP 1clk
            kk--; //0x080003AE 1E64 SUBS r4,r4,#1 1clk
        }
        while ( kk ); //0x080003B0 2C00 CMP r4,#0x00 1clk

        //0x080003B2 D1FC BNE 0x080003AE jump:2clk, no jump:1clk
    }
}

void delay_ms ( u16 usNum )
{
    unsigned short kk, i;

    for ( i = usNum; i > 0; i-- )
    {
        if ( i % 100 == 0 )
        {
            //FEEDDOG;
        }

        kk = 2000 * MULTIPLE8M - 1; //0x080003AA 4C06 LDR r4,[pc,#24] ; @0x080003C4 2clk

        do
        {
            //0x080003AC BF00 NOP 1clk
            kk--; //0x080003AE 1E64 SUBS r4,r4,#1 1clk
        }
        while ( kk ); //0x080003B0 2C00 CMP r4,#0x00 1clk

        //0x080003B2 D1FC BNE 0x080003AE jump:2clk, no jump:1clk
    }
}
#endif
#endif

//---------------------------------------------------------------------------

#ifdef SYSTICK_DELAY
//参考原子的代码. 修正了中断中调用出现死循环的错误, 防止延时不准确,采用do
static u8 fac_us = 0;    //us延时倍乘数
static u16 fac_ms = 0;    //ms延时倍乘数

//初始化延迟函数, SYSTICK的时钟固定为HCLK时钟的1/8, SYSCLK:系统时钟, 单位为MHZ
void delay_init ( u8 SYSCLK )
{
    SysTick->CTRL &= 0xfffffffb;    //bit2清空,选择外部时钟 HCLK/8
    fac_us = SYSCLK / 8;
    fac_ms = ( u16 ) fac_us * 1000;
}

//延时nms, 注意nms的范围
//SysTick->LOAD为24位寄存器,所以,最大延时为:nms<=0xffffff*8*1000/SYSCLK
//SYSCLK单位为Hz,nms单位为ms, 对72M条件下,nms<=1864
void delay_ms ( u16 nms )
{
    u32 temp;

    SysTick->LOAD = ( u32 ) nms * fac_ms; //时间加载(SysTick->LOAD为24bit)
    SysTick->VAL = 0x00; //清空计数器
    SysTick->CTRL = 0x01 ; //开始倒数

    do
    {
        temp = SysTick->CTRL;
    }
    while ( temp & 0x01 && ! ( temp & ( 1 << 16 ) ) ); //等待时间到达

    SysTick->CTRL = 0x00; //关闭计数器
    SysTick->VAL = 0X00; //清空计数器
}

//延时nus, nus为要延时的us数.
void delay_us ( u32 nus )
{
    u32 temp;

    SysTick->LOAD = nus * fac_us; //时间加载
    SysTick->VAL = 0x00; //清空计数器
    SysTick->CTRL = 0x01 ; //开始倒数

    do
    {
        temp = SysTick->CTRL;
    }
    while ( temp & 0x01 && ! ( temp & ( 1 << 16 ) ) ); //等待时间到达

    SysTick->CTRL = 0x00; //关闭计数器
    SysTick->VAL = 0X00; //清空计数器
}
#endif
//-------------------------------------------------------------------

#ifdef TIMER_DELAY

void decrement_delay ( void )
{
    if ( counter_delay_ms != 0 )
    {
        /* Decrement the counter */
        counter_delay_ms--;
    }
}

void delay_init ( u8 SYSCLK )
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    RCC_APB1PeriphClockCmd ( TIMER_DELAY_CLOCK, ENABLE );

    TIM_TimeBaseStructure.TIM_Period                 = TIMER_DELAY_PERIOD;
    TIM_TimeBaseStructure.TIM_Prescaler         = TIMER_DELAY_PRESCALER;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode     = TIM_CounterMode_Up;
    TIM_TimeBaseInit ( TIMER_DELAY, &TIM_TimeBaseStructure );

    TIM_UpdateRequestConfig ( TIMER_DELAY, TIM_UpdateSource_Global );
    TIM_ClearITPendingBit ( TIMER_DELAY, TIM_IT_Update );

    /* Enable TIMER Update interrupt */
    TIM_ITConfig ( TIMER_DELAY, TIM_IT_Update, ENABLE );

    // Disable timers
    TIM_Cmd ( TIMER_DELAY, DISABLE );

    /* Enable and set TIMER IRQ used for delays */
    NVIC_InitStructure.NVIC_IRQChannel                                          = TIMER_DELAY_IRQ_CHANNEL;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority     = TIMER_DELAY_PREEMPTION_PRIORITY;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority                  = TIMER_DELAY_SUB_PRIORITY;
    NVIC_InitStructure.NVIC_IRQChannelCmd                                 = ENABLE;
    NVIC_Init ( &NVIC_InitStructure );
}

void delay_ms ( uint16_t delay )
{
    counter_delay_ms = delay;

    TIM_SetCounter ( TIMER_DELAY, 0 );
    /* TIM4 enable counter */
    TIM_Cmd ( TIMER_DELAY, ENABLE );

    /* Wait for 'delay' milliseconds */
    while ( counter_delay_ms != 0 );

    /* TIM4 disable counter */
    TIM_Cmd ( TIMER_DELAY, DISABLE );
}

void TIMER_DELAY_IRQ_HANDLER ( void )
{
    /* Clear TIMER update interrupt flag */
    TIM_ClearITPendingBit ( TIMER_DELAY, TIM_IT_Update );

    decrement_delay();
}

#endif
