ES Unit-4
ES Unit-4
UNIT-4
Message Queues, Mailboxes and Pipes:
❖ Basic techniques for inter-task communication and data sharing are: interrupt
enable/disable and using semaphores. E.g., the tank monitoring tasks and serial port
and printer handling tasks
❖ Use services of vLogError and ErrorsTask (vLogError enqueues errors for ErrorsTask
to process)
the RTOS to interpret or map to error-type. Using the ReadFromQueue function, the
RTOS then activates ErrorTask to handle the error if the queue is not empty – freeing
Task1 and Task2 to continue their tasks.
❖ Functions AddToQueue and ReadFromQueue are non-reentrant, and the RTOS
switches between Task1 and Task2 in the middle of their tasks execution are
guaranteed to be ok.
EMBEDDED SYSTEMSJP
2
EMBEDDED SYSTEMSJP
3
task to a) guarantee correct start-up values and b) avoid uncertainty about task
priorities and order of execution which might affect the queue’s content
❖ Need code to manage the queue (when full and empty) if RTOS doesn’t – block
❖ RTOS may limit the amount of info to write/read to queue in any single call
EMBEDDED SYSTEMSJP
4
EMBEDDED SYSTEMSJP
5
❖ Code in above Figure limits the amount of data to write to or read from the queue.
❖ For tasks to communicate any amount of data, create a buffer and write the pointer to
the buffer to the queue. (The receiving task reads/retrieves data from the buffer via
the pointer, and frees the buffer space.)
EMBEDDED SYSTEMSJP
6
EMBEDDED SYSTEMSJP
7
Using Mailboxes:
❖ Purpose is similar to queues (both supporting asynchronous task
communication)
check-mail, destroy
start)
❖ # of messages per mailbox could be unlimited, but total # in the system could
Using Pipes:
❖ RTOS can create, read from, write to, destroy pipes (typically: each pipe has 2 ends)
❖ Pipes can have varying length messages (unlike fixed length for queues / mailboxes)
EMBEDDED SYSTEMSJP
8
❖ Coding tasks to read from or write to intended ‘structure’ (RTOS can’t help on mismatch)
❖ Overflow of ‘structure’ size – could cripple the software, so need to set size as large as
possible.
problem.
EMBEDDED SYSTEMSJP
9
Timer Functions:
❖ Issues:
❖ Embedded systems track time passage, hence, need to keep time (e.g., to save battery
life, power need to be shut off automatically after, say, X seconds; a message
send-task expects an ACK after Y seconds, it is delayed Y seconds and may
retransmit; task is allowed a slice of time after which it is blocked).
EMBEDDED SYSTEMSJP
10
❖ How long is delay – measured in ticks (a tick is like a single ‘heartbeat’ timer interrupt
time)
microprocessor’s hardware timer and its interrupt cycles (RTOS writers must know
this!) OR RTOS writers write ‘watchdog’ timers – based on non-standard timer
hardware – and corresponding software interrupts – called each time the software timer
expires.
❖ RTOS vendors provide board support packages (BSP) – of drivers for timers and other
hardware.
EMBEDDED SYSTEMSJP
11
❖ Accurate timing – short tick intervals OR use dedicated timer for purpose.
❖ Waiting time or delay on message, on a semaphore (but not too tight for high priority
❖ Place ‘call to’ or ‘activation of’ time-critical, high priority tasks inside timer interrupts
or specialized-time-critical tasks inside the RTOS (Note: OS task have higher priority
over other embedded software tasks).
associated nticks, and the parameter to the function. Also note how the
vRadioControlTask communicates with vTurnOnTxorRx and vSetFrequency using
the queue ‘queueRadio’ and msgQreceive/msgQSend).
EMBEDDED SYSTEMSJP
12
EMBEDDED SYSTEMSJP
13
EMBEDDED SYSTEMSJP
14
EMBEDDED SYSTEMSJP
15
EMBEDDED SYSTEMSJP
16
Events:
EMBEDDED SYSTEMSJP
17
❖ More than one task can wait on the same event (tasks are activated by priority)
❖ Events can be grouped, and tasks may wait on a subset of events in a group
software
❖ Tasks can wait on only one semaphore, queue, mbox or pipe, but on many events
simultaneously.
❖ Semaphores are faster, but unlike queues, mboxes, and pipes, they carry 1-bit info
❖ Queues, mboxes, and pipes are error prone and message posting/retrieval is
compute-intensive.
EMBEDDED SYSTEMSJP
18
EMBEDDED SYSTEMSJP
19
EMBEDDED SYSTEMSJP
20
EMBEDDED SYSTEMSJP
21
Memory Management:
❖ In general RTOS offer C lang equivalent of malloc and free for MM, which are slow
and unpredictable
❖ Real time system engineers prefer the faster and more predictable alloc/free functions
for fixed size buffers. E.g., MultiTask! RTOS allocates pools of fixed size buffers,
using
❖ getbuf() [with timed task blocking on no buffers] and reqbuf() [with no blocking and
EMBEDDED SYSTEMSJP
22
❖ Note that most embedded sw is integrated with the RTOS (same address space) and
the ES starts the microprocessor; hence your ES must tell the memory-pool
EMBEDDED SYSTEMSJP
23
EMBEDDED SYSTEMSJP
24
❖ Rules that IR’s must comply with (but not a task code).
❖ Rule 1: an IR can’t call RTOS function that will cause it to block, e.g., wait on
semaphores, reading empty queues or mailboxes, wait on events to avoid high latency
or large response time and potential deadlock.
EMBEDDED SYSTEMSJP
25
EMBEDDED SYSTEMSJP
26
EMBEDDED SYSTEMSJP
27
❖ Rule 2: an IR can’t call RTOS functions that will cause the RTOS to switch other
tasks (except other IR’s); breaking this rule will cause the RTOS to switch from the
IR itself to handle the task, leaving the IR code incomplete or delay lower priority
interrupts.
EMBEDDED SYSTEMSJP
28
❖ Let the RTOS intercept all the interrupts, aided by an RTOS function which tells the
RTOS where the IRs are and the corresponding interrupt hardware
❖ The RTOS then ‘activates’ the calling IR or the highest priority IR
❖ Control returns to the RTOS, and the RTOS scheduler decides which task gets the
microprocessor (allowing the IR to run to completion).
❖ Let the IR call a function in the RTOS to inform the RTOS of an interrupt
❖ After the IR is done, control goes back to the RTOS, where another function calls the
scheduler to schedule the next task.
which are called by the IR (on the appropriate interrupt). When these functions
complete, control goes back to that IR.
EMBEDDED SYSTEMSJP
29
❖ Nested Interrupts
EMBEDDED SYSTEMSJP