]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/crhook.c
Change version number in common files within the FreeRTOS-plus directory and check...
[freertos] / FreeRTOS / Demo / Common / Minimal / crhook.c
1 /*\r
2     FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \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
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\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
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \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 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not itcan be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*\r
76  * This demo file demonstrates how to send data between an ISR and a \r
77  * co-routine.  A tick hook function is used to periodically pass data between\r
78  * the RTOS tick and a set of 'hook' co-routines.\r
79  *\r
80  * hookNUM_HOOK_CO_ROUTINES co-routines are created.  Each co-routine blocks\r
81  * to wait for a character to be received on a queue from the tick ISR, checks\r
82  * to ensure the character received was that expected, then sends the number\r
83  * back to the tick ISR on a different queue.\r
84  * \r
85  * The tick ISR checks the numbers received back from the 'hook' co-routines \r
86  * matches the number previously sent.\r
87  *\r
88  * If at any time a queue function returns unexpectedly, or an incorrect value\r
89  * is received either by the tick hook or a co-routine then an error is \r
90  * latched.\r
91  *\r
92  * This demo relies on each 'hook' co-routine to execute between each \r
93  * hookTICK_CALLS_BEFORE_POST tick interrupts.  This and the heavy use of \r
94  * queues from within an interrupt may result in an error being detected on \r
95  * slower targets simply due to timing.\r
96  */\r
97 \r
98 /* Scheduler includes. */\r
99 #include "FreeRTOS.h"\r
100 #include "croutine.h"\r
101 #include "queue.h"\r
102 \r
103 /* Demo application includes. */\r
104 #include "crhook.h"\r
105 \r
106 /* The number of 'hook' co-routines that are to be created. */\r
107 #define hookNUM_HOOK_CO_ROUTINES        ( 4 )\r
108 \r
109 /* The number of times the tick hook should be called before a character is \r
110 posted to the 'hook' co-routines. */\r
111 #define hookTICK_CALLS_BEFORE_POST      ( 500 )\r
112 \r
113 /* There should never be more than one item in any queue at any time. */\r
114 #define hookHOOK_QUEUE_LENGTH           ( 1 )\r
115 \r
116 /* Don't block when initially posting to the queue. */\r
117 #define hookNO_BLOCK_TIME               ( 0 )\r
118 \r
119 /* The priority relative to other co-routines (rather than tasks) that the\r
120 'hook' co-routines should take. */\r
121 #define mainHOOK_CR_PRIORITY                    ( 1 )\r
122 /*-----------------------------------------------------------*/\r
123 \r
124 /*\r
125  * The co-routine function itself.\r
126  */\r
127 static void prvHookCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );\r
128 \r
129 \r
130 /*\r
131  * The tick hook function.  This receives a number from each 'hook' co-routine\r
132  * then sends a number to each co-routine.  An error is flagged if a send or \r
133  * receive fails, or an unexpected number is received.\r
134  */\r
135 void vApplicationTickHook( void );\r
136 \r
137 /*-----------------------------------------------------------*/\r
138 \r
139 /* Queues used to send data FROM a co-routine TO the tick hook function.\r
140 The hook functions received (Rx's) on these queues.  One queue per\r
141 'hook' co-routine. */\r
142 static xQueueHandle xHookRxQueues[ hookNUM_HOOK_CO_ROUTINES ];\r
143 \r
144 /* Queues used to send data FROM the tick hook TO a co-routine function.\r
145 The hood function transmits (Tx's) on these queues.  One queue per\r
146 'hook' co-routine. */\r
147 static xQueueHandle xHookTxQueues[ hookNUM_HOOK_CO_ROUTINES ];\r
148 \r
149 /* Set to true if an error is detected at any time. */\r
150 static portBASE_TYPE xCoRoutineErrorDetected = pdFALSE;\r
151 \r
152 /*-----------------------------------------------------------*/\r
153 \r
154 void vStartHookCoRoutines( void )\r
155 {\r
156 unsigned portBASE_TYPE uxIndex, uxValueToPost = 0;\r
157 \r
158         for( uxIndex = 0; uxIndex < hookNUM_HOOK_CO_ROUTINES; uxIndex++ )\r
159         {\r
160                 /* Create a queue to transmit to and receive from each 'hook' \r
161                 co-routine. */\r
162                 xHookRxQueues[ uxIndex ] = xQueueCreate( hookHOOK_QUEUE_LENGTH, sizeof( unsigned portBASE_TYPE ) );\r
163                 xHookTxQueues[ uxIndex ] = xQueueCreate( hookHOOK_QUEUE_LENGTH, sizeof( unsigned portBASE_TYPE ) );\r
164 \r
165                 /* To start things off the tick hook function expects the queue it \r
166                 uses to receive data to contain a value.  */\r
167                 xQueueSend( xHookRxQueues[ uxIndex ], &uxValueToPost, hookNO_BLOCK_TIME );\r
168 \r
169                 /* Create the 'hook' co-routine itself. */\r
170                 xCoRoutineCreate( prvHookCoRoutine, mainHOOK_CR_PRIORITY, uxIndex );\r
171         }\r
172 }\r
173 /*-----------------------------------------------------------*/\r
174 \r
175 static unsigned portBASE_TYPE uxCallCounter = 0, uxNumberToPost = 0;\r
176 void vApplicationTickHook( void )\r
177 {\r
178 unsigned portBASE_TYPE uxReceivedNumber;\r
179 signed portBASE_TYPE xIndex, xCoRoutineWoken;\r
180 \r
181         /* Is it time to talk to the 'hook' co-routines again? */\r
182         uxCallCounter++;\r
183         if( uxCallCounter >= hookTICK_CALLS_BEFORE_POST )\r
184         {\r
185                 uxCallCounter = 0;\r
186 \r
187                 for( xIndex = 0; xIndex < hookNUM_HOOK_CO_ROUTINES; xIndex++ )\r
188                 {\r
189                         xCoRoutineWoken = pdFALSE;\r
190                         if( crQUEUE_RECEIVE_FROM_ISR( xHookRxQueues[ xIndex ], &uxReceivedNumber, &xCoRoutineWoken ) != pdPASS )\r
191                         {\r
192                                 /* There is no reason why we would not expect the queue to \r
193                                 contain a value. */\r
194                                 xCoRoutineErrorDetected = pdTRUE;\r
195                         }\r
196                         else\r
197                         {\r
198                                 /* Each queue used to receive data from the 'hook' co-routines \r
199                                 should contain the number we last posted to the same co-routine. */\r
200                                 if( uxReceivedNumber != uxNumberToPost )\r
201                                 {\r
202                                         xCoRoutineErrorDetected = pdTRUE;\r
203                                 }\r
204 \r
205                                 /* Nothing should be blocked waiting to post to the queue. */\r
206                                 if( xCoRoutineWoken != pdFALSE )\r
207                                 {\r
208                                         xCoRoutineErrorDetected = pdTRUE;\r
209                                 }\r
210                         }\r
211                 }\r
212 \r
213                 /* Start the next cycle by posting the next number onto each Tx queue. */\r
214                 uxNumberToPost++;\r
215 \r
216                 for( xIndex = 0; xIndex < hookNUM_HOOK_CO_ROUTINES; xIndex++ )\r
217                 {\r
218                         if( crQUEUE_SEND_FROM_ISR( xHookTxQueues[ xIndex ], &uxNumberToPost, pdFALSE ) != pdTRUE )\r
219                         {\r
220                                 /* Posting to the queue should have woken the co-routine that \r
221                                 was blocked on the queue. */\r
222                                 xCoRoutineErrorDetected = pdTRUE;\r
223                         }\r
224                 }\r
225         }\r
226 }\r
227 /*-----------------------------------------------------------*/\r
228 \r
229 static void prvHookCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
230 {\r
231 static unsigned portBASE_TYPE uxReceivedValue[ hookNUM_HOOK_CO_ROUTINES ];\r
232 portBASE_TYPE xResult;\r
233 \r
234         /* Each co-routine MUST start with a call to crSTART(); */\r
235         crSTART( xHandle );\r
236 \r
237         for( ;; )\r
238         {\r
239                 /* Wait to receive a value from the tick hook. */\r
240                 xResult = pdFAIL;\r
241                 crQUEUE_RECEIVE( xHandle, xHookTxQueues[ uxIndex ], &( uxReceivedValue[ uxIndex ] ), portMAX_DELAY, &xResult );\r
242 \r
243                 /* There is no reason why we should not have received something on\r
244                 the queue. */\r
245                 if( xResult != pdPASS )\r
246                 {\r
247                         xCoRoutineErrorDetected = pdTRUE;\r
248                 }\r
249 \r
250                 /* Send the same number back to the idle hook so it can verify it. */\r
251                 xResult = pdFAIL;\r
252                 crQUEUE_SEND( xHandle, xHookRxQueues[ uxIndex ], &( uxReceivedValue[ uxIndex ] ), hookNO_BLOCK_TIME, &xResult );\r
253                 if( xResult != pdPASS )\r
254                 {\r
255                         /* There is no reason why we should not have been able to post to \r
256                         the queue. */\r
257                         xCoRoutineErrorDetected = pdTRUE;\r
258                 }\r
259         }\r
260 \r
261         /* Each co-routine MUST end with a call to crEND(). */\r
262         crEND();\r
263 }\r
264 /*-----------------------------------------------------------*/\r
265 \r
266 portBASE_TYPE xAreHookCoRoutinesStillRunning( void )\r
267 {\r
268         if( xCoRoutineErrorDetected )\r
269         {\r
270                 return pdFALSE;\r
271         }\r
272         else\r
273         {\r
274                 return pdTRUE;\r
275         }\r
276 }\r
277 \r
278 \r
279 \r