65
Embedded Systems More Operating System Services C.-Z. Yang http://syslab.cse.yzu.edu.tw/~czyang Sept.-Dec. 2001

Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

  • View
    218

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

Embedded SystemsMore Operating System

Services C.-Z. Yang

http://syslab.cse.yzu.edu.tw/~czyang

Sept.-Dec. 2001

Page 2: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 2

元智大學資訊工程系

Common Communication Services

• Data sharing– Tasks must be able to communicate with one another to co

ordinate their activities or to share data.

• Some mechanisms are provided in most RTOSs.– Queues

– Mailboxes

– Pipes

Page 3: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 3

元智大學資訊工程系

A Simple Example

• Three tasks– Task1

– Task2

– ErrorsTask

Task1

Task2

ErrorsTask

Page 4: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 4

元智大學資訊工程系

The Code

• Task1 and Task2

Page 5: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 5

元智大學資訊工程系

The Code

• ErrorsTask

If the queue is empty, this function will block the calling task.

Page 6: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 6

元智大學資訊工程系

Some Working Details

• Most RTOSs require that you initialize your queues before you use them.– You should guarantee that queue initialization should be ca

rried out before any task tries to use the queue.

• Most RTOSs allow you to has as many queues as you want.– So you should pass an additional parameter to every queue

function.

Page 7: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 7

元智大學資訊工程系

Some Working Details

• If your code tries to write to a queue when the queue is full, the RTOS must either– return an error to let you know that the write operation fail

ed

– or it must block the task until some other task reads data from the queue and thereby creates some space.

• Many RTOSs include an additional function that will read from a queue if there is any data and will return an error code if not.

Page 8: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 8

元智大學資訊工程系

Some Working Details

• The amount of data that the RTOS lets you write to the queue in one call may not be exactly the amount that want to write.– Many RTOSs are inflexible about this.

– One common characteristic is to allow you to write onto a queue in one call the number of byte taken up by a void pointer.

Page 9: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 9

元智大學資訊工程系

More Realistic Code

• Queue data structures and operations

Page 10: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 10

元智大學資訊工程系

More Realistic Code

• main, Task1 and Task2

Page 11: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 11

元智大學資訊工程系

More Realistic Code

• vLogError

Page 12: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 12

元智大學資訊工程系

More Realistic Code

• ErrorsTasks

Page 13: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 13

元智大學資訊工程系

Remarks on the Previous Code

• A fairly common RTOS interface– Write one void pointer to the queue with each call.

• A fairly common coding technique– Cast the data as a void pointers.

Page 14: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 14

元智大學資訊工程系

Another Example

• Reading temperatures

Page 15: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 15

元智大學資訊工程系

A Closer Look at OSQPost()

• OSQPost

UBYTE OSQPost(OS_EVENT *pevent, void *msg){ OS_Q *pq; OS_TCB *ptcb; UBYTE x; UBYTE y; UBYTE bitx; UBYTE bity; UBYTE p;

OS_ENTER_CRITICAL(); if (pevent->OSEventGrp) { /* See if any task pending on queue */ …

} else { …

}}

Page 16: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 16

元智大學資訊工程系

A Closer Look at OSQPost()

• OSQPost - if part

y = OSUnMapTbl[pevent->OSEventGrp]; /* Find highest prio. task waiting for message */bity = OSMapTbl[y];x = OSUnMapTbl[pevent->OSEventTbl[y]];bitx = OSMapTbl[x];p = (y << 3) + x; /* Find priority of task getting the msg */if ((pevent->OSEventTbl[y] &= ~bitx) == 0) { /* Remove this task from the waiting list */ pevent->OSEventGrp &= ~bity;}ptcb = OSTCBPrioTbl[p]; /* Point to this task's OS_TCB */ptcb->OSTCBDly = 0; /* Prevent OSTimeTick() from readying task */ptcb->OSTCBEventPtr = (OS_EVENT *)0; /* Unlink ECB from this task */ptcb->OSTCBMsg = msg; /* Send message directly to waiting task */ptcb->OSTCBStat &= ~OS_STAT_Q; /* Clear bit associated with event type */if (ptcb->OSTCBStat == OS_STAT_RDY) { /* See if task is ready (could be susp'd) */ OSRdyGrp |= bity; /* Put task in the ready to run list */ OSRdyTbl[y] |= bitx;}OS_EXIT_CRITICAL();OSSched(); /* Find highest priority task ready to run */return (OS_NO_ERR);

Page 17: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 17

元智大學資訊工程系

A Closer Look at OSQPost()

• OSQPost - else part

pq = pevent->OSEventPtr; /* Point to queue control block */if (pq->OSQEntries >= pq->OSQSize) { /* Make sure queue is not full */ OS_EXIT_CRITICAL(); return (OS_Q_FULL);} else { *pq->OSQIn++ = msg; /* Insert message into queue */ pq->OSQEntries++; /* Update the nbr of entries in the queue */ if (pq->OSQIn == pq->OSQEnd) { /* Wrap IN ptr if we are at end of queue */ pq->OSQIn = pq->OSQStart; } OS_EXIT_CRITICAL();}return (OS_NO_ERR);

