Embedded Systems Design (RTOS)
Computing Systems - Lecture 3 - Embedded Systems Design (RTOS)
Last Lecture Recap
The case of Embedded Systems:
Definitions and main features.
Modeling applications and the importance of concurrency.
Programming Computing Systems:
The design flow.
Typical bare metal design patterns.
This Lecture
Programming Embedded Systems:
Concepts of task, temporal control, priorities, and preemption.
Using a Real-Time Operating System.
Introduction to FreeRTOS:
The task management API.
Models of Computation (MoC)
MoC define:
Components: e.g., procedures, functions, finite state machines, tasks.
Communication between components: e.g., shared memory, asynchronous message passing, rendezvous communication.
Two common models:
Transformational: Single execution program. Input data transforms to output data.
Reactive: Recurrent (infinite) execution program. Data stream processing.
The Concept of Task
von Neumann models are based on sequences of instructions executed within tasks.
Task: An assigned piece of work often to be finished within a certain time.
Computations that have to be performed.
Task: Recurrent (possibly infinite) execution of a sequence of instructions.
Each execution is an instance or a job.
Task characteristics:
Code (instructions).
Recurrent activations (jobs).
Activation patterns:
Periodic.
Sporadic.
Aperiodic.
Tasks, Processes, and Threads
A task is a conceptual entity.
Implementation is typically done with:
Processes: Executing program (or part of) including memory content.
Entity of an operating system with its own dedicated address space (need MMU).
Threads: Executing program (or part of) sharing memory space.
Lightweight process (less context switching overhead).
A task always has:
Local variables (typically in the stack, possibly one stack per task).
Task control block (priority, period, deadline, pointer to code, pointer to stack).
Tasks, Processes, and Threads - Memory Management
Processes:
Processes "see" a dedicated and isolated memory partition (virtual memory concept) using a Memory Management Unit (MMU).
Threads:
Threads share a global memory space.
Requirements of an OS for Embedded Systems
Provide a suitable Model of Computation:
Support the right computation elements » (von Neumann MoC) Tasks / processes / threads
And the right communication and synchronization mechanisms » (von Neumann MoC) Messages / Pipes / locks / semaphores
Be reactive:
Respond to external (and internal) stimuli (events).
Common use of interrupts to signal events to threads or processes
Interrupt-service routines (ISR) → fast execution → keep short!
Use of priority and preemption → manage the response time to events
RTOS: Real-Time Operating Systems
Example Scenarios:
Non-preemptive event handling.
Preemptive execution based on priority.
Requirements of an OS for Embedded Systems - Configurability
Adaptation to the concrete platform (wide variety)
Object-orientation, Aspect-oriented programming,
Conditional compilation, Compile-time evaluation and optimization, Linker-based optimization
Include just device drivers that are needed
Possibly leaving drivers outside the OS → e.g., implemented as user programs or middleware
Protection mechanisms as needed, only
Tested SW may be assumed as reliable… (as long as operating conditions remain constant)
Microkernel approach (device drivers outside the kernel).
Monolithic approach (kernel includes device drivers).
Real-Time Operating Systems - RTOS
RTOS: Operating system that supports the creation of real-time systems.
Real-time systems: Systems in which the results of computations must be logically correct and produced in time.
Logical correctness.
Timeliness.
Requirements of an RTOS
Predictable timing behavior:
Minimal periods with interrupts disabled.
Low variability of execution times.
Schedule threads and processes:
Manage priorities, preemption, communication, and synchronization.
Manage time:
Provide internal time reference (possibly synchronizing with external/universal time).
Be fast → low overhead:
The OS is just a support SW layer, not the application!
Key Components:
Task management.
Time management.
Comm & Sync.
Device drivers.
System calls.
Current Embedded OS / RTOS
Many options exist for different application domains.
Safety-critical systems.
Internet-of-Things.
Wireless Sensor Networks.
Deeply embedded systems.
Introducing FreeRTOS
Almost 20 years old project.
Started in 2003, acquired by Amazon for its IoT services in 2017.
Market-leading position for small microcontrollers / microprocessors.
Distributed freely under the MIT open-source license.
Designed to be small, simple, portable, and easy to use.
Microkernel architecture:
The kernel offers services for scheduling, mutexes, semaphores, and software timers.
Device drivers, network stacks, testing & debugging are outside the kernel.
Fixed-priorities preemptive scheduler:
Real-time scheduling policies (allow determining response times).
FreeRTOS tasks in Arduino
Writing one task:
Processing done inside tasks, not "loop()" → loop() does nothing
Example:
void task_terminal( void *param ){
// param allows sending a parameter to the task when starting, frequently unused
// perform any task specific initializations here
// infinite loop - frequently you see this implemented as “for(;;)”
while (1){
Serial.println(“A”); // writes the A character in one line to the terminal
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait 1000 ms
}
// normally, tasks execute forever, if needed to exit, then use vTaskDelete()
vTaskDelete( NULL ); // ends and deletes the task itself
}
FreeRTOS tasks in Arduino - Creating Tasks
Allocation of a "context" (task control block + stack).
In Arduino, this is done inside "setup()".
Example:
```c++
setup() {
…
/* do this for each task to be created / xReturned = xTaskCreate( taskterminal, / Function that implements the task */
(const portCHAR *) “TaskTer