Procházet zdrojové kódy

🏗️ Rework STM32 timer frequency protection (#23187)

Tanguy Pruvot před 3 roky
rodič
revize
2a1facf853
No account linked to committer's email address
2 změnil soubory, kde provedl 26 přidání a 30 odebrání
  1. 20
    21
      Marlin/src/HAL/STM32/fast_pwm.cpp
  2. 6
    9
      Marlin/src/HAL/STM32/timers.h

+ 20
- 21
Marlin/src/HAL/STM32/fast_pwm.cpp Zobrazit soubor

@@ -25,19 +25,18 @@
25 25
 #ifdef HAL_STM32
26 26
 
27 27
 #include "../../inc/MarlinConfig.h"
28
-#include "timers.h"
29 28
 
30 29
 // Array to support sticky frequency sets per timer
31 30
 static uint16_t timer_freq[TIMER_NUM];
32 31
 
33 32
 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
34 33
   if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
35
-  PinName pin_name = digitalPinToPinName(pin);
36
-  TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM);
34
+  const PinName pin_name = digitalPinToPinName(pin);
35
+  TIM_TypeDef * const Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM);
37 36
 
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)
37
+  const timer_index_t index = get_timer_index(Instance);
38
+  const bool needs_freq = (HardwareTimer_Handle[index] == nullptr);
39
+  if (needs_freq) // A new instance must be set to the default frequency of PWM_FREQUENCY
41 40
     HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM));
42 41
 
43 42
   HardwareTimer * const HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
@@ -46,12 +45,9 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255
46 45
   if (previousMode != TIMER_OUTPUT_COMPARE_PWM1)
47 46
     HT->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
48 47
 
49
-  if (needs_freq) {
50
-    if (timer_freq[index] == 0 ) {                    // If the timer is unconfigured and no freq is set then default PWM_FREQUENCY.
51
-      timer_freq[index] = PWM_FREQUENCY;
52
-      set_pwm_frequency(pin_name, timer_freq[index]); // Set the frequency and save the value to the assigned index no.
53
-    }
54
-  }
48
+  if (needs_freq && timer_freq[index] == 0)     // If the timer is unconfigured and no freq is set then default PWM_FREQUENCY
49
+    set_pwm_frequency(pin_name, PWM_FREQUENCY); // Set the frequency and save the value to the assigned index no.
50
+
55 51
   // Note the resolution is sticky here, the input can be upto 16 bits and that would require RESOLUTION_16B_COMPARE_FORMAT (16)
56 52
   // 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 53
   const uint16_t value = invert ? v_size - v : v;
@@ -62,23 +58,26 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255
62 58
 
63 59
 void set_pwm_frequency(const pin_t pin, int f_desired) {
64 60
   if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
65
-
66 61
   const PinName pin_name = digitalPinToPinName(pin);
67 62
   TIM_TypeDef * const Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM); // Get HAL timer instance
68
-  const uint32_t index = get_timer_index(Instance);
63
+  const timer_index_t index = get_timer_index(Instance);
69 64
 
70
-  // Protect used timers
71
-  if (index == MF_TIMER_TEMP || index == MF_TIMER_STEP
72
-    #if MF_TIMER_PULSE != MF_TIMER_STEP
73
-      || index == MF_TIMER_PULSE
74
-    #endif
75
-  ) return;
65
+  // Protect used timers.
66
+  #ifdef STEP_TIMER
67
+    if (index == TIMER_INDEX(STEP_TIMER)) return;
68
+  #endif
69
+  #ifdef TEMP_TIMER
70
+    if (index == TIMER_INDEX(TEMP_TIMER)) return;
71
+  #endif
72
+  #if defined(PULSE_TIMER) && MF_TIMER_PULSE != MF_TIMER_STEP
73
+    if (index == TIMER_INDEX(PULSE_TIMER)) return;
74
+  #endif
76 75
 
77 76
   if (HardwareTimer_Handle[index] == nullptr) // If frequency is set before duty we need to create a handle here.
78 77
     HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM));
79 78
   HardwareTimer * const HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
80
-  timer_freq[index] = f_desired; // Save the last frequency so duty will not set the default for this timer number.
81 79
   HT->setOverflow(f_desired, HERTZ_FORMAT);
80
+  timer_freq[index] = f_desired; // Save the last frequency so duty will not set the default for this timer number.
82 81
 }
83 82
 
84 83
 #endif // HAL_STM32

+ 6
- 9
Marlin/src/HAL/STM32/timers.h Zobrazit soubor

@@ -40,17 +40,14 @@
40 40
 #define hal_timer_t uint32_t
41 41
 #define HAL_TIMER_TYPE_MAX UINT16_MAX
42 42
 
43
+// Marlin timer_instance[] content (unrelated to timer selection)
43 44
 #define NUM_HARDWARE_TIMERS 2
45
+#define MF_TIMER_STEP       0  // Timer Index for Stepper
46
+#define MF_TIMER_TEMP       1  // Timer Index for Temperature
47
+#define MF_TIMER_PULSE      MF_TIMER_STEP
44 48
 
45
-#ifndef MF_TIMER_STEP
46
-  #define MF_TIMER_STEP         0  // Timer Index for Stepper
47
-#endif
48
-#ifndef MF_TIMER_PULSE
49
-  #define MF_TIMER_PULSE        MF_TIMER_STEP
50
-#endif
51
-#ifndef MF_TIMER_TEMP
52
-  #define MF_TIMER_TEMP         1  // Timer Index for Temperature
53
-#endif
49
+#define TIMER_INDEX_(T) TIMER##T##_INDEX  // TIMER#_INDEX enums (timer_index_t) depend on TIM#_BASE defines.
50
+#define TIMER_INDEX(T) TIMER_INDEX_(T)    // Convert Timer ID to HardwareTimer_Handle index.
54 51
 
55 52
 #define TEMP_TIMER_FREQUENCY 1000   // Temperature::isr() is expected to be called at around 1kHz
56 53
 

Loading…
Zrušit
Uložit