]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/QueueOverwrite.c
Update FreeRTOS version number to V7.5.3
[freertos] / FreeRTOS / Demo / Common / Minimal / QueueOverwrite.c
1 /*\r
2     FreeRTOS V7.5.3 - Copyright (C) 2013 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 distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! 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 unsigned long ulLoopCounter = 0;\r
91 \r
92 /* Set to pdFALSE if an error is discovered by the\r
93 vQueueOverwritePeriodicISRDemo() function. */\r
94 static portBASE_TYPE 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 xQueueHandle xISRQueue = NULL;\r
99 \r
100 /*-----------------------------------------------------------*/\r
101 \r
102 void vStartQueueOverwriteTask( unsigned portBASE_TYPE uxPriority )\r
103 {\r
104 const unsigned portBASE_TYPE 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, ( unsigned portBASE_TYPE ) sizeof( unsigned long ) );\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, ( signed char * ) "QOver", configMINIMAL_STACK_SIZE, NULL, uxPriority, ( xTaskHandle * ) NULL );\r
113 }\r
114 /*-----------------------------------------------------------*/\r
115 \r
116 static void prvQueueOverwriteTask( void *pvParameters )\r
117 {\r
118 xQueueHandle xTaskQueue;\r
119 const unsigned portBASE_TYPE uxQueueLength = 1;\r
120 unsigned long 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, ( unsigned portBASE_TYPE ) sizeof( unsigned long ) );\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 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 portBASE_TYPE xIsQueueOverwriteTaskStillRunning( void )\r
186 {\r
187 portBASE_TYPE xReturn;\r
188 \r
189         if( xISRTestStatus != pdPASS )\r
190         {\r
191                 xReturn = pdFAIL;\r
192         }\r
193         else if( ulLoopCounter > 0 )\r
194         {\r
195                 xReturn = pdPASS;\r
196         }\r
197         else\r
198         {\r
199                 /* The task has either stalled of discovered an error. */\r
200                 xReturn = pdFAIL;\r
201         }\r
202 \r
203         ulLoopCounter = 0;\r
204 \r
205         return xReturn;\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 void vQueueOverwritePeriodicISRDemo( void )\r
210 {\r
211 static unsigned long ulCallCount = 0;\r
212 const unsigned long ulTx1 = 10UL, ulTx2 = 20UL, ulNumberOfSwitchCases = 3UL;\r
213 unsigned long ulRx;\r
214 \r
215         /* This function should be called from an interrupt, such as the tick hook\r
216         function vApplicationTickHook(). */\r
217 \r
218         configASSERT( xISRQueue );\r
219 \r
220         switch( ulCallCount )\r
221         {\r
222                 case 0:\r
223                         /* The queue is empty.  Write ulTx1 to the queue.  In this demo the\r
224                         last parameter is not used because there are no tasks blocked on\r
225                         this queue. */\r
226                         xQueueOverwriteFromISR( xISRQueue, &ulTx1, NULL );\r
227 \r
228                         /* Peek the queue to check it holds the expected value. */\r
229                         xQueuePeekFromISR( xISRQueue, &ulRx );\r
230                         if( ulRx != ulTx1 )\r
231                         {\r
232                                 xISRTestStatus = pdFAIL;\r
233                         }\r
234                         break;\r
235 \r
236                 case 1:\r
237                         /* The queue already holds ulTx1.  Overwrite the value in the queue\r
238                         with ulTx2. */\r
239                         xQueueOverwriteFromISR( xISRQueue, &ulTx2, NULL );                      \r
240                         break;\r
241 \r
242                 case 2:\r
243                         /* Read from the queue to empty the queue again.  The value read\r
244                         should be ulTx2. */\r
245                         xQueueReceiveFromISR( xISRQueue, &ulRx, NULL );\r
246 \r
247                         if( ulRx != ulTx2 )\r
248                         {\r
249                                 xISRTestStatus = pdFAIL;\r
250                         }\r
251                         break;\r
252         }\r
253 \r
254         /* Run the next case in the switch statement above next time this function\r
255         is called. */\r
256         ulCallCount++;\r
257 \r
258         if( ulCallCount >= ulNumberOfSwitchCases )\r
259         {\r
260                 /* Go back to the start. */\r
261                 ulCallCount = 0;\r
262         }\r
263 }\r
264 \r