Skip to content

Commit b8dc8c4

Browse files
Fix ta_wakes_up_on_or_before (earlephilhower#2190)
* Fix ta_wakes_up_on_or_before Handle target in the past and running for > 2^32 us. Fixes earlephilhower#2186 * Add test for ta_wakes_up_on_or_before fix
1 parent 61e2712 commit b8dc8c4

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/rp2_common/pico_time_adapter/include/pico/time_adapter.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ static inline void ta_set_timeout(alarm_pool_timer_t *timer, uint alarm_num, int
5555
}
5656

5757
static inline bool ta_wakes_up_on_or_before(alarm_pool_timer_t *timer, uint alarm_num, int64_t target) {
58-
uint32_t current = timer_time_us_32(timer_hw_from_timer(timer));
59-
uint32_t time_til_target = (uint32_t) target - current;
60-
uint32_t time_til_alarm = timer_hw_from_timer(timer)->alarm[alarm_num] - current;
58+
int64_t current = (int64_t)timer_time_us_64(timer_hw_from_timer(timer));
59+
int64_t time_til_target = target - current;
60+
uint32_t time_til_alarm = timer_hw_from_timer(timer)->alarm[alarm_num] - (uint32_t)current;
6161
return time_til_alarm <= time_til_target;
6262
}
6363

test/pico_time_test/pico_time_test.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ int issue_195_test(void);
7676
int issue_1812_test(void);
7777
int issue_1953_test(void);
7878
int issue_2118_test(void);
79+
int issue_2186_test(void);
7980

8081
int main() {
8182
setup_default_uart();
@@ -250,6 +251,8 @@ int main() {
250251

251252
issue_2118_test();
252253

254+
issue_2186_test();
255+
253256
PICOTEST_END_TEST();
254257
}
255258

@@ -311,8 +314,8 @@ int issue_1953_test(void) {
311314
repeating_timer_t timer1;
312315
repeating_timer_t timer2;
313316

314-
assert(add_repeating_timer_us(10, timer_callback_issue_1953, NULL, &timer1));
315-
assert(add_repeating_timer_us(100, timer_callback_issue_1953, NULL, &timer2));
317+
hard_assert(add_repeating_timer_us(10, timer_callback_issue_1953, NULL, &timer1));
318+
hard_assert(add_repeating_timer_us(100, timer_callback_issue_1953, NULL, &timer2));
316319

317320
int iterations = 0;
318321
while(iterations < 100) {
@@ -364,3 +367,13 @@ int issue_2118_test(void) {
364367
PICOTEST_END_SECTION();
365368
return 0;
366369
}
370+
371+
int issue_2186_test(void) {
372+
PICOTEST_START_SECTION("Issue #2186 defect - ta_wakes_up_on_or_before");
373+
374+
hard_assert(best_effort_wfe_or_timeout(get_absolute_time() - 1));
375+
hard_assert(best_effort_wfe_or_timeout(get_absolute_time() - 1)); // this will lockup without the fix - wfe which never happens
376+
377+
PICOTEST_END_SECTION();
378+
return 0;
379+
}

0 commit comments

Comments
 (0)