Skip to content

Commit 3d26880

Browse files
committed
change target to include all the mbed STM32H7s
1 parent 5569f73 commit 3d26880

File tree

6 files changed

+57
-45
lines changed

6 files changed

+57
-45
lines changed

src/drivers/hardware_specific/stm32/stm32_mcu.cpp

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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 };

src/drivers/hardware_specific/stm32/stm32_mcu.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "../../hardware_api.h"
44

5-
#if defined(_STM32_DEF_) || defined(TARGET_PORTENTA_H7)
5+
#if defined(_STM32_DEF_) || defined(TARGET_STM32H7)
66

77
#ifndef SIMPLEFOC_STM32_MAX_TIMERSUSED
88
#define SIMPLEFOC_STM32_MAX_TIMERSUSED 6
@@ -23,12 +23,13 @@
2323
#endif
2424

2525

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

3334
// 6pwm parameters
3435
#define _HARDWARE_6PWM 1

src/drivers/hardware_specific/stm32/stm32_searchtimers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "./stm32_searchtimers.h"
33
#include "./stm32_timerutils.h"
44

5-
#if defined(_STM32_DEF_) || defined(TARGET_PORTENTA_H7)
5+
#if defined(_STM32_DEF_) || defined(TARGET_STM32H7)
66

77

88

src/drivers/hardware_specific/stm32/stm32_searchtimers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "./stm32_mcu.h"
44

5-
#if defined(_STM32_DEF_) || defined(TARGET_PORTENTA_H7)
5+
#if defined(_STM32_DEF_) || defined(TARGET_STM32H7)
66

77

88
int stm32_findBestTimerCombination(int numPins, int pins[], PinMap* pinTimers[]);

src/drivers/hardware_specific/stm32/stm32_timerutils.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
#include "./stm32_timerutils.h"
33
#include <Arduino.h>
44

5-
#if defined(_STM32_DEF_) || defined(TARGET_PORTENTA_H7)
5+
#if defined(_STM32_DEF_) || defined(TARGET_STM32H7)
66

77

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

1517

@@ -211,7 +213,7 @@ int stm32_getInternalSourceTrigger(TIM_HandleTypeDef* master, TIM_HandleTypeDef*
211213
#endif
212214
return -1;
213215
}
214-
#elif defined(STM32F4xx) || defined(STM32F1xx) || defined(STM32L4xx) || defined(STM32F7xx)
216+
#elif defined(STM32F4xx) || defined(STM32F1xx) || defined(STM32L4xx) || defined(STM32F7xx) || defined(TARGET_STM32H7)
215217

216218
// function finds the appropriate timer source trigger for the master/slave timer combination
217219
// returns -1 if no trigger source is found
@@ -250,7 +252,7 @@ int stm32_getInternalSourceTrigger(TIM_HandleTypeDef* master, TIM_HandleTypeDef*
250252
#if defined(TIM8)
251253
else if(TIM_slave == TIM8) return LL_TIM_TS_ITR1;
252254
#endif
253-
#if defined(TIM5)
255+
#if defined(TIM5) && !defined(TARGET_STM32H7)
254256
else if(TIM_slave == TIM5) return LL_TIM_TS_ITR0;
255257
#endif
256258
}
@@ -266,9 +268,12 @@ int stm32_getInternalSourceTrigger(TIM_HandleTypeDef* master, TIM_HandleTypeDef*
266268
#if defined(TIM4)
267269
else if(TIM_slave == TIM4) return LL_TIM_TS_ITR2;
268270
#endif
269-
#if defined(TIM5)
271+
#if defined(TIM5) && !defined(TARGET_STM32H7)
270272
else if(TIM_slave == TIM5) return LL_TIM_TS_ITR1;
271273
#endif
274+
#if defined(TIM5) && defined(TARGET_STM32H7)
275+
else if(TIM_slave == TIM5) return LL_TIM_TS_ITR2;
276+
#endif
272277
}
273278
#endif
274279
#if defined(TIM4) && defined(LL_TIM_TS_ITR3)
@@ -285,9 +290,12 @@ int stm32_getInternalSourceTrigger(TIM_HandleTypeDef* master, TIM_HandleTypeDef*
285290
#if defined(TIM8)
286291
else if(TIM_slave == TIM8) return LL_TIM_TS_ITR2;
287292
#endif
288-
#if defined(TIM5)
293+
#if defined(TIM5) && !defined(TARGET_STM32H7)
289294
else if(TIM_slave == TIM5) return LL_TIM_TS_ITR1;
290295
#endif
296+
#if defined(TIM5) && defined(TARGET_STM32H7)
297+
else if(TIM_slave == TIM5) return LL_TIM_TS_ITR3;
298+
#endif
291299
}
292300
#endif
293301
#if defined(TIM5)
@@ -318,6 +326,16 @@ int stm32_getInternalSourceTrigger(TIM_HandleTypeDef* master, TIM_HandleTypeDef*
318326
#endif
319327
}
320328
#endif
329+
#if defined(TIM15) && defined(TARGET_STM32H7)
330+
else if (TIM_master == TIM15){
331+
#if defined(TIM1)
332+
if(TIM_slave == TIM1) return LL_TIM_TS_ITR0;
333+
#endif
334+
#if defined(TIM3)
335+
if(TIM_slave == TIM3) return LL_TIM_TS_ITR2;
336+
#endif
337+
}
338+
#endif
321339
return -1; // combination not supported
322340
}
323341
#else
@@ -484,7 +502,7 @@ uint32_t stm32_getTimerClockFreq(TIM_HandleTypeDef *handle) {
484502
return 0;
485503
}
486504

487-
#if defined(STM32H7xx)
505+
#if defined(STM32H7xx) || defined(TARGET_STM32H7)
488506
/* When TIMPRE bit of the RCC_CFGR register is reset,
489507
* if APBx prescaler is 1 or 2 then TIMxCLK = HCLK,
490508
* otherwise TIMxCLK = 2x PCLKx.

src/drivers/hardware_specific/stm32/stm32_timerutils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include "./stm32_mcu.h"
55

6-
#if defined(_STM32_DEF_) || defined(TARGET_PORTENTA_H7)
6+
#if defined(_STM32_DEF_) || defined(TARGET_STM32H7)
77

88
void stm32_pauseTimer(TIM_HandleTypeDef* handle);
99
void stm32_resumeTimer(TIM_HandleTypeDef* handle);

0 commit comments

Comments
 (0)