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 * This demo application file demonstrates the use of queues to pass data
\r
98 * between co-routines.
\r
100 * N represents the number of 'fixed delay' co-routines that are created and
\r
101 * is set during initialisation.
\r
103 * N 'fixed delay' co-routines are created that just block for a fixed
\r
104 * period then post the number of an LED onto a queue. Each such co-routine
\r
105 * uses a different block period. A single 'flash' co-routine is also created
\r
106 * that blocks on the same queue, waiting for the number of the next LED it
\r
107 * should flash. Upon receiving a number it simply toggle the instructed LED
\r
108 * then blocks on the queue once more. In this manner each LED from LED 0 to
\r
109 * LED N-1 is caused to flash at a different rate.
\r
111 * The 'fixed delay' co-routines are created with co-routine priority 0. The
\r
112 * flash co-routine is created with co-routine priority 1. This means that
\r
113 * the queue should never contain more than a single item. This is because
\r
114 * posting to the queue will unblock the 'flash' co-routine, and as this has
\r
115 * a priority greater than the tasks posting to the queue it is guaranteed to
\r
116 * have emptied the queue and blocked once again before the queue can contain
\r
117 * any more date. An error is indicated if an attempt to post data to the
\r
118 * queue fails - indicating that the queue is already full.
\r
122 /* Scheduler includes. */
\r
123 #include "FreeRTOS.h"
\r
124 #include "croutine.h"
\r
127 /* Demo application includes. */
\r
128 #include "partest.h"
\r
129 #include "crflash.h"
\r
131 /* The queue should only need to be of length 1. See the description at the
\r
132 top of the file. */
\r
133 #define crfQUEUE_LENGTH 1
\r
135 #define crfFIXED_DELAY_PRIORITY 0
\r
136 #define crfFLASH_PRIORITY 1
\r
138 /* Only one flash co-routine is created so the index is not significant. */
\r
139 #define crfFLASH_INDEX 0
\r
141 /* Don't allow more than crfMAX_FLASH_TASKS 'fixed delay' co-routines to be
\r
143 #define crfMAX_FLASH_TASKS 8
\r
145 /* We don't want to block when posting to the queue. */
\r
146 #define crfPOSTING_BLOCK_TIME 0
\r
149 * The 'fixed delay' co-routine as described at the top of the file.
\r
151 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex );
\r
154 * The 'flash' co-routine as described at the top of the file.
\r
156 static void prvFlashCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex );
\r
158 /* The queue used to pass data between the 'fixed delay' co-routines and the
\r
159 'flash' co-routine. */
\r
160 static QueueHandle_t xFlashQueue;
\r
162 /* This will be set to pdFALSE if we detect an error. */
\r
163 static BaseType_t xCoRoutineFlashStatus = pdPASS;
\r
165 /*-----------------------------------------------------------*/
\r
168 * See the header file for details.
\r
170 void vStartFlashCoRoutines( UBaseType_t uxNumberToCreate )
\r
172 UBaseType_t uxIndex;
\r
174 if( uxNumberToCreate > crfMAX_FLASH_TASKS )
\r
176 uxNumberToCreate = crfMAX_FLASH_TASKS;
\r
179 /* Create the queue used to pass data between the co-routines. */
\r
180 xFlashQueue = xQueueCreate( crfQUEUE_LENGTH, sizeof( UBaseType_t ) );
\r
184 /* Create uxNumberToCreate 'fixed delay' co-routines. */
\r
185 for( uxIndex = 0; uxIndex < uxNumberToCreate; uxIndex++ )
\r
187 xCoRoutineCreate( prvFixedDelayCoRoutine, crfFIXED_DELAY_PRIORITY, uxIndex );
\r
190 /* Create the 'flash' co-routine. */
\r
191 xCoRoutineCreate( prvFlashCoRoutine, crfFLASH_PRIORITY, crfFLASH_INDEX );
\r
194 /*-----------------------------------------------------------*/
\r
196 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
\r
198 /* Even though this is a co-routine the xResult variable does not need to be
\r
199 static as we do not need it to maintain its state between blocks. */
\r
200 BaseType_t xResult;
\r
201 /* The uxIndex parameter of the co-routine function is used as an index into
\r
202 the xFlashRates array to obtain the delay period to use. */
\r
203 static const TickType_t xFlashRates[ crfMAX_FLASH_TASKS ] = { 150 / portTICK_PERIOD_MS,
\r
204 200 / portTICK_PERIOD_MS,
\r
205 250 / portTICK_PERIOD_MS,
\r
206 300 / portTICK_PERIOD_MS,
\r
207 350 / portTICK_PERIOD_MS,
\r
208 400 / portTICK_PERIOD_MS,
\r
209 450 / portTICK_PERIOD_MS,
\r
210 500 / portTICK_PERIOD_MS };
\r
212 /* Co-routines MUST start with a call to crSTART. */
\r
213 crSTART( xHandle );
\r
217 /* Post our uxIndex value onto the queue. This is used as the LED to
\r
219 crQUEUE_SEND( xHandle, xFlashQueue, ( void * ) &uxIndex, crfPOSTING_BLOCK_TIME, &xResult );
\r
221 if( xResult != pdPASS )
\r
223 /* For the reasons stated at the top of the file we should always
\r
224 find that we can post to the queue. If we could not then an error
\r
226 xCoRoutineFlashStatus = pdFAIL;
\r
229 crDELAY( xHandle, xFlashRates[ uxIndex ] );
\r
232 /* Co-routines MUST end with a call to crEND. */
\r
235 /*-----------------------------------------------------------*/
\r
237 static void prvFlashCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
\r
239 /* Even though this is a co-routine the variable do not need to be
\r
240 static as we do not need it to maintain their state between blocks. */
\r
241 BaseType_t xResult;
\r
242 UBaseType_t uxLEDToFlash;
\r
244 /* Co-routines MUST start with a call to crSTART. */
\r
245 crSTART( xHandle );
\r
250 /* Block to wait for the number of the LED to flash. */
\r
251 crQUEUE_RECEIVE( xHandle, xFlashQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );
\r
253 if( xResult != pdPASS )
\r
255 /* We would not expect to wake unless we received something. */
\r
256 xCoRoutineFlashStatus = pdFAIL;
\r
260 /* We received the number of an LED to flash - flash it! */
\r
261 vParTestToggleLED( uxLEDToFlash );
\r
265 /* Co-routines MUST end with a call to crEND. */
\r
268 /*-----------------------------------------------------------*/
\r
270 BaseType_t xAreFlashCoRoutinesStillRunning( void )
\r
272 /* Return pdPASS or pdFAIL depending on whether an error has been detected
\r
274 return xCoRoutineFlashStatus;
\r