Rtos Ppts
Rtos Ppts
1
μC/OS-II Features
Services : Mailboxes, Queues, Semaphores,
fixed-sized memory partitions, time-related functions
Interrupt Management: Interrupt can be nested
up to 255 levels deep
Robust and Reliable
Shortcomings:
μC/OS-II does not support priority inheritance.
With μC/OS-II, all tasks must have a unique priority.
A task, also called a thread, is a simple program that
thinks it has the CPU all to itself.
2
Tasks
3
Task Control Blocks (OS_TCB)
4
Task States
RUNNING
DELAYED
PENDING
INTERRUPTED
5
Task Context Switch
6
Functions UCOS2.LIB
void OSInit(void);
Description
Initializes µC/OS-II data; must be called before any other
µC/OS-II functions are called.
void OSStart(void)
Description
Starts the multitasking process
Description
Creates a task to be managed by µC/OS-II.
7
OS Init exit functions UCOS2.LIB
void OS_ENTER_CRITICAL();
void OS_EXIT_CRITICAL();
Description
Enter and Exit a critical section.
8
OSSched functions UCOS2.LIB
void OSSchedLock(void);
Description
Prevents task rescheduling.
void OSSchedUnlock(void);
Description
Allow task rescheduling
9
OSSem functions UCOS2.LIB
10
OSSem functions UCOS2.LIB
11
OSFlag functions (OS_FLAG.C)
12
OSFlag functions (OS_FLAG.C)
13
OSFlag functions (OS_FLAG.C)
14
OSMbox functions OS_MBOX.C
Description
OSMboxAccept() does not suspend the calling task if a
message is not available
Description
Creates a message mailbox if event control blocks are available
15
OSMbox functions OS_MBOX.C
16
OSMbox functions OS_MBOX.C
OS_MBOX_DATA *pdata);
Description
Obtains information about a message mailbox.
17
OSMem Functions UCOS2.LIB
18
OSMem Functions UCOS2.LIB
19
OSMutex functions OS_MUTEX.C
Description
This function checks the mutual exclusion semaphore to
see if a resource is available.
20
OSMutex functions OS_MUTEX.C
21
OSMutex functions OS_MUTEX.C
Description
This function signals a mutual exclusion semaphore.
Description
This function obtains information about a mutex.
22
OSQ functions OS_Q.C
23
OSQ functions OS_Q.C
24
OSQ functions OS_Q.C
25
Functions
INT8U OSTaskChangePrio(INT8U oldprio, INT8U newprio);
Description
Allows a task's priority to be changed dynamically. Note that
the new priority MUST be available.
void OSStatInit(void);
Description
Determines CPU usage.
26
EXAMPLE 1
#include <dos.h> void task2()
#include "libepc.h" {
#include "os_cpu.h" while(1)
#include "os_cfg.h" {
#include "ucos_ii.h" PutCharAt('A',x,y++);
#define STK_SIZE 1024 OSTimeDly(100);
int x=0,y=0; PutCharAt('U',x,y++);
OSTimeDly(100);
void task1()
}
{
}
while(1)
{
if(x==24) x=0;
if(y==79) y=0;
PutCharAt('B',++x,y++);
OSTimeDly(100);
PutCharAt('L',x,y++);
OSTimeDly(100);
}
}
27
EXAMPLE 1
int main()
{
static OS_STK T1_STK[STK_SIZE];
static OS_STK T2_STK[STK_SIZE];
ClearScreen(0x07);
OSInit();
OSTaskCreate(task1,NULL,T1_S
TK,11);
OSTaskCreate(task2,NULL,T2_S
TK,12);
OSStart();
}
28
Example 2
#include <dos.h> void task2()
#include "libepc.h" {
#include "os_cpu.h" while(1)
#include "os_cfg.h" {
#include "ucos_ii.h" OSTimeDly(100);
#define os_stk 1024 PutCharAt('S',++x,y++);
int x=0,y=0,z=0; OSSemPend(p,0,err);
char *err; PutCharAt('M',x,y++);
OS_EVENT *p; OSSemPost(p);
void task1() OSTimeDly(100);
{ }
while(1) }
{
OSTimeDly(100);
OSSemPend(p,0,err);
PutCharAt('G',x,y++);
OSSemPost(p);
OSTimeDly(100);
}
}
29
Example 2
void task3() int main()
{ {
while(1) static OS_STK t1_stk[os_stk];
{ static OS_STK t2_stk[os_stk];
OSSemPend(p,0,err); static OS_STK t3_stk[os_stk];
OSTimeDly(100);
PutCharAt('I',x,y++); ClearScreen(0x07);
OSSemPost(p); OSInit();
PutCharAt('A',x,y++); OSTaskCreate(task1,NULL,t1_stk,11);
OSTimeDly(100); OSTaskCreate(task2,NULL,t2_stk,12);
} OSTaskCreate(task3,NULL,t3_stk,13);
} p=OSSemCreate(1);
OSStart();
}
30
1.Real-Time Systems
•Real-time systems process events.
•Events occurring on external inputs cause other events to
occur as outputs.
•Minimizing response time is usually a primary objective, or
otherwise the entire system may fail to operate properly.
31
1.Real-Time Systems
32
2. Multi-Tasking and Concurrency
33
2. The build and load process for
desktop application programs.
Object Files
Compiler Executabl
e Image
File Read-
Loader
Linker
Assembler Write
Memory
(RAM)
Run-Time Library:
Boot
Operating System Image:
Process
34
2. The build and load process for
embedded application programs
Object Executabl ROM
Files e Image Read-
Image
File Write
File
Memory
(RAM)
Locator
Assemble
r Program
Linker
Initialization
Re-Entrant
Library: Read-
Only
Real-Time ROM
Memory
Kernel: "Burner"
(ROM)
35
3.INTERRUPTS
• Interrupts are signals to processor from hardware.
• The interrupt comes generally in form of signal to special pin
for interrupt.
• Processor responds by stopping the current execution and
starts to execute the ISR.
• Sometimes same interrupt is shared between two or more
devices.
• Non Maskable Interrupt cannot be disabled, generally used in
critical situations.
• Priorities can be assigned to the interrupts to resolve confilcts.
• The processor jumps to a particular address in response to an
interrupt.
• Processor does not stop in between an instruction in response
to an interrupt. Nesting of interrupts is allowed.
• Interrupts are disabled at start up.
36
3.INTERRUPTS
Static int temp[2];
Void interrupt readtemp( void ){
temp[0]=read val;
temp[1]=read val;
}
void main( void ){ SHARED
int tem0,tem1;
while(TRUE) { DATA BUG
tem0=temp[0];
tem1=temp[1];
if(tem0!=tem1) {
activate alarm
}
}
}
37
3.INTERRUPTS
SOLUTION TO SHARED DATA BUG
38
3.INTERRUPTS
Static int temp[2];
Void interrupt readtemp( void ){
temp[0]=read val;
temp[1]=read val;
}
void main( void ){
int tem0,tem1;
while(TRUE) {
disable();
tem0=temp[0];
tem1=temp[1];
enable();
if(tem0!=tem1) {
activate alarm
} } }
39
3.INTERRUPTS
• Other solutions without disabling interrupts are like
relying upon compiler to convert code to a single
instruction but is not reliable.
40
3.INTERRUPTS
INTERRUPT LATENCY
• Defined as response time to an interrupt.
• Depends on:
• Duration for which interrupt is disabled.
• Duration of higher priority interrupts
• Time for bookkeeping.
• Time to save the context.
• Writing short and efficient code for ISR can reduce
latency.
• Make sure interrupts are not disabled for long durations.
41
3.INTERRUPTS
42
4.SOFTWARE ARCHITECTURES
43
4.SOFTWARE ARCHITECTURES
Disadvantages:
• Worst case response is total of execution times of all
routines.
• Improvement is possible by polling faster device more
often but still very slow.
• System is fragile-add one more device and response
times change drastically.
• Absolutely no priorities.
44
4.SOFTWARE ARCHITECTURES
45
4.SOFTWARE ARCHITECTURES
BOOL devicea=false;
BOOL deviceb=false; Void interrupt deviceB( void)
… {
… take care of B
BOOL devicez=false; deviceb=true;
Void interrupt deviceA( void) }
{
take care of A
…
devicea=true; …
} …
Void interrupt deviceZ( void)
{
take care of Z
devicez=true;
}
46
4.SOFTWARE ARCHITECTURES
void main( void)
{
while( true)
{ …
if(devicea) …
{
…
devicea=false;
handle data of device A if(devicez)
} {
if(deviceb) devicez=false;
{ handle data of device Z
deviceb=false;
}
handle data of device B
} }
}
47
4.SOFTWARE ARCHITECTURES
• Not simple
• All task code has same priority.
• Putting code in ISR makes device get higher priority.
• But this makes other responses slow.
• Polling of higher priority device more is possible like basic
round robin architecture.
• This technique is very fragile.
• Worst response is when interrupt happens just when
round robin passed by.
48
4.SOFTWARE ARCHITECTURES
49
4.SOFTWARE ARCHITECTURES
50
4.SOFTWARE ARCHITECTURES
51
4.SOFTWARE ARCHITECTURES
52
4.1 COMPARATIVE TABLE
Architecture Priorities Worst Stability of Simplicity
response for response on
task code code change
Round-robin None Sum of all Poor very simple
task code
Round-robin Interrupts in Total time for Good for ISR Shared data
with priority and all task code poor for task problem to be
interrupts all task code code tackled
at same
priority
Function Interrupts in Time for Relatively Shared data
queue priority then longest good problem and
scheduling tasks in function function
priority queue code
54
5.RTOS BASICS
FEATURES
• Term “Real Time Operating System” and “Real Time
Kernel” are used interchangingly.
• Normal OS takes control of system as soon as it is turned
on.
• Application usually gets control of the system the RTOS
gets control.
• RTOS does not protect itself from the applications,
generally if the application passes a wrong pointer to the
RTOS, whole system crashes along with RTOS.
• Only services needed for that particular system are
included, no extra services present.
55
5.RTOS BASICS
TASKS AND TASK STATES
• Tasks are basic building blocks of a RTOS based software.
• A task is a subroutine.
• A task can be in three states:
• Running: Presently executing.
• Ready: Waiting to run if processor is available.
• Blocked: Has nothing to generally unless some external
event triggers it.
• Other states like suspend, pending, waiting, dormant etc.
are forms of either of above states.
56
5.RTOS BASICS
SCHEDULER
• Scheduler decides which task should run.
• Decision is based on priorities among unblocked tasks.
• Scheduler does not fiddle with priorities.
• A task will decide for itself that it has to enter blocked
state that means it has to run before moving to blocked
state.
• An interrupt or some other task has to pull out a task out
of blocked state.
• Scheduler takes care of shuffling between running and
blocked states.
• If all tasks are blocked system will hang.
57
5.RTOS BASICS
58
5.RTOS BASICS
Void Buttontask( void) /*higher priority*/
{
while(true)
{
unblock on button push and respond void main( void )
}
{
}
void levels task( void) /* low priority*/ initrtos():
{ starttask(buttontask,
while(true) high);
{ starttask(levelstask,
read levels and calculate low);
}
} startrtos();
}
59
5.RTOS BASICS
60
5.RTOS BASICS
SHARED DATA BUG REVISITIED AGAIN
Void task1( void )
{
…
…
counterrors(9);
…
…
}
Void task2( void )
{
…
…
counterrors(11);
61
5.RTOS BASICS
…
…
}
Mov r1,(errors)
static int errors; Add r1,(newerrors)
Mov (cerrors),r1
void counterrors(int newerrors) return
{
errors+=newerrors;
}
62
6.REENTERNCY
• A function, which will work correctly even when called
by, more than one task is, called reentrant function.
• A function is reentrant if:
• It should not use non-atomic variables unless they are
stored in stack of calling task or are private variables of
that task
• Should not call anther non-reentrant function.
• Should not use hardware in non-atomic way.
63
7.SEMAPHORES
• A semaphore is akin to disabled interrupt for an ISR.
• A semaphore is taken and released.
• As long as a semaphore is taken by a task it won’t be
interrupted.
• Terms like get & give, take & release, pend & post, wait
& signal etc. are used.
• If a task calls take semaphore and does not call release
semaphore then any other task calling take semaphore
will be blocked.
64
7.SEMAPHORES
Struct void levels task( void)
/* low priority*/
{ {
long tanklevel; while(true)
long timeupdated; {
takesemaphore();
}tankdata[max_tanks]; set tankdata[I].timeupdate
Void Buttontask( void) /*higher set tankdata[I].tanklevel
priority*/ releasesemaphore();
}
{ }
while(true) void main( void )
{ {
unblock on button push and respond initrtos():
I=get id of tank where button pushed starttask(buttontask, high);
takesemaphore(); starttask(levelstask, low);
printf(“ time=%ld and level=%ld”, startrtos();
tankdata[I].timeupdatetankdata[I].
tanklevel); }
releasesemaphore();
}
}
65
7.SEMAPHORES
66
7.1 MULTIPLE SEMAPHORES
67
7.2 SEMAPHORE PROBLEMS
Priority inversion:
• Task a, task b and task c are in decreasing order of
priority.
• Task c acquires semaphore.
• Task b gets unblocked and takes control.
• Task a wants semaphore and gets blocked.
• Task c can’t release semaphore because task b is running.
• So task b holds up higher priority task a, hence priority
inversion.
• Solved by priority inheritance where task c is temporarily
elevated above task b.
68
7.2 SEMAPHORE PROBLEMS
69
7.2 SEMAPHORE PROBLEMS
70
7.3 SEMAPHORES TYPES
71
8.MESSAGE QUEUES
72
8.1 PITFALLS
73
9.COMPARISON OF
COMMUNICATION METHODS
74
10.ISR IN RTOS ENVIRONMENT
75
10. IMPILICATIONS: RULE 1
76
10. IMPILICATIONS: RULE 2
79
10. IMPILICATIONS: RULE 2
81