Преглед изворни кода

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

Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
Andrei M пре 3 година
родитељ
комит
da830e6ced
No account linked to committer's email address

+ 1
- 1
Marlin/src/HAL/AVR/HAL.h Прегледај датотеку

221
 
221
 
222
 /**
222
 /**
223
  * set_pwm_duty
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
  *  Optionally allows inverting the duty cycle [default = false]
225
  *  Optionally allows inverting the duty cycle [default = false]
226
  *  Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
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 Прегледај датотеку

22
 #ifdef __AVR__
22
 #ifdef __AVR__
23
 
23
 
24
 #include "../../inc/MarlinConfigPre.h"
24
 #include "../../inc/MarlinConfigPre.h"
25
+#include "HAL.h"
25
 
26
 
26
 #if NEEDS_HARDWARE_PWM // Specific meta-flag for features that mandate PWM
27
 #if NEEDS_HARDWARE_PWM // Specific meta-flag for features that mandate PWM
27
 
28
 
28
-#include "HAL.h"
29
-
30
 struct Timer {
29
 struct Timer {
31
   volatile uint8_t* TCCRnQ[3];  // max 3 TCCR registers per timer
30
   volatile uint8_t* TCCRnQ[3];  // max 3 TCCR registers per timer
32
   volatile uint16_t* OCRnQ[3];  // max 3 OCR registers per timer
31
   volatile uint16_t* OCRnQ[3];  // max 3 OCR registers per timer
153
 
152
 
154
 void set_pwm_frequency(const pin_t pin, int f_desired) {
153
 void set_pwm_frequency(const pin_t pin, int f_desired) {
155
   Timer timer = get_pwm_timer(pin);
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
   uint16_t size;
156
   uint16_t size;
158
   if (timer.n == 2) size = 255; else size = 65535;
157
   if (timer.n == 2) size = 255; else size = 65535;
159
 
158
 
243
     _SET_ICRn(timer.ICRn, res);         // Set ICRn value (TOP) = res
242
     _SET_ICRn(timer.ICRn, res);         // Set ICRn value (TOP) = res
244
 }
243
 }
245
 
244
 
245
+#endif // NEEDS_HARDWARE_PWM
246
+
246
 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
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
 #endif // __AVR__
280
 #endif // __AVR__

+ 5
- 0
Marlin/src/HAL/DUE/HAL.h Прегледај датотеку

145
 uint16_t HAL_adc_get_result();
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
 // Pin Map
153
 // Pin Map
149
 //
154
 //
150
 #define GET_PIN_MAP_PIN(index) index
155
 #define GET_PIN_MAP_PIN(index) index

+ 4
- 0
Marlin/src/HAL/ESP32/HAL.h Прегледај датотеку

129
 
129
 
130
 void HAL_adc_start_conversion(const uint8_t adc_pin);
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
 #define GET_PIN_MAP_PIN(index) index
136
 #define GET_PIN_MAP_PIN(index) index
133
 #define GET_PIN_MAP_INDEX(pin) pin
137
 #define GET_PIN_MAP_INDEX(pin) pin
134
 #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
138
 #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)

+ 3
- 0
Marlin/src/HAL/LINUX/HAL.h Прегледај датотеку

101
 void HAL_adc_start_conversion(const uint8_t ch);
101
 void HAL_adc_start_conversion(const uint8_t ch);
102
 uint16_t HAL_adc_get_result();
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
 // Reset source
107
 // Reset source
105
 inline void HAL_clear_reset_source(void) {}
108
 inline void HAL_clear_reset_source(void) {}
106
 inline uint8_t HAL_get_reset_source(void) { return RST_POWER_ON; }
109
 inline uint8_t HAL_get_reset_source(void) { return RST_POWER_ON; }

+ 8
- 8
Marlin/src/HAL/LPC1768/fast_pwm.cpp Прегледај датотеку

22
 #ifdef TARGET_LPC1768
22
 #ifdef TARGET_LPC1768
23
 
23
 
24
 #include "../../inc/MarlinConfigPre.h"
24
 #include "../../inc/MarlinConfigPre.h"
25
-
26
-#if NEEDS_HARDWARE_PWM // Specific meta-flag for features that mandate PWM
27
-
28
 #include <pwm.h>
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
 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
27
 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
35
   LPC176x::pwm_write_ratio(pin, invert ? 1.0f - (float)v / v_size : (float)v / v_size);
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
 #endif // TARGET_LPC1768
39
 #endif // TARGET_LPC1768

+ 3
- 0
Marlin/src/HAL/NATIVE_SIM/HAL.h Прегледај датотеку

133
 void HAL_adc_start_conversion(const uint8_t ch);
133
 void HAL_adc_start_conversion(const uint8_t ch);
134
 uint16_t HAL_adc_get_result();
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
 // Reset source
139
 // Reset source
137
 inline void HAL_clear_reset_source(void) {}
140
 inline void HAL_clear_reset_source(void) {}
138
 inline uint8_t HAL_get_reset_source(void) { return RST_POWER_ON; }
141
 inline uint8_t HAL_get_reset_source(void) { return RST_POWER_ON; }

+ 5
- 0
Marlin/src/HAL/SAMD51/HAL.h Прегледај датотеку

128
 void HAL_adc_start_conversion(const uint8_t adc_pin);
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
 // Pin Map
136
 // Pin Map
132
 //
137
 //
133
 #define GET_PIN_MAP_PIN(index) index
138
 #define GET_PIN_MAP_PIN(index) index

+ 18
- 19
Marlin/src/HAL/STM32/fast_pwm.cpp Прегледај датотеку

24
 
24
 
25
 #ifdef HAL_STM32
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
 #include "timers.h"
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
 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
30
 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
48
   PinName pin_name = digitalPinToPinName(pin);
31
   PinName pin_name = digitalPinToPinName(pin);
49
   TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM);
32
   TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM);
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
 #endif // HAL_STM32
61
 #endif // HAL_STM32

+ 1
- 2
Marlin/src/HAL/STM32F1/HAL.cpp Прегледај датотеку

449
 
449
 
450
 // Wrapper to maple unprotected analogWrite
450
 // Wrapper to maple unprotected analogWrite
451
 void analogWrite(pin_t pin, int pwm_val8) {
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
 void HAL_reboot() { nvic_sys_reset(); }
455
 void HAL_reboot() { nvic_sys_reset(); }

+ 30
- 30
Marlin/src/HAL/STM32F1/fast_pwm.cpp Прегледај датотеку

23
 
23
 
24
 #include "../../inc/MarlinConfigPre.h"
24
 #include "../../inc/MarlinConfigPre.h"
25
 
25
 
26
-#if NEEDS_HARDWARE_PWM
27
-
28
 #include <pwm.h>
26
 #include <pwm.h>
29
 #include "HAL.h"
27
 #include "HAL.h"
30
 #include "timers.h"
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
 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
30
 void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
61
   timer_dev *timer = PIN_MAP[pin].timer_device;
31
   timer_dev *timer = PIN_MAP[pin].timer_device;
62
   uint16_t max_val = timer->regs.bas->ARR * v / v_size;
32
   uint16_t max_val = timer->regs.bas->ARR * v / v_size;
64
   pwmWrite(pin, max_val);
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
 #endif // NEEDS_HARDWARE_PWM
67
 #endif // NEEDS_HARDWARE_PWM
68
 #endif // __STM32F1__
68
 #endif // __STM32F1__

+ 6
- 0
Marlin/src/HAL/TEENSY31_32/HAL.h Прегледај датотеку

122
 void HAL_adc_start_conversion(const uint8_t adc_pin);
122
 void HAL_adc_start_conversion(const uint8_t adc_pin);
123
 uint16_t HAL_adc_get_result();
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
 #define GET_PIN_MAP_PIN(index) index
131
 #define GET_PIN_MAP_PIN(index) index
126
 #define GET_PIN_MAP_INDEX(pin) pin
132
 #define GET_PIN_MAP_INDEX(pin) pin
127
 #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
133
 #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)

+ 6
- 0
Marlin/src/HAL/TEENSY35_36/HAL.h Прегледај датотеку

129
 void HAL_adc_start_conversion(const uint8_t adc_pin);
129
 void HAL_adc_start_conversion(const uint8_t adc_pin);
130
 uint16_t HAL_adc_get_result();
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
 #define GET_PIN_MAP_PIN(index) index
138
 #define GET_PIN_MAP_PIN(index) index
133
 #define GET_PIN_MAP_INDEX(pin) pin
139
 #define GET_PIN_MAP_INDEX(pin) pin
134
 #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
140
 #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)

+ 5
- 5
Marlin/src/HAL/TEENSY40_41/HAL.cpp Прегледај датотеку

106
 void HAL_clear_reset_source() {
106
 void HAL_clear_reset_source() {
107
   uint32_t reset_source = SRC_SRSR;
107
   uint32_t reset_source = SRC_SRSR;
108
   SRC_SRSR = reset_source;
108
   SRC_SRSR = reset_source;
109
- }
109
+}
110
 
110
 
111
 uint8_t HAL_get_reset_source() {
111
 uint8_t HAL_get_reset_source() {
112
   switch (SRC_SRSR & 0xFF) {
112
   switch (SRC_SRSR & 0xFF) {
113
     case 1: return RST_POWER_ON; break;
113
     case 1: return RST_POWER_ON; break;
114
     case 2: return RST_SOFTWARE; break;
114
     case 2: return RST_SOFTWARE; break;
115
     case 4: return RST_EXTERNAL; break;
115
     case 4: return RST_EXTERNAL; break;
116
-    // case 8: return RST_BROWN_OUT; break;
116
+    //case 8: return RST_BROWN_OUT; break;
117
     case 16: return RST_WATCHDOG; break;
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
   return 0;
121
   return 0;
122
 }
122
 }
168
   return 0;
168
   return 0;
169
 }
169
 }
170
 
170
 
171
-bool is_output(uint8_t pin) {
171
+bool is_output(pin_t pin) {
172
   const struct digital_pin_bitband_and_config_table_struct *p;
172
   const struct digital_pin_bitband_and_config_table_struct *p;
173
   p = digital_pin_to_info_PGM + pin;
173
   p = digital_pin_to_info_PGM + pin;
174
   return (*(p->reg + 1) & p->mask);
174
   return (*(p->reg + 1) & p->mask);

+ 7
- 1
Marlin/src/HAL/TEENSY40_41/HAL.h Прегледај датотеку

150
 void HAL_adc_start_conversion(const uint8_t adc_pin);
150
 void HAL_adc_start_conversion(const uint8_t adc_pin);
151
 uint16_t HAL_adc_get_result();
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
 #define GET_PIN_MAP_PIN(index) index
159
 #define GET_PIN_MAP_PIN(index) index
154
 #define GET_PIN_MAP_INDEX(pin) pin
160
 #define GET_PIN_MAP_INDEX(pin) pin
155
 #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
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 Прегледај датотеку

70
 
70
 
71
     #if CASELIGHT_USES_BRIGHTNESS
71
     #if CASELIGHT_USES_BRIGHTNESS
72
       if (pin_is_pwm())
72
       if (pin_is_pwm())
73
-        analogWrite(pin_t(CASE_LIGHT_PIN), (
73
+        set_pwm_duty(pin_t(CASE_LIGHT_PIN), (
74
           #if CASE_LIGHT_MAX_PWM == 255
74
           #if CASE_LIGHT_MAX_PWM == 255
75
             n10ct
75
             n10ct
76
           #else
76
           #else

+ 4
- 3
Marlin/src/feature/controllerfan.cpp Прегледај датотеку

72
       ? settings.active_speed : settings.idle_speed
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 Прегледај датотеку

121
 
121
 
122
     // This variant uses 3-4 separate pins for the RGB(W) components.
122
     // This variant uses 3-4 separate pins for the RGB(W) components.
123
     // If the pins can do PWM then their intensity will be set.
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
     }while(0)
129
     }while(0)
130
     #define UPDATE_RGBW(C,c) _UPDATE_RGBW(C, TERN1(CASE_LIGHT_USE_RGB_LED, caselight.on) ? incol.c : 0)
130
     #define UPDATE_RGBW(C,c) _UPDATE_RGBW(C, TERN1(CASE_LIGHT_USE_RGB_LED, caselight.on) ? incol.c : 0)
131
     UPDATE_RGBW(R,r); UPDATE_RGBW(G,g); UPDATE_RGBW(B,b);
131
     UPDATE_RGBW(R,r); UPDATE_RGBW(G,g); UPDATE_RGBW(B,b);

+ 2
- 4
Marlin/src/feature/spindle_laser.cpp Прегледај датотеку

66
   #endif
66
   #endif
67
   #if ENABLED(SPINDLE_LASER_USE_PWM)
67
   #if ENABLED(SPINDLE_LASER_USE_PWM)
68
     SET_PWM(SPINDLE_LASER_PWM_PIN);
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
   #endif
70
   #endif
71
   #if ENABLED(HAL_CAN_SET_PWM_FREQ) && defined(SPINDLE_LASER_FREQUENCY)
71
   #if ENABLED(HAL_CAN_SET_PWM_FREQ) && defined(SPINDLE_LASER_FREQUENCY)
72
     set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_FREQUENCY);
72
     set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_FREQUENCY);
92
   void SpindleLaser::_set_ocr(const uint8_t ocr) {
92
   void SpindleLaser::_set_ocr(const uint8_t ocr) {
93
     #if NEEDS_HARDWARE_PWM && SPINDLE_LASER_FREQUENCY
93
     #if NEEDS_HARDWARE_PWM && SPINDLE_LASER_FREQUENCY
94
       set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), TERN(MARLIN_DEV_MODE, frequency, SPINDLE_LASER_FREQUENCY));
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
     #endif
95
     #endif
96
+    set_pwm_duty(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF);
99
   }
97
   }
100
 
98
 
101
   void SpindleLaser::set_ocr(const uint8_t ocr) {
99
   void SpindleLaser::set_ocr(const uint8_t ocr) {

+ 2
- 2
Marlin/src/gcode/control/M42.cpp Прегледај датотеку

126
   extDigitalWrite(pin, pin_status);
126
   extDigitalWrite(pin, pin_status);
127
 
127
 
128
   #ifdef ARDUINO_ARCH_STM32
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
     if (pin_status <= 1 && !PWM_PIN(pin)) return;
130
     if (pin_status <= 1 && !PWM_PIN(pin)) return;
131
   #endif
131
   #endif
132
-  analogWrite(pin, pin_status);
132
+  set_pwm_duty(pin, pin_status);
133
 }
133
 }
134
 
134
 
135
 #endif // DIRECT_PIN_CONTROL
135
 #endif // DIRECT_PIN_CONTROL

+ 1
- 1
Marlin/src/lcd/dogm/marlinui_DOGM.cpp Прегледај датотеку

342
   void MarlinUI::_set_brightness() {
342
   void MarlinUI::_set_brightness() {
343
     #if PIN_EXISTS(TFT_BACKLIGHT)
343
     #if PIN_EXISTS(TFT_BACKLIGHT)
344
       if (PWM_PIN(TFT_BACKLIGHT_PIN))
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
     #endif
346
     #endif
347
   }
347
   }
348
 #endif
348
 #endif

+ 1
- 1
Marlin/src/lcd/tft/ui_common.cpp Прегледај датотеку

213
   void MarlinUI::_set_brightness() {
213
   void MarlinUI::_set_brightness() {
214
     #if PIN_EXISTS(TFT_BACKLIGHT)
214
     #if PIN_EXISTS(TFT_BACKLIGHT)
215
       if (PWM_PIN(TFT_BACKLIGHT_PIN))
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
     #endif
217
     #endif
218
   }
218
   }
219
 #endif
219
 #endif

+ 1
- 1
Marlin/src/module/endstops.cpp Прегледај датотеку

1342
         ES_REPORT_CHANGE(K_MAX);
1342
         ES_REPORT_CHANGE(K_MAX);
1343
       #endif
1343
       #endif
1344
       SERIAL_ECHOLNPGM("\n");
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
       local_LED_status ^= 255;
1346
       local_LED_status ^= 255;
1347
       old_live_state_local = live_state_local;
1347
       old_live_state_local = live_state_local;
1348
     }
1348
     }

+ 3
- 3
Marlin/src/module/planner.cpp Прегледај датотеку

1270
     #elif ENABLED(FAST_PWM_FAN)
1270
     #elif ENABLED(FAST_PWM_FAN)
1271
       #define _FAN_SET(F) set_pwm_duty(FAN##F##_PIN, CALC_FAN_SPEED(F));
1271
       #define _FAN_SET(F) set_pwm_duty(FAN##F##_PIN, CALC_FAN_SPEED(F));
1272
     #else
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
     #endif
1274
     #endif
1275
     #define FAN_SET(F) do{ kickstart_fan(fan_speed, ms, F); _FAN_SET(F); }while(0)
1275
     #define FAN_SET(F) do{ kickstart_fan(fan_speed, ms, F); _FAN_SET(F); }while(0)
1276
 
1276
 
1393
   TERN_(AUTOTEMP, autotemp_task());
1393
   TERN_(AUTOTEMP, autotemp_task());
1394
 
1394
 
1395
   #if ENABLED(BARICUDA)
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
   #endif
1398
   #endif
1399
 }
1399
 }
1400
 
1400
 

+ 1
- 1
Marlin/src/module/stepper.cpp Прегледај датотеку

3253
 
3253
 
3254
       #elif HAS_MOTOR_CURRENT_PWM
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
         switch (driver) {
3257
         switch (driver) {
3258
           case 0:
3258
           case 0:
3259
             #if PIN_EXISTS(MOTOR_CURRENT_PWM_X)
3259
             #if PIN_EXISTS(MOTOR_CURRENT_PWM_X)

+ 5
- 5
Marlin/src/module/temperature.cpp Прегледај датотеку

887
         SBI(fanState, pgm_read_byte(&fanBit[COOLER_FAN_INDEX]));
887
         SBI(fanState, pgm_read_byte(&fanBit[COOLER_FAN_INDEX]));
888
     #endif
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
     }while(0)
895
     }while(0)
896
 
896
 
897
     uint8_t fanDone = 0;
897
     uint8_t fanDone = 0;

Loading…
Откажи
Сачувај