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