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