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