]> git.sur5r.net Git - freertos/blob - Demo/MB96350_Softune_Dice_Kit/SegmentToggleTasks.c
Remove unnecessary use of portLONG, portCHAR and portSHORT.
[freertos] / Demo / MB96350_Softune_Dice_Kit / SegmentToggleTasks.c
1 /*\r
2     FreeRTOS V6.0.0 - 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     * The FreeRTOS eBook and reference manual are available to purchase for a *\r
29     * small fee. Help yourself get started quickly while also helping the     *\r
30     * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *\r
31     *                                                                         *\r
32     ***************************************************************************\r
33 \r
34     1 tab == 4 spaces!\r
35 \r
36     Please ensure to read the configuration and relevant port sections of the\r
37     online documentation.\r
38 \r
39     http://www.FreeRTOS.org - Documentation, latest information, license and\r
40     contact details.\r
41 \r
42     http://www.SafeRTOS.com - A version that is certified for use in safety\r
43     critical systems.\r
44 \r
45     http://www.OpenRTOS.com - Commercial support, development, porting,\r
46     licensing and training services.\r
47 */\r
48 \r
49 /**\r
50  * Defines the tasks and co-routines used to toggle the segments of the two\r
51  * seven segment displays, as described at the top of main.c\r
52  */\r
53 \r
54 \r
55 #include <stdlib.h>\r
56 \r
57 /* Scheduler include files. */\r
58 #include "FreeRTOS.h"\r
59 #include "task.h"\r
60 #include "croutine.h"\r
61 \r
62 /* Demo program include files. */\r
63 #include "partest.h"\r
64 \r
65 /*-----------------------------------------------------------*/\r
66 \r
67 /* One task per segment of the left side display. */\r
68 #define ledNUM_OF_LED_TASKS     ( 7 )\r
69 \r
70 /* Each task toggles at a frequency that is a multiple of 333ms. */\r
71 #define ledFLASH_RATE_BASE      ( ( portTickType ) 333 )\r
72 \r
73 /* One co-routine per segment of the right hand display. */\r
74 #define ledNUM_OF_LED_CO_ROUTINES       7\r
75 \r
76 /* All co-routines run at the same priority. */\r
77 #define ledCO_ROUTINE_PRIORITY          0\r
78 \r
79 /*-----------------------------------------------------------*/\r
80 \r
81 /* The task that is created 7 times. */\r
82 static void vLEDFlashTask( void *pvParameters );\r
83 \r
84 /* The co-routine that is created 7 times. */\r
85 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex );\r
86 \r
87 /* This task is created once, but itself creates 7 co-routines. */\r
88 static void vLEDCoRoutineControlTask( void *pvParameters );\r
89 \r
90 /* Handles to each of the 7 tasks.  Used so the tasks can be suspended\r
91 and resumed. */\r
92 static xTaskHandle xFlashTaskHandles[ ledNUM_OF_LED_TASKS ] = { 0 };\r
93 \r
94 /* Handle to the task in which the co-routines run.  Used so the\r
95 co-routines can be suspended and resumed. */\r
96 static xTaskHandle xCoroutineTask;\r
97 \r
98 /*-----------------------------------------------------------*/\r
99 \r
100 /**\r
101  * Creates the tasks and co-routines used to toggle the segments of the two\r
102  * seven segment displays, as described at the top of main.c\r
103  */\r
104 void vCreateFlashTasksAndCoRoutines( void )\r
105 {\r
106 signed short sLEDTask;\r
107 \r
108         /* Create the tasks that flash segments on the first LED. */\r
109         for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )\r
110         {\r
111                 /* Spawn the task. */\r
112                 xTaskCreate( vLEDFlashTask, ( signed char * ) "LEDt", configMINIMAL_STACK_SIZE, ( void * ) sLEDTask, ( tskIDLE_PRIORITY + 1 ), &( xFlashTaskHandles[ sLEDTask ] ) );\r
113         }\r
114 \r
115         /* Create the task in which the co-routines run.  The co-routines themselves\r
116         are created within the task. */\r
117         xTaskCreate( vLEDCoRoutineControlTask, ( signed char * ) "LEDc", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCoroutineTask );\r
118 }\r
119 /*-----------------------------------------------------------*/\r
120 \r
121 void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks )\r
122 {\r
123 short sLEDTask;\r
124 \r
125         if( ucIndex == configLEFT_DISPLAY )\r
126         {\r
127                 /* Suspend or resume the tasks that are toggling the segments of the\r
128                 left side display. */\r
129                 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )\r
130                 {\r
131                         if( xFlashTaskHandles[ sLEDTask ] != NULL )\r
132                         {\r
133                                 if( sSuspendTasks == pdTRUE )\r
134                                 {\r
135                                         vTaskSuspend( xFlashTaskHandles[ sLEDTask ] );\r
136                                 }\r
137                                 else\r
138                                 {\r
139                                         vTaskResume( xFlashTaskHandles[ sLEDTask ] );\r
140                                 }\r
141                         }\r
142                 }\r
143         }\r
144         else\r
145         {\r
146                 /* Suspend or resume the task in which the co-routines are running.  The\r
147                 co-routines toggle the segments of the right side display. */\r
148                 if( sSuspendTasks == pdTRUE )\r
149                 {\r
150                         vTaskSuspend( xCoroutineTask );\r
151                 }\r
152                 else\r
153                 {\r
154                         vTaskResume( xCoroutineTask );\r
155                 }\r
156         }\r
157 }\r
158 /*-----------------------------------------------------------*/\r
159 \r
160 static void vLEDFlashTask( void * pvParameters )\r
161 {\r
162 portTickType xFlashRate, xLastFlashTime;\r
163 unsigned short usLED;\r
164 \r
165         /* The LED to flash is passed in as the task parameter. */\r
166         usLED = ( unsigned short ) pvParameters;\r
167 \r
168         /* Calculate the rate at which this task is going to toggle its LED. */\r
169         xFlashRate = ledFLASH_RATE_BASE + ( ledFLASH_RATE_BASE * ( portTickType ) usLED );\r
170         xFlashRate /= portTICK_RATE_MS;\r
171 \r
172         /* We will turn the LED on and off again in the delay period, so each\r
173         delay is only half the total period. */\r
174         xFlashRate /= ( portTickType ) 2;\r
175 \r
176         /* We need to initialise xLastFlashTime prior to the first call to \r
177         vTaskDelayUntil(). */\r
178         xLastFlashTime = xTaskGetTickCount();\r
179 \r
180         for(;;)\r
181         {\r
182                 /* Delay for half the flash period then turn the LED on. */\r
183                 vTaskDelayUntil( &xLastFlashTime, xFlashRate );\r
184                 vParTestToggleLED( usLED );\r
185 \r
186                 /* Delay for half the flash period then turn the LED off. */\r
187                 vTaskDelayUntil( &xLastFlashTime, xFlashRate );\r
188                 vParTestToggleLED( usLED );\r
189         }\r
190 }\r
191 /*-----------------------------------------------------------*/\r
192 \r
193 static void vLEDCoRoutineControlTask( void *pvParameters )\r
194 {\r
195 unsigned short usCoroutine;\r
196 \r
197         ( void ) pvParameters;\r
198 \r
199         /* Create the co-routines - one of each segment of the right side display. */\r
200         for( usCoroutine = 0; usCoroutine < ledNUM_OF_LED_CO_ROUTINES; usCoroutine++ )\r
201         {\r
202                 xCoRoutineCreate( prvFixedDelayCoRoutine, ledCO_ROUTINE_PRIORITY, usCoroutine );\r
203         }\r
204 \r
205         /* This task has nothing else to do except scheduler the co-routines it just\r
206         created. */\r
207         for( ;; )\r
208         {\r
209                 vCoRoutineSchedule();\r
210         }\r
211 }\r
212 /*-----------------------------------------------------------*/\r
213 \r
214 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex )\r
215 {\r
216 /* The usIndex parameter of the co-routine function is used as an index into\r
217 the xFlashRates array to obtain the delay period to use. */\r
218 static const portTickType xFlashRates[ ledNUM_OF_LED_CO_ROUTINES ] = { 150 / portTICK_RATE_MS,\r
219                                                                                                                                 300 / portTICK_RATE_MS,\r
220                                                                                                                                 450 / portTICK_RATE_MS,\r
221                                                                                                                                 600 / portTICK_RATE_MS,\r
222                                                                                                                                 750 / portTICK_RATE_MS,\r
223                                                                                                                                 900 / portTICK_RATE_MS,\r
224                                                                                                                                 1050 / portTICK_RATE_MS };\r
225 \r
226         /* Co-routines MUST start with a call to crSTART. */\r
227         crSTART( xHandle );\r
228 \r
229         for( ;; )\r
230         {\r
231                 /* Toggle the LED.  An offset of 8 is used to skip over the segments of\r
232                 the left side display which use the low numbers. */\r
233                 vParTestToggleLED( usIndex + 8 );\r
234 \r
235                 /* Delay until it is time to toggle the segment that this co-routine is\r
236                 controlling again. */\r
237                 crDELAY( xHandle, xFlashRates[ usIndex ] );\r
238         }\r
239 \r
240         /* Co-routines MUST end with a call to crEND. */\r
241         crEND();\r
242 }\r
243 /*-----------------------------------------------------------*/\r
244 \r
245 \r