RTOS Based Embedded System Design
RTOS Based Embedded System Design
Design
Designing with RTOS
Process Management
Time Management
File System Management
I/O System Management
Device Driver
Interface
Underlying Hardware
The Kernel
The kernel is the core of the operating system
It is responsible for managing the system resources and the communication
among the hardware and other system services
Kernel acts as the abstraction layer between system resources and user
applications
Kernel contains a set of system libraries and services. For a general purpose OS,
the kernel contains different services like
Process Management
Primary Memory Management
File System management
I/O System (Device) Management
Secondary Storage Management
Protection
Time management
Interrupt Handling
Designing with RTOS
Monolithic Kernel
All kernel services run in the kernel space
All kernel modules run within the same
memory space under a single kernel thread
The tight internal integration of kernel Applications
Microkernel
The microkernel design incorporates only the
essential set of Operating System services
into the kernel
Rest of the Operating System services are Servers (kernel
services running Applications
implemented in programs known as ‘Servers’ in user space)
which runs in user space
The kernel design is highly modular provides
OS-neutral abstraction
Microkernel with essential
Memory management, process management, services like memory
management, process
timer systems and interrupt handlers are management, timer system etc...
examples of essential services, which forms
the part of the microkernel
QNX, Minix 3 kernels are examples for
microkernel
Designing with RTOS
The kernel of a Real Time Operating System is referred as Real Time kernel. In
complement to the conventional OS kernel, the Real Time kernel is highly
specialized and it contains only the minimal set of services required for running
the user applications/tasks. The basic functions of a Real Time kernel are
– Task/Process management
– Task/Process scheduling
– Task/Process synchronization
– Error/Exception handling
– Memory Management
– Interrupt handling
– Time management
Designing with RTOS
Memory Management
The memory management function of an RTOS kernel is slightly different compared to
the General Purpose Operating Systems
In general, the memory allocation time increases depending on the size of the block of
memory needs to be allocated and the state of the allocated memory block (initialized
memory block consumes more allocation time than un-initialized memory block)
Since predictable timing and deterministic behavior are the primary focus for an RTOS,
RTOS achieves this by compromising the effectiveness of memory allocation
RTOS generally uses ‘block’ based memory allocation technique, instead of the usual
dynamic memory allocation techniques used by the GPOS.
RTOS kernel uses blocks of fixed size of dynamic memory and the block is allocated for a
task on a need basis. The blocks are stored in a ‘Free buffer Queue’.
Most of the RTOS kernels allow tasks to access any of the memory blocks without any
memory protection to achieve predictable timing and avoid the timing overheads
RTOS kernels assume that the whole design is proven correct and protection is
unnecessary. Some commercial RTOS kernels allow memory protection as optional and
the kernel enters a fail-safe mode when an illegal memory access occurs
Designing with RTOS
Memory Management
The memory management function of an RTOS kernel is slightly different compared to
the General Purpose Operating Systems
A few RTOS kernels implement Virtual Memory concept for memory allocation if the
system supports secondary memory storage (like HDD and FLASH memory).
In the ‘block’ based memory allocation, a block of fixed memory is always allocated for
tasks on need basis and it is taken as a unit. Hence, there will not be any memory
fragmentation issues.
The memory allocation can be implemented as constant functions and thereby it
consumes fixed amount of time for memory allocation. This leaves the deterministic
behavior of the RTOS kernel untouched
Designing with RTOS
Interrupt Handling
Interrupts inform the processor that an external device or an associated task requires immediate attention
of the CPU.
Interrupts can be either Synchronous or Asynchronous.
Interrupts which occurs in sync with the currently executing task is known as Synchronous interrupts.
Usually the software interrupts fall under the Synchronous Interrupt category. Divide by zero, memory
segmentation error etc are examples of Synchronous interrupts.
For synchronous interrupts, the interrupt handler runs in the same context of the interrupting task.
Asynchronous interrupts are interrupts, which occurs at any point of execution of any task, and are not in
sync with the currently executing task.
The interrupts generated by external devices (by asserting the Interrupt line of the processor/controller to
which the interrupt line of the device is connected) connected to the processor/controller, timer overflow
interrupts, serial data reception/ transmission interrupts etc are examples for asynchronous interrupts.
For asynchronous interrupts, the interrupt handler is usually written as separate task (Depends on OS
Kernel implementation) and it runs in a different context. Hence, a context switch happens while handling
the asynchronous interrupts.
Priority levels can be assigned to the interrupts and each interrupts can be enabled or disabled individually.
Most of the RTOS kernel implements ‘Nested Interrupts’ architecture. Interrupt nesting allows the pre-
emption (interruption) of an Interrupt Service Routine (ISR), servicing an interrupt, by a higher priority
interrupt.
Designing with RTOS
Time Management
Interrupts inform the processor that an external device or an associated task requires immediate
attention of the CPU.
Accurate time management is essential for providing precise time reference for all applications
The time reference to kernel is provided by a high-resolution Real Time Clock (RTC) hardware chip
(hardware timer)
The hardware timer is programmed to interrupt the processor/controller at a fixed rate. This timer
interrupt is referred as ‘Timer tick’
The ‘Timer tick’ is taken as the timing reference by the kernel. The ‘Timer tick’ interval may vary
depending on the hardware timer. Usually the ‘Timer tick’ varies in the microseconds range
The time parameters for tasks are expressed as the multiples of the ‘Timer tick’
The System time is updated based on the ‘Timer tick’
If the System time register is 32 bits wide and the ‘Timer tick’ interval is 1 microsecond, the System
time register will reset in
232 * 10-6/ (24 * 60 * 60) = 49700 Days =~ 0.0497 Days = 1.19 Hours
If the ‘Timer tick’ interval is 1 millisecond, the System time register will reset in
232 * 10-3 / (24 * 60 * 60) = 497 Days = 49.7 Days =~ 50 Days
Designing with RTOS
Time Management
The ‘Timer tick’ interrupt is handled by the ‘Timer Interrupt’ handler of kernel. The ‘Timer
tick’ interrupt can be utilized for implementing the following actions.
Save the current context (Context of the currently executing task)
Increment the System time register by one. Generate timing error and reset the
System time register if the timer tick count is greater than the maximum range
available for System time register
Update the timers implemented in kernel (Increment or decrement the timer
registers for each timer depending on the count direction setting for each register.
Increment registers with count direction setting = ‘count up’ and decrement registers
with count direction setting = ‘count down’)
Activate the periodic tasks, which are in the idle state
Invoke the scheduler and schedule the tasks again based on the scheduling algorithm
Delete all the terminated tasks and their associated data structures (TCBs)
Load the context for the first task in the ready queue. Due to the re-scheduling, the
ready task might be changed to a new one from the task, which was pre-empted by
the ‘Timer Interrupt’ task