#include "delay.h"

/**
 * @brief  Microsecond delay function.
 * @note   Use the Systic timing counter, SystemCoreClock must be set correctly.
 * @param  us: 1 ~ 200000.
 * @retval None
 */
void DelayUS(uint32_t us)
{
    SysTick->LOAD = us * (SystemCoreClock/1000000);
    SysTick->VAL  = (0x00);
    SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk;

    /* Waiting for down-count to zero */
    while ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == 0);

    /* Disable SysTick counter */
    SysTick->CTRL = 0;
}

/**
 * @brief  Millisecond delay function.
 * @param  ms: 1 ~ 0xFFFFFFFF.
 * @retval None
 */
void DelayMS(uint32_t ms)
{
    uint32_t i;
    for(i=0; i<ms; i++)
    {
        DelayUS(1000);
    }
}
