Browse Source

FAST_PWM for STM32F1 (#19952)

Victor 4 years ago
parent
commit
de315c97b1
No account linked to committer's email address

+ 17
- 0
Marlin/src/HAL/STM32F1/HAL.h View File

@@ -244,3 +244,20 @@ void analogWrite(pin_t pin, int pwm_val8); // PWM only! mul by 257 in maple!?
244 244
 
245 245
 #define PLATFORM_M997_SUPPORT
246 246
 void flashFirmware(const int16_t);
247
+
248
+#define HAL_CAN_SET_PWM_FREQ   // This HAL supports PWM Frequency adjustment
249
+
250
+/**
251
+ * set_pwm_frequency
252
+ *  Set the frequency of the timer corresponding to the provided pin
253
+ *  All Timer PWM pins run at the same frequency
254
+ */
255
+void set_pwm_frequency(const pin_t pin, int f_desired);
256
+
257
+/**
258
+ * set_pwm_duty
259
+ *  Set the PWM duty cycle of the provided pin to the provided value
260
+ *  Optionally allows inverting the duty cycle [default = false]
261
+ *  Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
262
+ */
263
+void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);

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

@@ -0,0 +1,68 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+#ifdef __STM32F1__
23
+
24
+#include "../../inc/MarlinConfigPre.h"
25
+
26
+#if NEEDS_HARDWARE_PWM
27
+
28
+#include <pwm.h>
29
+#include "HAL.h"
30
+#include "timers.h"
31
+
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*/) {
61
+  timer_dev *timer = PIN_MAP[pin].timer_device;
62
+  uint16_t max_val = timer->regs.bas->ARR * v / v_size;
63
+  if (invert) max_val = v_size - max_val;
64
+  pwmWrite(pin, max_val);
65
+}
66
+
67
+#endif // NEEDS_HARDWARE_PWM
68
+#endif // __STM32F1__

+ 0
- 4
Marlin/src/HAL/STM32F1/inc/SanityCheck.h View File

@@ -25,10 +25,6 @@
25 25
  * Test STM32F1-specific configuration values for errors at compile-time.
26 26
  */
27 27
 
28
-#if ENABLED(FAST_PWM_FAN) || SPINDLE_LASER_FREQUENCY
29
-  #error "Features requiring Hardware PWM (FAST_PWM_FAN, SPINDLE_LASER_FREQUENCY) are not yet supported on STM32F1."
30
-#endif
31
-
32 28
 #if !defined(HAVE_SW_SERIAL) && HAS_TMC_SW_SERIAL
33 29
   #warning "With TMC2208/9 consider using SoftwareSerialM with HAVE_SW_SERIAL and appropriate SS_TIMER."
34 30
   #error "Missing SoftwareSerial implementation."

+ 2
- 4
Marlin/src/lcd/menu/menu_spindle_laser.cpp View File

@@ -55,10 +55,8 @@
55 55
       #endif
56 56
     }
57 57
 
58
-    #if ENABLED(MARLIN_DEV_MODE)
59
-      #if ENABLED(HAL_CAN_SET_PWM_FREQ) && defined(SPINDLE_LASER_FREQUENCY)
60
-        EDIT_ITEM_FAST(CUTTER_MENU_FREQUENCY_TYPE, MSG_CUTTER_FREQUENCY, &cutter.frequency, 2000, 50000, cutter.refresh_frequency);
61
-      #endif
58
+    #if BOTH(MARLIN_DEV_MODE, HAL_CAN_SET_PWM_FREQ) && defined(SPINDLE_LASER_FREQUENCY)
59
+      EDIT_ITEM_FAST(CUTTER_MENU_FREQUENCY_TYPE, MSG_CUTTER_FREQUENCY, &cutter.frequency, 2000, 50000, cutter.refresh_frequency);
62 60
     #endif
63 61
     END_MENU();
64 62
   }

Loading…
Cancel
Save