8000 ta_set_timeout can fail to set an alarm by peterharperuk · Pull Request #2127 · raspberrypi/pico-sdk · GitHub
[go: up one dir, main page]

Skip to content

ta_set_timeout can fail to set an alarm #2127

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

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
8000
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
ta_set_timeout can fail to set an alarm
If alarm_pool_irq_handler takes <1us between handling an alarm and
calling ta_set_timeout then no alarms will be set as 
8000
it will appear as
if an earlier alarm is already armedi before the target time.

Make sure ta_set_timeout always leaves with an alarm set by checking the
armed status.

Fixes #2118
  • Loading branch information
peterharperuk committed Dec 5, 2024
commit 4b1b18fafdf7373d8300d0dd97bd63a408f36afc
3 changes: 2 additions & 1 deletion src/rp2_common/pico_time_adapter/include/pico/time_adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ static inline void ta_set_timeout(alarm_pool_timer_t *timer, uint alarm_num, int
// 2. If we just passed the target time, then time_til_target will be high, meaning we will
// likely not do the update, but this is OK since the caller who has the full 64 bits
// must check if the target time has passed when we return anyway to avoid races.
if (time_til_target < time_til_alarm) {
// 3. We should never leave here without an alarm being armed
if (time_til_target < time_til_alarm || (timer_hw_from_timer(timer)->armed & (1 << alarm_num)) == 0) {
timer_hw_from_timer(timer)->alarm[alarm_num] = (uint32_t) target;
}
}
Expand Down
39 changes: 39 additions & 0 deletions test/pico_time_test/pico_time_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdlib.h>
#include <string.h>
#include <hardware/sync.h>
#include "hardware/clocks.h"
#include "pico/stdlib.h"
#include "pico/test.h"
// Include sys/types.h before inttypes.h to work around issue with
Expand Down Expand Up @@ -74,6 +75,7 @@ static bool repeating_timer_callback(struct repeating_timer *t) {
int issue_195_test(void);
int issue_1812_test(void);
int issue_1953_test(void);
int issue_2118_test(void);

int main() {
setup_default_uart();
Expand Down Expand Up @@ -246,6 +248,8 @@ int main() {

issue_1953_test();

issue_2118_test();

PICOTEST_END_TEST();
}

Expand Down Expand Up @@ -325,3 +329,38 @@ int issue_1953_test(void) {
PICOTEST_END_SECTION();
return 0;
}

static int counter_2118;
static bool timer_callback_issue_2118(repeating_timer_t *rt) {
counter_2118++;
return true;
}

int issue_2118_test(void) {
PICOTEST_START_SECTION("Issue #2118 defect - failure to set an alarm");

// this problem only happens when running the clock fast as it requires the time between
// alarm_pool_irq_handler handling an alarm and setting the next alarm to be <1us
set_sys_clock_hz(200 * MHZ, true);
setup_default_uart();

alarm_pool_t *pool = alarm_pool_create(2, 1);
repeating_timer_t timer;
alarm_pool_add_repeating_timer_ms(pool, -20, timer_callback_issue_2118, NULL, &timer);

int iterations = 0;
while(iterations < 100) {
iterations++;
sleep_ms(20);
}
PICOTEST_CHECK(counter_2118 >= 100, "Repeating timer failure");

cancel_repeating_timer(&timer);
alarm_pool_destroy(pool);

set_sys_clock_hz(SYS_CLK_HZ, true);
setup_default_uart();

PICOTEST_END_SECTION();
return 0;
}
Loading
0