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