3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
29 * Defines the tasks and co-routines used to toggle the segments of the two
30 * seven segment displays, as described at the top of main.c
36 /* Scheduler include files. */
41 /* Demo program include files. */
44 /*-----------------------------------------------------------*/
46 /* One task per segment of the left side display. */
47 #define ledNUM_OF_LED_TASKS ( 7 )
49 /* Each task toggles at a frequency that is a multiple of 333ms. */
50 #define ledFLASH_RATE_BASE ( ( TickType_t ) 333 )
52 /* One co-routine per segment of the right hand display. */
53 #define ledNUM_OF_LED_CO_ROUTINES 7
55 /* All co-routines run at the same priority. */
56 #define ledCO_ROUTINE_PRIORITY 0
58 /*-----------------------------------------------------------*/
60 /* The task that is created 7 times. */
61 static void vLEDFlashTask( void *pvParameters );
63 /* The co-routine that is created 7 times. */
64 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, unsigned short usIndex );
66 /* This task is created once, but itself creates 7 co-routines. */
67 static void vLEDCoRoutineControlTask( void *pvParameters );
69 /* Handles to each of the 7 tasks. Used so the tasks can be suspended
71 static TaskHandle_t xFlashTaskHandles[ ledNUM_OF_LED_TASKS ] = { 0 };
73 /* Handle to the task in which the co-routines run. Used so the
74 co-routines can be suspended and resumed. */
75 static TaskHandle_t xCoroutineTask;
77 /*-----------------------------------------------------------*/
80 * Creates the tasks and co-routines used to toggle the segments of the two
81 * seven segment displays, as described at the top of main.c
83 void vCreateFlashTasksAndCoRoutines( void )
85 signed short sLEDTask;
87 /* Create the tasks that flash segments on the first LED. */
88 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
91 xTaskCreate( vLEDFlashTask, "LEDt", configMINIMAL_STACK_SIZE, ( void * ) sLEDTask, ( tskIDLE_PRIORITY + 1 ), &( xFlashTaskHandles[ sLEDTask ] ) );
94 /* Create the task in which the co-routines run. The co-routines themselves
95 are created within the task. */
96 xTaskCreate( vLEDCoRoutineControlTask, "LEDc", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCoroutineTask );
98 /*-----------------------------------------------------------*/
100 void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks )
104 if( ucIndex == configLEFT_DISPLAY )
106 /* Suspend or resume the tasks that are toggling the segments of the
107 left side display. */
108 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
110 if( xFlashTaskHandles[ sLEDTask ] != NULL )
112 if( sSuspendTasks == pdTRUE )
114 vTaskSuspend( xFlashTaskHandles[ sLEDTask ] );
118 vTaskResume( xFlashTaskHandles[ sLEDTask ] );
125 /* Suspend or resume the task in which the co-routines are running. The
126 co-routines toggle the segments of the right side display. */
127 if( sSuspendTasks == pdTRUE )
129 vTaskSuspend( xCoroutineTask );
133 vTaskResume( xCoroutineTask );
137 /*-----------------------------------------------------------*/
139 static void vLEDFlashTask( void * pvParameters )
141 TickType_t xFlashRate, xLastFlashTime;
142 unsigned short usLED;
144 /* The LED to flash is passed in as the task parameter. */
145 usLED = ( unsigned short ) pvParameters;
147 /* Calculate the rate at which this task is going to toggle its LED. */
148 xFlashRate = ledFLASH_RATE_BASE + ( ledFLASH_RATE_BASE * ( TickType_t ) usLED );
149 xFlashRate /= portTICK_PERIOD_MS;
151 /* We will turn the LED on and off again in the delay period, so each
152 delay is only half the total period. */
153 xFlashRate /= ( TickType_t ) 2;
155 /* We need to initialise xLastFlashTime prior to the first call to
156 vTaskDelayUntil(). */
157 xLastFlashTime = xTaskGetTickCount();
161 /* Delay for half the flash period then turn the LED on. */
162 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
163 vParTestToggleLED( usLED );
165 /* Delay for half the flash period then turn the LED off. */
166 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
167 vParTestToggleLED( usLED );
170 /*-----------------------------------------------------------*/
172 static void vLEDCoRoutineControlTask( void *pvParameters )
174 unsigned short usCoroutine;
176 ( void ) pvParameters;
178 /* Create the co-routines - one of each segment of the right side display. */
179 for( usCoroutine = 0; usCoroutine < ledNUM_OF_LED_CO_ROUTINES; usCoroutine++ )
181 xCoRoutineCreate( prvFixedDelayCoRoutine, ledCO_ROUTINE_PRIORITY, usCoroutine );
184 /* This task has nothing else to do except scheduler the co-routines it just
188 vCoRoutineSchedule();
191 /*-----------------------------------------------------------*/
193 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, unsigned short usIndex )
195 /* The usIndex parameter of the co-routine function is used as an index into
196 the xFlashRates array to obtain the delay period to use. */
197 static const TickType_t xFlashRates[ ledNUM_OF_LED_CO_ROUTINES ] = { 150 / portTICK_PERIOD_MS,
198 300 / portTICK_PERIOD_MS,
199 450 / portTICK_PERIOD_MS,
200 600 / portTICK_PERIOD_MS,
201 750 / portTICK_PERIOD_MS,
202 900 / portTICK_PERIOD_MS,
203 1050 / portTICK_PERIOD_MS };
205 /* Co-routines MUST start with a call to crSTART. */
210 /* Toggle the LED. An offset of 8 is used to skip over the segments of
211 the left side display which use the low numbers. */
212 vParTestToggleLED( usIndex + 8 );
214 /* Delay until it is time to toggle the segment that this co-routine is
215 controlling again. */
216 crDELAY( xHandle, xFlashRates[ usIndex ] );
219 /* Co-routines MUST end with a call to crEND. */
222 /*-----------------------------------------------------------*/