Browse Source

⚗️ Use pwm_set_duty over analogWrite to set PWM (#23048)

Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
Andrei M 2 years ago
parent
commit
da830e6ced
No account linked to committer's email address

+ 1
- 1
Marlin/src/HAL/AVR/HAL.h View File

@@ -221,7 +221,7 @@ void set_pwm_frequency(const pin_t pin, int f_desired);
221 221
 
222 222
 /**
223 223
  * set_pwm_duty
224
- *  Sets the PWM duty cycle of the provided pin to the provided value
224
+ *  Set the PWM duty cycle of the provided pin to the provided value
225 225
  *  Optionally allows inverting the duty cycle [default = false]
226 226
  *  Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
227 227
  */

+ 30
- 32
Marlin/src/HAL/AVR/fast_pwm.cpp View File

@@ -22,11 +22,10 @@
22 22
 #ifdef __AVR__
23 23
 
24 24
 #include "../../inc/MarlinConfigPre.h"
25
+#include "HAL.h"
25 26
 
26 27
 #if NEEDS_HARDWARE_PWM // Specific meta-flag for features that mandate PWM
27 28
 
28
-#include "HAL.h"
29
-
30 29
 struct Timer {
31 30
   volatile uint8_t* TCCRnQ[3];  // max 3 TCCR registers per timer
32 31
   volatile uint16_t* OCRnQ[3];  // max 3 OCR registers per timer
@@ -153,7 +152,7 @@ Timer get_pwm_timer(const pin_t pin) {
153 152
 
154 153
 void set_pwm_frequency(const pin_t pin, int f_desired) {
155 154
   Timer timer = get_pwm_timer(pin);
156
-  if (timer.n == 0) return; // Don't proceed if protected timer or not recognised
155
+  if (timer.n == 0) return; // Don't proceed if protected timer or not recognized
157 156
   uint16_t size;
158 157
   if (timer.n == 2) size = 255; else size = 65535;
159 158
 
@@ -243,40 +242,39 @@ void set_pwm_frequency(const pin_t pin, int f_desired) {
243 242
     _SET_ICRn(timer.ICRn, res);         // Set ICRn value (TOP) = res
244 243
 }
245 244
 
245
+#endif // NEEDS_HARDWARE_PWM
246
+
246 247
 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
247
-  // If v is 0 or v_size (max), digitalWrite to LOW or HIGH.
248
-  // Note that digitalWrite also disables pwm output for us (sets COM bit to 0)
249
-  if (v == 0)
250
-    digitalWrite(pin, invert);
251
-  else if (v == v_size)
252
-    digitalWrite(pin, !invert);
253
-  else {
254
-    Timer timer = get_pwm_timer(pin);
255
-    if (timer.n == 0) return; // Don't proceed if protected timer or not recognised
256
-    // Set compare output mode to CLEAR -> SET or SET -> CLEAR (if inverted)
257
-    _SET_COMnQ(timer.TCCRnQ, (timer.q
258
-        #ifdef TCCR2
259
-          + (timer.q == 2) // COM20 is on bit 4 of TCCR2, thus requires q + 1 in the macro
260
-        #endif
261
-      ), COM_CLEAR_SET + invert
262
-    );
248
+  #if NEEDS_HARDWARE_PWM
263 249
 
264
-    uint16_t top;
265
-    if (timer.n == 2) { // if TIMER2
266
-      top = (
267
-        #if ENABLED(USE_OCR2A_AS_TOP)
268
-          *timer.OCRnQ[0] // top = OCR2A
269
-        #else
270
-          255 // top = 0xFF (max)
271
-        #endif
250
+    // If v is 0 or v_size (max), digitalWrite to LOW or HIGH.
251
+    // Note that digitalWrite also disables pwm output for us (sets COM bit to 0)
252
+    if (v == 0)
253
+      digitalWrite(pin, invert);
254
+    else if (v == v_size)
255
+      digitalWrite(pin, !invert);
256
+    else {
257
+      Timer timer = get_pwm_timer(pin);
258
+      if (timer.n == 0) return; // Don't proceed if protected timer or not recognized
259
+      // Set compare output mode to CLEAR -> SET or SET -> CLEAR (if inverted)
260
+      _SET_COMnQ(timer.TCCRnQ, (timer.q
261
+          #ifdef TCCR2
262
+            + (timer.q == 2) // COM20 is on bit 4 of TCCR2, thus requires q + 1 in the macro
263
+          #endif
264
+        ), COM_CLEAR_SET + invert
272 265
       );
266
+
267
+      uint16_t top = (timer.n == 2) ? TERN(USE_OCR2A_AS_TOP, *timer.OCRnQ[0], 255) : *timer.ICRn;
268
+      _SET_OCRnQ(timer.OCRnQ, timer.q, (v * top + v_size / 2) / v_size); // Scale 8/16-bit v to top value
273 269
     }
274
-    else
275
-      top = *timer.ICRn; // top = ICRn
276 270
 
277
-    _SET_OCRnQ(timer.OCRnQ, timer.q, v * float(top) / float(v_size)); // Scale 8/16-bit v to top value
278
-  }
271
+  #else
272
+
273
+    analogWrite(pin, v);
274
+    UNUSED(v_size);
275
+    UNUSED(invert);
276
+
277
+  #endif
279 278
 }
280 279
 
281
-#endif // NEEDS_HARDWARE_PWM
282 280
 #endif // __AVR__

+ 5
- 0
Marlin/src/HAL/DUE/HAL.h View File

@@ -145,6 +145,11 @@ void HAL_adc_start_conversion(const uint8_t ch);
145 145
 uint16_t HAL_adc_get_result();
146 146
 
147 147
 //
148
+// PWM
149
+//
150
+inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); }
151
+
152
+//
148 153
 // Pin Map
149 154
 //
150 155
 #define GET_PIN_MAP_PIN(index) index

+ 4
- 0
Marlin/src/HAL/ESP32/HAL.h View File

@@ -129,6 +129,10 @@ void HAL_adc_init();
129 129
 
130 130
 void HAL_adc_start_conversion(const uint8_t adc_pin);
131 131
 
132
+// PWM
133
+inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); }
134
+
135
+// Pin Map
132 136
 #define GET_PIN_MAP_PIN(index) index
133 137
 #define GET_PIN_MAP_INDEX(pin) pin
134 138
 #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)

+ 3
- 0
Marlin/src/HAL/LINUX/HAL.h View File

@@ -101,6 +101,9 @@ void HAL_adc_enable_channel(const uint8_t ch);
101 101
 void HAL_adc_start_conversion(const uint8_t ch);
102 102
 uint16_t HAL_adc_get_result();
103 103
 
104
+// PWM
105
+inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); }
106
+
104 107
 // Reset source
