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