Skip to content

RP2040: Fix 64-bit uptime being wrapped as a 32-bit value whenever a timer event happens #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: extrapatches-6.17.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions targets/TARGET_RASPBERRYPI/TARGET_RP2040/us_ticker.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const ticker_info_t* us_ticker_get_info()
}

static const uint8_t alarm_num = 0;
static uint64_t last_read_u64 = 0;

static void us_ticker_irq_handler_internal(uint alarm_src) {
if (alarm_num == alarm_src) {
Expand All @@ -69,37 +70,39 @@ void us_ticker_init(void)

uint32_t us_ticker_read()
{
return time_us_32();
uint64_t now_u64 = time_us_64();

core_util_critical_section_enter();
last_read_u64 = now_u64;
core_util_critical_section_exit();

return now_u64;
}

void us_ticker_set_interrupt(timestamp_t timestamp)
void us_ticker_set_interrupt(timestamp_t timestamp_u32)
{
core_util_critical_section_enter();

uint64_t _timestamp = (uint64_t)timestamp;

if (timestamp < time_us_32()) {
//32 bit timestamp has been wrapped
//We need to provide a 64 bit timestamp able to fire the irq for this round
_timestamp = (((time_us_64() >> 32) + 1) << 32) + timestamp;
} else {
//Then, at the next round, wrap the 64 bit timer to follow the 32 bit one
if ((time_us_64() >> 32) > 0) {
uint64_t current_time = time_us_64();
uint64_t wrapped_time = current_time - 0xFFFFFFFF;
timer_hw->timelw = (uint32_t)wrapped_time;
timer_hw->timehw = 0;
}
uint32_t last_read_u32 = (uint32_t)last_read_u64;
uint64_t timestamp_u64 = (uint64_t)timestamp_u32 | (last_read_u64 & 0xFFFFFFFF00000000ULL);

if (timestamp_u32 < last_read_u32) {
timestamp_u64 += 1ULL << 32;
}

absolute_time_t target = { timestamp_u64 };
bool missed = hardware_alarm_set_target(alarm_num, target);

if (missed) {
us_ticker_fire_interrupt();
}
absolute_time_t target = { _timestamp };
hardware_alarm_set_target(alarm_num, target);

core_util_critical_section_exit();
}

void us_ticker_fire_interrupt(void)
{
us_ticker_irq_handler();
hardware_alarm_force_irq(alarm_num);
}

void us_ticker_disable_interrupt(void)
Expand Down
Loading