2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\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
36 /* Scheduler include files. */
\r
37 #include "FreeRTOS.h"
\r
39 #include "croutine.h"
\r
41 /* Demo program include files. */
\r
42 #include "partest.h"
\r
44 /*-----------------------------------------------------------*/
\r
46 /* One task per segment of the left side display. */
\r
47 #define ledNUM_OF_LED_TASKS ( 7 )
\r
49 /* Each task toggles at a frequency that is a multiple of 333ms. */
\r
50 #define ledFLASH_RATE_BASE ( ( TickType_t ) 333 )
\r
52 /* One co-routine per segment of the right hand display. */
\r
53 #define ledNUM_OF_LED_CO_ROUTINES 7
\r
55 /* All co-routines run at the same priority. */
\r
56 #define ledCO_ROUTINE_PRIORITY 0
\r
58 /*-----------------------------------------------------------*/
\r
60 /* The task that is created 7 times. */
\r
61 static void vLEDFlashTask( void *pvParameters );
\r
63 /* The co-routine that is created 7 times. */
\r
64 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, unsigned short usIndex );
\r
66 /* This task is created once, but itself creates 7 co-routines. */
\r
67 static void vLEDCoRoutineControlTask( void *pvParameters );
\r
69 /* Handles to each of the 7 tasks. Used so the tasks can be suspended
\r
71 static TaskHandle_t xFlashTaskHandles[ ledNUM_OF_LED_TASKS ] = { 0 };
\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
77 /*-----------------------------------------------------------*/
\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
83 void vCreateFlashTasksAndCoRoutines( void )
\r
85 signed short sLEDTask;
\r
87 /* Create the tasks that flash segments on the first LED. */
\r
88 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
\r
90 /* Spawn the task. */
\r
91 xTaskCreate( vLEDFlashTask, "LEDt", configMINIMAL_STACK_SIZE, ( void * ) sLEDTask, ( tskIDLE_PRIORITY + 1 ), &( xFlashTaskHandles[ sLEDTask ] ) );
\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
98 /*-----------------------------------------------------------*/
\r
100 void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks )
\r
104 if( ucIndex == configLEFT_DISPLAY )
\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
110 if( xFlashTaskHandles[ sLEDTask ] != NULL )
\r
112 if( sSuspendTasks == pdTRUE )
\r
114 vTaskSuspend( xFlashTaskHandles[ sLEDTask ] );
\r
118 vTaskResume( xFlashTaskHandles[ sLEDTask ] );
\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
129 vTaskSuspend( xCoroutineTask );
\r
133 vTaskResume( xCoroutineTask );
\r
137 /*-----------------------------------------------------------*/
\r
139 static void vLEDFlashTask( void * pvParameters )
\r
141 TickType_t xFlashRate, xLastFlashTime;
\r
142 unsigned short usLED;
\r
144 /* The LED to flash is passed in as the task parameter. */
\r
145 usLED = ( unsigned short ) pvParameters;
\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
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
155 /* We need to initialise xLastFlashTime prior to the first call to
\r
156 vTaskDelayUntil(). */
\r
157 xLastFlashTime = xTaskGetTickCount();
\r
161 /* Delay for half the flash period then turn the LED on. */
\r
162 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
\r
163 vParTestToggleLED( usLED );
\r
165 /* Delay for half the flash period then turn the LED off. */
\r
166 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
\r
167 vParTestToggleLED( usLED );
\r
170 /*-----------------------------------------------------------*/
\r
172 static void vLEDCoRoutineControlTask( void *pvParameters )
\r
174 unsigned short usCoroutine;
\r
176 ( void ) pvParameters;
\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
181 xCoRoutineCreate( prvFixedDelayCoRoutine, ledCO_ROUTINE_PRIORITY, usCoroutine );
\r
184 /* This task has nothing else to do except scheduler the co-routines it just
\r
188 vCoRoutineSchedule();
\r
191 /*-----------------------------------------------------------*/
\r
193 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, unsigned short usIndex )
\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
205 /* Co-routines MUST start with a call to crSTART. */
\r
206 crSTART( xHandle );
\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
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
219 /* Co-routines MUST end with a call to crEND. */
\r
222 /*-----------------------------------------------------------*/
\r