1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- #include <ch.h>
- #include "timer.h"
- static uint32_t reset_point = 0;
- #if CH_CFG_ST_RESOLUTION < 32
- static uint32_t last_systime = 0;
- static uint32_t overflow = 0;
- #endif
- void timer_init(void) { timer_clear(); }
- void timer_clear(void) {
- reset_point = (uint32_t)chVTGetSystemTime();
- #if CH_CFG_ST_RESOLUTION < 32
- last_systime = reset_point;
- overflow = 0;
- #endif
- }
- uint16_t timer_read(void) { return (uint16_t)timer_read32(); }
- uint32_t timer_read32(void) {
- uint32_t systime = (uint32_t)chVTGetSystemTime();
- #if CH_CFG_ST_RESOLUTION < 32
-
-
-
-
-
-
-
- if (systime < last_systime) {
- overflow += ((uint32_t)1) << CH_CFG_ST_RESOLUTION;
- }
- last_systime = systime;
- return (uint32_t)TIME_I2MS(systime - reset_point + overflow);
- #else
- return (uint32_t)TIME_I2MS(systime - reset_point);
- #endif
- }
- uint16_t timer_elapsed(uint16_t last) { return TIMER_DIFF_16(timer_read(), last); }
- uint32_t timer_elapsed32(uint32_t last) { return TIMER_DIFF_32(timer_read32(), last); }
|