2 FreeRTOS.org V5.0.4 - Copyright (C) 2003-2008 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
\r
30 * and even write all or part of your application on your behalf. *
\r
31 * See http://www.OpenRTOS.com for details of the services we provide to *
\r
32 * expedite your project. *
\r
34 ***************************************************************************
\r
35 ***************************************************************************
\r
37 Please ensure to read the configuration and relevant port sections of the
\r
38 online documentation.
\r
40 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
43 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
46 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
47 licensing and training services.
\r
51 * This demo application file demonstrates the use of queues to pass data
\r
52 * between co-routines.
\r
54 * N represents the number of 'fixed delay' co-routines that are created and
\r
55 * is set during initialisation.
\r
57 * N 'fixed delay' co-routines are created that just block for a fixed
\r
58 * period then post the number of an LED onto a queue. Each such co-routine
\r
59 * uses a different block period. A single 'flash' co-routine is also created
\r
60 * that blocks on the same queue, waiting for the number of the next LED it
\r
61 * should flash. Upon receiving a number it simply toggle the instructed LED
\r
62 * then blocks on the queue once more. In this manner each LED from LED 0 to
\r
63 * LED N-1 is caused to flash at a different rate.
\r
65 * The 'fixed delay' co-routines are created with co-routine priority 0. The
\r
66 * flash co-routine is created with co-routine priority 1. This means that
\r
67 * the queue should never contain more than a single item. This is because
\r
68 * posting to the queue will unblock the 'flash' co-routine, and as this has
\r
69 * a priority greater than the tasks posting to the queue it is guaranteed to
\r
70 * have emptied the queue and blocked once again before the queue can contain
\r
71 * any more date. An error is indicated if an attempt to post data to the
\r
72 * queue fails - indicating that the queue is already full.
\r
76 /* Scheduler includes. */
\r
77 #include "FreeRTOS.h"
\r
78 #include "croutine.h"
\r
81 /* Demo application includes. */
\r
82 #include "partest.h"
\r
83 #include "crflash.h"
\r
85 /* The queue should only need to be of length 1. See the description at the
\r
87 #define crfQUEUE_LENGTH 1
\r
89 #define crfFIXED_DELAY_PRIORITY 0
\r
90 #define crfFLASH_PRIORITY 1
\r
92 /* Only one flash co-routine is created so the index is not significant. */
\r
93 #define crfFLASH_INDEX 0
\r
95 /* Don't allow more than crfMAX_FLASH_TASKS 'fixed delay' co-routines to be
\r
97 #define crfMAX_FLASH_TASKS 8
\r
99 /* We don't want to block when posting to the queue. */
\r
100 #define crfPOSTING_BLOCK_TIME 0
\r
102 /* Added by MPi, this define is added in order to make the vParTestToggleLED()
\r
103 work. This basically differentiates the PDR09 from PDR00. 7-seg display LEDs connected
\r
104 to PDR09 (SEG1) are used by the prvFlashCoRoutine() and PDR00 (SEG2) are used by tasks. */
\r
105 #define PDR00_Offset 8
\r
108 * The 'fixed delay' co-routine as described at the top of the file.
\r
110 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
\r
113 * The 'flash' co-routine as described at the top of the file.
\r
115 static void prvFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
\r
117 /* The queue used to pass data between the 'fixed delay' co-routines and the
\r
118 'flash' co-routine. */
\r
119 static xQueueHandle xFlashQueue;
\r
121 /* This will be set to pdFALSE if we detect an error. */
\r
122 static unsigned portBASE_TYPE uxCoRoutineFlashStatus = pdPASS;
\r
124 /*-----------------------------------------------------------*/
\r
127 * See the header file for details.
\r
129 void vStartFlashCoRoutines( unsigned portBASE_TYPE uxNumberToCreate )
\r
131 unsigned portBASE_TYPE uxIndex;
\r
133 if( uxNumberToCreate > crfMAX_FLASH_TASKS )
\r
135 uxNumberToCreate = crfMAX_FLASH_TASKS;
\r
138 /* Create the queue used to pass data between the co-routines. */
\r
139 xFlashQueue = xQueueCreate( crfQUEUE_LENGTH, sizeof( unsigned portBASE_TYPE ) );
\r
143 /* Create uxNumberToCreate 'fixed delay' co-routines. */
\r
144 for( uxIndex = 0; uxIndex < uxNumberToCreate; uxIndex++ )
\r
146 xCoRoutineCreate( prvFixedDelayCoRoutine, crfFIXED_DELAY_PRIORITY, uxIndex );
\r
149 /* Create the 'flash' co-routine. */
\r
150 xCoRoutineCreate( prvFlashCoRoutine, crfFLASH_PRIORITY, crfFLASH_INDEX );
\r
153 /*-----------------------------------------------------------*/
\r
155 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
157 /* Even though this is a co-routine the xResult variable does not need to be
\r
158 static as we do not need it to maintain its state between blocks. */
\r
159 signed portBASE_TYPE xResult;
\r
160 /* The uxIndex parameter of the co-routine function is used as an index into
\r
161 the xFlashRates array to obtain the delay period to use. */
\r
162 static const portTickType xFlashRates[ crfMAX_FLASH_TASKS ] = { 150 / portTICK_RATE_MS,
\r
163 200 / portTICK_RATE_MS,
\r
164 250 / portTICK_RATE_MS,
\r
165 300 / portTICK_RATE_MS,
\r
166 350 / portTICK_RATE_MS,
\r
167 400 / portTICK_RATE_MS,
\r
168 450 / portTICK_RATE_MS,
\r
169 500 / portTICK_RATE_MS };
\r
171 /* Co-routines MUST start with a call to crSTART. */
\r
172 crSTART( xHandle );
\r
176 /* Post our uxIndex value onto the queue. This is used as the LED to
\r
178 crQUEUE_SEND( xHandle, xFlashQueue, ( void * ) &uxIndex, crfPOSTING_BLOCK_TIME, &xResult );
\r
180 if( xResult != pdPASS )
\r
182 /* For the reasons stated at the top of the file we should always
\r
183 find that we can post to the queue. If we could not then an error
\r
185 uxCoRoutineFlashStatus = pdFAIL;
\r
188 crDELAY( xHandle, xFlashRates[ uxIndex ] );
\r
191 /* Co-routines MUST end with a call to crEND. */
\r
194 /*-----------------------------------------------------------*/
\r
196 static void prvFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
198 /* Even though this is a co-routine the variable do not need to be
\r
199 static as we do not need it to maintain their state between blocks. */
\r
200 signed portBASE_TYPE xResult;
\r
201 unsigned portBASE_TYPE uxLEDToFlash;
\r
203 /* Co-routines MUST start with a call to crSTART. */
\r
204 crSTART( xHandle );
\r
209 /* Block to wait for the number of the LED to flash. */
\r
210 crQUEUE_RECEIVE( xHandle, xFlashQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );
\r
212 if( xResult != pdPASS )
\r
214 /* We would not expect to wake unless we received something. */
\r
215 uxCoRoutineFlashStatus = pdFAIL;
\r
219 /* We received the number of an LED to flash - flash it! */
\r
220 /* Added by MPi, PDR00_Offset is added in order to make the
\r
221 vParTestToggleLED() work. */
\r
222 vParTestToggleLED( uxLEDToFlash + PDR00_Offset );
\r
226 /* Co-routines MUST end with a call to crEND. */
\r
229 /*-----------------------------------------------------------*/
\r
231 portBASE_TYPE xAreFlashCoRoutinesStillRunning( void )
\r
233 /* Return pdPASS or pdFAIL depending on whether an error has been detected
\r
235 return uxCoRoutineFlashStatus;
\r