IAR PowerPac RTOS User Guide
IAR PowerPac RTOS User Guide IAR PowerPac RTOS User Guide
** 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
- Page 1 and 2: IAR PowerPac RTOSUser GuidePPRTOS-2
- Page 3 and 4: PrefaceWelcome to the IAR PowerPac
- Page 5 and 6: ContentsPreface ...................
- Page 7 and 8: ContentsOS_WaitCSema() ............
- Page 9 and 10: ContentsOS_LeaveInterrupt() .......
- Page 11 and 12: Introduction to IAR PowerPacRTOSWha
- Page 13 and 14: Basic conceptsTasksIn this context,
- Page 15 and 16: Basic conceptsPREEMPTIVES MULTITASK
- Page 17 and 18: Basic conceptsMAILBOXES AND QUEUESA
- Page 19 and 20: Basic conceptsThe active task may b
- Page 21 and 22: Basic conceptsThe flowchart below i
- Page 23 and 24: Basic conceptsLIST OF LIBRARIESIn y
- Page 25 and 26: Task routinesIntroductionA task tha
- Page 27 and 28: Task routinesOS_CREATETASK() can be
- Page 29 and 30: Task routinesExampleThe following e
- Page 31 and 32: Task routinesExampleint sec,min;voi
- Page 33 and 34: Task routinesAdditional Information
- Page 35 and 36: Task routinesReturn valueOS_TASKID:
- Page 37 and 38: Software timersSoftware timerA soft
- Page 39 and 40: Software timers#define OS_CREATETIM
- Page 41 and 42: Software timersOS_SetTimerPeriod()D
- Page 43 and 44: Software timersAdditional Informati
- Page 45 and 46: Software timersAdditional Informati
- Page 47 and 48: Software timersExampleOS_TIMER TIME
- Page 49: Resource semaphoresIntroductionReso
- Page 53 and 54: Resource semaphoresOS_Request()Desc
- Page 55 and 56: Counting SemaphoresIntroductionCoun
- Page 57 and 58: Counting SemaphoresPrototypevoid OS
- Page 59 and 60: Counting SemaphoresReturn value0: I
- Page 61 and 62: MailboxesWhy mailboxes?In the prece
- Page 63 and 64: MailboxesMailboxes API function ove
- Page 65 and 66: MailboxesExampleSingle-byte mailbox
- Page 67 and 68: MailboxesPrototypevoid OS_GetMail (
- Page 69 and 70: MailboxesOS_WaitMail()DescriptionWa
- Page 71 and 72: QueuesWhy queues?In the preceding c
- Page 73 and 74: QueuesReturn valueThe size of the r
- Page 75 and 76: QueuesExamplestatic void MemoryTask
- Page 77 and 78: Task eventsIntroductionTask events
- Page 79 and 80: Task eventsExampleOS_WaitEventTimed
- Page 81 and 82: Task eventsPrototypechar OS_ClearEv
- Page 83 and 84: Event objectsIntroductionEvent obje
- Page 85 and 86: Event objectsExampleif (OS_EVENT_Wa
- Page 87 and 88: Event objectsExampleOS_EVENT_Reset(
- Page 89 and 90: Heap type memory managementANSI C o
- Page 91 and 92: Fixed block size memory poolsIntrod
- Page 93 and 94: Fixed block size memory poolsProtot
- Page 95 and 96: Fixed block size memory poolsProtot
- Page 97 and 98: StacksIntroductionThe stack is the
- Page 99 and 100: InterruptsIntroductionIn this chapt
** 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