Skip to content
0:00 / 0:00

#statemachines#fsm#hierarchicalstatemachines

#39 State Machines Part-5: Optimal Implementation in C

Quantum Leaps, LLC

74K subscribers • 20 videos on Hallway

YouTube
  • Recommended by 2 people: 2 in engineering communities.
  • A classic. People kept recommending it from 2022 to 2025.
  • Presented by Miro Samek, Founder of Quantum Leaps
  • Linked in the descriptionQuantumLeaps/modern-embedded-programming-course
  • Made by the vendor. The company whose product it covers made or sponsored it.

Intermediate. Assumes working knowledge.

28K views5 years ago2 recommendations

Systems programmingSoftware designCEmbedded systemsDesign patterns

Miro Samek demonstrates how to design a domain-specific language for state machines and implement it optimally in C.

This lesson continues the subject of STATE MACHINES. Today you will learn the "optimal" state machine implementation in C. You will start with designing a simple "domain-specific language" (DSL) for specifying state machines and then you will turn it into a C program. You will then generalize the implementation into a reusable "event processor" to include this optimal state machine implementation strategy directly in the Micro-C-AO active object framework that you've been building in this segment of lessons. This lesson includes quite a bit of coding with the intent to give you some "pair programming" experience as you witness the gradual transformation of the design into C. Also, you will see and compare other state machine DSL based on the SMC (State Machine Compiler). Finally, you will see a technique for measuring code execution speed on ARM Cortex-M CPUs by means of the Cycle Counter register in the DWT (Data Watchpoint and Trace) hardware block. This lesson comes with two projects: - the "optimal" implementation of the TimeBomb state machine and the updated uC/AO active object framework. - the implementation of the TimeBomb state machine based on the SMC (State Machine Compiler) ------- Resources: Companion web page for this video course: state-machine.com/quickstart GitHub repository for projects for this video course: github.com/QuantumLeaps/modern-embedded-programming-course Transcript of this lesson: state-machine.com/course/lesson39.txt Music credits: -------------- The background music comes from: bensound.com/royalty-free-music/track/straight

Why it's here

2 people recommend this

What people say. Highly recommended as part of an essential playlist explaining state machines and hierarchical state machines in embedded systems.

Moments people linked to

u/active-objectin r/embedded • Recommends

On “Do you use HSM (Hierarcical State Machines)?”

