From c565fd45c19c7c5b0d7a6cfa288039051a017b53 Mon Sep 17 00:00:00 2001 From: barnatahmed <101841699+barnatahmed@users.noreply.github.com> Date: Tue, 30 Jan 2024 20:44:27 +0100 Subject: [PATCH 1/7] Cmake: Create a single static library including port Modify portable/CMakeLists.txt to create only one static library containing both the common kernel code and kernel port. Change the freertos_kernel_port target from a STATIC library to an OBJECT library and introduce a new freertos_kernel_port_headers INTERFACE library target. --------- Co-authored-by: ABARNAT Co-authored-by: Soren Ptak Co-authored-by: Gaurav-Aggarwal-AWS <33462878+aggarg@users.noreply.github.com> --- CMakeLists.txt | 12 ++++++++---- portable/CMakeLists.txt | 6 ++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5323ec19b..07eeec471 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -217,16 +217,17 @@ elseif((FREERTOS_PORT STREQUAL "A_CUSTOM_PORT") AND (NOT TARGET freertos_kernel_ " port.c\n" " portmacro.h\n" " Where FreeRTOSCustomPort/CMakeLists.txt is a modified version of:\n" - " add_library(freertos_kernel_port STATIC)\n" + " add_library(freertos_kernel_port OBJECT)\n" " target_sources(freertos_kernel_port\n" " PRIVATE\n" " port.c\n" " portmacro.h)\n" - " target_include_directories(freertos_kernel_port\n" - " PUBLIC\n" + " add_library(freertos_kernel_port_headers INTERFACE)\n" + " target_include_directories(freertos_kernel_port_headers INTERFACE \n" " .)\n" " target_link_libraries(freertos_kernel_port\n" " PRIVATE\n" + " freertos_kernel_port_headers\n" " freertos_kernel_include)") endif() @@ -257,8 +258,11 @@ endif() target_link_libraries(freertos_kernel PUBLIC - freertos_kernel_port freertos_kernel_include + freertos_kernel_port_headers + PRIVATE + freertos_kernel_port + ) ######################################################################## diff --git a/portable/CMakeLists.txt b/portable/CMakeLists.txt index 9df221d21..98dcf8ed3 100644 --- a/portable/CMakeLists.txt +++ b/portable/CMakeLists.txt @@ -15,7 +15,7 @@ endif() # FreeRTOS internal cmake file. Do not use it in user top-level project -add_library(freertos_kernel_port STATIC +add_library(freertos_kernel_port OBJECT # TEMPLATE Port $<$: template/port.c> @@ -755,8 +755,9 @@ if( FREERTOS_PORT MATCHES "GCC_ARM_CM(3|4)_MPU" OR Common/mpu_wrappers_v2.c ) endif() +add_library(freertos_kernel_port_headers INTERFACE) -target_include_directories(freertos_kernel_port PUBLIC +target_include_directories(freertos_kernel_port_headers INTERFACE # TEMPLATE Port $<$:${CMAKE_CURRENT_LIST_DIR}/template> @@ -1094,6 +1095,7 @@ target_link_libraries(freertos_kernel_port PUBLIC $<$:pico_base_headers> $<$:idf::esp32> + freertos_kernel_port_headers PRIVATE freertos_kernel_include $<$:Threads::Threads> From ba1e2dad3cc6fcb4209e33093efba83d189aa836 Mon Sep 17 00:00:00 2001 From: Rahul Kar <118818625+kar-rahul-aws@users.noreply.github.com> Date: Wed, 31 Jan 2024 12:25:56 +0530 Subject: [PATCH 2/7] Update ARMv8-M config definitions in template file (#970) * Update ARMV8-M config definitions in template file --------- Co-authored-by: GitHub Action --- .../template_configuration/FreeRTOSConfig.h | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/examples/template_configuration/FreeRTOSConfig.h b/examples/template_configuration/FreeRTOSConfig.h index a672600ff..14aa3b24b 100644 --- a/examples/template_configuration/FreeRTOSConfig.h +++ b/examples/template_configuration/FreeRTOSConfig.h @@ -531,6 +531,38 @@ * setting configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined. */ #define configKERNEL_PROVIDED_STATIC_MEMORY 1 +/******************************************************************************/ +/* ARMv8-M port Specific Configuration definitions. ***************************/ +/******************************************************************************/ + +/* Set configENABLE_TRUSTZONE to 1 when running FreeRTOS on the non-secure side + * to enable the TrustZone support in FreeRTOS ARMv8-M ports which allows the + * non-secure FreeRTOS tasks to call the (non-secure callable) functions + * exported from secure side. */ +#define configENABLE_TRUSTZONE 1 + +/* If the application writer does not want to use TrustZone, but the hardware does + * not support disabling TrustZone then the entire application (including the FreeRTOS + * scheduler) can run on the secure side without ever branching to the non-secure side. + * To do that, in addition to setting configENABLE_TRUSTZONE to 0, also set + * configRUN_FREERTOS_SECURE_ONLY to 1. */ +#define configRUN_FREERTOS_SECURE_ONLY 1 + +/* Set configENABLE_MPU to 1 to enable the Memory Protection Unit (MPU), or 0 + * to leave the Memory Protection Unit disabled. */ +#define configENABLE_MPU 1 + +/* Set configENABLE_FPU to 1 to enable the Floating Point Unit (FPU), or 0 + * to leave the Floating Point Unit disabled. */ +#define configENABLE_FPU 1 + +/* Set configENABLE_MVE to 1 to enable the M-Profile Vector Extension (MVE) support, + * or 0 to leave the MVE support disabled. This option is only applicable to Cortex-M55 + * and Cortex-M85 ports as M-Profile Vector Extension (MVE) is available only on + * these architectures. configENABLE_MVE must be left undefined, or defined to 0 + * for the Cortex-M23,Cortex-M33 and Cortex-M35P ports. */ +#define configENABLE_MVE 1 + /******************************************************************************/ /* Definitions that include or exclude functionality. *************************/ /******************************************************************************/ From 1de764ba879a36fe3e155dece2d11b1116b64360 Mon Sep 17 00:00:00 2001 From: chinglee-iot <61685396+chinglee-iot@users.noreply.github.com> Date: Thu, 1 Feb 2024 11:12:08 +0800 Subject: [PATCH 3/7] Delete kernel created task in vTaskEndScheduler (#962) * Update vTaskDelete() to delete a task directly when scheduler is stopped instead of putting it on the xTasksWaitingTermination list. * Delete the idle tasks and timer task in vTaskEndScheduler(). * Reclaim resources for all the tasks on the xTasksWaitingTermination list in vTaskEndScheduler(). * Update POSIX to no longer delete FreeRTOS tasks in the port. --------- Signed-off-by: Gaurav Aggarwal Co-authored-by: Gaurav-Aggarwal-AWS <33462878+aggarg@users.noreply.github.com> Co-authored-by: Gaurav Aggarwal --- portable/ThirdParty/GCC/Posix/port.c | 39 +++++----------------------- tasks.c | 30 ++++++++++++++++++++- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/portable/ThirdParty/GCC/Posix/port.c b/portable/ThirdParty/GCC/Posix/port.c index 65ba54c7b..fdb556d04 100644 --- a/portable/ThirdParty/GCC/Posix/port.c +++ b/portable/ThirdParty/GCC/Posix/port.c @@ -74,7 +74,6 @@ /* Scheduler includes. */ #include "FreeRTOS.h" #include "task.h" -#include "list.h" #include "timers.h" #include "utils/wait_for_event.h" /*-----------------------------------------------------------*/ @@ -88,7 +87,6 @@ typedef struct THREAD void * pvParams; BaseType_t xDying; struct event * ev; - ListItem_t xThreadListItem; } Thread_t; /* @@ -113,7 +111,6 @@ static BaseType_t xSchedulerEnd = pdFALSE; static pthread_t hTimerTickThread; static bool xTimerTickThreadShouldRun; static uint64_t prvStartTimeNs; -static List_t xThreadList; /*-----------------------------------------------------------*/ static void prvSetupSignalsAndSchedulerPolicy( void ); @@ -197,14 +194,8 @@ StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack, thread->ev = event_create(); - vListInitialiseItem( &thread->xThreadListItem ); - listSET_LIST_ITEM_OWNER( &thread->xThreadListItem, thread ); - vPortEnterCritical(); - /* Add the new thread in xThreadList. */ - vListInsertEnd( &xThreadList, &thread->xThreadListItem ); - iRet = pthread_create( &thread->pthread, &xThreadAttributes, prvWaitForStart, thread ); @@ -235,8 +226,6 @@ BaseType_t xPortStartScheduler( void ) { int iSignal; sigset_t xSignals; - ListItem_t * pxIterator; - const ListItem_t * pxEndMarker; hMainThread = pthread_self(); prvPortSetCurrentThreadName("Scheduler"); @@ -263,19 +252,6 @@ BaseType_t xPortStartScheduler( void ) sigwait( &xSignals, &iSignal ); } - /* Cancel all the running thread. */ - pxEndMarker = listGET_END_MARKER( &xThreadList ); - - for( pxIterator = listGET_HEAD_ENTRY( &xThreadList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) ) - { - Thread_t * pxThread = ( Thread_t * ) listGET_LIST_ITEM_OWNER( pxIterator ); - - pthread_cancel( pxThread->pthread ); - event_signal( pxThread->ev ); - pthread_join( pxThread->pthread, NULL ); - event_delete( pxThread->ev ); - } - /* * clear out the variable that is used to end the scheduler, otherwise * subsequent scheduler restarts will end immediately. @@ -300,6 +276,8 @@ BaseType_t xPortStartScheduler( void ) void vPortEndScheduler( void ) { + Thread_t * pxCurrentThread; + /* Stop the timer tick thread. */ xTimerTickThreadShouldRun = false; pthread_join( hTimerTickThread, NULL ); @@ -308,7 +286,10 @@ void vPortEndScheduler( void ) xSchedulerEnd = pdTRUE; ( void ) pthread_kill( hMainThread, SIG_RESUME ); - pthread_exit( NULL ); + /* Waiting to be deleted here. */ + pxCurrentThread = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() ); + event_wait( pxCurrentThread->ev ); + pthread_testcancel(); } /*-----------------------------------------------------------*/ @@ -491,11 +472,6 @@ void vPortCancelThread( void * pxTaskToDelete ) { Thread_t * pxThreadToCancel = prvGetThreadFromTask( pxTaskToDelete ); - /* Remove the thread from xThreadList. */ - vPortEnterCritical(); - uxListRemove( &pxThreadToCancel->xThreadListItem ); - vPortExitCritical(); - /* * The thread has already been suspended so it can be safely cancelled. */ @@ -600,9 +576,6 @@ static void prvSetupSignalsAndSchedulerPolicy( void ) hMainThread = pthread_self(); - /* Setup thread list to record all the task which are not deleted. */ - vListInitialise( &xThreadList ); - /* Initialise common signal masks. */ sigfillset( &xAllSignals ); diff --git a/tasks.c b/tasks.c index b00d218bb..f07097eb0 100644 --- a/tasks.c +++ b/tasks.c @@ -2229,7 +2229,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, /* If the task is running (or yielding), we must add it to the * termination list so that an idle task can delete it when it is * no longer running. */ - if( taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) != pdFALSE ) + if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) != pdFALSE ) ) { /* A running task or a task which is scheduled to yield is being * deleted. This cannot complete when the task is still running @@ -3740,11 +3740,39 @@ void vTaskEndScheduler( void ) { traceENTER_vTaskEndScheduler(); + #if ( INCLUDE_vTaskDelete == 1 ) + { + BaseType_t xCoreID; + + #if ( configUSE_TIMERS == 1 ) + { + /* Delete the timer task created by the kernel. */ + vTaskDelete( xTimerGetTimerDaemonTaskHandle() ); + } + #endif /* #if ( configUSE_TIMERS == 1 ) */ + + /* Delete Idle tasks created by the kernel.*/ + for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ ) + { + vTaskDelete( xIdleTaskHandles[ xCoreID ] ); + } + + /* Idle task is responsible for reclaiming the resources of the tasks in + * xTasksWaitingTermination list. Since the idle task is now deleted and + * no longer going to run, we need to reclaim resources of all the tasks + * in the xTasksWaitingTermination list. */ + prvCheckTasksWaitingTermination(); + } + #endif /* #if ( INCLUDE_vTaskDelete == 1 ) */ + /* Stop the scheduler interrupts and call the portable scheduler end * routine so the original ISRs can be restored if necessary. The port * layer must ensure interrupts enable bit is left in the correct state. */ portDISABLE_INTERRUPTS(); xSchedulerRunning = pdFALSE; + + /* This function must be called from a task and the application is + * responsible for deleting that task after the scheduler is stopped. */ vPortEndScheduler(); traceRETURN_vTaskEndScheduler(); From edd35e8f6e9835e8eb9e1ce26636a2be63595027 Mon Sep 17 00:00:00 2001 From: bradleysmith23 <74752142+bradleysmith23@users.noreply.github.com> Date: Wed, 31 Jan 2024 23:06:56 -0800 Subject: [PATCH 4/7] Fix MISRA Rule 10.1 violations (#976) Co-authored-by: Gaurav-Aggarwal-AWS <33462878+aggarg@users.noreply.github.com> --- portable/Common/mpu_wrappers_v2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/portable/Common/mpu_wrappers_v2.c b/portable/Common/mpu_wrappers_v2.c index e257b25c2..3e25757e2 100644 --- a/portable/Common/mpu_wrappers_v2.c +++ b/portable/Common/mpu_wrappers_v2.c @@ -263,7 +263,7 @@ if( xKernelObjectPool[ i ].xInternalObjectHandle == NULL ) { /* Mark this index as not free. */ - xKernelObjectPool[ i ].xInternalObjectHandle = ( OpaqueObjectHandle_t ) ( ~0 ); + xKernelObjectPool[ i ].xInternalObjectHandle = ( OpaqueObjectHandle_t ) ( ~0U ); lFreeIndex = i; break; } From cd8c6c1f225a8298c97db7837c7c780c1c1c2d23 Mon Sep 17 00:00:00 2001 From: bradleysmith23 <74752142+bradleysmith23@users.noreply.github.com> Date: Thu, 1 Feb 2024 01:01:53 -0800 Subject: [PATCH 5/7] Fix MISRA C 2012 Rule 9.3 violations (#973) Co-authored-by: Gaurav-Aggarwal-AWS <33462878+aggarg@users.noreply.github.com> --- portable/Common/mpu_wrappers_v2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/portable/Common/mpu_wrappers_v2.c b/portable/Common/mpu_wrappers_v2.c index 3e25757e2..4e296430f 100644 --- a/portable/Common/mpu_wrappers_v2.c +++ b/portable/Common/mpu_wrappers_v2.c @@ -245,7 +245,7 @@ /** * @brief Kernel object pool. */ - PRIVILEGED_DATA static KernelObject_t xKernelObjectPool[ configPROTECTED_KERNEL_OBJECT_POOL_SIZE ] = { NULL }; + PRIVILEGED_DATA static KernelObject_t xKernelObjectPool[ configPROTECTED_KERNEL_OBJECT_POOL_SIZE ] = { 0 }; /*-----------------------------------------------------------*/ static int32_t MPU_GetFreeIndexInKernelObjectPool( void ) /* PRIVILEGED_FUNCTION */ From d94db2d11d4013b700fb8ee7d0e1a1910505cc12 Mon Sep 17 00:00:00 2001 From: bradleysmith23 <74752142+bradleysmith23@users.noreply.github.com> Date: Thu, 1 Feb 2024 01:28:24 -0800 Subject: [PATCH 6/7] Fix MISRA C 2012 Rule 10.3 Violations (#974) * Resolve violations for MISRA Rule 10.3-b2 * Formatting fix --------- Co-authored-by: bjbsmith Co-authored-by: chinglee-iot <61685396+chinglee-iot@users.noreply.github.com> --- portable/Common/mpu_wrappers_v2.c | 2 +- queue.c | 8 ++++---- tasks.c | 14 +++++++------- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/portable/Common/mpu_wrappers_v2.c b/portable/Common/mpu_wrappers_v2.c index 4e296430f..39315a2d1 100644 --- a/portable/Common/mpu_wrappers_v2.c +++ b/portable/Common/mpu_wrappers_v2.c @@ -2964,7 +2964,7 @@ QueueHandle_t xInternalQueueHandle = NULL; BaseType_t xReturn = pdFAIL; - lIndex = ( uint32_t ) xQueue; + lIndex = ( int32_t ) xQueue; if( IS_EXTERNAL_INDEX_VALID( lIndex ) != pdFALSE ) { diff --git a/queue.c b/queue.c index 18618202c..daf8d2e9d 100644 --- a/queue.c +++ b/queue.c @@ -1190,7 +1190,7 @@ BaseType_t xQueueGenericSendFromISR( QueueHandle_t xQueue, * read, instead return a flag to say whether a context switch is required or * not (i.e. has a task with a higher priority than us been woken by this * post). */ - uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR(); + uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR(); { if( ( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) || ( xCopyPosition == queueOVERWRITE ) ) { @@ -1365,7 +1365,7 @@ BaseType_t xQueueGiveFromISR( QueueHandle_t xQueue, * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */ portASSERT_IF_INTERRUPT_PRIORITY_INVALID(); - uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR(); + uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR(); { const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting; @@ -2055,7 +2055,7 @@ BaseType_t xQueueReceiveFromISR( QueueHandle_t xQueue, * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */ portASSERT_IF_INTERRUPT_PRIORITY_INVALID(); - uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR(); + uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR(); { const UBaseType_t uxMessagesWaiting = pxQueue->uxMessagesWaiting; @@ -2153,7 +2153,7 @@ BaseType_t xQueuePeekFromISR( QueueHandle_t xQueue, * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */ portASSERT_IF_INTERRUPT_PRIORITY_INVALID(); - uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR(); + uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR(); { /* Cannot block in an ISR, so check there is data available. */ if( pxQueue->uxMessagesWaiting > ( UBaseType_t ) 0 ) diff --git a/tasks.c b/tasks.c index f07097eb0..38a80e2de 100644 --- a/tasks.c +++ b/tasks.c @@ -2657,7 +2657,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */ portASSERT_IF_INTERRUPT_PRIORITY_INVALID(); - uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR(); + uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR(); { /* If null is passed in here then it is the priority of the calling * task that is being queried. */ @@ -2728,7 +2728,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode, * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */ portASSERT_IF_INTERRUPT_PRIORITY_INVALID(); - uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR(); + uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR(); { /* If null is passed in here then it is the base priority of the calling * task that is being queried. */ @@ -4657,7 +4657,7 @@ BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp ) /* This lets the task know it was forcibly removed from the * blocked state so it should not re-evaluate its block time and * then block again. */ - pxTCB->ucDelayAborted = pdTRUE; + pxTCB->ucDelayAborted = ( uint8_t ) pdTRUE; } else { @@ -5598,7 +5598,7 @@ BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut, { /* The delay was aborted, which is not the same as a time out, * but has the same result. */ - pxCurrentTCB->ucDelayAborted = pdFALSE; + pxCurrentTCB->ucDelayAborted = ( uint8_t ) pdFALSE; xReturn = pdTRUE; } else @@ -8064,7 +8064,7 @@ TickType_t uxTaskResetEventItemValue( void ) pxTCB = xTaskToNotify; - uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR(); + uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR(); { if( pulPreviousNotificationValue != NULL ) { @@ -8223,7 +8223,7 @@ TickType_t uxTaskResetEventItemValue( void ) pxTCB = xTaskToNotify; - uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR(); + uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR(); { ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ]; pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED; @@ -8497,7 +8497,7 @@ static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait, /* About to enter a delayed list, so ensure the ucDelayAborted flag is * reset to pdFALSE so it can be detected as having been set to pdTRUE * when the task leaves the Blocked state. */ - pxCurrentTCB->ucDelayAborted = pdFALSE; + pxCurrentTCB->ucDelayAborted = ( uint8_t ) pdFALSE; } #endif From b4da9e301ff0d8f44ae74531e5c4e3407d932ecc Mon Sep 17 00:00:00 2001 From: Rahul Kar <118818625+kar-rahul-aws@users.noreply.github.com> Date: Thu, 1 Feb 2024 17:38:24 +0530 Subject: [PATCH 7/7] Fix MISRA directive 4.7 warning (#977) --- portable/Common/mpu_wrappers_v2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/portable/Common/mpu_wrappers_v2.c b/portable/Common/mpu_wrappers_v2.c index 39315a2d1..cf2dcfbd9 100644 --- a/portable/Common/mpu_wrappers_v2.c +++ b/portable/Common/mpu_wrappers_v2.c @@ -269,7 +269,7 @@ } } } - xTaskResumeAll(); + ( void ) xTaskResumeAll(); return lFreeIndex; }