0% found this document useful (0 votes)
489 views

RTOS Based Embedded System Design

This document discusses the basics of real-time operating system (RTOS) design. It describes how an RTOS kernel acts as an abstraction layer between hardware and applications, managing resources like memory, processes, scheduling, synchronization, and interrupts. The key functions of an RTOS kernel are task management, scheduling, synchronization, error handling, and memory management to provide deterministic and predictable timing for real-time applications.

Uploaded by

Sagar Dhapke
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
489 views

RTOS Based Embedded System Design

This document discusses the basics of real-time operating system (RTOS) design. It describes how an RTOS kernel acts as an abstraction layer between hardware and applications, managing resources like memory, processes, scheduling, synchronization, and interrupts. The key functions of an RTOS kernel are task management, scheduling, synchronization, error handling, and memory management to provide deterministic and predictable timing for real-time applications.

Uploaded by

Sagar Dhapke
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

RTOS based Embedded System

Design
Designing with RTOS

Operating System Basics


 The Operating System acts as a bridge between the user applications/tasks
and the underlying system resources through a set of system functionalities
and services
 OS manages the system resources and makes them available to the user
applications/tasks on a need basis
 The primary functions of an Operating system is
 Make the system convenient to use
 Organize and manage the system resources efficiently and correctly
User Applications
Application Programming
Interface (API)
Memory Management
Kernel Services

Process Management
Time Management
File System Management
I/O System Management
Device Driver
Interface
Underlying Hardware

The Operating System Architecture


Designing with RTOS

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

Kernel Space and User Space


 The program code corresponding to the kernel applications/services are kept
in a contiguous area (OS dependent) of primary (working) memory and is
protected from the un-authorized access by user programs/applications
 The memory space at which the kernel code is located is known as ‘Kernel
Space’
 All user applications are loaded to a specific area of primary memory and this
memory area is referred as ‘User Space’
 The partitioning of memory into kernel and user space is purely Operating
System dependent
 An operating system with virtual memory support, loads the user applications
into its corresponding virtual memory space with demand paging technique
 Most of the operating systems keep the kernel application code in main
memory and it is not swapped out into the secondary memory
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

modules in monolithic kernel architecture


allows the effective utilization of the low-level
features of the underlying system Monolithic kernel with all
 The major drawback of monolithic kernel is operating system services
running in kernel space
that any error or failure in any one of the
kernel modules leads to the crashing of the
entire kernel application
 LINUX, SOLARIS, MS-DOS kernels are
examples of monolithic kernel
Designing with RTOS

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

Types of Operating Systems


Depending on the type of kernel and kernel services, purpose and type of computing
systems where the OS is deployed and the responsiveness to applications, Operating
Systems are classified into
General Purpose Operating System (GPOS)
Operating Systems, which are deployed in general computing systems
The kernel is more generalized and contains all the required services to
execute generic applications
Need not be deterministic in execution behavior
May inject random delays into application software and thus cause slow
responsiveness of an application at unexpected times
Usually deployed in computing systems where deterministic behavior is not an
important criterion
Personal Computer/Desktop system is a typical example for a system where
GPOSs are deployed.
Windows XP/MS-DOS etc are examples of General Purpose Operating System
Designing with RTOS

Real Time Purpose Operating System (RTOS)

Operating Systems, which are deployed in embedded systems demanding real-


time response
Deterministic in execution behavior. Consumes only known amount of time for
kernel applications
Implements scheduling policies for executing the highest priority
task/application always
Implements policies and rules concerning time-critical allocation of a system’s
resources
Windows CE, QNX, VxWorks MicroC/OS-II etc are examples of Real Time
Operating Systems (RTOS
Designing with RTOS

The Real Time Kernel

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

Real Time Kernel – Task/Process Management


Deals with setting up the memory space for the tasks, loading the task’s code into
the memory space, allocating system resources, setting up a Task Control Block (TCB)
for the task and task/process termination/deletion. A Task Control Block (TCB) is
used for holding the information corresponding to a task. TCB usually contains the
following set of information
• Task ID: Task Identification Number
• Task State: The current state of the task. (E.g. State= ‘Ready’ for a task which is ready to execute)
• Task Type: Task type. Indicates what is the type for this task. The task can be a hard real time or soft
real time or background task.
• Task Priority: Task priority (E.g. Task priority =1 for task with priority = 1)
• Task Context Pointer: Context pointer. Pointer for context saving
• Task Memory Pointers: Pointers to the code memory, data memory and stack memory for the task
• Task System Resource Pointers: Pointers to system resources (semaphores, mutex etc) used by the task
• Task Pointers: Pointers to other TCBs (TCBs for preceding, next and waiting tasks)
• Other Parameters Other relevant task parameters
The parameters and implementation of the TCB is kernel dependent. The TCB parameters vary across
different kernels, based on the task management implementation
Designing with RTOS

• Task/Process Scheduling: Deals with sharing the CPU among various


tasks/processes. A kernel application called ‘Scheduler’ handles the task
scheduling. Scheduler is nothing but an algorithm implementation, which
performs the efficient and optimal scheduling of tasks to provide a
deterministic behavior.
• Task/Process Synchronization: Deals with synchronizing the concurrent
access of a resource, which is shared across multiple tasks and the
communication between various tasks.
• Error/Exception handling: Deals with registering and handling the errors
occurred/exceptions raised during the execution of tasks. Insufficient
memory, timeouts, deadlocks, deadline missing, bus error, divide by zero,
unknown instruction execution etc, are examples of errors/exceptions.
Errors/Exceptions can happen at the kernel level services or at task level.
Deadlock is an example for kernel level exception, whereas timeout is an
example for a task level exception. The OS kernel gives the information about
the error in the form of a system call (API).
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

You might also like