hal_systick.c 737 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include "hal_systick.h"
  2. static u8 fac_us = 0; // us延时倍乘数
  3. static u16 fac_ms = 0; // ms延时倍乘数
  4. volatile uint32_t systick_ms = 0;
  5. void hal_systick_init(void)
  6. {
  7. SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
  8. if (SysTick_Config(SystemCoreClock / 1000))
  9. {
  10. while (1)
  11. {
  12. }
  13. }
  14. NVIC_SetPriority(SysTick_IRQn, 0x00);
  15. }
  16. uint32_t get_systick_ms(void)
  17. {
  18. uint32_t tick, tick_ms;
  19. __disable_irq();
  20. tick = (SYSTICK_EXT_CLOCK_1MS_CNT - SysTick->VAL);
  21. tick_ms = tick / (SYSTICK_EXT_CLOCK_1MS_CNT);
  22. tick_ms = tick_ms + systick_ms;
  23. __enable_irq();
  24. return tick_ms;
  25. }
  26. void systick_handler(void)
  27. {
  28. __disable_irq();
  29. systick_ms += 1;
  30. __enable_irq();
  31. }