4
4
#include " ./stm32_timerutils.h"
5
5
#include " ./stm32_searchtimers.h"
6
6
7
- #if defined(_STM32_DEF_) || defined(TARGET_PORTENTA_H7 )
7
+ #if defined(_STM32_DEF_) || defined(TARGET_STM32H7 )
8
8
9
9
#pragma message("")
10
10
#pragma message("SimpleFOC: compiling for STM32")
@@ -146,11 +146,24 @@ TIM_HandleTypeDef* stm32_initPinPWM(uint32_t PWM_freq, PinMap* timer, uint32_t m
146
146
if (timer==NULL )
147
147
return NULL ;
148
148
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
149
154
if (handle==NULL ) {
150
155
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
+
152
166
}
153
- uint32_t channel = STM_PIN_CHANNEL (timer->function );
154
167
TIM_OC_InitTypeDef channelOC;
155
168
channelOC.OCMode = TIM_OCMODE_PWM1;
156
169
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
171
184
if (IS_TIM_BREAK_INSTANCE (handle->Instance )) {
172
185
__HAL_TIM_MOE_ENABLE (handle);
173
186
}
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
179
187
return handle;
180
188
}
181
189
@@ -342,10 +350,7 @@ void* _configure1PWM(long pwm_frequency, const int pinA) {
342
350
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
343
351
}
344
352
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
349
354
350
355
int pins[1 ] = { pinA };
351
356
PinMap* pinTimers[1 ] = { NULL };
@@ -382,10 +387,7 @@ void* _configure2PWM(long pwm_frequency, const int pinA, const int pinB) {
382
387
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
383
388
}
384
389
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
389
391
390
392
int pins[2 ] = { pinA, pinB };
391
393
PinMap* pinTimers[2 ] = { NULL , NULL };
@@ -404,7 +406,7 @@ void* _configure2PWM(long pwm_frequency, const int pinA, const int pinB) {
404
406
.timers_handle = { HT1, HT2 },
405
407
.channels = { channel1, channel2 },
406
408
.llchannels = { stm32_getLLChannel (pinTimers[0 ]), stm32_getLLChannel (pinTimers[1 ]) },
407
- .pwm_frequency = pwm_frequency,
409
+ .pwm_frequency = pwm_frequency, // TODO set to actual frequency
408
410
.num_timers = stm32_countTimers (timers, 2 ),
409
411
.master_timer = NULL
410
412
};
@@ -425,10 +427,7 @@ void* _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const in
425
427
SIMPLEFOC_DEBUG (" STM32-DRV: ERR: too many drivers used" );
426
428
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
427
429
}
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
432
431
433
432
int pins[3 ] = { pinA, pinB, pinC };
434
433
PinMap* pinTimers[3 ] = { NULL , NULL , NULL };
@@ -469,10 +468,7 @@ void* _configure4PWM(long pwm_frequency,const int pinA, const int pinB, const in
469
468
SIMPLEFOC_DEBUG (" STM32-DRV: ERR: too many drivers used" );
470
469
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
471
470
}
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
476
472
477
473
int pins[4 ] = { pinA, pinB, pinC, pinD };
478
474
PinMap* pinTimers[4 ] = { NULL , NULL , NULL , NULL };
@@ -570,10 +566,7 @@ void* _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, cons
570
566
SIMPLEFOC_DEBUG (" STM32-DRV: ERR: too many drivers used" );
571
567
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
572
568
}
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
577
570
578
571
// find configuration
579
572
int pins[6 ] = { pinA_h, pinA_l, pinB_h, pinB_l, pinC_h, pinC_l };
0 commit comments