2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
97 * Defines the tasks and co-routines used to toggle the segments of the two
\r
98 * seven segment displays, as described at the top of main.c
\r
102 #include <stdlib.h>
\r
104 /* Scheduler include files. */
\r
105 #include "FreeRTOS.h"
\r
107 #include "croutine.h"
\r
109 /* Demo program include files. */
\r
110 #include "partest.h"
\r
112 /*-----------------------------------------------------------*/
\r
114 /* One task per segment of the left side display. */
\r
115 #define ledNUM_OF_LED_TASKS ( 7 )
\r
117 /* Each task toggles at a frequency that is a multiple of 333ms. */
\r
118 #define ledFLASH_RATE_BASE ( ( TickType_t ) 333 )
\r
120 /* One co-routine per segment of the right hand display. */
\r
121 #define ledNUM_OF_LED_CO_ROUTINES 7
\r
123 /* All co-routines run at the same priority. */
\r
124 #define ledCO_ROUTINE_PRIORITY 0
\r
126 /*-----------------------------------------------------------*/
\r
128 /* The task that is created 7 times. */
\r
129 static void vLEDFlashTask( void *pvParameters );
\r
131 /* The co-routine that is created 7 times. */
\r
132 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, unsigned short usIndex );
\r
134 /* This task is created once, but itself creates 7 co-routines. */
\r
135 static void vLEDCoRoutineControlTask( void *pvParameters );
\r
137 /* Handles to each of the 7 tasks. Used so the tasks can be suspended
\r
139 static TaskHandle_t xFlashTaskHandles[ ledNUM_OF_LED_TASKS ] = { 0 };
\r
141 /* Handle to the task in which the co-routines run. Used so the
\r
142 co-routines can be suspended and resumed. */
\r
143 static TaskHandle_t xCoroutineTask;
\r
145 /*-----------------------------------------------------------*/
\r
148 * Creates the tasks and co-routines used to toggle the segments of the two
\r
149 * seven segment displays, as described at the top of main.c
\r
151 void vCreateFlashTasksAndCoRoutines( void )
\r
153 signed short sLEDTask;
\r
155 /* Create the tasks that flash segments on the first LED. */
\r
156 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
\r
158 /* Spawn the task. */
\r
159 xTaskCreate( vLEDFlashTask, "LEDt", configMINIMAL_STACK_SIZE, ( void * ) sLEDTask, ( tskIDLE_PRIORITY + 1 ), &( xFlashTaskHandles[ sLEDTask ] ) );
\r
162 /* Create the task in which the co-routines run. The co-routines themselves
\r
163 are created within the task. */
\r
164 xTaskCreate( vLEDCoRoutineControlTask, "LEDc", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCoroutineTask );
\r
166 /*-----------------------------------------------------------*/
\r
168 void vSuspendFlashTasks( unsigned char ucIndex, short sSuspendTasks )
\r
172 if( ucIndex == configLEFT_DISPLAY )
\r
174 /* Suspend or resume the tasks that are toggling the segments of the
\r
175 left side display. */
\r
176 for( sLEDTask = 0; sLEDTask < ledNUM_OF_LED_TASKS; ++sLEDTask )
\r
178 if( xFlashTaskHandles[ sLEDTask ] != NULL )
\r
180 if( sSuspendTasks == pdTRUE )
\r
182 vTaskSuspend( xFlashTaskHandles[ sLEDTask ] );
\r
186 vTaskResume( xFlashTaskHandles[ sLEDTask ] );
\r
193 /* Suspend or resume the task in which the co-routines are running. The
\r
194 co-routines toggle the segments of the right side display. */
\r
195 if( sSuspendTasks == pdTRUE )
\r
197 vTaskSuspend( xCoroutineTask );
\r
201 vTaskResume( xCoroutineTask );
\r
205 /*-----------------------------------------------------------*/
\r
207 static void vLEDFlashTask( void * pvParameters )
\r
209 TickType_t xFlashRate, xLastFlashTime;
\r
210 unsigned short usLED;
\r
212 /* The LED to flash is passed in as the task parameter. */
\r
213 usLED = ( unsigned short ) pvParameters;
\r
215 /* Calculate the rate at which this task is going to toggle its LED. */
\r
216 xFlashRate = ledFLASH_RATE_BASE + ( ledFLASH_RATE_BASE * ( TickType_t ) usLED );
\r
217 xFlashRate /= portTICK_PERIOD_MS;
\r
219 /* We will turn the LED on and off again in the delay period, so each
\r
220 delay is only half the total period. */
\r
221 xFlashRate /= ( TickType_t ) 2;
\r
223 /* We need to initialise xLastFlashTime prior to the first call to
\r
224 vTaskDelayUntil(). */
\r
225 xLastFlashTime = xTaskGetTickCount();
\r
229 /* Delay for half the flash period then turn the LED on. */
\r
230 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
\r
231 vParTestToggleLED( usLED );
\r
233 /* Delay for half the flash period then turn the LED off. */
\r
234 vTaskDelayUntil( &xLastFlashTime, xFlashRate );
\r
235 vParTestToggleLED( usLED );
\r
238 /*-----------------------------------------------------------*/
\r
240 static void vLEDCoRoutineControlTask( void *pvParameters )
\r
242 unsigned short usCoroutine;
\r
244 ( void ) pvParameters;
\r
246 /* Create the co-routines - one of each segment of the right side display. */
\r
247 for( usCoroutine = 0; usCoroutine < ledNUM_OF_LED_CO_ROUTINES; usCoroutine++ )
\r
249 xCoRoutineCreate( prvFixedDelayCoRoutine, ledCO_ROUTINE_PRIORITY, usCoroutine );
\r
252 /* This task has nothing else to do except scheduler the co-routines it just
\r
256 vCoRoutineSchedule();
\r
259 /*-----------------------------------------------------------*/
\r
261 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, unsigned short usIndex )
\r
263 /* The usIndex parameter of the co-routine function is used as an index into
\r
264 the xFlashRates array to obtain the delay period to use. */
\r
265 static const TickType_t xFlashRates[ ledNUM_OF_LED_CO_ROUTINES ] = { 150 / portTICK_PERIOD_MS,
\r
266 300 / portTICK_PERIOD_MS,
\r
267 450 / portTICK_PERIOD_MS,
\r
268 600 / portTICK_PERIOD_MS,
\r
269 750 / portTICK_PERIOD_MS,
\r
270 900 / portTICK_PERIOD_MS,
\r
271 1050 / portTICK_PERIOD_MS };
\r
273 /* Co-routines MUST start with a call to crSTART. */
\r
274 crSTART( xHandle );
\r
278 /* Toggle the LED. An offset of 8 is used to skip over the segments of
\r
279 the left side display which use the low numbers. */
\r
280 vParTestToggleLED( usIndex + 8 );
\r
282 /* Delay until it is time to toggle the segment that this co-routine is
\r
283 controlling again. */
\r
284 crDELAY( xHandle, xFlashRates[ usIndex ] );
\r
287 /* Co-routines MUST end with a call to crEND. */
\r
290 /*-----------------------------------------------------------*/
\r