105 108
 inline void HAL_clear_reset_source(void) {}
106 109
 inline uint8_t HAL_get_reset_source(void) { return RST_POWER_ON; }

+ 8
- 8
Marlin/src/HAL/LPC1768/fast_pwm.cpp View File

@@ -22,18 +22,18 @@
22 22
 #ifdef TARGET_LPC1768
23 23
 
24 24
 #include "../../inc/MarlinConfigPre.h"
25
-
26
-#if NEEDS_HARDWARE_PWM // Specific meta-flag for features that mandate PWM
27
-
28 25
 #include <pwm.h>
29 26
 
30
-void set_pwm_frequency(const pin_t pin, int f_desired) {
31
-  LPC176x::pwm_set_frequency(pin, f_desired);
32
-}
33
-
34 27
 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
35 28
   LPC176x::pwm_write_ratio(pin, invert ? 1.0f - (float)v / v_size : (float)v / v_size);
36 29
 }
37 30
 
38
-#endif // NEEDS_HARDWARE_PWM
31
+#if NEEDS_HARDWARE_PWM // Specific meta-flag for features that mandate PWM
32
+
33
+  void set_pwm_frequency(const pin_t pin, int f_desired) {
34
+    LPC176x::pwm_set_frequency(pin, f_desired);
35
+  }
36
+
37
+#endif
38
+
39 39
 #endif // TARGET_LPC1768

+ 3
- 0
Marlin/src/HAL/NATIVE_SIM/HAL.h View File

@@ -133,6 +133,9 @@ void HAL_adc_enable_channel(const uint8_t ch);
133 133
 void HAL_adc_start_conversion(const uint8_t ch);
134 134
 uint16_t HAL_adc_get_result();
135 135
 
136
+// PWM
137
+inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); }
138
+
136 139
 // Reset source
137 140
 inline void HAL_clear_reset_source(void) {}