Page 18: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 18

元智大學資訊工程系

Mailboxes

Page 19: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 19

元智大學資訊工程系

Mailboxes

• In general, mailboxes are much like queues.

• The typical RTOS has functions to create, to write to, and to read from mailboxes.

• Perhaps there are functions to check whether the mailbox contains any messages and to destroy the mailbox if it is no longer needed.

Page 20: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 20

元智大學資訊工程系

Some Variations in RTOSs

• The number of messages in a mailbox– Some RTOSs allow a certain number.

– Others allow only one.

• Some RTOSs do not have such limitation for each mailbox, but they have a limit to the total number.

• In some RTOSs, you can prioritize mailbox messages.

Page 21: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 21

元智大學資訊工程系

An Example - MultiTask!

• Each message is a void pointer

– uMbId: the mailbox on which to operate.

– sndmsg: the function adding p_vMsg into the message queue with the priority indicated by uPriority

– rcvmsg: returning the highest-priority message

– chkmsg: returning the first message in the mailbox

Page 22: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 22

元智大學資訊工程系

Pipes

Page 23: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 23

元智大學資訊工程系

Pipes

• Pipes are also much like queues.

• Variations– Some RTOSs allow you to write message of varying lengt

hs onto pipes.

– Pipes in some RTOSs are entirely bye-oriented.

– Some RTOSs use the standard C library functions fread and fwrite to read from and write to pipes.

Page 24: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 24

元智大學資訊工程系

Which Should I Use?

• This depends on – flexibility

– speed

– memory space

– length of time that interrupts must be disabled within the RTOS functions

Page 25: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 25

元智大學資訊工程系

Some Pitfalls

• Most RTOSs do not restrict which tasks can read from or write to any given queue, mailbox, or pipe.– You must ensure that tasks use the correct one each time.

• The RTOS cannot ensure that data written onto a queue, mailbox, or pipe will be properly interpreted by the task that reads it.– You must declare your programming interface very clearly.

Page 26: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 26

元智大學資訊工程系

An Example

• A bug easy to be found by compilers

Page 27: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 27

元智大學資訊工程系

An Example

• A concealed bug

Page 28: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 28

元智大學資訊工程系

More Pitfalls

• Running out of space in queues, mailboxes, or pipes is usually a disaster for embedded software.

• Passing pointers from one task to another through a queue, mailbox, or pipe is one of several ways to create shared data inadvertently.

Page 29: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 29

元智大學資訊工程系

An Example

• No malloc and free

Before After

Page 30: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 30

元智大學資訊工程系

An Example

• No malloc and free

Before After

Page 31: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 31

元智大學資訊工程系

An Example

• However, there is a bug.– When the main task gets a value for pTemperatures fr

om the queue, pTemperatures will point to the iTemperatures array in vReadTemperaturesTask.

– If the RTOS switches from vMainTask to vReadTemperaturesTask while vMainTask was comparing iTemperatures[0] to iTemperatures[1], and if vReadTemperaturesTask then changes the values in iTemperatures, the shared-data bug occurs.

Page 32: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 32

元智大學資訊工程系

Timer Functions

Page 33: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 33

元智大學資訊工程系

Timing Is a Very Important Issue

• Most embedded systems must keep track of the passage of time.

• One simple service that most RTOSs offer is a function that delays a task for a period of time.

Page 34: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 34

元智大學資訊工程系

An Example for Making a TEL Call

• The code

Page 35: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 35

元智大學資訊工程系

Several Questions

• How do I know that the taskDelay function takes a number of milliseconds as its parameters?– Check your programming manuals.

– Usually the delay function in most RTOSs takes the number of system ticks as its parameters.

• How accurate are the delays produced by the taskDelay function?– The nearest system tick.

– A heartbeat timer.

Page 36: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 36

元智大學資訊工程系

Timer Accuracy

• vTaskDelay(3)

Page 37: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 37

元智大學資訊工程系

More Questions

• How does the RTOS know how to setup the timer hardware on ly particular hardware?– This is the job of the developer who is responsible for OS

porting.

– If you are the person, you may have to write your own timer setup software and timer interrupt routine.

• What is a “normal” length for the system tick?– There really isn’t one.

– A trade-off: more accurate, more timer interrupts.

Page 38: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 38

元智大學資訊工程系

More Questions

• What if my system needs extremely accurate timing?– Two choices:

– (1) to make the system tick short enough that RTOS timing fit your definition of “extremely accurate”.

– (2) to use a separate hardware timer for those timings that must be extremely accurate.

Page 39: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 39

元智大學資訊工程系

Other Timing Services

• Most RTOSs offer an array of other timing services.

• All of them are based on the system tick.

• Some cautions are needed.– Timeout for getting a semaphore

• Code for recovering must be provided.

• We should try to find other good solutions.

