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