Yes, I've been using and advocating HSMs for the past 20 years. I've found them to be very useful in even the smallest embedded MCUs (e.g., see my article "UML Statecharts at $10.99") Regarding the overhead of ENTRY, EXIT, INIT, etc. it can be small, but you need the right HSM implementation in C or C++. (The boost::sml suggested in the other comment is NOT the right implementation for deeply embedded systems, like ARM Cortex-M MCUs). Here, I would recommend my YouTube video "Optimal State Machine Implementation in C". While you are at it, you might want to also watch the following YouTube videos about Hierarchical State Machines: * What is a Hierarchical State Machine? * [Automatic Co…

u/active-objectin r/embedded • Recommends

On “Looking for good c++ code”

Since you expressed some interest in state machines, maybe you can check out the \QP/C++ framework\. This is decent, mostly classic, not heavily templetized C++11. QP/C++ implements the \"Active Object" model of computation\. But among others, it inclues implementation of \Hierarchical State Machines\. Now, "should FSM be an object?", it depends on the implementation strategy. In QP/C++ a state machine is an object, but states are methods of the state machine class. In the GoF "State" design patterns, states are objects. I presened an overview of state machine implementation strategies in my free \video course\. Specifically, you might check out the \"State Machines" playlist\, and in t…

u/active-objectin r/embedded • Recommends

On “How to handle multitasking in baremetal?”

You can achieve a form of multi-threading in the venerable "superloop" (a.k.a., "main+ISRs") architecture, but the threads/tasks are very different than in the conventional RTOS. Specifically, tasks in the "superloop" are one-shot, run-to-completion (RTC) calls as opposed to endless loops of the conventional RTOS tasks. These RTC tasks need to run quickly and *return* without blocking, so they must often preserve the context (state) between the calls. This is where state machines come in. And here, there are the two primary types of state machines: 1. input-driven state machines (a.k.a., polled state machines) run "always" (i.e., as frequently as you call them to poll for events). Y…

u/NotBooleanin r/embedded • Recommends

On “Finite State Machines”

Quantum Leaps has got a lot of videos on this subject and shows a few methods. Here is one of their videos. I would checkout the rest of the channel.

u/active-objectin r/embedded • Recommends

On “State Machines in embedded?”

Here is the YouTube playlist that explains state machines in the context of embedded systems: * State Machines Part-1: What is a state machine? * State Machines Part-2: Guard conditions * State Machines Part-3: Input-Driven State Machines * State Machines Part-4: State Tables and Entry/Exit Actions * State Machines Part-5: Optimal Implementation in C * State Machines Part-6: What is a Hierarchical State Machine? * State Machines Part-7:Automatic Code Generation * State Machines Part-8: Semantics of Hierarchical State Machines

u/active-objectin r/embedded • Recommends

On “Is preemptive RTOS costing you too much?”

Of course, sequential problems are most naturally solved with sequential solutions (like polling, using blocking RTOS primitives, async/await, or protothreads). The problem is that I have yet to see a problem that would stay sequential after any deeper investigation, even if it starts that way. As you work on the problem and learn more about it, you always discover event sequences that you haven't considered (and hard-coded) in your design yet. For that reason, I prefer not to *pretend* that any problem is sequential. I agree with your observation about the loss of clarity in the event-driven approach (with or without statecharts). You just don't easily see the possible event sequences. How…

u/active-objectin r/embedded • Recommends

On “Event driven state machines”

You might want to watch the YouTube video "Optimal State Machine Implementation", which at timestamp 29:55 discusses the SMC (State Machine Compiler). The SMC is based on the State Pattern (in C, among others), but it is NOT optimal. There are simply much better, more readable and more performant implementations for state machines...

u/active-objectin r/embedded • Mentions

On “Optimal State Machine Implementation in C”

ROOM (Real-Time Object-Oriented Modeling) was ahead of its time. It consists of mainly two components: "ROOM-actors" and "ROOM-charts". Some variations of these two components do show up in various designs, but they are more re-inventions and adaptations rather than the original ROOM. For example, many elements of "ROOM-charts" have been adapted into UML state-machines. Overall I can't say that ROOM is widely used in embedded systems. But there are real gems in ROOM that are applicable and very beneficial in deeply embedded systems. These useful ideas are: * actors (a.k.a. active objects) as opposed to "naked threads", with strict encapsulation and no sharing of resources * asynchronous,…

u/active-objectin r/embedded • Mentions

On “Do you use HSM (Hierarcical State Machines)?”

Various DSLs for representing state machines (e.g., State Machine Compiler and similar) make only sense if the generated implementation in C is unreadable. However, the C implementation can be exactly as expressive as any DSL you can come up with. In that case, the direct C implementation **is** the specification and the indirection level of DSL and generators is redundant. After all, you perform text-to-text translation. For a discussion and comparison between a good C implementation and SMC (State Machine Compiler) as an example of a DSL, you can watch: "Optimal State Machine Implementation in C, comparison to SMC".
1View on Reddit

u/active-objectin r/embedded • Mentions

On “I wrote StateSmith with embedded devs (like me) in mind. It compiles diagrams to FSM C or C++. Below sim uses arduino, but any platform works. Would you use it? Why/not? Feedback welcome!”

It's great to see this level of interest in state machines for embedded software applications. The presented state machine implementation is perhaps workable, but it's not optimal. You might want to check out the video "Optimal State Machine Implementation in C". The code for this lesson is available on GitHub and is also licensed under Apache-2.0. There is also the whole playlist about state machines.