Introduction to VxWorks RTOS Eli Carmi July 2007
Introduction to VxWorks RTOS
Eli Carmi
July 2007
RTOS Tasks Synchronization Asynchronous Information
2
Introduction to VxWorks RTOS
RTOS Real Time Multi Tasking Priority Scheduling Round-Robin Scheduling
3
RTOS - Real Time OS
Control External Events Synchronous Event Asynchronous Event Independents Event
Speed Fast response Low overheads
Deterministic A late answer is a wrong answer
4
Real Time
Sequence programming One task controlling all events in a Loop
5
Multi Tasking
FOREVER
{
if (Event1)
do Event1 action
if (Event2)
do Event2 action
…
}
Multitasking Programming Task is “Waiting” on Event Task becomes “Ready” upon Event
6
Multi Tasking cont.
Task1
FOREVER
{
wait on Event1
do Event1 action
}
Task2
FOREVER
{
wait on Event2
do Event2 action
}
Task3
FOREVER
{
wait on Event3
do Event3 action
}
Task in the system may have different Priority Preemptive Scheduling on Task Priority The Highest Priority Task ready to run is
allocated the CPU Equal Priority tasks won’t preempt each other Scheduling can occur by Synchronous or
Asynchronous Events No delay on Context Switch to the next Tick
7
Priority Scheduling
Slicing Context Switch in each (predefined) time slice
period Round-Robin
All tasks in the same priority share the CPU
8
Round-Robin Scheduling
Tasks Task States Task Create (t-name, t-id) TCB, Stack, errno… Context Switch
9
Tasks
Delay Pended Ready Execute Suspend
10
Task States
Event TaskPended
TaskPended
TaskPended
Timer
Task Delay + Pended
TaskDelay
TaskDelay
Ready Taskexecute
Taskready
Taskready
Taskready
EventTask
PendedTask
Pended
taskId = taskSpawn(name, Priority, Option, Stack, Entry, Arg1,… arg10 ) task Id Task name Priority Option Stack Entry Arg1,…arg10
11
Task Create
TCB – Task Control Block Entry pointer Registers Status Errno …
Task Stack May be filled with 0xee
12
TCB
13
Context Switch
New TCB CPU Old TCBCPU
Synchronization Need for Synchronization Binary Semaphores Mutex Semaphores Message Queue
14
Synchronization
Reentrancy & Shared Resource
Jobs Synchronization
15
Need for Synchronization
…
void func()
{
….
if (gResource.free)
{
gResource.free = FALSE;
gResource.xxx = …
}
….
}
….Action1
…. Action2
Mostly for Synchronization Creates Semaphore for Event Waits for Event
Task call the semaphore take() action Task is blocked until Event Semaphore is given
Upon Event becomes available Task or ISR give() semaphore
16
Binary Semaphores
SEM_ID semBCreate(option, InitState) SEM_ID Option InitState
Status semTake(semid, timeout) Status semGive(semid, timeout)
Status semid Timeout
Note: Counting Semaphores also available
17
Binary Semaphores (cont)
Taking a Binary Semaphore
Giving a Binary Semaphore
18
Binary Semaphores (cont)
SemaphoreAvailable?
Task pends until sem is given or timeout
Task unpendssemTake()
return ERROR
No Timeout
Task continuesemTake()return OK
Task unpendssemTake()return OK
Yes Semaphore give
TaskPended?
SemaphoreMade available
No
Task at front ofqueue made ready,Semaphore remain
unavailable
Yes
Mostly for Shared Resource Works like Semaphores, but with Ownership
Task which takes the mutex “owns” it This task is the only one that can give the mutex Mutex can be takes repeatedly ISR can not use Mutex
SEM_ID semMCreate(option) Option (SEM_INVERSION_SAFE,…)
Priority Inheritance Initial State is available
19
Mutex Semaphores
Priority Inversion
Priority Inheritance
20
Mutex Semaphores (Cont)
execute
execute
execute
Low
Priority
High
Priority
Medium
Priority
semTak
Event
Event
semTak
Ready
Ready
PendPend
PendPend
Msg_Q_ID msgQCreate (maxMsg, MaxMsgLen, Option) MSG_Q_ID maxMsg MaxMsgLen Option
21
Message Queue
Status msgQSend(msgQId, buffer, nBytes, timeout, priority) Status msgQId buffer nBytes Timeout (WAIT_FOREVER, NO_WAIT, …) Priority (MSG_PRI_URGENT, MSG_PRI_NORMAL)
22
Message Queue (cont)
Int msgQReceive(msgQId, buffer, MaxNBytes, timeout) Int –number of bytes reads or ERROR msgQId buffer nBytes Timeout (WAIT_FOREVER, NO_WAIT, …)
Note: Pipes are also available Built on top of message queue
23
Message Queue (cont)
Asynchronous Interrupts & Exceptions ISR Timers
24
Asynchronous
Interrupts Interrupts allow devices to notify the CPU on events Interrupt Levels Single Interrupt Stack allocate at startup
Exceptions Unplanned event generated by the CPU (divide by
0) Exceptions will generate an “internal” interrupt VxWorks installs exceptions handlers
intLock / intUnlock25
Interrupts & Exceptions
ISR – Interrupt Service Routine NOT a Task
No TCB Can NOT be blocked (can’t use semTake, malloc
and IO routines like printf) Keep ISR short
Delay lower and equal Interrupts Delay all Tasks
Off-load work from ISR
26
ISR
taskDelay() May “Drift”
System Clock tickGet() sysClkRateGet()
Watchdog WDOG_ID wdCreate() STATUS wdStart(wdid, delay, pRoutine, parameter) pRoutine is ISR
Note: Auxiliary Clock is also available
27
Timers
Thank You