Skip to content

Commit

Permalink
change target to include all the mbed STM32H7s
Browse files Browse the repository at this point in the history
  • Loading branch information
runger1101001 committed Aug 30, 2024
1 parent 5569f73 commit 3d26880
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 45 deletions.
51 changes: 22 additions & 29 deletions src/drivers/hardware_specific/stm32/stm32_mcu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "./stm32_timerutils.h"
#include "./stm32_searchtimers.h"

#if defined(_STM32_DEF_) || defined(TARGET_PORTENTA_H7)
#if defined(_STM32_DEF_) || defined(TARGET_STM32H7)

#pragma message("")
#pragma message("SimpleFOC: compiling for STM32")
Expand Down Expand Up @@ -146,11 +146,24 @@ TIM_HandleTypeDef* stm32_initPinPWM(uint32_t PWM_freq, PinMap* timer, uint32_t m
if (timer==NULL)
return NULL;
TIM_HandleTypeDef* handle = stm32_getTimer(timer);
uint32_t channel = STM_PIN_CHANNEL(timer->function);
#ifdef SIMPLEFOC_STM32_DEBUG
SIMPLEFOC_DEBUG("STM32-DRV: Configuring timer ", (int)stm32_getTimerNumber(handle->Instance));
SIMPLEFOC_DEBUG("STM32-DRV: Configuring channel ", (int)channel);
#endif
if (handle==NULL) {
handle = stm32_useTimer(timer);
stm32_setClockAndARR(handle, PWM_freq); // TODO add checks for PWM frequency limits
uint32_t arr = stm32_setClockAndARR(handle, PWM_freq);
if (arr<SIMPLEFOC_STM32_MIN_RESOLUTION) {
SIMPLEFOC_DEBUG("STM32-DRV: WARN timer resolution too low (<8bit): ", (int)arr+1);
}
else {
#ifdef SIMPLEFOC_STM32_DEBUG
SIMPLEFOC_DEBUG("STM32-DRV: Timer resolution set to: ", (int)arr+1);
#endif
}

}
uint32_t channel = STM_PIN_CHANNEL(timer->function);
TIM_OC_InitTypeDef channelOC;
channelOC.OCMode = TIM_OCMODE_PWM1;
channelOC.Pulse = 0; //__HAL_TIM_GET_COMPARE(handle, channel);
Expand All @@ -171,11 +184,6 @@ TIM_HandleTypeDef* stm32_initPinPWM(uint32_t PWM_freq, PinMap* timer, uint32_t m
if (IS_TIM_BREAK_INSTANCE(handle->Instance)) {
__HAL_TIM_MOE_ENABLE(handle);
}

#ifdef SIMPLEFOC_STM32_DEBUG
SIMPLEFOC_DEBUG("STM32-DRV: Configuring timer ", (int)stm32_getTimerNumber(handle->Instance));
SIMPLEFOC_DEBUG("STM32-DRV: Configuring channel ", (int)channel);
#endif
return handle;
}

Expand Down Expand Up @@ -342,10 +350,7 @@ void* _configure1PWM(long pwm_frequency, const int pinA) {
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
}

if( !pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to 50kHz max
// center-aligned frequency is uses two periods
pwm_frequency *=2;
if( !pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = SIMPLEFOC_STM32_PWM_FREQUENCY; // default frequency 25khz

int pins[1] = { pinA };
PinMap* pinTimers[1] = { NULL };
Expand Down Expand Up @@ -382,10 +387,7 @@ void* _configure2PWM(long pwm_frequency, const int pinA, const int pinB) {
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
}

if( !pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to 50kHz max
// center-aligned frequency is uses two periods
pwm_frequency *=2;
if( !pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = SIMPLEFOC_STM32_PWM_FREQUENCY; // default frequency 25khz

int pins[2] = { pinA, pinB };
PinMap* pinTimers[2] = { NULL, NULL };
Expand All @@ -404,7 +406,7 @@ void* _configure2PWM(long pwm_frequency, const int pinA, const int pinB) {
.timers_handle = { HT1, HT2 },
.channels = { channel1, channel2 },
.llchannels = { stm32_getLLChannel(pinTimers[0]), stm32_getLLChannel(pinTimers[1]) },
.pwm_frequency = pwm_frequency,
.pwm_frequency = pwm_frequency, // TODO set to actual frequency
.num_timers = stm32_countTimers(timers, 2),
.master_timer = NULL
};
Expand All @@ -425,10 +427,7 @@ void* _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const in
SIMPLEFOC_DEBUG("STM32-DRV: ERR: too many drivers used");
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
}
if( !pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to 50kHz max
// center-aligned frequency is uses two periods
//pwm_frequency *=2;
if( !pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = SIMPLEFOC_STM32_PWM_FREQUENCY; // default frequency 25khz

int pins[3] = { pinA, pinB, pinC };
PinMap* pinTimers[3] = { NULL, NULL, NULL };
Expand Down Expand Up @@ -469,10 +468,7 @@ void* _configure4PWM(long pwm_frequency,const int pinA, const int pinB, const in
SIMPLEFOC_DEBUG("STM32-DRV: ERR: too many drivers used");
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
}
if( !pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to 50kHz max
// center-aligned frequency is uses two periods
pwm_frequency *=2;
if( !pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = SIMPLEFOC_STM32_PWM_FREQUENCY; // default frequency 25khz

int pins[4] = { pinA, pinB, pinC, pinD };
PinMap* pinTimers[4] = { NULL, NULL, NULL, NULL };
Expand Down Expand Up @@ -570,10 +566,7 @@ void* _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, cons
SIMPLEFOC_DEBUG("STM32-DRV: ERR: too many drivers used");
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
}
if( !pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to |%0kHz max
// center-aligned frequency is uses two periods
pwm_frequency *=2;
if( !pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = SIMPLEFOC_STM32_PWM_FREQUENCY; // default frequency 25khz

// find configuration
int pins[6] = { pinA_h, pinA_l, pinB_h, pinB_l, pinC_h, pinC_l };
Expand Down
15 changes: 8 additions & 7 deletions src/drivers/hardware_specific/stm32/stm32_mcu.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "../../hardware_api.h"

#if defined(_STM32_DEF_) || defined(TARGET_PORTENTA_H7)
#if defined(_STM32_DEF_) || defined(TARGET_STM32H7)

#ifndef SIMPLEFOC_STM32_MAX_TIMERSUSED
#define SIMPLEFOC_STM32_MAX_TIMERSUSED 6
Expand All @@ -23,12 +23,13 @@
#endif



// default pwm parameters
#define _PWM_RESOLUTION 12 // 12bit
#define _PWM_RANGE 4095.0f // 2^12 -1 = 4095
#define _PWM_FREQUENCY 25000 // 25khz
#define _PWM_FREQUENCY_MAX 50000 // 50khz
/**
* No limits are placed on PWM frequency, so very fast or very slow frequencies can be set.
* A warning is displayed to debug if you get less than 8bit resolution for the PWM duty cycle.
* If no pwm_frequency is set, the default value is 25kHz.
*/
#define SIMPLEFOC_STM32_PWM_FREQUENCY 25000 // 25khz
#define SIMPLEFOC_STM32_MIN_RESOLUTION 255

// 6pwm parameters
#define _HARDWARE_6PWM 1
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/hardware_specific/stm32/stm32_searchtimers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "./stm32_searchtimers.h"
#include "./stm32_timerutils.h"

#if defined(_STM32_DEF_) || defined(TARGET_PORTENTA_H7)
#if defined(_STM32_DEF_) || defined(TARGET_STM32H7)



Expand Down
2 changes: 1 addition & 1 deletion src/drivers/hardware_specific/stm32/stm32_searchtimers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "./stm32_mcu.h"

#if defined(_STM32_DEF_) || defined(TARGET_PORTENTA_H7)
#if defined(_STM32_DEF_) || defined(TARGET_STM32H7)


int stm32_findBestTimerCombination(int numPins, int pins[], PinMap* pinTimers[]);
Expand Down
30 changes: 24 additions & 6 deletions src/drivers/hardware_specific/stm32/stm32_timerutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
#include "./stm32_timerutils.h"
#include <Arduino.h>

#if defined(_STM32_DEF_) || defined(TARGET_PORTENTA_H7)
#if defined(_STM32_DEF_) || defined(TARGET_STM32H7)


void stm32_pauseTimer(TIM_HandleTypeDef* handle){
/* Disable timer unconditionally. Required to guarantee timer is stopped,
* even if some channels are still running */
LL_TIM_DisableCounter(handle->Instance);
// handle->State = HAL_TIM_STATE_READY;
// on advanced control timers there is also the option of using the brake or the MOE?
// TIM1->EGR |= TIM_EGR_BG; // break generation
}


Expand Down Expand Up @@ -211,7 +213,7 @@ int stm32_getInternalSourceTrigger(TIM_HandleTypeDef* master, TIM_HandleTypeDef*
#endif
return -1;
}
#elif defined(STM32F4xx) || defined(STM32F1xx) || defined(STM32L4xx) || defined(STM32F7xx)
#elif defined(STM32F4xx) || defined(STM32F1xx) || defined(STM32L4xx) || defined(STM32F7xx) || defined(TARGET_STM32H7)

// function finds the appropriate timer source trigger for the master/slave timer combination
// returns -1 if no trigger source is found
Expand Down Expand Up @@ -250,7 +252,7 @@ int stm32_getInternalSourceTrigger(TIM_HandleTypeDef* master, TIM_HandleTypeDef*
#if defined(TIM8)
else if(TIM_slave == TIM8) return LL_TIM_TS_ITR1;
#endif
#if defined(TIM5)
#if defined(TIM5) && !defined(TARGET_STM32H7)
else if(TIM_slave == TIM5) return LL_TIM_TS_ITR0;
#endif
}
Expand All @@ -266,9 +268,12 @@ int stm32_getInternalSourceTrigger(TIM_HandleTypeDef* master, TIM_HandleTypeDef*
#if defined(TIM4)
else if(TIM_slave == TIM4) return LL_TIM_TS_ITR2;
#endif
#if defined(TIM5)
#if defined(TIM5) && !defined(TARGET_STM32H7)
else if(TIM_slave == TIM5) return LL_TIM_TS_ITR1;
#endif
#if defined(TIM5) && defined(TARGET_STM32H7)
else if(TIM_slave == TIM5) return LL_TIM_TS_ITR2;
#endif
}
#endif
#if defined(TIM4) && defined(LL_TIM_TS_ITR3)
Expand All @@ -285,9 +290,12 @@ int stm32_getInternalSourceTrigger(TIM_HandleTypeDef* master, TIM_HandleTypeDef*
#if defined(TIM8)
else if(TIM_slave == TIM8) return LL_TIM_TS_ITR2;
#endif
#if defined(TIM5)
#if defined(TIM5) && !defined(TARGET_STM32H7)
else if(TIM_slave == TIM5) return LL_TIM_TS_ITR1;
#endif
#if defined(TIM5) && defined(TARGET_STM32H7)
else if(TIM_slave == TIM5) return LL_TIM_TS_ITR3;
#endif
}
#endif
#if defined(TIM5)
Expand Down Expand Up @@ -318,6 +326,16 @@ int stm32_getInternalSourceTrigger(TIM_HandleTypeDef* master, TIM_HandleTypeDef*
#endif
}
#endif
#if defined(TIM15) && defined(TARGET_STM32H7)
else if (TIM_master == TIM15){
#if defined(TIM1)
if(TIM_slave == TIM1) return LL_TIM_TS_ITR0;
#endif
#if defined(TIM3)
if(TIM_slave == TIM3) return LL_TIM_TS_ITR2;
#endif
}
#endif
return -1; // combination not supported
}
#else
Expand Down Expand Up @@ -484,7 +502,7 @@ uint32_t stm32_getTimerClockFreq(TIM_HandleTypeDef *handle) {
return 0;
}

#if defined(STM32H7xx)
#if defined(STM32H7xx) || defined(TARGET_STM32H7)
/* When TIMPRE bit of the RCC_CFGR register is reset,
* if APBx prescaler is 1 or 2 then TIMxCLK = HCLK,
* otherwise TIMxCLK = 2x PCLKx.
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/hardware_specific/stm32/stm32_timerutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "./stm32_mcu.h"

#if defined(_STM32_DEF_) || defined(TARGET_PORTENTA_H7)
#if defined(_STM32_DEF_) || defined(TARGET_STM32H7)

void stm32_pauseTimer(TIM_HandleTypeDef* handle);
void stm32_resumeTimer(TIM_HandleTypeDef* handle);
Expand Down

0 comments on commit 3d26880

Please sign in to comment.