2 * FreeRTOS Kernel V10.3.0
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 * Contains sundry tests to exercise code that is not touched by the standard
30 * demo tasks (which are predominantly test tasks). Some tests are incldued
31 * here because they can only be executed when configASSERT() is not defined.
39 #include "event_groups.h"
41 #include "stream_buffer.h"
42 #include "message_buffer.h"
44 /*-----------------------------------------------------------*/
47 * Try creating static objects with one of the mandatory parameters set to NULL.
48 * This can't be done in the standard demos as asserts() will get hit.
50 static BaseType_t prvStaticAllocationsWithNullBuffers( void );
53 * Code coverage analysis is performed with tracing turned off, so this
54 * function executes the trace specific utility functions that would not
55 * otherwise be executed..
57 static BaseType_t prvTraceUtils( void );
60 * The queue peek standard demo does not cover the case where an attempt to peek
61 * times out, so test that case.
63 static BaseType_t prvPeekTimeout( void );
66 * Calls various interrupt safe functions designed to query the state of a
69 static BaseType_t prvQueueQueryFromISR( void );
72 * Hits a few paths in tasks state and status query functions not otherwise hit
73 * by standard demo and test files.
75 static BaseType_t prvTaskQueryFunctions( void );
78 * None of the standard demo tasks use the task tags - exercise them here.
80 static BaseType_t prvTaskTags( void );
83 * Exercises a few of the query functions that are not otherwise exercised in
84 * the standard demo and test functions.
86 static BaseType_t prvTimerQuery( void );
88 /*-----------------------------------------------------------*/
90 static BaseType_t prvStaticAllocationsWithNullBuffers( void )
92 uintptr_t ulReturned = 0;
93 BaseType_t xReturn = pdPASS;
94 UBaseType_t uxDummy = 10;
96 /* Don't expect to create any of the objects as a NULL parameter is always
97 passed in place of a required buffer. Hence if all passes then none of the
98 |= will be against 0, and ulReturned will still be zero at the end of this
100 ulReturned |= ( uintptr_t ) xEventGroupCreateStatic( NULL );
102 /* Try creating a task twice, once with puxStackBuffer NULL, and once with
103 pxTaskBuffer NULL. */
104 ulReturned |= ( uintptr_t ) xTaskCreateStatic( NULL, /* Task to run, not needed as the task is not created. */
105 "Dummy", /* Task name. */
106 configMINIMAL_STACK_SIZE,
110 ( StaticTask_t * ) &xReturn ); /* Dummy value just to pass a non NULL value in - won't get used. */
112 ulReturned |= ( uintptr_t ) xTaskCreateStatic( NULL, /* Task to run, not needed as the task is not created. */
113 "Dummy", /* Task name. */
114 configMINIMAL_STACK_SIZE,
117 ( StackType_t * ) &xReturn, /* Dummy value just to pass a non NULL value in - won't get used. */
120 ulReturned |= ( uintptr_t ) xQueueCreateStatic( uxDummy,
122 ( uint8_t * ) &xReturn, /* Dummy value just to pass a non NULL value in - won't get used. */
125 /* Try creating a stream buffer twice, once with pucStreamBufferStorageArea
126 set to NULL, and once with pxStaticStreamBuffer set to NULL. */
127 ulReturned |= ( uintptr_t ) xStreamBufferCreateStatic( uxDummy,
130 ( StaticStreamBuffer_t * ) &xReturn ); /* Dummy value just to pass a non NULL value in - won't get used. */
132 ulReturned |= ( uintptr_t ) xStreamBufferCreateStatic( uxDummy,
134 ( uint8_t * ) &xReturn, /* Dummy value just to pass a non NULL value in - won't get used. */
137 if( ulReturned != 0 )
139 /* Something returned a non-NULL value. */
145 /*-----------------------------------------------------------*/
147 static BaseType_t prvTraceUtils( void )
149 EventGroupHandle_t xEventGroup;
150 QueueHandle_t xQueue;
151 BaseType_t xReturn = pdPASS;
152 const UBaseType_t xNumber = ( UBaseType_t ) 100, xQueueLength = ( UBaseType_t ) 1;
154 TaskHandle_t xTaskHandle;
155 StreamBufferHandle_t xStreamBuffer;
156 MessageBufferHandle_t xMessageBuffer;
158 /* Exercise the event group trace utilities. */
159 xEventGroup = xEventGroupCreate();
161 if( xEventGroup != NULL )
163 vEventGroupSetNumber( xEventGroup, xNumber );
164 if( uxEventGroupGetNumber( NULL ) != 0 )
168 if( uxEventGroupGetNumber( xEventGroup ) != xNumber )
173 vEventGroupDelete( xEventGroup );
180 /* Exercise the queue trace utilities. */
181 xQueue = xQueueCreate( xQueueLength, ( UBaseType_t ) sizeof( uxValue ) );
184 vQueueSetQueueNumber( xQueue, xNumber );
185 if( uxQueueGetQueueNumber( xQueue ) != xNumber )
189 if( ucQueueGetQueueType( xQueue ) != queueQUEUE_TYPE_BASE )
194 vQueueDelete( xQueue );
201 /* Exercise the task trace utilities. Value of 100 is arbitrary, just want
202 to check the value that is set is also read back. */
204 xTaskHandle = xTaskGetCurrentTaskHandle();
205 vTaskSetTaskNumber( xTaskHandle, uxValue );
206 if( uxTaskGetTaskNumber( xTaskHandle ) != uxValue )
210 if( uxTaskGetTaskNumber( NULL ) != 0 )
215 /* Timer trace util functions are exercised in prvTimerQuery(). */
218 /* Exercise the stream buffer utilities. Try creating with a trigger level
219 of 0, it should then get capped to 1. */
220 xStreamBuffer = xStreamBufferCreate( sizeof( uint32_t ), 0 );
221 if( xStreamBuffer != NULL )
223 vStreamBufferSetStreamBufferNumber( xStreamBuffer, uxValue );
224 if( uxStreamBufferGetStreamBufferNumber( xStreamBuffer ) != uxValue )
228 if( ucStreamBufferGetStreamBufferType( xStreamBuffer ) != 0 )
230 /* "Is Message Buffer" flag should have been 0. */
234 vStreamBufferDelete( xStreamBuffer );
241 xMessageBuffer = xMessageBufferCreate( sizeof( uint32_t ) );
242 if( xMessageBuffer != NULL )
244 if( ucStreamBufferGetStreamBufferType( xMessageBuffer ) == 0 )
246 /* "Is Message Buffer" flag should have been 1. */
250 vMessageBufferDelete( xMessageBuffer );
259 /*-----------------------------------------------------------*/
261 static BaseType_t prvPeekTimeout( void )
263 QueueHandle_t xHandle;
264 const UBaseType_t xQueueLength = 1;
265 BaseType_t xReturn = pdPASS;
266 TickType_t xBlockTime = ( TickType_t ) 2;
267 UBaseType_t uxReceived;
269 /* Create the queue just to try peeking it while it is empty. */
270 xHandle = xQueueCreate( xQueueLength, ( UBaseType_t ) sizeof( xQueueLength ) );
272 if( xHandle != NULL )
274 if( uxQueueMessagesWaiting( xHandle ) != 0 )
279 /* Ensure peeking from the queue times out as the queue is empty. */
280 if( xQueuePeek( xHandle, &uxReceived, xBlockTime ) != pdFALSE )
285 vQueueDelete( xHandle );
294 /*-----------------------------------------------------------*/
296 static BaseType_t prvQueueQueryFromISR( void )
298 BaseType_t xReturn = pdPASS, xValue = 1;
299 const UBaseType_t xISRQueueLength = ( UBaseType_t ) 1;
300 const char *pcISRQueueName = "ISRQueue";
301 QueueHandle_t xISRQueue = NULL;
303 xISRQueue = xQueueCreate( xISRQueueLength, ( UBaseType_t ) sizeof( BaseType_t ) );
305 if( xISRQueue != NULL )
307 vQueueAddToRegistry( xISRQueue, pcISRQueueName );
308 if( strcmp( pcQueueGetName( xISRQueue ), pcISRQueueName ) )
313 /* Expect the queue to be empty here. */
314 if( uxQueueMessagesWaitingFromISR( xISRQueue ) != 0 )
319 if( xQueueIsQueueEmptyFromISR( xISRQueue ) != pdTRUE )
324 if( xQueueIsQueueFullFromISR( xISRQueue ) != pdFALSE )
329 /* Now fill the queue - it only has one space. */
330 if( xQueueSendFromISR( xISRQueue, &xValue, NULL ) != pdPASS )
335 /* Check it now reports as full. */
336 if( uxQueueMessagesWaitingFromISR( xISRQueue ) != 1 )
341 if( xQueueIsQueueEmptyFromISR( xISRQueue ) != pdFALSE )
346 if( xQueueIsQueueFullFromISR( xISRQueue ) != pdTRUE )
351 vQueueDelete( xISRQueue );
360 /*-----------------------------------------------------------*/
362 static BaseType_t prvTaskQueryFunctions( void )
364 static TaskStatus_t xStatus, *pxStatusArray;
365 TaskHandle_t xTimerTask, xIdleTask;
366 BaseType_t xReturn = pdPASS;
367 UBaseType_t uxNumberOfTasks, uxReturned, ux;
368 uint32_t ulTotalRunTime1, ulTotalRunTime2;
369 const uint32_t ulRunTimeTollerance = ( uint32_t ) 0xfff;
371 /* Obtain task status with the stack high water mark and without the
373 vTaskGetInfo( NULL, &xStatus, pdTRUE, eRunning );
375 if( uxTaskGetStackHighWaterMark( NULL ) != xStatus.usStackHighWaterMark )
380 if( uxTaskGetStackHighWaterMark2( NULL ) != ( configSTACK_DEPTH_TYPE ) xStatus.usStackHighWaterMark )
385 /* Now obtain a task status without the high water mark but with the state,
386 which in the case of the idle task should be Read. */
387 xTimerTask = xTimerGetTimerDaemonTaskHandle();
388 vTaskSuspend( xTimerTask ); /* Should never suspend Timer task normally!. */
389 vTaskGetInfo( xTimerTask, &xStatus, pdFALSE, eInvalid );
390 if( xStatus.eCurrentState != eSuspended )
394 if( xStatus.uxBasePriority != uxTaskPriorityGetFromISR( xTimerTask ) )
398 if( xStatus.uxBasePriority != ( configMAX_PRIORITIES - 1 ) )
402 xTaskResumeFromISR( xTimerTask );
403 vTaskGetInfo( xTimerTask, &xStatus, pdTRUE, eInvalid );
404 if( ( xStatus.eCurrentState != eReady ) && ( xStatus.eCurrentState != eBlocked ) )
408 if( uxTaskGetStackHighWaterMark( xTimerTask ) != xStatus.usStackHighWaterMark )
412 if( uxTaskGetStackHighWaterMark2( xTimerTask ) != ( configSTACK_DEPTH_TYPE ) xStatus.usStackHighWaterMark )
417 /* Attempting to abort a delay in the idle task should be guaranteed to
418 fail as the idle task should never block. */
419 xIdleTask = xTaskGetIdleTaskHandle();
420 if( xTaskAbortDelay( xIdleTask ) != pdFAIL )
425 /* Create an array of task status objects large enough to hold information
426 on the number of tasks at this time - note this may change at any time if
427 higher priority tasks are executing and creating tasks. */
428 uxNumberOfTasks = uxTaskGetNumberOfTasks();
429 pxStatusArray = ( TaskStatus_t * ) pvPortMalloc( uxNumberOfTasks * sizeof( TaskStatus_t ) );
431 if( pxStatusArray != NULL )
433 /* Pass part of the array into uxTaskGetSystemState() to ensure it doesn't
434 try using more space than there is available. */
435 uxReturned = uxTaskGetSystemState( pxStatusArray, uxNumberOfTasks / ( UBaseType_t ) 2, NULL );
436 if( uxReturned != ( UBaseType_t ) 0 )
441 /* Now do the same but passing in the complete array size, this is done
442 twice to check for a difference in the total run time. */
443 uxTaskGetSystemState( pxStatusArray, uxNumberOfTasks, &ulTotalRunTime1 );
444 memset( ( void * ) pxStatusArray, 0xaa, uxNumberOfTasks * sizeof( TaskStatus_t ) );
445 uxReturned = uxTaskGetSystemState( pxStatusArray, uxNumberOfTasks, &ulTotalRunTime2 );
446 if( ( ulTotalRunTime2 - ulTotalRunTime1 ) > ulRunTimeTollerance )
451 /* Basic santity check of array contents. */
452 for( ux = 0; ux < uxReturned; ux++ )
454 if( pxStatusArray[ ux ].eCurrentState >= ( UBaseType_t ) eInvalid )
458 if( pxStatusArray[ ux ].uxCurrentPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
464 vPortFree( pxStatusArray );
473 /*-----------------------------------------------------------*/
475 static BaseType_t prvDummyTagFunction( void *pvParameter )
477 return ( BaseType_t ) pvParameter;
479 /*-----------------------------------------------------------*/
481 static BaseType_t prvTaskTags( void )
483 BaseType_t xReturn = pdPASS, xParameter = ( BaseType_t ) 0xDEADBEEF;
486 /* First try with the handle of a different task. Use the timer task for
488 xTask = xTimerGetTimerDaemonTaskHandle();
490 vTaskSetApplicationTaskTag( xTask, prvDummyTagFunction );
491 if( xTaskGetApplicationTaskTag( xTask ) != prvDummyTagFunction )
497 if( xTaskCallApplicationTaskHook( xTask, ( void * ) xParameter ) != xParameter )
501 if( xTaskCallApplicationTaskHook( xTask, ( void * ) NULL ) != pdFAIL )
507 /* Try FromISR version too. */
508 if( xTaskGetApplicationTaskTagFromISR( xTask ) != prvDummyTagFunction )
513 /* Now try with a NULL handle, so using this task. */
514 vTaskSetApplicationTaskTag( NULL, NULL );
515 if( xTaskGetApplicationTaskTag( NULL ) != NULL )
519 if( xTaskGetApplicationTaskTagFromISR( NULL ) != NULL )
524 vTaskSetApplicationTaskTag( NULL, prvDummyTagFunction );
525 if( xTaskGetApplicationTaskTag( NULL ) != prvDummyTagFunction )
531 if( xTaskCallApplicationTaskHook( NULL, ( void * ) xParameter ) != xParameter )
535 if( xTaskCallApplicationTaskHook( NULL, ( void * ) NULL ) != pdFAIL )
541 /* Try FromISR version too. */
542 if( xTaskGetApplicationTaskTagFromISR( NULL ) != prvDummyTagFunction )
547 vTaskSetApplicationTaskTag( NULL, NULL );
548 if( xTaskGetApplicationTaskTag( NULL ) != NULL )
555 /*-----------------------------------------------------------*/
557 static BaseType_t prvTimerQuery( void )
559 TimerHandle_t xTimer;
560 BaseType_t xReturn = pdPASS;
561 const char *pcTimerName = "TestTimer";
562 const TickType_t xTimerPeriod = ( TickType_t ) 100;
563 const UBaseType_t uxTimerNumber = ( UBaseType_t ) 55;
565 xTimer = xTimerCreate( pcTimerName,
568 ( void * ) xTimerPeriod,
569 NULL ); /* Not actually going to start timer so NULL callback is ok. */
573 if( xTimerGetPeriod( xTimer ) != xTimerPeriod )
578 if( strcmp( pcTimerGetName( xTimer ), pcTimerName ) != 0 )
583 vTimerSetTimerNumber( xTimer, uxTimerNumber );
584 if( uxTimerGetTimerNumber( xTimer ) != uxTimerNumber )
589 xTimerDelete( xTimer, portMAX_DELAY );
598 /*-----------------------------------------------------------*/
600 BaseType_t xRunCodeCoverageTestAdditions( void )
602 BaseType_t xReturn = pdPASS;
604 xReturn &= prvStaticAllocationsWithNullBuffers();
605 xReturn &= prvTraceUtils();
606 xReturn &= prvPeekTimeout();
607 xReturn &= prvQueueQueryFromISR();
608 xReturn &= prvTaskQueryFunctions();
609 xReturn &= prvTaskTags();
610 xReturn &= prvTimerQuery();
614 /*-----------------------------------------------------------*/