From 6473a1728e8da1f7bce91c905dd31a1545215d84 Mon Sep 17 00:00:00 2001 From: runger1101001 Date: Thu, 7 Nov 2024 19:16:43 +0100 Subject: [PATCH] add ability to reserve timers so they don't get used for motors --- .../hardware_specific/stm32/stm32_mcu.cpp | 30 ++++++++++++++++++- .../hardware_specific/stm32/stm32_mcu.h | 6 ++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/drivers/hardware_specific/stm32/stm32_mcu.cpp b/src/drivers/hardware_specific/stm32/stm32_mcu.cpp index 1a73ddfa..ed9da6be 100644 --- a/src/drivers/hardware_specific/stm32/stm32_mcu.cpp +++ b/src/drivers/hardware_specific/stm32/stm32_mcu.cpp @@ -27,6 +27,10 @@ int numTimersUsed = 0; TIM_HandleTypeDef* timersUsed[SIMPLEFOC_STM32_MAX_TIMERSUSED]; +// reserve timers for other uses, so SimpleFOC doesn't use them for motors +int numTimersReserved = 0; +TIM_TypeDef* reservedTimers[SIMPLEFOC_STM32_MAX_TIMERSRESERVED]; + // track drivers initialized via SimpleFOC - used to know which timer channels are used int numMotorsUsed = 0; STM32DriverParams* motorsUsed[SIMPLEFOC_STM32_MAX_MOTORSUSED]; @@ -38,6 +42,16 @@ int stm32_getNumTimersUsed() { int stm32_getNumMotorsUsed() { return numMotorsUsed; } +int stm32_getNumTimersReserved() { + return numTimersReserved; +} +bool stm32_isTimerReserved(TIM_TypeDef* timer) { + for (int i=0; iperipheral)) { + return true; + } for (int i=0; itimers_handle[j] == NULL) break; @@ -65,7 +82,14 @@ TIM_HandleTypeDef* stm32_getTimer(PinMap* timer) { } return NULL; } - +bool stm32_reserveTimer(TIM_TypeDef* timer) { + if (numTimersReserved >= SIMPLEFOC_STM32_MAX_TIMERSRESERVED) { + SIMPLEFOC_DEBUG("STM32-DRV: ERR: too many timers reserved"); + return false; + } + reservedTimers[numTimersReserved++] = timer; + return true; +} // function to get a timer handle given the pinmap entry of the pin you want to use // after calling this function, the timer is marked as used by SimpleFOC TIM_HandleTypeDef* stm32_useTimer(PinMap* timer) { @@ -75,6 +99,10 @@ TIM_HandleTypeDef* stm32_useTimer(PinMap* timer) { SIMPLEFOC_DEBUG("STM32-DRV: ERR: too many timers used"); return NULL; } + if (stm32_isTimerReserved((TIM_TypeDef*)timer->peripheral)) { + SIMPLEFOC_DEBUG("STM32-DRV: ERR: timer reserved"); + return NULL; + } handle = new TIM_HandleTypeDef(); handle->Instance = (TIM_TypeDef*)timer->peripheral; handle->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED; diff --git a/src/drivers/hardware_specific/stm32/stm32_mcu.h b/src/drivers/hardware_specific/stm32/stm32_mcu.h index ee0bf569..feee6ba2 100644 --- a/src/drivers/hardware_specific/stm32/stm32_mcu.h +++ b/src/drivers/hardware_specific/stm32/stm32_mcu.h @@ -8,6 +8,9 @@ #ifndef SIMPLEFOC_STM32_MAX_TIMERSUSED #define SIMPLEFOC_STM32_MAX_TIMERSUSED 6 #endif +#ifndef SIMPLEFOC_STM32_MAX_TIMERSRESERVED +#define SIMPLEFOC_STM32_MAX_TIMERSRESERVED 4 +#endif #ifndef SIMPLEFOC_STM32_MAX_MOTORSUSED #define SIMPLEFOC_STM32_MAX_MOTORSUSED 4 #endif @@ -53,11 +56,14 @@ typedef struct STM32DriverParams { // timer allocation functions int stm32_getNumTimersUsed(); int stm32_getNumMotorsUsed(); +int stm32_getNumTimersReserved(); STM32DriverParams* stm32_getMotorUsed(int index); bool stm32_isTimerUsed(TIM_HandleTypeDef* timer); bool stm32_isChannelUsed(PinMap* pin); +bool stm32_isTimerReserved(TIM_TypeDef* timer); TIM_HandleTypeDef* stm32_getTimer(PinMap* timer); TIM_HandleTypeDef* stm32_useTimer(PinMap* timer); +bool stm32_reserveTimer(TIM_TypeDef* timer); void stm32_pause(STM32DriverParams* params); void stm32_resume(STM32DriverParams* params);