138 141
 inline uint8_t HAL_get_reset_source(void) { return RST_POWER_ON; }

+ 5
- 0
Marlin/src/HAL/SAMD51/HAL.h View File

@@ -128,6 +128,11 @@ void HAL_adc_init();
128 128
 void HAL_adc_start_conversion(const uint8_t adc_pin);
129 129
 
130 130
 //
131
+// PWM
132
+//
133
+inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); }
134
+
135
+//
131 136
 // Pin Map
132 137
 //
133 138
 #define GET_PIN_MAP_PIN(index) index

+ 18
- 19
Marlin/src/HAL/STM32/fast_pwm.cpp View File

@@ -24,26 +24,9 @@
24 24
 
25 25
 #ifdef HAL_STM32
26 26
 
27
-#include "../../inc/MarlinConfigPre.h"
28
-
29
-#if NEEDS_HARDWARE_PWM
30
-
31
-#include "HAL.h"
27
+#include "../../inc/MarlinConfig.h"
32 28
 #include "timers.h"
33 29
 
34
-void set_pwm_frequency(const pin_t pin, int f_desired) {
35
-  if (!PWM_PIN(pin)) return;            // Don't proceed if no hardware timer
36
-
37
-  PinName pin_name = digitalPinToPinName(pin);
38
-  TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM); // Get HAL timer instance
39
-
40
-  LOOP_S_L_N(i, 0, NUM_HARDWARE_TIMERS) // Protect used timers
41
-    if (timer_instance[i] && timer_instance[i]->getHandle()->Instance == Instance)
42
-      return;
43
-
44
-  pwm_start(pin_name, f_desired, 0, RESOLUTION_8B_COMPARE_FORMAT);
45
-}
46
-
47 30
 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
48 31
   PinName pin_name = digitalPinToPinName(pin);
49 32
   TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM);
@@ -58,5 +41,21 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255
58 41
   }
59 42
 }
60 43
 
61
-#endif // NEEDS_HARDWARE_PWM
44
+#if NEEDS_HARDWARE_PWM
45
+
46
+  void set_pwm_frequency(const pin_t pin, int f_desired) {
47
+    if (!PWM_PIN(pin)) return;            // Don't proceed if no hardware timer
48
+
49
+    PinName pin_name = digitalPinToPinName(pin);
50
+    TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM); // Get HAL timer instance
51
+
52
+    LOOP_S_L_N(i, 0, NUM_HARDWARE_TIMERS) // Protect used timers
53
+      if (timer_instance[i] && timer_instance[i]->getHandle()->Instance == Instance)
54
+        return;
55
+
56
+    pwm_start(pin_name, f_desired, 0, RESOLUTION_8B_COMPARE_FORMAT);
57
+  }
58
+
59
+#endif
60
+
62 61
 #endif // HAL_STM32

+ 1
- 2
Marlin/src/HAL/STM32F1/HAL.cpp View File

