]> git.sur5r.net Git - freertos/blob - Demo/MB96350_Softune_Dice_Kit/SegmentToggleTasks.c
Update to V5.4.1
[freertos] / Demo / MB96350_Softune_Dice_Kit / SegmentToggleTasks.c
1 /*\r
2         FreeRTOS V5.4.1 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4         This file is part of the FreeRTOS distribution.\r
5 \r
6         FreeRTOS is free software; you can redistribute it and/or modify it     under \r
7         the terms of the GNU General Public License (version 2) as published by the \r
8         Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS without being obliged to provide the \r
11         source code for proprietary components outside of the FreeRTOS kernel.  \r
12         Alternative commercial license and support terms are also available upon \r
13         request.  See the licensing section of http://www.FreeRTOS.org for full \r
14         license details.\r
15 \r
16         FreeRTOS is distributed in the hope that it will be useful,     but WITHOUT\r
17         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
18         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
19         more details.\r
20 \r
21         You should have received a copy of the GNU General Public License along\r
22         with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
23         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
24 \r
25 \r
26         ***************************************************************************\r
27         *                                                                         *\r
28         * Looking for a quick start?  Then check out the FreeRTOS eBook!          *\r
29         * See http://www.FreeRTOS.org/Documentation for details                   *\r
30         *                                                                         *\r
31         ***************************************************************************\r
32 \r
33         1 tab == 4 spaces!\r
34 \r
35         Please ensure to read the configuration and relevant port sections of the\r
36         online documentation.\r
37 \r
38         http://www.FreeRTOS.org - Documentation, latest information, license and\r
39         contact details.\r
40 \r
41         http://www.SafeRTOS.com - A version that is certified for use in safety\r
42         critical systems.\r
43 \r
44         http://www.OpenRTOS.com - Commercial support, development, porting,\r
45         licensing and training services.\r
46 */\r
47 \r
48 /**\r
49  * Defines the tasks and co-routines used to toggle the segments of the two\r
50  * seven segment displays, as described at the top of main.c\r
51  */\r
52 \r
53 \r
54 #include <stdlib.h>\r
55 \r
56 /* Scheduler include files. */\r
57 #include "FreeRTOS.h"\r
58 #include "task.h"\r
59 #include "croutine.h"\r
60 \r
61 /* Demo program include files. */\r
62 #include "partest.h"\r
63 \r
64 /*-----------------------------------------------------------*/\r
65 \r
66 /* One task per segment of the left side display. */\r
67 #define ledNUM_OF_LED_TASKS     ( 7 )\r
68 \r
69 /* Each task toggles at a frequency that is a multiple of 333ms. */\r
70 #define ledFLASH_RATE_BASE      ( ( portTickType ) 333 )\r
71 \r
72 /* One co-routine per segment of the right hand display. */\r
73 #define ledNUM_OF_LED_CO_ROUTINES       7\r
74 \r
75 /* All co-routines run at the same priority. */\r
76 #define ledCO_ROUTINE_PRIORITY          0\r
77 \r
78 /*-----------------------------------------------------------*/\r
79 \r
80 /* The task that is created 7 times. */\r
81 static void vLEDFlashTask( void *pvParameters );\r
82 \r
83 /* The co-routine that is created 7 times. */\r
84 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex );\r
85 \r
86 /* This task is created once, but itself creates 7 co-routines. */\r
87 static void vLEDCoRoutineControlTask( void *pvParameters );\r
88 \r
89 /* Handles to each of the 7 tasks.  Used so the tasks can be suspended\r
90 and resumed. */\r
91 static xTaskHandle xFlashTaskHandles[ ledNUM_OF_LED_TASKS ] = { 0 };\r
92 \r
93 /* Handle to the task in which the co-routines run.  Used so the\r
94 co-routines can be suspended and resumed. */\r
95 static xTaskHandle xCoroutineTask;\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 /**\r
100  * Creates the tasks and co-routines used to toggle the segments of the two\r
101  * seven segment displays, as described at the top of main.c\r
102  */\r
103 void vCreateFlashTasksAndCoRoutines( void )\r
104 {\r
105 signed short sLEDTask;\r
106 \r
107         /* Create the tasks that flash segments on the first LED. */\r
108         for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )\r
109         {\r
110                 /* Spawn the task. */\r
111                 xTaskCreate( vLEDFlashTask, ( signed char * ) "LEDt", configMINIMAL_STACK_SIZE, ( void * ) sLEDTask, ( tskIDLE_PRIORITY + 1 ), &( xFlashTaskHandles[ sLEDTask ] ) );\r
112         }\r
113 \r
114         /* Create the task in which the co-routines run.  The co-routines themselves\r
115         are created within the task. */\r
116         xTaskCreate( vLEDCoRoutineControlTask, ( signed char * ) "LEDc", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCoroutineTask );\r
117 }\r
118 /*-----------------------------------------------------------*/\r
119 \r
120 void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks )\r
121 {\r
122 short sLEDTask;\r
123 \r
124         if( ucIndex == configLEFT_DISPLAY )\r
125         {\r
126                 /* Suspend or resume the tasks that are toggling the segments of the\r
127                 left side display. */\r
128                 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )\r
129                 {\r
130                         if( xFlashTaskHandles[ sLEDTask ] != NULL )\r
131                         {\r
132                                 if( sSuspendTasks == pdTRUE )\r
133                                 {\r
134                                         vTaskSuspend( xFlashTaskHandles[ sLEDTask ] );\r
135                                 }\r
136                                 else\r
137                                 {\r
138                                         vTaskResume( xFlashTaskHandles[ sLEDTask ] );\r
139                                 }\r
140                         }\r
141                 }\r
142         }\r
143         else\r
144         {\r
145                 /* Suspend or resume the task in which the co-routines are running.  The\r
146                 co-routines toggle the segments of the right side display. */\r
147                 if( sSuspendTasks == pdTRUE )\r
148                 {\r
149                         vTaskSuspend( xCoroutineTask );\r
150                 }\r
151                 else\r
152                 {\r
153                         vTaskResume( xCoroutineTask );\r
154                 }\r
155         }\r
156 }\r
157 /*-----------------------------------------------------------*/\r
158 \r
159 static void vLEDFlashTask( void * pvParameters )\r
160 {\r
161 portTickType xFlashRate, xLastFlashTime;\r
162 unsigned short usLED;\r
163 \r
164         /* The LED to flash is passed in as the task parameter. */\r
165         usLED = ( unsigned short ) pvParameters;\r
166 \r
167         /* Calculate the rate at which this task is going to toggle its LED. */\r
168         xFlashRate = ledFLASH_RATE_BASE + ( ledFLASH_RATE_BASE * ( portTickType ) usLED );\r
169         xFlashRate /= portTICK_RATE_MS;\r
170 \r
171         /* We will turn the LED on and off again in the delay period, so each\r
172         delay is only half the total period. */\r
173         xFlashRate /= ( portTickType ) 2;\r
174 \r
175         /* We need to initialise xLastFlashTime prior to the first call to \r
176         vTaskDelayUntil(). */\r
177         xLastFlashTime = xTaskGetTickCount();\r
178 \r
179         for(;;)\r
180         {\r
181                 /* Delay for half the flash period then turn the LED on. */\r
182                 vTaskDelayUntil( &xLastFlashTime, xFlashRate );\r
183                 vParTestToggleLED( usLED );\r
184 \r
185                 /* Delay for half the flash period then turn the LED off. */\r
186                 vTaskDelayUntil( &xLastFlashTime, xFlashRate );\r
187                 vParTestToggleLED( usLED );\r
188         }\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 static void vLEDCoRoutineControlTask( void *pvParameters )\r
193 {\r
194 unsigned short usCoroutine;\r
195 \r
196         ( void ) pvParameters;\r
197 \r
198         /* Create the co-routines - one of each segment of the right side display. */\r
199         for( usCoroutine = 0; usCoroutine < ledNUM_OF_LED_CO_ROUTINES; usCoroutine++ )\r
200         {\r
201                 xCoRoutineCreate( prvFixedDelayCoRoutine, ledCO_ROUTINE_PRIORITY, usCoroutine );\r
202         }\r
203 \r
204         /* This task has nothing else to do except scheduler the co-routines it just\r
205         created. */\r
206         for( ;; )\r
207         {\r
208                 vCoRoutineSchedule();\r
209         }\r
210 }\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex )\r
214 {\r
215 /* The usIndex parameter of the co-routine function is used as an index into\r
216 the xFlashRates array to obtain the delay period to use. */\r
217 static const portTickType xFlashRates[ ledNUM_OF_LED_CO_ROUTINES ] = { 150 / portTICK_RATE_MS,\r
218                                                                                                                                 300 / portTICK_RATE_MS,\r
219                                                                                                                                 450 / portTICK_RATE_MS,\r
220                                                                                                                                 600 / portTICK_RATE_MS,\r
221                                                                                                                                 750 / portTICK_RATE_MS,\r
222                                                                                                                                 900 / portTICK_RATE_MS,\r
223                                                                                                                                 1050 / portTICK_RATE_MS };\r
224 \r
225         /* Co-routines MUST start with a call to crSTART. */\r
226         crSTART( xHandle );\r
227 \r
228         for( ;; )\r
229         {\r
230                 /* Toggle the LED.  An offset of 8 is used to skip over the segments of\r
231                 the left side display which use the low numbers. */\r
232                 vParTestToggleLED( usIndex + 8 );\r
233 \r
234                 /* Delay until it is time to toggle the segment that this co-routine is\r
235                 controlling again. */\r
236                 crDELAY( xHandle, xFlashRates[ usIndex ] );\r
237         }\r
238 \r
239         /* Co-routines MUST end with a call to crEND. */\r
240         crEND();\r
241 }\r
242 /*-----------------------------------------------------------*/\r
243 \r
244 \r