Ver código fonte

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

Mike La Spina 3 anos atrás
pai
commit
4411af655e
Nenhuma conta vinculada ao e-mail do autor do commit

+ 2
- 2
Marlin/src/HAL/AVR/timers.h Ver arquivo

109
  * (otherwise, characters will be lost due to UART overflow).
109
  * (otherwise, characters will be lost due to UART overflow).
110
  * Then: Stepper, Endstops, Temperature, and -finally- all others.
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
 /* 18 cycles maximum latency */
115
 /* 18 cycles maximum latency */
116
 #ifndef HAL_STEP_TIMER_ISR
116
 #ifndef HAL_STEP_TIMER_ISR

+ 1
- 1
Marlin/src/HAL/DUE/timers.h Ver arquivo

125
   pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_SR;
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 Ver arquivo

136
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
136
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
137
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
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 Ver arquivo

92
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
92
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
93
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
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 Ver arquivo

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 Ver arquivo

87
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
87
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
88
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
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 Ver arquivo

97
 #define STEP_TIMER_DEV _TIMER_DEV(STEP_TIMER)
97
 #define STEP_TIMER_DEV _TIMER_DEV(STEP_TIMER)
98
 #define TEMP_TIMER_DEV _TIMER_DEV(TEMP_TIMER)
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
 // Private Variables
107
 // Private Variables
102
-// ------------------------
108
+// --------------------------------------------------------------------------
103
 
109
 
104
 HardwareTimer *timer_instance[NUM_HARDWARE_TIMERS] = { nullptr };
110
 HardwareTimer *timer_instance[NUM_HARDWARE_TIMERS] = { nullptr };
105
 
111
 

+ 2
- 3
Marlin/src/HAL/STM32/timers.h Ver arquivo

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
 // Marlin timer_instance[] content (unrelated to timer selection)
44
-#define NUM_HARDWARE_TIMERS 2
45
 #define MF_TIMER_STEP       0  // Timer Index for Stepper
44
 #define MF_TIMER_STEP       0  // Timer Index for Stepper
46
 #define MF_TIMER_TEMP       1  // Timer Index for Temperature
45
 #define MF_TIMER_TEMP       1  // Timer Index for Temperature
47
 #define MF_TIMER_PULSE      MF_TIMER_STEP
46
 #define MF_TIMER_PULSE      MF_TIMER_STEP
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 Ver arquivo

27
 #include "HAL.h"
27
 #include "HAL.h"
28
 #include "timers.h"
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
 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
41
 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
31
   if (!PWM_PIN(pin)) return;
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
     set_pwm_frequency(pin, PWM_FREQUENCY);
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
 void set_pwm_frequency(const pin_t pin, int f_desired) {
54
 void set_pwm_frequency(const pin_t pin, int f_desired) {
41
   if (!PWM_PIN(pin)) return;                    // Don't proceed if no hardware timer
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
   // Protect used timers
60
   // Protect used timers
47
   if (timer == HAL_get_timer_dev(MF_TIMER_TEMP)) return;
61
   if (timer == HAL_get_timer_dev(MF_TIMER_TEMP)) return;
53
   if (!(timer->regs.bas->SR & TIMER_CR1_CEN))   // Ensure the timer is enabled
67
   if (!(timer->regs.bas->SR & TIMER_CR1_CEN))   // Ensure the timer is enabled
54
     timer_init(timer);
68
     timer_init(timer);
55
 
69
 
70
+  const uint8_t channel = PIN_MAP[pin].timer_channel;
56
   timer_set_mode(timer, channel, TIMER_PWM);
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
   int32_t prescaler = (HAL_TIMER_RATE) / (preload + 1) / f_desired - 1;
74
   int32_t prescaler = (HAL_TIMER_RATE) / (preload + 1) / f_desired - 1;
59
   if (prescaler > 65535) {                      // For low frequencies increase prescaler
75
   if (prescaler > 65535) {                      // For low frequencies increase prescaler
60
     prescaler = 65535;
76
     prescaler = 65535;

+ 1
- 1
Marlin/src/HAL/STM32F1/timers.h Ver arquivo

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

+ 1
- 1
Marlin/src/HAL/TEENSY31_32/timers.h Ver arquivo

110
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
110
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
111
 
111
 
112
 void HAL_timer_isr_prologue(const uint8_t timer_num);
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 Ver arquivo

109
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
109
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
110
 
110
 
111
 void HAL_timer_isr_prologue(const uint8_t timer_num);
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 Ver arquivo

114
 
114
 
115
 void HAL_timer_isr_prologue(const uint8_t timer_num);
115
 void HAL_timer_isr_prologue(const uint8_t timer_num);
116
 //void HAL_timer_isr_epilogue(const uint8_t timer_num) {}
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)

Carregando…
Cancelar
Salvar