]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/QueueOverwrite.c
***IMMINENT RELEASE NOTICE***
[freertos] / FreeRTOS / Demo / Common / Minimal / QueueOverwrite.c
1 /*\r
2     FreeRTOS V8.1.0 - 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  * Basic task to demonstrate the xQueueOverwrite() function.  See the comments\r
68  * in the function itself.\r
69  */\r
70 \r
71 /* Scheduler include files. */\r
72 #include "FreeRTOS.h"\r
73 #include "task.h"\r
74 #include "queue.h"\r
75 \r
76 /* Demo program include files. */\r
77 #include "QueueOverwrite.h"\r
78 \r
79 /* A block time of 0 just means "don't block". */\r
80 #define qoDONT_BLOCK            0\r
81 \r
82 /* Number of times to overwrite the value in the queue. */\r
83 #define qoLOOPS                 5\r
84 \r
85 /* The task that uses the queue. */\r
86 static void prvQueueOverwriteTask( void *pvParameters );\r
87 \r
88 /* Variable that is incremented on each loop of prvQueueOverwriteTask() provided\r
89 prvQueueOverwriteTask() has not found any errors. */\r
90 static uint32_t ulLoopCounter = 0;\r
91 \r
92 /* Set to pdFALSE if an error is discovered by the\r
93 vQueueOverwritePeriodicISRDemo() function. */\r
94 static BaseType_t xISRTestStatus = pdPASS;\r
95 \r
96 /* The queue that is accessed from the ISR.  The queue accessed by the task is\r
97 created inside the task itself. */\r
98 static QueueHandle_t xISRQueue = NULL;\r
99 \r
100 /*-----------------------------------------------------------*/\r
101 \r
102 void vStartQueueOverwriteTask( UBaseType_t uxPriority )\r
103 {\r
104 const UBaseType_t uxQueueLength = 1;\r
105 \r
106         /* Create the queue used by the ISR.  xQueueOverwriteFromISR() should only\r
107         be used on queues that have a length of 1. */\r
108         xISRQueue = xQueueCreate( uxQueueLength, ( UBaseType_t ) sizeof( uint32_t ) );\r
109 \r
110         /* Create the test task.  The queue used by the test task is created inside\r
111         the task itself. */\r
112         xTaskCreate( prvQueueOverwriteTask, "QOver", configMINIMAL_STACK_SIZE, NULL, uxPriority, ( TaskHandle_t * ) NULL );\r
113 }\r
114 /*-----------------------------------------------------------*/\r
115 \r
116 static void prvQueueOverwriteTask( void *pvParameters )\r
117 {\r
118 QueueHandle_t xTaskQueue;\r
119 const UBaseType_t uxQueueLength = 1;\r
120 uint32_t ulValue, ulStatus = pdPASS, x;\r
121 \r
122         /* The parameter is not used. */\r
123         ( void ) pvParameters;\r
124 \r
125         /* Create the queue.  xQueueOverwrite() should only be used on queues that\r
126         have a length of 1. */\r
127         xTaskQueue = xQueueCreate( uxQueueLength, ( UBaseType_t ) sizeof( uint32_t ) );\r
128         configASSERT( xTaskQueue );\r
129 \r
130         for( ;; )\r
131         {\r
132                 /* The queue is empty.  Writing to the queue then reading from the queue\r
133                 should return the item written. */\r
134                 ulValue = 10;\r
135                 xQueueOverwrite( xTaskQueue, &ulValue );\r
136 \r
137                 ulValue = 0;\r
138                 xQueueReceive( xTaskQueue, &ulValue, qoDONT_BLOCK );\r
139 \r
140                 if( ulValue != 10 )\r
141                 {\r
142                         ulStatus = pdFAIL;\r
143                 }\r
144 \r
145                 /* Now try writing to the queue several times.  Each time the value\r
146                 in the queue should get overwritten. */\r
147                 for( x = 0; x < qoLOOPS; x++ )\r
148                 {\r
149                         /* Write to the queue. */\r
150                         xQueueOverwrite( xTaskQueue, &x );\r
151 \r
152                         /* Check the value in the queue is that written, even though the\r
153                         queue was not necessarily empty. */\r
154                         xQueuePeek( xTaskQueue, &ulValue, qoDONT_BLOCK );\r
155                         if( ulValue != x )\r
156                         {\r
157                                 ulStatus = pdFAIL;\r
158                         }\r
159 \r
160                         /* There should always be one item in the queue. */\r
161                         if( uxQueueMessagesWaiting( xTaskQueue ) != uxQueueLength )\r
162                         {\r
163                                 ulStatus = pdFAIL;\r
164                         }\r
165                 }\r
166 \r
167                 /* Empty the queue again. */\r
168                 xQueueReceive( xTaskQueue, &ulValue, qoDONT_BLOCK );\r
169 \r
170                 if( uxQueueMessagesWaiting( xTaskQueue ) != 0 )\r
171                 {\r
172                         ulStatus = pdFAIL;\r
173                 }\r
174 \r
175                 if( ulStatus != pdFAIL )\r
176                 {\r
177                         /* Increment a counter to show this task is still running without\r
178                         error. */\r
179                         ulLoopCounter++;\r
180                 }\r
181 \r
182                 #if( configUSE_PREEMPTION == 0 )\r
183                         taskYIELD();\r
184                 #endif\r
185         }\r
186 }\r
187 /*-----------------------------------------------------------*/\r
188 \r
189 BaseType_t xIsQueueOverwriteTaskStillRunning( void )\r
190 {\r
191 BaseType_t xReturn;\r
192 \r
193         if( xISRTestStatus != pdPASS )\r
194         {\r
195                 xReturn = pdFAIL;\r
196         }\r
197         else if( ulLoopCounter > 0 )\r
198         {\r
199                 xReturn = pdPASS;\r
200         }\r
201         else\r
202         {\r
203                 /* The task has either stalled of discovered an error. */\r
204                 xReturn = pdFAIL;\r
205         }\r
206 \r
207         ulLoopCounter = 0;\r
208 \r
209         return xReturn;\r
210 }\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 void vQueueOverwritePeriodicISRDemo( void )\r
214 {\r
215 static uint32_t ulCallCount = 0;\r
216 const uint32_t ulTx1 = 10UL, ulTx2 = 20UL, ulNumberOfSwitchCases = 3UL;\r
217 uint32_t ulRx;\r
218 \r
219         /* This function should be called from an interrupt, such as the tick hook\r
220         function vApplicationTickHook(). */\r
221 \r
222         configASSERT( xISRQueue );\r
223 \r
224         switch( ulCallCount )\r
225         {\r
226                 case 0:\r
227                         /* The queue is empty.  Write ulTx1 to the queue.  In this demo the\r
228                         last parameter is not used because there are no tasks blocked on\r
229                         this queue. */\r
230                         xQueueOverwriteFromISR( xISRQueue, &ulTx1, NULL );\r
231 \r
232                         /* Peek the queue to check it holds the expected value. */\r
233                         xQueuePeekFromISR( xISRQueue, &ulRx );\r
234                         if( ulRx != ulTx1 )\r
235                         {\r
236                                 xISRTestStatus = pdFAIL;\r
237                         }\r
238                         break;\r
239 \r
240                 case 1:\r
241                         /* The queue already holds ulTx1.  Overwrite the value in the queue\r
242                         with ulTx2. */\r
243                         xQueueOverwriteFromISR( xISRQueue, &ulTx2, NULL );\r
244                         break;\r
245 \r
246                 case 2:\r
247                         /* Read from the queue to empty the queue again.  The value read\r
248                         should be ulTx2. */\r
249                         xQueueReceiveFromISR( xISRQueue, &ulRx, NULL );\r
250 \r
251                         if( ulRx != ulTx2 )\r
252                         {\r
253                                 xISRTestStatus = pdFAIL;\r
254                         }\r
255                         break;\r
256         }\r
257 \r
258         /* Run the next case in the switch statement above next time this function\r
259         is called. */\r
260         ulCallCount++;\r
261 \r
262         if( ulCallCount >= ulNumberOfSwitchCases )\r
263         {\r
264                 /* Go back to the start. */\r
265                 ulCallCount = 0;\r
266         }\r
267 }\r
268 \r