SinelaboreRT Header Logo


It's better when it's simple!

User Tools

Site Tools


Modelling-Tool specific Intro

Getting started


Language Backends


Designers Toolbox

There are better ways to model state machines than using spread sheets!

In the past different μC manufacturers have published application notes about the benefit of using state machines for the design of embedded software. An example is the application note SLAA402 from Texas Instruments (TI). It suggests to generate source code based on a spread sheet table. Nowadays several affordable UML modeling tools are available supporting the efficient design of state machines in a graphical way. SinelaboreRT generates production quality source code from state diagrams created with many different UML tools. Give it a try!

Latest Changes


Generate production ready source code from UML state – and activity diagrams!

Build robust event-driven embedded real-time software based on hierarchical state machines.

SinelaboreRT focus is on generation of readable and maintainable C / C++ / Java / Python / C# / Lua and Swift code from hierarchical UML state machine diagrams - and activity diagrams. With its unique features the tool covers perfectly the requirements of embedded real-time and low-power application developers. As well as other application domains that can benefit from state machine modelling and code generation. A Payback is usually already given within a few hours.

 How does the codegen work

How does it work?

Use your favorite modeling tool and generate code from it with an easy-to-use command line tool. Or use the built editor to create state machines within minutes. Automatic model checks warn from design flaws. Configure the generation process according to your needs. Simulate your model. Generate trace code automatically if needed. All major state diagram features like hierarchical states, regions, history, sub-machines … are supported.

Key Features

  • Generated code has production-quality. It is based on nested switch/case and if/then/else statements. It is easy to read and understand. This means that if the worst comes to the worst, you can easily change or expand the code by hand.
  • There will be no problems when using static code analyzers.
  • Can be used with any 8-, 16- or 32-bit CPUs. There is no run-time environment needed.
  • Fits well in different system designs. The code generator does not dictate how you design your system. Therefore it is no problem to use the generated code in the context of a real-time operating system (VxWorks, FreeRTOS, embOS, RTEMS, …) or within an interrupt service routine or in a foreground / background system.
  • No gap between design and code. The documentation is always up to date.
  • Run the code generator and the built-in editor across key platforms (Windows, Linux, Mac, …)
  • Use the code generator only for those parts of your software that benefit from state machine modeling and code generation. Use your existing development environment for all the other code. The code-generator does not dictate an “all or nothing” approach as many other commercial tools.
  • Automatic robustness tests, test-case generation, tracing and simulation
  • Extensive manual with getting started section

How to get started?

To get an impression of the capabilities of the tool download the demo version. Checkout the examples folder to see the generated code. Read the manual and the “Getting Started” pages on this website. The manual contains also a basic introduction into state-machines in case you need a refresh. Read the sections related to your UML tool and the language backend you want to use.

Then you have basically two options.

  • Run the examples on your PC. The example folder contains examples for all supported modelling tools and various languages (C, C++, …). The examples realizes a microwave oven and can be executed and tested. Play with the model and enhance it. Regenerate the code and learn from the warning and error messages.
  • Run examples on a Micro-Controller e.g. a MSP430 evaluation board using Energia. An example with all details is available on github.

Customers and what customers say

The Sinelabore Code Generator is used world wide by very well known tier 1 companies and OEMs. But also small engineering companies as well as single developers.

“Sinelabore has helped me implement the behavior of a complex, asynchronous system. All the UML 2 elements I needed are available. I like that I don’t have to draw the state machine, then separately implement it and keep these two synchronized; this saves me time and reduces the potential of bugs. The error checking to make sure the state machine is valid is also useful. —- Daniel Bedrenko / Software Developer @ BPS…tec GmbH”

“Thank you again for providing such great tool!”

“We like Your Tool, infact we will give intro for another local company next week.”

Using State-Machines in (Low-Power) Embedded Systems

There are different ways how to integrate state machines in a specific system design. Some design principles are more applicable for developers of deeply embedded systems. Others more relevant for developers having not so tight resource constraints.

Using state machines in a main-loop

