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