HAL_ChibiOS: fixed micros and millis on boards without 1MHz clock

This commit is contained in:
Andrew Tridgell 2023-12-24 09:28:21 +11:00
parent 5c1ed968b1
commit d2d2067f1c
2 changed files with 11 additions and 8 deletions

View File

@ -84,12 +84,16 @@ static uint32_t get_systime_us32(void)
wrap and directly gives a uint64_t (aka systimestamp_t) wrap and directly gives a uint64_t (aka systimestamp_t)
*/ */
uint64_t hrt_micros64I() static uint64_t hrt_micros64I(void)
{ {
#ifdef AP_BOARD_START_TIME uint64_t ret = chVTGetTimeStampI();
return chVTGetTimeStampI() + AP_BOARD_START_TIME; #if CH_CFG_ST_FREQUENCY != 1000000U
ret *= 1000000U/CH_CFG_ST_FREQUENCY;
#endif #endif
return chVTGetTimeStampI(); #ifdef AP_BOARD_START_TIME
ret += AP_BOARD_START_TIME;
#endif
return ret;
} }
static inline bool is_locked(void) { static inline bool is_locked(void) {
@ -99,17 +103,17 @@ static inline bool is_locked(void) {
uint64_t hrt_micros64() uint64_t hrt_micros64()
{ {
if (is_locked()) { if (is_locked()) {
return chVTGetTimeStampI(); return hrt_micros64I();
} else if (port_is_isr_context()) { } else if (port_is_isr_context()) {
uint64_t ret; uint64_t ret;
chSysLockFromISR(); chSysLockFromISR();
ret = chVTGetTimeStampI(); ret = hrt_micros64I();
chSysUnlockFromISR(); chSysUnlockFromISR();
return ret; return ret;
} else { } else {
uint64_t ret; uint64_t ret;
chSysLock(); chSysLock();
ret = chVTGetTimeStampI(); ret = hrt_micros64I();
chSysUnlock(); chSysUnlock();
return ret; return ret;
} }

View File

@ -7,7 +7,6 @@ extern "C" {
void hrt_init(void); void hrt_init(void);
uint64_t hrt_micros64(void); uint64_t hrt_micros64(void);
uint64_t hrt_micros64I(void); // from locked context
uint64_t hrt_micros64_from_ISR(void); // from an ISR uint64_t hrt_micros64_from_ISR(void); // from an ISR
uint32_t hrt_micros32(void); uint32_t hrt_micros32(void);
uint32_t hrt_millis32(void); uint32_t hrt_millis32(void);