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