2 FreeRTOS V7.0.2 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 * Defines the tasks and co-routines used to toggle the segments of the two
\r
56 * seven segment displays, as described at the top of main.c
\r
62 /* Scheduler include files. */
\r
63 #include "FreeRTOS.h"
\r
65 #include "croutine.h"
\r
67 /* Demo program include files. */
\r
68 #include "partest.h"
\r
70 /*-----------------------------------------------------------*/
\r
72 /* One task per segment of the left side display. */
\r
73 #define ledNUM_OF_LED_TASKS ( 7 )
\r
75 /* Each task toggles at a frequency that is a multiple of 333ms. */
\r
76 #define ledFLASH_RATE_BASE ( ( portTickType ) 333 )
\r
78 /* One co-routine per segment of the right hand display. */
\r
79 #define ledNUM_OF_LED_CO_ROUTINES 7
\r
81 /* All co-routines run at the same priority. */
\r
82 #define ledCO_ROUTINE_PRIORITY 0
\r
84 /*-----------------------------------------------------------*/
\r
86 /* The task that is created 7 times. */
\r
87 static void vLEDFlashTask( void *pvParameters );
\r
89 /* The co-routine that is created 7 times. */
\r
90 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex );
\r
92 /* This task is created once, but itself creates 7 co-routines. */
\r
93 static void vLEDCoRoutineControlTask( void *pvParameters );
\r
95 /* Handles to each of the 7 tasks. Used so the tasks can be suspended
\r
97 static xTaskHandle xFlashTaskHandles[ ledNUM_OF_LED_TASKS ] = { 0 };
\r
99 /* Handle to the task in which the co-routines run. Used so the
\r
100 co-routines can be suspended and resumed. */
\r
101 static xTaskHandle xCoroutineTask;
\r
103 /*-----------------------------------------------------------*/
\r
106 * Creates the tasks and co-routines used to toggle the segments of the two
\r
107 * seven segment displays, as described at the top of main.c
\r
109 void vCreateFlashTasksAndCoRoutines( void )
\r
111 signed short sLEDTask;
\r
113 /* Create the tasks that flash segments on the first LED. */
\r
114 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
\r
116 /* Spawn the task. */
\r
117 xTaskCreate( vLEDFlashTask, ( signed char * ) "LEDt", configMINIMAL_STACK_SIZE, ( void * ) sLEDTask, ( tskIDLE_PRIORITY + 1 ), &( xFlashTaskHandles[ sLEDTask ] ) );
\r
120 /* Create the task in which the co-routines run. The co-routines themselves
\r
121 are created within the task. */
\r
122 xTaskCreate( vLEDCoRoutineControlTask, ( signed char * ) "LEDc", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCoroutineTask );
\r
124 /*-----------------------------------------------------------*/
\r
126 void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks )
\r
130 if( ucIndex == configLEFT_DISPLAY )
\r
132 /* Suspend or resume the tasks that are toggling the segments of the
\r
133 left side display. */
\r
134 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
\r
136 if( xFlashTaskHandles[ sLEDTask ] != NULL )
\r
138 if( sSuspendTasks == pdTRUE )
\r
140 vTaskSuspend( xFlashTaskHandles[ sLEDTask ] );
\r
144 vTaskResume( xFlashTaskHandles[ sLEDTask ] );
\r
151 /* Suspend or resume the task in which the co-routines are running. The
\r
152 co-routines toggle the segments of the right side display. */
\r
153 if( sSuspendTasks == pdTRUE )
\r
155 vTaskSuspend( xCoroutineTask );
\r
159 vTaskResume( xCoroutineTask );
\r
163 /*-----------------------------------------------------------*/
\r
165 static void vLEDFlashTask( void * pvParameters )
\r
167 portTickType xFlashRate, xLastFlashTime;
\r
168 unsigned short usLED;
\r
170 /* The LED to flash is passed in as the task parameter. */
\r
171 usLED = ( unsigned short ) pvParameters;
\r
173 /* Calculate the rate at which this task is going to toggle its LED. */
\r
174 xFlashRate = ledFLASH_RATE_BASE + ( ledFLASH_RATE_BASE * ( portTickType ) usLED );
\r
175 xFlashRate /= portTICK_RATE_MS;
\r
177 /* We will turn the LED on and off again in the delay period, so each
\r
178 delay is only half the total period. */
\r
179 xFlashRate /= ( portTickType ) 2;
\r
181 /* We need to initialise xLastFlashTime prior to the first call to
\r
182 vTaskDelayUntil(). */
\r
183 xLastFlashTime = xTaskGetTickCount();
\r
187 /* Delay for half the flash period then turn the LED on. */
\r
188 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
\r
189 vParTestToggleLED( usLED );
\r
191 /* Delay for half the flash period then turn the LED off. */
\r
192 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
\r
193 vParTestToggleLED( usLED );
\r
196 /*-----------------------------------------------------------*/
\r
198 static void vLEDCoRoutineControlTask( void *pvParameters )
\r
200 unsigned short usCoroutine;
\r
202 ( void ) pvParameters;
\r
204 /* Create the co-routines - one of each segment of the right side display. */
\r
205 for( usCoroutine = 0; usCoroutine < ledNUM_OF_LED_CO_ROUTINES; usCoroutine++ )
\r
207 xCoRoutineCreate( prvFixedDelayCoRoutine, ledCO_ROUTINE_PRIORITY, usCoroutine );
\r
210 /* This task has nothing else to do except scheduler the co-routines it just
\r
214 vCoRoutineSchedule();
\r
217 /*-----------------------------------------------------------*/
\r
219 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex )
\r
221 /* The usIndex parameter of the co-routine function is used as an index into
\r
222 the xFlashRates array to obtain the delay period to use. */
\r
223 static const portTickType xFlashRates[ ledNUM_OF_LED_CO_ROUTINES ] = { 150 / portTICK_RATE_MS,
\r
224 300 / portTICK_RATE_MS,
\r
225 450 / portTICK_RATE_MS,
\r
226 600 / portTICK_RATE_MS,
\r
227 750 / portTICK_RATE_MS,
\r
228 900 / portTICK_RATE_MS,
\r
229 1050 / portTICK_RATE_MS };
\r
231 /* Co-routines MUST start with a call to crSTART. */
\r
232 crSTART( xHandle );
\r
236 /* Toggle the LED. An offset of 8 is used to skip over the segments of
\r
237 the left side display which use the low numbers. */
\r
238 vParTestToggleLED( usIndex + 8 );
\r
240 /* Delay until it is time to toggle the segment that this co-routine is
\r
241 controlling again. */
\r
242 crDELAY( xHandle, xFlashRates[ usIndex ] );
\r
245 /* Co-routines MUST end with a call to crEND. */
\r
248 /*-----------------------------------------------------------*/
\r