Sinelabore

Timers in State Machines#

Almost every embedded application needs time: a cyclic activity, a timeout, a blinking pattern, a checkback time. SinelaboreRT offers two ways to make a transition happen after some time. Both can be mixed in one project.

  • Modeled timeouts. The transition itself says when it fires: after(200ms) or every(1s). The code generator starts the timer when the state is entered and stops it when the state is left. The timing is visible in the diagram, and the model contains no timer calls. Available for C (-l cx) and C++ (-l cppx).
  • User-handled timeouts. Your timers send an ordinary event such as evTimeout to the machine, and the model starts and stops them in its action code. This works with every language and every timer implementation; see below.

The full description is in the section Time-Triggered Transitions of the manual (PDF).

A walkthrough from the model through the simulator to a main loop, FreeRTOS, AUTOSAR OS and embOS-Ultra is in the article From after(3s) in the diagram to the timer in the code.

Modeled timeouts#

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, usually on an internal transition

The duration t is a value with a unit: us, ms, s or min, e.g. 50us, 200ms, 1.5s, 2min. It can also be an expression of the target language, such as after(blinkTime ms). A guard can be added as on any transition: every(1s)[enabled].

The coffee machine below uses three timers. every(1s) prints a dot while it brews, a named timer brewTimer ends brewing after a time given by the variable brewTime, and after(3s) ends frothing. When brewTimer expires, a choice decides whether milk is added.

Coffee machine: Brewing with every(1s) and brewTimer: after(brewTime s), a choice with milk or not, Frothing with after(3s)

Download the model, coffee_machine.xml, and open it in the built-in editor (java -cp "codegen.jar" codegen.Main -E -p ssc -l cx -o coffee coffee_machine.xml) to simulate it yourself.

What the timers do#

  • A timer belongs to the source state of its transition. It starts when the state is entered, after the entry code, and stops when the state is left, by this transition or any other. Entering the state again starts it from zero.
  • every keeps its phase. A late step does not shift the following timeouts.
  • No event is lost to a timeout. By default a timeout is taken in a step of its own, so time triggers mix freely with events and conditional transitions. Inner states come first, as for every transition.
  • Each timed transition has its own timer, generated and numbered by the code generator. Nothing has to be declared.

Named timers#

A name in front of the trigger makes a named timer: cookTimer: after(3s). It belongs to the machine rather than to one transition and keeps a time of its own. The model can change that time in any state, and a state starts the timer with it.

In the model Effect
cookTimer: after(3s) on entry: set the time to 3 s and start
cookTimer: after() on entry: start with the time the timer keeps
cookTimer: increment(1s), decrement(1s), set(5s), stop in an action: change the time, or stop
TIME_LEFT(cookTimer) in an action or guard: the time left

Leaving the state keeps the time left. This gives a pause without any code in the model. In the microwave oven of the examples, + and - change the cooking time in every state. Opening the door leaves Cooking, and closing it cooks on with the time that was left.

How the timeout reaches the machine#

The generated code never runs a timer itself. It calls a small timer service to start and stop timers, and your application hands each expired timer back to the machine. There are two ways to do that, chosen with one setting:

  • As a condition (the default). The application tells the machine which timer expired, then calls it without an event, and the timed transition is taken. This fits a cyclic main loop.
  • As an event (TimeoutsAsEvents=yes). Each timer gets an event of its own, such as cookTimerTimeout, which the application sends like any other event: from its loop, from a timer callback through the task’s queue, or by hand in a test. This fits an RTOS task that waits on its queue.

Modeled timeouts are switched on with UseTimerService=yes in codegen.cfg, or with the checkbox Timer service in the built-in editor (Edit state machine code), next to Timeouts as events.

Timer services#

  • The reference timer service (C and C++, MIT licensed) is a table of the running timers that the main loop polls with the current time. It needs no RTOS and no lock, and it works in both modes.
  • FreeRTOS and Trampoline (AUTOSAR OS) services use the timers of the operating system, so the machine’s task needs no cycle: it sleeps until a timeout or an event arrives.
  • The SEGGER embOS-Ultra service runs each timer on an embOS software timer, for timeouts as events. The timer’s callback posts the timeout event into the machine task’s mailbox, beside the events of keys, buses and the like; an every timer posts once per period.
  • Your own service on any other timer facility implements a handful of functions: start, stop, and read or set the time left. The manual lists them, and the services above are good starting points.

All of them are in lib/timer_service of the examples on GitHub, with unit tests and a README.

Simulating timers in the built-in editor#

The simulation of the built-in editor runs the timers of the model on a model clock. Timers start and stop as in the generated code, and the timer list shows the time left of each one. The log shows every timeout with its model time. You can:

  • run the clock slower or faster than real time (0.1× to 10×), pause it, or jump to the next timeout;
  • let a timer expire now with a click on its row in the event list;
  • set the values the simulation cannot know, such as a duration given by a C variable, under Timer values….

The simulation does not run C code, so it cannot evaluate guards either. When a timeout leads into a choice or a guarded transition, it stops the clock and asks which branch to take.

The coffee machine above in the simulation (1.5 minutes, with sound): the variable brewTime is set, the clock runs at half speed, and the simulation asks at the choice.

Examples on GitHub#

All examples use the same microwave oven model and build on Linux and macOS.

Example Shows
microwave_builtin_editor_timer_c C, main loop with the reference timer service
microwave_builtin_editor_timer_cpp the same model generated as C++
freertos_posix_timer_c FreeRTOS software timers, timeouts as conditions
freertos_posix_timer_events_c FreeRTOS, timeouts as events through the task’s queue
autosar_trampoline_timer_c Trampoline (AUTOSAR OS) alarms, timeouts as conditions
autosar_trampoline_timer_events_c Trampoline, timeouts as events
embosultra_qemu_timer_events_c embOS-Ultra software timers, timeouts as events; keys from a receive interrupt and timeouts in one mailbox; watch it with embOSView

No target hardware is needed. The FreeRTOS and Trampoline examples run on the POSIX ports of these operating systems. The embOS-Ultra example runs on an STM32F405 (Cortex-M4) emulated by QEMU, in Docker; embOS-Ultra itself is downloaded from SEGGER and is not part of the example.

User-handled timeouts#

For languages other than C and C++, for existing designs, or for special timer needs, timers can be handled by your own code. Timers are created, started and stopped in the action code of the model. When a timer expires, your timer service puts a regular timeout event into the state machine’s event queue. A naming convention such as evTout... shows in the diagram which events come from timers.

In the following figure, a state machine uses two independent timers. One is a cyclic timer that loops between S11 and S12. A second, one-shot timer ends that loop after some time and stops the first one on the transition to S2.

State machine diagram with cyclic and single-shot timer use

  • RTOS designs. Every RTOS offers software timers. The timeout function puts the timeout event into the state machine’s queue. Worked examples are available for VxWorks, FreeRTOS and embOS.
  • Main loop designs. A hardware timer supplies the system tick. On each tick, a small software timer service checks for timeouts and queues the timeout events. See the low-power designs with MSP430 and PIC.

A note on UML time events#

UML defines relative time events (after) and absolute ones (at):

UML specification of after / at time events

SinelaboreRT supports relative time events, after, and adds every for periodic work and names for timers that are shared or changed by the model. Absolute times (at) are not supported. A timer started by the model and a timer service of your choice need no hidden specifications: everything the code needs is in the diagram.