瀏覽代碼

🍻 STM32 set_pwm_duty "on/off" for digital pins (#23665)

Mike La Spina 3 年之前
父節點
當前提交
71d54dab3c
沒有連結到貢獻者的電子郵件帳戶。

+ 25
- 20
Marlin/src/HAL/STM32/fast_pwm.cpp 查看文件

@@ -30,30 +30,35 @@
30 30
 static uint16_t timer_freq[TIMER_NUM];
31 31
 
32 32
 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
33
-  if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
34
-  const PinName pin_name = digitalPinToPinName(pin);
35
-  TIM_TypeDef * const Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM);
33
+  const uint16_t duty = invert ? v_size - v : v;
34
+  if (PWM_PIN(pin)) {
35
+    const PinName pin_name = digitalPinToPinName(pin);
36
+    TIM_TypeDef * const Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM);
36 37
 
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
40
-    HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM));
38
+    const timer_index_t index = get_timer_index(Instance);
39
+    const bool needs_freq = (HardwareTimer_Handle[index] == nullptr);
40
+    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));
41 42
 
42
-  HardwareTimer * const HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
43
-  const uint32_t channel = STM_PIN_CHANNEL(pinmap_function(pin_name, PinMap_PWM));
44
-  const TimerModes_t previousMode = HT->getMode(channel);
45
-  if (previousMode != TIMER_OUTPUT_COMPARE_PWM1)
46
-    HT->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
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);
46
+    if (previousMode != TIMER_OUTPUT_COMPARE_PWM1)
47
+      HT->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
47 48
 
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.
49
+    if (needs_freq && timer_freq[index] == 0)     // If the timer is unconfigured and no freq is set then default PWM_FREQUENCY
50
+      set_pwm_frequency(pin_name, PWM_FREQUENCY); // Set the frequency and save the value to the assigned index no.
50 51
 
51
-  // Note the resolution is sticky here, the input can be upto 16 bits and that would require RESOLUTION_16B_COMPARE_FORMAT (16)
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.
53
-  const uint16_t value = invert ? v_size - v : v;
54
-  HT->setCaptureCompare(channel, value, RESOLUTION_8B_COMPARE_FORMAT); // Sets the duty, the calc is done in the library :)
55
-  pinmap_pinout(pin_name, PinMap_PWM); // Make sure the pin output state is set.
56
-  if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) HT->resume();
52
+    // Note the resolution is sticky here, the input can be upto 16 bits and that would require RESOLUTION_16B_COMPARE_FORMAT (16)
53
+    // 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.
54
+    HT->setCaptureCompare(channel, duty, RESOLUTION_8B_COMPARE_FORMAT); // Set the duty, the calc is done in the library :)
55
+    pinmap_pinout(pin_name, PinMap_PWM); // Make sure the pin output state is set.
56
+    if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) HT->resume();
57
+  }
58
+  else {
59
+    pinMode(pin, OUTPUT);
60
+    digitalWrite(pin, duty < v_size / 2 ? LOW : HIGH);
61
+  }
57 62
 }
58 63
 
59 64
 void set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {

+ 12
- 9
Marlin/src/HAL/STM32F1/fast_pwm.cpp 查看文件

@@ -39,16 +39,19 @@ inline uint8_t timer_and_index_for_pin(const pin_t pin, timer_dev **timer_ptr) {
39 39
 }
40 40
 
41 41
 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
42
-  if (!PWM_PIN(pin)) return;
43
-
44
-  timer_dev *timer; UNUSED(timer);
45
-  if (timer_freq[timer_and_index_for_pin(pin, &timer)] == 0)
46
-    set_pwm_frequency(pin, PWM_FREQUENCY);
47
-
48
-  const uint8_t channel = PIN_MAP[pin].timer_channel;
49 42
   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
43
+  if (PWM_PIN(pin)) {
44
+    timer_dev *timer; UNUSED(timer);
45
+    if (timer_freq[timer_and_index_for_pin(pin, &timer)] == 0)
46
+      set_pwm_frequency(pin, PWM_FREQUENCY);
47
+    const uint8_t channel = PIN_MAP[pin].timer_channel;
48
+    timer_set_compare(timer, channel, duty);
49
+    timer_set_mode(timer, channel, TIMER_PWM); // PWM Output Mode
50
+  }
51
+  else {
52
+    pinMode(pin, OUTPUT);
53
+    digitalWrite(pin, duty < v_size / 2 ? LOW : HIGH);
54
+  }
52 55
 }
53 56
 
54 57
 void set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {

+ 2
- 2
buildroot/share/PlatformIO/variants/STM32G0xx/MARLIN_G0B1RE/variant_MARLIN_STM32G0B1RE.h 查看文件

@@ -150,10 +150,10 @@
150 150
 // Timer Definitions
151 151
 // Use TIM6/TIM7 when possible as servo and tone don't need GPIO output pin
152 152
 #ifndef TIMER_TONE
153
-  #define TIMER_TONE            TIM6
153
+  #define TIMER_TONE            TIM6  // TIMER_TONE must be defined in this file
154 154
 #endif
155 155
 #ifndef TIMER_SERVO
156
-  #define TIMER_SERVO           TIM7
156
+  #define TIMER_SERVO           TIM7  // TIMER_SERVO must be defined in this file
157 157
 #endif
158 158
 
159 159
 // UART Definitions

Loading…
取消
儲存