2 FreeRTOS V6.0.0 - 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 * The FreeRTOS eBook and reference manual are available to purchase for a *
\r
29 * small fee. Help yourself get started quickly while also helping the *
\r
30 * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *
\r
32 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
50 * Defines the tasks and co-routines used to toggle the segments of the two
\r
51 * seven segment displays, as described at the top of main.c
\r
57 /* Scheduler include files. */
\r
58 #include "FreeRTOS.h"
\r
60 #include "croutine.h"
\r
62 /* Demo program include files. */
\r
63 #include "partest.h"
\r
65 /*-----------------------------------------------------------*/
\r
67 /* One task per segment of the left side display. */
\r
68 #define ledNUM_OF_LED_TASKS ( 7 )
\r
70 /* Each task toggles at a frequency that is a multiple of 333ms. */
\r
71 #define ledFLASH_RATE_BASE ( ( portTickType ) 333 )
\r
73 /* One co-routine per segment of the right hand display. */
\r
74 #define ledNUM_OF_LED_CO_ROUTINES 7
\r
76 /* All co-routines run at the same priority. */
\r
77 #define ledCO_ROUTINE_PRIORITY 0
\r
79 /*-----------------------------------------------------------*/
\r
81 /* The task that is created 7 times. */
\r
82 static void vLEDFlashTask( void *pvParameters );
\r
84 /* The co-routine that is created 7 times. */
\r
85 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex );
\r
87 /* This task is created once, but itself creates 7 co-routines. */
\r
88 static void vLEDCoRoutineControlTask( void *pvParameters );
\r
90 /* Handles to each of the 7 tasks. Used so the tasks can be suspended
\r
92 static xTaskHandle xFlashTaskHandles[ ledNUM_OF_LED_TASKS ] = { 0 };
\r
94 /* Handle to the task in which the co-routines run. Used so the
\r
95 co-routines can be suspended and resumed. */
\r
96 static xTaskHandle xCoroutineTask;
\r
98 /*-----------------------------------------------------------*/
\r
101 * Creates the tasks and co-routines used to toggle the segments of the two
\r
102 * seven segment displays, as described at the top of main.c
\r
104 void vCreateFlashTasksAndCoRoutines( void )
\r
106 signed short sLEDTask;
\r
108 /* Create the tasks that flash segments on the first LED. */
\r
109 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
\r
111 /* Spawn the task. */
\r
112 xTaskCreate( vLEDFlashTask, ( signed char * ) "LEDt", configMINIMAL_STACK_SIZE, ( void * ) sLEDTask, ( tskIDLE_PRIORITY + 1 ), &( xFlashTaskHandles[ sLEDTask ] ) );
\r
115 /* Create the task in which the co-routines run. The co-routines themselves
\r
116 are created within the task. */
\r
117 xTaskCreate( vLEDCoRoutineControlTask, ( signed char * ) "LEDc", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCoroutineTask );
\r
119 /*-----------------------------------------------------------*/
\r
121 void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks )
\r
125 if( ucIndex == configLEFT_DISPLAY )
\r
127 /* Suspend or resume the tasks that are toggling the segments of the
\r
128 left side display. */
\r
129 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
\r
131 if( xFlashTaskHandles[ sLEDTask ] != NULL )
\r
133 if( sSuspendTasks == pdTRUE )
\r
135 vTaskSuspend( xFlashTaskHandles[ sLEDTask ] );
\r
139 vTaskResume( xFlashTaskHandles[ sLEDTask ] );
\r
146 /* Suspend or resume the task in which the co-routines are running. The
\r
147 co-routines toggle the segments of the right side display. */
\r
148 if( sSuspendTasks == pdTRUE )
\r
150 vTaskSuspend( xCoroutineTask );
\r
154 vTaskResume( xCoroutineTask );
\r
158 /*-----------------------------------------------------------*/
\r
160 static void vLEDFlashTask( void * pvParameters )
\r
162 portTickType xFlashRate, xLastFlashTime;
\r
163 unsigned short usLED;
\r
165 /* The LED to flash is passed in as the task parameter. */
\r
166 usLED = ( unsigned short ) pvParameters;
\r
168 /* Calculate the rate at which this task is going to toggle its LED. */
\r
169 xFlashRate = ledFLASH_RATE_BASE + ( ledFLASH_RATE_BASE * ( portTickType ) usLED );
\r
170 xFlashRate /= portTICK_RATE_MS;
\r
172 /* We will turn the LED on and off again in the delay period, so each
\r
173 delay is only half the total period. */
\r
174 xFlashRate /= ( portTickType ) 2;
\r
176 /* We need to initialise xLastFlashTime prior to the first call to
\r
177 vTaskDelayUntil(). */
\r
178 xLastFlashTime = xTaskGetTickCount();
\r
182 /* Delay for half the flash period then turn the LED on. */
\r
183 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
\r
184 vParTestToggleLED( usLED );
\r
186 /* Delay for half the flash period then turn the LED off. */
\r
187 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
\r
188 vParTestToggleLED( usLED );
\r
191 /*-----------------------------------------------------------*/
\r
193 static void vLEDCoRoutineControlTask( void *pvParameters )
\r
195 unsigned short usCoroutine;
\r
197 ( void ) pvParameters;
\r
199 /* Create the co-routines - one of each segment of the right side display. */
\r
200 for( usCoroutine = 0; usCoroutine < ledNUM_OF_LED_CO_ROUTINES; usCoroutine++ )
\r
202 xCoRoutineCreate( prvFixedDelayCoRoutine, ledCO_ROUTINE_PRIORITY, usCoroutine );
\r
205 /* This task has nothing else to do except scheduler the co-routines it just
\r
209 vCoRoutineSchedule();
\r
212 /*-----------------------------------------------------------*/
\r
214 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex )
\r
216 /* The usIndex parameter of the co-routine function is used as an index into
\r
217 the xFlashRates array to obtain the delay period to use. */
\r
218 static const portTickType xFlashRates[ ledNUM_OF_LED_CO_ROUTINES ] = { 150 / portTICK_RATE_MS,
\r
219 300 / portTICK_RATE_MS,
\r
220 450 / portTICK_RATE_MS,
\r
221 600 / portTICK_RATE_MS,
\r
222 750 / portTICK_RATE_MS,
\r
223 900 / portTICK_RATE_MS,
\r
224 1050 / portTICK_RATE_MS };
\r
226 /* Co-routines MUST start with a call to crSTART. */
\r
227 crSTART( xHandle );
\r
231 /* Toggle the LED. An offset of 8 is used to skip over the segments of
\r
232 the left side display which use the low numbers. */
\r
233 vParTestToggleLED( usIndex + 8 );
\r
235 /* Delay until it is time to toggle the segment that this co-routine is
\r
236 controlling again. */
\r
237 crDELAY( xHandle, xFlashRates[ usIndex ] );
\r
240 /* Co-routines MUST end with a call to crEND. */
\r
243 /*-----------------------------------------------------------*/
\r