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