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 file demonstrates how to send data between an ISR and a
|
56 |
|
|
* co-routine. A tick hook function is used to periodically pass data between
|
57 |
|
|
* the RTOS tick and a set of 'hook' co-routines.
|
58 |
|
|
*
|
59 |
|
|
* hookNUM_HOOK_CO_ROUTINES co-routines are created. Each co-routine blocks
|
60 |
|
|
* to wait for a character to be received on a queue from the tick ISR, checks
|
61 |
|
|
* to ensure the character received was that expected, then sends the number
|
62 |
|
|
* back to the tick ISR on a different queue.
|
63 |
|
|
*
|
64 |
|
|
* The tick ISR checks the numbers received back from the 'hook' co-routines
|
65 |
|
|
* matches the number previously sent.
|
66 |
|
|
*
|
67 |
|
|
* If at any time a queue function returns unexpectedly, or an incorrect value
|
68 |
|
|
* is received either by the tick hook or a co-routine then an error is
|
69 |
|
|
* latched.
|
70 |
|
|
*
|
71 |
|
|
* This demo relies on each 'hook' co-routine to execute between each
|
72 |
|
|
* hookTICK_CALLS_BEFORE_POST tick interrupts. This and the heavy use of
|
73 |
|
|
* queues from within an interrupt may result in an error being detected on
|
74 |
|
|
* slower targets simply due to timing.
|
75 |
|
|
*/
|
76 |
|
|
|
77 |
|
|
/* Scheduler includes. */
|
78 |
|
|
#include "FreeRTOS.h"
|
79 |
|
|
#include "croutine.h"
|
80 |
|
|
#include "queue.h"
|
81 |
|
|
|
82 |
|
|
/* Demo application includes. */
|
83 |
|
|
#include "crhook.h"
|
84 |
|
|
|
85 |
|
|
/* The number of 'hook' co-routines that are to be created. */
|
86 |
|
|
#define hookNUM_HOOK_CO_ROUTINES ( 4 )
|
87 |
|
|
|
88 |
|
|
/* The number of times the tick hook should be called before a character is
|
89 |
|
|
posted to the 'hook' co-routines. */
|
90 |
|
|
#define hookTICK_CALLS_BEFORE_POST ( 500 )
|
91 |
|
|
|
92 |
|
|
/* There should never be more than one item in any queue at any time. */
|
93 |
|
|
#define hookHOOK_QUEUE_LENGTH ( 1 )
|
94 |
|
|
|
95 |
|
|
/* Don't block when initially posting to the queue. */
|
96 |
|
|
#define hookNO_BLOCK_TIME ( 0 )
|
97 |
|
|
|
98 |
|
|
/* The priority relative to other co-routines (rather than tasks) that the
|
99 |
|
|
'hook' co-routines should take. */
|
100 |
|
|
#define mainHOOK_CR_PRIORITY ( 1 )
|
101 |
|
|
/*-----------------------------------------------------------*/
|
102 |
|
|
|
103 |
|
|
/*
|
104 |
|
|
* The co-routine function itself.
|
105 |
|
|
*/
|
106 |
|
|
static void prvHookCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
/*
|
110 |
|
|
* The tick hook function. This receives a number from each 'hook' co-routine
|
111 |
|
|
* then sends a number to each co-routine. An error is flagged if a send or
|
112 |
|
|
* receive fails, or an unexpected number is received.
|
113 |
|
|
*/
|
114 |
|
|
void vApplicationTickHook( void );
|
115 |
|
|
|
116 |
|
|
/*-----------------------------------------------------------*/
|
117 |
|
|
|
118 |
|
|
/* Queues used to send data FROM a co-routine TO the tick hook function.
|
119 |
|
|
The hook functions received (Rx's) on these queues. One queue per
|
120 |
|
|
'hook' co-routine. */
|
121 |
|
|
static xQueueHandle xHookRxQueues[ hookNUM_HOOK_CO_ROUTINES ];
|
122 |
|
|
|
123 |
|
|
/* Queues used to send data FROM the tick hook TO a co-routine function.
|
124 |
|
|
The hood function transmits (Tx's) on these queues. One queue per
|
125 |
|
|
'hook' co-routine. */
|
126 |
|
|
static xQueueHandle xHookTxQueues[ hookNUM_HOOK_CO_ROUTINES ];
|
127 |
|
|
|
128 |
|
|
/* Set to true if an error is detected at any time. */
|
129 |
|
|
static portBASE_TYPE xCoRoutineErrorDetected = pdFALSE;
|
130 |
|
|
|
131 |
|
|
/*-----------------------------------------------------------*/
|
132 |
|
|
|
133 |
|
|
void vStartHookCoRoutines( void )
|
134 |
|
|
{
|
135 |
|
|
unsigned portBASE_TYPE uxIndex, uxValueToPost = 0;
|
136 |
|
|
|
137 |
|
|
for( uxIndex = 0; uxIndex < hookNUM_HOOK_CO_ROUTINES; uxIndex++ )
|
138 |
|
|
{
|
139 |
|
|
/* Create a queue to transmit to and receive from each 'hook'
|
140 |
|
|
co-routine. */
|
141 |
|
|
xHookRxQueues[ uxIndex ] = xQueueCreate( hookHOOK_QUEUE_LENGTH, sizeof( unsigned portBASE_TYPE ) );
|
142 |
|
|
xHookTxQueues[ uxIndex ] = xQueueCreate( hookHOOK_QUEUE_LENGTH, sizeof( unsigned portBASE_TYPE ) );
|
143 |
|
|
|
144 |
|
|
/* To start things off the tick hook function expects the queue it
|
145 |
|
|
uses to receive data to contain a value. */
|
146 |
|
|
xQueueSend( xHookRxQueues[ uxIndex ], &uxValueToPost, hookNO_BLOCK_TIME );
|
147 |
|
|
|
148 |
|
|
/* Create the 'hook' co-routine itself. */
|
149 |
|
|
xCoRoutineCreate( prvHookCoRoutine, mainHOOK_CR_PRIORITY, uxIndex );
|
150 |
|
|
}
|
151 |
|
|
}
|
152 |
|
|
/*-----------------------------------------------------------*/
|
153 |
|
|
|
154 |
|
|
static unsigned portBASE_TYPE uxCallCounter = 0, uxNumberToPost = 0;
|
155 |
|
|
void vApplicationTickHook( void )
|
156 |
|
|
{
|
157 |
|
|
unsigned portBASE_TYPE uxReceivedNumber;
|
158 |
|
|
signed portBASE_TYPE xIndex, xCoRoutineWoken;
|
159 |
|
|
|
160 |
|
|
/* Is it time to talk to the 'hook' co-routines again? */
|
161 |
|
|
uxCallCounter++;
|
162 |
|
|
if( uxCallCounter >= hookTICK_CALLS_BEFORE_POST )
|
163 |
|
|
{
|
164 |
|
|
uxCallCounter = 0;
|
165 |
|
|
|
166 |
|
|
for( xIndex = 0; xIndex < hookNUM_HOOK_CO_ROUTINES; xIndex++ )
|
167 |
|
|
{
|
168 |
|
|
xCoRoutineWoken = pdFALSE;
|
169 |
|
|
if( crQUEUE_RECEIVE_FROM_ISR( xHookRxQueues[ xIndex ], &uxReceivedNumber, &xCoRoutineWoken ) != pdPASS )
|
170 |
|
|
{
|
171 |
|
|
/* There is no reason why we would not expect the queue to
|
172 |
|
|
contain a value. */
|
173 |
|
|
xCoRoutineErrorDetected = pdTRUE;
|
174 |
|
|
}
|
175 |
|
|
else
|
176 |
|
|
{
|
177 |
|
|
/* Each queue used to receive data from the 'hook' co-routines
|
178 |
|
|
should contain the number we last posted to the same co-routine. */
|
179 |
|
|
if( uxReceivedNumber != uxNumberToPost )
|
180 |
|
|
{
|
181 |
|
|
xCoRoutineErrorDetected = pdTRUE;
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
/* Nothing should be blocked waiting to post to the queue. */
|
185 |
|
|
if( xCoRoutineWoken != pdFALSE )
|
186 |
|
|
{
|
187 |
|
|
xCoRoutineErrorDetected = pdTRUE;
|
188 |
|
|
}
|
189 |
|
|
}
|
190 |
|
|
}
|
191 |
|
|
|
192 |
|
|
/* Start the next cycle by posting the next number onto each Tx queue. */
|
193 |
|
|
uxNumberToPost++;
|
194 |
|
|
|
195 |
|
|
for( xIndex = 0; xIndex < hookNUM_HOOK_CO_ROUTINES; xIndex++ )
|
196 |
|
|
{
|
197 |
|
|
if( crQUEUE_SEND_FROM_ISR( xHookTxQueues[ xIndex ], &uxNumberToPost, pdFALSE ) != pdTRUE )
|
198 |
|
|
{
|
199 |
|
|
/* Posting to the queue should have woken the co-routine that
|
200 |
|
|
was blocked on the queue. */
|
201 |
|
|
xCoRoutineErrorDetected = pdTRUE;
|
202 |
|
|
}
|
203 |
|
|
}
|
204 |
|
|
}
|
205 |
|
|
}
|
206 |
|
|
/*-----------------------------------------------------------*/
|
207 |
|
|
|
208 |
|
|
static void prvHookCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
|
209 |
|
|
{
|
210 |
|
|
static unsigned portBASE_TYPE uxReceivedValue[ hookNUM_HOOK_CO_ROUTINES ];
|
211 |
|
|
portBASE_TYPE xResult;
|
212 |
|
|
|
213 |
|
|
/* Each co-routine MUST start with a call to crSTART(); */
|
214 |
|
|
crSTART( xHandle );
|
215 |
|
|
|
216 |
|
|
for( ;; )
|
217 |
|
|
{
|
218 |
|
|
/* Wait to receive a value from the tick hook. */
|
219 |
|
|
xResult = pdFAIL;
|
220 |
|
|
crQUEUE_RECEIVE( xHandle, xHookTxQueues[ uxIndex ], &( uxReceivedValue[ uxIndex ] ), portMAX_DELAY, &xResult );
|
221 |
|
|
|
222 |
|
|
/* There is no reason why we should not have received something on
|
223 |
|
|
the queue. */
|
224 |
|
|
if( xResult != pdPASS )
|
225 |
|
|
{
|
226 |
|
|
xCoRoutineErrorDetected = pdTRUE;
|
227 |
|
|
}
|
228 |
|
|
|
229 |
|
|
/* Send the same number back to the idle hook so it can verify it. */
|
230 |
|
|
xResult = pdFAIL;
|
231 |
|
|
crQUEUE_SEND( xHandle, xHookRxQueues[ uxIndex ], &( uxReceivedValue[ uxIndex ] ), hookNO_BLOCK_TIME, &xResult );
|
232 |
|
|
if( xResult != pdPASS )
|
233 |
|
|
{
|
234 |
|
|
/* There is no reason why we should not have been able to post to
|
235 |
|
|
the queue. */
|
236 |
|
|
xCoRoutineErrorDetected = pdTRUE;
|
237 |
|
|
}
|
238 |
|
|
}
|
239 |
|
|
|
240 |
|
|
/* Each co-routine MUST end with a call to crEND(). */
|
241 |
|
|
crEND();
|
242 |
|
|
}
|
243 |
|
|
/*-----------------------------------------------------------*/
|
244 |
|
|
|
245 |
|
|
portBASE_TYPE xAreHookCoRoutinesStillRunning( void )
|
246 |
|
|
{
|
247 |
|
|
if( xCoRoutineErrorDetected )
|
248 |
|
|
{
|
249 |
|
|
return pdFALSE;
|
250 |
|
|
}
|
251 |
|
|
else
|
252 |
|
|
{
|
253 |
|
|
return pdTRUE;
|
254 |
|
|
}
|
255 |
|
|
}
|
256 |
|
|
|
257 |
|
|
|
258 |
|
|
|