Skip to content

Commit 3915891

Browse files
committed
add 1-PWM mode to STM32
1 parent a7cec89 commit 3915891

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/drivers/hardware_specific/stm32_mcu.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,36 @@ int findBestTimerCombination(int numPins, int pins[], PinMap* pinTimers[]) {
496496

497497

498498

499+
void* _configure1PWM(long pwm_frequency, const int pinA) {
500+
if (numTimerPinsUsed+1 > SIMPLEFOC_STM32_MAX_PINTIMERSUSED) {
501+
SIMPLEFOC_DEBUG("STM32-DRV: ERR: too many pins used");
502+
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
503+
}
504+
505+
if( !pwm_frequency || !_isset(pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25khz
506+
else pwm_frequency = _constrain(pwm_frequency, 0, _PWM_FREQUENCY_MAX); // constrain to 50kHz max
507+
// center-aligned frequency is uses two periods
508+
pwm_frequency *=2;
509+
510+
int pins[1] = { pinA };
511+
PinMap* pinTimers[1] = { NP };
512+
if (findBestTimerCombination(1, pins, pinTimers)<0)
513+
return (STM32DriverParams*)SIMPLEFOC_DRIVER_INIT_FAILED;
514+
515+
HardwareTimer* HT1 = _initPinPWM(pwm_frequency, pinTimers[0]);\
516+
// allign the timers
517+
_alignTimersNew();
518+
519+
uint32_t channel1 = STM_PIN_CHANNEL(pinTimers[0]->function);
499520

521+
STM32DriverParams* params = new STM32DriverParams {
522+
.timers = { HT1 },
523+
.channels = { channel1 },
524+
.pwm_frequency = pwm_frequency
525+
};
526+
timerPinsUsed[numTimerPinsUsed++] = pinTimers[0];
527+
return params;
528+
}
500529

501530

502531

@@ -629,6 +658,16 @@ void* _configure4PWM(long pwm_frequency,const int pinA, const int pinB, const in
629658

630659

631660

661+
// function setting the pwm duty cycle to the hardware
662+
// - DC motor - 1PWM setting
663+
// - hardware speciffic
664+
void _writeDutyCycle1PWM(float dc_a, void* params){
665+
// transform duty cycle from [0,1] to [0,255]
666+
_setPwm(((STM32DriverParams*)params)->timers[0], ((STM32DriverParams*)params)->channels[0], _PWM_RANGE*dc_a, _PWM_RESOLUTION);
667+
}
668+
669+
670+
632671
// function setting the pwm duty cycle to the hardware
633672
// - Stepper motor - 2PWM setting
634673
//- hardware speciffic

0 commit comments

Comments
 (0)