Skip to content

Commit

Permalink
add ability to reserve timers so they don't get used for motors
Browse files Browse the repository at this point in the history
  • Loading branch information
runger1101001 committed Nov 7, 2024
1 parent 4d8fa4a commit 6473a17
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/drivers/hardware_specific/stm32/stm32_mcu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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; i<numTimersReserved; i++) {
if (reservedTimers[i] == timer)
return true;
}
return false;
}
bool stm32_isTimerUsed(TIM_HandleTypeDef* timer) {
for (int i=0; i<numTimersUsed; i++) {
if (timersUsed[i] == timer)
Expand All @@ -49,6 +63,9 @@ STM32DriverParams* stm32_getMotorUsed(int index) {
return motorsUsed[index];
}
bool stm32_isChannelUsed(PinMap* pin) {
if (stm32_isTimerReserved((TIM_TypeDef*)pin->peripheral)) {
return true;
}
for (int i=0; i<numMotorsUsed; i++) {
for (int j=0; j<6; j++) {
if (motorsUsed[i]->timers_handle[j] == NULL) break;
Expand All @@ -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) {
Expand All @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/drivers/hardware_specific/stm32/stm32_mcu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6473a17

Please sign in to comment.