浏览代码

🏗️ Fix Maple HAL/STM32F1 PWM (#23211)

Mike La Spina 3 年前
父节点
当前提交
4411af655e
没有帐户链接到提交者的电子邮件

+ 2
- 2
Marlin/src/HAL/AVR/timers.h 查看文件

@@ -109,8 +109,8 @@ FORCE_INLINE void HAL_timer_start(const uint8_t timer_num, const uint32_t) {
109 109
  * (otherwise, characters will be lost due to UART overflow).
110 110
  * Then: Stepper, Endstops, Temperature, and -finally- all others.
111 111
  */
112
-#define HAL_timer_isr_prologue(TIMER_NUM)
113
-#define HAL_timer_isr_epilogue(TIMER_NUM)
112
+#define HAL_timer_isr_prologue(T)
113
+#define HAL_timer_isr_epilogue(T)
114 114
 
115 115
 /* 18 cycles maximum latency */
116 116
 #ifndef HAL_STEP_TIMER_ISR

+ 1
- 1
Marlin/src/HAL/DUE/timers.h 查看文件

@@ -125,4 +125,4 @@ FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
125 125
   pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_SR;
126 126
 }
127 127
 
128
-#define HAL_timer_isr_epilogue(TIMER_NUM)
128
+#define HAL_timer_isr_epilogue(T)

+ 2
- 2
Marlin/src/HAL/ESP32/timers.h 查看文件

@@ -136,5 +136,5 @@ void HAL_timer_enable_interrupt(const uint8_t timer_num);
136 136
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
137 137
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
138 138
 
139
-#define HAL_timer_isr_prologue(TIMER_NUM)
140
-#define HAL_timer_isr_epilogue(TIMER_NUM)
139
+#define HAL_timer_isr_prologue(T)
140
+#define HAL_timer_isr_epilogue(T)

+ 2
- 2
Marlin/src/HAL/LINUX/timers.h 查看文件

@@ -92,5 +92,5 @@ void HAL_timer_enable_interrupt(const uint8_t timer_num);
92 92
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
93 93
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
94 94
 
95
-#define HAL_timer_isr_prologue(TIMER_NUM)
96
-#define HAL_timer_isr_epilogue(TIMER_NUM)
95
+#define HAL_timer_isr_prologue(T)
96
+#define HAL_timer_isr_epilogue(T)

+ 1
- 1
Marlin/src/HAL/LPC1768/timers.h 查看文件

@@ -170,4 +170,4 @@ FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
170 170
   }
171 171
 }
172 172
 
173
-#define HAL_timer_isr_epilogue(TIMER_NUM)
173
+#define HAL_timer_isr_epilogue(T)

+ 2
- 2
Marlin/src/HAL/NATIVE_SIM/timers.h 查看文件

@@ -87,5 +87,5 @@ void HAL_timer_enable_interrupt(const uint8_t timer_num);
87 87
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
88 88
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
89 89
 
90
-#define HAL_timer_isr_prologue(TIMER_NUM)
91
-#define HAL_timer_isr_epilogue(TIMER_NUM)
90
+#define HAL_timer_isr_prologue(T)
91
+#define HAL_timer_isr_epilogue(T)

+ 8
- 2
Marlin/src/HAL/STM32/timers.cpp 查看文件

@@ -97,9 +97,15 @@
97 97
 #define STEP_TIMER_DEV _TIMER_DEV(STEP_TIMER)
98 98
 #define TEMP_TIMER_DEV _TIMER_DEV(TEMP_TIMER)
99 99
 
100
-// ------------------------
100
+// --------------------------------------------------------------------------
101
+// Local defines
102
+// --------------------------------------------------------------------------
103
+
104
+#define NUM_HARDWARE_TIMERS 2
105
+
106
+// --------------------------------------------------------------------------
101 107
 // Private Variables
102
-// ------------------------
108
+// --------------------------------------------------------------------------
103 109
 
104 110
 HardwareTimer *timer_instance[NUM_HARDWARE_TIMERS] = { nullptr };
105 111
 

+ 2
- 3
Marlin/src/HAL/STM32/timers.h 查看文件

@@ -41,7 +41,6 @@
41 41
 #define HAL_TIMER_TYPE_MAX UINT16_MAX
42 42
 
43 43
 // Marlin timer_instance[] content (unrelated to timer selection)
44
-#define NUM_HARDWARE_TIMERS 2
45 44
 #define MF_TIMER_STEP       0  // Timer Index for Stepper
46 45
 #define MF_TIMER_TEMP       1  // Timer Index for Temperature
47 46
 #define MF_TIMER_PULSE      MF_TIMER_STEP
@@ -117,5 +116,5 @@ FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const ha
117 116
   }
118 117
 }
119 118
 
120
-#define HAL_timer_isr_prologue(TIMER_NUM)
121
-#define HAL_timer_isr_epilogue(TIMER_NUM)
119
+#define HAL_timer_isr_prologue(T)
120
+#define HAL_timer_isr_epilogue(T)

+ 24
- 8
Marlin/src/HAL/STM32F1/fast_pwm.cpp 查看文件

@@ -27,21 +27,35 @@
27 27
 #include "HAL.h"
