]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MB96350_Softune_Dice_Kit/SegmentToggleTasks.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / MB96350_Softune_Dice_Kit / SegmentToggleTasks.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 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     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /**\r
68  * Defines the tasks and co-routines used to toggle the segments of the two\r
69  * seven segment displays, as described at the top of main.c\r
70  */\r
71 \r
72 \r
73 #include <stdlib.h>\r
74 \r
75 /* Scheduler include files. */\r
76 #include "FreeRTOS.h"\r
77 #include "task.h"\r
78 #include "croutine.h"\r
79 \r
80 /* Demo program include files. */\r
81 #include "partest.h"\r
82 \r
83 /*-----------------------------------------------------------*/\r
84 \r
85 /* One task per segment of the left side display. */\r
86 #define ledNUM_OF_LED_TASKS     ( 7 )\r
87 \r
88 /* Each task toggles at a frequency that is a multiple of 333ms. */\r
89 #define ledFLASH_RATE_BASE      ( ( portTickType ) 333 )\r
90 \r
91 /* One co-routine per segment of the right hand display. */\r
92 #define ledNUM_OF_LED_CO_ROUTINES       7\r
93 \r
94 /* All co-routines run at the same priority. */\r
95 #define ledCO_ROUTINE_PRIORITY          0\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 /* The task that is created 7 times. */\r
100 static void vLEDFlashTask( void *pvParameters );\r
101 \r
102 /* The co-routine that is created 7 times. */\r
103 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex );\r
104 \r
105 /* This task is created once, but itself creates 7 co-routines. */\r
106 static void vLEDCoRoutineControlTask( void *pvParameters );\r
107 \r
108 /* Handles to each of the 7 tasks.  Used so the tasks can be suspended\r
109 and resumed. */\r
110 static xTaskHandle xFlashTaskHandles[ ledNUM_OF_LED_TASKS ] = { 0 };\r
111 \r
112 /* Handle to the task in which the co-routines run.  Used so the\r
113 co-routines can be suspended and resumed. */\r
114 static xTaskHandle xCoroutineTask;\r
115 \r
116 /*-----------------------------------------------------------*/\r
117 \r
118 /**\r
119  * Creates the tasks and co-routines used to toggle the segments of the two\r
120  * seven segment displays, as described at the top of main.c\r
121  */\r
122 void vCreateFlashTasksAndCoRoutines( void )\r
123 {\r
124 signed short sLEDTask;\r
125 \r
126         /* Create the tasks that flash segments on the first LED. */\r
127         for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )\r
128         {\r
129                 /* Spawn the task. */\r
130                 xTaskCreate( vLEDFlashTask, ( signed char * ) "LEDt", configMINIMAL_STACK_SIZE, ( void * ) sLEDTask, ( tskIDLE_PRIORITY + 1 ), &( xFlashTaskHandles[ sLEDTask ] ) );\r
131         }\r
132 \r
133         /* Create the task in which the co-routines run.  The co-routines themselves\r
134         are created within the task. */\r
135         xTaskCreate( vLEDCoRoutineControlTask, ( signed char * ) "LEDc", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCoroutineTask );\r
136 }\r
137 /*-----------------------------------------------------------*/\r
138 \r
139 void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks )\r
140 {\r
141 short sLEDTask;\r
142 \r
143         if( ucIndex == configLEFT_DISPLAY )\r
144         {\r
145                 /* Suspend or resume the tasks that are toggling the segments of the\r
146                 left side display. */\r
147                 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )\r
148                 {\r
149                         if( xFlashTaskHandles[ sLEDTask ] != NULL )\r
150                         {\r
151                                 if( sSuspendTasks == pdTRUE )\r
152                                 {\r
153                                         vTaskSuspend( xFlashTaskHandles[ sLEDTask ] );\r
154                                 }\r
155                                 else\r
156                                 {\r
157                                         vTaskResume( xFlashTaskHandles[ sLEDTask ] );\r
158                                 }\r
159                         }\r
160                 }\r
161         }\r
162         else\r
163         {\r
164                 /* Suspend or resume the task in which the co-routines are running.  The\r
165                 co-routines toggle the segments of the right side display. */\r
166                 if( sSuspendTasks == pdTRUE )\r
167                 {\r
168                         vTaskSuspend( xCoroutineTask );\r
169                 }\r
170                 else\r
171                 {\r
172                         vTaskResume( xCoroutineTask );\r
173                 }\r
174         }\r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 static void vLEDFlashTask( void * pvParameters )\r
179 {\r
180 portTickType xFlashRate, xLastFlashTime;\r
181 unsigned short usLED;\r
182 \r
183         /* The LED to flash is passed in as the task parameter. */\r
184         usLED = ( unsigned short ) pvParameters;\r
185 \r
186         /* Calculate the rate at which this task is going to toggle its LED. */\r
187         xFlashRate = ledFLASH_RATE_BASE + ( ledFLASH_RATE_BASE * ( portTickType ) usLED );\r
188         xFlashRate /= portTICK_RATE_MS;\r
189 \r
190         /* We will turn the LED on and off again in the delay period, so each\r
191         delay is only half the total period. */\r
192         xFlashRate /= ( portTickType ) 2;\r
193 \r
194         /* We need to initialise xLastFlashTime prior to the first call to \r
195         vTaskDelayUntil(). */\r
196         xLastFlashTime = xTaskGetTickCount();\r
197 \r
198         for(;;)\r
199         {\r
200                 /* Delay for half the flash period then turn the LED on. */\r
201                 vTaskDelayUntil( &xLastFlashTime, xFlashRate );\r
202                 vParTestToggleLED( usLED );\r
203 \r
204                 /* Delay for half the flash period then turn the LED off. */\r
205                 vTaskDelayUntil( &xLastFlashTime, xFlashRate );\r
206                 vParTestToggleLED( usLED );\r
207         }\r
208 }\r
209 /*-----------------------------------------------------------*/\r
210 \r
211 static void vLEDCoRoutineControlTask( void *pvParameters )\r
212 {\r
213 unsigned short usCoroutine;\r
214 \r
215         ( void ) pvParameters;\r
216 \r
217         /* Create the co-routines - one of each segment of the right side display. */\r
218         for( usCoroutine = 0; usCoroutine < ledNUM_OF_LED_CO_ROUTINES; usCoroutine++ )\r
219         {\r
220                 xCoRoutineCreate( prvFixedDelayCoRoutine, ledCO_ROUTINE_PRIORITY, usCoroutine );\r
221         }\r
222 \r
223         /* This task has nothing else to do except scheduler the co-routines it just\r
224         created. */\r
225         for( ;; )\r
226         {\r
227                 vCoRoutineSchedule();\r
228         }\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex )\r
233 {\r
234 /* The usIndex parameter of the co-routine function is used as an index into\r
235 the xFlashRates array to obtain the delay period to use. */\r
236 static const portTickType xFlashRates[ ledNUM_OF_LED_CO_ROUTINES ] = { 150 / portTICK_RATE_MS,\r
237                                                                                                                                 300 / portTICK_RATE_MS,\r
238                                                                                                                                 450 / portTICK_RATE_MS,\r
239                                                                                                                                 600 / portTICK_RATE_MS,\r
240                                                                                                                                 750 / portTICK_RATE_MS,\r
241                                                                                                                                 900 / portTICK_RATE_MS,\r
242                                                                                                                                 1050 / portTICK_RATE_MS };\r
243 \r
244         /* Co-routines MUST start with a call to crSTART. */\r
245         crSTART( xHandle );\r
246 \r
247         for( ;; )\r
248         {\r
249                 /* Toggle the LED.  An offset of 8 is used to skip over the segments of\r
250                 the left side display which use the low numbers. */\r
251                 vParTestToggleLED( usIndex + 8 );\r
252 \r
253                 /* Delay until it is time to toggle the segment that this co-routine is\r
254                 controlling again. */\r
255                 crDELAY( xHandle, xFlashRates[ usIndex ] );\r
256         }\r
257 \r
258         /* Co-routines MUST end with a call to crEND. */\r
259         crEND();\r
260 }\r
261 /*-----------------------------------------------------------*/\r
262 \r
263 \r