In this design an endless loop — typically the main function — calls one or more state machines after each other. Using state machines in a main-loop It is still one of the most common ways of designing small embedded systems. The event information processed from the state machines might come from global or local variables fed from other code or IRQ handlers. The benefits of this design are no need for a runtime framework and only little RAM requirements.

The consequences are:

  • All housekeeping code has to be provided by the designer
  • Main loop must be fast enough for the overall required response time
  • In case of extensions the timing must be carefully rechecked again


void main(void){

Using state machines in a main loop with event queue

This design is like the one presented above. But the state machine receives its events from an event queue. The queue is filled from timer events, other state machines (cooperating machines) or interrupt handlers. Using state machines in a main loop with event queue Benefits:

  • Events are not lost (queuing)
  • Event order is preserved
  • Decoupling of event processing from event generation.


  • A minimal runtime framework is required: Timers and Queues
  • Main loop must be fast enough for the overall required response time

A minimal runtime framework for C is available here:

It offers timers and queues. The intended usage is as follows:

  • Each state machine has an own event queue
  • Eventually a state machine requires one or more timers (single shot or cyclically).
  • A state machine can create as many timers as needed. When creating a timer the event queue of the state machine and the timeout event has to be provided. For different timers it makes sense to provide different timeout events.
  • To make the timer work, a tick counter variable has to be incremented cyclically from a timer interrupt (e.g., every 10 ms). The tick frequency should be selected based on the minimal required resolution of the timeout times.
  • A tick() function must be called in the main loop to check if any timer has expired. In case a timeout has happened the provided event is stored in the event queue of the state machine.
  • The main loop has to check if events are stored for a state machine in its queue. If there are new events they are pulled from the queue and the state machine is called with the event.

Example code with two state machines shows the general principle:

Example code with two state machines shows the general principle:

// tick irq
void tick(void){
void main(void){
  // create two queues for two state machines and init the timer subsystem
  fifoInit(&fifo2VendingMachine, fifo2VendingMachineRawMem, 8);
  fifoInit(&fifo2ProductStoreMachine, fifo2ProductStoreMachineRawMem, 8);
  while (1) {
    uint8_t evt;
    // Check if there are new events for the state machine. If yes,
    // call state machine with event.
    bool fifoEmpty = fifoIsEmpty(&fifo2VendingMachine);
    if (!fifoEmpty) {
      fifoGet(&fifo2VendingMachine, &evt);
      vending_machine(&vendingMachine, evt);
    fifoEmpty = fifoIsEmpty(&fifo2ProductStoreMachine);
    if (!fifoEmpty) {
      fifoGet(&fifo2ProductStoreMachine, &evt);
      product_store_sm(&productStoreMachine, evt);
   // any new timeouts?

As indicated in the figure above also other state machines or interrupt handlers might push events to the queue of a state machine. An example how to do this is shown below.

// add event evErr to a state machine queue.
void ISR_Btn1() {
  fifoPut(&fifo2VendingMachine, evErr);

Using state machines in a main loop with event queue, optimized for low power consumption

In low power system designs a key design goal is to keep the processor as long as possible in low power mode and only wake it up if something needs to be processed. The design is very similar to the one described above. The main difference is that the main loop runs not all time but only in case an event has happened. The timer service for the small runtime framework is handled in the timer interrupt.

A skeleton for the MSP430 looks as follows:

A skeleton for the MSP430 looks as follows:

void main(void){
  // init system
  while(1) {
    // check event queues and run the state machine as shown above
     __bis_SR_register(LPM3_bits + GIE);  // Enter low power mode once
     __no_operation();                    // no more events to process
// Timer A0 interrupt service routine. If the timer 
// function tick() returns true there
// is a timeout and we wakeup the main loop.
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A0(void)
  bool retVal=false;
  P1OUT |= BIT0; // toggle for debugging
  retVal = tick();
    // at least one timeout timer fired.
    // wake up main loop
  P1OUT &= ~BIT0; // toggle for debugging
  // no more events must be processed

Using state machines in interrupts

Sometimes state dependent interrupt handling is required. Then it is useful to embed the state machine directly into the interrupt handler to save every us. Typical usage might be the pre-processing of characters received by a serial interface. Or state dependent filtering of an analog signal before further processing takes place. Using state machines in interrupts Using state machines in an interrupt handler can be useful in any system design.

For code generation some considerations are necessary. Usually it is necessary to decorate interrupt handlers with compiler specific keywords or vector information , etc. Furthermore interrupt service handlers have no parameters and no return value. To meet these requirements the Sinelabore code generator offers the parameters StateMachineFunctionPrefixHeader, StateMachineFunctionPrefixCFile and HsmFunctionWithInstanceParameters.

The example below shows an interrupt service routine with the compiler specific extensions as required by mspgcc.

// generated state machine code for an irq
interrupt (INTERRUPT_VECTOR) IntServiceRoutine(void)
   /* generated statemachine code goes here */

To generate this code, set the key/value pairs in your configuration file the following way:

StateMachineFunctionPrefixCFile=interrupt (INTERRUPT_VECTOR)

If the prefix of the interrupt service routine requires to span more than one line the line break ’\n’ character can be inserted as shown below:

StateMachineFunctionPrefixCFile=#pragma vector=UART0TX_VECTOR\n__interrupt void

Prefixes for the header and the C file can be specified separately.

Using state machines with a real-time operating system

In this design each state machine usually runs in the context of an own task. The principle design is shown in the following figure.

Using state machines with a real-time operating system

Each task executes a state machine (often called active object) in an endless while loop. The tasks wait for new events to be processed from the state machine. In case no event is present the task is set in idle mode from the RTOS. In case one or more new events are available the RTOS wakes up the task. The used RTOS mechanism for event signaling can be different. But often a message queue is used. Events might be stored in the event queue from various sources. E.g. from within another task or from inside an interrupt service routine. This design can be realized with every real-time operating system. Only the event transport mechanisms might differ.


  • Efficient and well tested runtime environment provided from the real-time operating system
  • Prioritization of tasks, scheduling available
  • State machine processing times decoupled from each other.


  • Need of a real-time operating system (complexity, ram usage, cost …)

In the how-to section an example of this pattern is presented with FreeRTOS. The examples below shows code for the RTEMS and embOS.

Example code for RTEMS

Example code for RTEMS

// rtems specific task body
rtems_task oven_task(rtems_task_argument unused)
  for ( ; ; ) {
    // returns if one event was processed
// generated state machine code
extern rtems_id Queue_id;
uint8_t msg=NO_MSG;
size_t received;
rtems_status_code status;
void   oven(OVEN_INSTANCEDATA_T *instanceVar) {
  OVEN_EV_CONSUMED_FLAG_T evConsumed = 0U;
  /*execute entry code of default state once to init machine */
  if (instanceVar->superEntry == 1U) {
    instanceVar->superEntry = 0U;
  /* action code */
  /* wait for message */
  status = rtems_message_queue_receive(
             (void *) &msg,
  if ( status != RTEMS_SUCCESSFUL )
    switch (instanceVar->stateVar) {
      // generated state handling code}

Example code for embOS RTOS from Segger.

Example code for embOS RTOS from Segger.

// state machine instance
// Task and queue objects.
static OS_STACKPTR int Stack_TASK_1[128];   /* Task stacks */
static OS_TASK         TCB_TASK_1;         /* Task-control-blocks */
static OS_Q            MyQueue;
static char            MyQBuffer[100];
char txbuf[32] ;
// Routine called from the embOS RTOS to signal 
// a timeout. A timeout event is sent to the state
// machine. Multiple timer callback functions might be created if
// several timers are needed at the same time. Each one then fires an own
// event. E.g. ev50ms or ev100ms
static void MyTimerCallback(void) {
    uint8_t msg=evtTimeout;
    OS_Q_Put(&MyQueue, &msg, 1);
// Task blocked until a new event is present. The new event is 
// then sent to the state machine.
static void TaskRunningStateMachine(void) {
    char* pData;
    while (1) {
        // waiting for new event
        volatile int Len = OS_Q_GetPtr(&MyQueue, (void**)&pData);
        volatile char msg = *pData;
        sm(&instanceVar, (SM_EVENT_T)msg); // call generated state machine with event

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies

Leave your comments

Enter your comment:
start.txt · Last modified: 2021/11/13 14:34 by webmin