Browse Source

🐛 Fix STM32 FastPWM

Scott Lahteine 3 years ago
parent
commit
c09641c7c0
1 changed files with 11 additions and 19 deletions
  1. 11
    19
      Marlin/src/HAL/STM32/fast_pwm.cpp

+ 11
- 19
Marlin/src/HAL/STM32/fast_pwm.cpp View File

@@ -32,26 +32,17 @@ static uint16_t timer_freq[TIMER_NUM];
32 32
 
33 33
 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
34 34
   if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
35
-  bool needs_freq;
36 35
   PinName pin_name = digitalPinToPinName(pin);
37 36
   TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM);
38
-  HardwareTimer *HT;
39
-  TimerModes_t previousMode;
40 37
 
41
-  uint16_t value = v;
42
-  if (invert) value = v_size - value;
43
-
44
-  uint32_t index = get_timer_index(Instance);
45
-
46
-  if (HardwareTimer_Handle[index] == nullptr) {
38
+  const uint32_t index = get_timer_index(Instance);
39
+  bool needs_freq = (HardwareTimer_Handle[index] == nullptr); // A new instance must be set to the default frequency of PWM_FREQUENCY
40
+  if (needs_freq)
47 41
     HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM));
48
-    needs_freq = true;                  // The instance must be new set the default frequency of PWM_FREQUENCY
49
-  }
50
-
51
-  HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
52
-  uint32_t channel = STM_PIN_CHANNEL(pinmap_function(pin_name, PinMap_PWM));
53
-  previousMode = HT->getMode(channel);
54 42
 
43
+  HardwareTimer * const HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
44
+  const uint32_t channel = STM_PIN_CHANNEL(pinmap_function(pin_name, PinMap_PWM));
45
+  const TimerModes_t previousMode = HT->getMode(channel);
55 46
   if (previousMode != TIMER_OUTPUT_COMPARE_PWM1)
56 47
     HT->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
57 48
 
@@ -63,6 +54,7 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255
63 54
   }
64 55
   // Note the resolution is sticky here, the input can be upto 16 bits and that would require RESOLUTION_16B_COMPARE_FORMAT (16)
65 56
   // 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.
57
+  const uint16_t value = invert ? v_size - v : v;
66 58
   HT->setCaptureCompare(channel, value, RESOLUTION_8B_COMPARE_FORMAT); // Sets the duty, the calc is done in the library :)
67 59
   pinmap_pinout(pin_name, PinMap_PWM); // Make sure the pin output state is set.
68 60
   if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) HT->resume();
@@ -73,12 +65,12 @@ void set_pwm_frequency(const pin_t pin, int f_desired) {
73 65
 
74 66
   const PinName pin_name = digitalPinToPinName(pin);
75 67
   TIM_TypeDef * const Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM); // Get HAL timer instance
76
-  uint32_t index = get_timer_index(Instance);
68
+  const uint32_t index = get_timer_index(Instance);
77 69
 
78 70
   // Protect used timers
79
-  if (index == TEMP_TIMER_NUM || index == STEP_TIMER_NUM
80
-    #if PULSE_TIMER_NUM != STEP_TIMER_NUM
81
-      || index == PULSE_TIMER_NUM
71
+  if (index == MF_TIMER_TEMP || index == MF_TIMER_STEP
72
+    #if MF_TIMER_PULSE != MF_TIMER_STEP
73
+      || index == MF_TIMER_PULSE
82 74
     #endif
83 75
   ) return;
84 76
 

Loading…
Cancel
Save