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