]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MB96350_Softune_Dice_Kit/SegmentToggleTasks.c
cfdb3d8e6648bd698d43672061698c93ed63aa5a
[freertos] / FreeRTOS / Demo / MB96350_Softune_Dice_Kit / SegmentToggleTasks.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /**\r
29  * Defines the tasks and co-routines used to toggle the segments of the two\r
30  * seven segment displays, as described at the top of main.c\r
31  */\r
32 \r
33 \r
34 #include <stdlib.h>\r
35 \r
36 /* Scheduler include files. */\r
37 #include "FreeRTOS.h"\r
38 #include "task.h"\r
39 #include "croutine.h"\r
40 \r
41 /* Demo program include files. */\r
42 #include "partest.h"\r
43 \r
44 /*-----------------------------------------------------------*/\r
45 \r
46 /* One task per segment of the left side display. */\r
47 #define ledNUM_OF_LED_TASKS     ( 7 )\r
48 \r
49 /* Each task toggles at a frequency that is a multiple of 333ms. */\r
50 #define ledFLASH_RATE_BASE      ( ( TickType_t ) 333 )\r
51 \r
52 /* One co-routine per segment of the right hand display. */\r
53 #define ledNUM_OF_LED_CO_ROUTINES       7\r
54 \r
55 /* All co-routines run at the same priority. */\r
56 #define ledCO_ROUTINE_PRIORITY          0\r
57 \r
58 /*-----------------------------------------------------------*/\r
59 \r
60 /* The task that is created 7 times. */\r
61 static void vLEDFlashTask( void *pvParameters );\r
62 \r
63 /* The co-routine that is created 7 times. */\r
64 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, unsigned short usIndex );\r
65 \r
66 /* This task is created once, but itself creates 7 co-routines. */\r
67 static void vLEDCoRoutineControlTask( void *pvParameters );\r
68 \r
69 /* Handles to each of the 7 tasks.  Used so the tasks can be suspended\r
70 and resumed. */\r
71 static TaskHandle_t xFlashTaskHandles[ ledNUM_OF_LED_TASKS ] = { 0 };\r
72 \r
73 /* Handle to the task in which the co-routines run.  Used so the\r
74 co-routines can be suspended and resumed. */\r
75 static TaskHandle_t xCoroutineTask;\r
76 \r
77 /*-----------------------------------------------------------*/\r
78 \r
79 /**\r
80  * Creates the tasks and co-routines used to toggle the segments of the two\r
81  * seven segment displays, as described at the top of main.c\r
82  */\r
83 void vCreateFlashTasksAndCoRoutines( void )\r
84 {\r
85 signed short sLEDTask;\r
86 \r
87         /* Create the tasks that flash segments on the first LED. */\r
88         for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )\r
89         {\r
90                 /* Spawn the task. */\r
91                 xTaskCreate( vLEDFlashTask, "LEDt", configMINIMAL_STACK_SIZE, ( void * ) sLEDTask, ( tskIDLE_PRIORITY + 1 ), &( xFlashTaskHandles[ sLEDTask ] ) );\r
92         }\r
93 \r
94         /* Create the task in which the co-routines run.  The co-routines themselves\r
95         are created within the task. */\r
96         xTaskCreate( vLEDCoRoutineControlTask, "LEDc", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCoroutineTask );\r
97 }\r
98 /*-----------------------------------------------------------*/\r
99 \r
100 void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks )\r
101 {\r
102 short sLEDTask;\r
103 \r
104         if( ucIndex == configLEFT_DISPLAY )\r
105         {\r
106                 /* Suspend or resume the tasks that are toggling the segments of the\r
107                 left side display. */\r
108                 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )\r
109                 {\r
110                         if( xFlashTaskHandles[ sLEDTask ] != NULL )\r
111                         {\r
112                                 if( sSuspendTasks == pdTRUE )\r
113                                 {\r
114                                         vTaskSuspend( xFlashTaskHandles[ sLEDTask ] );\r
115                                 }\r
116                                 else\r
117                                 {\r
118                                         vTaskResume( xFlashTaskHandles[ sLEDTask ] );\r
119                                 }\r
120                         }\r
121                 }\r
122         }\r
123         else\r
124         {\r
125                 /* Suspend or resume the task in which the co-routines are running.  The\r
126                 co-routines toggle the segments of the right side display. */\r
127                 if( sSuspendTasks == pdTRUE )\r
128                 {\r
129                         vTaskSuspend( xCoroutineTask );\r
130                 }\r
131                 else\r
132                 {\r
133                         vTaskResume( xCoroutineTask );\r
134                 }\r
135         }\r
136 }\r
137 /*-----------------------------------------------------------*/\r
138 \r
139 static void vLEDFlashTask( void * pvParameters )\r
140 {\r
141 TickType_t xFlashRate, xLastFlashTime;\r
142 unsigned short usLED;\r
143 \r
144         /* The LED to flash is passed in as the task parameter. */\r
145         usLED = ( unsigned short ) pvParameters;\r
146 \r
147         /* Calculate the rate at which this task is going to toggle its LED. */\r
148         xFlashRate = ledFLASH_RATE_BASE + ( ledFLASH_RATE_BASE * ( TickType_t ) usLED );\r
149         xFlashRate /= portTICK_PERIOD_MS;\r
150 \r
151         /* We will turn the LED on and off again in the delay period, so each\r
152         delay is only half the total period. */\r
153         xFlashRate /= ( TickType_t ) 2;\r
154 \r
155         /* We need to initialise xLastFlashTime prior to the first call to\r
156         vTaskDelayUntil(). */\r
157         xLastFlashTime = xTaskGetTickCount();\r
158 \r
159         for(;;)\r
160         {\r
161                 /* Delay for half the flash period then turn the LED on. */\r
162                 vTaskDelayUntil( &xLastFlashTime, xFlashRate );\r
163                 vParTestToggleLED( usLED );\r
164 \r
165                 /* Delay for half the flash period then turn the LED off. */\r
166                 vTaskDelayUntil( &xLastFlashTime, xFlashRate );\r
167                 vParTestToggleLED( usLED );\r
168         }\r
169 }\r
170 /*-----------------------------------------------------------*/\r
171 \r
172 static void vLEDCoRoutineControlTask( void *pvParameters )\r
173 {\r
174 unsigned short usCoroutine;\r
175 \r
176         ( void ) pvParameters;\r
177 \r
178         /* Create the co-routines - one of each segment of the right side display. */\r
179         for( usCoroutine = 0; usCoroutine < ledNUM_OF_LED_CO_ROUTINES; usCoroutine++ )\r
180         {\r
181                 xCoRoutineCreate( prvFixedDelayCoRoutine, ledCO_ROUTINE_PRIORITY, usCoroutine );\r
182         }\r
183 \r
184         /* This task has nothing else to do except scheduler the co-routines it just\r
185         created. */\r
186         for( ;; )\r
187         {\r
188                 vCoRoutineSchedule();\r
189         }\r
190 }\r
191 /*-----------------------------------------------------------*/\r
192 \r
193 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, unsigned short usIndex )\r
194 {\r
195 /* The usIndex parameter of the co-routine function is used as an index into\r
196 the xFlashRates array to obtain the delay period to use. */\r
197 static const TickType_t xFlashRates[ ledNUM_OF_LED_CO_ROUTINES ] = { 150 / portTICK_PERIOD_MS,\r
198                                                                                                                                 300 / portTICK_PERIOD_MS,\r
199                                                                                                                                 450 / portTICK_PERIOD_MS,\r
200                                                                                                                                 600 / portTICK_PERIOD_MS,\r
201                                                                                                                                 750 / portTICK_PERIOD_MS,\r
202                                                                                                                                 900 / portTICK_PERIOD_MS,\r
203                                                                                                                                 1050 / portTICK_PERIOD_MS };\r
204 \r
205         /* Co-routines MUST start with a call to crSTART. */\r
206         crSTART( xHandle );\r
207 \r
208         for( ;; )\r
209         {\r
210                 /* Toggle the LED.  An offset of 8 is used to skip over the segments of\r
211                 the left side display which use the low numbers. */\r
212                 vParTestToggleLED( usIndex + 8 );\r
213 \r
214                 /* Delay until it is time to toggle the segment that this co-routine is\r
215                 controlling again. */\r
216                 crDELAY( xHandle, xFlashRates[ usIndex ] );\r
217         }\r
218 \r
219         /* Co-routines MUST end with a call to crEND. */\r
220         crEND();\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 \r