because tasks can be blocked to wait for availability of RTOS objects. Most RTOS have objects such as: ■ Message queue ■ First-in-first-out (FIFO) queue to pass data ■ Data can be sent by copy or by reference (pointer) ■ Used to send data between tasks or between interrupt and task
A Real-Time Operating System (RTOS) is a lightweight OS used to ease multitasking and task integration in resource and time constrained designs, which is normally the case in embedded systems. Besides, the term ‘real- time’ indicates predictability/ determinism in execution time rather than raw speed, hence an RTOS can usually be proven to satisfy hard real-time requirements due to its determinism.
Scheduler
Schedulers in RTOS control which task to run on the CPU, and different scheduling algorithms are available. Normally they are: ■ Pre-emptive – task execution can be interrupted if another task with higher priority is ready ■ Co-operative – task switch will only happen if the current running task yields itself Pre-emptive scheduling allows higher priority tasks to interrupt a lower task in order to fulfil real-time constraints, but it comes in the cost of more overhead in context switching. Inter-task communication (ITC) Multiple tasks will normally need to share information or events with each other. The simplest way to share is to directly read/write shared global variables in RAM, but this is undesirable due to risk of data corruption caused by a race condition. A better way is to read/ write file-scoped static variables accessible by setter and getter functions, and race conditions can be prevented by disabling interrupts or using a mutual exclusion object (mutex) inside the setter/getter function. The cleaner way is using thread-safe RTOS objects like message queue to pass information between tasks. Besides sharing of information, RTOS objects are also able to synchronize task execution
Semaphore ■ Can be treated as a reference counter to record availability of a particular resource ■ Can be a binary or counting semaphore ■ Used to guard usage of resources or synchronize task execution Mutex ■ Similar to binary semaphore, generally used to guard usage of a single resource (MUTual EXclusion) ■ FreeRTOS mutex comes with a priority inheritance mechanism to avoid priority inversion (condition when high priority task ends up waiting for lower priority task) problem Mailbox ■ Simple storage location to share a single variable ■ Can be considered as a single element queue Event Group ■ Group of conditions (availability of semaphore, queue, event flag, etc.) ■ Task can be blocked and can wait for a specific
■
Key concepts of RTOS are:
Task
Tasks (could also be called processes/threads) are independent functions running in infinite loops, usually each responsible for one feature. Tasks are running independently in their own time (temporal isolation) and memory stack (spatial isolation). Spatial isolation between tasks can be guaranteed with the use of a hardware memory protection unit (MPU), which restricts accessible memory region and triggers fault exceptions on access violation. Normally, internal peripherals are memory-mapped, so an MPU can be used to restrict access to peripherals as well. Tasks can be in different states: ■ Blocked – task is waiting for an event (e.g., delay timeout, availability of data/resources) ■ Ready – task is ready to run on CPU but not running because CPU is in use by another task ■ Running – task is assigned to be running on CPU
■
Real-Time Operating Systems (RTOS) and their applications Written by:
■
■
Lim Jia Zhi, Senior Embedded Software Engineer at DigiKey
we get technical
4
5
Powered by FlippingBook