I reduced this to a simple case from a larger project.
The pico-sdk is up-to-date with github. I build the code with either define BOARD as pico2_w or pico in CMakeLists.txt, depending on the whether I am using pico or pico2_w..
The code below fails after about 1000 alarm callbacks on a Pico2_w. It works for several hours on a Pico.
EDIT: on a later try, on a pico, the repeating alarm failed after 33050 callbacks. So the repeating alarm fails on both a pico and a pico2_w.
An earlier, similar bug was closed as complete.
What am I doing wrong?
/////////////////////////////////////////////////////////////////////////////
// includes
#include <stdio.h>
#include "pico/stdlib.h"
/////////////////////////////////////////////////////////////////////////////
// global variables
volatile int count;
/////////////////////////////////////////////////////////////////////////////
// repeating timer callback
bool repeating_timer_callback(struct repeating_timer *t) {
++count;
return true;
}
/////////////////////////////////////////////////////////////////////////////
// main
int main() {
stdio_init_all();
printf("\nTest repeating timer\n");
// Create a repeating timer
static struct repeating_timer timer;
#include <stdio.h>
#include "pico/stdlib.h"
/////////////////////////////////////////////////////////////////////////////
// global variables
volatile int count;
/////////////////////////////////////////////////////////////////////////////
// repeating timer callback
bool repeating_timer_callback(struct repeating_timer *t) {
++count;
return true;
}
/////////////////////////////////////////////////////////////////////////////
// blink the led
/////////////////////////////////////////////////////////////////////////////
// main
int main() {
stdio_init_all();
printf("\nTest repeating timer\n");
// Create a repeating timer
static struct repeating_timer timer;
// Negative delay so means we will call repeating_timer_callback,
// and call it again 500ms later regardless of how long the
// callback took to execute
add_repeating_timer_ms(-50, repeating_timer_callback, NULL, &timer);
//sleep_ms(1000); // allow time to setup usb tty and skip the first few expirations
while (1) {
if ((count > 0) && ((count %50) == 0)) {
printf("At time %ld, count %d\n", time_us_32(), count);
}
sleep_ms(100); // wait until at least 1 timer expirations
}
cancel_repeating_timer(&timer);
while (1); // do not run off the end ...
return 0;
}
add_repeating_timer_ms(-50, repeating_timer_callback, NULL, &timer);
while (1) {
if ((count > 0) && ((count %50) == 0)) {
printf("At time %ld, count %d\n", time_us_32(), count);
}
sleep_ms(100); // wait until at least 1 timer expirations
}
cancel_repeating_timer(&timer);
while (1); // do not run off the end ...
return 0;
}
The pico-sdk is up-to-date with github. I build the code with either define BOARD as pico2_w or pico in CMakeLists.txt, depending on the whether I am using pico or pico2_w..
The code below fails after about 1000 alarm callbacks on a Pico2_w. It works for several hours on a Pico.
EDIT: on a later try, on a pico, the repeating alarm failed after 33050 callbacks. So the repeating alarm fails on both a pico and a pico2_w.
An earlier, similar bug was closed as complete.
What am I doing wrong?
/////////////////////////////////////////////////////////////////////////////
// includes
#include <stdio.h>
#include "pico/stdlib.h"
/////////////////////////////////////////////////////////////////////////////
// global variables
volatile int count;
/////////////////////////////////////////////////////////////////////////////
// repeating timer callback
bool repeating_timer_callback(struct repeating_timer *t) {
++count;
return true;
}
/////////////////////////////////////////////////////////////////////////////
// main
int main() {
stdio_init_all();
printf("\nTest repeating timer\n");
// Create a repeating timer
static struct repeating_timer timer;
#include <stdio.h>
#include "pico/stdlib.h"
/////////////////////////////////////////////////////////////////////////////
// global variables
volatile int count;
/////////////////////////////////////////////////////////////////////////////
// repeating timer callback
bool repeating_timer_callback(struct repeating_timer *t) {
++count;
return true;
}
/////////////////////////////////////////////////////////////////////////////
// blink the led
/////////////////////////////////////////////////////////////////////////////
// main
int main() {
stdio_init_all();
printf("\nTest repeating timer\n");
// Create a repeating timer
static struct repeating_timer timer;
// Negative delay so means we will call repeating_timer_callback,
// and call it again 500ms later regardless of how long the
// callback took to execute
add_repeating_timer_ms(-50, repeating_timer_callback, NULL, &timer);
//sleep_ms(1000); // allow time to setup usb tty and skip the first few expirations
while (1) {
if ((count > 0) && ((count %50) == 0)) {
printf("At time %ld, count %d\n", time_us_32(), count);
}
sleep_ms(100); // wait until at least 1 timer expirations
}
cancel_repeating_timer(&timer);
while (1); // do not run off the end ...
return 0;
}
add_repeating_timer_ms(-50, repeating_timer_callback, NULL, &timer);
while (1) {
if ((count > 0) && ((count %50) == 0)) {
printf("At time %ld, count %d\n", time_us_32(), count);
}
sleep_ms(100); // wait until at least 1 timer expirations
}
cancel_repeating_timer(&timer);
while (1); // do not run off the end ...
return 0;
}
Statistics: Posted by tomdean — Wed Jan 28, 2026 6:34 am