From 4e9fd6550bd0030d6353157b3f89f9c371586c78 Mon Sep 17 00:00:00 2001 From: kedeng Date: Thu, 10 Jul 2025 15:09:11 +0800 Subject: [PATCH] Optimize timer index lookup and fix CLI DMA option boundaries --- src/main/cli/cli.c | 16 ++++++++-------- src/platform/APM32/timer_apm32.c | 7 +------ src/platform/AT32/timer_at32bsp.c | 7 +------ src/platform/STM32/timer_hal.c | 7 +------ src/platform/STM32/timer_stdperiph.c | 7 +------ 5 files changed, 12 insertions(+), 32 deletions(-) diff --git a/src/main/cli/cli.c b/src/main/cli/cli.c index 7f209e906e..e23a24f2a4 100644 --- a/src/main/cli/cli.c +++ b/src/main/cli/cli.c @@ -5418,29 +5418,29 @@ static void optToString(int optval, char *buf) static int getDmaOptDisplayNumber(dmaoptEntry_t *entry, int index) { if (entry->peripheral == DMA_PERIPH_TIMUP) { - int uiIndex = timerGetNumberByIndex(index); - if (!(TIM_N(uiIndex) & entry->presenceMask)) { + const int dispNum = timerGetNumberByIndex(index); + if (!(TIM_N(dispNum) & entry->presenceMask)) { return -1; } - return uiIndex; + return dispNum; } return DMA_OPT_UI_INDEX(index); } -static int displayNumberToDmaOptIndex(dmaoptEntry_t *entry, int index) +static int displayNumberToDmaOptIndex(dmaoptEntry_t *entry, int dispNum) { - if (index <= 0) { + if (dispNum < 0) { return -1; } if (entry->peripheral == DMA_PERIPH_TIMUP) { - if (!(entry->presenceMask & TIM_N(index))) { + if (!(entry->presenceMask & TIM_N(dispNum))) { return -1; } - return timerGetIndexByNumber(index); + return timerGetIndexByNumber(dispNum); } - return index > entry->maxIndex ? -1 : index - 1; + return dispNum > entry->maxIndex ? -1 : dispNum - 1; } static int dmaOptIndexToPeripheralMappingIndex(dmaoptEntry_t *entry, int index) diff --git a/src/platform/APM32/timer_apm32.c b/src/platform/APM32/timer_apm32.c index 932265b7a0..e516c2af8b 100644 --- a/src/platform/APM32/timer_apm32.c +++ b/src/platform/APM32/timer_apm32.c @@ -286,12 +286,7 @@ int8_t timerGetNumberByIndex(uint8_t index) int8_t timerGetIndexByNumber(uint8_t number) { - for (int i = 0; i < USED_TIMER_COUNT; i++) { - if (timerNumbers[i] == number) { - return i; - } - } - return -1; + return TIM_N(number) & USED_TIMERS ? popcount((TIM_N(number) - 1) & USED_TIMERS) : -1; } int8_t timerGetTIMNumber(const TMR_TypeDef *tim) diff --git a/src/platform/AT32/timer_at32bsp.c b/src/platform/AT32/timer_at32bsp.c index 6b01034836..24e28a0fd7 100644 --- a/src/platform/AT32/timer_at32bsp.c +++ b/src/platform/AT32/timer_at32bsp.c @@ -285,12 +285,7 @@ int8_t timerGetNumberByIndex(uint8_t index) int8_t timerGetIndexByNumber(uint8_t number) { - for (int i = 0; i < USED_TIMER_COUNT; i++) { - if (timerNumbers[i] == number) { - return i; - } - } - return -1; + return TIM_N(number) & USED_TIMERS ? popcount((TIM_N(number) - 1) & USED_TIMERS) : -1; } int8_t timerGetTIMNumber(const tmr_type *tim) diff --git a/src/platform/STM32/timer_hal.c b/src/platform/STM32/timer_hal.c index b24dc41230..c22fb2186c 100644 --- a/src/platform/STM32/timer_hal.c +++ b/src/platform/STM32/timer_hal.c @@ -294,12 +294,7 @@ int8_t timerGetNumberByIndex(uint8_t index) int8_t timerGetIndexByNumber(uint8_t number) { - for (int i = 0; i < USED_TIMER_COUNT; i++) { - if (timerNumbers[i] == number) { - return i; - } - } - return -1; + return TIM_N(number) & USED_TIMERS ? popcount((TIM_N(number) - 1) & USED_TIMERS) : -1; } int8_t timerGetTIMNumber(const TIM_TypeDef *tim) diff --git a/src/platform/STM32/timer_stdperiph.c b/src/platform/STM32/timer_stdperiph.c index 3d4748705f..2bba4b28b0 100644 --- a/src/platform/STM32/timer_stdperiph.c +++ b/src/platform/STM32/timer_stdperiph.c @@ -276,12 +276,7 @@ int8_t timerGetNumberByIndex(uint8_t index) int8_t timerGetIndexByNumber(uint8_t number) { - for (int i = 0; i < USED_TIMER_COUNT; i++) { - if (timerNumbers[i] == number) { - return i; - } - } - return -1; + return TIM_N(number) & USED_TIMERS ? popcount((TIM_N(number) - 1) & USED_TIMERS) : -1; } int8_t timerGetTIMNumber(const TIM_TypeDef *tim)