44#include " ./stm32_timerutils.h"
55#include " ./stm32_searchtimers.h"
66
7- #if defined(_STM32_DEF_) || defined(TARGET_PORTENTA_H7 )
7+ #if defined(_STM32_DEF_) || defined(TARGET_STM32H7 )
88
99#pragma message("")
1010#pragma message("SimpleFOC: compiling for STM32")
@@ -146,11 +146,24 @@ TIM_HandleTypeDef* stm32_initPinPWM(uint32_t PWM_freq, PinMap* timer, uint32_t m
146146 if (timer==NULL )
147147 return NULL ;
148148 TIM_HandleTypeDef* handle = stm32_getTimer (timer);
149+ uint32_t channel = STM_PIN_CHANNEL (timer->function );
150+ #ifdef SIMPLEFOC_STM32_DEBUG
151+ SIMPLEFOC_DEBUG (" STM32-DRV: Configuring timer " , (int )stm32_getTimerNumber (handle->Instance ));
152+ SIMPLEFOC_DEBUG (" STM32-DRV: Configuring channel " , (int )channel);
153+ #endif
149154 if (handle==NULL ) {
150155 handle = stm32_useTimer (timer);
151- stm32_setClockAndARR (handle, PWM_freq); // TODO add checks for PWM frequency limits
156+ uint32_t arr = stm32_setClockAndARR (handle, PWM_freq);
157+ if (arr<SIMPLEFOC_STM32_MIN_RESOLUTION) {
158+ SIMPLEFOC_DEBUG (" STM32-DRV: WARN timer resolution too low (<8bit): " , (int )arr+1 );
159+ }
160+ else {
161+ #ifdef SIMPLEFOC_STM32_DEBUG
162+ SIMPLEFOC_DEBUG (" STM32-DRV: Timer resolution set to: " , (int )arr+1 );
163+ #endif
164+ }
165+
152166 }
153- uint32_t channel = STM_PIN_CHANNEL (timer->function );
154167 TIM_OC_InitTypeDef channelOC;
155168 channelOC.OCMode = TIM_OCMODE_PWM1;
156169 channelOC.Pulse = 0 ; // __HAL_TIM_GET_COMPARE(handle, channel);
@@ -171,11 +184,6 @@ TIM_HandleTypeDef* stm32_initPinPWM(uint32_t PWM_freq, PinMap* timer, uint32_t m
171184 if (IS_TIM_BREAK_INSTANCE (handle->Instance )) {
172185 __HAL_TIM_MOE_ENABLE (handle);
173186 }
174-
175- #ifdef SIMPLEFOC_STM32_DEBUG
176- SIMPLEFOC_DEBUG (" STM32-DRV: Configuring timer " , (int )stm32_getTimerNumber (handle->Instance ));
177- SIMPLEFOC_DEBUG (" STM32-DRV: Configuring channel " , (int )channel);
178- #endif
179187 return handle;
180188}
181189
@@ -342,10 +350,7 @@ void* _configure1PWM(long pwm_frequency, const int pinA) {
342350 return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
343351 }
344352
345- if ( !pwm_frequency || !_isset (pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
346- else pwm_frequency = _constrain (pwm_frequency, 0 , _PWM_FREQUENCY_MAX); // constrain to 50kHz max
347- // center-aligned frequency is uses two periods
348- pwm_frequency *=2 ;
353+ if ( !pwm_frequency || !_isset (pwm_frequency) ) pwm_frequency = SIMPLEFOC_STM32_PWM_FREQUENCY; // default frequency 25khz
349354
350355 int pins[1 ] = { pinA };
351356 PinMap* pinTimers[1 ] = { NULL };
@@ -382,10 +387,7 @@ void* _configure2PWM(long pwm_frequency, const int pinA, const int pinB) {
382387 return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
383388 }
384389
385- if ( !pwm_frequency || !_isset (pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
386- else pwm_frequency = _constrain (pwm_frequency, 0 , _PWM_FREQUENCY_MAX); // constrain to 50kHz max
387- // center-aligned frequency is uses two periods
388- pwm_frequency *=2 ;
390+ if ( !pwm_frequency || !_isset (pwm_frequency) ) pwm_frequency = SIMPLEFOC_STM32_PWM_FREQUENCY; // default frequency 25khz
389391
390392 int pins[2 ] = { pinA, pinB };
391393 PinMap* pinTimers[2 ] = { NULL , NULL };
@@ -404,7 +406,7 @@ void* _configure2PWM(long pwm_frequency, const int pinA, const int pinB) {
404406 .timers_handle = { HT1, HT2 },
405407 .channels = { channel1, channel2 },
406408 .llchannels = { stm32_getLLChannel (pinTimers[0 ]), stm32_getLLChannel (pinTimers[1 ]) },
407- .pwm_frequency = pwm_frequency,
409+ .pwm_frequency = pwm_frequency, // TODO set to actual frequency
408410 .num_timers = stm32_countTimers (timers, 2 ),
409411 .master_timer = NULL
410412 };
@@ -425,10 +427,7 @@ void* _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const in
425427 SIMPLEFOC_DEBUG (" STM32-DRV: ERR: too many drivers used" );
426428 return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
427429 }
428- if ( !pwm_frequency || !_isset (pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
429- else pwm_frequency = _constrain (pwm_frequency, 0 , _PWM_FREQUENCY_MAX); // constrain to 50kHz max
430- // center-aligned frequency is uses two periods
431- // pwm_frequency *=2;
430+ if ( !pwm_frequency || !_isset (pwm_frequency) ) pwm_frequency = SIMPLEFOC_STM32_PWM_FREQUENCY; // default frequency 25khz
432431
433432 int pins[3 ] = { pinA, pinB, pinC };
434433 PinMap* pinTimers[3 ] = { NULL , NULL , NULL };
@@ -469,10 +468,7 @@ void* _configure4PWM(long pwm_frequency,const int pinA, const int pinB, const in
469468 SIMPLEFOC_DEBUG (" STM32-DRV: ERR: too many drivers used" );
470469 return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
471470 }
472- if ( !pwm_frequency || !_isset (pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
473- else pwm_frequency = _constrain (pwm_frequency, 0 , _PWM_FREQUENCY_MAX); // constrain to 50kHz max
474- // center-aligned frequency is uses two periods
475- pwm_frequency *=2 ;
471+ if ( !pwm_frequency || !_isset (pwm_frequency) ) pwm_frequency = SIMPLEFOC_STM32_PWM_FREQUENCY; // default frequency 25khz
476472
477473 int pins[4 ] = { pinA, pinB, pinC, pinD };
478474 PinMap* pinTimers[4 ] = { NULL , NULL , NULL , NULL };
@@ -570,10 +566,7 @@ void* _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, cons
570566 SIMPLEFOC_DEBUG (" STM32-DRV: ERR: too many drivers used" );
571567 return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
572568 }
573- if ( !pwm_frequency || !_isset (pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
574- else pwm_frequency = _constrain (pwm_frequency, 0 , _PWM_FREQUENCY_MAX); // constrain to |%0kHz max
575- // center-aligned frequency is uses two periods
576- pwm_frequency *=2 ;
569+ if ( !pwm_frequency || !_isset (pwm_frequency) ) pwm_frequency = SIMPLEFOC_STM32_PWM_FREQUENCY; // default frequency 25khz
577570
578571 // find configuration
579572 int pins[6 ] = { pinA_h, pinA_l, pinB_h, pinB_l, pinC_h, pinC_l };
0 commit comments