timer.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <ch.h>
  2. #include "timer.h"
  3. static uint32_t ticks_offset = 0;
  4. static uint32_t last_ticks = 0;
  5. static uint32_t ms_offset = 0;
  6. #if CH_CFG_ST_RESOLUTION < 32
  7. static uint32_t last_systime = 0;
  8. static uint32_t overflow = 0;
  9. #endif
  10. // Get the current system time in ticks as a 32-bit number.
  11. // This function must be called from within a system lock zone (so that it can safely use and update the static data).
  12. static inline uint32_t get_system_time_ticks(void) {
  13. uint32_t systime = (uint32_t)chVTGetSystemTimeX();
  14. #if CH_CFG_ST_RESOLUTION < 32
  15. // If the real system timer resolution is less than 32 bits, provide the missing bits by checking for the counter
  16. // overflow. For this to work, this function must be called at least once for every overflow of the system timer.
  17. // In the 16-bit case, the corresponding times are:
  18. // - CH_CFG_ST_FREQUENCY = 100000, overflow will occur every ~0.65 seconds
  19. // - CH_CFG_ST_FREQUENCY = 10000, overflow will occur every ~6.5 seconds
  20. // - CH_CFG_ST_FREQUENCY = 1000, overflow will occur every ~65 seconds
  21. if (systime < last_systime) {
  22. overflow += ((uint32_t)1) << CH_CFG_ST_RESOLUTION;
  23. }
  24. last_systime = systime;
  25. systime += overflow;
  26. #endif
  27. return systime;
  28. }
  29. #if CH_CFG_ST_RESOLUTION < 32
  30. static virtual_timer_t update_timer;
  31. // Update the system tick counter every half of the timer overflow period; this should keep the tick counter correct
  32. // even if something blocks timer interrupts for 1/2 of the timer overflow period.
  33. # define UPDATE_INTERVAL (((sysinterval_t)1) << (CH_CFG_ST_RESOLUTION - 1))
  34. // VT callback function to keep the overflow bits of the system tick counter updated.
  35. static void update_fn(void *arg) {
  36. (void)arg;
  37. chSysLockFromISR();
  38. get_system_time_ticks();
  39. chVTSetI(&update_timer, UPDATE_INTERVAL, update_fn, NULL);
  40. chSysUnlockFromISR();
  41. }
  42. #endif
  43. // The highest multiple of CH_CFG_ST_FREQUENCY that fits into uint32_t. This number of ticks will necessarily
  44. // correspond to some integer number of seconds.
  45. #define OVERFLOW_ADJUST_TICKS ((uint32_t)((UINT32_MAX / CH_CFG_ST_FREQUENCY) * CH_CFG_ST_FREQUENCY))
  46. // The time in milliseconds which corresponds to OVERFLOW_ADJUST_TICKS ticks (this is a precise conversion, because
  47. // OVERFLOW_ADJUST_TICKS corresponds to an integer number of seconds).
  48. #define OVERFLOW_ADJUST_MS (TIME_I2MS(OVERFLOW_ADJUST_TICKS))
  49. void timer_init(void) {
  50. timer_clear();
  51. #if CH_CFG_ST_RESOLUTION < 32
  52. chVTObjectInit(&update_timer);
  53. chVTSet(&update_timer, UPDATE_INTERVAL, update_fn, NULL);
  54. #endif
  55. }
  56. void timer_clear(void) {
  57. chSysLock();
  58. ticks_offset = get_system_time_ticks();
  59. last_ticks = 0;
  60. ms_offset = 0;
  61. chSysUnlock();
  62. }
  63. uint16_t timer_read(void) {
  64. return (uint16_t)timer_read32();
  65. }
  66. uint32_t timer_read32(void) {
  67. chSysLock();
  68. uint32_t ticks = get_system_time_ticks() - ticks_offset;
  69. if (ticks < last_ticks) {
  70. // The 32-bit tick counter overflowed and wrapped around. We cannot just extend the counter to 64 bits here,
  71. // because TIME_I2MS() may encounter overflows when handling a 64-bit argument; therefore the solution here is
  72. // to subtract a reasonably large number of ticks from the tick counter to bring its value below the 32-bit
  73. // limit again, and then add the equivalent number of milliseconds to the converted value. (Adjusting just the
  74. // converted value to account for 2**32 ticks is not possible in general, because 2**32 ticks may not correspond
  75. // to an integer number of milliseconds).
  76. ticks -= OVERFLOW_ADJUST_TICKS;
  77. ticks_offset += OVERFLOW_ADJUST_TICKS;
  78. ms_offset += OVERFLOW_ADJUST_MS;
  79. }
  80. last_ticks = ticks;
  81. uint32_t ms_offset_copy = ms_offset; // read while still holding the lock to ensure a consistent value
  82. chSysUnlock();
  83. return (uint32_t)TIME_I2MS(ticks) + ms_offset_copy;
  84. }
  85. uint16_t timer_elapsed(uint16_t last) {
  86. return TIMER_DIFF_16(timer_read(), last);
  87. }
  88. uint32_t timer_elapsed32(uint32_t last) {
  89. return TIMER_DIFF_32(timer_read32(), last);
  90. }