IAR PowerPac RTOS User Guide

IAR PowerPac RTOS User Guide IAR PowerPac RTOS User Guide

ie.ksu.edu.tw
from ie.ksu.edu.tw More from this publisher
11.07.2015 Views

** demo program to illustrate the use of resource semaphores*/OS_STACKPTR int StackMain[100], StackClock[50];OS_TASK TaskMain,TaskClock;OS_SEMA SemaLCD;void TaskClock(void) {char t=-1;char s[] = "00:00";while(1) {while (TimeSec==t) Delay(10);t= TimeSec;s[4] = TimeSec%10+'0';s[3] = TimeSec/10+'0';s[1] = TimeMin%10+'0';s[0] = TimeMin/10+'0';OS_Use(&SemaLCD); /* Make sure nobody else uses LCD */LCD_Write(10,0,s);OS_Unuse(&SemaLCD); /* Release LCD */}}void TaskMain(void) {signed char pos ;LCD_Write(0,0,"Software tools by IAR Systems ! ") ;OS_Delay(2000);while (1) {for ( pos=14 ; pos >=0 ; pos-- ) {OS_Use(&SemaLCD); /* Make sure nobody else uses LCD */LCD_Write(pos,1,"train "); /* Draw train */OS_Unuse(&SemaLCD); /* Release LCD */OS_Delay(500);}OS_Use(&SemaLCD); /* Make sure nobody else uses LCD */LCD_Write(0,1," ") ;OS_Unuse(&SemaLCD); /* Release LCD */}}void InitTask(void) {OS_CREATERSEMA(&SemaLCD); /* Creates resource semaphore */OS_CREATETASK(&TaskMain, 0, Main, 50, StackMain);OS_CREATETASK(&TaskClock, 0, Clock, 100, StackClock);}In most applications, the routines that access a resource should automatically call OS_Use() and OS_Unuse() so thatwhen using the resource you do not have to worry about it and can use it just as you would in a single-task system. Thefollowing is an example of how to implement a resource into the routines that actually access the display:/** Simple example when accessing single line dot matrix LCD*/OS_RSEMA RDisp; /* Define resource semaphore */void UseDisp() { /* Simple routine to be called before using display */OS_Use(&RDisp);}void UnuseDisp() { /* Simple routine to be called after using display */OS_Unuse(&RDisp);}void DispCharAt(char c, char x) {UseDisp();LCDGoto(x, y);LCDWrite1(ASCII2LCD(c));UnuseDisp();}void DISPInit(void) {OS_CREATERSEMA(&RDisp);}50IAR PowerPac RTOSfor ARM CoresPPRTOS-2

Resource semaphoresResource semaphores API function overviewRoutineOS_CREATERSEMA()OS_Use()OS_Unuse()OS_Request()OS_GetSemaValue()OS_GetResourceOwner()OS_DeleteRSema()Table 40: Resource semaphore API overviewOS_CREATERSEMA()DescriptionMacro that creates a resource semaphore.Prototypevoid OS_CREATERSEMA (OS_RSEMA* pRSema);ParameterDescriptionpRSemaTable 41: OS_CREATESEMA() parameter listAdditional InformationAfter creation, the resource is not blocked; the value of the counter is 0.OS_Use()DescriptionClaims a resource and blocks it for other tasks.Prototypeint OS_Use (OS_RSEMA* pRSema);ParameterDescriptionpRSemaTable 42: OS_Use() parameter listReturn valueThe counter value of the semaphore.A value larger than 1 means the resource was already locked by the calling task.Additional InformationDescriptionMacro that creates a resource semaphore.Claims a resource and blocks it for other tasks.Releases a semaphore currently in use by a task.Requests a specified semaphore, blocks it for other tasks if it is available. Continuesexecution in any case.Returns the value of the usage counter of a specified resource semaphore.Returns a pointer to the task that is currently using (blocking) a resource.Deletes a specified resource semaphore.Pointer to the data structure for a resource semaphore.Pointer to the data structure for a resource semaphore.The following situations are possible:● Case A: The resource is not in use.If the resource is not used by a task, which means the counter of the semaphore is 0, the resource will be blockedfor other tasks by incrementing the counter and writing a unique code for the task that uses it into the semahore.● Case B: The resource is used by this task.The counter of the semaphore is simply incremented. The program continues without a break.● Case C: The resource is being used by another task.The execution of this task is suspended until the resource semaphore is released. In the meantime if the taskblocked by the resource semaphore has a higher priority than the task blocking the semaphore, the blocking task isassigned the priority of the task requesting the resource semaphore. This is called priority inversion. Priorityinversion can only temporarily increase the priority of a task, never reduce it.PPRTOS-251

** demo program to illustrate the use of resource semaphores*/OS_STACKPTR int StackMain[100], StackClock[50];OS_TASK TaskMain,TaskClock;OS_SEMA SemaLCD;void TaskClock(void) {char t=-1;char s[] = "00:00";while(1) {while (TimeSec==t) Delay(10);t= TimeSec;s[4] = TimeSec%10+'0';s[3] = TimeSec/10+'0';s[1] = TimeMin%10+'0';s[0] = TimeMin/10+'0';OS_Use(&SemaLCD); /* Make sure nobody else uses LCD */LCD_Write(10,0,s);OS_Unuse(&SemaLCD); /* Release LCD */}}void TaskMain(void) {signed char pos ;LCD_Write(0,0,"Software tools by <strong>IAR</strong> Systems ! ") ;OS_Delay(2000);while (1) {for ( pos=14 ; pos >=0 ; pos-- ) {OS_Use(&SemaLCD); /* Make sure nobody else uses LCD */LCD_Write(pos,1,"train "); /* Draw train */OS_Unuse(&SemaLCD); /* Release LCD */OS_Delay(500);}OS_Use(&SemaLCD); /* Make sure nobody else uses LCD */LCD_Write(0,1," ") ;OS_Unuse(&SemaLCD); /* Release LCD */}}void InitTask(void) {OS_CREATERSEMA(&SemaLCD); /* Creates resource semaphore */OS_CREATETASK(&TaskMain, 0, Main, 50, StackMain);OS_CREATETASK(&TaskClock, 0, Clock, 100, StackClock);}In most applications, the routines that access a resource should automatically call OS_Use() and OS_Unuse() so thatwhen using the resource you do not have to worry about it and can use it just as you would in a single-task system. Thefollowing is an example of how to implement a resource into the routines that actually access the display:/** Simple example when accessing single line dot matrix LCD*/OS_RSEMA RDisp; /* Define resource semaphore */void UseDisp() { /* Simple routine to be called before using display */OS_Use(&RDisp);}void UnuseDisp() { /* Simple routine to be called after using display */OS_Unuse(&RDisp);}void DispCharAt(char c, char x) {UseDisp();LCDGoto(x, y);LCDWrite1(ASCII2LCD(c));UnuseDisp();}void DISPInit(void) {OS_CREATERSEMA(&RDisp);}50<strong>IAR</strong> <strong>PowerPac</strong> <strong>RTOS</strong>for ARM CoresPP<strong>RTOS</strong>-2

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

Saved successfully!

Ooh no, something went wrong!