What You Need to Know About RTOSes February 1, 2012 Jack Ganssle
Microprocesors, Advanced
The RTOS
The kernel the multitasker
The kernel handles tasking It also has resources to pass data between tasks safely, and to synchronize tasking.
An RTOS also has resources like communications, a GUI, a file system, and debugging tools.
Polled Loop
Wait for something
Do something
Polled Loop With Interrupts
Interrupt event Wait for interrupt Wait for something signal
Handle Interrupt Do something
Signal main loop
Enter the RTOS
Interrupt event Kernel
Handle Interrupt
Handle screen
Signal RTOS
Handle TCP/IP
Process data
When Do You Need an RTOS?
Multiple independent activities Communications File system GUIs
An Infusion Pump
What About Linux?
Memory needs Real-time response Reliability, validation and security Licensing issues Porting Linux is hard!
Multitasking and Tasks
void { App_TaskADC (void *p_arg) while (1) { ADC_Read(); OSTimeDly(10); } }
Multitasking and Tasks
void { App_TaskLowFluid (void *p_arg)
code to handle low fluid; OSTaskDel(. . .);
Task Creation
void main (void) { OSInit();
/* Kernel initialization */
OSTaskCreate(...); /* Task creation OSTaskCreate(...);
OSStart(); } /* Start multitasking */
*/
TCBs and Stacks
Task Control Block Stack pointer Task Priority Stack Registers Rest of stack
Task state
Other task data
Task States
Scheduling
Preemption
The suspension of one task and start of another
Context switching
The process of switching from one task to another
Round robin
The giving of equal time to all same-priority tasks
Round-Robin Scheduling
Priorities
Scheduling Points
When a task decides to wait for time to expire When a task sends a message or a signal to another task When an ISR sends a message or a signal to a task When a kernel object is deleted When the priority of a task is changed Other places
I/O Perils
Task 1 Hi There Terminal
Task 2 Howdy
HHio wTdhyere
The Mutex
Acquire_mutex; Access_device; Release_mutex;
Semaphores
AtoD_Read (uint16 *result) { start ADC conversion wait for semaphore Read A/D return result }
ADC_ISR (void) { release semaphore clear interrupt }
Message Queues
Rx ISR
Message queue
Serial Stream Rx Task
Roll Your Own?
Consider the cost of buying vs. cost to write Cost to validate Messaging and synchronization Modularity
Rate Monotonic Scheduling
If tasks are periodic, and do not share resources or sync with each other, then RMA says: n Time
if: E1/T1 + E2/T2 + E3/T3 <= n(2 1/n - 1) all hard real time deadlines will be met.
1 2 3 4 5 10000 1.000 0.828 0.780 0.757 0.743 0.693
// Task A if angle> 90 swap modes void a (void){ int raw; float angle; float scale=2*pi; raw=inport(encoder); angle=(float)raw * scale; if(angle>90)swap_mode; }
0013DA 0013DF 0013E2 0013E7 0013EA 0013EF 0013F2 0013F7 0013FC 0013FF
9A 83 9A FF 9A 83 9A 9A 83 9A
00 C4 00 76 00 C4 00 00 C4 00
00 04 00 FC 00 02 00 00 10 00
CALL ADD CALL PUSH CALL ADD CALL CALL ADD CALL
ddload SP,4 dpush WORD PTR [BP-4] dicvt SP,2 dpush drmul SP,16 dpush
k=(l1>l2); 000E8A 000E8D 000E90 000E93 000E95 000E97 000E9A 000E9C 000E9F 000EA1 000EA3 000EA6 8B 8B 3B 7F 7C 3B 76 B8 EB 2B 89 FF 56 46 56 07 0A 46 05 01 02 C0 46 46 CA C8 D6 MOV MOV CMP JG JL CMP JBE L42 MOV JMP L40 SUB L41 MOV INC DX,[BP-54] AX,[BP-56] DX,[BP-42] SHORT L42 SHORT L40 AX,[BP-44] SHORT L40 AX,1 L41 AX,AX BP-10],AX [BP-2]
D4
00
F6 FE
Talk to the vendors
Were mad as hell and were not gonna take it anymore!
Software Selection
CPU/Language/Tool compatibility Pricing model Source or object? Size and performance Debugging tools Services, drivers Other software components
Reputation/Tech Support Reliability
Questions?