Programming embedded programs: RTOS – automating the context change

In this lesson, you’ll commence constructing your individual Minimal Actual-time Functioning Method (MiROS), where you will automate the context change.

In the very last lesson, you encountered the thought of a Actual-Time Working Process (RTOS), and you labored out a manual method of switching the context from just one thread to a further. In this lesson, you’ll commence constructing your own Negligible Actual-time Functioning Program (MiROS) [1], wherever you will automate the context switch.

Lesson 23 – RTOS: Automating the context swap

Let’s Pair-Program…

As I firmly believe that in learning by performing, I invite you to make the MiROS RTOS with me in the approach acknowledged as pair programming. We start out by adding a new group to the job with a header and resource documents. Then, we little by little fill in the facts, largely by copying, pasting, and modifying the snippets of code you experienced presently. By continuously getting two facet-by-side sights, we can more conveniently determine out the kernel API (Software Programming Interface) in a single look at though straight away seeking to use the API in the other. I hope you learn anything useful if you’ve under no circumstances labored like that.

The PendSV Exception

From the last lesson, you ought to bear in mind that the RTOS context switch “hijacks” the interrupt handling mechanism by now available in the CPU. Especially, by the conclude of an ISR (these as the SysTick_Handler in the video clip), you “trick” the CPU into returning to a unique thread than the a single preempted initially. For that, you’d have to include the context swap code to every ISR, which is inconvenient.

In ARM Cortex-M, ISRs can also nest (preempt each individual other), so only the return from the very last nested interrupt (back again to the thread amount) really should execute a context swap. The problem is that the order of ISR preemption changes dynamically at runtime, so you normally really do not know which one will be the very last.

An exquisite remedy employed in just about all RTOSs for ARM Cortex-M is to acquire gain of the same interrupt nesting system that established the issue in the 1st place. Especially, Cortex-M delivers the PendSV exception (Pend Assistance Contact) [2], which you can system to carry out the context swap and configure with the lowest interrupt priority (0xFF). When the RTOS detects the need to have to swap the context, it can pend the PendSV exception. The interrupt prioritization guarantees that PendSV will be the past ISR to run just just before returning to the thread level. In addition, the NVIC in ARM Cortex-M has a developed-in components optimization called “tail chaining,” which eliminates the overhead of exiting a person interrupt (e.g., SysTick) and getting into PendSV, so the context switch is executed with small overhead.

Coding the Context-Change in Assembly

The context swap in PendSV_Handler() are unable to be coded in common C due to the fact it needs to manipulate the Stack Pointer (SP) as very well as drive and pop CPU registers. But you can however leverage the C compiler to give you a fantastic setting up issue for your assembly programming. The video clip demonstrates how to code an “approximate” algorithm in C and then employ the produced equipment code from the disassembly perspective.

Close Notes

The MiROS kernel starts off to choose condition [1], but the scheduling is still carried out manually. In the following lesson, you will increase the MiROS RTOS with the clear-cut scheduling coverage identified as spherical-robin. That way, you will employ a time-sharing process on your TivaC LaunchPad. Continue to be tuned!

[1] Miro Samek, MiROS (Small Authentic-Time Operating Technique), GitHub

[2] Joseph Yiu, “The Definitive Manual to ARM Cortex-M3 and Cortex-M4 Processors, 3rd Edition”, ISBN:‎ 978-0124080829

Dr. Miro M. Samek is the creator of the open resource QP actual-time embedded frameworks and the freeware QM graphical model-dependent layout instrument. He is also the founder and CEO of Quantum Leaps — the provider of modern-day embedded application centered on energetic objects and hierarchical point out equipment as properly as applications for visible modeling, automatic code technology, and device testing of deeply embedded computer software. Miro teaches the common YouTube “Modern Embedded Devices Programming” video study course on which this article sequence is dependent.

Linked Contents:

For much more Embedded, subscribe to Embedded’s weekly electronic mail newsletter.

Related posts