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