From after(3s) in the diagram to the timer in the code#
Almost every state machine in an embedded system needs time: a timeout while waiting for a checkback signal, a blinking pattern, a cyclic job while a state is active. In hand-written code that means a timer started in one place and stopped in several others. A forgotten stop on one exit path leaves a timer running into a state it no longer belongs to. A timeout that was already queued when the state changed arrives late and triggers a transition it was never meant for. And every RTOS has its own timer API, so the timing logic gets tied to the platform.
SinelaboreRT moves the timing into the state diagram and the platform into a small timer service. The diagram says when, the generated code decides which timer starts and stops, and a service of five functions maps that onto whatever timers the platform has. The same generated code then runs in a bare-metal main loop, on FreeRTOS, on an AUTOSAR OS and on SEGGER embOS-Ultra.
Time in the diagram#
A time trigger is written as the event of a transition:
| Trigger | Meaning |
|---|---|
after(t) |
fires once, t after the source state was entered |
every(t) |
fires every t as long as the source state is active |
brewTimer: after(3s) |
a named timer: it belongs to the machine and keeps its time |
t is a value with a unit (50us, 200ms, 1.5s, 2min) or an expression of the target
language, such as after(brewTime s). A guard works as on any transition:
every(1s)[enabled].
Test it in the simulator, timers included#
Before any code is generated, the machine can be tried out in the simulator of the built-in
editor. It runs the timers on a model clock: faster or slower than real time, paused, or
straight to the next timeout. The timer table shows the time left of each timer, and a
timeout can be fired on a click. Values the simulator cannot know, such as brewTime, are set
by hand.

The simulator does not run C code, so it cannot evaluate guards. When brewTimer expires
into the choice, it stops the clock and asks which branch to take, with milk or without. The
event coverage shows how much of the machine the test has reached so far. Timing errors in the model show
up here, long before the code runs on a target.
What the generator writes#
The generated code starts and stops the timers itself. When the coffee machine enters
Brewing, the entry code switches the heater on and starts both timers of that state
(arguments shortened):
/* OnEntry code of state Brewing */
heaterOn();
tsStart(..., COFFEE_TM_BREWING_0, TS_S(1U), TS_EVERY); /* every(1s) */
tsStart(..., COFFEE_TM_BREWTIMER, tsS(brewTime), TS_ONCE); /* brewTimer: after(brewTime s) */Every way out of Brewing stops them again, and a timeout that arrives after the state was
left is ignored. The diagram itself contains no timer calls.
tsStart belongs to the timer service: five functions to start, stop and stop all timers,
and to read or set the time left. They are the only part that depends on the platform. How an
expired timer gets back into the machine is one setting in codegen.cfg.
As a condition, for a cyclic main loop. The loop asks the service which timers have expired and hands them to the machine:
n = tsPoll(&timers, now, expired, COFFEE_TM_COUNT);
for(i = 0U; i < n; i++){
coffeeSetTimeout(&coffee, expired[i].timerId);
}The reference service behind this is plain C99 or C++11: a table of running timers, no heap, no threads, no interrupts. All it needs from the platform is the current time.
As an event (TimeoutsAsEvents=yes in addition), for an RTOS task that blocks on its
queue. Each timer gets an event of its own, such as brewTimerTimeout, and the service calls a
function of yours when a timer expires. On embOS-Ultra that function runs in the timer’s
callback and posts the event into the machine task’s mailbox:
static void onTimeout(void *context, TS_TIMER_ID_T timerId)
{
COFFEE_EVENT_T ev = coffeeTimeoutEvent(timerId);
(void)OS_MAILBOX_Put((OS_MAILBOX *)context, &ev);
}Timeouts now queue up beside button presses, bus messages and everything else, and the machine task handles them one at a time, in the order they arrived. The task sleeps until something happens; there is no polling cycle.
The services#
Each service is a folder with its own timer_service.h, with the same names and meanings. The
generated code compiles unchanged against any of them; the include path chooses.
| Service | Timers used | Timeouts as |
|---|---|---|
| reference (C and C++) | a table polled by your loop | conditions or events |
freertos / freertos_events |
FreeRTOS software timers | task notification bits / events in the task’s queue |
trampoline / trampoline_events |
AUTOSAR OS / OSEK alarms (Trampoline) | task events / events through a small task per timer |
embos_ultra_events |
SEGGER embOS-Ultra software timers | events in the task’s mailbox |
The services differ where their RTOS differs, and the README says how. On overrun, for example, a late FreeRTOS timer task delivers each missed period, Trampoline drops one, and embOS-Ultra’s callbacks are never late because they run above every task.
For any other timer facility (a hardware timer, Zephyr, ThreadX, a simulated clock in a host test), you copy the header closest to your platform and implement the five functions. The README lists the three rules an implementation has to keep; the most important one: a stopped or restarted timer must never deliver a timeout it set before.
Try it: the examples on GitHub#
The examples use a close relative of the coffee machine, a microwave oven: a named cooking
timer set with + and -, a door that pauses cooking, and a dot every second while it cooks.
They build on Linux and macOS, and none needs target hardware.
| Example | Platform |
|---|---|
| microwave_builtin_editor_timer_c | C, main loop, reference service |
| microwave_builtin_editor_timer_cpp | the same model generated as C++ |
| freertos_posix_timer_c | FreeRTOS (POSIX port), timeouts as conditions |
| freertos_posix_timer_events_c | FreeRTOS, timeouts as events through the task’s queue |
| autosar_trampoline_timer_c | Trampoline (AUTOSAR OS), timeouts as conditions |
| autosar_trampoline_timer_events_c | Trampoline, timeouts as events |
| embosultra_qemu_timer_events_c | embOS-Ultra on an STM32F405 emulated by QEMU, in Docker; keys from a UART interrupt; observable with embOSView |
The generated code is committed in every example, so they build without the code generator. The timer services are in lib/timer_service, with unit tests and API documentation. The embOS-Ultra example needs the embOS-Ultra download from SEGGER, which is not part of it. To generate code from your own models, a demo version of SinelaboreRT is on the download page.
The timers page has a short film of the simulator running the coffee machine.
Summary#
The diagram holds the timing, where reviewers can see it. The generator holds the rules that are easy to get wrong by hand: start on entry, stop on every exit, ignore what is stale. A five-function service holds the platform. Moving from a main loop to an RTOS, or from one RTOS to another, means choosing another folder on the include path. The state machine and its generated code stay as they are.