]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/QueueOverwrite.c
Update version number.
[freertos] / FreeRTOS / Demo / Common / Minimal / QueueOverwrite.c
1 /*\r
2     FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /*\r
66  * Basic task to demonstrate the xQueueOverwrite() function.  See the comments\r
67  * in the function itself.\r
68  */\r
69 \r
70 /* Scheduler include files. */\r
71 #include "FreeRTOS.h"\r
72 #include "task.h"\r
73 #include "queue.h"\r
74 \r
75 /* Demo program include files. */\r
76 #include "QueueOverwrite.h"\r
77 \r
78 /* A block time of 0 just means "don't block". */\r
79 #define qoDONT_BLOCK            0\r
80 \r
81 /* Number of times to overwrite the value in the queue. */\r
82 #define qoLOOPS                 5\r
83 \r
84 /* The task that uses the queue. */\r
85 static void prvQueueOverwriteTask( void *pvParameters );\r
86 \r
87 /* Variable that is incremented on each loop of prvQueueOverwriteTask() provided\r
88 prvQueueOverwriteTask() has not found any errors. */\r
89 static unsigned long ulLoopCounter = 0;\r
90 \r
91 /* Set to pdFALSE if an error is discovered by the\r
92 vQueueOverwritePeriodicISRDemo() function. */\r
93 static portBASE_TYPE xISRTestStatus = pdPASS;\r
94 \r
95 /* The queue that is accessed from the ISR.  The queue accessed by the task is\r
96 created inside the task itself. */\r
97 static xQueueHandle xISRQueue = NULL;\r
98 \r
99 /*-----------------------------------------------------------*/\r
100 \r
101 void vStartQueueOverwriteTask( unsigned portBASE_TYPE uxPriority )\r
102 {\r
103 const unsigned portBASE_TYPE uxQueueLength = 1;\r
104 \r
105         /* Create the queue used by the ISR.  xQueueOverwriteFromISR() should only\r
106         be used on queues that have a length of 1. */\r
107         xISRQueue = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( unsigned long ) );\r
108 \r
109         /* Create the test task.  The queue used by the test task is created inside\r
110         the task itself. */\r
111         xTaskCreate( prvQueueOverwriteTask, ( signed char * ) "QOver", configMINIMAL_STACK_SIZE, NULL, uxPriority, ( xTaskHandle * ) NULL );\r
112 }\r
113 /*-----------------------------------------------------------*/\r
114 \r
115 static void prvQueueOverwriteTask( void *pvParameters )\r
116 {\r
117 xQueueHandle xTaskQueue;\r
118 const unsigned portBASE_TYPE uxQueueLength = 1;\r
119 unsigned long ulValue, ulStatus = pdPASS, x;\r
120 \r
121         /* The parameter is not used. */\r
122         ( void ) pvParameters;\r
123 \r
124         /* Create the queue.  xQueueOverwrite() should only be used on queues that\r
125         have a length of 1. */\r
126         xTaskQueue = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( unsigned long ) );\r
127         configASSERT( xTaskQueue );\r
128 \r
129         for( ;; )\r
130         {\r
131                 /* The queue is empty.  Writing to the queue then reading from the queue\r
132                 should return the item written. */\r
133                 ulValue = 10;\r
134                 xQueueOverwrite( xTaskQueue, &ulValue );\r
135 \r
136                 ulValue = 0;\r
137                 xQueueReceive( xTaskQueue, &ulValue, qoDONT_BLOCK );\r
138 \r
139                 if( ulValue != 10 )\r
140                 {\r
141                         ulStatus = pdFAIL;\r
142                 }\r
143 \r
144                 /* Now try writing to the queue several times.  Each time the value\r
145                 in the queue should get overwritten. */\r
146                 for( x = 0; x < qoLOOPS; x++ )\r
147                 {\r
148                         /* Write to the queue. */\r
149                         xQueueOverwrite( xTaskQueue, &x );\r
150 \r
151                         /* Check the value in the queue is that written, even though the\r
152                         queue was not necessarily empty. */\r
153                         xQueuePeek( xTaskQueue, &ulValue, qoDONT_BLOCK );\r
154                         if( ulValue != x )\r
155                         {\r
156                                 ulStatus = pdFAIL;\r
157                         }\r
158 \r
159                         /* There should always be one item in the queue. */\r
160                         if( uxQueueMessagesWaiting( xTaskQueue ) != uxQueueLength )\r
161                         {\r
162                                 ulStatus = pdFAIL;\r
163                         }\r
164                 }\r
165 \r
166                 /* Empty the queue again. */\r
167                 xQueueReceive( xTaskQueue, &ulValue, qoDONT_BLOCK );\r
168 \r
169                 if( uxQueueMessagesWaiting( xTaskQueue ) != 0 )\r
170                 {\r
171                         ulStatus = pdFAIL;\r
172                 }\r
173 \r
174                 if( ulStatus != pdFAIL )\r
175                 {\r
176                         /* Increment a counter to show this task is still running without\r
177                         error. */\r
178                         ulLoopCounter++;\r
179                 }\r
180         }\r
181 }\r
182 /*-----------------------------------------------------------*/\r
183 \r
184 portBASE_TYPE xIsQueueOverwriteTaskStillRunning( void )\r
185 {\r
186 portBASE_TYPE xReturn;\r
187 \r
188         if( xISRTestStatus != pdPASS )\r
189         {\r
190                 xReturn = pdFAIL;\r
191         }\r
192         else if( ulLoopCounter > 0 )\r
193         {\r
194                 xReturn = pdPASS;\r
195         }\r
196         else\r
197         {\r
198                 /* The task has either stalled of discovered an error. */\r
199                 xReturn = pdFAIL;\r
200         }\r
201 \r
202         ulLoopCounter = 0;\r
203 \r
204         return xReturn;\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 void vQueueOverwritePeriodicISRDemo( void )\r
209 {\r
210 static unsigned long ulCallCount = 0;\r
211 const unsigned long ulTx1 = 10UL, ulTx2 = 20UL, ulNumberOfSwitchCases = 3UL;\r
212 unsigned long ulRx;\r
213 \r
214         /* This function should be called from an interrupt, such as the tick hook\r
215         function vApplicationTickHook(). */\r
216 \r
217         configASSERT( xISRQueue );\r
218 \r
219         switch( ulCallCount )\r
220         {\r
221                 case 0:\r
222                         /* The queue is empty.  Write ulTx1 to the queue.  In this demo the\r
223                         last parameter is not used because there are no tasks blocked on\r
224                         this queue. */\r
225                         xQueueOverwriteFromISR( xISRQueue, &ulTx1, NULL );\r
226 \r
227                         /* Peek the queue to check it holds the expected value. */\r
228                         xQueuePeekFromISR( xISRQueue, &ulRx );\r
229                         if( ulRx != ulTx1 )\r
230                         {\r
231                                 xISRTestStatus = pdFAIL;\r
232                         }\r
233                         break;\r
234 \r
235                 case 1:\r
236                         /* The queue already holds ulTx1.  Overwrite the value in the queue\r
237                         with ulTx2. */\r
238                         xQueueOverwriteFromISR( xISRQueue, &ulTx2, NULL );                      \r
239                         break;\r
240 \r
241                 case 2:\r
242                         /* Read from the queue to empty the queue again.  The value read\r
243                         should be ulTx2. */\r
244                         xQueueReceiveFromISR( xISRQueue, &ulRx, NULL );\r
245 \r
246                         if( ulRx != ulTx2 )\r
247                         {\r
248                                 xISRTestStatus = pdFAIL;\r
249                         }\r
250                         break;\r
251         }\r
252 \r
253         /* Run the next case in the switch statement above next time this function\r
254         is called. */\r
255         ulCallCount++;\r
256 \r
257         if( ulCallCount >= ulNumberOfSwitchCases )\r
258         {\r
259                 /* Go back to the start. */\r
260                 ulCallCount = 0;\r
261         }\r
262 }\r
263 \r