2 FreeRTOS.org V5.2.0 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
10 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
15 You should have received a copy of the GNU General Public License along
\r
16 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
17 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
19 A special exception to the GPL is included to allow you to distribute a
\r
20 combined work that includes FreeRTOS.org without being obliged to provide
\r
21 the source code for any proprietary components. See the licensing section
\r
22 of http://www.FreeRTOS.org for full details.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
53 * Defines the tasks and co-routines used to toggle the segments of the two
\r
54 * seven segment displays, as described at the top of main.c
\r
60 /* Scheduler include files. */
\r
61 #include "FreeRTOS.h"
\r
63 #include "croutine.h"
\r
65 /* Demo program include files. */
\r
66 #include "partest.h"
\r
68 /*-----------------------------------------------------------*/
\r
70 /* One task per segment of the left side display. */
\r
71 #define ledNUM_OF_LED_TASKS ( 7 )
\r
73 /* Each task toggles at a frequency that is a multiple of 333ms. */
\r
74 #define ledFLASH_RATE_BASE ( ( portTickType ) 333 )
\r
76 /* One co-routine per segment of the right hand display. */
\r
77 #define ledNUM_OF_LED_CO_ROUTINES 7
\r
79 /* All co-routines run at the same priority. */
\r
80 #define ledCO_ROUTINE_PRIORITY 0
\r
82 /*-----------------------------------------------------------*/
\r
84 /* The task that is created 7 times. */
\r
85 static void vLEDFlashTask( void *pvParameters );
\r
87 /* The co-routine that is created 7 times. */
\r
88 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex );
\r
90 /* This task is created once, but itself creates 7 co-routines. */
\r
91 static void vLEDCoRoutineControlTask( void *pvParameters );
\r
93 /* Handles to each of the 7 tasks. Used so the tasks can be suspended
\r
95 static xTaskHandle xFlashTaskHandles[ ledNUM_OF_LED_TASKS ] = { 0 };
\r
97 /* Handle to the task in which the co-routines run. Used so the
\r
98 co-routines can be suspended and resumed. */
\r
99 static xTaskHandle xCoroutineTask;
\r
101 /*-----------------------------------------------------------*/
\r
104 * Creates the tasks and co-routines used to toggle the segments of the two
\r
105 * seven segment displays, as described at the top of main.c
\r
107 void vCreateFlashTasksAndCoRoutines( void )
\r
109 signed short sLEDTask;
\r
111 /* Create the tasks that flash segments on the first LED. */
\r
112 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
\r
114 /* Spawn the task. */
\r
115 xTaskCreate( vLEDFlashTask, ( signed char * ) "LEDt", configMINIMAL_STACK_SIZE, ( void * ) sLEDTask, ( tskIDLE_PRIORITY + 1 ), &( xFlashTaskHandles[ sLEDTask ] ) );
\r
118 /* Create the task in which the co-routines run. The co-routines themselves
\r
119 are created within the task. */
\r
120 xTaskCreate( vLEDCoRoutineControlTask, ( signed char * ) "LEDc", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCoroutineTask );
\r
122 /*-----------------------------------------------------------*/
\r
124 void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks )
\r
128 if( ucIndex == configLEFT_DISPLAY )
\r
130 /* Suspend or resume the tasks that are toggling the segments of the
\r
131 left side display. */
\r
132 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
\r
134 if( xFlashTaskHandles[ sLEDTask ] != NULL )
\r
136 if( sSuspendTasks == pdTRUE )
\r
138 vTaskSuspend( xFlashTaskHandles[ sLEDTask ] );
\r
142 vTaskResume( xFlashTaskHandles[ sLEDTask ] );
\r
149 /* Suspend or resume the task in which the co-routines are running. The
\r
150 co-routines toggle the segments of the right side display. */
\r
151 if( sSuspendTasks == pdTRUE )
\r
153 vTaskSuspend( xCoroutineTask );
\r
157 vTaskResume( xCoroutineTask );
\r
161 /*-----------------------------------------------------------*/
\r
163 static void vLEDFlashTask( void * pvParameters )
\r
165 portTickType xFlashRate, xLastFlashTime;
\r
166 unsigned short usLED;
\r
168 /* The LED to flash is passed in as the task parameter. */
\r
169 usLED = ( unsigned short ) pvParameters;
\r
171 /* Calculate the rate at which this task is going to toggle its LED. */
\r
172 xFlashRate = ledFLASH_RATE_BASE + ( ledFLASH_RATE_BASE * ( portTickType ) usLED );
\r
173 xFlashRate /= portTICK_RATE_MS;
\r
175 /* We will turn the LED on and off again in the delay period, so each
\r
176 delay is only half the total period. */
\r
177 xFlashRate /= ( portTickType ) 2;
\r
179 /* We need to initialise xLastFlashTime prior to the first call to
\r
180 vTaskDelayUntil(). */
\r
181 xLastFlashTime = xTaskGetTickCount();
\r
185 /* Delay for half the flash period then turn the LED on. */
\r
186 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
\r
187 vParTestToggleLED( usLED );
\r
189 /* Delay for half the flash period then turn the LED off. */
\r
190 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
\r
191 vParTestToggleLED( usLED );
\r
194 /*-----------------------------------------------------------*/
\r
196 static void vLEDCoRoutineControlTask( void *pvParameters )
\r
198 unsigned short usCoroutine;
\r
200 ( void ) pvParameters;
\r
202 /* Create the co-routines - one of each segment of the right side display. */
\r
203 for( usCoroutine = 0; usCoroutine < ledNUM_OF_LED_CO_ROUTINES; usCoroutine++ )
\r
205 xCoRoutineCreate( prvFixedDelayCoRoutine, ledCO_ROUTINE_PRIORITY, usCoroutine );
\r
208 /* This task has nothing else to do except scheduler the co-routines it just
\r
212 vCoRoutineSchedule();
\r
215 /*-----------------------------------------------------------*/
\r
217 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex )
\r
219 /* The usIndex parameter of the co-routine function is used as an index into
\r
220 the xFlashRates array to obtain the delay period to use. */
\r
221 static const portTickType xFlashRates[ ledNUM_OF_LED_CO_ROUTINES ] = { 150 / portTICK_RATE_MS,
\r
222 300 / portTICK_RATE_MS,
\r
223 450 / portTICK_RATE_MS,
\r
224 600 / portTICK_RATE_MS,
\r
225 750 / portTICK_RATE_MS,
\r
226 900 / portTICK_RATE_MS,
\r
227 1050 / portTICK_RATE_MS };
\r
229 /* Co-routines MUST start with a call to crSTART. */
\r
230 crSTART( xHandle );
\r
234 /* Toggle the LED. An offset of 8 is used to skip over the segments of
\r
235 the left side display which use the low numbers. */
\r
236 vParTestToggleLED( usIndex + 8 );
\r
238 /* Delay until it is time to toggle the segment that this co-routine is
\r
239 controlling again. */
\r
240 crDELAY( xHandle, xFlashRates[ usIndex ] );
\r
243 /* Co-routines MUST end with a call to crEND. */
\r
246 /*-----------------------------------------------------------*/
\r