#include "hal_systick.h"

static u8  fac_us = 0; // us延时倍乘数
static u16 fac_ms = 0; // ms延时倍乘数

volatile uint32_t systick_ms = 0;

void hal_systick_init(void)
{
    SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
    if (SysTick_Config(SystemCoreClock / 1000))
    {
        while (1)
        {
        }
    }
    NVIC_SetPriority(SysTick_IRQn, 0x00);
}

uint32_t get_systick_ms(void)
{
    uint32_t tick, tick_ms;
    __disable_irq();
    tick    = (SYSTICK_EXT_CLOCK_1MS_CNT - SysTick->VAL);
    tick_ms = tick / (SYSTICK_EXT_CLOCK_1MS_CNT);
    tick_ms = tick_ms + systick_ms;
    __enable_irq();

    return tick_ms;
}

void systick_handler(void)
{
    __disable_irq();
    systick_ms += 1;
    __enable_irq();
}