|
|
@ -27,37 +27,67 @@
|
|
|
|
#include "../../inc/MarlinConfig.h"
|
|
|
|
#include "../../inc/MarlinConfig.h"
|
|
|
|
#include "timers.h"
|
|
|
|
#include "timers.h"
|
|
|
|
|
|
|
|
|
|
|
|
void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
|
|
|
|
// Array to support sticky frequency sets per timer
|
|
|
|
if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
|
|
|
|
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*/) {
|
|
|
|
|
|
|
|
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 adj_val = Instance->ARR * v / v_size;
|
|
|
|
uint16_t value = v;
|
|
|
|
if (invert) adj_val = Instance->ARR - adj_val;
|
|
|
|
if (invert) value = v_size - value;
|
|
|
|
switch (get_pwm_channel(pin_name)) {
|
|
|
|
|
|
|
|
case TIM_CHANNEL_1: LL_TIM_OC_SetCompareCH1(Instance, adj_val); break;
|
|
|
|
uint32_t index = get_timer_index(Instance);
|
|
|
|
case TIM_CHANNEL_2: LL_TIM_OC_SetCompareCH2(Instance, adj_val); break;
|
|
|
|
|
|
|
|
case TIM_CHANNEL_3: LL_TIM_OC_SetCompareCH3(Instance, adj_val); break;
|
|
|
|
if (HardwareTimer_Handle[index] == nullptr) {
|
|
|
|
case TIM_CHANNEL_4: LL_TIM_OC_SetCompareCH4(Instance, adj_val); break;
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (previousMode != TIMER_OUTPUT_COMPARE_PWM1)
|
|
|
|
|
|
|
|
HT->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (needs_freq) {
|
|
|
|
|
|
|
|
if (timer_freq[index] == 0 ) { // If the timer is unconfigured and no freq is set then default PWM_FREQUENCY.
|
|
|
|
|
|
|
|
timer_freq[index] = PWM_FREQUENCY;
|
|
|
|
|
|
|
|
set_pwm_frequency(pin_name, timer_freq[index]); // Set the frequency and save the value to the assigned index no.
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) HT->resume();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if NEEDS_HARDWARE_PWM
|
|
|
|
void set_pwm_frequency(const pin_t pin, int f_desired) {
|
|
|
|
|
|
|
|
if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
|
|
|
|
|
|
|
|
HardwareTimer *HT;
|
|
|
|
|
|
|
|
PinName pin_name = digitalPinToPinName(pin);
|
|
|
|
|
|
|
|
TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM); // Get HAL timer instance
|
|
|
|
|
|
|
|
|
|
|
|
void set_pwm_frequency(const pin_t pin, int f_desired) {
|
|
|
|
uint32_t index = get_timer_index(Instance);
|
|
|
|
if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PinName pin_name = digitalPinToPinName(pin);
|
|
|
|
// Protect used timers
|
|
|
|
TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM); // Get HAL timer instance
|
|
|
|
if (index == TEMP_TIMER_NUM || index == STEP_TIMER_NUM
|
|
|
|
|
|
|
|
#if PULSE_TIMER_NUM != STEP_TIMER_NUM
|
|
|
|
|
|
|
|
|| index == PULSE_TIMER_NUM
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
) return;
|
|
|
|
|
|
|
|
|
|
|
|
LOOP_S_L_N(i, 0, NUM_HARDWARE_TIMERS) // Protect used timers
|
|
|
|
if (HardwareTimer_Handle[index] == nullptr) // If frequency is set before duty we need to create a handle here.
|
|
|
|
if (timer_instance[i] && timer_instance[i]->getHandle()->Instance == Instance)
|
|
|
|
HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM));
|
|
|
|
return;
|
|
|
|
HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
|
|
|
|
|
|
|
|
timer_freq[index] = f_desired; // Save the last frequency so duty will not set the default for this timer number.
|
|
|
|
pwm_start(pin_name, f_desired, 0, RESOLUTION_8B_COMPARE_FORMAT);
|
|
|
|
HT->setOverflow(f_desired, HERTZ_FORMAT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif // HAL_STM32
|
|
|
|
#endif // HAL_STM32
|
|
|
|