Page 40: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 40

元智大學資訊工程系

A Useful Service

• To call the function of your choice after a given number of system ticks.

• An example– Radio on/off

– Turning-on procedure

• power on

• 12ms, setting the frequency

• 3ms, turning on the transmitter

Page 41: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 41

元智大學資訊工程系

The Code

Page 42: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 42

元智大學資訊工程系

Events

Page 43: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 43

元智大學資訊工程系

Events

• An event is essentially a Boolean flag that tasks can set or reset and that other tasks can wait for.

Task1 Task2

Page 44: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 44

元智大學資訊工程系

Some Standard Features

• More than one task can block waiting for the same event, and the RTOS will unblock all of them when the event occurs.

• RTOSs typically form groups of events, and tasks can wait for any subset of events within the group.

• Different RTOSs deal in different ways with the issue of resetting an event after it has occurred and tasks that were waiting for it have been blocked.

Page 45: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 45

元智大學資訊工程系

An Example

• AMX code

Page 46: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 46

元智大學資訊工程系

A Brief Comparison of the Methods for Intertask Communication

Page 47: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 47

元智大學資訊工程系

• Semaphores are usually the fastest and simplest methods.– However, not much information can pass through a semap

hore.

• Events are a little more complicated than semaphores and take up just a hair more microprocessor time than semaphores.– ADV: A task can wait for any one of several events at the s

ame time, whereas it can only wait for one semaphore.

– ADV: Some RTOSs make it convenient to use events and make it inconvenient to use semaphores.

Page 48: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 48

元智大學資訊工程系

• Queues allow you to send a lot of information from one task to another.– The drawbacks

• putting messages into and taking messages out of queues is more microprocessor-intensive

• that queues offer you many more opportunities to insert bugs into your code.

Page 49: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 49

元智大學資訊工程系

Memory Management

Page 50: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 50

元智大學資訊工程系

Memory Management

• Most RTOSs have some kind of memory management subsystem.

• Real-time engineers often avoid using malloc() and free() because– they are typically slow and

– their execution times are unpredictable.

• They favor instead functions that allocate and free fixed-size buffers.

Page 51: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 51

元智大學資訊工程系

An Example in MultiTask!

• Pools– Each of which consists of some number of memory buffers.

– In any give pool, all of the buffers are the same size.

• Two functions to allocate a buffer– getbuf()

– reqbuf()

Page 52: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 52

元智大學資訊工程系

An Example in MultiTask!

• You have to tell MultiTask! where the free memory is.

• A function to initialize the pool– init_mem_pool

Page 53: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 53

元智大學資訊工程系

A Simple Example Code

• The printing subsystem

Page 54: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 54

元智大學資訊工程系

Some Important Notes

• The code always allocates a full 40-char buffer.– This waste of memory is the price you pay for the

improved speed that fixed-size buffers allow.

• A common compromise that retains the high-speed memory routines but uses memory reasonably efficiently is to allocate three of four memory buffer pools, each with a different size of buffer.

Page 55: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 55

元智大學資訊工程系

Interrupt Routines in an RTOS Environment

Page 56: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 56

元智大學資訊工程系

Interrupt Routines

• Two rules must be followed.

• Rule 1– An interrupt routine must not call any RTOS function that

might block the caller.

• Rule 2– An interrupt routine may not call any RTOS function that

might cause the RTOS to switch tasks unless the RTOS knows that an interrupt routine, and not a task, is executing.

Page 57: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 57

元智大學資訊工程系

Rule 1: No Blocking

• The nuclear reactor

Page 58: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 58

元智大學資訊工程系

An Example of Nonblocking

Page 59: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 59

元智大學資訊工程系

Rule 2: No RTOS Calls without Fair Warning

• A naïve view

Page 60: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 60

元智大學資訊工程系

Rule 2: No RTOS Calls without Fair Warning

• What really happen

Page 61: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 61

元智大學資訊工程系

The First Solution

• Requiring your cooperation to intercept all the interrupts.

Page 62: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 62

元智大學資訊工程系

The Second Solution

• The RTOS provides a function that the interrupt routines call to let the RTOS know that an interrupt routine is running.

Page 63: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 63

元智大學資訊工程系

The Third Mechanism

• Some RTOSs provide a separate set of functions especially for interrupt routines.

• So there might be OSISRSemPost in addition to OSSemPost.

Page 64: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 64

元智大學資訊工程系

Rule 2 and Nested Interrupts

• If your system allows interrupt routines to nest, then another consideration comes into play.

• If the high-priority interrupt routine makes any calls to RTOS functions, then the lower-priority interrupt routine must let the RTOS know when the lower-priority interrupt occurs.

Page 65: Embedded Systems More Operating System Services C.-Z. Yang czyang Sept.-Dec. 2001

[email protected] Embedded Systems - More Operating Systems Services 65

元智大學資訊工程系

Rule 2 and Nested Interrupts

• The RTOS scheduler should not run until all interrupt routines are complete.