28 28
 #include "timers.h"
29 29
 
30
+#define NR_TIMERS TERN(STM32_XL_DENSITY, 14, 8) // Maple timers, 14 for STM32_XL_DENSITY (F/G chips), 8 for HIGH density (C D E)
31
+
32
+static uint16_t timer_freq[NR_TIMERS];
33
+
34
+inline uint8_t timer_and_index_for_pin(const pin_t pin, timer_dev **timer_ptr) {
35
+  *timer_ptr = PIN_MAP[pin].timer_device;
36
+  for (uint8_t i = 0; i < NR_TIMERS; i++) if (*timer_ptr == HAL_get_timer_dev(i))
37
+    return i;
38
+  return 0;
39
+}
40
+
30 41
 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
31 42
   if (!PWM_PIN(pin)) return;
32
-  timer_dev *timer = PIN_MAP[pin].timer_device;
33
-  if (!(timer->regs.bas->SR & TIMER_CR1_CEN))   // Ensure the timer is enabled
43
+
44
+  timer_dev *timer; UNUSED(timer);
45
+  if (timer_freq[timer_and_index_for_pin(pin, &timer)] == 0)
34 46
     set_pwm_frequency(pin, PWM_FREQUENCY);
35
-  uint16_t max_val = timer->regs.bas->ARR * v / v_size;
36
-  if (invert) max_val = v_size - max_val;
37
-  pwmWrite(pin, max_val);
47
+
48
+  const uint8_t channel = PIN_MAP[pin].timer_channel;
49
+  const uint16_t duty = invert ? v_size - v : v;
50
+  timer_set_compare(timer, channel, duty);
51
+  timer_set_mode(timer, channel, TIMER_PWM); // PWM Output Mode
38 52
 }
39 53
 
40 54
 void set_pwm_frequency(const pin_t pin, int f_desired) {
41 55
   if (!PWM_PIN(pin)) return;                    // Don't proceed if no hardware timer
42 56
 
43
-  timer_dev *timer = PIN_MAP[pin].timer_device;
44
-  uint8_t channel = PIN_MAP[pin].timer_channel;
57
+  timer_dev *timer; UNUSED(timer);
58
+  timer_freq[timer_and_index_for_pin(pin, &timer)] = f_desired;
45 59
 
46 60
   // Protect used timers
47 61
   if (timer == HAL_get_timer_dev(MF_TIMER_TEMP)) return;
@@ -53,8 +67,10 @@ void set_pwm_frequency(const pin_t pin, int f_desired) {
53 67
   if (!(timer->regs.bas->SR & TIMER_CR1_CEN))   // Ensure the timer is enabled
54 68
     timer_init(timer);
55 69
 
70
+  const uint8_t channel = PIN_MAP[pin].timer_channel;
56 71
   timer_set_mode(timer, channel, TIMER_PWM);
57
-  uint16_t preload = 255;                       // Lock 255 PWM resolution for high frequencies
72
+  // Preload (resolution) cannot be equal to duty of 255 otherwise it may not result in digital off or on.
73
+  uint16_t preload = 254;
58 74
   int32_t prescaler = (HAL_TIMER_RATE) / (preload + 1) / f_desired - 1;
59 75
   if (prescaler > 65535) {                      // For low frequencies increase prescaler
60 76
     prescaler = 65535;

+ 1
- 1
Marlin/src/HAL/STM32F1/timers.h 查看文件

@@ -188,7 +188,7 @@ FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
188 188
   }
189 189
 }
190 190
 
191
-#define HAL_timer_isr_epilogue(TIMER_NUM)
191
+#define HAL_timer_isr_epilogue(T)
192 192
 
193 193
 // No command is available in framework to turn off ARPE bit, which is turned on by default in libmaple.
194 194
 // Needed here to reset ARPE=0 for stepper timer

+ 1
- 1
Marlin/src/HAL/TEENSY31_32/timers.h 查看文件

@@ -110,4 +110,4 @@ void HAL_timer_disable_interrupt(const uint8_t timer_num);
110 110
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
111 111
 
112 112
 void HAL_timer_isr_prologue(const uint8_t timer_num);
113
-#define HAL_timer_isr_epilogue(TIMER_NUM)
113
+#define HAL_timer_isr_epilogue(T)

+ 1
- 1
Marlin/src/HAL/TEENSY35_36/timers.h 查看文件

@@ -109,4 +109,4 @@ void HAL_timer_disable_interrupt(const uint8_t timer_num);
109 109
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
110 110
 
111 111
 void HAL_timer_isr_prologue(const uint8_t timer_num);
112
-#define HAL_timer_isr_epilogue(TIMER_NUM)
112
+#define HAL_timer_isr_epilogue(T)

+ 1
- 1
Marlin/src/HAL/TEENSY40_41/timers.h 查看文件

@@ -114,4 +114,4 @@ bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
114 114
 
115 115
 void HAL_timer_isr_prologue(const uint8_t timer_num);
116 116
 //void HAL_timer_isr_epilogue(const uint8_t timer_num) {}
117
-#define HAL_timer_isr_epilogue(TIMER_NUM)
117
+#define HAL_timer_isr_epilogue(T)

正在加载...
取消
保存