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