2 FreeRTOS V7.2.0 - Copyright (C) 2012 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
68 * Defines the tasks and co-routines used to toggle the segments of the two
\r
69 * seven segment displays, as described at the top of main.c
\r
75 /* Scheduler include files. */
\r
76 #include "FreeRTOS.h"
\r
78 #include "croutine.h"
\r
80 /* Demo program include files. */
\r
81 #include "partest.h"
\r
83 /*-----------------------------------------------------------*/
\r
85 /* One task per segment of the left side display. */
\r
86 #define ledNUM_OF_LED_TASKS ( 7 )
\r
88 /* Each task toggles at a frequency that is a multiple of 333ms. */
\r
89 #define ledFLASH_RATE_BASE ( ( portTickType ) 333 )
\r
91 /* One co-routine per segment of the right hand display. */
\r
92 #define ledNUM_OF_LED_CO_ROUTINES 7
\r
94 /* All co-routines run at the same priority. */
\r
95 #define ledCO_ROUTINE_PRIORITY 0
\r
97 /*-----------------------------------------------------------*/
\r
99 /* The task that is created 7 times. */
\r
100 static void vLEDFlashTask( void *pvParameters );
\r
102 /* The co-routine that is created 7 times. */
\r
103 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex );
\r
105 /* This task is created once, but itself creates 7 co-routines. */
\r
106 static void vLEDCoRoutineControlTask( void *pvParameters );
\r
108 /* Handles to each of the 7 tasks. Used so the tasks can be suspended
\r
110 static xTaskHandle xFlashTaskHandles[ ledNUM_OF_LED_TASKS ] = { 0 };
\r
112 /* Handle to the task in which the co-routines run. Used so the
\r
113 co-routines can be suspended and resumed. */
\r
114 static xTaskHandle xCoroutineTask;
\r
116 /*-----------------------------------------------------------*/
\r
119 * Creates the tasks and co-routines used to toggle the segments of the two
\r
120 * seven segment displays, as described at the top of main.c
\r
122 void vCreateFlashTasksAndCoRoutines( void )
\r
124 signed short sLEDTask;
\r
126 /* Create the tasks that flash segments on the first LED. */
\r
127 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
\r
129 /* Spawn the task. */
\r
130 xTaskCreate( vLEDFlashTask, ( signed char * ) "LEDt", configMINIMAL_STACK_SIZE, ( void * ) sLEDTask, ( tskIDLE_PRIORITY + 1 ), &( xFlashTaskHandles[ sLEDTask ] ) );
\r
133 /* Create the task in which the co-routines run. The co-routines themselves
\r
134 are created within the task. */
\r
135 xTaskCreate( vLEDCoRoutineControlTask, ( signed char * ) "LEDc", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCoroutineTask );
\r
137 /*-----------------------------------------------------------*/
\r
139 void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks )
\r
143 if( ucIndex == configLEFT_DISPLAY )
\r
145 /* Suspend or resume the tasks that are toggling the segments of the
\r
146 left side display. */
\r
147 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
\r
149 if( xFlashTaskHandles[ sLEDTask ] != NULL )
\r
151 if( sSuspendTasks == pdTRUE )
\r
153 vTaskSuspend( xFlashTaskHandles[ sLEDTask ] );
\r
157 vTaskResume( xFlashTaskHandles[ sLEDTask ] );
\r
164 /* Suspend or resume the task in which the co-routines are running. The
\r
165 co-routines toggle the segments of the right side display. */
\r
166 if( sSuspendTasks == pdTRUE )
\r
168 vTaskSuspend( xCoroutineTask );
\r
172 vTaskResume( xCoroutineTask );
\r
176 /*-----------------------------------------------------------*/
\r
178 static void vLEDFlashTask( void * pvParameters )
\r
180 portTickType xFlashRate, xLastFlashTime;
\r
181 unsigned short usLED;
\r
183 /* The LED to flash is passed in as the task parameter. */
\r
184 usLED = ( unsigned short ) pvParameters;
\r
186 /* Calculate the rate at which this task is going to toggle its LED. */
\r
187 xFlashRate = ledFLASH_RATE_BASE + ( ledFLASH_RATE_BASE * ( portTickType ) usLED );
\r
188 xFlashRate /= portTICK_RATE_MS;
\r
190 /* We will turn the LED on and off again in the delay period, so each
\r
191 delay is only half the total period. */
\r
192 xFlashRate /= ( portTickType ) 2;
\r
194 /* We need to initialise xLastFlashTime prior to the first call to
\r
195 vTaskDelayUntil(). */
\r
196 xLastFlashTime = xTaskGetTickCount();
\r
200 /* Delay for half the flash period then turn the LED on. */
\r
201 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
\r
202 vParTestToggleLED( usLED );
\r
204 /* Delay for half the flash period then turn the LED off. */
\r
205 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
\r
206 vParTestToggleLED( usLED );
\r
209 /*-----------------------------------------------------------*/
\r
211 static void vLEDCoRoutineControlTask( void *pvParameters )
\r
213 unsigned short usCoroutine;
\r
215 ( void ) pvParameters;
\r
217 /* Create the co-routines - one of each segment of the right side display. */
\r
218 for( usCoroutine = 0; usCoroutine < ledNUM_OF_LED_CO_ROUTINES; usCoroutine++ )
\r
220 xCoRoutineCreate( prvFixedDelayCoRoutine, ledCO_ROUTINE_PRIORITY, usCoroutine );
\r
223 /* This task has nothing else to do except scheduler the co-routines it just
\r
227 vCoRoutineSchedule();
\r
230 /*-----------------------------------------------------------*/
\r
232 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex )
\r
234 /* The usIndex parameter of the co-routine function is used as an index into
\r
235 the xFlashRates array to obtain the delay period to use. */
\r
236 static const portTickType xFlashRates[ ledNUM_OF_LED_CO_ROUTINES ] = { 150 / portTICK_RATE_MS,
\r
237 300 / portTICK_RATE_MS,
\r
238 450 / portTICK_RATE_MS,
\r
239 600 / portTICK_RATE_MS,
\r
240 750 / portTICK_RATE_MS,
\r
241 900 / portTICK_RATE_MS,
\r
242 1050 / portTICK_RATE_MS };
\r
244 /* Co-routines MUST start with a call to crSTART. */
\r
245 crSTART( xHandle );
\r
249 /* Toggle the LED. An offset of 8 is used to skip over the segments of
\r
250 the left side display which use the low numbers. */
\r
251 vParTestToggleLED( usIndex + 8 );
\r
253 /* Delay until it is time to toggle the segment that this co-routine is
\r
254 controlling again. */
\r
255 crDELAY( xHandle, xFlashRates[ usIndex ] );
\r
258 /* Co-routines MUST end with a call to crEND. */
\r
261 /*-----------------------------------------------------------*/
\r