]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/crhook.c
b98124fef3956f17356e1a1265c2850ca45e5989
[freertos] / FreeRTOS / Demo / Common / Minimal / crhook.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 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     >>>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
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \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
67 */\r
68 \r
69 /*\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
73  *\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
78  * \r
79  * The tick ISR checks the numbers received back from the 'hook' co-routines \r
80  * matches the number previously sent.\r
81  *\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
84  * latched.\r
85  *\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
90  */\r
91 \r
92 /* Scheduler includes. */\r
93 #include "FreeRTOS.h"\r
94 #include "croutine.h"\r
95 #include "queue.h"\r
96 \r
97 /* Demo application includes. */\r
98 #include "crhook.h"\r
99 \r
100 /* The number of 'hook' co-routines that are to be created. */\r
101 #define hookNUM_HOOK_CO_ROUTINES        ( 4 )\r
102 \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
106 \r
107 /* There should never be more than one item in any queue at any time. */\r
108 #define hookHOOK_QUEUE_LENGTH           ( 1 )\r
109 \r
110 /* Don't block when initially posting to the queue. */\r
111 #define hookNO_BLOCK_TIME               ( 0 )\r
112 \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
117 \r
118 /*\r
119  * The co-routine function itself.\r
120  */\r
121 static void prvHookCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex );\r
122 \r
123 \r
124 /*\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
128  */\r
129 void vApplicationTickHook( void );\r
130 \r
131 /*-----------------------------------------------------------*/\r
132 \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
137 \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
142 \r
143 /* Set to true if an error is detected at any time. */\r
144 static portBASE_TYPE xCoRoutineErrorDetected = pdFALSE;\r
145 \r
146 /*-----------------------------------------------------------*/\r
147 \r
148 void vStartHookCoRoutines( void )\r
149 {\r
150 unsigned portBASE_TYPE uxIndex, uxValueToPost = 0;\r
151 \r
152         for( uxIndex = 0; uxIndex < hookNUM_HOOK_CO_ROUTINES; uxIndex++ )\r
153         {\r
154                 /* Create a queue to transmit to and receive from each 'hook' \r
155                 co-routine. */\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
158 \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
162 \r
163                 /* Create the 'hook' co-routine itself. */\r
164                 xCoRoutineCreate( prvHookCoRoutine, mainHOOK_CR_PRIORITY, uxIndex );\r
165         }\r
166 }\r
167 /*-----------------------------------------------------------*/\r
168 \r
169 static unsigned portBASE_TYPE uxCallCounter = 0, uxNumberToPost = 0;\r
170 void vApplicationTickHook( void )\r
171 {\r
172 unsigned portBASE_TYPE uxReceivedNumber;\r
173 signed portBASE_TYPE xIndex, xCoRoutineWoken;\r
174 \r
175         /* Is it time to talk to the 'hook' co-routines again? */\r
176         uxCallCounter++;\r
177         if( uxCallCounter >= hookTICK_CALLS_BEFORE_POST )\r
178         {\r
179                 uxCallCounter = 0;\r
180 \r
181                 for( xIndex = 0; xIndex < hookNUM_HOOK_CO_ROUTINES; xIndex++ )\r
182                 {\r
183                         xCoRoutineWoken = pdFALSE;\r
184                         if( crQUEUE_RECEIVE_FROM_ISR( xHookRxQueues[ xIndex ], &uxReceivedNumber, &xCoRoutineWoken ) != pdPASS )\r
185                         {\r
186                                 /* There is no reason why we would not expect the queue to \r
187                                 contain a value. */\r
188                                 xCoRoutineErrorDetected = pdTRUE;\r
189                         }\r
190                         else\r
191                         {\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
195                                 {\r
196                                         xCoRoutineErrorDetected = pdTRUE;\r
197                                 }\r
198 \r
199                                 /* Nothing should be blocked waiting to post to the queue. */\r
200                                 if( xCoRoutineWoken != pdFALSE )\r
201                                 {\r
202                                         xCoRoutineErrorDetected = pdTRUE;\r
203                                 }\r
204                         }\r
205                 }\r
206 \r
207                 /* Start the next cycle by posting the next number onto each Tx queue. */\r
208                 uxNumberToPost++;\r
209 \r
210                 for( xIndex = 0; xIndex < hookNUM_HOOK_CO_ROUTINES; xIndex++ )\r
211                 {\r
212                         if( crQUEUE_SEND_FROM_ISR( xHookTxQueues[ xIndex ], &uxNumberToPost, pdFALSE ) != pdTRUE )\r
213                         {\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
217                         }\r
218                 }\r
219         }\r
220 }\r
221 /*-----------------------------------------------------------*/\r
222 \r
223 static void prvHookCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
224 {\r
225 static unsigned portBASE_TYPE uxReceivedValue[ hookNUM_HOOK_CO_ROUTINES ];\r
226 portBASE_TYPE xResult;\r
227 \r
228         /* Each co-routine MUST start with a call to crSTART(); */\r
229         crSTART( xHandle );\r
230 \r
231         for( ;; )\r
232         {\r
233                 /* Wait to receive a value from the tick hook. */\r
234                 xResult = pdFAIL;\r
235                 crQUEUE_RECEIVE( xHandle, xHookTxQueues[ uxIndex ], &( uxReceivedValue[ uxIndex ] ), portMAX_DELAY, &xResult );\r
236 \r
237                 /* There is no reason why we should not have received something on\r
238                 the queue. */\r
239                 if( xResult != pdPASS )\r
240                 {\r
241                         xCoRoutineErrorDetected = pdTRUE;\r
242                 }\r
243 \r
244                 /* Send the same number back to the idle hook so it can verify it. */\r
245                 xResult = pdFAIL;\r
246                 crQUEUE_SEND( xHandle, xHookRxQueues[ uxIndex ], &( uxReceivedValue[ uxIndex ] ), hookNO_BLOCK_TIME, &xResult );\r
247                 if( xResult != pdPASS )\r
248                 {\r
249                         /* There is no reason why we should not have been able to post to \r
250                         the queue. */\r
251                         xCoRoutineErrorDetected = pdTRUE;\r
252                 }\r
253         }\r
254 \r
255         /* Each co-routine MUST end with a call to crEND(). */\r
256         crEND();\r
257 }\r
258 /*-----------------------------------------------------------*/\r
259 \r
260 portBASE_TYPE xAreHookCoRoutinesStillRunning( void )\r
261 {\r
262         if( xCoRoutineErrorDetected )\r
263         {\r
264                 return pdFALSE;\r
265         }\r
266         else\r
267         {\r
268                 return pdTRUE;\r
269         }\r
270 }\r
271 \r
272 \r
273 \r