2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 * This demo file demonstrates how to send data between an ISR and a
\r
71 * co-routine. A tick hook function is used to periodically pass data between
\r
72 * the RTOS tick and a set of 'hook' co-routines.
\r
74 * hookNUM_HOOK_CO_ROUTINES co-routines are created. Each co-routine blocks
\r
75 * to wait for a character to be received on a queue from the tick ISR, checks
\r
76 * to ensure the character received was that expected, then sends the number
\r
77 * back to the tick ISR on a different queue.
\r
79 * The tick ISR checks the numbers received back from the 'hook' co-routines
\r
80 * matches the number previously sent.
\r
82 * If at any time a queue function returns unexpectedly, or an incorrect value
\r
83 * is received either by the tick hook or a co-routine then an error is
\r
86 * This demo relies on each 'hook' co-routine to execute between each
\r
87 * hookTICK_CALLS_BEFORE_POST tick interrupts. This and the heavy use of
\r
88 * queues from within an interrupt may result in an error being detected on
\r
89 * slower targets simply due to timing.
\r
92 /* Scheduler includes. */
\r
93 #include "FreeRTOS.h"
\r
94 #include "croutine.h"
\r
97 /* Demo application includes. */
\r
100 /* The number of 'hook' co-routines that are to be created. */
\r
101 #define hookNUM_HOOK_CO_ROUTINES ( 4 )
\r
103 /* The number of times the tick hook should be called before a character is
\r
104 posted to the 'hook' co-routines. */
\r
105 #define hookTICK_CALLS_BEFORE_POST ( 500 )
\r
107 /* There should never be more than one item in any queue at any time. */
\r
108 #define hookHOOK_QUEUE_LENGTH ( 1 )
\r
110 /* Don't block when initially posting to the queue. */
\r
111 #define hookNO_BLOCK_TIME ( 0 )
\r
113 /* The priority relative to other co-routines (rather than tasks) that the
\r
114 'hook' co-routines should take. */
\r
115 #define mainHOOK_CR_PRIORITY ( 1 )
\r
116 /*-----------------------------------------------------------*/
\r
119 * The co-routine function itself.
\r
121 static void prvHookCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );
\r
125 * The tick hook function. This receives a number from each 'hook' co-routine
\r
126 * then sends a number to each co-routine. An error is flagged if a send or
\r
127 * receive fails, or an unexpected number is received.
\r
129 void vApplicationTickHook( void );
\r
131 /*-----------------------------------------------------------*/
\r
133 /* Queues used to send data FROM a co-routine TO the tick hook function.
\r
134 The hook functions received (Rx's) on these queues. One queue per
\r
135 'hook' co-routine. */
\r
136 static xQueueHandle xHookRxQueues[ hookNUM_HOOK_CO_ROUTINES ];
\r
138 /* Queues used to send data FROM the tick hook TO a co-routine function.
\r
139 The hood function transmits (Tx's) on these queues. One queue per
\r
140 'hook' co-routine. */
\r
141 static xQueueHandle xHookTxQueues[ hookNUM_HOOK_CO_ROUTINES ];
\r
143 /* Set to true if an error is detected at any time. */
\r
144 static portBASE_TYPE xCoRoutineErrorDetected = pdFALSE;
\r
146 /*-----------------------------------------------------------*/
\r
148 void vStartHookCoRoutines( void )
\r
150 unsigned portBASE_TYPE uxIndex, uxValueToPost = 0;
\r
152 for( uxIndex = 0; uxIndex < hookNUM_HOOK_CO_ROUTINES; uxIndex++ )
\r
154 /* Create a queue to transmit to and receive from each 'hook'
\r
156 xHookRxQueues[ uxIndex ] = xQueueCreate( hookHOOK_QUEUE_LENGTH, sizeof( unsigned portBASE_TYPE ) );
\r
157 xHookTxQueues[ uxIndex ] = xQueueCreate( hookHOOK_QUEUE_LENGTH, sizeof( unsigned portBASE_TYPE ) );
\r
159 /* To start things off the tick hook function expects the queue it
\r
160 uses to receive data to contain a value. */
\r
161 xQueueSend( xHookRxQueues[ uxIndex ], &uxValueToPost, hookNO_BLOCK_TIME );
\r
163 /* Create the 'hook' co-routine itself. */
\r
164 xCoRoutineCreate( prvHookCoRoutine, mainHOOK_CR_PRIORITY, uxIndex );
\r
167 /*-----------------------------------------------------------*/
\r
169 static unsigned portBASE_TYPE uxCallCounter = 0, uxNumberToPost = 0;
\r
170 void vApplicationTickHook( void )
\r
172 unsigned portBASE_TYPE uxReceivedNumber;
\r
173 signed portBASE_TYPE xIndex, xCoRoutineWoken;
\r
175 /* Is it time to talk to the 'hook' co-routines again? */
\r
177 if( uxCallCounter >= hookTICK_CALLS_BEFORE_POST )
\r
181 for( xIndex = 0; xIndex < hookNUM_HOOK_CO_ROUTINES; xIndex++ )
\r
183 xCoRoutineWoken = pdFALSE;
\r
184 if( crQUEUE_RECEIVE_FROM_ISR( xHookRxQueues[ xIndex ], &uxReceivedNumber, &xCoRoutineWoken ) != pdPASS )
\r
186 /* There is no reason why we would not expect the queue to
\r
187 contain a value. */
\r
188 xCoRoutineErrorDetected = pdTRUE;
\r
192 /* Each queue used to receive data from the 'hook' co-routines
\r
193 should contain the number we last posted to the same co-routine. */
\r
194 if( uxReceivedNumber != uxNumberToPost )
\r
196 xCoRoutineErrorDetected = pdTRUE;
\r
199 /* Nothing should be blocked waiting to post to the queue. */
\r
200 if( xCoRoutineWoken != pdFALSE )
\r
202 xCoRoutineErrorDetected = pdTRUE;
\r
207 /* Start the next cycle by posting the next number onto each Tx queue. */
\r
210 for( xIndex = 0; xIndex < hookNUM_HOOK_CO_ROUTINES; xIndex++ )
\r
212 if( crQUEUE_SEND_FROM_ISR( xHookTxQueues[ xIndex ], &uxNumberToPost, pdFALSE ) != pdTRUE )
\r
214 /* Posting to the queue should have woken the co-routine that
\r
215 was blocked on the queue. */
\r
216 xCoRoutineErrorDetected = pdTRUE;
\r
221 /*-----------------------------------------------------------*/
\r
223 static void prvHookCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
225 static unsigned portBASE_TYPE uxReceivedValue[ hookNUM_HOOK_CO_ROUTINES ];
\r
226 portBASE_TYPE xResult;
\r
228 /* Each co-routine MUST start with a call to crSTART(); */
\r
229 crSTART( xHandle );
\r
233 /* Wait to receive a value from the tick hook. */
\r
235 crQUEUE_RECEIVE( xHandle, xHookTxQueues[ uxIndex ], &( uxReceivedValue[ uxIndex ] ), portMAX_DELAY, &xResult );
\r
237 /* There is no reason why we should not have received something on
\r
239 if( xResult != pdPASS )
\r
241 xCoRoutineErrorDetected = pdTRUE;
\r
244 /* Send the same number back to the idle hook so it can verify it. */
\r
246 crQUEUE_SEND( xHandle, xHookRxQueues[ uxIndex ], &( uxReceivedValue[ uxIndex ] ), hookNO_BLOCK_TIME, &xResult );
\r
247 if( xResult != pdPASS )
\r
249 /* There is no reason why we should not have been able to post to
\r
251 xCoRoutineErrorDetected = pdTRUE;
\r
255 /* Each co-routine MUST end with a call to crEND(). */
\r
258 /*-----------------------------------------------------------*/
\r
260 portBASE_TYPE xAreHookCoRoutinesStillRunning( void )
\r
262 if( xCoRoutineErrorDetected )
\r