@@ -449,8 +449,7 @@ uint16_t analogRead(pin_t pin) {
449 449
 
450 450
 // Wrapper to maple unprotected analogWrite
451 451
 void analogWrite(pin_t pin, int pwm_val8) {
452
-  if (PWM_PIN(pin))
453
-    analogWrite(uint8_t(pin), pwm_val8);
452
+  if (PWM_PIN(pin)) analogWrite(uint8_t(pin), pwm_val8);
454 453
 }
455 454
 
456 455
 void HAL_reboot() { nvic_sys_reset(); }

+ 30
- 30
Marlin/src/HAL/STM32F1/fast_pwm.cpp View File

@@ -23,40 +23,10 @@
23 23
 
24 24
 #include "../../inc/MarlinConfigPre.h"
25 25
 
26
-#if NEEDS_HARDWARE_PWM
27
-
28 26
 #include <pwm.h>
29 27
 #include "HAL.h"
30 28
 #include "timers.h"
31 29
 
32
-void set_pwm_frequency(const pin_t pin, int f_desired) {
33
-  if (!PWM_PIN(pin)) return;                    // Don't proceed if no hardware timer
34
-
35
-  timer_dev *timer = PIN_MAP[pin].timer_device;
36
-  uint8_t channel = PIN_MAP[pin].timer_channel;
37
-
38
-  // Protect used timers
39
-  if (timer == get_timer_dev(TEMP_TIMER_NUM)) return;
40
-  if (timer == get_timer_dev(STEP_TIMER_NUM)) return;
41
-  #if PULSE_TIMER_NUM != STEP_TIMER_NUM
42
-    if (timer == get_timer_dev(PULSE_TIMER_NUM)) return;
43
-  #endif
44
-
45
-  if (!(timer->regs.bas->SR & TIMER_CR1_CEN))   // Ensure the timer is enabled
46
-    timer_init(timer);
47
-
48
-  timer_set_mode(timer, channel, TIMER_PWM);
49
-  uint16_t preload = 255;                       // Lock 255 PWM resolution for high frequencies
50
-  int32_t prescaler = (HAL_TIMER_RATE) / (preload + 1) / f_desired - 1;
51
-  if (prescaler > 65535) {                      // For low frequencies increase prescaler
52
-    prescaler = 65535;
53
-    preload = (HAL_TIMER_RATE) / (prescaler + 1) / f_desired - 1;
54
-  }
55
-  if (prescaler < 0) return;                    // Too high frequency
56
-  timer_set_reload(timer, preload);
57
-  timer_set_prescaler(timer, prescaler);
58
-}
59
-
60 30
 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
61 31
   timer_dev *timer = PIN_MAP[pin].timer_device;
62 32
   uint16_t max_val = timer->regs.bas->ARR * v / v_size;
@@ -64,5 +34,35 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255
64 34
   pwmWrite(pin, max_val);
65 35
 }
66 36
 
37
+#if NEEDS_HARDWARE_PWM
38
+
39
+  void set_pwm_frequency(const pin_t pin, int f_desired) {
40
+    if (!PWM_PIN(pin)) return;                    // Don't proceed if no hardware timer
41
+
42
+    timer_dev *timer = PIN_MAP[pin].timer_device;
43
+    uint8_t channel = PIN_MAP[pin].timer_channel;
44
+
45
+    // Protect used timers
46
+    if (timer == get_timer_dev(TEMP_TIMER_NUM)) return;
47
+    if (timer == get_timer_dev(STEP_TIMER_NUM)) return;
48
+    #if PULSE_TIMER_NUM != STEP_TIMER_NUM
49
+      if (timer == get_timer_dev(PULSE_TIMER_NUM)) return;
50
+    #endif
51
+
52
+    if (!(timer->regs.bas->SR & TIMER_CR1_CEN))   // Ensure the timer is enabled
53
+      timer_init(timer);
54
+
55
+    timer_set_mode(timer, channel, TIMER_PWM);
56
+    uint16_t preload = 255;                       // Lock 255 PWM resolution for high frequencies
57
+    int32_t prescaler = (HAL_TIMER_RATE) / (preload + 1) / f_desired - 1;
58
+    if (prescaler > 65535) {                      // For low frequencies increase prescaler
59
+      prescaler = 65535;
60
+      preload = (HAL_TIMER_RATE) / (prescaler + 1) / f_desired - 1;
61
+    }
62
+    if (prescaler < 0) return;                    // Too high frequency
63
+    timer_set_reload(timer, preload);
64
+    timer_set_prescaler(timer, prescaler);
65
+  }
66
+
67 67
 #endif // NEEDS_HARDWARE_PWM
68 68
 #endif // __STM32F1__

+ 6
- 0
Marlin/src/HAL/TEENSY31_32/HAL.h View File

@@ -122,6 +122,12 @@ void HAL_adc_init();
122 122
 void HAL_adc_start_conversion(const uint8_t adc_pin);
123 123
 uint16_t HAL_adc_get_result();
124 124
 
125
+// PWM
126
+
127
+inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); }
128
+
129
+// Pin Map
130
+
125 131
 #define GET_PIN_MAP_PIN(index) index
126 132
 #define GET_PIN_MAP_INDEX(pin) pin
127 133
 #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)

+ 6
- 0
Marlin/src/HAL/TEENSY35_36/HAL.h View File

@@ -129,6 +129,12 @@ void HAL_adc_init();
129 129
 void HAL_adc_start_conversion(const uint8_t adc_pin);
130 130
 uint16_t HAL_adc_get_result();
131 131
 
132
+// PWM
133
+
134
+inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); }
135
+
136
+// Pin Map
137
+
132 138
 #define GET_PIN_MAP_PIN(index) index
