2 FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 * Defines the tasks and co-routines used to toggle the segments of the two
\r
67 * seven segment displays, as described at the top of main.c
\r
73 /* Scheduler include files. */
\r
74 #include "FreeRTOS.h"
\r
76 #include "croutine.h"
\r
78 /* Demo program include files. */
\r
79 #include "partest.h"
\r
81 /*-----------------------------------------------------------*/
\r
83 /* One task per segment of the left side display. */
\r
84 #define ledNUM_OF_LED_TASKS ( 7 )
\r
86 /* Each task toggles at a frequency that is a multiple of 333ms. */
\r
87 #define ledFLASH_RATE_BASE ( ( portTickType ) 333 )
\r
89 /* One co-routine per segment of the right hand display. */
\r
90 #define ledNUM_OF_LED_CO_ROUTINES 7
\r
92 /* All co-routines run at the same priority. */
\r
93 #define ledCO_ROUTINE_PRIORITY 0
\r
95 /*-----------------------------------------------------------*/
\r
97 /* The task that is created 7 times. */
\r
98 static void vLEDFlashTask( void *pvParameters );
\r
100 /* The co-routine that is created 7 times. */
\r
101 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex );
\r
103 /* This task is created once, but itself creates 7 co-routines. */
\r
104 static void vLEDCoRoutineControlTask( void *pvParameters );
\r
106 /* Handles to each of the 7 tasks. Used so the tasks can be suspended
\r
108 static xTaskHandle xFlashTaskHandles[ ledNUM_OF_LED_TASKS ] = { 0 };
\r
110 /* Handle to the task in which the co-routines run. Used so the
\r
111 co-routines can be suspended and resumed. */
\r
112 static xTaskHandle xCoroutineTask;
\r
114 /*-----------------------------------------------------------*/
\r
117 * Creates the tasks and co-routines used to toggle the segments of the two
\r
118 * seven segment displays, as described at the top of main.c
\r
120 void vCreateFlashTasksAndCoRoutines( void )
\r
122 signed short sLEDTask;
\r
124 /* Create the tasks that flash segments on the first LED. */
\r
125 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
\r
127 /* Spawn the task. */
\r
128 xTaskCreate( vLEDFlashTask, ( signed char * ) "LEDt", configMINIMAL_STACK_SIZE, ( void * ) sLEDTask, ( tskIDLE_PRIORITY + 1 ), &( xFlashTaskHandles[ sLEDTask ] ) );
\r
131 /* Create the task in which the co-routines run. The co-routines themselves
\r
132 are created within the task. */
\r
133 xTaskCreate( vLEDCoRoutineControlTask, ( signed char * ) "LEDc", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCoroutineTask );
\r
135 /*-----------------------------------------------------------*/
\r
137 void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks )
\r
141 if( ucIndex == configLEFT_DISPLAY )
\r
143 /* Suspend or resume the tasks that are toggling the segments of the
\r
144 left side display. */
\r
145 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
\r
147 if( xFlashTaskHandles[ sLEDTask ] != NULL )
\r
149 if( sSuspendTasks == pdTRUE )
\r
151 vTaskSuspend( xFlashTaskHandles[ sLEDTask ] );
\r
155 vTaskResume( xFlashTaskHandles[ sLEDTask ] );
\r
162 /* Suspend or resume the task in which the co-routines are running. The
\r
163 co-routines toggle the segments of the right side display. */
\r
164 if( sSuspendTasks == pdTRUE )
\r
166 vTaskSuspend( xCoroutineTask );
\r
170 vTaskResume( xCoroutineTask );
\r
174 /*-----------------------------------------------------------*/
\r
176 static void vLEDFlashTask( void * pvParameters )
\r
178 portTickType xFlashRate, xLastFlashTime;
\r
179 unsigned short usLED;
\r
181 /* The LED to flash is passed in as the task parameter. */
\r
182 usLED = ( unsigned short ) pvParameters;
\r
184 /* Calculate the rate at which this task is going to toggle its LED. */
\r
185 xFlashRate = ledFLASH_RATE_BASE + ( ledFLASH_RATE_BASE * ( portTickType ) usLED );
\r
186 xFlashRate /= portTICK_RATE_MS;
\r
188 /* We will turn the LED on and off again in the delay period, so each
\r
189 delay is only half the total period. */
\r
190 xFlashRate /= ( portTickType ) 2;
\r
192 /* We need to initialise xLastFlashTime prior to the first call to
\r
193 vTaskDelayUntil(). */
\r
194 xLastFlashTime = xTaskGetTickCount();
\r
198 /* Delay for half the flash period then turn the LED on. */
\r
199 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
\r
200 vParTestToggleLED( usLED );
\r
202 /* Delay for half the flash period then turn the LED off. */
\r
203 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
\r
204 vParTestToggleLED( usLED );
\r
207 /*-----------------------------------------------------------*/
\r
209 static void vLEDCoRoutineControlTask( void *pvParameters )
\r
211 unsigned short usCoroutine;
\r
213 ( void ) pvParameters;
\r
215 /* Create the co-routines - one of each segment of the right side display. */
\r
216 for( usCoroutine = 0; usCoroutine < ledNUM_OF_LED_CO_ROUTINES; usCoroutine++ )
\r
218 xCoRoutineCreate( prvFixedDelayCoRoutine, ledCO_ROUTINE_PRIORITY, usCoroutine );
\r
221 /* This task has nothing else to do except scheduler the co-routines it just
\r
225 vCoRoutineSchedule();
\r
228 /*-----------------------------------------------------------*/
\r
230 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned short usIndex )
\r
232 /* The usIndex parameter of the co-routine function is used as an index into
\r
233 the xFlashRates array to obtain the delay period to use. */
\r
234 static const portTickType xFlashRates[ ledNUM_OF_LED_CO_ROUTINES ] = { 150 / portTICK_RATE_MS,
\r
235 300 / portTICK_RATE_MS,
\r
236 450 / portTICK_RATE_MS,
\r
237 600 / portTICK_RATE_MS,
\r
238 750 / portTICK_RATE_MS,
\r
239 900 / portTICK_RATE_MS,
\r
240 1050 / portTICK_RATE_MS };
\r
242 /* Co-routines MUST start with a call to crSTART. */
\r
243 crSTART( xHandle );
\r
247 /* Toggle the LED. An offset of 8 is used to skip over the segments of
\r
248 the left side display which use the low numbers. */
\r
249 vParTestToggleLED( usIndex + 8 );
\r
251 /* Delay until it is time to toggle the segment that this co-routine is
\r
252 controlling again. */
\r
253 crDELAY( xHandle, xFlashRates[ usIndex ] );
\r
256 /* Co-routines MUST end with a call to crEND. */
\r
259 /*-----------------------------------------------------------*/
\r