Skip to content

Commit

Permalink
AP_HAL_ChibiOS: add support for inducing clock drift
Browse files Browse the repository at this point in the history
  • Loading branch information
bugobliterator committed Feb 4, 2024
1 parent 4828f19 commit 911b594
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
10 changes: 10 additions & 0 deletions libraries/AP_HAL_ChibiOS/hwdef/common/hrt.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ static uint32_t system_time_u32_us(void)
static uint32_t get_systime_us32(void)
{
uint32_t now = system_time_u32_us();
#ifdef AP_INDUCE_PPM_OFFSET
// Please note that this will induce incorrect
// clock drift after 71 minutes, use hrt_micros64()
// as "now" here if you want to avoid that
now += (((uint64_t)now) * AP_INDUCE_PPM_OFFSET) / 1000000U;
#endif
#ifdef AP_BOARD_START_TIME
now += AP_BOARD_START_TIME;
#endif
Expand All @@ -90,6 +96,10 @@ static uint64_t hrt_micros64I(void)
#if CH_CFG_ST_FREQUENCY != 1000000U
ret *= 1000000U/CH_CFG_ST_FREQUENCY;
#endif
#ifdef AP_INDUCE_PPM_OFFSET
// induce clock drift
ret += (ret * AP_INDUCE_PPM_OFFSET) / 1000000U;
#endif
#ifdef AP_BOARD_START_TIME
ret += AP_BOARD_START_TIME;
#endif
Expand Down
13 changes: 12 additions & 1 deletion libraries/AP_HAL_ChibiOS/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,18 @@ __FASTRAMFUNC__ uint32_t micros()
{
#if CH_CFG_ST_RESOLUTION == 32 && CH_CFG_ST_FREQUENCY==1000000U
// special case optimisation for 32 bit timers
#ifdef AP_BOARD_START_TIME
#if defined(AP_INDUCE_PPM_OFFSET)
uint64_t now = st_lld_get_counter();
#if defined(AP_BOARD_START_TIME)
now += AP_BOARD_START_TIME;
#else
// Please note that this will induce incorrect
// clock drift after 71 minutes, use hrt_micros64()
// as "now" here if you want to avoid that
now += (now * AP_INDUCE_PPM_OFFSET) / 1000000U;
return now & 0xFFFFFFFF;
#endif
#elif defined(AP_BOARD_START_TIME)
return st_lld_get_counter() + AP_BOARD_START_TIME;
#else
return st_lld_get_counter();
Expand Down

0 comments on commit 911b594

Please sign in to comment.