]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MB96350_Softune_Dice_Kit/SegmentToggleTasks.c
Prepare for V7.4.0 release.
[freertos] / FreeRTOS / Demo / MB96350_Softune_Dice_Kit / SegmentToggleTasks.c
1 /*\r
2     FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not itcan be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /**\r
76  * Defines the tasks and co-routines used to toggle the segments of the two\r
77  * seven segment displays, as described at the top of main.c\r
78  */\r
79 \r
80 \r
81 #include <stdlib.h>\r
82 \r
83 /* Scheduler include files. */\r
84 #include "FreeRTOS.h"\r
85 #include "task.h"\r
86 #include "croutine.h"\r
87 \r
88 /* Demo program include files. */\r
89 #include "partest.h"\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 /* One task per segment of the left side display. */\r
94 #define ledNUM_OF_LED_TASKS     ( 7 )\r
95 \r
96 /* Each task toggles at a frequency that is a multiple of 333ms. */\r
97 #define ledFLASH_RATE_BASE      ( ( portTickType ) 333 )\r
98 \r
99 /* One co-routine per segment of the right hand display. */\r
100 #define ledNUM_OF_LED_CO_ROUTINES       7\r
101 \r
102 /* All co-routines run at the same priority. */\r
103 #define ledCO_ROUTINE_PRIORITY          0\r
104 \r
105 /*-----------------------------------------------------------*/\r
106 \r
107 /* The task that is created 7 times. */\r
108 static void vLEDFlashTask( void *pvParameters );\r
109 \r
110 /* The co-routine that is created 7 times. */\r
111 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex );\r
112 \r
113 /* This task is created once, but itself creates 7 co-routines. */\r
114 static void vLEDCoRoutineControlTask( void *pvParameters );\r
115 \r
116 /* Handles to each of the 7 tasks.  Used so the tasks can be suspended\r
117 and resumed. */\r
118 static xTaskHandle xFlashTaskHandles[ ledNUM_OF_LED_TASKS ] = { 0 };\r
119 \r
120 /* Handle to the task in which the co-routines run.  Used so the\r
121 co-routines can be suspended and resumed. */\r
122 static xTaskHandle xCoroutineTask;\r
123 \r
124 /*-----------------------------------------------------------*/\r
125 \r
126 /**\r
127  * Creates the tasks and co-routines used to toggle the segments of the two\r
128  * seven segment displays, as described at the top of main.c\r
129  */\r
130 void vCreateFlashTasksAndCoRoutines( void )\r
131 {\r
132 signed short sLEDTask;\r
133 \r
134         /* Create the tasks that flash segments on the first LED. */\r
135         for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )\r
136         {\r
137                 /* Spawn the task. */\r
138                 xTaskCreate( vLEDFlashTask, ( signed char * ) "LEDt", configMINIMAL_STACK_SIZE, ( void * ) sLEDTask, ( tskIDLE_PRIORITY + 1 ), &( xFlashTaskHandles[ sLEDTask ] ) );\r
139         }\r
140 \r
141         /* Create the task in which the co-routines run.  The co-routines themselves\r
142         are created within the task. */\r
143         xTaskCreate( vLEDCoRoutineControlTask, ( signed char * ) "LEDc", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCoroutineTask );\r
144 }\r
145 /*-----------------------------------------------------------*/\r
146 \r
147 void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks )\r
148 {\r
149 short sLEDTask;\r
150 \r
151         if( ucIndex == configLEFT_DISPLAY )\r
152         {\r
153                 /* Suspend or resume the tasks that are toggling the segments of the\r
154                 left side display. */\r
155                 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )\r
156                 {\r
157                         if( xFlashTaskHandles[ sLEDTask ] != NULL )\r
158                         {\r
159                                 if( sSuspendTasks == pdTRUE )\r
160                                 {\r
161                                         vTaskSuspend( xFlashTaskHandles[ sLEDTask ] );\r
162                                 }\r
163                                 else\r
164                                 {\r
165                                         vTaskResume( xFlashTaskHandles[ sLEDTask ] );\r
166                                 }\r
167                         }\r
168                 }\r
169         }\r
170         else\r
171         {\r
172                 /* Suspend or resume the task in which the co-routines are running.  The\r
173                 co-routines toggle the segments of the right side display. */\r
174                 if( sSuspendTasks == pdTRUE )\r
175                 {\r
176                         vTaskSuspend( xCoroutineTask );\r
177                 }\r
178                 else\r
179                 {\r
180                         vTaskResume( xCoroutineTask );\r
181                 }\r
182         }\r
183 }\r
184 /*-----------------------------------------------------------*/\r
185 \r
186 static void vLEDFlashTask( void * pvParameters )\r
187 {\r
188 portTickType xFlashRate, xLastFlashTime;\r
189 unsigned short usLED;\r
190 \r
191         /* The LED to flash is passed in as the task parameter. */\r
192         usLED = ( unsigned short ) pvParameters;\r
193 \r
194         /* Calculate the rate at which this task is going to toggle its LED. */\r
195         xFlashRate = ledFLASH_RATE_BASE + ( ledFLASH_RATE_BASE * ( portTickType ) usLED );\r
196         xFlashRate /= portTICK_RATE_MS;\r
197 \r
198         /* We will turn the LED on and off again in the delay period, so each\r
199         delay is only half the total period. */\r
200         xFlashRate /= ( portTickType ) 2;\r
201 \r
202         /* We need to initialise xLastFlashTime prior to the first call to \r
203         vTaskDelayUntil(). */\r
204         xLastFlashTime = xTaskGetTickCount();\r
205 \r
206         for(;;)\r
207         {\r
208                 /* Delay for half the flash period then turn the LED on. */\r
209                 vTaskDelayUntil( &xLastFlashTime, xFlashRate );\r
210                 vParTestToggleLED( usLED );\r
211 \r
212                 /* Delay for half the flash period then turn the LED off. */\r
213                 vTaskDelayUntil( &xLastFlashTime, xFlashRate );\r
214                 vParTestToggleLED( usLED );\r
215         }\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 static void vLEDCoRoutineControlTask( void *pvParameters )\r
220 {\r
221 unsigned short usCoroutine;\r
222 \r
223         ( void ) pvParameters;\r
224 \r
225         /* Create the co-routines - one of each segment of the right side display. */\r
226         for( usCoroutine = 0; usCoroutine < ledNUM_OF_LED_CO_ROUTINES; usCoroutine++ )\r
227         {\r
228                 xCoRoutineCreate( prvFixedDelayCoRoutine, ledCO_ROUTINE_PRIORITY, usCoroutine );\r
229         }\r
230 \r
231         /* This task has nothing else to do except scheduler the co-routines it just\r
232         created. */\r
233         for( ;; )\r
234         {\r
235                 vCoRoutineSchedule();\r
236         }\r
237 }\r
238 /*-----------------------------------------------------------*/\r
239 \r
240 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex )\r
241 {\r
242 /* The usIndex parameter of the co-routine function is used as an index into\r
243 the xFlashRates array to obtain the delay period to use. */\r
244 static const portTickType xFlashRates[ ledNUM_OF_LED_CO_ROUTINES ] = { 150 / portTICK_RATE_MS,\r
245                                                                                                                                 300 / portTICK_RATE_MS,\r
246                                                                                                                                 450 / portTICK_RATE_MS,\r
247                                                                                                                                 600 / portTICK_RATE_MS,\r
248                                                                                                                                 750 / portTICK_RATE_MS,\r
249                                                                                                                                 900 / portTICK_RATE_MS,\r
250                                                                                                                                 1050 / portTICK_RATE_MS };\r
251 \r
252         /* Co-routines MUST start with a call to crSTART. */\r
253         crSTART( xHandle );\r
254 \r
255         for( ;; )\r
256         {\r
257                 /* Toggle the LED.  An offset of 8 is used to skip over the segments of\r
258                 the left side display which use the low numbers. */\r
259                 vParTestToggleLED( usIndex + 8 );\r
260 \r
261                 /* Delay until it is time to toggle the segment that this co-routine is\r
262                 controlling again. */\r
263                 crDELAY( xHandle, xFlashRates[ usIndex ] );\r
264         }\r
265 \r
266         /* Co-routines MUST end with a call to crEND. */\r
267         crEND();\r
268 }\r
269 /*-----------------------------------------------------------*/\r
270 \r
271 \r