11.07.2015 Views

IAR PowerPac RTOS User Guide

IAR PowerPac RTOS User Guide

IAR PowerPac RTOS User Guide

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Resource semaphoresIntroductionResource semaphores are used for managing resources by avoiding conflicts caused by simultaneous use of a resource.The resource managed can be of any kind: a part of the program that is not reentrant, a piece of hardware like thedisplay, a flash prom that can only be written to by a single task at a time, a motor in a CNC control that can only becontrolled by one task at a time, and a lot more.The basic procedure is as follows:Any task that uses a resource first claims it by calling the OS_Use() or OS_Request() routines of <strong>IAR</strong> <strong>PowerPac</strong><strong>RTOS</strong>. If the resource is available, the program execution of the task continues, but the resource is blocked for othertasks. If a second task now tries to use the same resource while it is in use by the first task, this second task is suspendeduntil the first task releases the resource. However, if the first task that uses the resource calls OS_Use() again for thatresource, it is not suspended because the resource is blocked only for other tasks.The following diagram illustrates the process of using a resource:USE()Access resourceUNUSE()A resource semaphore contains a counter that keeps track of how many times the resource has been claimed by callingOS_Request() or OS_Use() by a particular task. It is released when that counter reaches 0, which means theOS_Unuse() routine has to be called exactly the same number of times as OS_Use() or OS_Request(). If it is not,the resource remains blocked for other tasks.On the other hand, a task cannot release a resource that it does not own by calling OS_Unuse(). In the debug versionof <strong>IAR</strong> <strong>PowerPac</strong> <strong>RTOS</strong>, a call of OS_Unuse() for a semaphore that is not owned by this task will result in a call tothe error handler OS_Error().Example of using resource semaphoresHere, two tasks access an LC display completely independently from each other. The LCD is a resource that needs tobe protected with a resource semaphore. One task may not interrupt another task which is writing to the LCD, becauseotherwise the following might occur:● Task A positions the cursor● Task B interrupts Task A and repositions the cursor● Task A writes to the wrong place in the LCD' s memory.To avoid this type of situation, every the LCD must be accessed by a task, it is first claimed by a call to OS_Use() (andis automatically waited for if the resource is blocked). After the LCD has been written to, it is released by a call toOS_Unuse().PP<strong>RTOS</strong>-2 49

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!