Просмотр исходного кода

🏗️ Rework STM32 timer frequency protection (#23187)

Tanguy Pruvot 3 лет назад
Родитель
Сommit
2a1facf853
Аккаунт пользователя с таким Email не найден
2 измененных файлов: 26 добавлений и 30 удалений
  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 Просмотреть файл

25
 #ifdef HAL_STM32
25
 #ifdef HAL_STM32
26
 
26
 
27
 #include "../../inc/MarlinConfig.h"
27
 #include "../../inc/MarlinConfig.h"
28
-#include "timers.h"
29
 
28
 
30
 // Array to support sticky frequency sets per timer
29
 // Array to support sticky frequency sets per timer
31
 static uint16_t timer_freq[TIMER_NUM];
30
 static uint16_t timer_freq[TIMER_NUM];
32
 
31
 
33
 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
32
 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
34
   if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
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
     HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM));
40
     HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM));
42
 
41
 
43
   HardwareTimer * const HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
42
   HardwareTimer * const HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
46
   if (previousMode != TIMER_OUTPUT_COMPARE_PWM1)
45
   if (previousMode != TIMER_OUTPUT_COMPARE_PWM1)
47
     HT->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
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
   // Note the resolution is sticky here, the input can be upto 16 bits and that would require RESOLUTION_16B_COMPARE_FORMAT (16)
51
   // Note the resolution is sticky here, the input can be upto 16 bits and that would require RESOLUTION_16B_COMPARE_FORMAT (16)
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.
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
   const uint16_t value = invert ? v_size - v : v;
53
   const uint16_t value = invert ? v_size - v : v;
62
 
58
 
63
 void set_pwm_frequency(const pin_t pin, int f_desired) {
59
 void set_pwm_frequency(const pin_t pin, int f_desired) {
64
   if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
60
   if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
65
-
66
   const PinName pin_name = digitalPinToPinName(pin);
61
   const PinName pin_name = digitalPinToPinName(pin);
67
   TIM_TypeDef * const Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM); // Get HAL timer instance
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
   if (HardwareTimer_Handle[index] == nullptr) // If frequency is set before duty we need to create a handle here.
76
   if (HardwareTimer_Handle[index] == nullptr) // If frequency is set before duty we need to create a handle here.
78
     HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM));
77
     HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM));
79
   HardwareTimer * const HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
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
   HT->setOverflow(f_desired, HERTZ_FORMAT);
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
 #endif // HAL_STM32
83
 #endif // HAL_STM32

+ 6
- 9
Marlin/src/HAL/STM32/timers.h Просмотреть файл

40
 #define hal_timer_t uint32_t
40
 #define hal_timer_t uint32_t
41
 #define HAL_TIMER_TYPE_MAX UINT16_MAX
41
 #define HAL_TIMER_TYPE_MAX UINT16_MAX
42
 
42
 
43
+// Marlin timer_instance[] content (unrelated to timer selection)
43
 #define NUM_HARDWARE_TIMERS 2
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
 #define TEMP_TIMER_FREQUENCY 1000   // Temperature::isr() is expected to be called at around 1kHz
52
 #define TEMP_TIMER_FREQUENCY 1000   // Temperature::isr() is expected to be called at around 1kHz
56
 
53
 

Загрузка…
Отмена
Сохранить