133 139
 #define GET_PIN_MAP_INDEX(pin) pin
134 140
 #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)

+ 5
- 5
Marlin/src/HAL/TEENSY40_41/HAL.cpp View File

@@ -106,17 +106,17 @@ void HAL_adc_init() {
106 106
 void HAL_clear_reset_source() {
107 107
   uint32_t reset_source = SRC_SRSR;
108 108
   SRC_SRSR = reset_source;
109
- }
109
+}
110 110
 
111 111
 uint8_t HAL_get_reset_source() {
112 112
   switch (SRC_SRSR & 0xFF) {
113 113
     case 1: return RST_POWER_ON; break;
114 114
     case 2: return RST_SOFTWARE; break;
115 115
     case 4: return RST_EXTERNAL; break;
116
-    // case 8: return RST_BROWN_OUT; break;
116
+    //case 8: return RST_BROWN_OUT; break;
117 117
     case 16: return RST_WATCHDOG; break;
118
-     case 64: return RST_JTAG; break;
119
-    // case 128: return RST_OVERTEMP; break;
118
+    case 64: return RST_JTAG; break;
119
+    //case 128: return RST_OVERTEMP; break;
120 120
   }
121 121
   return 0;
122 122
 }
@@ -168,7 +168,7 @@ uint16_t HAL_adc_get_result() {
168 168
   return 0;
169 169
 }
170 170
 
171
-bool is_output(uint8_t pin) {
171
+bool is_output(pin_t pin) {
172 172
   const struct digital_pin_bitband_and_config_table_struct *p;
173 173
   p = digital_pin_to_info_PGM + pin;
174 174
   return (*(p->reg + 1) & p->mask);

+ 7
- 1
Marlin/src/HAL/TEENSY40_41/HAL.h View File

@@ -150,8 +150,14 @@ void HAL_adc_init();
150 150
 void HAL_adc_start_conversion(const uint8_t adc_pin);
151 151
 uint16_t HAL_adc_get_result();
152 152
 
153
+// PWM
154
+
155
+inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); }
156
+
157
+// Pin Map
158
+
153 159
 #define GET_PIN_MAP_PIN(index) index
154 160
 #define GET_PIN_MAP_INDEX(pin) pin
155 161
 #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
156 162
 
157
-bool is_output(uint8_t pin);
163
+bool is_output(pin_t pin);

+ 1
- 1
Marlin/src/feature/caselight.cpp View File

