2 FreeRTOS.org V5.1.1 - Copyright (C) 2003-2008 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
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
\r
30 * and even write all or part of your application on your behalf. *
\r
31 * See http://www.OpenRTOS.com for details of the services we provide to *
\r
32 * expedite your project. *
\r
34 ***************************************************************************
\r
35 ***************************************************************************
\r
37 Please ensure to read the configuration and relevant port sections of the
\r
38 online documentation.
\r
40 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
43 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
46 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
47 licensing and training services.
\r
51 * This version of flash .c is for use on systems that have limited stack space
\r
52 * and no display facilities. The complete version can be found in the
\r
53 * Demo/Common/Full directory.
\r
55 * Three tasks are created, each of which flash an LED at a different rate. The first
\r
56 * LED flashes every 200ms, the second every 400ms, the third every 600ms.
\r
58 * The LED flash tasks provide instant visual feedback. They show that the scheduler
\r
59 * is still operational.
\r
66 /* Scheduler include files. */
\r
67 #include "FreeRTOS.h"
\r
69 #include "croutine.h"
\r
71 /* Demo program include files. */
\r
72 #include "partest.h"
\r
75 #define ledSTACK_SIZE configMINIMAL_STACK_SIZE
\r
76 #define ledNUMBER_OF_LEDS ( 7 )
\r
77 #define ledFLASH_RATE_BASE ( ( portTickType ) 333 )
\r
79 #define ledMAX_FLASH_CO_ROUTINES 7
\r
80 #define ledCO_ROUTINE_PRIORITY 0
\r
82 /* The task that is created three times. */
\r
83 static void vLEDFlashTask( void *pvParameters );
\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 static xTaskHandle xFlashTaskHandles[ ledNUMBER_OF_LEDS ] = { 0 };
\r
90 static xTaskHandle xCoroutineTask;
\r
92 /*-----------------------------------------------------------*/
\r
94 void vStartLEDFlashTasks( unsigned portBASE_TYPE uxPriority )
\r
96 signed short sLEDTask;
\r
98 /* Create the three tasks that flash segments on the first LED. */
\r
99 for( sLEDTask = 0; sLEDTask < ledNUMBER_OF_LEDS; ++sLEDTask )
\r
101 /* Spawn the task. */
\r
102 xTaskCreate( vLEDFlashTask, ( signed char * ) "LEDt", ledSTACK_SIZE, ( void * ) sLEDTask, uxPriority, &( xFlashTaskHandles[ sLEDTask ] ) );
\r
105 /* Create the task in which the co-routines run. */
\r
106 xTaskCreate( vLEDCoRoutineControlTask, ( signed char * ) "LEDc", ledSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xCoroutineTask );
\r
108 /*-----------------------------------------------------------*/
\r
110 void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks )
\r
116 for( sLEDTask = 0; sLEDTask < ledNUMBER_OF_LEDS; ++sLEDTask )
\r
118 if( xFlashTaskHandles[ sLEDTask ] != NULL )
\r
120 if( sSuspendTasks == pdTRUE )
\r
122 vTaskSuspend( xFlashTaskHandles[ sLEDTask ] );
\r
126 vTaskResume( xFlashTaskHandles[ sLEDTask ] );
\r
133 if( sSuspendTasks == pdTRUE )
\r
135 vTaskSuspend( xCoroutineTask );
\r
139 vTaskResume( xCoroutineTask );
\r
143 /*-----------------------------------------------------------*/
\r
145 static void vLEDFlashTask( void * pvParameters )
\r
147 portTickType xFlashRate, xLastFlashTime;
\r
148 unsigned short usLED;
\r
150 /* The LED to flash is passed in as the task parameter. */
\r
151 usLED = ( unsigned short ) pvParameters;
\r
153 /* Calculate the rate at which this task is going to toggle its LED. */
\r
154 xFlashRate = ledFLASH_RATE_BASE + ( ledFLASH_RATE_BASE * ( portTickType ) usLED );
\r
155 xFlashRate /= portTICK_RATE_MS;
\r
157 /* We will turn the LED on and off again in the delay period, so each
\r
158 delay is only half the total period. */
\r
159 xFlashRate /= ( portTickType ) 2;
\r
161 /* We need to initialise xLastFlashTime prior to the first call to
\r
162 vTaskDelayUntil(). */
\r
163 xLastFlashTime = xTaskGetTickCount();
\r
167 /* Delay for half the flash period then turn the LED on. */
\r
168 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
\r
169 vParTestToggleLED( usLED );
\r
171 /* Delay for half the flash period then turn the LED off. */
\r
172 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
\r
173 vParTestToggleLED( usLED );
\r
176 /*-----------------------------------------------------------*/
\r
178 static void vLEDCoRoutineControlTask( void *pvParameters )
\r
180 unsigned short usCoroutine;
\r
182 ( void ) pvParameters;
\r
184 for( usCoroutine = 0; usCoroutine < ledMAX_FLASH_CO_ROUTINES; usCoroutine++ )
\r
186 xCoRoutineCreate( prvFixedDelayCoRoutine, ledCO_ROUTINE_PRIORITY, usCoroutine );
\r
191 vCoRoutineSchedule();
\r
194 /*-----------------------------------------------------------*/
\r
196 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex )
\r
198 /* The usIndex parameter of the co-routine function is used as an index into
\r
199 the xFlashRates array to obtain the delay period to use. */
\r
200 static const portTickType xFlashRates[ ledMAX_FLASH_CO_ROUTINES ] = { 150 / portTICK_RATE_MS,
\r
201 300 / portTICK_RATE_MS,
\r
202 450 / portTICK_RATE_MS,
\r
203 600 / portTICK_RATE_MS,
\r
204 750 / portTICK_RATE_MS,
\r
205 900 / portTICK_RATE_MS,
\r
206 1050 / portTICK_RATE_MS };
\r
208 /* Co-routines MUST start with a call to crSTART. */
\r
209 crSTART( xHandle );
\r
213 vParTestToggleLED( usIndex + 8 );
\r
214 crDELAY( xHandle, xFlashRates[ usIndex ] );
\r
217 /* Co-routines MUST end with a call to crEND. */
\r
220 /*-----------------------------------------------------------*/
\r