2 FreeRTOS.org V5.2.0 - Copyright (C) 2003-2009 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 it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
10 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
15 You should have received a copy of the GNU General Public License along
\r
16 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
17 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
19 A special exception to the GPL is included to allow you to distribute a
\r
20 combined work that includes FreeRTOS.org without being obliged to provide
\r
21 the source code for any proprietary components. See the licensing section
\r
22 of http://www.FreeRTOS.org for full details.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
53 * This demo file demonstrates how to send data between an ISR and a
\r
54 * co-routine. A tick hook function is used to periodically pass data between
\r
55 * the RTOS tick and a set of 'hook' co-routines.
\r
57 * hookNUM_HOOK_CO_ROUTINES co-routines are created. Each co-routine blocks
\r
58 * to wait for a character to be received on a queue from the tick ISR, checks
\r
59 * to ensure the character received was that expected, then sends the number
\r
60 * back to the tick ISR on a different queue.
\r
62 * The tick ISR checks the numbers received back from the 'hook' co-routines
\r
63 * matches the number previously sent.
\r
65 * If at any time a queue function returns unexpectedly, or an incorrect value
\r
66 * is received either by the tick hook or a co-routine then an error is
\r
69 * This demo relies on each 'hook' co-routine to execute between each
\r
70 * hookTICK_CALLS_BEFORE_POST tick interrupts. This and the heavy use of
\r
71 * queues from within an interrupt may result in an error being detected on
\r
72 * slower targets simply due to timing.
\r
75 /* Scheduler includes. */
\r
76 #include "FreeRTOS.h"
\r
77 #include "croutine.h"
\r
80 /* Demo application includes. */
\r
83 /* The number of 'hook' co-routines that are to be created. */
\r
84 #define hookNUM_HOOK_CO_ROUTINES ( 4 )
\r
86 /* The number of times the tick hook should be called before a character is
\r
87 posted to the 'hook' co-routines. */
\r
88 #define hookTICK_CALLS_BEFORE_POST ( 500 )
\r
90 /* There should never be more than one item in any queue at any time. */
\r
91 #define hookHOOK_QUEUE_LENGTH ( 1 )
\r
93 /* Don't block when initially posting to the queue. */
\r
94 #define hookNO_BLOCK_TIME ( 0 )
\r
96 /* The priority relative to other co-routines (rather than tasks) that the
\r
97 'hook' co-routines should take. */
\r
98 #define mainHOOK_CR_PRIORITY ( 1 )
\r
99 /*-----------------------------------------------------------*/
\r
102 * The co-routine function itself.
\r
104 static void prvHookCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
\r
108 * The tick hook function. This receives a number from each 'hook' co-routine
\r
109 * then sends a number to each co-routine. An error is flagged if a send or
\r
110 * receive fails, or an unexpected number is received.
\r
112 void vApplicationTickHook( void );
\r
114 /*-----------------------------------------------------------*/
\r
116 /* Queues used to send data FROM a co-routine TO the tick hook function.
\r
117 The hook functions received (Rx's) on these queues. One queue per
\r
118 'hook' co-routine. */
\r
119 static xQueueHandle xHookRxQueues[ hookNUM_HOOK_CO_ROUTINES ];
\r
121 /* Queues used to send data FROM the tick hook TO a co-routine function.
\r
122 The hood function transmits (Tx's) on these queues. One queue per
\r
123 'hook' co-routine. */
\r
124 static xQueueHandle xHookTxQueues[ hookNUM_HOOK_CO_ROUTINES ];
\r
126 /* Set to true if an error is detected at any time. */
\r
127 static portBASE_TYPE xCoRoutineErrorDetected = pdFALSE;
\r
129 /*-----------------------------------------------------------*/
\r
131 void vStartHookCoRoutines( void )
\r
133 unsigned portBASE_TYPE uxIndex, uxValueToPost = 0;
\r
135 for( uxIndex = 0; uxIndex < hookNUM_HOOK_CO_ROUTINES; uxIndex++ )
\r
137 /* Create a queue to transmit to and receive from each 'hook'
\r
139 xHookRxQueues[ uxIndex ] = xQueueCreate( hookHOOK_QUEUE_LENGTH, sizeof( unsigned portBASE_TYPE ) );
\r
140 xHookTxQueues[ uxIndex ] = xQueueCreate( hookHOOK_QUEUE_LENGTH, sizeof( unsigned portBASE_TYPE ) );
\r
142 /* To start things off the tick hook function expects the queue it
\r
143 uses to receive data to contain a value. */
\r
144 xQueueSend( xHookRxQueues[ uxIndex ], &uxValueToPost, hookNO_BLOCK_TIME );
\r
146 /* Create the 'hook' co-routine itself. */
\r
147 xCoRoutineCreate( prvHookCoRoutine, mainHOOK_CR_PRIORITY, uxIndex );
\r
150 /*-----------------------------------------------------------*/
\r
152 static unsigned portBASE_TYPE uxCallCounter = 0, uxNumberToPost = 0;
\r
153 void vApplicationTickHook( void )
\r
155 unsigned portBASE_TYPE uxReceivedNumber;
\r
156 signed portBASE_TYPE xIndex, xCoRoutineWoken;
\r
158 /* Is it time to talk to the 'hook' co-routines again? */
\r
160 if( uxCallCounter >= hookTICK_CALLS_BEFORE_POST )
\r
164 for( xIndex = 0; xIndex < hookNUM_HOOK_CO_ROUTINES; xIndex++ )
\r
166 xCoRoutineWoken = pdFALSE;
\r
167 if( crQUEUE_RECEIVE_FROM_ISR( xHookRxQueues[ xIndex ], &uxReceivedNumber, &xCoRoutineWoken ) != pdPASS )
\r
169 /* There is no reason why we would not expect the queue to
\r
170 contain a value. */
\r
171 xCoRoutineErrorDetected = pdTRUE;
\r
175 /* Each queue used to receive data from the 'hook' co-routines
\r
176 should contain the number we last posted to the same co-routine. */
\r
177 if( uxReceivedNumber != uxNumberToPost )
\r
179 xCoRoutineErrorDetected = pdTRUE;
\r
182 /* Nothing should be blocked waiting to post to the queue. */
\r
183 if( xCoRoutineWoken != pdFALSE )
\r
185 xCoRoutineErrorDetected = pdTRUE;
\r
190 /* Start the next cycle by posting the next number onto each Tx queue. */
\r
193 for( xIndex = 0; xIndex < hookNUM_HOOK_CO_ROUTINES; xIndex++ )
\r
195 if( crQUEUE_SEND_FROM_ISR( xHookTxQueues[ xIndex ], &uxNumberToPost, pdFALSE ) != pdTRUE )
\r
197 /* Posting to the queue should have woken the co-routine that
\r
198 was blocked on the queue. */
\r
199 xCoRoutineErrorDetected = pdTRUE;
\r
204 /*-----------------------------------------------------------*/
\r
206 static void prvHookCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
208 static unsigned portBASE_TYPE uxReceivedValue[ hookNUM_HOOK_CO_ROUTINES ];
\r
209 portBASE_TYPE xResult;
\r
211 /* Each co-routine MUST start with a call to crSTART(); */
\r
212 crSTART( xHandle );
\r
216 /* Wait to receive a value from the tick hook. */
\r
218 crQUEUE_RECEIVE( xHandle, xHookTxQueues[ uxIndex ], &( uxReceivedValue[ uxIndex ] ), portMAX_DELAY, &xResult );
\r
220 /* There is no reason why we should not have received something on
\r
222 if( xResult != pdPASS )
\r
224 xCoRoutineErrorDetected = pdTRUE;
\r
227 /* Send the same number back to the idle hook so it can verify it. */
\r
229 crQUEUE_SEND( xHandle, xHookRxQueues[ uxIndex ], &( uxReceivedValue[ uxIndex ] ), hookNO_BLOCK_TIME, &xResult );
\r
230 if( xResult != pdPASS )
\r
232 /* There is no reason why we should not have been able to post to
\r
234 xCoRoutineErrorDetected = pdTRUE;
\r
238 /* Each co-routine MUST end with a call to crEND(). */
\r
241 /*-----------------------------------------------------------*/
\r
243 portBASE_TYPE xAreHookCoRoutinesStillRunning( void )
\r
245 if( xCoRoutineErrorDetected )
\r