@@ -70,7 +70,7 @@ void CaseLight::update(const bool sflag) {
70 70
 
71 71
     #if CASELIGHT_USES_BRIGHTNESS
72 72
       if (pin_is_pwm())
73
-        analogWrite(pin_t(CASE_LIGHT_PIN), (
73
+        set_pwm_duty(pin_t(CASE_LIGHT_PIN), (
74 74
           #if CASE_LIGHT_MAX_PWM == 255
75 75
             n10ct
76 76
           #else

+ 4
- 3
Marlin/src/feature/controllerfan.cpp View File

@@ -72,9 +72,10 @@ void ControllerFan::update() {
72 72
       ? settings.active_speed : settings.idle_speed
73 73
     );
74 74
 
75
-    // Allow digital or PWM fan output (see M42 handling)
76
-    WRITE(CONTROLLER_FAN_PIN, speed);
77
-    analogWrite(pin_t(CONTROLLER_FAN_PIN), speed);
75
+    if (PWM_PIN(CONTROLLER_FAN_PIN))
76
+      set_pwm_duty(pin_t(CONTROLLER_FAN_PIN), speed);
77
+    else
78
+      WRITE(CONTROLLER_FAN_PIN, speed);
78 79
   }
79 80
 }
80 81
 

+ 5
- 5
Marlin/src/feature/leds/leds.cpp View File

@@ -121,11 +121,11 @@ void LEDLights::set_color(const LEDColor &incol
121 121
 
122 122
     // This variant uses 3-4 separate pins for the RGB(W) components.
123 123
     // If the pins can do PWM then their intensity will be set.
124
-    #define _UPDATE_RGBW(C,c) do {                \
125
-      if (PWM_PIN(RGB_LED_##C##_PIN))             \
126
-        analogWrite(pin_t(RGB_LED_##C##_PIN), c); \
127
-      else                                        \
128
-        WRITE(RGB_LED_##C##_PIN, c ? HIGH : LOW); \
124
+    #define _UPDATE_RGBW(C,c) do {                 \
125
+      if (PWM_PIN(RGB_LED_##C##_PIN))              \
126
+        set_pwm_duty(pin_t(RGB_LED_##C##_PIN), c); \
127
+      else                                         \
128
+        WRITE(RGB_LED_##C##_PIN, c ? HIGH : LOW);  \
129 129
     }while(0)
130 130
     #define UPDATE_RGBW(C,c) _UPDATE_RGBW(C, TERN1(CASE_LIGHT_USE_RGB_LED, caselight.on) ? incol.c : 0)
131 131
     UPDATE_RGBW(R,r); UPDATE_RGBW(G,g); UPDATE_RGBW(B,b);

+ 2
- 4
Marlin/src/feature/spindle_laser.cpp View File

@@ -66,7 +66,7 @@ void SpindleLaser::init() {
66 66
   #endif
67 67
   #if ENABLED(SPINDLE_LASER_USE_PWM)
68 68
     SET_PWM(SPINDLE_LASER_PWM_PIN);
69
-    analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Set to lowest speed
69
+    set_pwm_duty(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Set to lowest speed
70 70
   #endif
71 71
   #if ENABLED(HAL_CAN_SET_PWM_FREQ) && defined(SPINDLE_LASER_FREQUENCY)
72 72
     set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_FREQUENCY);
@@ -92,10 +92,8 @@ void SpindleLaser::init() {
92 92
   void SpindleLaser::_set_ocr(const uint8_t ocr) {
93 93
     #if NEEDS_HARDWARE_PWM && SPINDLE_LASER_FREQUENCY
94 94
       set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), TERN(MARLIN_DEV_MODE, frequency, SPINDLE_LASER_FREQUENCY));
95
-      set_pwm_duty(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF);
96
-    #else
97
-      analogWrite(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF);
98 95
     #endif
96
+    set_pwm_duty(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF);
99 97
   }
100 98
 
101 99
   void SpindleLaser::set_ocr(const uint8_t ocr) {

+ 2
- 2
Marlin/src/gcode/control/M42.cpp View File

@@ -126,10 +126,10 @@ void GcodeSuite::M42() {
126 126
   extDigitalWrite(pin, pin_status);
127 127
 
128 128
   #ifdef ARDUINO_ARCH_STM32
129
-    // A simple I/O will be set to 0 by analogWrite()
129
+    // A simple I/O will be set to 0 by set_pwm_duty()
130 130
     if (pin_status <= 1 && !PWM_PIN(pin)) return;
131 131
   #endif
132
-  analogWrite(pin, pin_status);
132
+  set_pwm_duty(pin, pin_status);
133 133
 }
134 134
 
135 135
 #endif // DIRECT_PIN_CONTROL

+ 1
- 1
Marlin/src/lcd/dogm/marlinui_DOGM.cpp View File

@@ -342,7 +342,7 @@ void MarlinUI::clear_lcd() { } // Automatically cleared by Picture Loop
342 342
   void MarlinUI::_set_brightness() {
343 343
     #if PIN_EXISTS(TFT_BACKLIGHT)
344 344
       if (PWM_PIN(TFT_BACKLIGHT_PIN))
345
-        analogWrite(pin_t(TFT_BACKLIGHT_PIN), brightness);
345
+        set_pwm_duty(pin_t(TFT_BACKLIGHT_PIN), brightness);
346 346
     #endif
347 347
   }
348 348
 #endif

+ 1
- 1
Marlin/src/lcd/tft/ui_common.cpp View File

@@ -213,7 +213,7 @@ void MarlinUI::clear_lcd() {
213 213
   void MarlinUI::_set_brightness() {
214 214
     #if PIN_EXISTS(TFT_BACKLIGHT)
215 215
       if (PWM_PIN(TFT_BACKLIGHT_PIN))
216
-        analogWrite(pin_t(TFT_BACKLIGHT_PIN), brightness);
216
+        set_pwm_duty(pin_t(TFT_BACKLIGHT_PIN), brightness);
217 217
     #endif
218 218
   }
219 219
 #endif

+ 1
- 1
Marlin/src/module/endstops.cpp View File

@@ -1342,7 +1342,7 @@ void Endstops::update() {
1342 1342
         ES_REPORT_CHANGE(K_MAX);
1343 1343
       #endif
1344 1344
       SERIAL_ECHOLNPGM("\n");
1345
-      analogWrite(pin_t(LED_PIN), local_LED_status);
1345
+      set_pwm_duty(pin_t(LED_PIN), local_LED_status);
1346 1346
       local_LED_status ^= 255;
1347 1347
       old_live_state_local = live_state_local;
1348 1348
     }

+ 3
- 3
Marlin/src/module/planner.cpp View File

@@ -1270,7 +1270,7 @@ void Planner::recalculate() {
1270 1270
     #elif ENABLED(FAST_PWM_FAN)
1271 1271
       #define _FAN_SET(F) set_pwm_duty(FAN##F##_PIN, CALC_FAN_SPEED(F));
1272 1272
     #else
1273
-      #define _FAN_SET(F) analogWrite(pin_t(FAN##F##_PIN), CALC_FAN_SPEED(F));
1273
+      #define _FAN_SET(F) set_pwm_duty(pin_t(FAN##F##_PIN), CALC_FAN_SPEED(F));
1274 1274
     #endif
1275 1275
     #define FAN_SET(F) do{ kickstart_fan(fan_speed, ms, F); _FAN_SET(F); }while(0)
1276 1276
 
@@ -1393,8 +1393,8 @@ void Planner::check_axes_activity() {
1393 1393
   TERN_(AUTOTEMP, autotemp_task());
1394 1394
 
1395 1395
   #if ENABLED(BARICUDA)
1396
-    TERN_(HAS_HEATER_1, analogWrite(pin_t(HEATER_1_PIN), tail_valve_pressure));
1397
-    TERN_(HAS_HEATER_2, analogWrite(pin_t(HEATER_2_PIN), tail_e_to_p_pressure));
1396
+    TERN_(HAS_HEATER_1, set_pwm_duty(pin_t(HEATER_1_PIN), tail_valve_pressure));
1397
+    TERN_(HAS_HEATER_2, set_pwm_duty(pin_t(HEATER_2_PIN), tail_e_to_p_pressure));
1398 1398
   #endif
1399 1399
 }
1400 1400
 

+ 1
- 1
Marlin/src/module/stepper.cpp View File

@@ -3253,7 +3253,7 @@ void Stepper::report_positions() {
3253 3253
 
3254 3254
       #elif HAS_MOTOR_CURRENT_PWM
3255 3255
 
3256
-        #define _WRITE_CURRENT_PWM(P) analogWrite(pin_t(MOTOR_CURRENT_PWM_## P ##_PIN), 255L * current / (MOTOR_CURRENT_PWM_RANGE))
3256
+        #define _WRITE_CURRENT_PWM(P) set_pwm_duty(pin_t(MOTOR_CURRENT_PWM_## P ##_PIN), 255L * current / (MOTOR_CURRENT_PWM_RANGE))
3257 3257
         switch (driver) {
3258 3258
           case 0:
3259 3259
             #if PIN_EXISTS(MOTOR_CURRENT_PWM_X)

+ 5
- 5
Marlin/src/module/temperature.cpp View File

@@ -887,11 +887,11 @@ int16_t Temperature::getHeaterPower(const heater_id_t heater_id) {
887 887
         SBI(fanState, pgm_read_byte(&fanBit[COOLER_FAN_INDEX]));
888 888
     #endif
889 889
 
890
-    #define _UPDATE_AUTO_FAN(P,D,A) do{                  \
891
-      if (PWM_PIN(P##_AUTO_FAN_PIN) && A < 255)          \
892
-        analogWrite(pin_t(P##_AUTO_FAN_PIN), D ? A : 0); \
893
-      else                                               \
894
-        WRITE(P##_AUTO_FAN_PIN, D);                      \
890
+    #define _UPDATE_AUTO_FAN(P,D,A) do{                   \
891
+      if (PWM_PIN(P##_AUTO_FAN_PIN) && A < 255)           \
892
+        set_pwm_duty(pin_t(P##_AUTO_FAN_PIN), D ? A : 0); \
893
+      else                                                \
894
+        WRITE(P##_AUTO_FAN_PIN, D);                       \
895 895
     }while(0)
896 896
 
897 897
     uint8_t fanDone = 0;

Loading…
Cancel
Save