Real Time Operating System For Embedded Systems
Rajan Singh
M.E EXTC Sem1(2015-2017)
kumar.rajan1812@gmail.com
Abstract— An embedded system is a special-purpose computer
system designed to perform one or a few dedicated functions,
often with real-time computing constraints. Embedded systems
contain a processor, software and Memory and The processor
may be 8051 micro-controller or a Pentium-IV processor,
Memory ROM and RAM respectively Memory Input Processor
Output.
Index Terms— freeRTOS, ARM LPC2148X, Task, Scheduling,
Semaphore, mutex .
INTRODUCTION (RTOS)
An RTOS is an OS for response time-controlled and event-
controlled processes. It is very essential for large scale
embedded systems. RTOS occupy little space from 10 KB to
100KB The main task of a RTOS is to manage the resources
of the computer such that a particular operation executes in
precisely the same amount of time every time it occur.
When RTOS is not necessary?
For small-scaled systems, RTOS’s function can be replaced by C.
For example, instead of the memory allocation and de-allocation
functions of RTOS, the C function , 'melloc' and 'free' can be used.
Software can directly handle inter-process communication.
3. When RTOS is necessary?
However, RTOS is essential when… A common and effective way
of handling of the hardware source calls from the interrupts I/O
management with devices, files, mailboxes becomes simple using an
RTOS Effectively scheduling and running and blocking of the tasks
in cases of many tasks and many more….. In conclusion, an RTOS
may not be necessary in a small-scaled embedded system. An RTOS
is necessary when scheduling of multiple processes and devices is
important.
THEORY
Hard Real time system: Failure to meet such a dead line is
considered to be a fatal fault and will lead to disastrous consequences
e.g. Response of the break system of a speeding Train approaching a
Red Signal to the stop command must come before the train crosses
the Red signal.
Soft Real time system : Failure to miss a Soft Deadline is
undesirable but a few misses does no serious harm like occasional
delay in an on line trading of stocks. However the system’s overall
performance becomes poorer and poorer as more and more jobs starts
missing the deadline.
The most important component of RTOS is its kernel (Monolithic &
Microkernel). BSP or Board Support Package makes an RTOS
target-specific (It’s a processor specific code onto (processor) which
we like to have our RTOS running).
RTOS Kernel Functions:
1. Task Management2. Intertask Communication & Synchronization
3. Dynamic Memory Allocation 4. Timers5. Devices I/O Supervisor*
Task States of RTOS :--
WORKING
Task Management:
Set of services used to allow application software developers to
design their software as a number of separate chunks of software
each handling a distinct topic, a distinct goal, and sometimes its own
real-time deadline. Main service offered is Task Scheduling controls
the execution of application software tasks  can make them run in a
very timely and responsive fashion.
RTOS perform priority-based pre emptive task scheduling. Basic
rules for priority based pre emptive task scheduling  The Highest
Priority Task that is Ready to Run, will be the Task that Must be
Running.
Priority based Preemptive Task Scheduling: Every Task in a software
application is assigned a priority. Higher Priority = Higher Need for
Quick Response.
Nested Preemption Timeline for Priority-based Preemptive
Scheduling.
Inter task Communication &Synchronization These services makes
it possible to pass information from one task to another without
information ever being damaged. Makes it possible for tasks to
coordinate & productively cooperate with each other.
Inter-Task communication &Synchronization The most important
communication b/w tasks in an OS is the passing of data from one
task to another. Message Producer Task Receiver Task If messages
are sent more quickly than they can be handled, the OS provides
message queues for holding the messages until they can be
processed.
Message passing in RTOS In RTOS, the OS copies a pointer to the
message, delivers the pointer to the message-receiver task, and then
deletes the copy of the pointer with message-sender task. Message
Sender RAM Task Message Message Message
Example of Rtos:-
freeTOS, VxWorks, pSOS OS QNX •ETLinux • VRTX •uCLinux •uCOS
•uLinux (muLinux) SymbianOS distro fits on a single floppy
Mutexes :
Mutex means mutual exclusion A mutex is a synchronization object that
can have only two states. They are not-owned and owned. Two operations
are defined for mutexes
Lock: This operation attempts to take ownership of a mutex, if the mutex
is already owned by another thread then the invoking thread is queued.
Unlock: This operation relinquishes ownership of a mutex. If there are
queued threads then a thread is removed from the queue and resumed,
ownership is implicitly assigned to the thread.
Interrupt processing using Semaphore mutex
Priority-ceiling mutex locking and unlocking
uint8_t SST_mutexLock(uint8_t
prioCeiling) {
uint8_t p;
SST_INT_LOCK();
p = S_currPrio_; /* save
the original SST priority to return */
if (prioCeiling > SST_currPrio_) {
SST_currPrio_ = prioCeiling; /*
set the SST priority to the ceiling */
}
SST_INT_UNLOCK();
return p;
}
/*.........................................
.................................*/
void SST_mutexUnlock(uint8_t orgPrio) {
SST_INT_LOCK();
if (orgPrio < SST_currPrio_) {
SST_currPrio_ = orgPrio; /*
restore the saved priority to unlock */
SST_schedule_(); /* the
scheduler unlocks the interrupts internally
*/
}
SST_INT_UNLOCK();
}
Unlike the simple INT_LOCK macros, the SST mutex interface
allows locks to be nested, because the original SST priority is
preserved on the stack. Above example shows how the mutex is
used in the SST example to protect the non-reentrant DOS
random number generator calls inside the clock-tick
tasks tickTaskA() and tickTaskB().
Priority Inversion Problem In any real time embedded system ,if
a high priority task is blocked or waiting and a low priority task is
running or under execution ,this situation is called Priority
Inversion. This priority Inversion is shown in the diagram below.
In Scheduling, priority inversion is the scenario where a low priority
Task holds a shared resource that is required by a high priority task.
This causes the execution of the high priority task to be blocked
until the low priority task releases the resource, effectively
“inverting” the relative priorities of the two tasks.
Suppose some other medium priority task, one that does not depend
on the shared resource, attempts to run in the interim, it will take
precedence over both the low priority task and the high priority task.
The consequences of the priority Inversion are
(i) Reduce the performance of the system (ii) May reduce the
system responsiveness
which leads to the violation of response time guarantees (iii) Create
problems in real-time systems.
There are two types of priority inversions.(i) Bounded and
(ii).Unbounded.
The Priority Inversion is avoided by using two protocolas.,namely
(i).Priority Inheritance Protocol (PIP)
(ii) Priority Ceiling Protocol(PCP).
APPLICATION
Creating Three Tasks :-
1. Blinking LED 2. Print on USART1 3. Print on USART2
void blinkyLed_task()
{
while(1)
{ // Set PORTC.8
GPIOC->BSRR = (1 << 8);
vTaskDelay(200);
// Reset PORTC.8
GPIOC->BRR = (1 << 8);
vTaskDelay(500);
}
}
void usart_task1()
{ while(1)
{
if(xSemaphoreTake( mutex, ( TickType_t ) 0 )
== pdTRUE )
{ usart1_puts("HOD of EXTC Department ");
xSemaphoreGive(mutex);
vTaskDelay(1000);
} } }
void usart_task2()
{ while(1)
{
if(xSemaphoreTake( mutex, ( TickType_t ) 0 )
== pdTRUE )
{
usart1_puts("Y . S . RAO Sir ");
xSemaphoreGive(mutex);
vTaskDelay(1000);
} }}
Calling Tasks inside main( ) :
int main(void)
{
RCC->AHBENR |= RCC_AHBENR_GPIOCEN;
// enable the clock to GPIOC
RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
// enable the clock to GPIOA
// Put PORTC.8 in output mode
GPIOC->MODER |= (1 << 16);
// Put PORTC.9 in output mode
GPIOC->MODER |= (1 << 18);
// Put PORTA.0 in input mode
GPIOA->MODER &= ~(3 << 0);
usart1_init();
mutex = xSemaphoreCreateMutex();
xTaskCreate(usart_task1,
(const char *)"blinkyLed_task",
configMINIMAL_STACK_SIZE,
NULL, /* pvParameters */
tskIDLE_PRIORITY + 1, /* uxPriority */
NULL /* pvCreatedTask */);
xTaskCreate(usart_task1,
(const char *)"usart_task2",
configMINIMAL_STACK_SIZE,
NULL, /* pvParameters */
tskIDLE_PRIORITY + 2, /* uxPriority */
NULL /* pvCreatedTask */);
xTaskCreate(usart_task2,
(const char *)"usart_task3",
configMINIMAL_STACK_SIZE,
NULL, /* pvParameters */
tskIDLE_PRIORITY + 3, /* uxPriority */
NULL /* pvCreatedTask */);
vTaskStartScheduler();
while(1); }
RESULT
After successful compilation of program .hex file will be
generated. we used to connect the ARM board kit via
Universal serial bus for debugging purpose to see Blinking
LED.
For debugging purpose openOCD.bin file is used. it provides
Real time interfacing of ARM LPC2148X to the code i.e
RTOS.
Procedure to see the USART output on
Minicom screen:---
rajan@Rajan:~$ sudo minicom
Welcome to minicom 2.7
OPTIONS: I18n
Compiled on Jan 1 2014, 17:13:19.
Port /dev/ttySNX0, 19:59:10
Press CTRL-A Z for help on special keys
+-----[configuration]------+
| Filenames and paths |
| File transfer protocols |
| Serial port setup |
| Modem and dialing |
| Screen and keyboard |
| Save setup as dfl |
| Save setup as.. |
| Exit |
+--------------------------+
CTRL-A Z for help | 9600 8N1 | NOR | Minicom 2.7 | VT102 |
Offline | ttySNX0
After setting the serial usb port and Baud rate we will start
getting the output on the Minicom screen as shown below:--
If Tasks have equal priority then use of Semaphore is essential.
It is better shown in the output on Minicom window.
1. With Mutex (i.e Output device is locked for a single Task)
Task 1: Display the task1 output at USART1 ( Embedded GURU of
SPIT )
Task 2: Display the task2 output at USART2 ( Y . S . RAO Sir )
OUTPUT : HOD of EXTC Department Y . S . RAO Sir HOD of
EXTC Department Y . S . RAO Sir HOD of EXTC Department
Y . S . RAO Sir HOD of EXTC Department Y . S . RAO Sir
HOD of EXTC Department Y . S . RAO Sir HOD of EXTC
Department Y . S . RAO Sir .........
2. Without Mutex (i.e Output device is used Task)
OUTPUT : HOD of EXTC Department Y . S . RAO Sir HO Y .
D .S o. FRA EXO STC irDe HOpar D otm f EXenTCt S o. FRA
EXO STC irDe HOpar D otm f EXenTCt S o. FRA EXO STC
irDe HOpar D otm f EXenTCt S o. FRA EXO STC irDe HOpar
D otm f ExenTCt...........................
References:
1.Embedded/Real Time Systems : Concepts,Design
&Programming –Dr.K.V.K.K Prasad : Dreamtech Publs
2. Embedded & Real Time Systems Notes - Mr. Suman
Kalyan Oduri
3.www.freertos.org
RTOS implementation

