2 FreeRTOS V5.4.2 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * Looking for a quick start? Then check out the FreeRTOS eBook! *
\r
29 * See http://www.FreeRTOS.org/Documentation for details *
\r
31 ***************************************************************************
\r
35 Please ensure to read the configuration and relevant port sections of the
\r
36 online documentation.
\r
38 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
41 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
44 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
45 licensing and training services.
\r
49 * Defines the tasks and co-routines used to toggle the segments of the two
\r
50 * seven segment displays, as described at the top of main.c
\r
56 /* Scheduler include files. */
\r
57 #include "FreeRTOS.h"
\r
59 #include "croutine.h"
\r
61 /* Demo program include files. */
\r
62 #include "partest.h"
\r
64 /*-----------------------------------------------------------*/
\r
66 /* One task per segment of the left side display. */
\r
67 #define ledNUM_OF_LED_TASKS ( 7 )
\r
69 /* Each task toggles at a frequency that is a multiple of 333ms. */
\r
70 #define ledFLASH_RATE_BASE ( ( portTickType ) 333 )
\r
72 /* One co-routine per segment of the right hand display. */
\r
73 #define ledNUM_OF_LED_CO_ROUTINES 7
\r
75 /* All co-routines run at the same priority. */
\r
76 #define ledCO_ROUTINE_PRIORITY 0
\r
78 /*-----------------------------------------------------------*/
\r
80 /* The task that is created 7 times. */
\r
81 static void vLEDFlashTask( void *pvParameters );
\r
83 /* The co-routine that is created 7 times. */
\r
84 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex );
\r
86 /* This task is created once, but itself creates 7 co-routines. */
\r
87 static void vLEDCoRoutineControlTask( void *pvParameters );
\r
89 /* Handles to each of the 7 tasks. Used so the tasks can be suspended
\r
91 static xTaskHandle xFlashTaskHandles[ ledNUM_OF_LED_TASKS ] = { 0 };
\r
93 /* Handle to the task in which the co-routines run. Used so the
\r
94 co-routines can be suspended and resumed. */
\r
95 static xTaskHandle xCoroutineTask;
\r
97 /*-----------------------------------------------------------*/
\r
100 * Creates the tasks and co-routines used to toggle the segments of the two
\r
101 * seven segment displays, as described at the top of main.c
\r
103 void vCreateFlashTasksAndCoRoutines( void )
\r
105 signed short sLEDTask;
\r
107 /* Create the tasks that flash segments on the first LED. */
\r
108 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
\r
110 /* Spawn the task. */
\r
111 xTaskCreate( vLEDFlashTask, ( signed char * ) "LEDt", configMINIMAL_STACK_SIZE, ( void * ) sLEDTask, ( tskIDLE_PRIORITY + 1 ), &( xFlashTaskHandles[ sLEDTask ] ) );
\r
114 /* Create the task in which the co-routines run. The co-routines themselves
\r
115 are created within the task. */
\r
116 xTaskCreate( vLEDCoRoutineControlTask, ( signed char * ) "LEDc", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCoroutineTask );
\r
118 /*-----------------------------------------------------------*/
\r
120 void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks )
\r
124 if( ucIndex == configLEFT_DISPLAY )
\r
126 /* Suspend or resume the tasks that are toggling the segments of the
\r
127 left side display. */
\r
128 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
\r
130 if( xFlashTaskHandles[ sLEDTask ] != NULL )
\r
132 if( sSuspendTasks == pdTRUE )
\r
134 vTaskSuspend( xFlashTaskHandles[ sLEDTask ] );
\r
138 vTaskResume( xFlashTaskHandles[ sLEDTask ] );
\r
145 /* Suspend or resume the task in which the co-routines are running. The
\r
146 co-routines toggle the segments of the right side display. */
\r
147 if( sSuspendTasks == pdTRUE )
\r
149 vTaskSuspend( xCoroutineTask );
\r
153 vTaskResume( xCoroutineTask );
\r
157 /*-----------------------------------------------------------*/
\r
159 static void vLEDFlashTask( void * pvParameters )
\r
161 portTickType xFlashRate, xLastFlashTime;
\r
162 unsigned short usLED;
\r
164 /* The LED to flash is passed in as the task parameter. */
\r
165 usLED = ( unsigned short ) pvParameters;
\r
167 /* Calculate the rate at which this task is going to toggle its LED. */
\r
168 xFlashRate = ledFLASH_RATE_BASE + ( ledFLASH_RATE_BASE * ( portTickType ) usLED );
\r
169 xFlashRate /= portTICK_RATE_MS;
\r
171 /* We will turn the LED on and off again in the delay period, so each
\r
172 delay is only half the total period. */
\r
173 xFlashRate /= ( portTickType ) 2;
\r
175 /* We need to initialise xLastFlashTime prior to the first call to
\r
176 vTaskDelayUntil(). */
\r
177 xLastFlashTime = xTaskGetTickCount();
\r
181 /* Delay for half the flash period then turn the LED on. */
\r
182 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
\r
183 vParTestToggleLED( usLED );
\r
185 /* Delay for half the flash period then turn the LED off. */
\r
186 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
\r
187 vParTestToggleLED( usLED );
\r
190 /*-----------------------------------------------------------*/
\r
192 static void vLEDCoRoutineControlTask( void *pvParameters )
\r
194 unsigned short usCoroutine;
\r
196 ( void ) pvParameters;
\r
198 /* Create the co-routines - one of each segment of the right side display. */
\r
199 for( usCoroutine = 0; usCoroutine < ledNUM_OF_LED_CO_ROUTINES; usCoroutine++ )
\r
201 xCoRoutineCreate( prvFixedDelayCoRoutine, ledCO_ROUTINE_PRIORITY, usCoroutine );
\r
204 /* This task has nothing else to do except scheduler the co-routines it just
\r
208 vCoRoutineSchedule();
\r
211 /*-----------------------------------------------------------*/
\r
213 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex )
\r
215 /* The usIndex parameter of the co-routine function is used as an index into
\r
216 the xFlashRates array to obtain the delay period to use. */
\r
217 static const portTickType xFlashRates[ ledNUM_OF_LED_CO_ROUTINES ] = { 150 / portTICK_RATE_MS,
\r
218 300 / portTICK_RATE_MS,
\r
219 450 / portTICK_RATE_MS,
\r
220 600 / portTICK_RATE_MS,
\r
221 750 / portTICK_RATE_MS,
\r
222 900 / portTICK_RATE_MS,
\r
223 1050 / portTICK_RATE_MS };
\r
225 /* Co-routines MUST start with a call to crSTART. */
\r
226 crSTART( xHandle );
\r
230 /* Toggle the LED. An offset of 8 is used to skip over the segments of
\r
231 the left side display which use the low numbers. */
\r
232 vParTestToggleLED( usIndex + 8 );
\r
234 /* Delay until it is time to toggle the segment that this co-routine is
\r
235 controlling again. */
\r
236 crDELAY( xHandle, xFlashRates[ usIndex ] );
\r
239 /* Co-routines MUST end with a call to crEND. */
\r
242 /*-----------------------------------------------------------*/
\r