Skip to content

Commit 00b22fb

Browse files
committed
FEAT added true pwm frequency stm + FIX driver init delay
1 parent 74aacac commit 00b22fb

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

src/drivers/BLDCDriver3PWM.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ void BLDCDriver3PWM::disable()
3535

3636
// init hardware pins
3737
int BLDCDriver3PWM::init() {
38+
// a bit of separation
39+
_delay(1000);
3840

3941
// PWM pins
4042
pinMode(pwmA, OUTPUT);

src/drivers/BLDCDriver6PWM.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ void BLDCDriver6PWM::disable()
4141

4242
// init hardware pins
4343
int BLDCDriver6PWM::init() {
44+
// a bit of separation
45+
_delay(1000);
4446

4547
// PWM pins
4648
pinMode(pwmA_l, OUTPUT);

src/drivers/StepperDriver2PWM.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ void StepperDriver2PWM::disable()
4141

4242
// init hardware pins
4343
int StepperDriver2PWM::init() {
44+
// a bit of separation
45+
_delay(1000);
4446

4547
// PWM pins
4648
pinMode(pwm1, OUTPUT);

src/drivers/StepperDriver4PWM.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ void StepperDriver4PWM::disable()
3939

4040
// init hardware pins
4141
int StepperDriver4PWM::init() {
42+
// a bit of separation
43+
_delay(1000);
4244

4345
// PWM pins
4446
pinMode(pwm1A, OUTPUT);

src/drivers/hardware_specific/stm32_mcu.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
#define _PWM_RESOLUTION 12 // 12bit
77
#define _PWM_RANGE 4095.0// 2^12 -1 = 4095
8-
#define _PWM_FREQUENCY 50000 // 50khz
8+
#define _PWM_FREQUENCY 25000 // 25khz
9+
#define _PWM_FREQUENCY_MAX 50000 // 50khz
910

1011

1112
#define _HARDWARE_6PWM 1
@@ -188,8 +189,11 @@ int _interfaceType(const int pinA_h, const int pinA_l, const int pinB_h, const
188189
// - Stepper motor - 2PWM setting
189190
// - hardware speciffic
190191
void _configure2PWM(long pwm_frequency,const int pinA, const int pinB) {
191-
if( !pwm_frequency || pwm_frequency == NOT_SET) pwm_frequency = _PWM_FREQUENCY; // default frequency 50khz
192-
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY); // constrain to 50kHz max
192+
if( !pwm_frequency || pwm_frequency == NOT_SET) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
193+
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to 50kHz max
194+
// center-aligned frequency is uses two periods
195+
pwm_frequency *=2;
196+
193197
HardwareTimer* HT1 = _initPinPWM(pwm_frequency, pinA);
194198
HardwareTimer* HT2 = _initPinPWM(pwm_frequency, pinB);
195199
// allign the timers
@@ -201,8 +205,11 @@ void _configure2PWM(long pwm_frequency,const int pinA, const int pinB) {
201205
// - BLDC motor - 3PWM setting
202206
// - hardware speciffic
203207
void _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const int pinC) {
204-
if( !pwm_frequency || pwm_frequency == NOT_SET) pwm_frequency = _PWM_FREQUENCY; // default frequency 50khz
205-
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY); // constrain to 50kHz max
208+
if( !pwm_frequency || pwm_frequency == NOT_SET) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
209+
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to 50kHz max
210+
// center-aligned frequency is uses two periods
211+
pwm_frequency *=2;
212+
206213
HardwareTimer* HT1 = _initPinPWM(pwm_frequency, pinA);
207214
HardwareTimer* HT2 = _initPinPWM(pwm_frequency, pinB);
208215
HardwareTimer* HT3 = _initPinPWM(pwm_frequency, pinC);
@@ -214,8 +221,11 @@ void _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const int
214221
// - Stepper motor - 4PWM setting
215222
// - hardware speciffic
216223
void _configure4PWM(long pwm_frequency,const int pinA, const int pinB, const int pinC, const int pinD) {
217-
if( !pwm_frequency || pwm_frequency == NOT_SET) pwm_frequency = _PWM_FREQUENCY; // default frequency 50khz
218-
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY); // constrain to 50kHz max
224+
if( !pwm_frequency || pwm_frequency == NOT_SET) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
225+
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to 50kHz max
226+
// center-aligned frequency is uses two periods
227+
pwm_frequency *=2;
228+
219229
HardwareTimer* HT1 = _initPinPWM(pwm_frequency, pinA);
220230
HardwareTimer* HT2 = _initPinPWM(pwm_frequency, pinB);
221231
HardwareTimer* HT3 = _initPinPWM(pwm_frequency, pinC);
@@ -262,8 +272,10 @@ void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, in
262272
// - BLDC driver - 6PWM setting
263273
// - hardware specific
264274
int _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, const int pinA_l, const int pinB_h, const int pinB_l, const int pinC_h, const int pinC_l){
265-
if( !pwm_frequency || pwm_frequency == NOT_SET) pwm_frequency = _PWM_FREQUENCY; // default frequency 50khz
266-
else pwm_frequency = _constrain(pwm_frequency, 0, 100000); // constrain to 100kHz max
275+
if( !pwm_frequency || pwm_frequency == NOT_SET) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
276+
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to |%0kHz max
277+
// center-aligned frequency is uses two periods
278+
pwm_frequency *=2;
267279

268280
// find configuration
269281
int config = _interfaceType(pinA_h, pinA_l, pinB_h, pinB_l, pinC_h, pinC_l);

0 commit comments

Comments
 (0)