🐛 Fix STM32 FastPWM

This commit is contained in:
Scott Lahteine 2021-11-30 15:04:05 -06:00
parent 9b1c0a75e1
commit 52a44eb200

View File

@ -32,26 +32,17 @@ static uint16_t timer_freq[TIMER_NUM];
void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) { void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
bool needs_freq;
PinName pin_name = digitalPinToPinName(pin); PinName pin_name = digitalPinToPinName(pin);
TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM); TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM);
HardwareTimer *HT;
TimerModes_t previousMode;
uint16_t value = v; const uint32_t index = get_timer_index(Instance);
if (invert) value = v_size - value; bool needs_freq = (HardwareTimer_Handle[index] == nullptr); // A new instance must be set to the default frequency of PWM_FREQUENCY
if (needs_freq)
uint32_t index = get_timer_index(Instance);
if (HardwareTimer_Handle[index] == nullptr) {
HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM)); HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM));
needs_freq = true; // The instance must be new set the default frequency of PWM_FREQUENCY
}
HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
uint32_t channel = STM_PIN_CHANNEL(pinmap_function(pin_name, PinMap_PWM));
previousMode = HT->getMode(channel);
HardwareTimer * const HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
const uint32_t channel = STM_PIN_CHANNEL(pinmap_function(pin_name, PinMap_PWM));
const TimerModes_t previousMode = HT->getMode(channel);
if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) if (previousMode != TIMER_OUTPUT_COMPARE_PWM1)
HT->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin); HT->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
@ -63,6 +54,7 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255
} }
// Note the resolution is sticky here, the input can be upto 16 bits and that would require RESOLUTION_16B_COMPARE_FORMAT (16) // Note the resolution is sticky here, the input can be upto 16 bits and that would require RESOLUTION_16B_COMPARE_FORMAT (16)
// If such a need were to manifest then we would need to calc the resolution based on the v_size parameter and add code for it. // If such a need were to manifest then we would need to calc the resolution based on the v_size parameter and add code for it.
const uint16_t value = invert ? v_size - v : v;
HT->setCaptureCompare(channel, value, RESOLUTION_8B_COMPARE_FORMAT); // Sets the duty, the calc is done in the library :) HT->setCaptureCompare(channel, value, RESOLUTION_8B_COMPARE_FORMAT); // Sets the duty, the calc is done in the library :)
pinmap_pinout(pin_name, PinMap_PWM); // Make sure the pin output state is set. pinmap_pinout(pin_name, PinMap_PWM); // Make sure the pin output state is set.
if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) HT->resume(); if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) HT->resume();
@ -73,12 +65,12 @@ void set_pwm_frequency(const pin_t pin, int f_desired) {
const PinName pin_name = digitalPinToPinName(pin); const PinName pin_name = digitalPinToPinName(pin);
TIM_TypeDef * const Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM); // Get HAL timer instance TIM_TypeDef * const Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM); // Get HAL timer instance
uint32_t index = get_timer_index(Instance); const uint32_t index = get_timer_index(Instance);
// Protect used timers // Protect used timers
if (index == TEMP_TIMER_NUM || index == STEP_TIMER_NUM if (index == MF_TIMER_TEMP || index == MF_TIMER_STEP
#if PULSE_TIMER_NUM != STEP_TIMER_NUM #if MF_TIMER_PULSE != MF_TIMER_STEP
|| index == PULSE_TIMER_NUM || index == MF_TIMER_PULSE
#endif #endif
) return; ) return;