systick.c 958 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "systick.h"
  2. #include "yc_timer.h"
  3. #include "ycdef.h"
  4. #include "system.h"
  5. uint32_t tick_count = 0;
  6. void systick_init(void)
  7. {
  8. SysTick_Config(SystemCoreClock/TICK_PER_SECOND);
  9. }
  10. // Return system uptime in microseconds (rollover in 70minutes)
  11. uint32_t systick_get_us(void)
  12. {
  13. register uint32_t ms, cycle_cnt;
  14. do {
  15. ms = tick_count;
  16. cycle_cnt = SysTick->VAL;
  17. } while (ms != tick_count);
  18. return ms * TICK_US + (SysTick->LOAD - cycle_cnt) * TICK_US / SysTick->LOAD;
  19. }
  20. uint32_t systick_get_ms(void)
  21. {
  22. return tick_count;
  23. }
  24. void delay_us(uint32_t us)
  25. {
  26. //SysTick_Config(SystemCoreClock/100000);
  27. uint32_t now = systick_get_us();
  28. while (systick_get_us() - now < us);
  29. }
  30. void delay_ms(uint32_t ms)
  31. {
  32. uint32_t now = tick_count;
  33. while (tick_count - now < ms);
  34. }
  35. void SysTick_Handler(void)
  36. {
  37. #ifdef DEBUG_SYS
  38. MyPrintf("SYSTICK Interrupt trigger a success!\n");
  39. #endif
  40. tick_count++;
  41. SYStick_handle();
  42. }