Skip to content

Commit 7cba010

Browse files
committed
RP2040: us_ticker: fix missing interrupts after 32bit wrap
1 parent 0d1e7e4 commit 7cba010

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

targets/TARGET_RASPBERRYPI/TARGET_RP2040/us_ticker.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,18 @@ const ticker_info_t* us_ticker_get_info()
5353
return &info;
5454
}
5555

56-
const uint8_t alarm_num = 0;
56+
static const uint8_t alarm_num = 0;
57+
58+
static void us_ticker_irq_handler_internal(uint alarm_src) {
59+
if (alarm_num == alarm_src) {
60+
us_ticker_irq_handler();
61+
}
62+
}
5763

5864
void us_ticker_init(void)
5965
{
60-
hardware_alarm_set_callback(alarm_num, us_ticker_irq_handler);
66+
hardware_alarm_claim(alarm_num);
67+
hardware_alarm_set_callback(alarm_num, us_ticker_irq_handler_internal);
6168
}
6269

6370
uint32_t us_ticker_read()
@@ -68,8 +75,25 @@ uint32_t us_ticker_read()
6875
void us_ticker_set_interrupt(timestamp_t timestamp)
6976
{
7077
core_util_critical_section_enter();
71-
absolute_time_t target = {timestamp};
78+
79+
uint64_t _timestamp = (uint64_t)timestamp;
80+
81+
if (timestamp < time_us_32()) {
82+
//32 bit timestamp has been wrapped
83+
//We need to provide a 64 bit timestamp able to fire the irq for this round
84+
_timestamp = (((time_us_64() >> 32) + 1) << 32) + timestamp;
85+
} else {
86+
//Then, at the next round, wrap the 64 bit timer to follow the 32 bit one
87+
if ((time_us_64() >> 32) > 0) {
88+
uint64_t current_time = time_us_64();
89+
uint64_t wrapped_time = current_time - 0xFFFFFFFF;
90+
timer_hw->timelw = (uint32_t)wrapped_time;
91+
timer_hw->timehw = 0;
92+
}
93+
}
94+
absolute_time_t target = { _timestamp };
7295
hardware_alarm_set_target(alarm_num, target);
96+
7397
core_util_critical_section_exit();
7498
}
7599

@@ -90,4 +114,5 @@ void us_ticker_clear_interrupt(void)
90114

91115
void us_ticker_free(void)
92116
{
117+
hardware_alarm_unclaim(alarm_num);
93118
}

0 commit comments

Comments
 (0)