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

add_repeating_timer_ms() along with attachInterrupt() causes the Pico to hang #2776

Closed
jaszczurtd opened this issue Jan 23, 2025 · 1 comment

Comments

@jaszczurtd
Copy link

This simple code snippet:

int interruptValue;
bool repeating_timer_callback(repeating_timer_t *rt) {
interruptValue++;
return true;
}

void interruptInit() {

noInterrupts();
repeating_timer_t timer;
add_repeating_timer_ms(100, repeating_timer_callback, NULL, &timer);
interrupts();

}

...doesn't work. Exactly: it causes a pico to be blocked and reset by the watchdog. The documentation has almost no information about interrupts – though that may be understandable, since in the context of attachInterrupt(), the topic is simple and doesn’t require special handling. Should I understand that it's not possible to use the interrupt generation function with the pico timer, or did I do something incorrectly? I tested the code on SDK version 4.2.1 and 4.4.2 – same result. In the code, I generally use attachInterrupt() assigned to several pins. I set the interrupts from the core0 context.

@earlephilhower
Copy link
Owner

void interruptInit() {
  noInterrupts();
  repeating_timer_t timer;   // **illegal, disappears
  add_repeating_timer_ms(100, repeating_timer_callback, NULL, &timer);
  interrupts();
}

timer needs to live for the life of the timer, but you've placed it on the stack. That's not legal, you can't access that data after exiting the function. Instead, move timer to a global or heap allocation. When the timer IRQ fires it will try and access some region of the stack that's been overwritten (and in use) by other routines and boom...

From the SDK docs...

<timer> the pointer to the user owned structure to store the repeating timer info in. BEWARE this storage location must outlive the repeating timer, so be careful of using stack space 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants