From ea51a3bfbf7c06e6e3024403369f7313656dc371 Mon Sep 17 00:00:00 2001 From: RichardBarry Date: Sat, 20 Feb 2021 13:34:56 -0800 Subject: [PATCH] Introduce tasks that test the coherency of the reported space available in a message buffer from two separate tasks. Designed to highlight the issue reported in FreeRTOS/FreeRTOS-Kernel#264 Introduce configRUN_ADDITIONAL_TESTS which must be set to 1 to run the new tests. That is because the new tests got added to an existing standard demo file and smaller platforms may not have the resources to run them. Set configRUN_ADDITIONAL_TESTS to 1 in the MSVC and IAR/QEMU project so both project run the new test. Also add missing 'volatile' qualifier on some register accesses in the same file. --- .../CORTEX_MPS2_QEMU_IAR/FreeRTOSConfig.h | 4 + FreeRTOS/Demo/CORTEX_MPS2_QEMU_IAR/main.c | 8 +- .../Demo/Common/Minimal/MessageBufferDemo.c | 104 ++++++++++++++++++ FreeRTOS/Demo/WIN32-MSVC/FreeRTOSConfig.h | 4 + FreeRTOS/Demo/WIN32-MSVC/main.c | 2 +- 5 files changed, 117 insertions(+), 5 deletions(-) diff --git a/FreeRTOS/Demo/CORTEX_MPS2_QEMU_IAR/FreeRTOSConfig.h b/FreeRTOS/Demo/CORTEX_MPS2_QEMU_IAR/FreeRTOSConfig.h index ac34ce7e34c..7a5b4134dc5 100644 --- a/FreeRTOS/Demo/CORTEX_MPS2_QEMU_IAR/FreeRTOSConfig.h +++ b/FreeRTOS/Demo/CORTEX_MPS2_QEMU_IAR/FreeRTOSConfig.h @@ -107,6 +107,10 @@ See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */ version. */ #define configUSE_PORT_OPTIMISED_TASK_SELECTION 1 +/* The Win32 target is capable of running all the tests tasks at the same + * time. */ +#define configRUN_ADDITIONAL_TESTS 1 + /* The test that checks the trigger level on stream buffers requires an allowable margin of error on slower processors (slower than the Win32 machine on which the test is developed). */ diff --git a/FreeRTOS/Demo/CORTEX_MPS2_QEMU_IAR/main.c b/FreeRTOS/Demo/CORTEX_MPS2_QEMU_IAR/main.c index 14a47bd1b7d..0f1fd6e0c4d 100644 --- a/FreeRTOS/Demo/CORTEX_MPS2_QEMU_IAR/main.c +++ b/FreeRTOS/Demo/CORTEX_MPS2_QEMU_IAR/main.c @@ -65,9 +65,9 @@ implemented and described in main_full.c. */ /* printf() output uses the UART. These constants define the addresses of the required UART registers. */ #define UART0_ADDRESS ( 0x40004000UL ) -#define UART0_DATA ( * ( ( ( uint32_t * )( UART0_ADDRESS + 0UL ) ) ) ) -#define UART0_CTRL ( * ( ( ( uint32_t * )( UART0_ADDRESS + 8UL ) ) ) ) -#define UART0_BAUDDIV ( * ( ( ( uint32_t * )( UART0_ADDRESS + 16UL ) ) ) ) +#define UART0_DATA ( * ( ( ( volatile uint32_t * )( UART0_ADDRESS + 0UL ) ) ) ) +#define UART0_CTRL ( * ( ( ( volatile uint32_t * )( UART0_ADDRESS + 8UL ) ) ) ) +#define UART0_BAUDDIV ( * ( ( ( volatile uint32_t * )( UART0_ADDRESS + 16UL ) ) ) ) /* * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1. @@ -188,7 +188,7 @@ volatile uint32_t ulSetToNonZeroInDebuggerToContinue = 0; /* Called if an assertion passed to configASSERT() fails. See http://www.freertos.org/a00110.html#configASSERT for more information. */ - printf( "ASSERT! Line %ld, file %s\r\n", ulLine, pcFileName ); + printf( "ASSERT! Line %d, file %s\r\n", ( int ) ulLine, pcFileName ); taskENTER_CRITICAL(); { diff --git a/FreeRTOS/Demo/Common/Minimal/MessageBufferDemo.c b/FreeRTOS/Demo/Common/Minimal/MessageBufferDemo.c index 57f69fbe569..d6d770bb87f 100644 --- a/FreeRTOS/Demo/Common/Minimal/MessageBufferDemo.c +++ b/FreeRTOS/Demo/Common/Minimal/MessageBufferDemo.c @@ -98,6 +98,20 @@ static void prvNonBlockingSenderTask( void *pvParameters ); static uint32_t ulSenderLoopCounters[ mbNUMBER_OF_SENDER_TASKS ] = { 0 }; #endif /* configSUPPORT_STATIC_ALLOCATION */ + +#if( configRUN_ADDITIONAL_TESTS == 1 ) + #define mbCOHERENCE_TEST_BUFFER_SIZE 20 + #define mbCOHERENCE_TEST_BYTES_WRITTEN 5 + #define mbBYTES_TO_STORE_MESSAGE_LENGTH ( sizeof( configMESSAGE_BUFFER_LENGTH_TYPE ) ) + #define mbEXPECTED_FREE_BYTES_AFTER_WRITING_STRING ( mbCOHERENCE_TEST_BUFFER_SIZE - ( mbCOHERENCE_TEST_BYTES_WRITTEN + mbBYTES_TO_STORE_MESSAGE_LENGTH ) ) + + static void prvSpaceAvailableCoherenceActor( void *pvParameters ); + static void prvSpaceAvailableCoherenceTester( void *pvParameters ); + static MessageBufferHandle_t xCoherenceTestMessageBuffer = NULL; + + static uint32_t ulSizeCoherencyTestCycles = 0UL; +#endif + /*-----------------------------------------------------------*/ /* The buffers used by the echo client and server tasks. */ @@ -157,6 +171,16 @@ MessageBufferHandle_t xMessageBuffer; xTaskCreate( prvSenderTask, "2Sender", xBlockingStackSize, NULL, mbLOWER_PRIORITY, NULL ); } #endif /* configSUPPORT_STATIC_ALLOCATION */ + + #if( configRUN_ADDITIONAL_TESTS == 1 ) + { + xCoherenceTestMessageBuffer = xMessageBufferCreate( mbCOHERENCE_TEST_BUFFER_SIZE ); + configASSERT( xCoherenceTestMessageBuffer ); + + xTaskCreate( prvSpaceAvailableCoherenceActor, "mbsanity1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL ); + xTaskCreate( prvSpaceAvailableCoherenceTester, "mbsanity2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL ); + } + #endif } /*-----------------------------------------------------------*/ @@ -819,6 +843,71 @@ const TickType_t xTicksToBlock = pdMS_TO_TICKS( 250UL ); } /*-----------------------------------------------------------*/ +/* Tests within configRUN_ADDITIONAL_TESTS blocks only execute on larger + * platforms or have been added to pre-existing files that are already in use + * by other test projects without ensuring they don't cause those pre-existing + * projects to run out of program or data memory. */ +#if( configRUN_ADDITIONAL_TESTS == 1 ) + + static void prvSpaceAvailableCoherenceActor( void *pvParameters ) + { + static char *cTxString = "12345"; + char cRxString[ mbCOHERENCE_TEST_BYTES_WRITTEN + 1 ]; /* +1 for NULL terminator. */ + + ( void ) pvParameters; + + for( ;; ) + { + /* Add bytes to the buffer so the other task should see + mbEXPECTED_FREE_BYTES_AFTER_WRITING_STRING bytes free. */ + xMessageBufferSend( xCoherenceTestMessageBuffer, ( void * ) cTxString, strlen( cTxString ), 0 ); + configASSERT( xMessageBufferSpacesAvailable( xCoherenceTestMessageBuffer ) == mbEXPECTED_FREE_BYTES_AFTER_WRITING_STRING ); + + /* Read out message again so the other task should read the full + mbCOHERENCE_TEST_BUFFER_SIZE bytes free again. */ + memset( ( void * ) cRxString, 0x00, sizeof( cRxString ) ); + xMessageBufferReceive( xCoherenceTestMessageBuffer, ( void * ) cRxString, mbCOHERENCE_TEST_BYTES_WRITTEN, 0 ); + configASSERT( strcmp( cTxString, cRxString ) == 0 ); + } + } + /*-----------------------------------------------------------*/ + + static void prvSpaceAvailableCoherenceTester( void *pvParameters ) + { + size_t xSpaceAvailable; + BaseType_t xErrorFound = pdFALSE; + + ( void ) pvParameters; + + for( ;; ) + { + /* This message buffer is only ever empty or contains 5 bytes. So all + queries of its free space should result in one of the two values tested + below. */ + xSpaceAvailable = xMessageBufferSpacesAvailable( xCoherenceTestMessageBuffer ); + + if( ( xSpaceAvailable == mbCOHERENCE_TEST_BUFFER_SIZE ) || + ( xSpaceAvailable == mbEXPECTED_FREE_BYTES_AFTER_WRITING_STRING ) ) + { + /* Only continue to increment the variable that shows this task + is still executing if no errors have been found. */ + if( xErrorFound == pdFALSE ) + { + ulSizeCoherencyTestCycles++; + } + } + else + { + xErrorFound = pdTRUE; + } + + configASSERT( xErrorFound == pdFALSE ); + } + } + +#endif /* configRUN_ADDITIONAL_TESTS == 1 */ +/*-----------------------------------------------------------*/ + BaseType_t xAreMessageBufferTasksStillRunning( void ) { static uint32_t ulLastEchoLoopCounters[ mbNUMBER_OF_ECHO_CLIENTS ] = { 0 }; @@ -864,6 +953,21 @@ BaseType_t xReturn = pdPASS, x; } #endif /* configSUPPORT_STATIC_ALLOCATION */ + #if( configRUN_ADDITIONAL_TESTS == 1 ) + { + static uint32_t ullastSizeCoherencyTestCycles = 0UL; + + if( ullastSizeCoherencyTestCycles == ulSizeCoherencyTestCycles ) + { + xReturn = pdFAIL; + } + else + { + ullastSizeCoherencyTestCycles = ulSizeCoherencyTestCycles; + } + } + #endif + return xReturn; } /*-----------------------------------------------------------*/ diff --git a/FreeRTOS/Demo/WIN32-MSVC/FreeRTOSConfig.h b/FreeRTOS/Demo/WIN32-MSVC/FreeRTOSConfig.h index 0912eee5142..d45a8195c0b 100644 --- a/FreeRTOS/Demo/WIN32-MSVC/FreeRTOSConfig.h +++ b/FreeRTOS/Demo/WIN32-MSVC/FreeRTOSConfig.h @@ -112,6 +112,10 @@ functions anyway. */ #define INCLUDE_xTimerPendFunctionCall 1 #define INCLUDE_xTaskAbortDelay 1 +/* The Win32 target is capable of running all the tests tasks at the same + * time. */ +#define configRUN_ADDITIONAL_TESTS 1 + /* It is a good idea to define configASSERT() while developing. configASSERT() uses the same semantics as the standard C assert() macro. */ extern void vAssertCalled( unsigned long ulLine, const char * const pcFileName ); diff --git a/FreeRTOS/Demo/WIN32-MSVC/main.c b/FreeRTOS/Demo/WIN32-MSVC/main.c index a721a099f9f..7bd46c3793d 100644 --- a/FreeRTOS/Demo/WIN32-MSVC/main.c +++ b/FreeRTOS/Demo/WIN32-MSVC/main.c @@ -80,7 +80,7 @@ smaller heap regions - in which case heap_4.c would be the more appropriate choice. See http://www.freertos.org/a00111.html for an explanation. */ #define mainREGION_1_SIZE 8201 #define mainREGION_2_SIZE 29905 -#define mainREGION_3_SIZE 7607 +#define mainREGION_3_SIZE 7807 /*-----------------------------------------------------------*/