/* Software-timer CALLBACK demo (Wokwi, ST Nucleo-C031C6)
*
* A FreeRTOS SOFTWARE TIMER fires beat() every 500 ms. The timer-service task
* "calls back" your function on a schedule — no hardware, no polling loop.
* Same callback idea as the button/ISR demo, but triggered by TIME, not a pin.
*
* REQUIRES: "STM32duino FreeRTOS" (Library Manager) AND configUSE_TIMERS == 1 in that
* library's FreeRTOSConfig.h. If the build fails with "undefined reference to
* xTimerCreate", timers are OFF in your build -> use sketch-fallback-ticker.ino instead.
* No wiring needed (Serial + on-board LED only).
*/
#include <STM32FreeRTOS.h>
#define LED_PIN LED_BUILTIN /* on-board LED (PA5) */
static uint32_t beats = 0;
void beat(TimerHandle_t t) { /* <-- THE CALLBACK. Keep it short; never block here. */
(void)t;
beats++;
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
Serial.print("timer callback fired, beat #"); Serial.println(beats);
}
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
TimerHandle_t hb = xTimerCreate(
"heartbeat", /* name */
pdMS_TO_TICKS(500), /* period: 500 ms */
pdTRUE, /* auto-reload -> periodic */
NULL, /* timer ID (unused) */
beat); /* the callback function */
xTimerStart(hb, 0); /* arm it */
vTaskStartScheduler(); /* the timer service task now runs beat() on schedule */
}
void loop() {} /* empty on purpose — the callback does the work */