Skip to content
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

ta_set_timeout can fail to set an alarm #2127

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 3 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) {
peterharperuk marked this conversation as resolved.
Show resolved Hide resolved
timer_hw_from_timer(timer)->alarm[alarm_num] = (uint32_t) target;
}
}
Expand All @@ -75,6 +76,7 @@ static inline void ta_enable_irq_handler(alarm_pool_timer_t *timer, uint alarm_n

static inline void ta_disable_irq_handler(alarm_pool_timer_t *timer, uint alarm_num, irq_handler_t irq_handler) {
uint irq_num = timer_hardware_alarm_get_irq_num(timer, alarm_num);
timer_hw_from_timer(timer)->armed = 1u << alarm_num; // disarm the timer
hw_clear_bits(&timer_hw_from_timer(timer)->inte, 1u << alarm_num);
irq_set_enabled(irq_num, true);
irq_remove_handler(irq_num, irq_handler);
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");

alarm_pool_destroy(pool);
hard_assert(timer_hw->armed == 0); // check destroying the pool unarms its timer

set_sys_clock_hz(SYS_CLK_HZ, true);
setup_default_uart();

PICOTEST_END_SECTION();
return 0;
}
Loading