Programming embedded methods: Foreground-background structure (“superloop”)

Programming embedded methods: Foreground-background structure (“superloop”)

The superloop fundamental structure is necessary in itself and is a stepping stone to understanding all different, extra superior architectures, such because the Actual-Time Working System (RTOS).

This episode begins a brand new group of classes in regards to the structure and design of embedded software program. At the moment’s topic is the ever-present foreground-background structure, often known as “superloop” or “important+ISRs.” This fundamental structure is necessary in itself and is a stepping stone to understanding all different, extra superior architectures, such because the Actual-Time Working System (RTOS).

Lesson 21 – Foreground-Background Structure (“Superloop”)

Foreground-Background Structure

Because the identify suggests, the structure consists of two important elements: the limitless, whereas(1){…} background loop inside the primary() perform and the interrupt service routines (ISRs) comprising the foreground [1]. The ISRs operating within the foreground (e.g., SysTick_Handler() within the video) preempt the background loop, however they all the time return to the purpose of preemption. The 2 elements of the system talk via shared variables (e.g., l_tickCtr within the video.) To keep away from race circumstances attributable to asynchronous ISRs, these shared variables have to be protected by briefly disabling interrupts round any entry to them from the background.

Blocking Background Loop

The background loop could be structured in some ways. The “Blinky” instance initially of the video demonstrates the only sequential construction (additionally used within the Arduino Blink instance [2]). The implementation relies on the BSP_delay() perform, whose entire objective is to block the background loop (stop it from progressing).

Such a code construction known as sequential as a result of the sequence of anticipated occasions is hard-coded within the blocking calls, every explicitly ready for a selected occasion. For instance, after turning the LED on, the blocking name BSP_delay(BSP_TICKS_PER_SEC/4) waits for a timeout occasion in 1/4 second, and after turning the LED off, one other blocking name BSP_delay(BSP_TICKS_PER_SEC*3/4) waits for a timeout occasion in 3/4 seconds.

Sequential code is easy, however it’s tough to increase. A background loop clogged with blocking calls executes slowly (solely as soon as per second within the case of Blinky). Consequently, any occasion requiring a sooner response can’t be well timed dealt with by such a loop.

Non-Blocking Background Loop

Nevertheless, it is usually attainable to construction the background loop with out blocking, which is demonstrated within the “Blinky” instance on the finish of the video (see additionally Arduino “Blink With out Delay” [3]). On this case, the background loop consistently checks for the timeout occasions relying on the present state of the LED (both ON_STATE or OFF_STATE within the video). Solely when the appropriate timeout occasion is detected the loop performs acceptable actions, corresponding to turning the LED on or off, once more relying on the state. Such a non-blocking code construction known as event-driven within the video.

Probably the most important benefit of event-driven code is extensibility as a result of an occasion loop can deal with any occasion sequence. The worth for it’s that event-driven code is far much less “apparent” than sequential as a result of the anticipated occasion sequence is now not readily seen (no blocking calls!). Additionally equally necessary is {that a} non-blocking background loop spins shortly (a number of thousand instances per second within the case of “Blinky”), so it could simply deal with any new occasions, together with these requiring a quick response.

Finish Notes

The fully-blocking and fully non-blocking background loops kind an entire spectrum of foreground-background architectures. In apply, background loops fall someplace between these two beliefs. A typical background loop usually begins with a easy sequential construction however turns into more and more “event-driven” as new occasions are added, and the timing via the loop must be shortened. This usually ends with the worst of each worlds—the sequence of occasions turning into now not readily seen however not fairly extensible both, attributable to being clogged with occasional blocking calls. The makes an attempt to forestall such architectural decay lead in two instructions: Efforts to avoid wasting the sequential construction result in the Actual-Time Working System (RTOS), which would be the topic of the following phase of classes. Makes an attempt to forestall the “spaghetti code” ensuing from the improvised “event-driven” method result in event-driven programming, state machines, and lively objects, which I will even clarify in future installments. Keep tuned!

[1] Jean Labrosse, “µC/OS-III: The Actual-Time Kernel” (PDF obtain)

[2] Arduino, “Blink” instance (sequential model with blocking)

[3] Arduino, “Blink With out Delay” instance (event-driven model with out blocking)

Programming embedded methods: Foreground-background structure (“superloop”)Dr. Miro M. Samek is the creator of the open supply QP real-time embedded frameworks and the freeware QM graphical model-based design device. He’s additionally the founder and CEO of Quantum Leaps — the supplier of recent embedded software program primarily based on lively objects and hierarchical state machines in addition to instruments for visible modeling, automated code era, and unit testing of deeply embedded software program. Miro teaches the favored YouTube “Trendy Embedded Techniques Programming” video course on which this text sequence relies.

Associated Contents:

For extra Embedded, subscribe to Embedded’s weekly e mail publication.