From a50edad08b29052631aa469d4df6e6ec7ff68878 Mon Sep 17 00:00:00 2001 From: Anubhav Rawal Date: Wed, 8 Jul 2026 17:38:51 -0700 Subject: [PATCH] fix: Add MPU wrapper for xTimerDelete API (#1412) When using MPU wrappers v2, xTimerDelete needs to be a real function rather than a macro so that the kernel object pool index can be freed after the timer is successfully deleted. Without this, deleting a timer leaks the kernel object pool entry. Signed-off-by: Gaurav Aggarwal Co-authored-by: Gaurav Aggarwal --- include/mpu_prototypes.h | 3 +++ include/mpu_wrappers.h | 1 + include/timers.h | 7 ++++++- portable/Common/mpu_wrappers_v2.c | 34 +++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/include/mpu_prototypes.h b/include/mpu_prototypes.h index b4c0f4745..d8dc56a48 100644 --- a/include/mpu_prototypes.h +++ b/include/mpu_prototypes.h @@ -369,6 +369,9 @@ BaseType_t MPU_xTimerGenericCommandFromISR( TimerHandle_t xTimer, BaseType_t * const pxHigherPriorityTaskWoken, const TickType_t xTicksToWait ) PRIVILEGED_FUNCTION; +BaseType_t MPU_xTimerDelete( TimerHandle_t xTimer, + TickType_t xTicksToWait ) PRIVILEGED_FUNCTION; + /* MPU versions of event_group.h API functions. */ EventBits_t MPU_xEventGroupWaitBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToWaitFor, diff --git a/include/mpu_wrappers.h b/include/mpu_wrappers.h index 3b4738e96..3de2013ce 100644 --- a/include/mpu_wrappers.h +++ b/include/mpu_wrappers.h @@ -190,6 +190,7 @@ #define xTimerCreateStatic MPU_xTimerCreateStatic #define xTimerGetStaticBuffer MPU_xTimerGetStaticBuffer #define xTimerGenericCommandFromISR MPU_xTimerGenericCommandFromISR + #define xTimerDelete MPU_xTimerDelete #endif /* #if ( configUSE_MPU_WRAPPERS_V1 == 0 ) */ /* Map standard event_group.h API functions to the MPU equivalents. */ diff --git a/include/timers.h b/include/timers.h index 7d99d3536..191703928 100644 --- a/include/timers.h +++ b/include/timers.h @@ -667,8 +667,13 @@ TaskHandle_t xTimerGetTimerDaemonTaskHandle( void ) PRIVILEGED_FUNCTION; * * See the xTimerChangePeriod() API function example usage scenario. */ -#define xTimerDelete( xTimer, xTicksToWait ) \ +#if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configUSE_MPU_WRAPPERS_V1 == 0 ) ) + BaseType_t xTimerDelete( TimerHandle_t xTimer, + TickType_t xTicksToWait ); +#else + #define xTimerDelete( xTimer, xTicksToWait ) \ xTimerGenericCommand( ( xTimer ), tmrCOMMAND_DELETE, 0U, NULL, ( xTicksToWait ) ) +#endif /** * BaseType_t xTimerReset( TimerHandle_t xTimer, TickType_t xTicksToWait ); diff --git a/portable/Common/mpu_wrappers_v2.c b/portable/Common/mpu_wrappers_v2.c index 70082b829..612cbae18 100644 --- a/portable/Common/mpu_wrappers_v2.c +++ b/portable/Common/mpu_wrappers_v2.c @@ -3890,6 +3890,40 @@ #endif /* if ( configUSE_TIMERS == 1 ) */ /*-----------------------------------------------------------*/ + #if ( configUSE_TIMERS == 1 ) + + BaseType_t MPU_xTimerDelete( TimerHandle_t xTimer, TickType_t xTicksToWait ) /* PRIVILEGED_FUNCTION */ + { + BaseType_t xReturn = pdFALSE; + TimerHandle_t xInternalTimerHandle = NULL; + int32_t lIndex; + + lIndex = ( int32_t ) xTimer; + + if( IS_EXTERNAL_INDEX_VALID( lIndex ) != pdFALSE ) + { + xInternalTimerHandle = MPU_GetTimerHandleAtIndex( CONVERT_TO_INTERNAL_INDEX( lIndex ) ); + + if( xInternalTimerHandle != NULL ) + { + xReturn = xTimerGenericCommandFromTask( xInternalTimerHandle, + tmrCOMMAND_DELETE, + 0U, /* xOptionalValue */ + NULL, /* pxHigherPriorityTaskWoken */ + xTicksToWait ); + if( xReturn != pdFALSE ) + { + MPU_SetIndexFreeInKernelObjectPool( CONVERT_TO_INTERNAL_INDEX( lIndex ) ); + } + } + } + + return xReturn; + } + + #endif /* if ( configUSE_TIMERS == 1 ) */ +/*-----------------------------------------------------------*/ + /*-----------------------------------------------------------*/ /* MPU wrappers for event group APIs. */ /*-----------------------------------------------------------*/