2 FreeRTOS V9.0.0 - 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 * This demo application file demonstrates the use of queues to pass data
\r
72 * between co-routines.
\r
74 * N represents the number of 'fixed delay' co-routines that are created and
\r
75 * is set during initialisation.
\r
77 * N 'fixed delay' co-routines are created that just block for a fixed
\r
78 * period then post the number of an LED onto a queue. Each such co-routine
\r
79 * uses a different block period. A single 'flash' co-routine is also created
\r
80 * that blocks on the same queue, waiting for the number of the next LED it
\r
81 * should flash. Upon receiving a number it simply toggle the instructed LED
\r
82 * then blocks on the queue once more. In this manner each LED from LED 0 to
\r
83 * LED N-1 is caused to flash at a different rate.
\r
85 * The 'fixed delay' co-routines are created with co-routine priority 0. The
\r
86 * flash co-routine is created with co-routine priority 1. This means that
\r
87 * the queue should never contain more than a single item. This is because
\r
88 * posting to the queue will unblock the 'flash' co-routine, and as this has
\r
89 * a priority greater than the tasks posting to the queue it is guaranteed to
\r
90 * have emptied the queue and blocked once again before the queue can contain
\r
91 * any more date. An error is indicated if an attempt to post data to the
\r
92 * queue fails - indicating that the queue is already full.
\r
96 /* Scheduler includes. */
\r
97 #include "FreeRTOS.h"
\r
98 #include "croutine.h"
\r
101 /* Demo application includes. */
\r
102 #include "partest.h"
\r
103 #include "crflash.h"
\r
105 /* The queue should only need to be of length 1. See the description at the
\r
106 top of the file. */
\r
107 #define crfQUEUE_LENGTH 1
\r
109 #define crfFIXED_DELAY_PRIORITY 0
\r
110 #define crfFLASH_PRIORITY 1
\r
112 /* Only one flash co-routine is created so the index is not significant. */
\r
113 #define crfFLASH_INDEX 0
\r
115 /* Don't allow more than crfMAX_FLASH_TASKS 'fixed delay' co-routines to be
\r
117 #define crfMAX_FLASH_TASKS 8
\r
119 /* We don't want to block when posting to the queue. */
\r
120 #define crfPOSTING_BLOCK_TIME 0
\r
122 /* Added by MPi, this define is added in order to make the vParTestToggleLED()
\r
123 work. This basically differentiates the PDR09 from PDR00. 7-seg display LEDs connected
\r
124 to PDR09 (SEG1) are used by the prvFlashCoRoutine() and PDR00 (SEG2) are used by tasks. */
\r
125 #define PDR00_Offset 8
\r
128 * The 'fixed delay' co-routine as described at the top of the file.
\r
130 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex );
\r
133 * The 'flash' co-routine as described at the top of the file.
\r
135 static void prvFlashCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex );
\r
137 /* The queue used to pass data between the 'fixed delay' co-routines and the
\r
138 'flash' co-routine. */
\r
139 static QueueHandle_t xFlashQueue;
\r
141 /* This will be set to pdFALSE if we detect an error. */
\r
142 static unsigned portBASE_TYPE uxCoRoutineFlashStatus = pdPASS;
\r
144 /*-----------------------------------------------------------*/
\r
147 * See the header file for details.
\r
149 void vStartFlashCoRoutines( unsigned portBASE_TYPE uxNumberToCreate )
\r
151 unsigned portBASE_TYPE uxIndex;
\r
153 if( uxNumberToCreate > crfMAX_FLASH_TASKS )
\r
155 uxNumberToCreate = crfMAX_FLASH_TASKS;
\r
158 /* Create the queue used to pass data between the co-routines. */
\r
159 xFlashQueue = xQueueCreate( crfQUEUE_LENGTH, sizeof( unsigned portBASE_TYPE ) );
\r
163 /* Create uxNumberToCreate 'fixed delay' co-routines. */
\r
164 for( uxIndex = 0; uxIndex < uxNumberToCreate; uxIndex++ )
\r
166 xCoRoutineCreate( prvFixedDelayCoRoutine, crfFIXED_DELAY_PRIORITY, uxIndex );
\r
169 /* Create the 'flash' co-routine. */
\r
170 xCoRoutineCreate( prvFlashCoRoutine, crfFLASH_PRIORITY, crfFLASH_INDEX );
\r
173 /*-----------------------------------------------------------*/
\r
175 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex )
\r
177 /* Even though this is a co-routine the xResult variable does not need to be
\r
178 static as we do not need it to maintain its state between blocks. */
\r
179 signed portBASE_TYPE xResult;
\r
180 /* The uxIndex parameter of the co-routine function is used as an index into
\r
181 the xFlashRates array to obtain the delay period to use. */
\r
182 static const TickType_t xFlashRates[ crfMAX_FLASH_TASKS ] = { 150 / portTICK_PERIOD_MS,
\r
183 200 / portTICK_PERIOD_MS,
\r
184 250 / portTICK_PERIOD_MS,
\r
185 300 / portTICK_PERIOD_MS,
\r
186 350 / portTICK_PERIOD_MS,
\r
187 400 / portTICK_PERIOD_MS,
\r
188 450 / portTICK_PERIOD_MS,
\r
189 500 / portTICK_PERIOD_MS };
\r
191 /* Co-routines MUST start with a call to crSTART. */
\r
192 crSTART( xHandle );
\r
196 /* Post our uxIndex value onto the queue. This is used as the LED to
\r
198 crQUEUE_SEND( xHandle, xFlashQueue, ( void * ) &uxIndex, crfPOSTING_BLOCK_TIME, &xResult );
\r
200 if( xResult != pdPASS )
\r
202 /* For the reasons stated at the top of the file we should always
\r
203 find that we can post to the queue. If we could not then an error
\r
205 uxCoRoutineFlashStatus = pdFAIL;
\r
208 crDELAY( xHandle, xFlashRates[ uxIndex ] );
\r
211 /* Co-routines MUST end with a call to crEND. */
\r
214 /*-----------------------------------------------------------*/
\r
216 static void prvFlashCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex )
\r
218 /* Even though this is a co-routine the variable do not need to be
\r
219 static as we do not need it to maintain their state between blocks. */
\r
220 signed portBASE_TYPE xResult;
\r
221 unsigned portBASE_TYPE uxLEDToFlash;
\r
223 /* Co-routines MUST start with a call to crSTART. */
\r
224 crSTART( xHandle );
\r
229 /* Block to wait for the number of the LED to flash. */
\r
230 crQUEUE_RECEIVE( xHandle, xFlashQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );
\r
232 if( xResult != pdPASS )
\r
234 /* We would not expect to wake unless we received something. */
\r
235 uxCoRoutineFlashStatus = pdFAIL;
\r
239 /* We received the number of an LED to flash - flash it! */
\r
240 /* Added by MPi, PDR00_Offset is added in order to make the
\r
241 vParTestToggleLED() work. */
\r
242 vParTestToggleLED( uxLEDToFlash + PDR00_Offset );
\r
246 /* Co-routines MUST end with a call to crEND. */
\r
249 /*-----------------------------------------------------------*/
\r
251 portBASE_TYPE xAreFlashCoRoutinesStillRunning( void )
\r
253 /* Return pdPASS or pdFAIL depending on whether an error has been detected
\r
255 return uxCoRoutineFlashStatus;
\r