RTOS implementation

  • 1.
    Real Time OperatingSystem For Embedded Systems Rajan Singh M.E EXTC Sem1(2015-2017) kumar.rajan1812@gmail.com Abstract— An embedded system is a special-purpose computer system designed to perform one or a few dedicated functions, often with real-time computing constraints. Embedded systems contain a processor, software and Memory and The processor may be 8051 micro-controller or a Pentium-IV processor, Memory ROM and RAM respectively Memory Input Processor Output. Index Terms— freeRTOS, ARM LPC2148X, Task, Scheduling, Semaphore, mutex . INTRODUCTION (RTOS) An RTOS is an OS for response time-controlled and event- controlled processes. It is very essential for large scale embedded systems. RTOS occupy little space from 10 KB to 100KB The main task of a RTOS is to manage the resources of the computer such that a particular operation executes in precisely the same amount of time every time it occur. When RTOS is not necessary? For small-scaled systems, RTOS’s function can be replaced by C. For example, instead of the memory allocation and de-allocation functions of RTOS, the C function , 'melloc' and 'free' can be used. Software can directly handle inter-process communication. 3. When RTOS is necessary? However, RTOS is essential when… A common and effective way of handling of the hardware source calls from the interrupts I/O management with devices, files, mailboxes becomes simple using an RTOS Effectively scheduling and running and blocking of the tasks in cases of many tasks and many more….. In conclusion, an RTOS may not be necessary in a small-scaled embedded system. An RTOS is necessary when scheduling of multiple processes and devices is important. THEORY Hard Real time system: Failure to meet such a dead line is considered to be a fatal fault and will lead to disastrous consequences e.g. Response of the break system of a speeding Train approaching a Red Signal to the stop command must come before the train crosses the Red signal. Soft Real time system : Failure to miss a Soft Deadline is undesirable but a few misses does no serious harm like occasional delay in an on line trading of stocks. However the system’s overall performance becomes poorer and poorer as more and more jobs starts missing the deadline. The most important component of RTOS is its kernel (Monolithic & Microkernel). BSP or Board Support Package makes an RTOS target-specific (It’s a processor specific code onto (processor) which we like to have our RTOS running). RTOS Kernel Functions: 1. Task Management2. Intertask Communication & Synchronization 3. Dynamic Memory Allocation 4. Timers5. Devices I/O Supervisor* Task States of RTOS :-- WORKING Task Management: Set of services used to allow application software developers to design their software as a number of separate chunks of software each handling a distinct topic, a distinct goal, and sometimes its own real-time deadline. Main service offered is Task Scheduling controls the execution of application software tasks  can make them run in a very timely and responsive fashion. RTOS perform priority-based pre emptive task scheduling. Basic rules for priority based pre emptive task scheduling  The Highest Priority Task that is Ready to Run, will be the Task that Must be Running.
  • 2.
    Priority based PreemptiveTask Scheduling: Every Task in a software application is assigned a priority. Higher Priority = Higher Need for Quick Response. Nested Preemption Timeline for Priority-based Preemptive Scheduling. Inter task Communication &Synchronization These services makes it possible to pass information from one task to another without information ever being damaged. Makes it possible for tasks to coordinate & productively cooperate with each other. Inter-Task communication &Synchronization The most important communication b/w tasks in an OS is the passing of data from one task to another. Message Producer Task Receiver Task If messages are sent more quickly than they can be handled, the OS provides message queues for holding the messages until they can be processed. Message passing in RTOS In RTOS, the OS copies a pointer to the message, delivers the pointer to the message-receiver task, and then deletes the copy of the pointer with message-sender task. Message Sender RAM Task Message Message Message Example of Rtos:- freeTOS, VxWorks, pSOS OS QNX •ETLinux • VRTX •uCLinux •uCOS •uLinux (muLinux) SymbianOS distro fits on a single floppy Mutexes : Mutex means mutual exclusion A mutex is a synchronization object that can have only two states. They are not-owned and owned. Two operations are defined for mutexes Lock: This operation attempts to take ownership of a mutex, if the mutex is already owned by another thread then the invoking thread is queued. Unlock: This operation relinquishes ownership of a mutex. If there are queued threads then a thread is removed from the queue and resumed, ownership is implicitly assigned to the thread. Interrupt processing using Semaphore mutex Priority-ceiling mutex locking and unlocking uint8_t SST_mutexLock(uint8_t prioCeiling) { uint8_t p; SST_INT_LOCK(); p = S_currPrio_; /* save the original SST priority to return */ if (prioCeiling > SST_currPrio_) { SST_currPrio_ = prioCeiling; /* set the SST priority to the ceiling */ } SST_INT_UNLOCK(); return p; } /*......................................... .................................*/ void SST_mutexUnlock(uint8_t orgPrio) { SST_INT_LOCK(); if (orgPrio < SST_currPrio_) { SST_currPrio_ = orgPrio; /* restore the saved priority to unlock */ SST_schedule_(); /* the scheduler unlocks the interrupts internally */ } SST_INT_UNLOCK(); } Unlike the simple INT_LOCK macros, the SST mutex interface allows locks to be nested, because the original SST priority is preserved on the stack. Above example shows how the mutex is used in the SST example to protect the non-reentrant DOS random number generator calls inside the clock-tick tasks tickTaskA() and tickTaskB(). Priority Inversion Problem In any real time embedded system ,if a high priority task is blocked or waiting and a low priority task is running or under execution ,this situation is called Priority Inversion. This priority Inversion is shown in the diagram below. In Scheduling, priority inversion is the scenario where a low priority Task holds a shared resource that is required by a high priority task. This causes the execution of the high priority task to be blocked until the low priority task releases the resource, effectively “inverting” the relative priorities of the two tasks. Suppose some other medium priority task, one that does not depend on the shared resource, attempts to run in the interim, it will take precedence over both the low priority task and the high priority task. The consequences of the priority Inversion are (i) Reduce the performance of the system (ii) May reduce the system responsiveness which leads to the violation of response time guarantees (iii) Create problems in real-time systems. There are two types of priority inversions.(i) Bounded and (ii).Unbounded. The Priority Inversion is avoided by using two protocolas.,namely (i).Priority Inheritance Protocol (PIP) (ii) Priority Ceiling Protocol(PCP). APPLICATION Creating Three Tasks :- 1. Blinking LED 2. Print on USART1 3. Print on USART2 void blinkyLed_task() { while(1) { // Set PORTC.8 GPIOC->BSRR = (1 << 8); vTaskDelay(200); // Reset PORTC.8 GPIOC->BRR = (1 << 8); vTaskDelay(500); } }
  • 3.
    void usart_task1() { while(1) { if(xSemaphoreTake(mutex, ( TickType_t ) 0 ) == pdTRUE ) { usart1_puts("HOD of EXTC Department "); xSemaphoreGive(mutex); vTaskDelay(1000); } } } void usart_task2() { while(1) { if(xSemaphoreTake( mutex, ( TickType_t ) 0 ) == pdTRUE ) { usart1_puts("Y . S . RAO Sir "); xSemaphoreGive(mutex); vTaskDelay(1000); } }} Calling Tasks inside main( ) : int main(void) { RCC->AHBENR |= RCC_AHBENR_GPIOCEN; // enable the clock to GPIOC RCC->AHBENR |= RCC_AHBENR_GPIOAEN; // enable the clock to GPIOA // Put PORTC.8 in output mode GPIOC->MODER |= (1 << 16); // Put PORTC.9 in output mode GPIOC->MODER |= (1 << 18); // Put PORTA.0 in input mode GPIOA->MODER &= ~(3 << 0); usart1_init(); mutex = xSemaphoreCreateMutex(); xTaskCreate(usart_task1, (const char *)"blinkyLed_task", configMINIMAL_STACK_SIZE, NULL, /* pvParameters */ tskIDLE_PRIORITY + 1, /* uxPriority */ NULL /* pvCreatedTask */); xTaskCreate(usart_task1, (const char *)"usart_task2", configMINIMAL_STACK_SIZE, NULL, /* pvParameters */ tskIDLE_PRIORITY + 2, /* uxPriority */ NULL /* pvCreatedTask */); xTaskCreate(usart_task2, (const char *)"usart_task3", configMINIMAL_STACK_SIZE, NULL, /* pvParameters */ tskIDLE_PRIORITY + 3, /* uxPriority */ NULL /* pvCreatedTask */); vTaskStartScheduler(); while(1); } RESULT After successful compilation of program .hex file will be generated. we used to connect the ARM board kit via Universal serial bus for debugging purpose to see Blinking LED. For debugging purpose openOCD.bin file is used. it provides Real time interfacing of ARM LPC2148X to the code i.e RTOS. Procedure to see the USART output on Minicom screen:--- rajan@Rajan:~$ sudo minicom Welcome to minicom 2.7 OPTIONS: I18n Compiled on Jan 1 2014, 17:13:19. Port /dev/ttySNX0, 19:59:10 Press CTRL-A Z for help on special keys +-----[configuration]------+ | Filenames and paths | | File transfer protocols | | Serial port setup | | Modem and dialing | | Screen and keyboard | | Save setup as dfl | | Save setup as.. | | Exit | +--------------------------+ CTRL-A Z for help | 9600 8N1 | NOR | Minicom 2.7 | VT102 | Offline | ttySNX0 After setting the serial usb port and Baud rate we will start getting the output on the Minicom screen as shown below:-- If Tasks have equal priority then use of Semaphore is essential. It is better shown in the output on Minicom window. 1. With Mutex (i.e Output device is locked for a single Task) Task 1: Display the task1 output at USART1 ( Embedded GURU of SPIT ) Task 2: Display the task2 output at USART2 ( Y . S . RAO Sir ) OUTPUT : HOD of EXTC Department Y . S . RAO Sir HOD of EXTC Department Y . S . RAO Sir HOD of EXTC Department Y . S . RAO Sir HOD of EXTC Department Y . S . RAO Sir HOD of EXTC Department Y . S . RAO Sir HOD of EXTC Department Y . S . RAO Sir ......... 2. Without Mutex (i.e Output device is used Task) OUTPUT : HOD of EXTC Department Y . S . RAO Sir HO Y . D .S o. FRA EXO STC irDe HOpar D otm f EXenTCt S o. FRA EXO STC irDe HOpar D otm f EXenTCt S o. FRA EXO STC irDe HOpar D otm f EXenTCt S o. FRA EXO STC irDe HOpar D otm f ExenTCt........................... References: 1.Embedded/Real Time Systems : Concepts,Design &Programming –Dr.K.V.K.K Prasad : Dreamtech Publs 2. Embedded & Real Time Systems Notes - Mr. Suman Kalyan Oduri 3.www.freertos.org