2 FreeRTOS V7.4.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
76 * Defines the tasks and co-routines used to toggle the segments of the two
\r
77 * seven segment displays, as described at the top of main.c
\r
83 /* Scheduler include files. */
\r
84 #include "FreeRTOS.h"
\r
86 #include "croutine.h"
\r
88 /* Demo program include files. */
\r
89 #include "partest.h"
\r
91 /*-----------------------------------------------------------*/
\r
93 /* One task per segment of the left side display. */
\r
94 #define ledNUM_OF_LED_TASKS ( 7 )
\r
96 /* Each task toggles at a frequency that is a multiple of 333ms. */
\r
97 #define ledFLASH_RATE_BASE ( ( portTickType ) 333 )
\r
99 /* One co-routine per segment of the right hand display. */
\r
100 #define ledNUM_OF_LED_CO_ROUTINES 7
\r
102 /* All co-routines run at the same priority. */
\r
103 #define ledCO_ROUTINE_PRIORITY 0
\r
105 /*-----------------------------------------------------------*/
\r
107 /* The task that is created 7 times. */
\r
108 static void vLEDFlashTask( void *pvParameters );
\r
110 /* The co-routine that is created 7 times. */
\r
111 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex );
\r
113 /* This task is created once, but itself creates 7 co-routines. */
\r
114 static void vLEDCoRoutineControlTask( void *pvParameters );
\r
116 /* Handles to each of the 7 tasks. Used so the tasks can be suspended
\r
118 static xTaskHandle xFlashTaskHandles[ ledNUM_OF_LED_TASKS ] = { 0 };
\r
120 /* Handle to the task in which the co-routines run. Used so the
\r
121 co-routines can be suspended and resumed. */
\r
122 static xTaskHandle xCoroutineTask;
\r
124 /*-----------------------------------------------------------*/
\r
127 * Creates the tasks and co-routines used to toggle the segments of the two
\r
128 * seven segment displays, as described at the top of main.c
\r
130 void vCreateFlashTasksAndCoRoutines( void )
\r
132 signed short sLEDTask;
\r
134 /* Create the tasks that flash segments on the first LED. */
\r
135 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
\r
137 /* Spawn the task. */
\r
138 xTaskCreate( vLEDFlashTask, ( signed char * ) "LEDt", configMINIMAL_STACK_SIZE, ( void * ) sLEDTask, ( tskIDLE_PRIORITY + 1 ), &( xFlashTaskHandles[ sLEDTask ] ) );
\r
141 /* Create the task in which the co-routines run. The co-routines themselves
\r
142 are created within the task. */
\r
143 xTaskCreate( vLEDCoRoutineControlTask, ( signed char * ) "LEDc", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCoroutineTask );
\r
145 /*-----------------------------------------------------------*/
\r
147 void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks )
\r
151 if( ucIndex == configLEFT_DISPLAY )
\r
153 /* Suspend or resume the tasks that are toggling the segments of the
\r
154 left side display. */
\r
155 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
\r
157 if( xFlashTaskHandles[ sLEDTask ] != NULL )
\r
159 if( sSuspendTasks == pdTRUE )
\r
161 vTaskSuspend( xFlashTaskHandles[ sLEDTask ] );
\r
165 vTaskResume( xFlashTaskHandles[ sLEDTask ] );
\r
172 /* Suspend or resume the task in which the co-routines are running. The
\r
173 co-routines toggle the segments of the right side display. */
\r
174 if( sSuspendTasks == pdTRUE )
\r
176 vTaskSuspend( xCoroutineTask );
\r
180 vTaskResume( xCoroutineTask );
\r
184 /*-----------------------------------------------------------*/
\r
186 static void vLEDFlashTask( void * pvParameters )
\r
188 portTickType xFlashRate, xLastFlashTime;
\r
189 unsigned short usLED;
\r
191 /* The LED to flash is passed in as the task parameter. */
\r
192 usLED = ( unsigned short ) pvParameters;
\r
194 /* Calculate the rate at which this task is going to toggle its LED. */
\r
195 xFlashRate = ledFLASH_RATE_BASE + ( ledFLASH_RATE_BASE * ( portTickType ) usLED );
\r
196 xFlashRate /= portTICK_RATE_MS;
\r
198 /* We will turn the LED on and off again in the delay period, so each
\r
199 delay is only half the total period. */
\r
200 xFlashRate /= ( portTickType ) 2;
\r
202 /* We need to initialise xLastFlashTime prior to the first call to
\r
203 vTaskDelayUntil(). */
\r
204 xLastFlashTime = xTaskGetTickCount();
\r
208 /* Delay for half the flash period then turn the LED on. */
\r
209 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
\r
210 vParTestToggleLED( usLED );
\r
212 /* Delay for half the flash period then turn the LED off. */
\r
213 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
\r
214 vParTestToggleLED( usLED );
\r
217 /*-----------------------------------------------------------*/
\r
219 static void vLEDCoRoutineControlTask( void *pvParameters )
\r
221 unsigned short usCoroutine;
\r
223 ( void ) pvParameters;
\r
225 /* Create the co-routines - one of each segment of the right side display. */
\r
226 for( usCoroutine = 0; usCoroutine < ledNUM_OF_LED_CO_ROUTINES; usCoroutine++ )
\r
228 xCoRoutineCreate( prvFixedDelayCoRoutine, ledCO_ROUTINE_PRIORITY, usCoroutine );
\r
231 /* This task has nothing else to do except scheduler the co-routines it just
\r
235 vCoRoutineSchedule();
\r
238 /*-----------------------------------------------------------*/
\r
240 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex )
\r
242 /* The usIndex parameter of the co-routine function is used as an index into
\r
243 the xFlashRates array to obtain the delay period to use. */
\r
244 static const portTickType xFlashRates[ ledNUM_OF_LED_CO_ROUTINES ] = { 150 / portTICK_RATE_MS,
\r
245 300 / portTICK_RATE_MS,
\r
246 450 / portTICK_RATE_MS,
\r
247 600 / portTICK_RATE_MS,
\r
248 750 / portTICK_RATE_MS,
\r
249 900 / portTICK_RATE_MS,
\r
250 1050 / portTICK_RATE_MS };
\r
252 /* Co-routines MUST start with a call to crSTART. */
\r
253 crSTART( xHandle );
\r
257 /* Toggle the LED. An offset of 8 is used to skip over the segments of
\r
258 the left side display which use the low numbers. */
\r
259 vParTestToggleLED( usIndex + 8 );
\r
261 /* Delay until it is time to toggle the segment that this co-routine is
\r
262 controlling again. */
\r
263 crDELAY( xHandle, xFlashRates[ usIndex ] );
\r
266 /* Co-routines MUST end with a call to crEND. */
\r
269 /*-----------------------------------------------------------*/
\r