2 FreeRTOS V9.0.0rc1 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
71 * Defines the tasks and co-routines used to toggle the segments of the two
\r
72 * seven segment displays, as described at the top of main.c
\r
78 /* Scheduler include files. */
\r
79 #include "FreeRTOS.h"
\r
81 #include "croutine.h"
\r
83 /* Demo program include files. */
\r
84 #include "partest.h"
\r
86 /*-----------------------------------------------------------*/
\r
88 /* One task per segment of the left side display. */
\r
89 #define ledNUM_OF_LED_TASKS ( 7 )
\r
91 /* Each task toggles at a frequency that is a multiple of 333ms. */
\r
92 #define ledFLASH_RATE_BASE ( ( TickType_t ) 333 )
\r
94 /* One co-routine per segment of the right hand display. */
\r
95 #define ledNUM_OF_LED_CO_ROUTINES 7
\r
97 /* All co-routines run at the same priority. */
\r
98 #define ledCO_ROUTINE_PRIORITY 0
\r
100 /*-----------------------------------------------------------*/
\r
102 /* The task that is created 7 times. */
\r
103 static void vLEDFlashTask( void *pvParameters );
\r
105 /* The co-routine that is created 7 times. */
\r
106 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, unsigned short usIndex );
\r
108 /* This task is created once, but itself creates 7 co-routines. */
\r
109 static void vLEDCoRoutineControlTask( void *pvParameters );
\r
111 /* Handles to each of the 7 tasks. Used so the tasks can be suspended
\r
113 static TaskHandle_t xFlashTaskHandles[ ledNUM_OF_LED_TASKS ] = { 0 };
\r
115 /* Handle to the task in which the co-routines run. Used so the
\r
116 co-routines can be suspended and resumed. */
\r
117 static TaskHandle_t xCoroutineTask;
\r
119 /*-----------------------------------------------------------*/
\r
122 * Creates the tasks and co-routines used to toggle the segments of the two
\r
123 * seven segment displays, as described at the top of main.c
\r
125 void vCreateFlashTasksAndCoRoutines( void )
\r
127 signed short sLEDTask;
\r
129 /* Create the tasks that flash segments on the first LED. */
\r
130 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
\r
132 /* Spawn the task. */
\r
133 xTaskCreate( vLEDFlashTask, "LEDt", configMINIMAL_STACK_SIZE, ( void * ) sLEDTask, ( tskIDLE_PRIORITY + 1 ), &( xFlashTaskHandles[ sLEDTask ] ) );
\r
136 /* Create the task in which the co-routines run. The co-routines themselves
\r
137 are created within the task. */
\r
138 xTaskCreate( vLEDCoRoutineControlTask, "LEDc", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCoroutineTask );
\r
140 /*-----------------------------------------------------------*/
\r
142 void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks )
\r
146 if( ucIndex == configLEFT_DISPLAY )
\r
148 /* Suspend or resume the tasks that are toggling the segments of the
\r
149 left side display. */
\r
150 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
\r
152 if( xFlashTaskHandles[ sLEDTask ] != NULL )
\r
154 if( sSuspendTasks == pdTRUE )
\r
156 vTaskSuspend( xFlashTaskHandles[ sLEDTask ] );
\r
160 vTaskResume( xFlashTaskHandles[ sLEDTask ] );
\r
167 /* Suspend or resume the task in which the co-routines are running. The
\r
168 co-routines toggle the segments of the right side display. */
\r
169 if( sSuspendTasks == pdTRUE )
\r
171 vTaskSuspend( xCoroutineTask );
\r
175 vTaskResume( xCoroutineTask );
\r
179 /*-----------------------------------------------------------*/
\r
181 static void vLEDFlashTask( void * pvParameters )
\r
183 TickType_t xFlashRate, xLastFlashTime;
\r
184 unsigned short usLED;
\r
186 /* The LED to flash is passed in as the task parameter. */
\r
187 usLED = ( unsigned short ) pvParameters;
\r
189 /* Calculate the rate at which this task is going to toggle its LED. */
\r
190 xFlashRate = ledFLASH_RATE_BASE + ( ledFLASH_RATE_BASE * ( TickType_t ) usLED );
\r
191 xFlashRate /= portTICK_PERIOD_MS;
\r
193 /* We will turn the LED on and off again in the delay period, so each
\r
194 delay is only half the total period. */
\r
195 xFlashRate /= ( TickType_t ) 2;
\r
197 /* We need to initialise xLastFlashTime prior to the first call to
\r
198 vTaskDelayUntil(). */
\r
199 xLastFlashTime = xTaskGetTickCount();
\r
203 /* Delay for half the flash period then turn the LED on. */
\r
204 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
\r
205 vParTestToggleLED( usLED );
\r
207 /* Delay for half the flash period then turn the LED off. */
\r
208 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
\r
209 vParTestToggleLED( usLED );
\r
212 /*-----------------------------------------------------------*/
\r
214 static void vLEDCoRoutineControlTask( void *pvParameters )
\r
216 unsigned short usCoroutine;
\r
218 ( void ) pvParameters;
\r
220 /* Create the co-routines - one of each segment of the right side display. */
\r
221 for( usCoroutine = 0; usCoroutine < ledNUM_OF_LED_CO_ROUTINES; usCoroutine++ )
\r
223 xCoRoutineCreate( prvFixedDelayCoRoutine, ledCO_ROUTINE_PRIORITY, usCoroutine );
\r
226 /* This task has nothing else to do except scheduler the co-routines it just
\r
230 vCoRoutineSchedule();
\r
233 /*-----------------------------------------------------------*/
\r
235 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, unsigned short usIndex )
\r
237 /* The usIndex parameter of the co-routine function is used as an index into
\r
238 the xFlashRates array to obtain the delay period to use. */
\r
239 static const TickType_t xFlashRates[ ledNUM_OF_LED_CO_ROUTINES ] = { 150 / portTICK_PERIOD_MS,
\r
240 300 / portTICK_PERIOD_MS,
\r
241 450 / portTICK_PERIOD_MS,
\r
242 600 / portTICK_PERIOD_MS,
\r
243 750 / portTICK_PERIOD_MS,
\r
244 900 / portTICK_PERIOD_MS,
\r
245 1050 / portTICK_PERIOD_MS };
\r
247 /* Co-routines MUST start with a call to crSTART. */
\r
248 crSTART( xHandle );
\r
252 /* Toggle the LED. An offset of 8 is used to skip over the segments of
\r
253 the left side display which use the low numbers. */
\r
254 vParTestToggleLED( usIndex + 8 );
\r
256 /* Delay until it is time to toggle the segment that this co-routine is
\r
257 controlling again. */
\r
258 crDELAY( xHandle, xFlashRates[ usIndex ] );
\r
261 /* Co-routines MUST end with a call to crEND. */
\r
264 /*-----------------------------------------------------------*/
\r