2 * FreeRTOS Kernel V10.3.1
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
29 * Basic task to demonstrate the xQueueOverwrite() function. See the comments
30 * in the function itself.
33 /* Scheduler include files. */
38 /* Demo program include files. */
39 #include "QueueOverwrite.h"
41 /* A block time of 0 just means "don't block". */
42 #define qoDONT_BLOCK 0
44 /* Number of times to overwrite the value in the queue. */
47 /* The task that uses the queue. */
48 static void prvQueueOverwriteTask( void *pvParameters );
50 /* Variable that is incremented on each loop of prvQueueOverwriteTask() provided
51 prvQueueOverwriteTask() has not found any errors. */
52 static uint32_t ulLoopCounter = 0;
54 /* Set to pdFALSE if an error is discovered by the
55 vQueueOverwritePeriodicISRDemo() function. */
56 static BaseType_t xISRTestStatus = pdPASS;
58 /* The queue that is accessed from the ISR. The queue accessed by the task is
59 created inside the task itself. */
60 static QueueHandle_t xISRQueue = NULL;
62 /*-----------------------------------------------------------*/
64 void vStartQueueOverwriteTask( UBaseType_t uxPriority )
66 const UBaseType_t uxQueueLength = 1;
68 /* Create the queue used by the ISR. xQueueOverwriteFromISR() should only
69 be used on queues that have a length of 1. */
70 xISRQueue = xQueueCreate( uxQueueLength, ( UBaseType_t ) sizeof( uint32_t ) );
72 /* Create the test task. The queue used by the test task is created inside
74 xTaskCreate( prvQueueOverwriteTask, "QOver", configMINIMAL_STACK_SIZE, NULL, uxPriority, ( TaskHandle_t * ) NULL );
76 /*-----------------------------------------------------------*/
78 static void prvQueueOverwriteTask( void *pvParameters )
80 QueueHandle_t xTaskQueue;
81 const UBaseType_t uxQueueLength = 1;
82 uint32_t ulValue, ulStatus = pdPASS, x;
84 /* The parameter is not used. */
85 ( void ) pvParameters;
87 /* Create the queue. xQueueOverwrite() should only be used on queues that
88 have a length of 1. */
89 xTaskQueue = xQueueCreate( uxQueueLength, ( UBaseType_t ) sizeof( uint32_t ) );
90 configASSERT( xTaskQueue );
94 /* The queue is empty. Writing to the queue then reading from the queue
95 should return the item written. */
97 xQueueOverwrite( xTaskQueue, &ulValue );
100 xQueueReceive( xTaskQueue, &ulValue, qoDONT_BLOCK );
107 /* Now try writing to the queue several times. Each time the value
108 in the queue should get overwritten. */
109 for( x = 0; x < qoLOOPS; x++ )
111 /* Write to the queue. */
112 xQueueOverwrite( xTaskQueue, &x );
114 /* Check the value in the queue is that written, even though the
115 queue was not necessarily empty. */
116 xQueuePeek( xTaskQueue, &ulValue, qoDONT_BLOCK );
122 /* There should always be one item in the queue. */
123 if( uxQueueMessagesWaiting( xTaskQueue ) != uxQueueLength )
129 /* Empty the queue again. */
130 xQueueReceive( xTaskQueue, &ulValue, qoDONT_BLOCK );
132 if( uxQueueMessagesWaiting( xTaskQueue ) != 0 )
137 if( ulStatus != pdFAIL )
139 /* Increment a counter to show this task is still running without
144 #if( configUSE_PREEMPTION == 0 )
149 /*-----------------------------------------------------------*/
151 BaseType_t xIsQueueOverwriteTaskStillRunning( void )
155 if( xISRTestStatus != pdPASS )
159 else if( ulLoopCounter > 0 )
165 /* The task has either stalled of discovered an error. */
173 /*-----------------------------------------------------------*/
175 void vQueueOverwritePeriodicISRDemo( void )
177 static uint32_t ulCallCount = 0;
178 const uint32_t ulTx1 = 10UL, ulTx2 = 20UL, ulNumberOfSwitchCases = 3UL;
181 /* This function should be called from an interrupt, such as the tick hook
182 function vApplicationTickHook(). */
184 configASSERT( xISRQueue );
186 switch( ulCallCount )
189 /* The queue is empty. Write ulTx1 to the queue. In this demo the
190 last parameter is not used because there are no tasks blocked on
192 xQueueOverwriteFromISR( xISRQueue, &ulTx1, NULL );
194 /* Peek the queue to check it holds the expected value. */
195 xQueuePeekFromISR( xISRQueue, &ulRx );
198 xISRTestStatus = pdFAIL;
203 /* The queue already holds ulTx1. Overwrite the value in the queue
205 xQueueOverwriteFromISR( xISRQueue, &ulTx2, NULL );
209 /* Read from the queue to empty the queue again. The value read
211 xQueueReceiveFromISR( xISRQueue, &ulRx, NULL );
215 xISRTestStatus = pdFAIL;
220 /* Run the next case in the switch statement above next time this function
224 if( ulCallCount >= ulNumberOfSwitchCases )
226 /* Go back to the start. */