1 |
606 |
jeremybenn |
/*
|
2 |
|
|
FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
|
3 |
|
|
|
4 |
|
|
***************************************************************************
|
5 |
|
|
* *
|
6 |
|
|
* If you are: *
|
7 |
|
|
* *
|
8 |
|
|
* + New to FreeRTOS, *
|
9 |
|
|
* + Wanting to learn FreeRTOS or multitasking in general quickly *
|
10 |
|
|
* + Looking for basic training, *
|
11 |
|
|
* + Wanting to improve your FreeRTOS skills and productivity *
|
12 |
|
|
* *
|
13 |
|
|
* then take a look at the FreeRTOS books - available as PDF or paperback *
|
14 |
|
|
* *
|
15 |
|
|
* "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
|
16 |
|
|
* http://www.FreeRTOS.org/Documentation *
|
17 |
|
|
* *
|
18 |
|
|
* A pdf reference manual is also available. Both are usually delivered *
|
19 |
|
|
* to your inbox within 20 minutes to two hours when purchased between 8am *
|
20 |
|
|
* and 8pm GMT (although please allow up to 24 hours in case of *
|
21 |
|
|
* exceptional circumstances). Thank you for your support! *
|
22 |
|
|
* *
|
23 |
|
|
***************************************************************************
|
24 |
|
|
|
25 |
|
|
This file is part of the FreeRTOS distribution.
|
26 |
|
|
|
27 |
|
|
FreeRTOS is free software; you can redistribute it and/or modify it under
|
28 |
|
|
the terms of the GNU General Public License (version 2) as published by the
|
29 |
|
|
Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
|
30 |
|
|
***NOTE*** The exception to the GPL is included to allow you to distribute
|
31 |
|
|
a combined work that includes FreeRTOS without being obliged to provide the
|
32 |
|
|
source code for proprietary components outside of the FreeRTOS kernel.
|
33 |
|
|
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
|
34 |
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
35 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
36 |
|
|
more details. You should have received a copy of the GNU General Public
|
37 |
|
|
License and the FreeRTOS license exception along with FreeRTOS; if not it
|
38 |
|
|
can be viewed here: http://www.freertos.org/a00114.html and also obtained
|
39 |
|
|
by writing to Richard Barry, contact details for whom are available on the
|
40 |
|
|
FreeRTOS WEB site.
|
41 |
|
|
|
42 |
|
|
1 tab == 4 spaces!
|
43 |
|
|
|
44 |
|
|
http://www.FreeRTOS.org - Documentation, latest information, license and
|
45 |
|
|
contact details.
|
46 |
|
|
|
47 |
|
|
http://www.SafeRTOS.com - A version that is certified for use in safety
|
48 |
|
|
critical systems.
|
49 |
|
|
|
50 |
|
|
http://www.OpenRTOS.com - Commercial support, development, porting,
|
51 |
|
|
licensing and training services.
|
52 |
|
|
*/
|
53 |
|
|
|
54 |
|
|
/*
|
55 |
|
|
* This demo application file demonstrates the use of queues to pass data
|
56 |
|
|
* between co-routines.
|
57 |
|
|
*
|
58 |
|
|
* N represents the number of 'fixed delay' co-routines that are created and
|
59 |
|
|
* is set during initialisation.
|
60 |
|
|
*
|
61 |
|
|
* N 'fixed delay' co-routines are created that just block for a fixed
|
62 |
|
|
* period then post the number of an LED onto a queue. Each such co-routine
|
63 |
|
|
* uses a different block period. A single 'flash' co-routine is also created
|
64 |
|
|
* that blocks on the same queue, waiting for the number of the next LED it
|
65 |
|
|
* should flash. Upon receiving a number it simply toggle the instructed LED
|
66 |
|
|
* then blocks on the queue once more. In this manner each LED from LED 0 to
|
67 |
|
|
* LED N-1 is caused to flash at a different rate.
|
68 |
|
|
*
|
69 |
|
|
* The 'fixed delay' co-routines are created with co-routine priority 0. The
|
70 |
|
|
* flash co-routine is created with co-routine priority 1. This means that
|
71 |
|
|
* the queue should never contain more than a single item. This is because
|
72 |
|
|
* posting to the queue will unblock the 'flash' co-routine, and as this has
|
73 |
|
|
* a priority greater than the tasks posting to the queue it is guaranteed to
|
74 |
|
|
* have emptied the queue and blocked once again before the queue can contain
|
75 |
|
|
* any more date. An error is indicated if an attempt to post data to the
|
76 |
|
|
* queue fails - indicating that the queue is already full.
|
77 |
|
|
*
|
78 |
|
|
*/
|
79 |
|
|
|
80 |
|
|
/* Scheduler includes. */
|
81 |
|
|
#include "FreeRTOS.h"
|
82 |
|
|
#include "croutine.h"
|
83 |
|
|
#include "queue.h"
|
84 |
|
|
|
85 |
|
|
/* Demo application includes. */
|
86 |
|
|
#include "partest.h"
|
87 |
|
|
#include "crflash.h"
|
88 |
|
|
|
89 |
|
|
/* The queue should only need to be of length 1. See the description at the
|
90 |
|
|
top of the file. */
|
91 |
|
|
#define crfQUEUE_LENGTH 1
|
92 |
|
|
|
93 |
|
|
#define crfFIXED_DELAY_PRIORITY 0
|
94 |
|
|
#define crfFLASH_PRIORITY 1
|
95 |
|
|
|
96 |
|
|
/* Only one flash co-routine is created so the index is not significant. */
|
97 |
|
|
#define crfFLASH_INDEX 0
|
98 |
|
|
|
99 |
|
|
/* Don't allow more than crfMAX_FLASH_TASKS 'fixed delay' co-routines to be
|
100 |
|
|
created. */
|
101 |
|
|
#define crfMAX_FLASH_TASKS 8
|
102 |
|
|
|
103 |
|
|
/* We don't want to block when posting to the queue. */
|
104 |
|
|
#define crfPOSTING_BLOCK_TIME 0
|
105 |
|
|
|
106 |
|
|
/*
|
107 |
|
|
* The 'fixed delay' co-routine as described at the top of the file.
|
108 |
|
|
*/
|
109 |
|
|
static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
|
110 |
|
|
|
111 |
|
|
/*
|
112 |
|
|
* The 'flash' co-routine as described at the top of the file.
|
113 |
|
|
*/
|
114 |
|
|
static void prvFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
|
115 |
|
|
|
116 |
|
|
/* The queue used to pass data between the 'fixed delay' co-routines and the
|
117 |
|
|
'flash' co-routine. */
|
118 |
|
|
static xQueueHandle xFlashQueue;
|
119 |
|
|
|
120 |
|
|
/* This will be set to pdFALSE if we detect an error. */
|
121 |
|
|
static portBASE_TYPE xCoRoutineFlashStatus = pdPASS;
|
122 |
|
|
|
123 |
|
|
/*-----------------------------------------------------------*/
|
124 |
|
|
|
125 |
|
|
/*
|
126 |
|
|
* See the header file for details.
|
127 |
|
|
*/
|
128 |
|
|
void vStartFlashCoRoutines( unsigned portBASE_TYPE uxNumberToCreate )
|
129 |
|
|
{
|
130 |
|
|
unsigned portBASE_TYPE uxIndex;
|
131 |
|
|
|
132 |
|
|
if( uxNumberToCreate > crfMAX_FLASH_TASKS )
|
133 |
|
|
{
|
134 |
|
|
uxNumberToCreate = crfMAX_FLASH_TASKS;
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
/* Create the queue used to pass data between the co-routines. */
|
138 |
|
|
xFlashQueue = xQueueCreate( crfQUEUE_LENGTH, sizeof( unsigned portBASE_TYPE ) );
|
139 |
|
|
|
140 |
|
|
if( xFlashQueue )
|
141 |
|
|
{
|
142 |
|
|
/* Create uxNumberToCreate 'fixed delay' co-routines. */
|
143 |
|
|
for( uxIndex = 0; uxIndex < uxNumberToCreate; uxIndex++ )
|
144 |
|
|
{
|
145 |
|
|
xCoRoutineCreate( prvFixedDelayCoRoutine, crfFIXED_DELAY_PRIORITY, uxIndex );
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
/* Create the 'flash' co-routine. */
|
149 |
|
|
xCoRoutineCreate( prvFlashCoRoutine, crfFLASH_PRIORITY, crfFLASH_INDEX );
|
150 |
|
|
}
|
151 |
|
|
}
|
152 |
|
|
/*-----------------------------------------------------------*/
|
153 |
|
|
|
154 |
|
|
static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
|
155 |
|
|
{
|
156 |
|
|
/* Even though this is a co-routine the xResult variable does not need to be
|
157 |
|
|
static as we do not need it to maintain its state between blocks. */
|
158 |
|
|
signed portBASE_TYPE xResult;
|
159 |
|
|
/* The uxIndex parameter of the co-routine function is used as an index into
|
160 |
|
|
the xFlashRates array to obtain the delay period to use. */
|
161 |
|
|
static const portTickType xFlashRates[ crfMAX_FLASH_TASKS ] = { 150 / portTICK_RATE_MS,
|
162 |
|
|
200 / portTICK_RATE_MS,
|
163 |
|
|
250 / portTICK_RATE_MS,
|
164 |
|
|
300 / portTICK_RATE_MS,
|
165 |
|
|
350 / portTICK_RATE_MS,
|
166 |
|
|
400 / portTICK_RATE_MS,
|
167 |
|
|
450 / portTICK_RATE_MS,
|
168 |
|
|
500 / portTICK_RATE_MS };
|
169 |
|
|
|
170 |
|
|
/* Co-routines MUST start with a call to crSTART. */
|
171 |
|
|
crSTART( xHandle );
|
172 |
|
|
|
173 |
|
|
for( ;; )
|
174 |
|
|
{
|
175 |
|
|
/* Post our uxIndex value onto the queue. This is used as the LED to
|
176 |
|
|
flash. */
|
177 |
|
|
crQUEUE_SEND( xHandle, xFlashQueue, ( void * ) &uxIndex, crfPOSTING_BLOCK_TIME, &xResult );
|
178 |
|
|
|
179 |
|
|
if( xResult != pdPASS )
|
180 |
|
|
{
|
181 |
|
|
/* For the reasons stated at the top of the file we should always
|
182 |
|
|
find that we can post to the queue. If we could not then an error
|
183 |
|
|
has occurred. */
|
184 |
|
|
xCoRoutineFlashStatus = pdFAIL;
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
crDELAY( xHandle, xFlashRates[ uxIndex ] );
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
/* Co-routines MUST end with a call to crEND. */
|
191 |
|
|
crEND();
|
192 |
|
|
}
|
193 |
|
|
/*-----------------------------------------------------------*/
|
194 |
|
|
|
195 |
|
|
static void prvFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
|
196 |
|
|
{
|
197 |
|
|
/* Even though this is a co-routine the variable do not need to be
|
198 |
|
|
static as we do not need it to maintain their state between blocks. */
|
199 |
|
|
signed portBASE_TYPE xResult;
|
200 |
|
|
unsigned portBASE_TYPE uxLEDToFlash;
|
201 |
|
|
|
202 |
|
|
/* Co-routines MUST start with a call to crSTART. */
|
203 |
|
|
crSTART( xHandle );
|
204 |
|
|
( void ) uxIndex;
|
205 |
|
|
|
206 |
|
|
for( ;; )
|
207 |
|
|
{
|
208 |
|
|
/* Block to wait for the number of the LED to flash. */
|
209 |
|
|
crQUEUE_RECEIVE( xHandle, xFlashQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );
|
210 |
|
|
|
211 |
|
|
if( xResult != pdPASS )
|
212 |
|
|
{
|
213 |
|
|
/* We would not expect to wake unless we received something. */
|
214 |
|
|
xCoRoutineFlashStatus = pdFAIL;
|
215 |
|
|
}
|
216 |
|
|
else
|
217 |
|
|
{
|
218 |
|
|
/* We received the number of an LED to flash - flash it! */
|
219 |
|
|
vParTestToggleLED( uxLEDToFlash );
|
220 |
|
|
}
|
221 |
|
|
}
|
222 |
|
|
|
223 |
|
|
/* Co-routines MUST end with a call to crEND. */
|
224 |
|
|
crEND();
|
225 |
|
|
}
|
226 |
|
|
/*-----------------------------------------------------------*/
|
227 |
|
|
|
228 |
|
|
portBASE_TYPE xAreFlashCoRoutinesStillRunning( void )
|
229 |
|
|
{
|
230 |
|
|
/* Return pdPASS or pdFAIL depending on whether an error has been detected
|
231 |
|
|
or not. */
|
232 |
|
|
return xCoRoutineFlashStatus;
|
233 |
|
|
}
|
234 |
|
|
|