6 Event source ②: timer
6.1 SoftwareTimer = FreeRTOS software timer
The Adafruit nRF52 core’s SoftwareTimer is a class wrapping a FreeRTOS software timer.
#include "drv_timer.h"
#include "app_event.h"
#include <Arduino.h>
// SoftwareTimer is a FreeRTOS software timer.
// Callbacks are called inside the "timer task", not in an interrupt.
// Here too we only post events and leave processing to the main loop.
static SoftwareTimer s_timer_100ms;
static SoftwareTimer s_timer_1s;
static void on_100ms(TimerHandle_t)
{
event_post(EVT_TIMER_100MS);
}
static void on_1s(TimerHandle_t)
{
event_post(EVT_TIMER_1S);
}
void timer_init()
{
s_timer_100ms.begin(100, on_100ms);
s_timer_1s.begin(1000, on_1s);
s_timer_100ms.start();
s_timer_1s.start();
}| Item | Details |
|---|---|
| Where the callback is called | The timer service task (FreeRTOS’s “daemon task”). Not an interrupt |
| Resolution | FreeRTOS ticks (on this core 1 tick ≈ 0.98 ms, configTICK_RATE_HZ = 1024) |
| Repetition | begin(ms, cb) repeats by default. One-shot with false as the 4th argument |
All software timer callbacks are called in turn in the same single timer task. If one callback stalls for a long time, all the other timers are delayed too. Here too, just post events.
6.2 Difference from hardware timers
| Software timer | Hardware timer (TIMER / RTC peripheral) | |
|---|---|---|
| Accuracy | Tick resolution; may be delayed by task scheduling | Accurate to the clock |
| Callback | Inside a task (few restrictions) | Inside an ISR (many restrictions) |
| Count | Practically unlimited | As many as there are peripherals |
| Use | Periodic processing of tens of ms or more, timeouts | Sampling that needs high timing accuracy (e.g. a 1 kHz ECG) |
For reading a sensor every 100 ms, a software timer is sufficient. If you want equally spaced sampling at several hundred Hz or more, as with an ECG, use the sensor’s own FIFO and interrupts, or a hardware timer plus PPI (the nRF mechanism that connects peripherals to each other without going through the CPU).
6.3 How to make a 1-second period
There are two ways to build once-a-second processing.
- Provide an independent 1-second timer (this sample)
- In the 100 ms timer’s callback, count 10 times and emit a 1-second event
Method 2 guarantees that “aggregation happens exactly when ten 100 ms samples have accumulated”. With method 1, the phases of the two timers are independent, so the number of samples entering a 1-second aggregate can be 9 to 11 (look at n= in the measurement log).
- In the serial log during measurement, check whether
n=(samples per second) is always 10 - Rewrite
drv_timer.cppto method 2 (counting in the 100 ms callback) and see whethern=becomes stable
static void on_100ms(TimerHandle_t)
{
static uint8_t count = 0; // only touched from the timer task, so volatile is not needed
event_post(EVT_TIMER_100MS);
if (++count >= 10)
{
count = 0;
event_post(EVT_TIMER_1S);
}
}Because EVT_TIMER_100MS is posted before EVT_TIMER_1S, the queue order ensures aggregation happens after the 10th sample has been read.