Sfoglia il codice sorgente

♻️ Refactor HAL as singleton (#23357)

Scott Lahteine 2 anni fa
parent
commit
44eff9a233
79 ha cambiato i file con 1953 aggiunte e 1395 eliminazioni
  1. 20
    20
      Marlin/src/HAL/AVR/HAL.cpp
  2. 110
    74
      Marlin/src/HAL/AVR/HAL.h
  3. 2
    2
      Marlin/src/HAL/AVR/MarlinSerial.cpp
  4. 3
    3
      Marlin/src/HAL/AVR/MarlinSerial.h
  5. 3
    3
      Marlin/src/HAL/AVR/fast_pwm.cpp
  6. 2
    2
      Marlin/src/HAL/AVR/math.h
  7. 3
    3
      Marlin/src/HAL/AVR/timers.h
  8. 9
    34
      Marlin/src/HAL/DUE/HAL.cpp
  9. 87
    48
      Marlin/src/HAL/DUE/HAL.h
  10. 2
    2
      Marlin/src/HAL/DUE/MarlinSerial.cpp
  11. 1
    1
      Marlin/src/HAL/DUE/Tone.cpp
  12. 1
    1
      Marlin/src/HAL/DUE/timers.h
  13. 17
    18
      Marlin/src/HAL/ESP32/HAL.cpp
  14. 87
    57
      Marlin/src/HAL/ESP32/HAL.h
  15. 1
    1
      Marlin/src/HAL/ESP32/Tone.cpp
  16. 1
    1
      Marlin/src/HAL/ESP32/timers.cpp
  17. 3
    3
      Marlin/src/HAL/ESP32/timers.h
  18. 10
    27
      Marlin/src/HAL/LINUX/HAL.cpp
  19. 94
    49
      Marlin/src/HAL/LINUX/HAL.h
  20. 1
    3
      Marlin/src/HAL/LINUX/arduino.cpp
  21. 3
    4
      Marlin/src/HAL/LINUX/include/Arduino.h
  22. 2
    2
      Marlin/src/HAL/LINUX/timers.h
  23. 20
    22
      Marlin/src/HAL/LPC1768/HAL.cpp
  24. 98
    59
      Marlin/src/HAL/LPC1768/HAL.h
  25. 2
    1
      Marlin/src/HAL/LPC1768/Servo.h
  26. 3
    3
      Marlin/src/HAL/LPC1768/fast_pwm.cpp
  27. 3
    3
      Marlin/src/HAL/LPC1768/main.cpp
  28. 1
    1
      Marlin/src/HAL/LPC1768/timers.h
  29. 107
    66
      Marlin/src/HAL/NATIVE_SIM/HAL.h
  30. 2
    2
      Marlin/src/HAL/NATIVE_SIM/timers.h
  31. 182
    47
      Marlin/src/HAL/SAMD51/HAL.cpp
  32. 83
    46
      Marlin/src/HAL/SAMD51/HAL.h
  33. 10
    18
      Marlin/src/HAL/STM32/HAL.cpp
  34. 100
    76
      Marlin/src/HAL/STM32/HAL.h
  35. 4
    4
      Marlin/src/HAL/STM32/HAL_SPI.cpp
  36. 4
    4
      Marlin/src/HAL/STM32/eeprom_flash.cpp
  37. 2
    2
      Marlin/src/HAL/STM32/fast_pwm.cpp
  38. 5
    1
      Marlin/src/HAL/STM32/pinsDebug.h
  39. 2
    2
      Marlin/src/HAL/STM32/timers.h
  40. 88
    97
      Marlin/src/HAL/STM32F1/HAL.cpp
  41. 100
    80
      Marlin/src/HAL/STM32F1/HAL.h
  42. 8
    0
      Marlin/src/HAL/STM32F1/SPI.cpp
  43. 0
    8
      Marlin/src/HAL/STM32F1/SPI.h
  44. 2
    1
      Marlin/src/HAL/STM32F1/Servo.h
  45. 3
    5
      Marlin/src/HAL/STM32F1/fast_pwm.cpp
  46. 1
    1
      Marlin/src/HAL/STM32F1/timers.h
  47. 41
    38
      Marlin/src/HAL/TEENSY31_32/HAL.cpp
  48. 92
    38
      Marlin/src/HAL/TEENSY31_32/HAL.h
  49. 1
    1
      Marlin/src/HAL/TEENSY31_32/timers.h
  50. 57
    53
      Marlin/src/HAL/TEENSY35_36/HAL.cpp
  51. 93
    39
      Marlin/src/HAL/TEENSY35_36/HAL.h
  52. 1
    1
      Marlin/src/HAL/TEENSY35_36/timers.h
  53. 100
    94
      Marlin/src/HAL/TEENSY40_41/HAL.cpp
  54. 100
    47
      Marlin/src/HAL/TEENSY40_41/HAL.h
  55. 1
    1
      Marlin/src/HAL/TEENSY40_41/timers.h
  56. 36
    0
      Marlin/src/HAL/shared/HAL.cpp
  57. 4
    4
      Marlin/src/HAL/shared/HAL_spi_L6470.cpp
  58. 1
    1
      Marlin/src/HAL/shared/math_32bit.h
  59. 21
    27
      Marlin/src/MarlinCore.cpp
  60. 1
    1
      Marlin/src/feature/caselight.cpp
  61. 1
    1
      Marlin/src/feature/controllerfan.cpp
  62. 3
    1
      Marlin/src/feature/e_parser.h
  63. 5
    5
      Marlin/src/feature/leds/leds.cpp
  64. 4
    4
      Marlin/src/feature/spindle_laser.cpp
  65. 1
    1
      Marlin/src/feature/spindle_laser.h
  66. 2
    2
      Marlin/src/gcode/control/M42.cpp
  67. 5
    5
      Marlin/src/gcode/gcode_d.cpp
  68. 1
    0
      Marlin/src/inc/MarlinConfig.h
  69. 7
    0
      Marlin/src/inc/SanityCheck.h
  70. 2
    2
      Marlin/src/lcd/dogm/marlinui_DOGM.cpp
  71. 1
    1
      Marlin/src/lcd/e3v2/proui/dwin.cpp
  72. 1
    1
      Marlin/src/module/endstops.cpp
  73. 4
    4
      Marlin/src/module/planner.cpp
  74. 1
    1
      Marlin/src/module/servo.cpp
  75. 1
    1
      Marlin/src/module/servo.h
  76. 13
    13
      Marlin/src/module/stepper.cpp
  77. 57
    98
      Marlin/src/module/temperature.cpp
  78. 1
    1
      ini/features.ini
  79. 2
    2
      ini/native.ini

+ 20
- 20
Marlin/src/HAL/AVR/HAL.cpp Vedi File

@@ -36,7 +36,7 @@
36 36
 // ------------------------
37 37
 
38 38
 // Don't initialize/override variable (which would happen in .init4)
39
-uint8_t reset_reason __attribute__((section(".noinit")));
39
+uint8_t MarlinHAL::reset_reason __attribute__((section(".noinit")));
40 40
 
41 41
 // ------------------------
42 42
 // Public functions
@@ -45,22 +45,22 @@ uint8_t reset_reason __attribute__((section(".noinit")));
45 45
 __attribute__((naked))             // Don't output function pro- and epilogue
46 46
 __attribute__((used))              // Output the function, even if "not used"
47 47
 __attribute__((section(".init3"))) // Put in an early user definable section
48
-void HAL_save_reset_reason() {
48
+void save_reset_reason() {
49 49
   #if ENABLED(OPTIBOOT_RESET_REASON)
50 50
     __asm__ __volatile__(
51 51
       A("STS %0, r2")
52
-      : "=m"(reset_reason)
52
+      : "=m"(hal.reset_reason)
53 53
     );
54 54
   #else
55
-    reset_reason = MCUSR;
55
+    hal.reset_reason = MCUSR;
56 56
   #endif
57 57
 
58 58
   // Clear within 16ms since WDRF bit enables a 16ms watchdog timer -> Boot loop
59
-  MCUSR = 0;
59
+  hal.clear_reset_source();
60 60
   wdt_disable();
61 61
 }
62 62
 
63
-void HAL_init() {
63
+void MarlinHAL::init() {
64 64
   // Init Servo Pins
65 65
   #define INIT_SERVO(N) OUT_WRITE(SERVO##N##_PIN, LOW)
66 66
   #if HAS_SERVO_0
@@ -79,7 +79,7 @@ void HAL_init() {
79 79
   init_pwm_timers();   // Init user timers to default frequency - 1000HZ
80 80
 }
81 81
 
82
-void HAL_reboot() {
82
+void MarlinHAL::reboot() {
83 83
   #if ENABLED(USE_WATCHDOG)
84 84
     while (1) { /* run out the watchdog */ }
85 85
   #else
@@ -95,20 +95,20 @@ void HAL_reboot() {
95 95
 
96 96
 #else // !SDSUPPORT
97 97
 
98
-extern "C" {
99
-  extern char __bss_end;
100
-  extern char __heap_start;
101
-  extern void* __brkval;
102
-
103
-  int freeMemory() {
104
-    int free_memory;
105
-    if ((int)__brkval == 0)
106
-      free_memory = ((int)&free_memory) - ((int)&__bss_end);
107
-    else
108
-      free_memory = ((int)&free_memory) - ((int)__brkval);
109
-    return free_memory;
98
+  extern "C" {
99
+    extern char __bss_end;
100
+    extern char __heap_start;
101
+    extern void* __brkval;
102
+
103
+    int freeMemory() {
104
+      int free_memory;
105
+      if ((int)__brkval == 0)
106
+        free_memory = ((int)&free_memory) - ((int)&__bss_end);
107
+      else
108
+        free_memory = ((int)&free_memory) - ((int)__brkval);
109
+      return free_memory;
110
+    }
110 111
   }
111
-}
112 112
 
113 113
 #endif // !SDSUPPORT
114 114
 

+ 110
- 74
Marlin/src/HAL/AVR/HAL.h Vedi File

@@ -74,9 +74,9 @@
74 74
   #define CRITICAL_SECTION_START()  unsigned char _sreg = SREG; cli()
75 75
   #define CRITICAL_SECTION_END()    SREG = _sreg
76 76
 #endif
77
-#define ISRS_ENABLED() TEST(SREG, SREG_I)
78
-#define ENABLE_ISRS()  sei()
79
-#define DISABLE_ISRS() cli()
77
+
78
+#define HAL_CAN_SET_PWM_FREQ   // This HAL supports PWM Frequency adjustment
79
+#define PWM_FREQUENCY 1000     // Default PWM frequency when set_pwm_duty() is called without set_pwm_frequency()
80 80
 
81 81
 // ------------------------
82 82
 // Types
@@ -84,16 +84,15 @@
84 84
 
85 85
 typedef int8_t pin_t;
86 86
 
87
-#define SHARED_SERVOS HAS_SERVOS
88
-#define HAL_SERVO_LIB Servo
87
+#define SHARED_SERVOS HAS_SERVOS  // Use shared/servos.cpp
88
+
89
+class Servo;
90
+typedef Servo hal_servo_t;
89 91
 
90 92
 // ------------------------
91
-// Public Variables
93
+// Serial ports
92 94
 // ------------------------
93 95
 
94
-extern uint8_t reset_reason;
95
-
96
-// Serial ports
97 96
 #ifdef USBCON
98 97
   #include "../../core/serial_hook.h"
99 98
   typedef ForwardSerial1Class< decltype(Serial) > DefaultSerial1;
@@ -142,20 +141,31 @@ extern uint8_t reset_reason;
142 141
   #endif
143 142
 #endif
144 143
 
145
-// ------------------------
146
-// Public functions
147
-// ------------------------
144
+//
145
+// ADC
146
+//
147
+#define HAL_ADC_VREF        5.0
148
+#define HAL_ADC_RESOLUTION 10
148 149
 
149
-void HAL_init();
150
+//
151
+// Pin Mapping for M42, M43, M226
152
+//
153
+#define GET_PIN_MAP_PIN(index) index
154
+#define GET_PIN_MAP_INDEX(pin) pin
155
+#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
150 156
 
151
-//void cli();
157
+#define HAL_SENSITIVE_PINS 0, 1,
152 158
 
153
-//void _delay_ms(const int delay);
159
+#ifdef __AVR_AT90USB1286__
160
+  #define JTAG_DISABLE() do{ MCUCR = 0x80; MCUCR = 0x80; }while(0)
161
+#endif
154 162
 
155
-inline void HAL_clear_reset_source() { }
156
-inline uint8_t HAL_get_reset_source() { return reset_reason; }
163
+// AVR compatibility
164
+#define strtof strtod
157 165
 
158
-void HAL_reboot();
166
+// ------------------------
167
+// Class Utilities
168
+// ------------------------
159 169
 
160 170
 #pragma GCC diagnostic push
161 171
 #if GCC_VERSION <= 50000
@@ -166,70 +176,96 @@ extern "C" int freeMemory();
166 176
 
167 177
 #pragma GCC diagnostic pop
168 178
 
169
-// ADC
170
-#ifdef DIDR2
171
-  #define HAL_ANALOG_SELECT(ind) do{ if (ind < 8) SBI(DIDR0, ind); else SBI(DIDR2, ind & 0x07); }while(0)
172
-#else
173
-  #define HAL_ANALOG_SELECT(ind) SBI(DIDR0, ind);
174
-#endif
179
+// ------------------------
180
+// MarlinHAL Class
181
+// ------------------------
175 182
 
176
-inline void HAL_adc_init() {
177
-  ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
178
-  DIDR0 = 0;
179
-  #ifdef DIDR2
180
-    DIDR2 = 0;
181
-  #endif
182
-}
183
+class MarlinHAL {
184
+public:
183 185
 
184
-#define SET_ADMUX_ADCSRA(ch) ADMUX = _BV(REFS0) | (ch & 0x07); SBI(ADCSRA, ADSC)
185
-#ifdef MUX5
186
-  #define HAL_START_ADC(ch) if (ch > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(ch)
187
-#else
188
-  #define HAL_START_ADC(ch) ADCSRB = 0; SET_ADMUX_ADCSRA(ch)
189
-#endif
186
+  // Earliest possible init, before setup()
187
+  MarlinHAL() {}
190 188
 
191
-#define HAL_ADC_VREF        5.0
192
-#define HAL_ADC_RESOLUTION 10
193
-#define HAL_READ_ADC()  ADC
194
-#define HAL_ADC_READY() !TEST(ADCSRA, ADSC)
189
+  static void init();          // Called early in setup()
190
+  static void init_board() {}  // Called less early in setup()
191
+  static void reboot();        // Restart the firmware from 0x0
195 192
 
196
-#define GET_PIN_MAP_PIN(index) index
197
-#define GET_PIN_MAP_INDEX(pin) pin
198
-#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
193
+  // Interrupts
194
+  static bool isr_state() { return TEST(SREG, SREG_I); }
195
+  static void isr_on()  { sei(); }
196
+  static void isr_off() { cli(); }
199 197
 
200
-#define HAL_SENSITIVE_PINS 0, 1,
198
+  static void delay_ms(const int ms) { _delay_ms(ms); }
201 199
 
202
-#ifdef __AVR_AT90USB1286__
203
-  #define JTAG_DISABLE() do{ MCUCR = 0x80; MCUCR = 0x80; }while(0)
204
-#endif
200
+  // Tasks, called from idle()
201
+  static void idletask() {}
205 202
 
206
-// AVR compatibility
207
-#define strtof strtod
203
+  // Reset
204
+  static uint8_t reset_reason;
205
+  static uint8_t get_reset_source() { return reset_reason; }
206
+  static void clear_reset_source() { MCUSR = 0; }
208 207
 
209
-#define HAL_CAN_SET_PWM_FREQ   // This HAL supports PWM Frequency adjustment
210
-#define PWM_FREQUENCY 1000     // Default PWM frequency when set_pwm_duty() is called without set_pwm_frequency()
208
+  // Free SRAM
209
+  static int freeMemory() { return ::freeMemory(); }
211 210
 
212
-/**
213
- *  set_pwm_frequency
214
- *  Sets the frequency of the timer corresponding to the provided pin
215
- *  as close as possible to the provided desired frequency. Internally
216
- *  calculates the required waveform generation mode, prescaler and
217
- *  resolution values required and sets the timer registers accordingly.
218
- *  NOTE that the frequency is applied to all pins on the timer (Ex OC3A, OC3B and OC3B)
219
- *  NOTE that there are limitations, particularly if using TIMER2. (see Configuration_adv.h -> FAST FAN PWM Settings)
220
- */
221
-void set_pwm_frequency(const pin_t pin, const uint16_t f_desired);
211
+  //
212
+  // ADC Methods
213
+  //
222 214
 
223
-/**
224
- * set_pwm_duty
225
- *  Set the PWM duty cycle of the provided pin to the provided value
226
- *  Optionally allows inverting the duty cycle [default = false]
227
- *  Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
228
- */
229
-void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);
215
+  // Called by Temperature::init once at startup
216
+  static void adc_init() {
217
+    ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
218
+    DIDR0 = 0;
219
+    #ifdef DIDR2
220
+      DIDR2 = 0;
221
+    #endif
222
+  }
230 223
 
231
-/*
232
- * init_pwm_timers
233
- * sets the default frequency for timers 2-5 to 1000HZ
234
- */
235
-void init_pwm_timers();
224
+  // Called by Temperature::init for each sensor at startup
225
+  static void adc_enable(const uint8_t ch) {
226
+    #ifdef DIDR2
227
+      if (ch > 7) { SBI(DIDR2, ch & 0x07); return; }
228
+    #endif
229
+    SBI(DIDR0, ch);
230
+  }
231
+
232
+  // Begin ADC sampling on the given channel
233
+  static void adc_start(const uint8_t ch) {
234
+    #ifdef MUX5
235
+      ADCSRB = ch > 7 ? _BV(MUX5) : 0;
236
+    #else
237
+      ADCSRB = 0;
238
+    #endif
239
+    ADMUX = _BV(REFS0) | (ch & 0x07);
240
+    SBI(ADCSRA, ADSC);
241
+  }
242
+
243
+  // Is the ADC ready for reading?
244
+  static bool adc_ready() { return !TEST(ADCSRA, ADSC); }
245
+
246
+  // The current value of the ADC register
247
+  static __typeof__(ADC) adc_value() { return ADC; }
248
+
249
+  /**
250
+   * init_pwm_timers
251
+   * Set the default frequency for timers 2-5 to 1000HZ
252
+   */
253
+  static void init_pwm_timers();
254
+
255
+  /**
256
+   * Set the PWM duty cycle for the pin to the given value.
257
+   * Optionally invert the duty cycle [default = false]
258
+   * Optionally change the scale of the provided value to enable finer PWM duty control [default = 255]
259
+   */
260
+  static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);
261
+
262
+  /**
263
+   * Set the frequency of the timer for the given pin as close as
264
+   * possible to the provided desired frequency. Internally calculate
265
+   * the required waveform generation mode, prescaler, and resolution
266
+   * values and set timer registers accordingly.
267
+   * NOTE that the frequency is applied to all pins on the timer (Ex OC3A, OC3B and OC3B)
268
+   * NOTE that there are limitations, particularly if using TIMER2. (see Configuration_adv.h -> FAST_PWM_FAN Settings)
269
+   */
270
+  static void set_pwm_frequency(const pin_t pin, const uint16_t f_desired);
271
+};

+ 2
- 2
Marlin/src/HAL/AVR/MarlinSerial.cpp Vedi File

@@ -486,7 +486,7 @@ void MarlinSerial<Cfg>::write(const uint8_t c) {
486 486
     const uint8_t i = (tx_buffer.head + 1) & (Cfg::TX_SIZE - 1);
487 487
 
488 488
     // If global interrupts are disabled (as the result of being called from an ISR)...
489
-    if (!ISRS_ENABLED()) {
489
+    if (!hal.isr_state()) {
490 490
 
491 491
       // Make room by polling if it is possible to transmit, and do so!
492 492
       while (i == tx_buffer.tail) {
@@ -534,7 +534,7 @@ void MarlinSerial<Cfg>::flushTX() {
534 534
     if (!_written) return;
535 535
 
536 536
     // If global interrupts are disabled (as the result of being called from an ISR)...
537
-    if (!ISRS_ENABLED()) {
537
+    if (!hal.isr_state()) {
538 538
 
539 539
       // Wait until everything was transmitted - We must do polling, as interrupts are disabled
540 540
       while (tx_buffer.head != tx_buffer.tail || !B_TXC) {

+ 3
- 3
Marlin/src/HAL/AVR/MarlinSerial.h Vedi File

@@ -191,13 +191,13 @@
191 191
                    rx_framing_errors;
192 192
     static ring_buffer_pos_t rx_max_enqueued;
193 193
 
194
-    static FORCE_INLINE ring_buffer_pos_t atomic_read_rx_head();
194
+    FORCE_INLINE static ring_buffer_pos_t atomic_read_rx_head();
195 195
 
196 196
     static volatile bool rx_tail_value_not_stable;
197 197
     static volatile uint16_t rx_tail_value_backup;
198 198
 
199
-    static FORCE_INLINE void atomic_set_rx_tail(ring_buffer_pos_t value);
200
-    static FORCE_INLINE ring_buffer_pos_t atomic_read_rx_tail();
199
+    FORCE_INLINE static void atomic_set_rx_tail(ring_buffer_pos_t value);
200
+    FORCE_INLINE static ring_buffer_pos_t atomic_read_rx_tail();
201 201
 
202 202
   public:
203 203
     FORCE_INLINE static void store_rxd_char();

+ 3
- 3
Marlin/src/HAL/AVR/fast_pwm.cpp Vedi File

@@ -107,7 +107,7 @@ const Timer get_pwm_timer(const pin_t pin) {
107 107
   return Timer();
108 108
 }
109 109
 
110
-void set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {
110
+void MarlinHAL::set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {
111 111
   const Timer timer = get_pwm_timer(pin);
112 112
   if (timer.isProtected || !timer.isPWM) return; // Don't proceed if protected timer or not recognized
113 113
 
@@ -176,7 +176,7 @@ void set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {
176 176
     _SET_ICRn(timer, res);                              // Set ICRn value (TOP) = res
177 177
 }
178 178
 
179
-void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
179
+void MarlinHAL::set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
180 180
   // If v is 0 or v_size (max), digitalWrite to LOW or HIGH.
181 181
   // Note that digitalWrite also disables PWM output for us (sets COM bit to 0)
182 182
   if (v == 0)
@@ -201,7 +201,7 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255
201 201
   }
202 202
 }
203 203
 
204
-void init_pwm_timers() {
204
+void MarlinHAL::init_pwm_timers() {
205 205
   // Init some timer frequencies to a default 1KHz
206 206
   const pin_t pwm_pin[] = {
207 207
     #ifdef __AVR_ATmega2560__

+ 2
- 2
Marlin/src/HAL/AVR/math.h Vedi File

@@ -35,7 +35,7 @@
35 35
 // C B A is longIn1
36 36
 // D C B A is longIn2
37 37
 //
38
-static FORCE_INLINE uint16_t MultiU24X32toH16(uint32_t longIn1, uint32_t longIn2) {
38
+FORCE_INLINE static uint16_t MultiU24X32toH16(uint32_t longIn1, uint32_t longIn2) {
39 39
   uint8_t tmp1;
40 40
   uint8_t tmp2;
41 41
   uint16_t intRes;
@@ -89,7 +89,7 @@ static FORCE_INLINE uint16_t MultiU24X32toH16(uint32_t longIn1, uint32_t longIn2
89 89
 // uses:
90 90
 // r26 to store 0
91 91
 // r27 to store the byte 1 of the 24 bit result
92
-static FORCE_INLINE uint16_t MultiU16X8toH16(uint8_t charIn1, uint16_t intIn2) {
92
+FORCE_INLINE static uint16_t MultiU16X8toH16(uint8_t charIn1, uint16_t intIn2) {
93 93
   uint8_t tmp;
94 94
   uint16_t intRes;
95 95
   __asm__ __volatile__ (

+ 3
- 3
Marlin/src/HAL/AVR/timers.h Vedi File

@@ -109,12 +109,12 @@ FORCE_INLINE void HAL_timer_start(const uint8_t timer_num, const uint32_t) {
109 109
  * (otherwise, characters will be lost due to UART overflow).
110 110
  * Then: Stepper, Endstops, Temperature, and -finally- all others.
111 111
  */
112
-#define HAL_timer_isr_prologue(T)
113
-#define HAL_timer_isr_epilogue(T)
112
+#define HAL_timer_isr_prologue(T) NOOP
113
+#define HAL_timer_isr_epilogue(T) NOOP
114 114
 
115
-/* 18 cycles maximum latency */
116 115
 #ifndef HAL_STEP_TIMER_ISR
117 116
 
117
+/* 18 cycles maximum latency */
118 118
 #define HAL_STEP_TIMER_ISR() \
119 119
 extern "C" void TIMER1_COMPA_vect() __attribute__ ((signal, naked, used, externally_visible)); \
120 120
 extern "C" void TIMER1_COMPA_vect_bottom() asm ("TIMER1_COMPA_vect_bottom") __attribute__ ((used, externally_visible, noinline)); \

+ 9
- 34
Marlin/src/HAL/DUE/HAL.cpp Vedi File

@@ -34,7 +34,7 @@
34 34
 // Public Variables
35 35
 // ------------------------
36 36
 
37
-uint16_t HAL_adc_result;
37
+uint16_t MarlinHAL::adc_result;
38 38
 
39 39
 // ------------------------
40 40
 // Public functions
@@ -42,8 +42,7 @@ uint16_t HAL_adc_result;
42 42
 
43 43
 TERN_(POSTMORTEM_DEBUGGING, extern void install_min_serial());
44 44
 
45
-// HAL initialization task
46
-void HAL_init() {
45
+void MarlinHAL::init() {
47 46
   // Initialize the USB stack
48 47
   #if ENABLED(SDSUPPORT)
49 48
     OUT_WRITE(SDSS, HIGH);  // Try to set SDSS inactive before any other SPI users start up
@@ -52,21 +51,15 @@ void HAL_init() {
52 51
   TERN_(POSTMORTEM_DEBUGGING, install_min_serial()); // Install the min serial handler
53 52
 }
54 53
 
55
-// HAL idle task
56
-void HAL_idletask() {
57
-  // Perform USB stack housekeeping
58
-  usb_task_idle();
54
+void MarlinHAL::init_board() {
55
+  #ifdef BOARD_INIT
56
+    BOARD_INIT();
57
+  #endif
59 58
 }
60 59
 
61
-// Disable interrupts
62
-void cli() { noInterrupts(); }
63
-
64
-// Enable interrupts
65
-void sei() { interrupts(); }
66
-
67
-void HAL_clear_reset_source() { }
60
+void MarlinHAL::idletask() { usb_task_idle(); } // Perform USB stack housekeeping
68 61
 
69
-uint8_t HAL_get_reset_source() {
62
+uint8_t MarlinHAL::get_reset_source() {
70 63
   switch ((RSTC->RSTC_SR >> 8) & 0x07) {
71 64
     case 0: return RST_POWER_ON;
72 65
     case 1: return RST_BACKUP;
@@ -77,12 +70,7 @@ uint8_t HAL_get_reset_source() {
77 70
   }
78 71
 }
79 72
 
80
-void HAL_reboot() { rstc_start_software_reset(RSTC); }
81
-
82
-void _delay_ms(const int delay_ms) {
83
-  // Todo: port for Due?
84
-  delay(delay_ms);
85
-}
73
+void MarlinHAL::reboot() { rstc_start_software_reset(RSTC); }
86 74
 
87 75
 extern "C" {
88 76
   extern unsigned int _ebss; // end of bss section
@@ -94,19 +82,6 @@ int freeMemory() {
94 82
   return (int)&free_memory - (heap_end ?: (int)&_ebss);
95 83
 }
96 84
 
97
-// ------------------------
98
-// ADC
99
-// ------------------------
100
-
101
-void HAL_adc_start_conversion(const uint8_t ch) {
102
-  HAL_adc_result = analogRead(ch);
103
-}
104
-
105
-uint16_t HAL_adc_get_result() {
106
-  // nop
107
-  return HAL_adc_result;
108
-}
109
-
110 85
 // Forward the default serial ports
111 86
 #if USING_HW_SERIAL0
112 87
   DefaultSerial1 MSerial0(false, Serial);

+ 87
- 48
Marlin/src/HAL/DUE/HAL.h Vedi File

@@ -38,6 +38,10 @@
38 38
 
39 39
 #include "../../core/serial_hook.h"
40 40
 
41
+// ------------------------
42
+// Serial ports
43
+// ------------------------
44
+
41 45
 typedef ForwardSerial1Class< decltype(Serial) > DefaultSerial1;
42 46
 typedef ForwardSerial1Class< decltype(Serial1) > DefaultSerial2;
43 47
 typedef ForwardSerial1Class< decltype(Serial2) > DefaultSerial3;
@@ -97,60 +101,38 @@ extern DefaultSerial4 MSerial3;
97 101
 #include "MarlinSerial.h"
98 102
 #include "MarlinSerialUSB.h"
99 103
 
100
-// On AVR this is in math.h?
101
-#define square(x) ((x)*(x))
104
+// ------------------------
105
+// Types
106
+// ------------------------
102 107
 
103 108
 typedef int8_t pin_t;
104 109
 
105
-#define SHARED_SERVOS HAS_SERVOS
106
-#define HAL_SERVO_LIB Servo
110
+#define SHARED_SERVOS HAS_SERVOS  // Use shared/servos.cpp
111
+
112
+class Servo;
113
+typedef Servo hal_servo_t;
107 114
 
108 115
 //
109 116
 // Interrupts
110 117
 //
111
-#define CRITICAL_SECTION_START()  uint32_t primask = __get_PRIMASK(); __disable_irq()
112
-#define CRITICAL_SECTION_END()    if (!primask) __enable_irq()
113
-#define ISRS_ENABLED() (!__get_PRIMASK())
114
-#define ENABLE_ISRS()  __enable_irq()
115
-#define DISABLE_ISRS() __disable_irq()
116
-
117
-void cli();                     // Disable interrupts
118
-void sei();                     // Enable interrupts
118
+#define sei() noInterrupts()
119
+#define cli() interrupts()
119 120
 
120
-void HAL_clear_reset_source();  // clear reset reason
121
-uint8_t HAL_get_reset_source(); // get reset reason
122
-
123
-void HAL_reboot();
121
+#define CRITICAL_SECTION_START()  const bool _irqon = hal.isr_state(); hal.isr_off()
122
+#define CRITICAL_SECTION_END()    if (_irqon) hal.isr_on()
124 123
 
125 124
 //
126 125
 // ADC
127 126
 //
128
-extern uint16_t HAL_adc_result;     // result of last ADC conversion
127
+#define HAL_ADC_VREF         3.3
128
+#define HAL_ADC_RESOLUTION  10
129 129
 
130 130
 #ifndef analogInputToDigitalPin
131 131
   #define analogInputToDigitalPin(p) ((p < 12U) ? (p) + 54U : -1)
132 132
 #endif
133 133
 
134
-#define HAL_ANALOG_SELECT(ch)
135
-
136
-inline void HAL_adc_init() {}//todo
137
-
138
-#define HAL_ADC_VREF         3.3
139
-#define HAL_ADC_RESOLUTION  10
140
-#define HAL_START_ADC(ch)   HAL_adc_start_conversion(ch)
141
-#define HAL_READ_ADC()      HAL_adc_result
142
-#define HAL_ADC_READY()     true
143
-
144
-void HAL_adc_start_conversion(const uint8_t ch);
145
-uint16_t HAL_adc_get_result();
146
-
147
-//
148
-// PWM
149 134
 //
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
-//
153
-// Pin Map
135
+// Pin Mapping for M42, M43, M226
154 136
 //
155 137
 #define GET_PIN_MAP_PIN(index) index
156 138
 #define GET_PIN_MAP_INDEX(pin) pin
@@ -159,27 +141,18 @@ inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255,
159 141
 //
160 142
 // Tone
161 143
 //
162
-void toneInit();
163 144
 void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration=0);
164 145
 void noTone(const pin_t _pin);
165 146
 
166
-// Enable hooks into idle and setup for HAL
167
-#define HAL_IDLETASK 1
168
-void HAL_idletask();
169
-void HAL_init();
170
-
171
-//
172
-// Utility functions
173
-//
174
-void _delay_ms(const int delay);
147
+// ------------------------
148
+// Class Utilities
149
+// ------------------------
175 150
 
176 151
 #pragma GCC diagnostic push
177 152
 #if GCC_VERSION <= 50000
178 153
   #pragma GCC diagnostic ignored "-Wunused-function"
179 154
 #endif
180 155
 
181
-int freeMemory();
182
-
183 156
 #pragma GCC diagnostic pop
184 157
 
185 158
 #ifdef __cplusplus
@@ -189,3 +162,69 @@ char *dtostrf(double __val, signed char __width, unsigned char __prec, char *__s
189 162
 #ifdef __cplusplus
190 163
   }
191 164
 #endif
165
+
166
+// Return free RAM between end of heap (or end bss) and whatever is current
167
+int freeMemory();
168
+
169
+// ------------------------
170
+// MarlinHAL Class
171
+// ------------------------
172
+
173
+class MarlinHAL {
174
+public:
175
+
176
+  // Earliest possible init, before setup()
177
+  MarlinHAL() {}
178
+
179
+  static void init();       // Called early in setup()
180
+  static void init_board(); // Called less early in setup()
181
+  static void reboot();     // Software reset
182
+
183
+  // Interrupts
184
+  static bool isr_state() { return !__get_PRIMASK(); }
185
+  static void isr_on()  { __enable_irq(); }
186
+  static void isr_off() { __disable_irq(); }
187
+
188
+  static void delay_ms(const int ms) { delay(ms); }
189
+
190
+  // Tasks, called from idle()
191
+  static void idletask();
192
+
193
+  // Reset
194
+  static uint8_t get_reset_source();
195
+  static void clear_reset_source() {}
196
+
197
+  // Free SRAM
198
+  static int freeMemory() { return ::freeMemory(); }
199
+
200
+  //
201
+  // ADC Methods
202
+  //
203
+
204
+  static uint16_t adc_result;
205
+
206
+  // Called by Temperature::init once at startup
207
+  static void adc_init() {}
208
+
209
+  // Called by Temperature::init for each sensor at startup
210
+  static void adc_enable(const uint8_t ch) {}
211
+
212
+  // Begin ADC sampling on the given channel
213
+  static void adc_start(const uint8_t ch) { adc_result = analogRead(ch); }
214
+
215
+  // Is the ADC ready for reading?
216
+  static bool adc_ready() { return true; }
217
+
218
+  // The current value of the ADC register
219
+  static uint16_t adc_value() { return adc_result; }
220
+
221
+  /**
222
+   * Set the PWM duty cycle for the pin to the given value.
223
+   * No inverting the duty cycle in this HAL.
224
+   * No changing the maximum size of the provided value to enable finer PWM duty control in this HAL.
225
+   */
226
+  static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) {
227
+    analogWrite(pin, v);
228
+  }
229
+
230
+};

+ 2
- 2
Marlin/src/HAL/DUE/MarlinSerial.cpp Vedi File

@@ -406,7 +406,7 @@ size_t MarlinSerial<Cfg>::write(const uint8_t c) {
406 406
     const uint8_t i = (tx_buffer.head + 1) & (Cfg::TX_SIZE - 1);
407 407
 
408 408
     // If global interrupts are disabled (as the result of being called from an ISR)...
409
-    if (!ISRS_ENABLED()) {
409
+    if (!hal.isr_state()) {
410 410
 
411 411
       // Make room by polling if it is possible to transmit, and do so!
412 412
       while (i == tx_buffer.tail) {
@@ -454,7 +454,7 @@ void MarlinSerial<Cfg>::flushTX() {
454 454
     if (!_written) return;
455 455
 
456 456
     // If global interrupts are disabled (as the result of being called from an ISR)...
457
-    if (!ISRS_ENABLED()) {
457
+    if (!hal.isr_state()) {
458 458
 
459 459
       // Wait until everything was transmitted - We must do polling, as interrupts are disabled
460 460
       while (tx_buffer.head != tx_buffer.tail || !(HWUART->UART_SR & UART_SR_TXEMPTY)) {

+ 1
- 1
Marlin/src/HAL/DUE/Tone.cpp Vedi File

@@ -35,7 +35,7 @@
35 35
 static pin_t tone_pin;
36 36
 volatile static int32_t toggles;
37 37
 
38
-void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration) {
38
+void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration/*=0*/) {
39 39
   tone_pin = _pin;
40 40
   toggles = 2 * frequency * duration / 1000;
41 41
   HAL_timer_start(MF_TIMER_TONE, 2 * frequency);

+ 1
- 1
Marlin/src/HAL/DUE/timers.h Vedi File

@@ -125,4 +125,4 @@ FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
125 125
   pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_SR;
126 126
 }
127 127
 
128
-#define HAL_timer_isr_epilogue(T)
128
+#define HAL_timer_isr_epilogue(T) NOOP

+ 17
- 18
Marlin/src/HAL/ESP32/HAL.cpp Vedi File

@@ -52,7 +52,7 @@
52 52
 // Externs
53 53
 // ------------------------
54 54
 
55
-portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED;
55
+portMUX_TYPE MarlinHAL::spinlock = portMUX_INITIALIZER_UNLOCKED;
56 56
 
57 57
 // ------------------------
58 58
 // Local defines
@@ -64,7 +64,7 @@ portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED;
64 64
 // Public Variables
65 65
 // ------------------------
66 66
 
67
-uint16_t HAL_adc_result;
67
+uint16_t MarlinHAL::adc_result;
68 68
 
69 69
 // ------------------------
70 70
 // Private Variables
@@ -95,20 +95,22 @@ volatile int numPWMUsed = 0,
95 95
 #endif
96 96
 
97 97
 #if ENABLED(USE_ESP32_EXIO)
98
+
98 99
   HardwareSerial YSerial2(2);
99 100
 
100 101
   void Write_EXIO(uint8_t IO, uint8_t v) {
101
-    if (ISRS_ENABLED()) {
102
-      DISABLE_ISRS();
102
+    if (hal.isr_state()) {
103
+      hal.isr_off();
103 104
       YSerial2.write(0x80 | (((char)v) << 5) | (IO - 100));
104
-      ENABLE_ISRS();
105
+      hal.isr_on();
105 106
     }
106 107
     else
107 108
       YSerial2.write(0x80 | (((char)v) << 5) | (IO - 100));
108 109
   }
110
+
109 111
 #endif
110 112
 
111
-void HAL_init_board() {
113
+void MarlinHAL::init_board() {
112 114
   #if ENABLED(USE_ESP32_TASK_WDT)
113 115
     esp_task_wdt_init(10, true);
114 116
   #endif
@@ -154,27 +156,24 @@ void HAL_init_board() {
154 156
   #endif
155 157
 }
156 158
 
157
-void HAL_idletask() {
159
+void MarlinHAL::idletask() {
158 160
   #if BOTH(WIFISUPPORT, OTASUPPORT)
159 161
     OTA_handle();
160 162
   #endif
161 163
   TERN_(ESP3D_WIFISUPPORT, esp3dlib.idletask());
162 164
 }
163 165
 
164
-void HAL_clear_reset_source() { }
165
-
166
-uint8_t HAL_get_reset_source() { return rtc_get_reset_reason(1); }
166
+uint8_t MarlinHAL::get_reset_source() { return rtc_get_reset_reason(1); }
167 167
 
168
-void HAL_reboot() { ESP.restart(); }
169
-
170
-void _delay_ms(int delay_ms) { delay(delay_ms); }
168
+void MarlinHAL::reboot() { ESP.restart(); }
171 169
 
172 170
 // return free memory between end of heap (or end bss) and whatever is current
173
-int freeMemory() { return ESP.getFreeHeap(); }
171
+int MarlinHAL::freeMemory() { return ESP.getFreeHeap(); }
174 172
 
175 173
 // ------------------------
176 174
 // ADC
177 175
 // ------------------------
176
+
178 177
 #define ADC1_CHANNEL(pin) ADC1_GPIO ## pin ## _CHANNEL
179 178
 
180 179
 adc1_channel_t get_channel(int pin) {
@@ -196,7 +195,7 @@ void adc1_set_attenuation(adc1_channel_t chan, adc_atten_t atten) {
196 195
   }
197 196
 }
198 197
 
199
-void HAL_adc_init() {
198
+void MarlinHAL::adc_init() {
200 199
   // Configure ADC
201 200
   adc1_config_width(ADC_WIDTH_12Bit);
202 201
 
@@ -228,11 +227,11 @@ void HAL_adc_init() {
228 227
   }
229 228
 }
230 229
 
231
-void HAL_adc_start_conversion(const uint8_t adc_pin) {
232
-  const adc1_channel_t chan = get_channel(adc_pin);
230
+void MarlinHAL::adc_start(const pin_t pin) {
231
+  const adc1_channel_t chan = get_channel(pin);
233 232
   uint32_t mv;
234 233
   esp_adc_cal_get_voltage((adc_channel_t)chan, &characteristics[attenuations[chan]], &mv);
235
-  HAL_adc_result = mv * 1023.0 / 3300.0;
234
+  adc_result = mv * 1023.0 / 3300.0;
236 235
 
237 236
   // Change the attenuation level based on the new reading
238 237
   adc_atten_t atten;

+ 87
- 57
Marlin/src/HAL/ESP32/HAL.h Vedi File

@@ -49,8 +49,6 @@
49 49
 // Defines
50 50
 // ------------------------
51 51
 
52
-extern portMUX_TYPE spinlock;
53
-
54 52
 #define MYSERIAL1 flushableSerial
55 53
 
56 54
 #if EITHER(WIFISUPPORT, ESP3D_WIFISUPPORT)
@@ -65,9 +63,6 @@ extern portMUX_TYPE spinlock;
65 63
 
66 64
 #define CRITICAL_SECTION_START() portENTER_CRITICAL(&spinlock)
67 65
 #define CRITICAL_SECTION_END()   portEXIT_CRITICAL(&spinlock)
68
-#define ISRS_ENABLED() (spinlock.owner == portMUX_FREE_VAL)
69
-#define ENABLE_ISRS()  if (spinlock.owner != portMUX_FREE_VAL) portEXIT_CRITICAL(&spinlock)
70
-#define DISABLE_ISRS() portENTER_CRITICAL(&spinlock)
71 66
 
72 67
 // ------------------------
73 68
 // Types
@@ -75,14 +70,8 @@ extern portMUX_TYPE spinlock;
75 70
 
76 71
 typedef int16_t pin_t;
77 72
 
78
-#define HAL_SERVO_LIB Servo
79
-
80
-// ------------------------
81
-// Public Variables
82
-// ------------------------
83
-
84
-/** result of last ADC conversion */
85
-extern uint16_t HAL_adc_result;
73
+class Servo;
74
+typedef Servo hal_servo_t;
86 75
 
87 76
 // ------------------------
88 77
 // Public functions
@@ -91,59 +80,18 @@ extern uint16_t HAL_adc_result;
91 80
 //
92 81
 // Tone
93 82
 //
94
-void toneInit();
95 83
 void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration=0);
96 84
 void noTone(const pin_t _pin);
97 85
 
98
-// clear reset reason
99
-void HAL_clear_reset_source();
100
-
101
-// reset reason
102
-uint8_t HAL_get_reset_source();
103
-
104
-void HAL_reboot();
105
-
106
-void _delay_ms(int delay);
107
-
108
-#pragma GCC diagnostic push
109
-#if GCC_VERSION <= 50000
110
-  #pragma GCC diagnostic ignored "-Wunused-function"
111
-#endif
112
-
113
-int freeMemory();
114
-
115
-#pragma GCC diagnostic pop
116
-
117 86
 void analogWrite(pin_t pin, int value);
118 87
 
119
-// ADC
120
-#define HAL_ANALOG_SELECT(pin)
121
-
122
-void HAL_adc_init();
123
-
124
-#define HAL_ADC_VREF         3.3
125
-#define HAL_ADC_RESOLUTION  10
126
-#define HAL_START_ADC(pin)  HAL_adc_start_conversion(pin)
127
-#define HAL_READ_ADC()      HAL_adc_result
128
-#define HAL_ADC_READY()     true
129
-
130
-void HAL_adc_start_conversion(const uint8_t adc_pin);
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
88
+//
89
+// Pin Mapping for M42, M43, M226
90
+//
136 91
 #define GET_PIN_MAP_PIN(index) index
137 92
 #define GET_PIN_MAP_INDEX(pin) pin
138 93
 #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
139 94
 
140
-// Enable hooks into idle and setup for HAL
141
-#define HAL_IDLETASK 1
142
-#define BOARD_INIT() HAL_init_board()
143
-void HAL_idletask();
144
-inline void HAL_init() {}
145
-void HAL_init_board();
146
-
147 95
 #if ENABLED(USE_ESP32_EXIO)
148 96
   void Write_EXIO(uint8_t IO, uint8_t v);
149 97
 #endif
@@ -188,3 +136,85 @@ FORCE_INLINE static void DELAY_CYCLES(uint32_t x) {
188 136
   }
189 137
 
190 138
 }
139
+
140
+// ------------------------
141
+// Class Utilities
142
+// ------------------------
143
+
144
+#pragma GCC diagnostic push
145
+#if GCC_VERSION <= 50000
146
+  #pragma GCC diagnostic ignored "-Wunused-function"
147
+#endif
148
+
149
+int freeMemory();
150
+
151
+#pragma GCC diagnostic pop
152
+
153
+void _delay_ms(const int ms);
154
+
155
+// ------------------------
156
+// MarlinHAL Class
157
+// ------------------------
158
+
159
+#define HAL_ADC_VREF         3.3
160
+#define HAL_ADC_RESOLUTION  10
161
+
162
+class MarlinHAL {
163
+public:
164
+
165
+  // Earliest possible init, before setup()
166
+  MarlinHAL() {}
167
+
168
+  static void init() {}  // Called early in setup()
169
+  static void init_board();     // Called less early in setup()
170
+  static void reboot();         // Restart the firmware
171
+
172
+  // Interrupts
173
+  static portMUX_TYPE spinlock;
174
+  static bool isr_state() { return spinlock.owner == portMUX_FREE_VAL; }
175
+  static void isr_on()  { if (spinlock.owner != portMUX_FREE_VAL) portEXIT_CRITICAL(&spinlock); }
176
+  static void isr_off() { portENTER_CRITICAL(&spinlock); }
177
+
178
+  static void delay_ms(const int ms) { _delay_ms(ms); }
179
+
180
+  // Tasks, called from idle()
181
+  static void idletask();
182
+
183
+  // Reset
184
+  static uint8_t get_reset_source();
185
+  static void clear_reset_source() {}
186
+
187
+  // Free SRAM
188
+  static int freeMemory();
189
+
190
+  //
191
+  // ADC Methods
192
+  //
193
+
194
+  static uint16_t adc_result;
195
+
196
+  // Called by Temperature::init once at startup
197
+  static void adc_init();
198
+
199
+  // Called by Temperature::init for each sensor at startup
200
+  static void adc_enable(const pin_t pin) {}
201
+
202
+  // Begin ADC sampling on the given channel
203
+  static void adc_start(const pin_t pin);
204
+
205
+  // Is the ADC ready for reading?
206
+  static bool adc_ready() { return true; }
207
+
208
+  // The current value of the ADC register
209
+  static uint16_t adc_value() { return adc_result; }
210
+
211
+  /**
212
+   * Set the PWM duty cycle for the pin to the given value.
213
+   * No inverting the duty cycle in this HAL.
214
+   * No changing the maximum size of the provided value to enable finer PWM duty control in this HAL.
215
+   */
216
+  static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) {
217
+    analogWrite(pin, v);
218
+  }
219
+
220
+};

+ 1
- 1
Marlin/src/HAL/ESP32/Tone.cpp Vedi File

@@ -35,7 +35,7 @@
35 35
 static pin_t tone_pin;
36 36
 volatile static int32_t toggles;
37 37
 
38
-void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration) {
38
+void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration/*=0*/) {
39 39
   tone_pin = _pin;
40 40
   toggles = 2 * frequency * duration / 1000;
41 41
   HAL_timer_start(MF_TIMER_TONE, 2 * frequency);

+ 1
- 1
Marlin/src/HAL/ESP32/timers.cpp Vedi File

@@ -81,7 +81,7 @@ void IRAM_ATTR timer_isr(void *para) {
81 81
  * @param timer_num timer number to initialize
82 82
  * @param frequency frequency of the timer
83 83
  */
84
-void HAL_timer_start(const uint8_t timer_num, uint32_t frequency) {
84
+void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
85 85
   const tTimerConfig timer = timer_config[timer_num];
86 86
 
87 87
   timer_config_t config;

+ 3
- 3
Marlin/src/HAL/ESP32/timers.h Vedi File

@@ -127,7 +127,7 @@ extern const tTimerConfig timer_config[];
127 127
 // Public functions
128 128
 // ------------------------
129 129
 
130
-void HAL_timer_start (const uint8_t timer_num, uint32_t frequency);
130
+void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency);
131 131
 void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t count);
132 132
 hal_timer_t HAL_timer_get_compare(const uint8_t timer_num);
133 133
 hal_timer_t HAL_timer_get_count(const uint8_t timer_num);
@@ -136,5 +136,5 @@ void HAL_timer_enable_interrupt(const uint8_t timer_num);
136 136
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
137 137
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
138 138
 
139
-#define HAL_timer_isr_prologue(T)
140
-#define HAL_timer_isr_epilogue(T)
139
+#define HAL_timer_isr_prologue(T) NOOP
140
+#define HAL_timer_isr_epilogue(T) NOOP

+ 10
- 27
Marlin/src/HAL/LINUX/HAL.cpp Vedi File

@@ -24,6 +24,10 @@
24 24
 #include "../../inc/MarlinConfig.h"
25 25
 #include "../shared/Delay.h"
26 26
 
27
+// ------------------------
28
+// Serial ports
29
+// ------------------------
30
+
27 31
 MSerialT usb_serial(TERN0(EMERGENCY_PARSER, true));
28 32
 
29 33
 // U8glib required functions
@@ -37,42 +41,21 @@ extern "C" {
37 41
 //************************//
38 42
 
39 43
 // return free heap space
40
-int freeMemory() {
41
-  return 0;
42
-}
44
+int freeMemory() { return 0; }
43 45
 
44 46
 // ------------------------
45 47
 // ADC
46 48
 // ------------------------
47 49
 
48
-void HAL_adc_init() {
49
-
50
-}
51
-
52
-void HAL_adc_enable_channel(const uint8_t ch) {
53
-
54
-}
55
-
56
-uint8_t active_ch = 0;
57
-void HAL_adc_start_conversion(const uint8_t ch) {
58
-  active_ch = ch;
59
-}
60
-
61
-bool HAL_adc_finished() {
62
-  return true;
63
-}
50
+uint8_t MarlinHAL::active_ch = 0;
64 51
 
65
-uint16_t HAL_adc_get_result() {
66
-  pin_t pin = analogInputToDigitalPin(active_ch);
52
+uint16_t MarlinHAL::adc_value() {
53
+  const pin_t pin = analogInputToDigitalPin(active_ch);
67 54
   if (!VALID_PIN(pin)) return 0;
68
-  uint16_t data = ((Gpio::get(pin) >> 2) & 0x3FF);
55
+  const uint16_t data = ((Gpio::get(pin) >> 2) & 0x3FF);
69 56
   return data;    // return 10bit value as Marlin expects
70 57
 }
71 58
 
72
-void HAL_pwm_init() {
73
-
74
-}
75
-
76
-void HAL_reboot() { /* Reset the application state and GPIO */ }
59
+void MarlinHAL::reboot() { /* Reset the application state and GPIO */ }
77 60
 
78 61
 #endif // __PLAT_LINUX__

+ 94
- 49
Marlin/src/HAL/LINUX/HAL.h Vedi File

@@ -21,25 +21,42 @@
21 21
  */
22 22
 #pragma once
23 23
 
24
-#define CPU_32_BIT
25
-
26
-#define F_CPU 100000000UL
27
-#define SystemCoreClock F_CPU
28 24
 #include <iostream>
29 25
 #include <stdint.h>
30 26
 #include <stdarg.h>
31
-
32 27
 #undef min
33 28
 #undef max
34
-
35 29
 #include <algorithm>
36 30
 
37
-void _printf (const  char *format, ...);
31
+#include "hardware/Clock.h"
32
+
33
+#include "../shared/Marduino.h"
34
+#include "../shared/math_32bit.h"
35
+#include "../shared/HAL_SPI.h"
36
+#include "fastio.h"
37
+#include "watchdog.h"
38
+#include "serial.h"
39
+
40
+// ------------------------
41
+// Defines
42
+// ------------------------
43
+
44
+#define CPU_32_BIT
45
+#define SHARED_SERVOS HAS_SERVOS  // Use shared/servos.cpp
46
+
47
+#define F_CPU 100000000UL
48
+#define SystemCoreClock F_CPU
49
+
50
+#define DELAY_CYCLES(x) Clock::delayCycles(x)
51
+
52
+#define CPU_ST7920_DELAY_1 600
53
+#define CPU_ST7920_DELAY_2 750
54
+#define CPU_ST7920_DELAY_3 750
55
+
56
+void _printf(const  char *format, ...);
38 57
 void _putc(uint8_t c);
39 58
 uint8_t _getc();
40 59
 
41
-//extern "C" volatile uint32_t _millis;
42
-
43 60
 //arduino: Print.h
44 61
 #define DEC 10
45 62
 #define HEX 16
@@ -49,36 +66,27 @@ uint8_t _getc();
49 66
 #define B01 1
50 67
 #define B10 2
51 68
 
52
-#include "hardware/Clock.h"
53
-
54
-#include "../shared/Marduino.h"
55
-#include "../shared/math_32bit.h"
56
-#include "../shared/HAL_SPI.h"
57
-#include "fastio.h"
58
-#include "watchdog.h"
59
-#include "serial.h"
60
-
61
-#define SHARED_SERVOS HAS_SERVOS
69
+// ------------------------
70
+// Serial ports
71
+// ------------------------
62 72
 
63 73
 extern MSerialT usb_serial;
64 74
 #define MYSERIAL1 usb_serial
65 75
 
66
-#define CPU_ST7920_DELAY_1 600
67
-#define CPU_ST7920_DELAY_2 750
68
-#define CPU_ST7920_DELAY_3 750
69
-
70 76
 //
71 77
 // Interrupts
72 78
 //
73 79
 #define CRITICAL_SECTION_START()
74 80
 #define CRITICAL_SECTION_END()
75
-#define ISRS_ENABLED()
76
-#define ENABLE_ISRS()
77
-#define DISABLE_ISRS()
78 81
 
79
-inline void HAL_init() {}
82
+// ADC
83
+#define HAL_ADC_VREF           5.0
84
+#define HAL_ADC_RESOLUTION    10
85
+
86
+// ------------------------
87
+// Class Utilities
88
+// ------------------------
80 89
 
81
-// Utility functions
82 90
 #pragma GCC diagnostic push
83 91
 #if GCC_VERSION <= 50000
84 92
   #pragma GCC diagnostic ignored "-Wunused-function"
@@ -88,29 +96,66 @@ int freeMemory();
88 96
 
89 97
 #pragma GCC diagnostic pop
90 98
 
91
-// ADC
92
-#define HAL_ADC_VREF           5.0
93
-#define HAL_ADC_RESOLUTION    10
94
-#define HAL_ANALOG_SELECT(ch) HAL_adc_enable_channel(ch)
95
-#define HAL_START_ADC(ch)     HAL_adc_start_conversion(ch)
96
-#define HAL_READ_ADC()        HAL_adc_get_result()
97
-#define HAL_ADC_READY()       true
99
+// ------------------------
100
+// MarlinHAL Class
101
+// ------------------------
102
+
103
+class MarlinHAL {
104
+public:
105
+
106
+  // Earliest possible init, before setup()
107
+  MarlinHAL() {}
108
+
109
+  static void init() {}        // Called early in setup()
110
+  static void init_board() {}  // Called less early in setup()
111
+  static void reboot();               // Reset the application state and GPIO
112
+
113
+  // Interrupts
114
+  static bool isr_state() { return true; }
115
+  static void isr_on()  {}
116
+  static void isr_off() {}
117
+
118
+  static void delay_ms(const int ms) { _delay_ms(ms); }
119
+
120
+  // Tasks, called from idle()
121
+  static void idletask() {}
122
+
123
+  // Reset
124
+  static constexpr uint8_t reset_reason = RST_POWER_ON;
125
+  static uint8_t get_reset_source() { return reset_reason; }
126
+  static void clear_reset_source() {}
127
+
128
+  // Free SRAM
129
+  static int freeMemory() { return ::freeMemory(); }
130
+
131
+  //
132
+  // ADC Methods
133
+  //
134
+
135
+  static uint8_t active_ch;
136
+
137
+  // Called by Temperature::init once at startup
138
+  static void adc_init() {}
139
+
140
+  // Called by Temperature::init for each sensor at startup
141
+  static void adc_enable(const uint8_t) {}
98 142
 
99
-void HAL_adc_init();
100
-void HAL_adc_enable_channel(const uint8_t ch);
101
-void HAL_adc_start_conversion(const uint8_t ch);
102
-uint16_t HAL_adc_get_result();
143
+  // Begin ADC sampling on the given channel
144
+  static void adc_start(const uint8_t ch) { active_ch = ch; }
103 145
 
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); }
146
+  // Is the ADC ready for reading?
147
+  static bool adc_ready() { return true; }
106 148
 
107
-// Reset source
108
-inline void HAL_clear_reset_source(void) {}
109
-inline uint8_t HAL_get_reset_source(void) { return RST_POWER_ON; }
149
+  // The current value of the ADC register
150
+  static uint16_t adc_value();
110 151
 
111
-void HAL_reboot(); // Reset the application state and GPIO
152
+  /**
153
+   * Set the PWM duty cycle for the pin to the given value.
154
+   * No option to change the resolution or invert the duty cycle.
155
+   */
156
+  static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) {
157
+    analogWrite(pin, v);
158
+  }
112 159
 
113
-/* ---------------- Delay in cycles */
114
-FORCE_INLINE static void DELAY_CYCLES(uint64_t x) {
115
-  Clock::delayCycles(x);
116
-}
160
+  static void set_pwm_frequency(const pin_t, int) {}
161
+};

+ 1
- 3
Marlin/src/HAL/LINUX/arduino.cpp Vedi File

@@ -31,9 +31,7 @@ void cli() { } // Disable
31 31
 void sei() { } // Enable
32 32
 
33 33
 // Time functions
34
-void _delay_ms(const int delay_ms) {
35
-  delay(delay_ms);
36
-}
34
+void _delay_ms(const int ms) { delay(ms); }
37 35
 
38 36
 uint32_t millis() {
39 37
   return (uint32_t)Clock::millis();

+ 3
- 4
Marlin/src/HAL/LINUX/include/Arduino.h Vedi File

@@ -59,10 +59,9 @@ typedef uint8_t byte;
59 59
 #endif
60 60
 
61 61
 #define sq(v) ((v) * (v))
62
-#define square(v) sq(v)
63 62
 #define constrain(value, arg_min, arg_max) ((value) < (arg_min) ? (arg_min) :((value) > (arg_max) ? (arg_max) : (value)))
64 63
 
65
-//Interrupts
64
+// Interrupts
66 65
 void cli(); // Disable
67 66
 void sei(); // Enable
68 67
 void attachInterrupt(uint32_t pin, void (*callback)(), uint32_t mode);
@@ -74,8 +73,8 @@ extern "C" {
74 73
 }
75 74
 
76 75
 // Time functions
77
-extern "C" void delay(const int milis);
78
-void _delay_ms(const int delay);
76
+extern "C" void delay(const int ms);
77
+void _delay_ms(const int ms);
79 78
 void delayMicroseconds(unsigned long);
80 79
 uint32_t millis();
81 80
 

+ 2
- 2
Marlin/src/HAL/LINUX/timers.h Vedi File

@@ -92,5 +92,5 @@ void HAL_timer_enable_interrupt(const uint8_t timer_num);
92 92
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
93 93
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
94 94
 
95
-#define HAL_timer_isr_prologue(T)
96
-#define HAL_timer_isr_epilogue(T)
95
+#define HAL_timer_isr_prologue(T) NOOP
96
+#define HAL_timer_isr_epilogue(T) NOOP

+ 20
- 22
Marlin/src/HAL/LPC1768/HAL.cpp Vedi File

@@ -31,7 +31,7 @@
31 31
 
32 32
 DefaultSerial1 USBSerial(false, UsbSerial);
33 33
 
34
-uint32_t HAL_adc_reading = 0;
34
+uint32_t MarlinHAL::adc_result = 0;
35 35
 
36 36
 // U8glib required functions
37 37
 extern "C" {
@@ -41,8 +41,6 @@ extern "C" {
41 41
   void u8g_Delay(uint16_t val)       { delay(val); }
42 42
 }
43 43
 
44
-//************************//
45
-
46 44
 // return free heap space
47 45
 int freeMemory() {
48 46
   char stack_end;
@@ -54,33 +52,33 @@ int freeMemory() {
54 52
   return result;
55 53
 }
56 54
 
57
-// scan command line for code
58
-//   return index into pin map array if found and the pin is valid.
59
-//   return dval if not found or not a valid pin.
60
-int16_t PARSED_PIN_INDEX(const char code, const int16_t dval) {
61
-  const uint16_t val = (uint16_t)parser.intval(code, -1), port = val / 100, pin = val % 100;
62
-  const  int16_t ind = (port < ((NUM_DIGITAL_PINS) >> 5) && pin < 32) ? ((port << 5) | pin) : -2;
63
-  return ind > -1 ? ind : dval;
55
+void MarlinHAL::reboot() { NVIC_SystemReset(); }
56
+
57
+uint8_t MarlinHAL::get_reset_source() {
58
+  #if ENABLED(USE_WATCHDOG)
59
+    if (watchdog_timed_out()) return RST_WATCHDOG;
60
+  #endif
61
+  return RST_POWER_ON;
62
+}
63
+
64
+void MarlinHAL::clear_reset_source() {
65
+  TERN_(USE_WATCHDOG, watchdog_clear_timeout_flag());
64 66
 }
65 67
 
66 68
 void flashFirmware(const int16_t) {
67 69
   delay(500);          // Give OS time to disconnect
68 70
   USB_Connect(false);  // USB clear connection
69 71
   delay(1000);         // Give OS time to notice
70
-  HAL_reboot();
72
+  hal.reboot();
71 73
 }
72 74
 
73
-void HAL_clear_reset_source(void) {
74
-  TERN_(USE_WATCHDOG, watchdog_clear_timeout_flag());
75
-}
76
-
77
-uint8_t HAL_get_reset_source(void) {
78
-  #if ENABLED(USE_WATCHDOG)
79
-    if (watchdog_timed_out()) return RST_WATCHDOG;
80
-  #endif
81
-  return RST_POWER_ON;
75
+// For M42/M43, scan command line for pin code
76
+//   return index into pin map array if found and the pin is valid.
77
+//   return dval if not found or not a valid pin.
78
+int16_t PARSED_PIN_INDEX(const char code, const int16_t dval) {
79
+  const uint16_t val = (uint16_t)parser.intval(code, -1), port = val / 100, pin = val % 100;
80
+  const  int16_t ind = (port < ((NUM_DIGITAL_PINS) >> 5) && pin < 32) ? ((port << 5) | pin) : -2;
81
+  return ind > -1 ? ind : dval;
82 82
 }
83 83
 
84
-void HAL_reboot() { NVIC_SystemReset(); }
85
-
86 84
 #endif // TARGET_LPC1768

+ 98
- 59
Marlin/src/HAL/LPC1768/HAL.h Vedi File

@@ -28,8 +28,6 @@
28 28
 
29 29
 #define CPU_32_BIT
30 30
 
31
-void HAL_init();
32
-
33 31
 #include <stdint.h>
34 32
 #include <stdarg.h>
35 33
 #include <algorithm>
@@ -47,12 +45,9 @@ extern "C" volatile uint32_t _millis;
47 45
 #include <pinmapping.h>
48 46
 #include <CDCSerial.h>
49 47
 
50
-//
51
-// Default graphical display delays
52
-//
53
-#define CPU_ST7920_DELAY_1 600
54
-#define CPU_ST7920_DELAY_2 750
55
-#define CPU_ST7920_DELAY_3 750
48
+// ------------------------
49
+// Serial ports
50
+// ------------------------
56 51
 
57 52
 typedef ForwardSerial1Class< decltype(UsbSerial) > DefaultSerial1;
58 53
 extern DefaultSerial1 USBSerial;
@@ -114,26 +109,12 @@ extern DefaultSerial1 USBSerial;
114 109
 //
115 110
 // Interrupts
116 111
 //
117
-#define CRITICAL_SECTION_START()  uint32_t primask = __get_PRIMASK(); __disable_irq()
118
-#define CRITICAL_SECTION_END()    if (!primask) __enable_irq()
119
-#define ISRS_ENABLED() (!__get_PRIMASK())
120
-#define ENABLE_ISRS()  __enable_irq()
121
-#define DISABLE_ISRS() __disable_irq()
122
-
123
-//
124
-// Utility functions
125
-//
126
-#pragma GCC diagnostic push
127
-#if GCC_VERSION <= 50000
128
-  #pragma GCC diagnostic ignored "-Wunused-function"
129
-#endif
130 112
 
131
-int freeMemory();
132
-
133
-#pragma GCC diagnostic pop
113
+#define CRITICAL_SECTION_START()  const bool irqon = !__get_PRIMASK(); __disable_irq()
114
+#define CRITICAL_SECTION_END()    if (irqon) __enable_irq()
134 115
 
135 116
 //
136
-// ADC API
117
+// ADC
137 118
 //
138 119
 
139 120
 #define ADC_MEDIAN_FILTER_SIZE (23) // Higher values increase step delay (phase shift),
@@ -152,20 +133,9 @@ int freeMemory();
152 133
 #define HAL_ADC_RESOLUTION     12   // 15 bit maximum, raw temperature is stored as int16_t
153 134
 #define HAL_ADC_FILTERED            // Disable oversampling done in Marlin as ADC values already filtered in HAL
154 135
 
155
-using FilteredADC = LPC176x::ADC<ADC_LOWPASS_K_VALUE, ADC_MEDIAN_FILTER_SIZE>;
156
-extern uint32_t HAL_adc_reading;
157
-[[gnu::always_inline]] inline void HAL_adc_start_conversion(const pin_t pin) {
158
-  HAL_adc_reading = FilteredADC::read(pin) >> (16 - HAL_ADC_RESOLUTION); // returns 16bit value, reduce to required bits
159
-}
160
-[[gnu::always_inline]] inline uint16_t HAL_adc_get_result() {
161
-  return HAL_adc_reading;
162
-}
163
-
164
-#define HAL_adc_init()
165
-#define HAL_ANALOG_SELECT(pin) FilteredADC::enable_channel(pin)
166
-#define HAL_START_ADC(pin)     HAL_adc_start_conversion(pin)
167
-#define HAL_READ_ADC()         HAL_adc_get_result()
168
-#define HAL_ADC_READY()        (true)
136
+//
137
+// Pin Mapping for M42, M43, M226
138
+//
169 139
 
170 140
 // Test whether the pin is valid
171 141
 constexpr bool VALID_PIN(const pin_t pin) {
@@ -192,32 +162,101 @@ int16_t PARSED_PIN_INDEX(const char code, const int16_t dval);
192 162
 // P0.6 thru P0.9 are for the onboard SD card
193 163
 #define HAL_SENSITIVE_PINS P0_06, P0_07, P0_08, P0_09,
194 164
 
195
-#define HAL_IDLETASK 1
196
-void HAL_idletask();
165
+// ------------------------
166
+// Defines
167
+// ------------------------
197 168
 
198 169
 #define PLATFORM_M997_SUPPORT
199 170
 void flashFirmware(const int16_t);
200 171
 
201 172
 #define HAL_CAN_SET_PWM_FREQ   // This HAL supports PWM Frequency adjustment
202 173
 
203
-/**
204
- * set_pwm_frequency
205
- *  Set the frequency of the timer corresponding to the provided pin
206
- *  All Hardware PWM pins run at the same frequency and all
207
- *  Software PWM pins run at the same frequency
208
- */
209
-void set_pwm_frequency(const pin_t pin, const uint16_t f_desired);
174
+// Default graphical display delays
175
+#define CPU_ST7920_DELAY_1 600
176
+#define CPU_ST7920_DELAY_2 750
177
+#define CPU_ST7920_DELAY_3 750
210 178
 
211
-/**
212
- * set_pwm_duty
213
- *  Set the PWM duty cycle of the provided pin to the provided value
214
- *  Optionally allows inverting the duty cycle [default = false]
215
- *  Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
216
- */
217
-void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);
179
+// ------------------------
180
+// Class Utilities
181
+// ------------------------
182
+
183
+#pragma GCC diagnostic push
184
+#if GCC_VERSION <= 50000
185
+  #pragma GCC diagnostic ignored "-Wunused-function"
186
+#endif
187
+
188
+int freeMemory();
189
+
190
+#pragma GCC diagnostic pop
191
+
192
+// ------------------------
193
+// MarlinHAL Class
194
+// ------------------------
195
+
196
+class MarlinHAL {
197
+public:
198
+
199
+  // Earliest possible init, before setup()
200
+  MarlinHAL() {}
201
+
202
+  static void init();                 // Called early in setup()
203
+  static void init_board() {}  // Called less early in setup()
204
+  static void reboot();               // Restart the firmware from 0x0
205
+
206
+  // Interrupts
207
+  static bool isr_state() { return !__get_PRIMASK(); }
208
+  static void isr_on()  { __enable_irq(); }
209
+  static void isr_off() { __disable_irq(); }
210
+
211
+  static void delay_ms(const int ms) { _delay_ms(ms); }
212
+
213
+  // Tasks, called from idle()
214
+  static void idletask();
215
+
216
+  // Reset
217
+  static uint8_t get_reset_source();
218
+  static void clear_reset_source();
219
+
220
+  // Free SRAM
221
+  static int freeMemory() { return ::freeMemory(); }
222
+
223
+  //
224
+  // ADC Methods
225
+  //
226
+
227
+  using FilteredADC = LPC176x::ADC<ADC_LOWPASS_K_VALUE, ADC_MEDIAN_FILTER_SIZE>;
228
+
229
+  // Called by Temperature::init once at startup
230
+  static void adc_init() {}
231
+
232
+  // Called by Temperature::init for each sensor at startup
233
+  static void adc_enable(const pin_t pin) {
234
+    FilteredADC::enable_channel(pin);
235
+  }
236
+
237
+  // Begin ADC sampling on the given pin
238
+  static uint32_t adc_result;
239
+  static void adc_start(const pin_t pin) {
240
+    adc_result = FilteredADC::read(pin) >> (16 - HAL_ADC_RESOLUTION); // returns 16bit value, reduce to required bits
241
+  }
242
+
243
+  // Is the ADC ready for reading?
244
+  static bool adc_ready() { return true; }
245
+
246
+  // The current value of the ADC register
247
+  static uint16_t adc_value() { return uint16_t(adc_result); }
218 248
 
219
-// Reset source
220
-void HAL_clear_reset_source(void);
221
-uint8_t HAL_get_reset_source(void);
249
+  /**
250
+   * Set the PWM duty cycle for the pin to the given value.
251
+   * Optionally invert the duty cycle [default = false]
252
+   * Optionally change the scale of the provided value to enable finer PWM duty control [default = 255]
253
+   */
254
+  static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);
222 255
 
223
-void HAL_reboot();
256
+  /**
257
+   * Set the frequency of the timer corresponding to the provided pin
258
+   * All Hardware PWM pins will run at the same frequency and
259
+   * All Software PWM pins will run at the same frequency
260
+   */
261
+  static void set_pwm_frequency(const pin_t pin, const uint16_t f_desired);
262
+};

+ 2
- 1
Marlin/src/HAL/LPC1768/Servo.h Vedi File

@@ -65,4 +65,5 @@ class libServo: public Servo {
65 65
   }
66 66
 };
67 67
 
68
-#define HAL_SERVO_LIB libServo
68
+class libServo;
69
+typedef libServo hal_servo_t;

+ 3
- 3
Marlin/src/HAL/LPC1768/fast_pwm.cpp Vedi File

@@ -21,16 +21,16 @@
21 21
  */
22 22
 #ifdef TARGET_LPC1768
23 23
 
24
-#include "../../inc/MarlinConfigPre.h"
24
+#include "../../inc/MarlinConfig.h"
25 25
 #include <pwm.h>
26 26
 
27
-void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
27
+void MarlinHAL::set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
28 28
   if (!LPC176x::pin_is_valid(pin)) return;
29 29
   if (LPC176x::pwm_attach_pin(pin))
30 30
     LPC176x::pwm_write_ratio(pin, invert ? 1.0f - (float)v / v_size : (float)v / v_size);  // map 1-254 onto PWM range
31 31
 }
32 32
 
33
-void set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {
33
+void MarlinHAL::set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {
34 34
   LPC176x::pwm_set_frequency(pin, f_desired);
35 35
 }
36 36
 

+ 3
- 3
Marlin/src/HAL/LPC1768/main.cpp Vedi File

@@ -48,7 +48,7 @@ void SysTick_Callback() { disk_timerproc(); }
48 48
 
49 49
 TERN_(POSTMORTEM_DEBUGGING, extern void install_min_serial());
50 50
 
51
-void HAL_init() {
51
+void MarlinHAL::init() {
52 52
 
53 53
   // Init LEDs
54 54
   #if PIN_EXISTS(LED)
@@ -130,7 +130,7 @@ void HAL_init() {
130 130
   const millis_t usb_timeout = millis() + 2000;
131 131
   while (!USB_Configuration && PENDING(millis(), usb_timeout)) {
132 132
     delay(50);
133
-    HAL_idletask();
133
+    idletask();
134 134
     #if PIN_EXISTS(LED)
135 135
       TOGGLE(LED_PIN);     // Flash quickly during USB initialization
136 136
     #endif
@@ -142,7 +142,7 @@ void HAL_init() {
142 142
 }
143 143
 
144 144
 // HAL idle task
145
-void HAL_idletask() {
145
+void MarlinHAL::idletask() {
146 146
   #if HAS_SHARED_MEDIA
147 147
     // If Marlin is using the SD card we need to lock it to prevent access from
148 148
     // a PC via USB.

+ 1
- 1
Marlin/src/HAL/LPC1768/timers.h Vedi File

@@ -170,4 +170,4 @@ FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
170 170
   }
171 171
 }
172 172
 
173
-#define HAL_timer_isr_epilogue(T)
173
+#define HAL_timer_isr_epilogue(T) NOOP

+ 107
- 66
Marlin/src/HAL/NATIVE_SIM/HAL.h Vedi File

@@ -21,18 +21,10 @@
21 21
  */
22 22
 #pragma once
23 23
 
24
-#define CPU_32_BIT
25
-#define HAL_IDLETASK
26
-void HAL_idletask();
27
-
28
-#define F_CPU 100000000
29
-#define SystemCoreClock F_CPU
30 24
 #include <stdint.h>
31 25
 #include <stdarg.h>
32
-
33 26
 #undef min
34 27
 #undef max
35
-
36 28
 #include <algorithm>
37 29
 #include "pinmapping.h"
38 30
 
@@ -40,8 +32,6 @@ void _printf (const  char *format, ...);
40 32
 void _putc(uint8_t c);
41 33
 uint8_t _getc();
42 34
 
43
-//extern "C" volatile uint32_t _millis;
44
-
45 35
 //arduino: Print.h
46 36
 #define DEC 10
47 37
 #define HEX 16
@@ -58,7 +48,23 @@ uint8_t _getc();
58 48
 #include "watchdog.h"
59 49
 #include "serial.h"
60 50
 
61
-#define SHARED_SERVOS HAS_SERVOS
51
+// ------------------------
52
+// Defines
53
+// ------------------------
54
+
55
+#define CPU_32_BIT
56
+#define SHARED_SERVOS HAS_SERVOS  // Use shared/servos.cpp
57
+
58
+#define F_CPU 100000000
59
+#define SystemCoreClock F_CPU
60
+
61
+#define CPU_ST7920_DELAY_1 600
62
+#define CPU_ST7920_DELAY_2 750
63
+#define CPU_ST7920_DELAY_3 750
64
+
65
+// ------------------------
66
+// Serial ports
67
+// ------------------------
62 68
 
63 69
 extern MSerialT serial_stream_0;
64 70
 extern MSerialT serial_stream_1;
@@ -98,49 +104,19 @@ extern MSerialT serial_stream_3;
98 104
   #endif
99 105
 #endif
100 106
 
101
-
102
-#define CPU_ST7920_DELAY_1 600
103
-#define CPU_ST7920_DELAY_2 750
104
-#define CPU_ST7920_DELAY_3 750
105
-
106
-//
107
+// ------------------------
107 108
 // Interrupts
108
-//
109
+// ------------------------
110
+
109 111
 #define CRITICAL_SECTION_START()
110 112
 #define CRITICAL_SECTION_END()
111
-#define ISRS_ENABLED()
112
-#define ENABLE_ISRS()
113
-#define DISABLE_ISRS()
114
-
115
-inline void HAL_init() {}
116
-
117
-// Utility functions
118
-#pragma GCC diagnostic push
119
-#pragma GCC diagnostic ignored "-Wunused-function"
120
-int freeMemory();
121
-#pragma GCC diagnostic pop
122 113
 
114
+// ------------------------
123 115
 // ADC
116
+// ------------------------
117
+
124 118
 #define HAL_ADC_VREF           5.0
125 119
 #define HAL_ADC_RESOLUTION    10
126
-#define HAL_ANALOG_SELECT(ch) HAL_adc_enable_channel(ch)
127
-#define HAL_START_ADC(ch)     HAL_adc_start_conversion(ch)
128
-#define HAL_READ_ADC()        HAL_adc_get_result()
129
-#define HAL_ADC_READY()       true
130
-
131
-void HAL_adc_init();
132
-void HAL_adc_enable_channel(const uint8_t ch);
133
-void HAL_adc_start_conversion(const uint8_t ch);
134
-uint16_t HAL_adc_get_result();
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
-
139
-// Reset source
140
-inline void HAL_clear_reset_source(void) {}
141
-inline uint8_t HAL_get_reset_source(void) { return RST_POWER_ON; }
142
-
143
-void HAL_reboot();
144 120
 
145 121
 /* ---------------- Delay in cycles */
146 122
 
@@ -159,29 +135,22 @@ constexpr inline std::size_t strlen_constexpr(const char* str) {
159 135
   // https://github.com/gcc-mirror/gcc/blob/5c7634a0e5f202935aa6c11b6ea953b8bf80a00a/libstdc%2B%2B-v3/include/bits/char_traits.h#L329
160 136
   if (str != nullptr) {
161 137
     std::size_t i = 0;
162
-    while (str[i] != '\0') {
163
-      ++i;
164
-    }
165
-
138
+    while (str[i] != '\0') ++i;
166 139
     return i;
167 140
   }
168
-
169 141
   return 0;
170 142
 }
171 143
 
172 144
 constexpr inline int strncmp_constexpr(const char* lhs, const char* rhs, std::size_t count) {
173 145
   // https://github.com/gcc-mirror/gcc/blob/13b9cbfc32fe3ac4c81c4dd9c42d141c8fb95db4/libstdc%2B%2B-v3/include/bits/char_traits.h#L655
174
-  if (lhs == nullptr || rhs == nullptr) {
146
+  if (lhs == nullptr || rhs == nullptr)
175 147
     return rhs != nullptr ? -1 : 1;
176
-  }
177 148
 
178
-  for (std::size_t i = 0; i < count; ++i) {
179
-    if (lhs[i] != rhs[i]) {
149
+  for (std::size_t i = 0; i < count; ++i)
150
+    if (lhs[i] != rhs[i])
180 151
       return lhs[i] < rhs[i] ? -1 : 1;
181
-    } else if (lhs[i] == '\0') {
152
+    else if (lhs[i] == '\0')
182 153
       return 0;
183
-    }
184
-  }
185 154
 
186 155
   return 0;
187 156
 }
@@ -193,14 +162,11 @@ constexpr inline const char* strstr_constexpr(const char* str, const char* targe
193 162
     do {
194 163
       char sc = {};
195 164
       do {
196
-        if ((sc = *str++) == '\0') {
197
-          return nullptr;
198
-        }
165
+        if ((sc = *str++) == '\0') return nullptr;
199 166
       } while (sc != c);
200 167
     } while (strncmp_constexpr(str, target, len) != 0);
201 168
     --str;
202 169
   }
203
-
204 170
   return str;
205 171
 }
206 172
 
@@ -211,12 +177,87 @@ constexpr inline char* strstr_constexpr(char* str, const char* target) {
211 177
     do {
212 178
       char sc = {};
213 179
       do {
214
-        if ((sc = *str++) == '\0') {
215
-          return nullptr;
216
-        }
180
+        if ((sc = *str++) == '\0') return nullptr;
217 181
       } while (sc != c);
218 182
     } while (strncmp_constexpr(str, target, len) != 0);
219 183
     --str;
220 184
   }
221 185
   return str;
222 186
 }
187
+
188
+// ------------------------
189
+// Class Utilities
190
+// ------------------------
191
+
192
+#pragma GCC diagnostic push
193
+#if GCC_VERSION <= 50000
194
+  #pragma GCC diagnostic ignored "-Wunused-function"
195
+#endif
196
+
197
+int freeMemory();
198
+
199
+#pragma GCC diagnostic pop
200
+
201
+// ------------------------
202
+// MarlinHAL Class
203
+// ------------------------
204
+
205
+class MarlinHAL {
206
+public:
207
+
208
+  // Earliest possible init, before setup()
209
+  MarlinHAL() {}
210
+
211
+  static void init() {}        // Called early in setup()
212
+  static void init_board() {}  // Called less early in setup()
213
+  static void reboot();        // Restart the firmware from 0x0
214
+
215
+  // Interrupts
216
+  static bool isr_state() { return true; }
217
+  static void isr_on()  {}
218
+  static void isr_off() {}
219
+
220
+  static void delay_ms(const int ms) { _delay_ms(ms); }
221
+
222
+  // Tasks, called from idle()
223
+  static void idletask();
224
+
225
+  // Reset
226
+  static constexpr uint8_t reset_reason = RST_POWER_ON;
227
+  static uint8_t get_reset_source() { return reset_reason; }
228
+  static void clear_reset_source() {}
229
+
230
+  // Free SRAM
231
+  static int freeMemory() { return ::freeMemory(); }
232
+
233
+  //
234
+  // ADC Methods
235
+  //
236
+
237
+  static uint8_t active_ch;
238
+
239
+  // Called by Temperature::init once at startup
240
+  static void adc_init();
241
+
242
+  // Called by Temperature::init for each sensor at startup
243
+  static void adc_enable(const uint8_t ch);
244
+
245
+  // Begin ADC sampling on the given channel
246
+  static void adc_start(const uint8_t ch);
247
+
248
+  // Is the ADC ready for reading?
249
+  static bool adc_ready();
250
+
251
+  // The current value of the ADC register
252
+  static uint16_t adc_value();
253
+
254
+  /**
255
+   * Set the PWM duty cycle for the pin to the given value.
256
+   * No option to invert the duty cycle [default = false]
257
+   * No option to change the scale of the provided value to enable finer PWM duty control [default = 255]
258
+   */
259
+  static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) {
260
+    analogWrite(pin, v);
261
+  }
262
+
263
+};

+ 2
- 2
Marlin/src/HAL/NATIVE_SIM/timers.h Vedi File

@@ -87,5 +87,5 @@ void HAL_timer_enable_interrupt(const uint8_t timer_num);
87 87
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
88 88
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
89 89
 
90
-#define HAL_timer_isr_prologue(T)
91
-#define HAL_timer_isr_epilogue(T)
90
+#define HAL_timer_isr_prologue(T) NOOP
91
+#define HAL_timer_isr_epilogue(T) NOOP

+ 182
- 47
Marlin/src/HAL/SAMD51/HAL.cpp Vedi File

@@ -42,10 +42,6 @@
42 42
   #endif
43 43
 #endif
44 44
 
45
-// ------------------------
46
-// Local defines
47
-// ------------------------
48
-
49 45
 #define GET_TEMP_0_ADC()          TERN(HAS_TEMP_ADC_0,        PIN_TO_ADC(TEMP_0_PIN),       -1)
50 46
 #define GET_TEMP_1_ADC()          TERN(HAS_TEMP_ADC_1,        PIN_TO_ADC(TEMP_1_PIN),       -1)
51 47
 #define GET_TEMP_2_ADC()          TERN(HAS_TEMP_ADC_2,        PIN_TO_ADC(TEMP_2_PIN),       -1)
@@ -61,17 +57,21 @@
61 57
 #define GET_BOARD_ADC()           TERN(HAS_TEMP_ADC_BOARD,    PIN_TO_ADC(TEMP_BOARD_PIN),   -1)
62 58
 #define GET_FILAMENT_WIDTH_ADC()  TERN(FILAMENT_WIDTH_SENSOR, PIN_TO_ADC(FILWIDTH_PIN),     -1)
63 59
 #define GET_BUTTONS_ADC()         TERN(HAS_ADC_BUTTONS,       PIN_TO_ADC(ADC_KEYPAD_PIN),   -1)
60
+#define GET_JOY_ADC_X()           TERN(HAS_JOY_ADC_X,         PIN_TO_ADC(JOY_X_PIN),        -1)
61
+#define GET_JOY_ADC_Y()           TERN(HAS_JOY_ADC_Y,         PIN_TO_ADC(JOY_Y_PIN),        -1)
62
+#define GET_JOY_ADC_Z()           TERN(HAS_JOY_ADC_Z,         PIN_TO_ADC(JOY_Z_PIN),        -1)
64 63
 
65 64
 #define IS_ADC_REQUIRED(n) ( \
66 65
      GET_TEMP_0_ADC() == n || GET_TEMP_1_ADC() == n || GET_TEMP_2_ADC() == n || GET_TEMP_3_ADC() == n \
67 66
   || GET_TEMP_4_ADC() == n || GET_TEMP_5_ADC() == n || GET_TEMP_6_ADC() == n || GET_TEMP_7_ADC() == n \
68
-  || GET_BED_ADC() == n            \
69
-  || GET_CHAMBER_ADC() == n        \
70
-  || GET_PROBE_ADC() == n          \
71
-  || GET_COOLER_ADC() == n         \
72
-  || GET_BOARD_ADC() == n          \
67
+  || GET_BED_ADC() == n \
68
+  || GET_CHAMBER_ADC() == n \
69
+  || GET_PROBE_ADC() == n \
70
+  || GET_COOLER_ADC() == n \
71
+  || GET_BOARD_ADC() == n \
73 72
   || GET_FILAMENT_WIDTH_ADC() == n \
74
-  || GET_BUTTONS_ADC() == n        \
73
+  || GET_BUTTONS_ADC() == n \
74
+  || GET_JOY_ADC_X() == n || GET_JOY_ADC_Y() == n || GET_JOY_ADC_Z() == n \
75 75
 )
76 76
 
77 77
 #if IS_ADC_REQUIRED(0)
@@ -91,6 +91,118 @@
91 91
   #define DMA_IS_REQUIRED 1
92 92
 #endif
93 93
 
94
+enum ADCIndex {
95
+  #if GET_TEMP_0_ADC() == 0
96
+    TEMP_0,
97
+  #endif
98
+  #if GET_TEMP_1_ADC() == 0
99
+    TEMP_1,
100
+  #endif
101
+  #if GET_TEMP_2_ADC() == 0
102
+    TEMP_2,
103
+  #endif
104
+  #if GET_TEMP_3_ADC() == 0
105
+    TEMP_3,
106
+  #endif
107
+  #if GET_TEMP_4_ADC() == 0
108
+    TEMP_4,
109
+  #endif
110
+  #if GET_TEMP_5_ADC() == 0
111
+    TEMP_5,
112
+  #endif
113
+  #if GET_TEMP_6_ADC() == 0
114
+    TEMP_6,
115
+  #endif
116
+  #if GET_TEMP_7_ADC() == 0
117
+    TEMP_7,
118
+  #endif
119
+  #if GET_BED_ADC() == 0
120
+    TEMP_BED,
121
+  #endif
122
+  #if GET_CHAMBER_ADC() == 0
123
+    TEMP_CHAMBER,
124
+  #endif
125
+  #if GET_PROBE_ADC() == 0
126
+    TEMP_PROBE,
127
+  #endif
128
+  #if GET_COOLER_ADC() == 0
129
+    TEMP_COOLER,
130
+  #endif
131
+  #if GET_BOARD_ADC() == 0
132
+    TEMP_BOARD,
133
+  #endif
134
+  #if GET_FILAMENT_WIDTH_ADC() == 0
135
+    FILWIDTH,
136
+  #endif
137
+  #if GET_BUTTONS_ADC() == 0
138
+    ADC_KEY,
139
+  #endif
140
+  #if GET_JOY_ADC_X() == 0
141
+    JOY_X,
142
+  #endif
143
+  #if GET_JOY_ADC_Y() == 0
144
+    JOY_Y,
145
+  #endif
146
+  #if GET_JOY_ADC_Z() == 0
147
+    JOY_Z,
148
+  #endif
149
+  #if GET_TEMP_0_ADC() == 1
150
+    TEMP_0,
151
+  #endif
152
+  #if GET_TEMP_1_ADC() == 1
153
+    TEMP_1,
154
+  #endif
155
+  #if GET_TEMP_2_ADC() == 1
156
+    TEMP_2,
157
+  #endif
158
+  #if GET_TEMP_3_ADC() == 1
159
+    TEMP_3,
160
+  #endif
161
+  #if GET_TEMP_4_ADC() == 1
162
+    TEMP_4,
163
+  #endif
164
+  #if GET_TEMP_5_ADC() == 1
165
+    TEMP_5,
166
+  #endif
167
+  #if GET_TEMP_6_ADC() == 1
168
+    TEMP_6,
169
+  #endif
170
+  #if GET_TEMP_7_ADC() == 1
171
+    TEMP_7,
172
+  #endif
173
+  #if GET_BED_ADC() == 1
174
+    TEMP_BED,
175
+  #endif
176
+  #if GET_CHAMBER_ADC() == 1
177
+    TEMP_CHAMBER,
178
+  #endif
179
+  #if GET_PROBE_ADC() == 1
180
+    TEMP_PROBE,
181
+  #endif
182
+  #if GET_COOLER_ADC() == 1
183
+    TEMP_COOLER,
184
+  #endif
185
+  #if GET_BOARD_ADC() == 1
186
+    TEMP_BOARD,
187
+  #endif
188
+  #if GET_FILAMENT_WIDTH_ADC() == 1
189
+    FILWIDTH,
190
+  #endif
191
+  #if GET_BUTTONS_ADC() == 1
192
+    ADC_KEY,
193
+  #endif
194
+  #if GET_JOY_ADC_X() == 1
195
+    JOY_X,
196
+  #endif
197
+  #if GET_JOY_ADC_Y() == 1
198
+    JOY_Y,
199
+  #endif
200
+  #if GET_JOY_ADC_Z() == 1
201
+    JOY_Z,
202
+  #endif
203
+  ADC_COUNT
204
+};
205
+
94 206
 // ------------------------
95 207
 // Types
96 208
 // ------------------------
@@ -108,12 +220,10 @@
108 220
 // Private Variables
109 221
 // ------------------------
110 222
 
111
-uint16_t HAL_adc_result;
112
-
113 223
 #if ADC_IS_REQUIRED
114 224
 
115 225
   // Pins used by ADC inputs. Order must be ADC0 inputs first then ADC1
116
-  const uint8_t adc_pins[] = {
226
+  static constexpr uint8_t adc_pins[ADC_COUNT] = {
117 227
     // ADC0 pins
118 228
     #if GET_TEMP_0_ADC() == 0
119 229
       TEMP_0_PIN,
@@ -160,6 +270,15 @@ uint16_t HAL_adc_result;
160 270
     #if GET_BUTTONS_ADC() == 0
161 271
       ADC_KEYPAD_PIN,
162 272
     #endif
273
+    #if GET_JOY_ADC_X() == 0
274
+      JOY_X_PIN,
275
+    #endif
276
+    #if GET_JOY_ADC_Y() == 0
277
+      JOY_Y_PIN,
278
+    #endif
279
+    #if GET_JOY_ADC_Z() == 0
280
+      JOY_Z_PIN,
281
+    #endif
163 282
     // ADC1 pins
164 283
     #if GET_TEMP_0_ADC() == 1
165 284
       TEMP_0_PIN,
@@ -206,15 +325,23 @@ uint16_t HAL_adc_result;
206 325
     #if GET_BUTTONS_ADC() == 1
207 326
       ADC_KEYPAD_PIN,
208 327
     #endif
328
+    #if GET_JOY_ADC_X() == 1
329
+      JOY_X_PIN,
330
+    #endif
331
+    #if GET_JOY_ADC_Y() == 1
332
+      JOY_Y_PIN,
333
+    #endif
334
+    #if GET_JOY_ADC_Z() == 1
335
+      JOY_Z_PIN,
336
+    #endif
209 337
   };
210 338
 
211
-  uint16_t HAL_adc_results[COUNT(adc_pins)];
339
+  static uint16_t adc_results[ADC_COUNT];
212 340
 
213 341
   #if ADC0_IS_REQUIRED
214
-    Adafruit_ZeroDMA adc0DMAProgram,
215
-                     adc0DMARead;
342
+    Adafruit_ZeroDMA adc0DMAProgram, adc0DMARead;
216 343
 
217
-    const HAL_DMA_DAC_Registers adc0_dma_regs_list[] = {
344
+    static constexpr HAL_DMA_DAC_Registers adc0_dma_regs_list[ADC_COUNT] = {
218 345
       #if GET_TEMP_0_ADC() == 0
219 346
         { PIN_TO_INPUTCTRL(TEMP_0_PIN) },
220 347
       #endif
@@ -260,16 +387,24 @@ uint16_t HAL_adc_result;
260 387
       #if GET_BUTTONS_ADC() == 0
261 388
         { PIN_TO_INPUTCTRL(ADC_KEYPAD_PIN) },
262 389
       #endif
390
+      #if GET_JOY_ADC_X() == 0
391
+        { PIN_TO_INPUTCTRL(JOY_X_PIN) },
392
+      #endif
393
+      #if GET_JOY_ADC_Y() == 0
394
+        { PIN_TO_INPUTCTRL(JOY_Y_PIN) },
395
+      #endif
396
+      #if GET_JOY_ADC_Z() == 0
397
+        { PIN_TO_INPUTCTRL(JOY_Z_PIN) },
398
+      #endif
263 399
     };
264 400
 
265 401
     #define ADC0_AINCOUNT   COUNT(adc0_dma_regs_list)
266 402
   #endif // ADC0_IS_REQUIRED
267 403
 
268 404
   #if ADC1_IS_REQUIRED
269
-    Adafruit_ZeroDMA adc1DMAProgram,
270
-                     adc1DMARead;
405
+    Adafruit_ZeroDMA adc1DMAProgram, adc1DMARead;
271 406
 
272
-    const HAL_DMA_DAC_Registers adc1_dma_regs_list[] = {
407
+    static constexpr HAL_DMA_DAC_Registers adc1_dma_regs_list[ADC_COUNT] = {
273 408
       #if GET_TEMP_0_ADC() == 1
274 409
         { PIN_TO_INPUTCTRL(TEMP_0_PIN) },
275 410
       #endif
@@ -315,6 +450,15 @@ uint16_t HAL_adc_result;
315 450
       #if GET_BUTTONS_ADC() == 1
316 451
         { PIN_TO_INPUTCTRL(ADC_KEYPAD_PIN) },
317 452
       #endif
453
+      #if GET_JOY_ADC_X() == 1
454
+        { PIN_TO_INPUTCTRL(JOY_X_PIN) },
455
+      #endif
456
+      #if GET_JOY_ADC_Y() == 1
457
+        { PIN_TO_INPUTCTRL(JOY_Y_PIN) },
458
+      #endif
459
+      #if GET_JOY_ADC_Z() == 1
460
+        { PIN_TO_INPUTCTRL(JOY_Z_PIN) },
461
+      #endif
318 462
     };
319 463
 
320 464
     #define ADC1_AINCOUNT   COUNT(adc1_dma_regs_list)
@@ -326,9 +470,10 @@ uint16_t HAL_adc_result;
326 470
 // Private functions
327 471
 // ------------------------
328 472
 
329
-#if DMA_IS_REQUIRED
473
+void MarlinHAL::dma_init() {
474
+
475
+  #if DMA_IS_REQUIRED
330 476
 
331
-  void dma_init() {
332 477
     DmacDescriptor *descriptor;
333 478
 
334 479
     #if ADC0_IS_REQUIRED
@@ -357,7 +502,7 @@ uint16_t HAL_adc_result;
357 502
       if (adc0DMARead.allocate() == DMA_STATUS_OK) {
358 503
         adc0DMARead.addDescriptor(
359 504
           (void *)&ADC0->RESULT.reg,          // SRC
360
-          &HAL_adc_results,                   // DEST
505
+          &adc_results,                       // DEST
361 506
           ADC0_AINCOUNT,                      // CNT
362 507
           DMA_BEAT_SIZE_HWORD,
363 508
           false,                              // SRCINC
@@ -394,7 +539,7 @@ uint16_t HAL_adc_result;
394 539
       if (adc1DMARead.allocate() == DMA_STATUS_OK) {
395 540
         adc1DMARead.addDescriptor(
396 541
           (void *)&ADC1->RESULT.reg,          // SRC
397
-          &HAL_adc_results[ADC0_AINCOUNT],    // DEST
542
+          &adc_results[ADC0_AINCOUNT],        // DEST
398 543
           ADC1_AINCOUNT,                      // CNT
399 544
           DMA_BEAT_SIZE_HWORD,
400 545
           false,                              // SRCINC
@@ -407,16 +552,16 @@ uint16_t HAL_adc_result;
407 552
     #endif
408 553
 
409 554
     DMAC->PRICTRL0.bit.RRLVLEN0 = true;                         // Activate round robin for DMA channels required by ADCs
410
-  }
411 555
 
412
-#endif // DMA_IS_REQUIRED
556
+  #endif // DMA_IS_REQUIRED
557
+}
413 558
 
414 559
 // ------------------------
415 560
 // Public functions
416 561
 // ------------------------
417 562
 
418 563
 // HAL initialization task
419
-void HAL_init() {
564
+void MarlinHAL::init() {
420 565
   TERN_(DMA_IS_REQUIRED, dma_init());
421 566
   #if ENABLED(SDSUPPORT)
422 567
     #if SD_CONNECTION_IS(ONBOARD) && PIN_EXISTS(SD_DETECT)
@@ -426,17 +571,9 @@ void HAL_init() {
426 571
   #endif
427 572
 }
428 573
 
429
-// HAL idle task
430
-/*
431
-void HAL_idletask() {
432
-}
433
-*/
434
-
435
-void HAL_clear_reset_source() { }
436
-
437 574
 #pragma push_macro("WDT")
438 575
 #undef WDT    // Required to be able to use '.bit.WDT'. Compiler wrongly replace struct field with WDT define
439
-uint8_t HAL_get_reset_source() {
576
+uint8_t MarlinHAL::get_reset_source() {
440 577
   RSTC_RCAUSE_Type resetCause;
441 578
 
442 579
   resetCause.reg = REG_RSTC_RCAUSE;
@@ -450,7 +587,7 @@ uint8_t HAL_get_reset_source() {
450 587
 }
451 588
 #pragma pop_macro("WDT")
452 589
 
453
-void HAL_reboot() { NVIC_SystemReset(); }
590
+void MarlinHAL::reboot() { NVIC_SystemReset(); }
454 591
 
455 592
 extern "C" {
456 593
   void * _sbrk(int incr);
@@ -468,9 +605,11 @@ int freeMemory() {
468 605
 // ADC
469 606
 // ------------------------
470 607
 
471
-void HAL_adc_init() {
608
+uint16_t MarlinHAL::adc_result;
609
+
610
+void MarlinHAL::adc_init() {
472 611
   #if ADC_IS_REQUIRED
473
-    memset(HAL_adc_results, 0xFF, sizeof(HAL_adc_results));                 // Fill result with invalid values
612
+    memset(adc_results, 0xFF, sizeof(adc_results));                         // Fill result with invalid values
474 613
 
475 614
     LOOP_L_N(pi, COUNT(adc_pins))
476 615
       pinPeripheral(adc_pins[pi], PIO_ANALOG);
@@ -505,17 +644,13 @@ void HAL_adc_init() {
505 644
   #endif // ADC_IS_REQUIRED
506 645
 }
507 646
 
508
-void HAL_adc_start_conversion(const uint8_t adc_pin) {
647
+void MarlinHAL::adc_start(const pin_t pin) {
509 648
   #if ADC_IS_REQUIRED
510
-    LOOP_L_N(pi, COUNT(adc_pins)) {
511
-      if (adc_pin == adc_pins[pi]) {
512
-        HAL_adc_result = HAL_adc_results[pi];
513
-        return;
514
-      }
515
-    }
649
+    LOOP_L_N(pi, COUNT(adc_pins))
650
+      if (pin == adc_pins[pi]) { adc_result = adc_results[pi]; return; }
516 651
   #endif
517 652
 
518
-  HAL_adc_result = 0xFFFF;
653
+  adc_result = 0xFFFF;
519 654
 }
520 655
 
521 656
 #endif // __SAMD51__

+ 83
- 46
Marlin/src/HAL/SAMD51/HAL.h Vedi File

@@ -89,51 +89,30 @@
89 89
 
90 90
 typedef int8_t pin_t;
91 91
 
92
-#define SHARED_SERVOS HAS_SERVOS
93
-#define HAL_SERVO_LIB Servo
92
+#define SHARED_SERVOS HAS_SERVOS  // Use shared/servos.cpp
93
+
94
+class Servo;
95
+typedef Servo hal_servo_t;
94 96
 
95 97
 //
96 98
 // Interrupts
97 99
 //
98
-#define CRITICAL_SECTION_START()  uint32_t primask = __get_PRIMASK(); __disable_irq()
99
-#define CRITICAL_SECTION_END()    if (!primask) __enable_irq()
100
-#define ISRS_ENABLED() (!__get_PRIMASK())
101
-#define ENABLE_ISRS()  __enable_irq()
102
-#define DISABLE_ISRS() __disable_irq()
103
-
104
-#define cli() __disable_irq()       // Disable interrupts
105
-#define sei() __enable_irq()        // Enable interrupts
106
-
107
-void HAL_clear_reset_source();  // clear reset reason
108
-uint8_t HAL_get_reset_source(); // get reset reason
100
+#define CRITICAL_SECTION_START()  const bool irqon = !__get_PRIMASK(); __disable_irq()
101
+#define CRITICAL_SECTION_END()    if (irqon) __enable_irq()
109 102
 
110
-void HAL_reboot();
103
+#define cli() __disable_irq() // Disable interrupts
104
+#define sei() __enable_irq()  // Enable interrupts
111 105
 
112 106
 //
113 107
 // ADC
114 108
 //
115
-extern uint16_t HAL_adc_result;     // Most recent ADC conversion
116
-
117
-#define HAL_ANALOG_SELECT(pin)
118
-
119
-void HAL_adc_init();
120 109
 
121 110
 //#define HAL_ADC_FILTERED          // Disable Marlin's oversampling. The HAL filters ADC values.
122 111
 #define HAL_ADC_VREF         3.3
123 112
 #define HAL_ADC_RESOLUTION  10      // ... 12
124
-#define HAL_START_ADC(pin)  HAL_adc_start_conversion(pin)
125
-#define HAL_READ_ADC()      HAL_adc_result
126
-#define HAL_ADC_READY()     true
127 113
 
128
-void HAL_adc_start_conversion(const uint8_t adc_pin);
129
-
130
-//
131
-// PWM
132 114
 //
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
-//
136
-// Pin Map
115
+// Pin Mapping for M42, M43, M226
137 116
 //
138 117
 #define GET_PIN_MAP_PIN(index) index
139 118
 #define GET_PIN_MAP_INDEX(pin) pin
@@ -142,35 +121,93 @@ inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255,
142 121
 //
143 122
 // Tone
144 123
 //
145
-void toneInit();
146 124
 void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration=0);
147 125
 void noTone(const pin_t _pin);
148 126
 
149
-// Enable hooks into idle and setup for HAL
150
-void HAL_init();
151
-/*
152
-#define HAL_IDLETASK 1
153
-void HAL_idletask();
154
-*/
155
-
156
-//
157
-// Utility functions
158
-//
159
-FORCE_INLINE void _delay_ms(const int delay_ms) { delay(delay_ms); }
127
+// ------------------------
128
+// Class Utilities
129
+// ------------------------
160 130
 
161 131
 #pragma GCC diagnostic push
162 132
 #if GCC_VERSION <= 50000
163 133
   #pragma GCC diagnostic ignored "-Wunused-function"
164 134
 #endif
165 135
 
166
-int freeMemory();
167
-
168
-#pragma GCC diagnostic pop
169
-
170 136
 #ifdef __cplusplus
171 137
   extern "C" {
172 138
 #endif
139
+
173 140
 char *dtostrf(double __val, signed char __width, unsigned char __prec, char *__s);
141
+
142
+extern "C" int freeMemory();
143
+
174 144
 #ifdef __cplusplus
175 145
   }
176 146
 #endif
147
+
148
+#pragma GCC diagnostic pop
149
+
150
+// ------------------------
151
+// MarlinHAL Class
152
+// ------------------------
153
+
154
+class MarlinHAL {
155
+public:
156
+
157
+  // Earliest possible init, before setup()
158
+  MarlinHAL() {}
159
+
160
+  static void init();          // Called early in setup()
161
+  static void init_board() {}  // Called less early in setup()
162
+  static void reboot();        // Restart the firmware from 0x0
163
+
164
+  // Interrupts
165
+  static bool isr_state() { return !__get_PRIMASK(); }
166
+  static void isr_on()  { sei(); }
167
+  static void isr_off() { cli(); }
168
+
169
+  static void delay_ms(const int ms) { delay(ms); }
170
+
171
+  // Tasks, called from idle()
172
+  static void idletask() {}
173
+
174
+  // Reset
175
+  static uint8_t get_reset_source();
176
+  static void clear_reset_source() {}
177
+
178
+  // Free SRAM
179
+  static int freeMemory() { return ::freeMemory(); }
180
+
181
+  //
182
+  // ADC Methods
183
+  //
184
+
185
+  static uint16_t adc_result;
186
+
187
+  // Called by Temperature::init once at startup
188
+  static void adc_init();
189
+
190
+  // Called by Temperature::init for each sensor at startup
191
+  static void adc_enable(const uint8_t ch) {}
192
+
193
+  // Begin ADC sampling on the given channel
194
+  static void adc_start(const pin_t pin);
195
+
196
+  // Is the ADC ready for reading?
197
+  static bool adc_ready() { return true; }
198
+
199
+  // The current value of the ADC register
200
+  static uint16_t adc_value() { return adc_result; }
201
+
202
+  /**
203
+   * Set the PWM duty cycle for the pin to the given value.
204
+   * No option to invert the duty cycle [default = false]
205
+   * No option to change the scale of the provided value to enable finer PWM duty control [default = 255]
206
+   */
207
+  static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) {
208
+    analogWrite(pin, v);
209
+  }
210
+
211
+private:
212
+  static void dma_init();
213
+};

+ 10
- 18
Marlin/src/HAL/STM32/HAL.cpp Vedi File

@@ -53,16 +53,18 @@
53 53
 // Public Variables
54 54
 // ------------------------
55 55
 
56
-uint16_t HAL_adc_result;
56
+uint16_t MarlinHAL::adc_result;
57 57
 
58 58
 // ------------------------
59 59
 // Public functions
60 60
 // ------------------------
61 61
 
62
-TERN_(POSTMORTEM_DEBUGGING, extern void install_min_serial());
62
+#if ENABLED(POSTMORTEM_DEBUGGING)
63
+  extern void install_min_serial();
64
+#endif
63 65
 
64 66
 // HAL initialization task
65
-void HAL_init() {
67
+void MarlinHAL::init() {
66 68
   // Ensure F_CPU is a constant expression.
67 69
   // If the compiler breaks here, it means that delay code that should compute at compile time will not work.
68 70
   // So better safe than sorry here.
@@ -103,7 +105,7 @@ void HAL_init() {
103 105
 }
104 106
 
105 107
 // HAL idle task
106
-void HAL_idletask() {
108
+void MarlinHAL::idletask() {
107 109
   #if HAS_SHARED_MEDIA
108 110
     // Stm32duino currently doesn't have a "loop/idle" method
109 111
     CDC_resume_receive();
@@ -111,9 +113,9 @@ void HAL_idletask() {
111 113
   #endif
112 114
 }
113 115
 
114
-void HAL_clear_reset_source() { __HAL_RCC_CLEAR_RESET_FLAGS(); }
116
+void MarlinHAL::reboot() { NVIC_SystemReset(); }
115 117
 
116
-uint8_t HAL_get_reset_source() {
118
+uint8_t MarlinHAL::get_reset_source() {
117 119
   return
118 120
     #ifdef RCC_FLAG_IWDGRST // Some sources may not exist...
119 121
       RESET != __HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST)  ? RST_WATCHDOG :
@@ -137,24 +139,14 @@ uint8_t HAL_get_reset_source() {
137 139
   ;
138 140
 }
139 141
 
140
-void HAL_reboot() { NVIC_SystemReset(); }
141
-
142
-void _delay_ms(const int delay_ms) { delay(delay_ms); }
142
+void MarlinHAL::clear_reset_source() { __HAL_RCC_CLEAR_RESET_FLAGS(); }
143 143
 
144 144
 extern "C" {
145 145
   extern unsigned int _ebss; // end of bss section
146 146
 }
147 147
 
148
-// ------------------------
149
-// ADC
150
-// ------------------------
151
-
152
-// TODO: Make sure this doesn't cause any delay
153
-void HAL_adc_start_conversion(const uint8_t adc_pin) { HAL_adc_result = analogRead(adc_pin); }
154
-uint16_t HAL_adc_get_result() { return HAL_adc_result; }
155
-
156 148
 // Reset the system to initiate a firmware flash
157
-WEAK void flashFirmware(const int16_t) { HAL_reboot(); }
149
+WEAK void flashFirmware(const int16_t) { hal.reboot(); }
158 150
 
159 151
 // Maple Compatibility
160 152
 volatile uint32_t systick_uptime_millis = 0;

+ 100
- 76
Marlin/src/HAL/STM32/HAL.h Vedi File

@@ -44,9 +44,9 @@
44 44
 #define CPU_ST7920_DELAY_2  40
45 45
 #define CPU_ST7920_DELAY_3 340
46 46
 
47
-//
48
-// Serial Ports
49
-//
47
+// ------------------------
48
+// Serial ports
49
+// ------------------------
50 50
 #ifdef USBCON
51 51
   #include <USBSerial.h>
52 52
   #include "../../core/serial_hook.h"
@@ -115,17 +115,14 @@
115 115
   #define analogInputToDigitalPin(p) (p)
116 116
 #endif
117 117
 
118
-#define CRITICAL_SECTION_START()  uint32_t primask = __get_PRIMASK(); __disable_irq()
119
-#define CRITICAL_SECTION_END()    if (!primask) __enable_irq()
120
-#define ISRS_ENABLED() (!__get_PRIMASK())
121
-#define ENABLE_ISRS()  __enable_irq()
122
-#define DISABLE_ISRS() __disable_irq()
118
+//
119
+// Interrupts
120
+//
121
+#define CRITICAL_SECTION_START()  const bool irqon = !__get_PRIMASK(); __disable_irq()
122
+#define CRITICAL_SECTION_END()    if (irqon) __enable_irq()
123 123
 #define cli() __disable_irq()
124 124
 #define sei() __enable_irq()
125 125
 
126
-// On AVR this is in math.h?
127
-#define square(x) ((x)*(x))
128
-
129 126
 // ------------------------
130 127
 // Types
131 128
 // ------------------------
@@ -136,43 +133,61 @@
136 133
   typedef int16_t pin_t;
137 134
 #endif
138 135
 
139
-#define HAL_SERVO_LIB libServo
136
+class libServo;
137
+typedef libServo hal_servo_t;
140 138
 #define PAUSE_SERVO_OUTPUT() libServo::pause_all_servos()
141 139
 #define RESUME_SERVO_OUTPUT() libServo::resume_all_servos()
142 140
 
143 141
 // ------------------------
144
-// Public Variables
142
+// ADC
145 143
 // ------------------------
146 144
 
147
-// result of last ADC conversion
148
-extern uint16_t HAL_adc_result;
145
+#ifdef ADC_RESOLUTION
146
+  #define HAL_ADC_RESOLUTION ADC_RESOLUTION
147
+#else
148
+  #define HAL_ADC_RESOLUTION 12
149
+#endif
149 150
 
150
-// ------------------------
151
-// Public functions
152
-// ------------------------
151
+#define HAL_ADC_VREF         3.3
153 152
 
154
-// Memory related
155
-#define __bss_end __bss_end__
153
+//
154
+// Pin Mapping for M42, M43, M226
155
+//
156
+#define GET_PIN_MAP_PIN(index) index
157
+#define GET_PIN_MAP_INDEX(pin) pin
158
+#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
156 159
 
157
-// Enable hooks into  setup for HAL
158
-void HAL_init();
159
-#define HAL_IDLETASK 1
160
-void HAL_idletask();
160
+#ifdef STM32F1xx
161
+  #define JTAG_DISABLE() AFIO_DBGAFR_CONFIG(AFIO_MAPR_SWJ_CFG_JTAGDISABLE)
162
+  #define JTAGSWD_DISABLE() AFIO_DBGAFR_CONFIG(AFIO_MAPR_SWJ_CFG_DISABLE)
163
+  #define JTAGSWD_RESET() AFIO_DBGAFR_CONFIG(AFIO_MAPR_SWJ_CFG_RESET); // Reset: FULL SWD+JTAG
164
+#endif
161 165
 
162
-// Clear reset reason
163
-void HAL_clear_reset_source();
166
+#define PLATFORM_M997_SUPPORT
167
+void flashFirmware(const int16_t);
164 168
 
165
-// Reset reason
166
-uint8_t HAL_get_reset_source();
169
+// Maple Compatibility
170
+typedef void (*systickCallback_t)(void);
171
+void systick_attach_callback(systickCallback_t cb);
172
+void HAL_SYSTICK_Callback();
167 173
 
168
-void HAL_reboot();
174
+extern volatile uint32_t systick_uptime_millis;
175
+
176
+#define HAL_CAN_SET_PWM_FREQ   // This HAL supports PWM Frequency adjustment
177
+
178
+// ------------------------
179
+// Class Utilities
180
+// ------------------------
169 181
 
170
-void _delay_ms(const int delay);
182
+// Memory related
183
+#define __bss_end __bss_end__
171 184
 
172 185
 extern "C" char* _sbrk(int incr);
173 186
 
174 187
 #pragma GCC diagnostic push
175
-#pragma GCC diagnostic ignored "-Wunused-function"
188
+#if GCC_VERSION <= 50000
189
+  #pragma GCC diagnostic ignored "-Wunused-function"
190
+#endif
176 191
 
177 192
 static inline int freeMemory() {
178 193
   volatile char top;
@@ -181,62 +196,71 @@ static inline int freeMemory() {
181 196
 
182 197
 #pragma GCC diagnostic pop
183 198
 
184
-//
185
-// ADC
186
-//
199
+// ------------------------
200
+// MarlinHAL Class
201
+// ------------------------
187 202
 
188
-#define HAL_ANALOG_SELECT(pin) pinMode(pin, INPUT)
203
+class MarlinHAL {
204
+public:
189 205
 
190
-#ifdef ADC_RESOLUTION
191
-  #define HAL_ADC_RESOLUTION ADC_RESOLUTION
192
-#else
193
-  #define HAL_ADC_RESOLUTION 12
194
-#endif
206
+  // Earliest possible init, before setup()
207
+  MarlinHAL() {}
195 208
 
196
-#define HAL_ADC_VREF         3.3
197
-#define HAL_START_ADC(pin)  HAL_adc_start_conversion(pin)
198
-#define HAL_READ_ADC()      HAL_adc_result
199
-#define HAL_ADC_READY()     true
209
+  static void init();                 // Called early in setup()
210
+  static void init_board() {}  // Called less early in setup()
211
+  static void reboot();               // Restart the firmware from 0x0
200 212
 
201
-inline void HAL_adc_init() { analogReadResolution(HAL_ADC_RESOLUTION); }
213
+  // Interrupts
214
+  static bool isr_state() { return !__get_PRIMASK(); }
215
+  static void isr_on()  { sei(); }
216
+  static void isr_off() { cli(); }
202 217
 
203
-void HAL_adc_start_conversion(const uint8_t adc_pin);
218
+  static void delay_ms(const int ms) { delay(ms); }
204 219
 
205
-uint16_t HAL_adc_get_result();
220
+  // Tasks, called from idle()
221
+  static void idletask();
206 222
 
207
-#define GET_PIN_MAP_PIN(index) index
208
-#define GET_PIN_MAP_INDEX(pin) pin
209
-#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
223
+  // Reset
224
+  static uint8_t get_reset_source();
225
+  static void clear_reset_source();
210 226
 
211
-#ifdef STM32F1xx
212
-  #define JTAG_DISABLE() AFIO_DBGAFR_CONFIG(AFIO_MAPR_SWJ_CFG_JTAGDISABLE)
213
-  #define JTAGSWD_DISABLE() AFIO_DBGAFR_CONFIG(AFIO_MAPR_SWJ_CFG_DISABLE)
214
-  #define JTAGSWD_RESET() AFIO_DBGAFR_CONFIG(AFIO_MAPR_SWJ_CFG_RESET); // Reset: FULL SWD+JTAG
215
-#endif
227
+  // Free SRAM
228
+  static int freeMemory() { return ::freeMemory(); }
216 229
 
217
-#define PLATFORM_M997_SUPPORT
218
-void flashFirmware(const int16_t);
230
+  //
231
+  // ADC Methods
232
+  //
219 233
 
220
-// Maple Compatibility
221
-typedef void (*systickCallback_t)(void);
222
-void systick_attach_callback(systickCallback_t cb);
223
-void HAL_SYSTICK_Callback();
234
+  static uint16_t adc_result;
224 235
 
225
-extern volatile uint32_t systick_uptime_millis;
236
+  // Called by Temperature::init once at startup
237
+  static void adc_init() {
238
+    analogReadResolution(HAL_ADC_RESOLUTION);
239
+  }
226 240
 
227
-#define HAL_CAN_SET_PWM_FREQ   // This HAL supports PWM Frequency adjustment
241
+  // Called by Temperature::init for each sensor at startup
242
+  static void adc_enable(const pin_t pin) { pinMode(pin, INPUT); }
228 243
 
229
-/**
230
- * set_pwm_frequency
231
- *  Set the frequency of the timer corresponding to the provided pin
232
- *  All Timer PWM pins run at the same frequency
233
- */
234
-void set_pwm_frequency(const pin_t pin, const uint16_t f_desired);
244
+  // Begin ADC sampling on the given channel
245
+  static void adc_start(const pin_t pin) { adc_result = analogRead(pin); }
235 246
 
236
-/**
237
- * set_pwm_duty
238
- *  Set the PWM duty cycle of the provided pin to the provided value
239
- *  Optionally allows inverting the duty cycle [default = false]
240
- *  Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
241
- */
242
-void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);
247
+  // Is the ADC ready for reading?
248
+  static bool adc_ready() { return true; }
249
+
250
+  // The current value of the ADC register
251
+  static uint16_t adc_value() { return adc_result; }
252
+
253
+  /**
254
+   * Set the PWM duty cycle for the pin to the given value.
255
+   * Optionally invert the duty cycle [default = false]
256
+   * Optionally change the maximum size of the provided value to enable finer PWM duty control [default = 255]
257
+   */
258
+  static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);
259
+
260
+  /**
261
+   * Set the frequency of the timer for the given pin.
262
+   * All Timer PWM pins run at the same frequency.
263
+   */
264
+  static void set_pwm_frequency(const pin_t pin, const uint16_t f_desired);
265
+
266
+};

+ 4
- 4
Marlin/src/HAL/STM32/HAL_SPI.cpp Vedi File

@@ -102,9 +102,9 @@ static SPISettings spiConfig;
102 102
 
103 103
   // Soft SPI receive byte
104 104
   uint8_t spiRec() {
105
-    DISABLE_ISRS();                                               // No interrupts during byte receive
105
+    hal.isr_off();                                                // No interrupts during byte receive
106 106
     const uint8_t data = HAL_SPI_STM32_SpiTransfer_Mode_3(0xFF);
107
-    ENABLE_ISRS();                                                // Enable interrupts
107
+    hal.isr_on();                                                 // Enable interrupts
108 108
     return data;
109 109
   }
110 110
 
@@ -116,9 +116,9 @@ static SPISettings spiConfig;
116 116
 
117 117
   // Soft SPI send byte
118 118
   void spiSend(uint8_t data) {
119
-    DISABLE_ISRS();                         // No interrupts during byte send
119
+    hal.isr_off();                          // No interrupts during byte send
120 120
     HAL_SPI_STM32_SpiTransfer_Mode_3(data); // Don't care what is received
121
-    ENABLE_ISRS();                          // Enable interrupts
121
+    hal.isr_on();                           // Enable interrupts
122 122
   }
123 123
 
124 124
   // Soft SPI send block

+ 4
- 4
Marlin/src/HAL/STM32/eeprom_flash.cpp Vedi File

@@ -174,9 +174,9 @@ bool PersistentStore::access_finish() {
174 174
         UNLOCK_FLASH();
175 175
 
176 176
         TERN_(HAS_PAUSE_SERVO_OUTPUT, PAUSE_SERVO_OUTPUT());
177
-        DISABLE_ISRS();
177
+        hal.isr_off();
178 178
         status = HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError);
179
-        ENABLE_ISRS();
179
+        hal.isr_on();
180 180
         TERN_(HAS_PAUSE_SERVO_OUTPUT, RESUME_SERVO_OUTPUT());
181 181
         if (status != HAL_OK) {
182 182
           DEBUG_ECHOLNPGM("HAL_FLASHEx_Erase=", status);
@@ -229,9 +229,9 @@ bool PersistentStore::access_finish() {
229 229
       // output. Servo output still glitches with interrupts disabled, but recovers after the
230 230
       // erase.
231 231
       TERN_(HAS_PAUSE_SERVO_OUTPUT, PAUSE_SERVO_OUTPUT());
232
-      DISABLE_ISRS();
232
+      hal.isr_off();
233 233
       eeprom_buffer_flush();
234
-      ENABLE_ISRS();
234
+      hal.isr_on();
235 235
       TERN_(HAS_PAUSE_SERVO_OUTPUT, RESUME_SERVO_OUTPUT());
236 236
 
237 237
       eeprom_data_written = false;

+ 2
- 2
Marlin/src/HAL/STM32/fast_pwm.cpp Vedi File

@@ -29,7 +29,7 @@
29 29
 // Array to support sticky frequency sets per timer
30 30
 static uint16_t timer_freq[TIMER_NUM];
31 31
 
32
-void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
32
+void MarlinHAL::set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
33 33
   const uint16_t duty = invert ? v_size - v : v;
34 34
   if (PWM_PIN(pin)) {
35 35
     const PinName pin_name = digitalPinToPinName(pin);
@@ -61,7 +61,7 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255
61 61
   }
62 62
 }
63 63
 
64
-void set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {
64
+void MarlinHAL::set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {
65 65
   if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
66 66
   const PinName pin_name = digitalPinToPinName(pin);
67 67
   TIM_TypeDef * const Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM); // Get HAL timer instance

+ 5
- 1
Marlin/src/HAL/STM32/pinsDebug.h Vedi File

@@ -115,7 +115,6 @@ const XrefInfo pin_xref[] PROGMEM = {
115 115
 #define PRINT_PIN_ANALOG(p) do{ sprintf_P(buffer, PSTR(" (A%2d)  "), DIGITAL_PIN_TO_ANALOG_PIN(pin)); SERIAL_ECHO(buffer); }while(0)
116 116
 #define PRINT_PORT(ANUM) port_print(ANUM)
117 117
 #define DIGITAL_PIN_TO_ANALOG_PIN(ANUM) -1  // will report analog pin number in the print port routine
118
-#define GET_PIN_MAP_PIN_M43(Index) pin_xref[Index].Ard_num
119 118
 
120 119
 // x is a variable used to search pin_array
121 120
 #define GET_ARRAY_IS_DIGITAL(x) ((bool) pin_array[x].is_digital)
@@ -123,6 +122,11 @@ const XrefInfo pin_xref[] PROGMEM = {
123 122
 #define PRINT_ARRAY_NAME(x) do{ sprintf_P(buffer, PSTR("%-" STRINGIFY(MAX_NAME_LENGTH) "s"), pin_array[x].name); SERIAL_ECHO(buffer); }while(0)
124 123
 #define MULTI_NAME_PAD 33 // space needed to be pretty if not first name assigned to a pin
125 124
 
125
+//
126
+// Pin Mapping for M43
127
+//
128
+#define GET_PIN_MAP_PIN_M43(Index) pin_xref[Index].Ard_num
129
+
126 130
 #ifndef M43_NEVER_TOUCH
127 131
   #define _M43_NEVER_TOUCH(Index) (Index >= 9 && Index <= 12) // SERIAL/USB pins: PA9(TX) PA10(RX) PA11(USB_DM) PA12(USB_DP)
128 132
   #ifdef KILL_PIN

+ 2
- 2
Marlin/src/HAL/STM32/timers.h Vedi File

@@ -116,5 +116,5 @@ FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const ha
116 116
   }
117 117
 }
118 118
 
119
-#define HAL_timer_isr_prologue(T)
120
-#define HAL_timer_isr_epilogue(T)
119
+#define HAL_timer_isr_prologue(T) NOOP
120
+#define HAL_timer_isr_epilogue(T) NOOP

+ 88
- 97
Marlin/src/HAL/STM32F1/HAL.cpp Vedi File

@@ -79,7 +79,7 @@
79 79
 #define SCB_AIRCR_PRIGROUP_Msk             (7UL << SCB_AIRCR_PRIGROUP_Pos)                /*!< SCB AIRCR: PRIGROUP Mask */
80 80
 
81 81
 // ------------------------
82
-// Public Variables
82
+// Serial ports
83 83
 // ------------------------
84 84
 
85 85
 #if defined(SERIAL_USB) && !HAS_SD_HOST_DRIVE
@@ -112,74 +112,37 @@
112 112
   #endif
113 113
 #endif
114 114
 
115
-uint16_t HAL_adc_result;
116
-
117 115
 // ------------------------
118
-// Private Variables
116
+// ADC
119 117
 // ------------------------
120
-STM32ADC adc(ADC1);
121
-
122
-const uint8_t adc_pins[] = {
123
-  OPTITEM(HAS_TEMP_ADC_0, TEMP_0_PIN)
124
-  OPTITEM(HAS_TEMP_ADC_1, TEMP_1_PIN)
125
-  OPTITEM(HAS_TEMP_ADC_2, TEMP_2_PIN)
126
-  OPTITEM(HAS_TEMP_ADC_3, TEMP_3_PIN)
127
-  OPTITEM(HAS_TEMP_ADC_4, TEMP_4_PIN)
128
-  OPTITEM(HAS_TEMP_ADC_5, TEMP_5_PIN)
129
-  OPTITEM(HAS_TEMP_ADC_6, TEMP_6_PIN)
130
-  OPTITEM(HAS_TEMP_ADC_7, TEMP_7_PIN)
131
-  OPTITEM(HAS_HEATED_BED, TEMP_BED_PIN)
132
-  OPTITEM(HAS_TEMP_CHAMBER, TEMP_CHAMBER_PIN)
133
-  OPTITEM(HAS_TEMP_ADC_PROBE, TEMP_PROBE_PIN)
134
-  OPTITEM(HAS_TEMP_COOLER, TEMP_COOLER_PIN)
135
-  OPTITEM(HAS_TEMP_BOARD, TEMP_BOARD_PIN)
136
-  OPTITEM(FILAMENT_WIDTH_SENSOR, FILWIDTH_PIN)
137
-  OPTITEM(HAS_ADC_BUTTONS, ADC_KEYPAD_PIN)
138
-  OPTITEM(HAS_JOY_ADC_X, JOY_X_PIN)
139
-  OPTITEM(HAS_JOY_ADC_Y, JOY_Y_PIN)
140
-  OPTITEM(HAS_JOY_ADC_Z, JOY_Z_PIN)
141
-  OPTITEM(POWER_MONITOR_CURRENT, POWER_MONITOR_CURRENT_PIN)
142
-  OPTITEM(POWER_MONITOR_VOLTAGE, POWER_MONITOR_VOLTAGE_PIN)
143
-};
144 118
 
145
-enum TempPinIndex : char {
146
-  OPTITEM(HAS_TEMP_ADC_0, TEMP_0)
147
-  OPTITEM(HAS_TEMP_ADC_1, TEMP_1)
148
-  OPTITEM(HAS_TEMP_ADC_2, TEMP_2)
149
-  OPTITEM(HAS_TEMP_ADC_3, TEMP_3)
150
-  OPTITEM(HAS_TEMP_ADC_4, TEMP_4)
151
-  OPTITEM(HAS_TEMP_ADC_5, TEMP_5)
152
-  OPTITEM(HAS_TEMP_ADC_6, TEMP_6)
153
-  OPTITEM(HAS_TEMP_ADC_7, TEMP_7)
154
-  OPTITEM(HAS_HEATED_BED, TEMP_BED)
155
-  OPTITEM(HAS_TEMP_CHAMBER, TEMP_CHAMBER)
156
-  OPTITEM(HAS_TEMP_ADC_PROBE, TEMP_PROBE)
157
-  OPTITEM(HAS_TEMP_COOLER, TEMP_COOLER)
158
-  OPTITEM(HAS_TEMP_BOARD, TEMP_BOARD)
159
-  OPTITEM(FILAMENT_WIDTH_SENSOR, FILWIDTH)
160
-  OPTITEM(HAS_ADC_BUTTONS, ADC_KEY)
161
-  OPTITEM(HAS_JOY_ADC_X, JOY_X)
162
-  OPTITEM(HAS_JOY_ADC_Y, JOY_Y)
163
-  OPTITEM(HAS_JOY_ADC_Z, JOY_Z)
164
-  OPTITEM(POWER_MONITOR_CURRENT, POWERMON_CURRENT)
165
-  OPTITEM(POWER_MONITOR_VOLTAGE, POWERMON_VOLTS)
166
-  ADC_PIN_COUNT
167
-};
119
+// Watch out for recursion here! Our pin_t is signed, so pass through to Arduino -> analogRead(uint8_t)
120
+
121
+uint16_t analogRead(const pin_t pin) {
122
+  const bool is_analog = _GET_MODE(pin) == GPIO_INPUT_ANALOG;
123
+  return is_analog ? analogRead(uint8_t(pin)) : 0;
124
+}
125
+
126
+// Wrapper to maple unprotected analogWrite
127
+void analogWrite(const pin_t pin, int pwm_val8) {
128
+  if (PWM_PIN(pin)) analogWrite(uint8_t(pin), pwm_val8);
129
+}
168 130
 
169
-uint16_t HAL_adc_results[ADC_PIN_COUNT];
131
+uint16_t MarlinHAL::adc_result;
170 132
 
171 133
 // ------------------------
172 134
 // Private functions
173 135
 // ------------------------
136
+
174 137
 static void NVIC_SetPriorityGrouping(uint32_t PriorityGroup) {
175 138
   uint32_t reg_value;
176
-  uint32_t PriorityGroupTmp = (PriorityGroup & (uint32_t)0x07);               /* only values 0..7 are used          */
139
+  uint32_t PriorityGroupTmp = (PriorityGroup & (uint32_t)0x07);               // only values 0..7 are used
177 140
 
178
-  reg_value  =  SCB->AIRCR;                                                   /* read old register configuration    */
179
-  reg_value &= ~(SCB_AIRCR_VECTKEY_Msk | SCB_AIRCR_PRIGROUP_Msk);             /* clear bits to change               */
141
+  reg_value  =  SCB->AIRCR;                                                   // read old register configuration
142
+  reg_value &= ~(SCB_AIRCR_VECTKEY_Msk | SCB_AIRCR_PRIGROUP_Msk);             // clear bits to change
180 143
   reg_value  =  (reg_value                                 |
181 144
                 ((uint32_t)0x5FA << SCB_AIRCR_VECTKEY_Pos) |
182
-                (PriorityGroupTmp << 8));                                     /* Insert write key & priority group  */
145
+                (PriorityGroupTmp << 8));                                     // Insert write key & priority group
183 146
   SCB->AIRCR =  reg_value;
184 147
 }
185 148
 
@@ -187,6 +150,8 @@ static void NVIC_SetPriorityGrouping(uint32_t PriorityGroup) {
187 150
 // Public functions
188 151
 // ------------------------
189 152
 
153
+void flashFirmware(const int16_t) { hal.reboot(); }
154
+
190 155
 //
191 156
 // Leave PA11/PA12 intact if USBSerial is not used
192 157
 //
@@ -206,7 +171,11 @@ static void NVIC_SetPriorityGrouping(uint32_t PriorityGroup) {
206 171
 
207 172
 TERN_(POSTMORTEM_DEBUGGING, extern void install_min_serial());
208 173
 
209
-void HAL_init() {
174
+// ------------------------
175
+// MarlinHAL class
176
+// ------------------------
177
+
178
+void MarlinHAL::init() {
210 179
   NVIC_SetPriorityGrouping(0x3);
211 180
   #if PIN_EXISTS(LED)
212 181
     OUT_WRITE(LED_PIN, LOW);
@@ -225,7 +194,7 @@ void HAL_init() {
225 194
 }
226 195
 
227 196
 // HAL idle task
228
-void HAL_idletask() {
197
+void MarlinHAL::idletask() {
229 198
   #if HAS_SHARED_MEDIA
230 199
     // If Marlin is using the SD card we need to lock it to prevent access from
231 200
     // a PC via USB.
@@ -240,14 +209,7 @@ void HAL_idletask() {
240 209
   #endif
241 210
 }
242 211
 
243
-void HAL_clear_reset_source() { }
244
-
245
-/**
246
- * TODO: Check this and change or remove.
247
- */
248
-uint8_t HAL_get_reset_source() { return RST_POWER_ON; }
249
-
250
-void _delay_ms(const int delay_ms) { delay(delay_ms); }
212
+void MarlinHAL::reboot() { nvic_sys_reset(); }
251 213
 
252 214
 extern "C" {
253 215
   extern unsigned int _ebss; // end of bss section
@@ -281,31 +243,76 @@ extern "C" {
281 243
 }
282 244
 */
283 245
 
284
-// ------------------------
246
+//
285 247
 // ADC
286
-// ------------------------
248
+//
249
+
250
+enum ADCIndex : uint8_t {
251
+  OPTITEM(HAS_TEMP_ADC_0, TEMP_0)
252
+  OPTITEM(HAS_TEMP_ADC_1, TEMP_1)
253
+  OPTITEM(HAS_TEMP_ADC_2, TEMP_2)
254
+  OPTITEM(HAS_TEMP_ADC_3, TEMP_3)
255
+  OPTITEM(HAS_TEMP_ADC_4, TEMP_4)
256
+  OPTITEM(HAS_TEMP_ADC_5, TEMP_5)
257
+  OPTITEM(HAS_TEMP_ADC_6, TEMP_6)
258
+  OPTITEM(HAS_TEMP_ADC_7, TEMP_7)
259
+  OPTITEM(HAS_HEATED_BED, TEMP_BED)
260
+  OPTITEM(HAS_TEMP_CHAMBER, TEMP_CHAMBER)
261
+  OPTITEM(HAS_TEMP_ADC_PROBE, TEMP_PROBE)
262
+  OPTITEM(HAS_TEMP_COOLER, TEMP_COOLER)
263
+  OPTITEM(HAS_TEMP_BOARD, TEMP_BOARD)
264
+  OPTITEM(FILAMENT_WIDTH_SENSOR, FILWIDTH)
265
+  OPTITEM(HAS_ADC_BUTTONS, ADC_KEY)
266
+  OPTITEM(HAS_JOY_ADC_X, JOY_X)
267
+  OPTITEM(HAS_JOY_ADC_Y, JOY_Y)
268
+  OPTITEM(HAS_JOY_ADC_Z, JOY_Z)
269
+  OPTITEM(POWER_MONITOR_CURRENT, POWERMON_CURRENT)
270
+  OPTITEM(POWER_MONITOR_VOLTAGE, POWERMON_VOLTS)
271
+  ADC_COUNT
272
+};
273
+
274
+static uint16_t adc_results[ADC_COUNT];
275
+
287 276
 // Init the AD in continuous capture mode
288
-void HAL_adc_init() {
277
+void MarlinHAL::adc_init() {
278
+  static const uint8_t adc_pins[] = {
279
+    OPTITEM(HAS_TEMP_ADC_0, TEMP_0_PIN)
280
+    OPTITEM(HAS_TEMP_ADC_1, TEMP_1_PIN)
281
+    OPTITEM(HAS_TEMP_ADC_2, TEMP_2_PIN)
282
+    OPTITEM(HAS_TEMP_ADC_3, TEMP_3_PIN)
283
+    OPTITEM(HAS_TEMP_ADC_4, TEMP_4_PIN)
284
+    OPTITEM(HAS_TEMP_ADC_5, TEMP_5_PIN)
285
+    OPTITEM(HAS_TEMP_ADC_6, TEMP_6_PIN)
286
+    OPTITEM(HAS_TEMP_ADC_7, TEMP_7_PIN)
287
+    OPTITEM(HAS_HEATED_BED, TEMP_BED_PIN)
288
+    OPTITEM(HAS_TEMP_CHAMBER, TEMP_CHAMBER_PIN)
289
+    OPTITEM(HAS_TEMP_ADC_PROBE, TEMP_PROBE_PIN)
290
+    OPTITEM(HAS_TEMP_COOLER, TEMP_COOLER_PIN)
291
+    OPTITEM(HAS_TEMP_BOARD, TEMP_BOARD_PIN)
292
+    OPTITEM(FILAMENT_WIDTH_SENSOR, FILWIDTH_PIN)
293
+    OPTITEM(HAS_ADC_BUTTONS, ADC_KEYPAD_PIN)
294
+    OPTITEM(HAS_JOY_ADC_X, JOY_X_PIN)
295
+    OPTITEM(HAS_JOY_ADC_Y, JOY_Y_PIN)
296
+    OPTITEM(HAS_JOY_ADC_Z, JOY_Z_PIN)
297
+    OPTITEM(POWER_MONITOR_CURRENT, POWER_MONITOR_CURRENT_PIN)
298
+    OPTITEM(POWER_MONITOR_VOLTAGE, POWER_MONITOR_VOLTAGE_PIN)
299
+  };
300
+  static STM32ADC adc(ADC1);
289 301
   // configure the ADC
290 302
   adc.calibrate();
291
-  #if F_CPU > 72000000
292
-    adc.setSampleRate(ADC_SMPR_71_5); // 71.5 ADC cycles
293
-  #else
294
-    adc.setSampleRate(ADC_SMPR_41_5); // 41.5 ADC cycles
295
-  #endif
296
-  adc.setPins((uint8_t *)adc_pins, ADC_PIN_COUNT);
297
-  adc.setDMA(HAL_adc_results, (uint16_t)ADC_PIN_COUNT, (uint32_t)(DMA_MINC_MODE | DMA_CIRC_MODE), nullptr);
303
+  adc.setSampleRate((F_CPU > 72000000) ? ADC_SMPR_71_5 : ADC_SMPR_41_5); // 71.5 or 41.5 ADC cycles
304
+  adc.setPins((uint8_t *)adc_pins, ADC_COUNT);
305
+  adc.setDMA(adc_results, uint16_t(ADC_COUNT), uint32_t(DMA_MINC_MODE | DMA_CIRC_MODE), nullptr);
298 306
   adc.setScanMode();
299 307
   adc.setContinuous();
300 308
   adc.startConversion();
301 309
 }
302 310
 
303
-void HAL_adc_start_conversion(const uint8_t adc_pin) {
311
+void MarlinHAL::adc_start(const pin_t pin) {
304 312
   #define __TCASE(N,I) case N: pin_index = I; break;
305 313
   #define _TCASE(C,N,I) TERN_(C, __TCASE(N, I))
306
-  //TEMP_PINS pin_index;
307
-  TempPinIndex pin_index;
308
-  switch (adc_pin) {
314
+  ADCIndex pin_index;
315
+  switch (pin) {
309 316
     default: return;
310 317
     _TCASE(HAS_TEMP_ADC_0,        TEMP_0_PIN,                TEMP_0)
311 318
     _TCASE(HAS_TEMP_ADC_1,        TEMP_1_PIN,                TEMP_1)
@@ -328,23 +335,7 @@ void HAL_adc_start_conversion(const uint8_t adc_pin) {
328 335
     _TCASE(POWER_MONITOR_CURRENT, POWER_MONITOR_CURRENT_PIN, POWERMON_CURRENT)
329 336
     _TCASE(POWER_MONITOR_VOLTAGE, POWER_MONITOR_VOLTAGE_PIN, POWERMON_VOLTS)
330 337
   }
331
-  HAL_adc_result = HAL_adc_results[(int)pin_index] >> (12 - HAL_ADC_RESOLUTION); // shift out unused bits
332
-}
333
-
334
-uint16_t HAL_adc_get_result() { return HAL_adc_result; }
335
-
336
-uint16_t analogRead(pin_t pin) {
337
-  const bool is_analog = _GET_MODE(pin) == GPIO_INPUT_ANALOG;
338
-  return is_analog ? analogRead(uint8_t(pin)) : 0;
339
-}
340
-
341
-// Wrapper to maple unprotected analogWrite
342
-void analogWrite(pin_t pin, int pwm_val8) {
343
-  if (PWM_PIN(pin)) analogWrite(uint8_t(pin), pwm_val8);
338
+  adc_result = adc_results[(int)pin_index] >> (12 - HAL_ADC_RESOLUTION); // shift out unused bits
344 339
 }
345 340
 
346
-void HAL_reboot() { nvic_sys_reset(); }
347
-
348
-void flashFirmware(const int16_t) { HAL_reboot(); }
349
-
350 341
 #endif // __STM32F1__

+ 100
- 80
Marlin/src/HAL/STM32F1/HAL.h Vedi File

@@ -66,6 +66,10 @@
66 66
   #endif
67 67
 #endif
68 68
 
69
+// ------------------------
70
+// Serial ports
71
+// ------------------------
72
+
69 73
 #ifdef SERIAL_USB
70 74
   typedef ForwardSerial1Class< USBSerial > DefaultSerial1;
71 75
   extern DefaultSerial1 MSerial0;
@@ -141,11 +145,6 @@
141 145
   #endif
142 146
 #endif
143 147
 
144
-// Set interrupt grouping for this MCU
145
-void HAL_init();
146
-#define HAL_IDLETASK 1
147
-void HAL_idletask();
148
-
149 148
 /**
150 149
  * TODO: review this to return 1 for pins that are not analog input
151 150
  */
@@ -158,15 +157,7 @@ void HAL_idletask();
158 157
   #define NO_COMPILE_TIME_PWM
159 158
 #endif
160 159
 
161
-#define CRITICAL_SECTION_START()  uint32_t primask = __get_primask(); (void)__iCliRetVal()
162
-#define CRITICAL_SECTION_END()    if (!primask) (void)__iSeiRetVal()
163
-#define ISRS_ENABLED() (!__get_primask())
164
-#define ENABLE_ISRS()  ((void)__iSeiRetVal())
165
-#define DISABLE_ISRS() ((void)__iCliRetVal())
166
-
167
-// On AVR this is in math.h?
168
-#define square(x) ((x)*(x))
169
-
160
+// Reset Reason
170 161
 #define RST_POWER_ON   1
171 162
 #define RST_EXTERNAL   2
172 163
 #define RST_BROWN_OUT  4
@@ -182,46 +173,63 @@ void HAL_idletask();
182 173
 typedef int8_t pin_t;
183 174
 
184 175
 // ------------------------
185
-// Public Variables
176
+// Interrupts
186 177
 // ------------------------
187 178
 
188
-// Result of last ADC conversion
189
-extern uint16_t HAL_adc_result;
179
+#define CRITICAL_SECTION_START()  const bool irqon = !__get_primask(); (void)__iCliRetVal()
180
+#define CRITICAL_SECTION_END()    if (!primask) (void)__iSeiRetVal()
181
+#define cli() noInterrupts()
182
+#define sei() interrupts()
190 183
 
191 184
 // ------------------------
192
-// Public functions
185
+// ADC
193 186
 // ------------------------
194 187
 
195
-// Disable interrupts
196
-#define cli() noInterrupts()
188
+#ifdef ADC_RESOLUTION
189
+  #define HAL_ADC_RESOLUTION ADC_RESOLUTION
190
+#else
191
+  #define HAL_ADC_RESOLUTION 12
192
+#endif
197 193
 
198
-// Enable interrupts
199
-#define sei() interrupts()
194
+#define HAL_ADC_VREF         3.3
200 195
 
201
-// Memory related
202
-#define __bss_end __bss_end__
196
+uint16_t analogRead(const pin_t pin); // need hal.adc_enable() first
197
+void analogWrite(const pin_t pin, int pwm_val8); // PWM only! mul by 257 in maple!?
203 198
 
204
-// Clear reset reason
205
-void HAL_clear_reset_source();
199
+//
200
+// Pin Mapping for M42, M43, M226
201
+//
202
+#define GET_PIN_MAP_PIN(index) index
203
+#define GET_PIN_MAP_INDEX(pin) pin
204
+#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
206 205
 
207
-// Reset reason
208
-uint8_t HAL_get_reset_source();
206
+#define JTAG_DISABLE()    afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY)
207
+#define JTAGSWD_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_NONE)
209 208
 
210
-void HAL_reboot();
209
+#define PLATFORM_M997_SUPPORT
210
+void flashFirmware(const int16_t);
211 211
 
212
-void _delay_ms(const int delay);
212
+#define HAL_CAN_SET_PWM_FREQ      // This HAL supports PWM Frequency adjustment
213
+#ifndef PWM_FREQUENCY
214
+  #define PWM_FREQUENCY      1000 // Default PWM Frequency
215
+#endif
213 216
 
214
-#pragma GCC diagnostic push
215
-#pragma GCC diagnostic ignored "-Wunused-function"
217
+// ------------------------
218
+// Class Utilities
219
+// ------------------------
216 220
 
217
-/*
218
-extern "C" {
219
-  int freeMemory();
220
-}
221
-*/
221
+// Memory related
222
+#define __bss_end __bss_end__
223
+
224
+void _delay_ms(const int ms);
222 225
 
223 226
 extern "C" char* _sbrk(int incr);
224 227
 
228
+#pragma GCC diagnostic push
229
+#if GCC_VERSION <= 50000
230
+  #pragma GCC diagnostic ignored "-Wunused-function"
231
+#endif
232
+
225 233
 static inline int freeMemory() {
226 234
   volatile char top;
227 235
   return &top - _sbrk(0);
@@ -229,58 +237,70 @@ static inline int freeMemory() {
229 237
 
230 238
 #pragma GCC diagnostic pop
231 239
 
232
-//
233
-// ADC
234
-//
240
+// ------------------------
241
+// MarlinHAL Class
242
+// ------------------------
235 243
 
236
-#define HAL_ANALOG_SELECT(pin) pinMode(pin, INPUT_ANALOG);
244
+class MarlinHAL {
245
+public:
237 246
 
238
-void HAL_adc_init();
247
+  // Earliest possible init, before setup()
248
+  MarlinHAL() {}
239 249
 
240
-#ifdef ADC_RESOLUTION
241
-  #define HAL_ADC_RESOLUTION ADC_RESOLUTION
242
-#else
243
-  #define HAL_ADC_RESOLUTION 12
244
-#endif
250
+  static void init();          // Called early in setup()
251
+  static void init_board() {}  // Called less early in setup()
252
+  static void reboot();        // Restart the firmware from 0x0
245 253
 
246
-#define HAL_ADC_VREF         3.3
247
-#define HAL_START_ADC(pin)  HAL_adc_start_conversion(pin)
248
-#define HAL_READ_ADC()      HAL_adc_result
249
-#define HAL_ADC_READY()     true
254
+  // Interrupts
255
+  static bool isr_state() { return !__get_primask(); }
256
+  static void isr_on()  { ((void)__iSeiRetVal()); }
257
+  static void isr_off() { ((void)__iCliRetVal()); }
250 258
 
251
-void HAL_adc_start_conversion(const uint8_t adc_pin);
252
-uint16_t HAL_adc_get_result();
259
+  static void delay_ms(const int ms) { delay(ms); }
253 260
 
254
-uint16_t analogRead(pin_t pin); // need HAL_ANALOG_SELECT() first
255
-void analogWrite(pin_t pin, int pwm_val8); // PWM only! mul by 257 in maple!?
261
+  // Tasks, called from idle()
262
+  static void idletask();
256 263
 
257
-#define GET_PIN_MAP_PIN(index) index
258
-#define GET_PIN_MAP_INDEX(pin) pin
259
-#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
264
+  // Reset
265
+  static uint8_t get_reset_source() { return RST_POWER_ON; }
266
+  static void clear_reset_source() {}
260 267
 
261
-#define JTAG_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY)
262
-#define JTAGSWD_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_NONE)
268
+  // Free SRAM
269
+  static int freeMemory() { return ::freeMemory(); }
263 270
 
264
-#define PLATFORM_M997_SUPPORT
265
-void flashFirmware(const int16_t);
271
+  //
272
+  // ADC Methods
273
+  //
266 274
 
267
-#ifndef PWM_FREQUENCY
268
-  #define PWM_FREQUENCY      1000 // Default PWM Frequency
269
-#endif
270
-#define HAL_CAN_SET_PWM_FREQ      // This HAL supports PWM Frequency adjustment
275
+  static uint16_t adc_result;
271 276
 
272
-/**
273
- * set_pwm_frequency
274
- *  Set the frequency of the timer corresponding to the provided pin
275
- *  All Timer PWM pins run at the same frequency
276
- */
277
-void set_pwm_frequency(const pin_t pin, const uint16_t f_desired);
277
+  // Called by Temperature::init once at startup
278
+  static void adc_init();
278 279
 
279
-/**
280
- * set_pwm_duty
281
- *  Set the PWM duty cycle of the provided pin to the provided value
282
- *  Optionally allows inverting the duty cycle [default = false]
283
- *  Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
284
- *  The timer must be pre-configured with set_pwm_frequency() if the default frequency is not desired.
285
- */
286
-void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);
280
+  // Called by Temperature::init for each sensor at startup
281
+  static void adc_enable(const pin_t pin) { pinMode(pin, INPUT_ANALOG); }
282
+
283
+  // Begin ADC sampling on the given channel
284
+  static void adc_start(const pin_t pin);
285
+
286
+  // Is the ADC ready for reading?
287
+  static bool adc_ready() { return true; }
288
+
289
+  // The current value of the ADC register
290
+  static uint16_t adc_value() { return adc_result; }
291
+
292
+  /**
293
+   * Set the PWM duty cycle for the pin to the given value.
294
+   * Optionally invert the duty cycle [default = false]
295
+   * Optionally change the maximum size of the provided value to enable finer PWM duty control [default = 255]
296
+   * The timer must be pre-configured with set_pwm_frequency() if the default frequency is not desired.
297
+   */
298
+  static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false);
299
+
300
+  /**
301
+   * Set the frequency of the timer for the given pin.
302
+   * All Timer PWM pins run at the same frequency.
303
+   */
304
+  static void set_pwm_frequency(const pin_t pin, const uint16_t f_desired);
305
+
306
+};

+ 8
- 0
Marlin/src/HAL/STM32F1/SPI.cpp Vedi File

@@ -92,6 +92,14 @@ static const spi_pins board_spi_pins[] __FLASH__ = {
92 92
 #endif
93 93
 
94 94
 /**
95
+ * @brief Wait until TXE (tx empty) flag is set and BSY (busy) flag unset.
96
+ */
97
+static inline void waitSpiTxEnd(spi_dev *spi_d) {
98
+  while (spi_is_tx_empty(spi_d) == 0) { /* nada */ } // wait until TXE=1
99
+  while (spi_is_busy(spi_d) != 0) { /* nada */ }     // wait until BSY=0
100
+}
101
+
102
+/**
95 103
  * Constructor
96 104
  */
97 105
 SPIClass::SPIClass(uint32_t spi_num) {

+ 0
- 8
Marlin/src/HAL/STM32F1/SPI.h Vedi File

@@ -414,12 +414,4 @@ private:
414 414
   */
415 415
 };
416 416
 
417
-/**
418
- * @brief Wait until TXE (tx empty) flag is set and BSY (busy) flag unset.
419
- */
420
-static void waitSpiTxEnd(spi_dev *spi_d) {
421
-  while (spi_is_tx_empty(spi_d) == 0) { /* nada */ } // wait until TXE=1
422
-  while (spi_is_busy(spi_d) != 0) { /* nada */ }     // wait until BSY=0
423
-}
424
-
425 417
 extern SPIClass SPI;

+ 2
- 1
Marlin/src/HAL/STM32F1/Servo.h Vedi File

@@ -35,7 +35,8 @@
35 35
 #define SERVO_DEFAULT_MIN_ANGLE         0
36 36
 #define SERVO_DEFAULT_MAX_ANGLE         180
37 37
 
38
-#define HAL_SERVO_LIB libServo
38
+class libServo;
39
+typedef libServo hal_servo_t;
39 40
 
40 41
 class libServo {
41 42
   public:

+ 3
- 5
Marlin/src/HAL/STM32F1/fast_pwm.cpp Vedi File

@@ -21,11 +21,9 @@
21 21
  */
22 22
 #ifdef __STM32F1__
23 23
 
24
-#include "../../inc/MarlinConfigPre.h"
24
+#include "../../inc/MarlinConfig.h"
25 25
 
26 26
 #include <pwm.h>
27
-#include "HAL.h"
28
-#include "timers.h"
29 27
 
30 28
 #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 29
 
@@ -38,7 +36,7 @@ inline uint8_t timer_and_index_for_pin(const pin_t pin, timer_dev **timer_ptr) {
38 36
   return 0;
39 37
 }
40 38
 
41
-void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
39
+void MarlinHAL::set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
42 40
   const uint16_t duty = invert ? v_size - v : v;
43 41
   if (PWM_PIN(pin)) {
44 42
     timer_dev *timer; UNUSED(timer);
@@ -54,7 +52,7 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255
54 52
   }
55 53
 }
56 54
 
57
-void set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {
55
+void MarlinHAL::set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {
58 56
   if (!PWM_PIN(pin)) return;                    // Don't proceed if no hardware timer
59 57
 
60 58
   timer_dev *timer; UNUSED(timer);

+ 1
- 1
Marlin/src/HAL/STM32F1/timers.h Vedi File

@@ -188,7 +188,7 @@ FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
188 188
   }
189 189
 }
190 190
 
191
-#define HAL_timer_isr_epilogue(T)
191
+#define HAL_timer_isr_epilogue(T) NOOP
192 192
 
193 193
 // No command is available in framework to turn off ARPE bit, which is turned on by default in libmaple.
194 194
 // Needed here to reset ARPE=0 for stepper timer

+ 41
- 38
Marlin/src/HAL/TEENSY31_32/HAL.cpp Vedi File

@@ -31,6 +31,10 @@
31 31
 
32 32
 #include <Wire.h>
33 33
 
34
+// ------------------------
35
+// Serial ports
36
+// ------------------------
37
+
34 38
 #define _IMPLEMENT_SERIAL(X) DefaultSerial##X MSerial##X(false, Serial##X)
35 39
 #define IMPLEMENT_SERIAL(X)  _IMPLEMENT_SERIAL(X)
36 40
 #if WITHIN(SERIAL_PORT, 0, 3)
@@ -40,33 +44,32 @@
40 44
 #endif
41 45
 USBSerialType USBSerial(false, SerialUSB);
42 46
 
43
-uint16_t HAL_adc_result;
44
-
45
-static const uint8_t pin2sc1a[] = {
46
-    5, 14, 8, 9, 13, 12, 6, 7, 15, 4, 0, 19, 3, 31, // 0-13, we treat them as A0-A13
47
-    5, 14, 8, 9, 13, 12, 6, 7, 15, 4, // 14-23 (A0-A9)
48
-    31, 31, 31, 31, 31, 31, 31, 31, 31, 31, // 24-33
49
-    0+64, 19+64, 3+64, 31+64, // 34-37 (A10-A13)
50
-    26, 22, 23, 27, 29, 30 // 38-43: temp. sensor, VREF_OUT, A14, bandgap, VREFH, VREFL. A14 isn't connected to anything in Teensy 3.0.
51
-};
52
-
53
-/*
54
-  // disable interrupts
55
-  void cli() { noInterrupts(); }
47
+// ------------------------
48
+// Class Utilities
49
+// ------------------------
56 50
 
57
-  // enable interrupts
58
-  void sei() { interrupts(); }
59
-*/
51
+extern "C" {
52
+  extern char __bss_end;
53
+  extern char __heap_start;
54
+  extern void* __brkval;
60 55
 
61
-void HAL_adc_init() {
62
-  analog_init();
63
-  while (ADC0_SC3 & ADC_SC3_CAL) {}; // Wait for calibration to finish
64
-  NVIC_ENABLE_IRQ(IRQ_FTM1);
56
+  int freeMemory() {
57
+    int free_memory;
58
+    if ((int)__brkval == 0)
59
+      free_memory = ((int)&free_memory) - ((int)&__bss_end);
60
+    else
61
+      free_memory = ((int)&free_memory) - ((int)__brkval);
62
+    return free_memory;
63
+  }
65 64
 }
66 65
 
67
-void HAL_clear_reset_source() { }
66
+// ------------------------
67
+// MarlinHAL Class
68
+// ------------------------
68 69
 
69
-uint8_t HAL_get_reset_source() {
70
+void MarlinHAL::reboot() { _reboot_Teensyduino_(); }
71
+
72
+uint8_t MarlinHAL::get_reset_source() {
70 73
   switch (RCM_SRS0) {
71 74
     case 128: return RST_POWER_ON; break;
72 75
     case 64: return RST_EXTERNAL; break;
@@ -78,25 +81,25 @@ uint8_t HAL_get_reset_source() {
78 81
   return 0;
79 82
 }
80 83
 
81
-void HAL_reboot() { _reboot_Teensyduino_(); }
84
+// ADC
82 85
 
83
-extern "C" {
84
-  extern char __bss_end;
85
-  extern char __heap_start;
86
-  extern void* __brkval;
87
-
88
-  int freeMemory() {
89
-    int free_memory;
90
-    if ((int)__brkval == 0)
91
-      free_memory = ((int)&free_memory) - ((int)&__bss_end);
92
-    else
93
-      free_memory = ((int)&free_memory) - ((int)__brkval);
94
-    return free_memory;
95
-  }
86
+void MarlinHAL::adc_init() {
87
+  analog_init();
88
+  while (ADC0_SC3 & ADC_SC3_CAL) {}; // Wait for calibration to finish
89
+  NVIC_ENABLE_IRQ(IRQ_FTM1);
96 90
 }
97 91
 
98
-void HAL_adc_start_conversion(const uint8_t adc_pin) { ADC0_SC1A = pin2sc1a[adc_pin]; }
92
+void MarlinHAL::adc_start(const pin_t pin) {
93
+  static const uint8_t pin2sc1a[] = {
94
+      5, 14, 8, 9, 13, 12, 6, 7, 15, 4, 0, 19, 3, 31, // 0-13, we treat them as A0-A13
95
+      5, 14, 8, 9, 13, 12, 6, 7, 15, 4, // 14-23 (A0-A9)
96
+      31, 31, 31, 31, 31, 31, 31, 31, 31, 31, // 24-33
97
+      0+64, 19+64, 3+64, 31+64, // 34-37 (A10-A13)
98
+      26, 22, 23, 27, 29, 30 // 38-43: temp. sensor, VREF_OUT, A14, bandgap, VREFH, VREFL. A14 isn't connected to anything in Teensy 3.0.
99
+  };
100
+  ADC0_SC1A = pin2sc1a[pin];
101
+}
99 102
 
100
-uint16_t HAL_adc_get_result() { return ADC0_RA; }
103
+uint16_t MarlinHAL::adc_value() { return ADC0_RA; }
101 104
 
102 105
 #endif // __MK20DX256__

+ 92
- 38
Marlin/src/HAL/TEENSY31_32/HAL.h Vedi File

@@ -36,12 +36,9 @@
36 36
 
37 37
 #include <stdint.h>
38 38
 
39
-#define CPU_ST7920_DELAY_1 600
40
-#define CPU_ST7920_DELAY_2 750
41
-#define CPU_ST7920_DELAY_3 750
42
-
43
-//#undef MOTHERBOARD
44
-//#define MOTHERBOARD BOARD_TEENSY31_32
39
+// ------------------------
40
+// Defines
41
+// ------------------------
45 42
 
46 43
 #define IS_32BIT_TEENSY 1
47 44
 #define IS_TEENSY_31_32 1
@@ -49,6 +46,14 @@
49 46
   #define IS_TEENSY32 1
50 47
 #endif
51 48
 
49
+#define CPU_ST7920_DELAY_1 600
50
+#define CPU_ST7920_DELAY_2 750
51
+#define CPU_ST7920_DELAY_3 750
52
+
53
+// ------------------------
54
+// Serial ports
55
+// ------------------------
56
+
52 57
 #include "../../core/serial_hook.h"
53 58
 
54 59
 #define Serial0 Serial
@@ -72,31 +77,44 @@ extern USBSerialType USBSerial;
72 77
   #error "The required SERIAL_PORT must be from 0 to 3, or -1 for Native USB."
73 78
 #endif
74 79
 
75
-#define HAL_SERVO_LIB libServo
80
+// ------------------------
81
+// Types
82
+// ------------------------
83
+
84
+class libServo;
85
+typedef libServo hal_servo_t;
76 86
 
77 87
 typedef int8_t pin_t;
78 88
 
79
-#ifndef analogInputToDigitalPin
80
-  #define analogInputToDigitalPin(p) ((p < 12U) ? (p) + 54U : -1)
81
-#endif
89
+// ------------------------
90
+// Interrupts
91
+// ------------------------
82 92
 
83
-#define CRITICAL_SECTION_START()  uint32_t primask = __get_PRIMASK(); __disable_irq()
84
-#define CRITICAL_SECTION_END()    if (!primask) __enable_irq()
85
-#define ISRS_ENABLED() (!__get_PRIMASK())
86
-#define ENABLE_ISRS()  __enable_irq()
87
-#define DISABLE_ISRS() __disable_irq()
93
+uint32_t __get_PRIMASK(void); // CMSIS
94
+#define CRITICAL_SECTION_START()  const bool irqon = !__get_PRIMASK(); __disable_irq()
95
+#define CRITICAL_SECTION_END()    if (irqon) __enable_irq()
88 96
 
89
-inline void HAL_init() {}
97
+// ------------------------
98
+// ADC
99
+// ------------------------
90 100
 
91
-// Clear the reset reason
92
-void HAL_clear_reset_source();
101
+#ifndef analogInputToDigitalPin
102
+  #define analogInputToDigitalPin(p) ((p < 12U) ? (p) + 54U : -1)
103
+#endif
93 104
 
94
-// Get the reason for the reset
95
-uint8_t HAL_get_reset_source();
105
+#define HAL_ADC_VREF         3.3
106
+#define HAL_ADC_RESOLUTION  10
96 107
 
97
-void HAL_reboot();
108
+//
109
+// Pin Mapping for M42, M43, M226
110
+//
111
+#define GET_PIN_MAP_PIN(index) index
112
+#define GET_PIN_MAP_INDEX(pin) pin
113
+#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
98 114
 
99
-FORCE_INLINE void _delay_ms(const int delay_ms) { delay(delay_ms); }
115
+// ------------------------
116
+// Class Utilities
117
+// ------------------------
100 118
 
101 119
 #pragma GCC diagnostic push
102 120
 #if GCC_VERSION <= 50000
@@ -107,27 +125,63 @@ extern "C" int freeMemory();
107 125
 
108 126
 #pragma GCC diagnostic pop
109 127
 
110
-// ADC
128
+// ------------------------
129
+// MarlinHAL Class
130
+// ------------------------
111 131
 
112
-void HAL_adc_init();
132
+class MarlinHAL {
133
+public:
113 134
 
114
-#define HAL_ADC_VREF         3.3
115
-#define HAL_ADC_RESOLUTION  10
116
-#define HAL_START_ADC(pin)  HAL_adc_start_conversion(pin)
117
-#define HAL_READ_ADC()      HAL_adc_get_result()
118
-#define HAL_ADC_READY()     true
135
+  // Earliest possible init, before setup()
136
+  MarlinHAL() {}
119 137
 
120
-#define HAL_ANALOG_SELECT(pin)
138
+  static void init() {}        // Called early in setup()
139
+  static void init_board() {}  // Called less early in setup()
140
+  static void reboot();        // Restart the firmware from 0x0
121 141
 
122
-void HAL_adc_start_conversion(const uint8_t adc_pin);
123
-uint16_t HAL_adc_get_result();
142
+  // Interrupts
143
+  static bool isr_state() { return !__get_PRIMASK(); }
144
+  static void isr_on()  { __enable_irq(); }
145
+  static void isr_off() { __disable_irq(); }
124 146
 
125
-// PWM
147
+  static void delay_ms(const int ms) { delay(ms); }
126 148
 
127
-inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); }
149
+  // Tasks, called from idle()
150
+  static void idletask() {}
128 151
 
129
-// Pin Map
152
+  // Reset
153
+  static uint8_t get_reset_source();
154
+  static void clear_reset_source() {}
130 155
 
131
-#define GET_PIN_MAP_PIN(index) index
132
-#define GET_PIN_MAP_INDEX(pin) pin
133
-#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
156
+  // Free SRAM
157
+  static int freeMemory() { return ::freeMemory(); }
158
+
159
+  //
160
+  // ADC Methods
161
+  //
162
+
163
+  // Called by Temperature::init once at startup
164
+  static void adc_init();
165
+
166
+  // Called by Temperature::init for each sensor at startup
167
+  static void adc_enable(const pin_t ch) {}
168
+
169
+  // Begin ADC sampling on the given channel
170
+  static void adc_start(const pin_t ch);
171
+
172
+  // Is the ADC ready for reading?
173
+  static bool adc_ready() { return true; }
174
+
175
+  // The current value of the ADC register
176
+  static uint16_t adc_value();
177
+
178
+  /**
179
+   * Set the PWM duty cycle for the pin to the given value.
180
+   * No option to invert the duty cycle [default = false]
181
+   * No option to change the scale of the provided value to enable finer PWM duty control [default = 255]
182
+   */
183
+  static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) {
184
+    analogWrite(pin, v);
185
+  }
186
+
187
+};

+ 1
- 1
Marlin/src/HAL/TEENSY31_32/timers.h Vedi File

@@ -110,4 +110,4 @@ void HAL_timer_disable_interrupt(const uint8_t timer_num);
110 110
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
111 111
 
112 112
 void HAL_timer_isr_prologue(const uint8_t timer_num);
113
-#define HAL_timer_isr_epilogue(T)
113
+#define HAL_timer_isr_epilogue(T) NOOP

+ 57
- 53
Marlin/src/HAL/TEENSY35_36/HAL.cpp Vedi File

@@ -31,6 +31,10 @@
31 31
 
32 32
 #include <Wire.h>
33 33
 
34
+// ------------------------
35
+// Serial ports
36
+// ------------------------
37
+
34 38
 #define _IMPLEMENT_SERIAL(X) DefaultSerial##X MSerial##X(false, Serial##X)
35 39
 #define IMPLEMENT_SERIAL(X)  _IMPLEMENT_SERIAL(X)
36 40
 #if WITHIN(SERIAL_PORT, 0, 3)
@@ -39,42 +43,34 @@
39 43
 
40 44
 USBSerialType USBSerial(false, SerialUSB);
41 45
 
42
-uint16_t HAL_adc_result, HAL_adc_select;
43
-
44
-static const uint8_t pin2sc1a[] = {
45
-  5, 14, 8, 9, 13, 12, 6, 7, 15, 4, 3, 19+128, 14+128, 15+128, // 0-13 -> A0-A13
46
-  5, 14, 8, 9, 13, 12, 6, 7, 15, 4, // 14-23 are A0-A9
47
-  255, 255, 255, 255, 255, 255, 255, // 24-30 are digital only
48
-  14+128, 15+128, 17, 18, 4+128, 5+128, 6+128, 7+128, 17+128,  // 31-39 are A12-A20
49
-  255, 255, 255, 255, 255, 255, 255, 255, 255,  // 40-48 are digital only
50
-  10+128, 11+128, // 49-50 are A23-A24
51
-  255, 255, 255, 255, 255, 255, 255, // 51-57 are digital only
52
-  255, 255, 255, 255, 255, 255, // 58-63 (sd card pins) are digital only
53
-  3, 19+128, // 64-65 are A10-A11
54
-  23, 23+128,// 66-67 are A21-A22 (DAC pins)
55
-  1, 1+128,  // 68-69 are A25-A26 (unused USB host port on Teensy 3.5)
56
-  26,        // 70 is Temperature Sensor
57
-  18+128     // 71 is Vref
58
-};
59
-
60
-/*
61
-  // disable interrupts
62
-  void cli() { noInterrupts(); }
63
-
64
-  // enable interrupts
65
-  void sei() { interrupts(); }
66
-*/
67
-
68
-void HAL_adc_init() {
69
-  analog_init();
70
-  while (ADC0_SC3 & ADC_SC3_CAL) {}; // Wait for calibration to finish
71
-  while (ADC1_SC3 & ADC_SC3_CAL) {}; // Wait for calibration to finish
72
-  NVIC_ENABLE_IRQ(IRQ_FTM1);
46
+// ------------------------
47
+// Class Utilities
48
+// ------------------------
49
+
50
+extern "C" {
51
+  extern char __bss_end;
52
+  extern char __heap_start;
53
+  extern void* __brkval;
54
+
55
+  int freeMemory() {
56
+    int free_memory;
57
+    if ((int)__brkval == 0)
58
+      free_memory = ((int)&free_memory) - ((int)&__bss_end);
59
+    else
60
+      free_memory = ((int)&free_memory) - ((int)__brkval);
61
+    return free_memory;
62
+  }
73 63
 }
74 64
 
75
-void HAL_clear_reset_source() { }
65
+// ------------------------
66
+// MarlinHAL Class
67
+// ------------------------
76 68
 
77
-uint8_t HAL_get_reset_source() {
69
+void MarlinHAL::reboot() { _reboot_Teensyduino_(); }
70
+
71
+// Reset
72
+
73
+uint8_t MarlinHAL::get_reset_source() {
78 74
   switch (RCM_SRS0) {
79 75
     case 128: return RST_POWER_ON; break;
80 76
     case 64: return RST_EXTERNAL; break;
@@ -86,41 +82,49 @@ uint8_t HAL_get_reset_source() {
86 82
   return 0;
87 83
 }
88 84
 
89
-void HAL_reboot() { _reboot_Teensyduino_(); }
85
+// ADC
90 86
 
91
-extern "C" {
92
-  extern char __bss_end;
93
-  extern char __heap_start;
94
-  extern void* __brkval;
87
+int8_t MarlinHAL::adc_select;
95 88
 
96
-  int freeMemory() {
97
-    int free_memory;
98
-    if ((int)__brkval == 0)
99
-      free_memory = ((int)&free_memory) - ((int)&__bss_end);
100
-    else
101
-      free_memory = ((int)&free_memory) - ((int)__brkval);
102
-    return free_memory;
103
-  }
89
+void MarlinHAL::adc_init() {
90
+  analog_init();
91
+  while (ADC0_SC3 & ADC_SC3_CAL) { /* Wait for calibration to finish */ }
92
+  while (ADC1_SC3 & ADC_SC3_CAL) { /* Wait for calibration to finish */ }
93
+  NVIC_ENABLE_IRQ(IRQ_FTM1);
104 94
 }
105 95
 
106
-void HAL_adc_start_conversion(const uint8_t adc_pin) {
96
+void MarlinHAL::adc_start(const pin_t adc_pin) {
97
+  static const uint8_t pin2sc1a[] = {
98
+    5, 14, 8, 9, 13, 12, 6, 7, 15, 4, 3, 19+128, 14+128, 15+128, // 0-13 -> A0-A13
99
+    5, 14, 8, 9, 13, 12, 6, 7, 15, 4, // 14-23 are A0-A9
100
+    255, 255, 255, 255, 255, 255, 255, // 24-30 are digital only
101
+    14+128, 15+128, 17, 18, 4+128, 5+128, 6+128, 7+128, 17+128,  // 31-39 are A12-A20
102
+    255, 255, 255, 255, 255, 255, 255, 255, 255,  // 40-48 are digital only
103
+    10+128, 11+128, // 49-50 are A23-A24
104
+    255, 255, 255, 255, 255, 255, 255, // 51-57 are digital only
105
+    255, 255, 255, 255, 255, 255, // 58-63 (sd card pins) are digital only
106
+    3, 19+128, // 64-65 are A10-A11
107
+    23, 23+128,// 66-67 are A21-A22 (DAC pins)
108
+    1, 1+128,  // 68-69 are A25-A26 (unused USB host port on Teensy 3.5)
109
+    26,        // 70 is Temperature Sensor
110
+    18+128     // 71 is Vref
111
+  };
107 112
   const uint16_t pin = pin2sc1a[adc_pin];
108 113
   if (pin == 0xFF) {
109
-    // Digital only
110
-    HAL_adc_select = -1;
114
+    adc_select = -1;    // Digital only
111 115
   }
112 116
   else if (pin & 0x80) {
113
-    HAL_adc_select = 1;
117
+    adc_select = 1;
114 118
     ADC1_SC1A = pin & 0x7F;
115 119
   }
116 120
   else {
117
-    HAL_adc_select = 0;
121
+    adc_select = 0;
118 122
     ADC0_SC1A = pin;
119 123
   }
120 124
 }
121 125
 
122
-uint16_t HAL_adc_get_result() {
123
-  switch (HAL_adc_select) {
126
+uint16_t MarlinHAL::adc_value() {
127
+  switch (adc_select) {
124 128
     case 0: return ADC0_RA;
125 129
     case 1: return ADC1_RA;
126 130
   }

+ 93
- 39
Marlin/src/HAL/TEENSY35_36/HAL.h Vedi File

@@ -37,10 +37,6 @@
37 37
 #include <stdint.h>
38 38
 #include <util/atomic.h>
39 39
 
40
-#define CPU_ST7920_DELAY_1 600
41
-#define CPU_ST7920_DELAY_2 750
42
-#define CPU_ST7920_DELAY_3 750
43
-
44 40
 // ------------------------
45 41
 // Defines
46 42
 // ------------------------
@@ -53,6 +49,17 @@
53 49
   #define IS_TEENSY35 1
54 50
 #endif
55 51
 
52
+#define CPU_ST7920_DELAY_1 600
53
+#define CPU_ST7920_DELAY_2 750
54
+#define CPU_ST7920_DELAY_3 750
55
+
56
+#undef sq
57
+#define sq(x) ((x)*(x))
58
+
59
+// ------------------------
60
+// Serial ports
61
+// ------------------------
62
+
56 63
 #include "../../core/serial_hook.h"
57 64
 
58 65
 #define Serial0 Serial
@@ -76,34 +83,43 @@ extern USBSerialType USBSerial;
76 83
   #error "SERIAL_PORT must be from 0 to 3, or -1 for Native USB."
77 84
 #endif
78 85
 
79
-#define HAL_SERVO_LIB libServo
86
+// ------------------------
87
+// Types
88
+// ------------------------
80 89
 
81
-typedef int8_t pin_t;
90
+class libServo;
91
+typedef libServo hal_servo_t;
82 92
 
83
-#ifndef analogInputToDigitalPin
84
-  #define analogInputToDigitalPin(p) ((p < 12U) ? (p) + 54U : -1)
85
-#endif
93
+typedef int8_t pin_t;
86 94
 
87
-#define CRITICAL_SECTION_START()  uint32_t primask = __get_primask(); __disable_irq()
88
-#define CRITICAL_SECTION_END()    if (!primask) __enable_irq()
89
-#define ISRS_ENABLED() (!__get_primask())
90
-#define ENABLE_ISRS()  __enable_irq()
91
-#define DISABLE_ISRS() __disable_irq()
95
+// ------------------------
96
+// Interrupts
97
+// ------------------------
92 98
 
93
-#undef sq
94
-#define sq(x) ((x)*(x))
99
+#define CRITICAL_SECTION_START()  const bool irqon = !__get_primask(); __disable_irq()
100
+#define CRITICAL_SECTION_END()    if (irqon) __enable_irq()
95 101
 
96
-inline void HAL_init() {}
102
+// ------------------------
103
+// ADC
104
+// ------------------------
97 105
 
98
-// Clear reset reason
99
-void HAL_clear_reset_source();
106
+#ifndef analogInputToDigitalPin
107
+  #define analogInputToDigitalPin(p) ((p < 12U) ? (p) + 54U : -1)
108
+#endif
100 109
 
101
-// Reset reason
102
-uint8_t HAL_get_reset_source();
110
+#define HAL_ADC_VREF         3.3
111
+#define HAL_ADC_RESOLUTION  10
103 112
 
104
-void HAL_reboot();
113
+//
114
+// Pin Mapping for M42, M43, M226
115
+//
116
+#define GET_PIN_MAP_PIN(index) index
117
+#define GET_PIN_MAP_INDEX(pin) pin
118
+#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
105 119
 
106
-FORCE_INLINE void _delay_ms(const int delay_ms) { delay(delay_ms); }
120
+// ------------------------
121
+// Class Utilities
122
+// ------------------------
107 123
 
108 124
 #pragma GCC diagnostic push
109 125
 #if GCC_VERSION <= 50000
@@ -114,27 +130,65 @@ extern "C" int freeMemory();
114 130
 
115 131
 #pragma GCC diagnostic pop
116 132
 
117
-// ADC
133
+// ------------------------
134
+// MarlinHAL Class
135
+// ------------------------
118 136
 
119
-void HAL_adc_init();
137
+class MarlinHAL {
138
+public:
120 139
 
121
-#define HAL_ADC_VREF         3.3
122
-#define HAL_ADC_RESOLUTION  10
123
-#define HAL_START_ADC(pin)  HAL_adc_start_conversion(pin)
124
-#define HAL_READ_ADC()      HAL_adc_get_result()
125
-#define HAL_ADC_READY()     true
140
+  // Earliest possible init, before setup()
141
+  MarlinHAL() {}
126 142
 
127
-#define HAL_ANALOG_SELECT(pin)
143
+  static void init() {}        // Called early in setup()
144
+  static void init_board() {}  // Called less early in setup()
145
+  static void reboot();        // Restart the firmware from 0x0
128 146
 
129
-void HAL_adc_start_conversion(const uint8_t adc_pin);
130
-uint16_t HAL_adc_get_result();
147
+  // Interrupts
148
+  static bool isr_state() { return true; }
149
+  static void isr_on()  { __enable_irq(); }
150
+  static void isr_off() { __disable_irq(); }
131 151
 
132
-// PWM
152
+  static void delay_ms(const int ms) { delay(ms); }
133 153
 
134
-inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); }
154
+  // Tasks, called from idle()
155
+  static void idletask() {}
135 156
 
136
-// Pin Map
157
+  // Reset
158
+  static uint8_t get_reset_source();
159
+  static void clear_reset_source() {}
137 160
 
138
-#define GET_PIN_MAP_PIN(index) index
139
-#define GET_PIN_MAP_INDEX(pin) pin
140
-#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
161
+  // Free SRAM
162
+  static int freeMemory() { return ::freeMemory(); }
163
+
164
+  //
165
+  // ADC Methods
166
+  //
167
+
168
+  static int8_t adc_select;
169
+
170
+  // Called by Temperature::init once at startup
171
+  static void adc_init();
172
+
173
+  // Called by Temperature::init for each sensor at startup
174
+  static void adc_enable(const pin_t) {}
175
+
176
+  // Begin ADC sampling on the given channel
177
+  static void adc_start(const pin_t pin);
178
+
179
+  // Is the ADC ready for reading?
180
+  static bool adc_ready() { return true; }
181
+
182
+  // The current value of the ADC register
183
+  static uint16_t adc_value();
184
+
185
+  /**
186
+   * Set the PWM duty cycle for the pin to the given value.
187
+   * No option to invert the duty cycle [default = false]
188
+   * No option to change the scale of the provided value to enable finer PWM duty control [default = 255]
189
+   */
190
+  static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) {
191
+    analogWrite(pin, v);
192
+  }
193
+
194
+};

+ 1
- 1
Marlin/src/HAL/TEENSY35_36/timers.h Vedi File

@@ -109,4 +109,4 @@ void HAL_timer_disable_interrupt(const uint8_t timer_num);
109 109
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
110 110
 
111 111
 void HAL_timer_isr_prologue(const uint8_t timer_num);
112
-#define HAL_timer_isr_epilogue(T)
112
+#define HAL_timer_isr_epilogue(T) NOOP

+ 100
- 94
Marlin/src/HAL/TEENSY40_41/HAL.cpp Vedi File

@@ -33,6 +33,10 @@
33 33
 #include "timers.h"
34 34
 #include <Wire.h>
35 35
 
36
+// ------------------------
37
+// Serial ports
38
+// ------------------------
39
+
36 40
 #define _IMPLEMENT_SERIAL(X) DefaultSerial##X MSerial##X(false, Serial##X)
37 41
 #define IMPLEMENT_SERIAL(X)  _IMPLEMENT_SERIAL(X)
38 42
 #if WITHIN(SERIAL_PORT, 0, 3)
@@ -40,75 +44,42 @@
40 44
 #endif
41 45
 USBSerialType USBSerial(false, SerialUSB);
42 46
 
43
-uint16_t HAL_adc_result, HAL_adc_select;
44
-
45
-static const uint8_t pin2sc1a[] = {
46
-  0x07,  // 0/A0  AD_B1_02
47
-  0x08,  // 1/A1  AD_B1_03
48
-  0x0C,  // 2/A2  AD_B1_07
49
-  0x0B,  // 3/A3  AD_B1_06
50
-  0x06,  // 4/A4  AD_B1_01
51
-  0x05,  // 5/A5  AD_B1_00
52
-  0x0F,  // 6/A6  AD_B1_10
53
-  0x00,  // 7/A7  AD_B1_11
54
-  0x0D,  // 8/A8  AD_B1_08
55
-  0x0E,  // 9/A9  AD_B1_09
56
-  0x01,  // 24/A10 AD_B0_12
57
-  0x02,  // 25/A11 AD_B0_13
58
-  0x83,  // 26/A12 AD_B1_14 - only on ADC2, 3
59
-  0x84,  // 27/A13 AD_B1_15 - only on ADC2, 4
60
-  0x07,  // 14/A0  AD_B1_02
61
-  0x08,  // 15/A1  AD_B1_03
62
-  0x0C,  // 16/A2  AD_B1_07
63
-  0x0B,  // 17/A3  AD_B1_06
64
-  0x06,  // 18/A4  AD_B1_01
65
-  0x05,  // 19/A5  AD_B1_00
66
-  0x0F,  // 20/A6  AD_B1_10
67
-  0x00,  // 21/A7  AD_B1_11
68
-  0x0D,  // 22/A8  AD_B1_08
69
-  0x0E,  // 23/A9  AD_B1_09
70
-  0x01,  // 24/A10 AD_B0_12
71
-  0x02,  // 25/A11 AD_B0_13
72
-  0x83,  // 26/A12 AD_B1_14 - only on ADC2, 3
73
-  0x84,  // 27/A13 AD_B1_15 - only on ADC2, 4
74
-  #ifdef ARDUINO_TEENSY41
75
-    0xFF,  // 28
76
-    0xFF,  // 29
77
-    0xFF,  // 30
78
-    0xFF,  // 31
79
-    0xFF,  // 32
80
-    0xFF,  // 33
81
-    0xFF,  // 34
82
-    0xFF,  // 35
83
-    0xFF,  // 36
84
-    0xFF,  // 37
85
-    0x81,  // 38/A14 AD_B1_12 - only on ADC2, 1
86
-    0x82,  // 39/A15 AD_B1_13 - only on ADC2, 2
87
-    0x09,  // 40/A16 AD_B1_04
88
-    0x0A,  // 41/A17 AD_B1_05
89
-  #endif
90
-};
91
-
92
-/*
93
-// disable interrupts
94
-void cli() { noInterrupts(); }
95
-
96
-// enable interrupts
97
-void sei() { interrupts(); }
98
-*/
99
-
100
-void HAL_adc_init() {
101
-  analog_init();
102
-  while (ADC1_GC & ADC_GC_CAL) ;
103
-  while (ADC2_GC & ADC_GC_CAL) ;
47
+// ------------------------
48
+// Class Utilities
49
+// ------------------------
50
+
51
+#define __bss_end _ebss
52
+
53
+extern "C" {
54
+  extern char __bss_end;
55
+  extern char __heap_start;
56
+  extern void* __brkval;
57
+
58
+  // Doesn't work on Teensy 4.x
59
+  uint32_t freeMemory() {
60
+    uint32_t free_memory;
61
+    free_memory = ((uint32_t)&free_memory) - (((uint32_t)__brkval) ?: ((uint32_t)&__bss_end));
62
+    return free_memory;
63
+  }
104 64
 }
105 65
 
106
-void HAL_clear_reset_source() {
107
-  uint32_t reset_source = SRC_SRSR;
108
-  SRC_SRSR = reset_source;
66
+// ------------------------
67
+// FastIO
68
+// ------------------------
69
+
70
+bool is_output(pin_t pin) {
71
+  const struct digital_pin_bitband_and_config_table_struct *p;
72
+  p = digital_pin_to_info_PGM + pin;
73
+  return (*(p->reg + 1) & p->mask);
109 74
 }
110 75
 
111
-uint8_t HAL_get_reset_source() {
76
+// ------------------------
77
+// MarlinHAL Class
78
+// ------------------------
79
+
80
+void MarlinHAL::reboot() { _reboot_Teensyduino_(); }
81
+
82
+uint8_t MarlinHAL::get_reset_source() {
112 83
   switch (SRC_SRSR & 0xFF) {
113 84
     case 1: return RST_POWER_ON; break;
114 85
     case 2: return RST_SOFTWARE; break;
@@ -121,57 +92,92 @@ uint8_t HAL_get_reset_source() {
121 92
   return 0;
122 93
 }
123 94
 
124
-void HAL_reboot() { _reboot_Teensyduino_(); }
95
+void MarlinHAL::clear_reset_source() {
96
+  uint32_t reset_source = SRC_SRSR;
97
+  SRC_SRSR = reset_source;
98
+}
125 99
 
126
-#define __bss_end _ebss
100
+// ADC
127 101
 
128
-extern "C" {
129
-  extern char __bss_end;
130
-  extern char __heap_start;
131
-  extern void* __brkval;
102
+int8_t MarlinHAL::adc_select;
132 103
 
133
-  // Doesn't work on Teensy 4.x
134
-  uint32_t freeMemory() {
135
-    uint32_t free_memory;
136
-    if ((uint32_t)__brkval == 0)
137
-      free_memory = ((uint32_t)&free_memory) - ((uint32_t)&__bss_end);
138
-    else
139
-      free_memory = ((uint32_t)&free_memory) - ((uint32_t)__brkval);
140
-    return free_memory;
141
-  }
104
+void MarlinHAL::adc_init() {
105
+  analog_init();
106
+  while (ADC1_GC & ADC_GC_CAL) { /* wait */ }
107
+  while (ADC2_GC & ADC_GC_CAL) { /* wait */ }
142 108
 }
143 109
 
144
-void HAL_adc_start_conversion(const uint8_t adc_pin) {
110
+void MarlinHAL::adc_start(const pin_t adc_pin) {
111
+  static const uint8_t pin2sc1a[] = {
112
+    0x07,  // 0/A0  AD_B1_02
113
+    0x08,  // 1/A1  AD_B1_03
114
+    0x0C,  // 2/A2  AD_B1_07
115
+    0x0B,  // 3/A3  AD_B1_06
116
+    0x06,  // 4/A4  AD_B1_01
117
+    0x05,  // 5/A5  AD_B1_00
118
+    0x0F,  // 6/A6  AD_B1_10
119
+    0x00,  // 7/A7  AD_B1_11
120
+    0x0D,  // 8/A8  AD_B1_08
121
+    0x0E,  // 9/A9  AD_B1_09
122
+    0x01,  // 24/A10 AD_B0_12
123
+    0x02,  // 25/A11 AD_B0_13
124
+    0x83,  // 26/A12 AD_B1_14 - only on ADC2, 3
125
+    0x84,  // 27/A13 AD_B1_15 - only on ADC2, 4
126
+    0x07,  // 14/A0  AD_B1_02
127
+    0x08,  // 15/A1  AD_B1_03
128
+    0x0C,  // 16/A2  AD_B1_07
129
+    0x0B,  // 17/A3  AD_B1_06
130
+    0x06,  // 18/A4  AD_B1_01
131
+    0x05,  // 19/A5  AD_B1_00
132
+    0x0F,  // 20/A6  AD_B1_10
133
+    0x00,  // 21/A7  AD_B1_11
134
+    0x0D,  // 22/A8  AD_B1_08
135
+    0x0E,  // 23/A9  AD_B1_09
136
+    0x01,  // 24/A10 AD_B0_12
137
+    0x02,  // 25/A11 AD_B0_13
138
+    0x83,  // 26/A12 AD_B1_14 - only on ADC2, 3
139
+    0x84,  // 27/A13 AD_B1_15 - only on ADC2, 4
140
+    #ifdef ARDUINO_TEENSY41
141
+      0xFF,  // 28
142
+      0xFF,  // 29
143
+      0xFF,  // 30
144
+      0xFF,  // 31
145
+      0xFF,  // 32
146
+      0xFF,  // 33
147
+      0xFF,  // 34
148
+      0xFF,  // 35
149
+      0xFF,  // 36
150
+      0xFF,  // 37
151
+      0x81,  // 38/A14 AD_B1_12 - only on ADC2, 1
152
+      0x82,  // 39/A15 AD_B1_13 - only on ADC2, 2
153
+      0x09,  // 40/A16 AD_B1_04
154
+      0x0A,  // 41/A17 AD_B1_05
155
+    #endif
156
+  };
145 157
   const uint16_t pin = pin2sc1a[adc_pin];
146 158
   if (pin == 0xFF) {
147
-    HAL_adc_select = -1; // Digital only
159
+    adc_select = -1; // Digital only
148 160
   }
149 161
   else if (pin & 0x80) {
150
-    HAL_adc_select = 1;
162
+    adc_select = 1;
151 163
     ADC2_HC0 = pin & 0x7F;
152 164
   }
153 165
   else {
154
-    HAL_adc_select = 0;
166
+    adc_select = 0;
155 167
     ADC1_HC0 = pin;
156 168
   }
157 169
 }
158 170
 
159
-uint16_t HAL_adc_get_result() {
160
-  switch (HAL_adc_select) {
171
+uint16_t MarlinHAL::adc_value() {
172
+  switch (adc_select) {
161 173
     case 0:
162
-      while (!(ADC1_HS & ADC_HS_COCO0)) ; // wait
174
+      while (!(ADC1_HS & ADC_HS_COCO0)) { /* wait */ }
163 175
       return ADC1_R0;
164 176
     case 1:
165
-      while (!(ADC2_HS & ADC_HS_COCO0)) ; // wait
177
+      while (!(ADC2_HS & ADC_HS_COCO0)) { /* wait */ }
166 178
       return ADC2_R0;
167 179
   }
168 180
   return 0;
169 181
 }
170 182
 
171
-bool is_output(pin_t pin) {
172
-  const struct digital_pin_bitband_and_config_table_struct *p;
173
-  p = digital_pin_to_info_PGM + pin;
174
-  return (*(p->reg + 1) & p->mask);
175
-}
176
-
177 183
 #endif // __IMXRT1062__

+ 100
- 47
Marlin/src/HAL/TEENSY40_41/HAL.h Vedi File

@@ -41,10 +41,6 @@
41 41
   #include "../../feature/ethernet.h"
42 42
 #endif
43 43
 
44
-#define CPU_ST7920_DELAY_1 600
45
-#define CPU_ST7920_DELAY_2 750
46
-#define CPU_ST7920_DELAY_3 750
47
-
48 44
 // ------------------------
49 45
 // Defines
50 46
 // ------------------------
@@ -55,7 +51,23 @@
55 51
   #define IS_TEENSY41 1
56 52
 #endif
57 53
 
54
+#define CPU_ST7920_DELAY_1 600
55
+#define CPU_ST7920_DELAY_2 750
56
+#define CPU_ST7920_DELAY_3 750
57
+
58
+#undef sq
59
+#define sq(x) ((x)*(x))
60
+
61
+// Don't place string constants in PROGMEM
62
+#undef PSTR
63
+#define PSTR(str) ({static const char *data = (str); &data[0];})
64
+
65
+// ------------------------
66
+// Serial ports
67
+// ------------------------
68
+
58 69
 #include "../../core/serial_hook.h"
70
+
59 71
 #define Serial0 Serial
60 72
 #define _DECLARE_SERIAL(X) \
61 73
   typedef ForwardSerial1Class<decltype(Serial##X)> DefaultSerial##X; \
@@ -89,41 +101,47 @@ extern USBSerialType USBSerial;
89 101
   #endif
90 102
 #endif
91 103
 
92
-#define HAL_SERVO_LIB libServo
104
+// ------------------------
105
+// Types
106
+// ------------------------
93 107
 
94
-typedef int8_t pin_t;
108
+class libServo;
109
+typedef libServo hal_servo_t;
95 110
 
96
-#ifndef analogInputToDigitalPin
97
-  #define analogInputToDigitalPin(p) ((p < 12U) ? (p) + 54U : -1)
98
-#endif
111
+typedef int8_t pin_t;
99 112
 
100
-#define CRITICAL_SECTION_START()  uint32_t primask = __get_primask(); __disable_irq()
101
-#define CRITICAL_SECTION_END()    if (!primask) __enable_irq()
102
-#define ISRS_ENABLED() (!__get_primask())
103
-#define ENABLE_ISRS()  __enable_irq()
104
-#define DISABLE_ISRS() __disable_irq()
113
+// ------------------------
114
+// Interrupts
115
+// ------------------------
105 116
 
106
-#undef sq
107
-#define sq(x) ((x)*(x))
117
+#define CRITICAL_SECTION_START()  const bool irqon = !__get_primask(); __disable_irq()
118
+#define CRITICAL_SECTION_END()    if (irqon) __enable_irq()
108 119
 
109
-// Don't place string constants in PROGMEM
110
-#undef PSTR
111
-#define PSTR(str) ({static const char *data = (str); &data[0];})
120
+// ------------------------
121
+// ADC
122
+// ------------------------
112 123
 
113
-// Enable hooks into idle and setup for HAL
114
-#define HAL_IDLETASK 1
115
-FORCE_INLINE void HAL_idletask() {}
116
-FORCE_INLINE void HAL_init() {}
124
+#ifndef analogInputToDigitalPin
125
+  #define analogInputToDigitalPin(p) ((p < 12U) ? (p) + 54U : -1)
126
+#endif
117 127
 
118
-// Clear reset reason
119
-void HAL_clear_reset_source();
128
+#define HAL_ADC_VREF         3.3
129
+#define HAL_ADC_RESOLUTION  10
130
+#define HAL_ADC_FILTERED      // turn off ADC oversampling
120 131
 
121
-// Reset reason
122
-uint8_t HAL_get_reset_source();
132
+//
133
+// Pin Mapping for M42, M43, M226
134
+//
135
+#define GET_PIN_MAP_PIN(index) index
136
+#define GET_PIN_MAP_INDEX(pin) pin
137
+#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
123 138
 
124
-void HAL_reboot();
139
+// FastIO
140
+bool is_output(pin_t pin);
125 141
 
126
-FORCE_INLINE void _delay_ms(const int delay_ms) { delay(delay_ms); }
142
+// ------------------------
143
+// Class Utilities
144
+// ------------------------
127 145
 
128 146
 #pragma GCC diagnostic push
129 147
 #if GCC_VERSION <= 50000
@@ -134,30 +152,65 @@ extern "C" uint32_t freeMemory();
134 152
 
135 153
 #pragma GCC diagnostic pop
136 154
 
137
-// ADC
155
+// ------------------------
156
+// MarlinHAL Class
157
+// ------------------------
138 158
 
139
-void HAL_adc_init();
159
+class MarlinHAL {
160
+public:
140 161
 
141
-#define HAL_ADC_VREF         3.3
142
-#define HAL_ADC_RESOLUTION  10
143
-#define HAL_ADC_FILTERED      // turn off ADC oversampling
144
-#define HAL_START_ADC(pin)  HAL_adc_start_conversion(pin)
145
-#define HAL_READ_ADC()      HAL_adc_get_result()
146
-#define HAL_ADC_READY()     true
162
+  // Earliest possible init, before setup()
163
+  MarlinHAL() {}
147 164
 
148
-#define HAL_ANALOG_SELECT(pin)
165
+  static void init() {}        // Called early in setup()
166
+  static void init_board() {}  // Called less early in setup()
167
+  static void reboot();        // Restart the firmware from 0x0
149 168
 
150
-void HAL_adc_start_conversion(const uint8_t adc_pin);
151
-uint16_t HAL_adc_get_result();
169
+  // Interrupts
170
+  static bool isr_state() { return !__get_primask(); }
171
+  static void isr_on()  { __enable_irq(); }
172
+  static void isr_off() { __disable_irq(); }
152 173
 
153
-// PWM
174
+  static void delay_ms(const int ms) { delay(ms); }
154 175
 
155
-inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); }
176
+  // Tasks, called from idle()
177
+  static void idletask() {}
156 178
 
157
-// Pin Map
179
+  // Reset
180
+  static uint8_t get_reset_source();
181
+  static void clear_reset_source();
158 182
 
159
-#define GET_PIN_MAP_PIN(index) index
160
-#define GET_PIN_MAP_INDEX(pin) pin
161
-#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
183
+  // Free SRAM
184
+  static int freeMemory() { return ::freeMemory(); }
162 185
 
163
-bool is_output(pin_t pin);
186
+  //
187
+  // ADC Methods
188
+  //
189
+
190
+  static int8_t adc_select;
191
+
192
+  // Called by Temperature::init once at startup
193
+  static void adc_init();
194
+
195
+  // Called by Temperature::init for each sensor at startup
196
+  static void adc_enable(const pin_t pin) {}
197
+
198
+  // Begin ADC sampling on the given channel
199
+  static void adc_start(const pin_t pin);
200
+
201
+  // Is the ADC ready for reading?
202
+  static bool adc_ready() { return true; }
203
+
204
+  // The current value of the ADC register
205
+  static uint16_t adc_value();
206
+
207
+  /**
208
+   * Set the PWM duty cycle for the pin to the given value.
209
+   * No option to invert the duty cycle [default = false]
210
+   * No option to change the scale of the provided value to enable finer PWM duty control [default = 255]
211
+   */
212
+  static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) {
213
+    analogWrite(pin, v);
214
+  }
215
+
216
+};

+ 1
- 1
Marlin/src/HAL/TEENSY40_41/timers.h Vedi File

@@ -114,4 +114,4 @@ bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
114 114
 
115 115
 void HAL_timer_isr_prologue(const uint8_t timer_num);
116 116
 //void HAL_timer_isr_epilogue(const uint8_t timer_num) {}
117
-#define HAL_timer_isr_epilogue(T)
117
+#define HAL_timer_isr_epilogue(T) NOOP

+ 36
- 0
Marlin/src/HAL/shared/HAL.cpp Vedi File

@@ -0,0 +1,36 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (c) 2022 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
+
23
+/**
24
+ * HAL/shared/HAL.cpp
25
+ */
26
+
27
+#include "../../inc/MarlinConfig.h"
28
+
29
+MarlinHAL hal;
30
+
31
+#if ENABLED(SOFT_RESET_VIA_SERIAL)
32
+
33
+  // Global for use by e_parser.h
34
+  void HAL_reboot() { hal.reboot(); }
35
+
36
+#endif

+ 4
- 4
Marlin/src/HAL/shared/HAL_spi_L6470.cpp Vedi File

@@ -92,9 +92,9 @@ uint8_t L64XX_Marlin::transfer_single(uint8_t data, int16_t ss_pin) {
92 92
   // First device in chain has data sent last
93 93
   extDigitalWrite(ss_pin, LOW);
94 94
 
95
-  DISABLE_ISRS(); // Disable interrupts during SPI transfer (can't allow partial command to chips)
95
+  hal.isr_off();  // Disable interrupts during SPI transfer (can't allow partial command to chips)
96 96
   const uint8_t data_out = L6470_SpiTransfer_Mode_3(data);
97
-  ENABLE_ISRS();  // Enable interrupts
97
+  hal.isr_on();   // Enable interrupts
98 98
 
99 99
   extDigitalWrite(ss_pin, HIGH);
100 100
   return data_out;
@@ -107,9 +107,9 @@ uint8_t L64XX_Marlin::transfer_chain(uint8_t data, int16_t ss_pin, uint8_t chain
107 107
   extDigitalWrite(ss_pin, LOW);
108 108
 
109 109
   for (uint8_t i = L64XX::chain[0]; !L64xxManager.spi_abort && i >= 1; i--) {   // Send data unless aborted
110
-    DISABLE_ISRS();   // Disable interrupts during SPI transfer (can't allow partial command to chips)
110
+    hal.isr_off();    // Disable interrupts during SPI transfer (can't allow partial command to chips)
111 111
     const uint8_t temp = L6470_SpiTransfer_Mode_3(uint8_t(i == chain_position ? data : dSPIN_NOP));
112
-    ENABLE_ISRS();    // Enable interrupts
112
+    hal.isr_on();     // Enable interrupts
113 113
     if (i == chain_position) data_out = temp;
114 114
   }
115 115
 

+ 1
- 1
Marlin/src/HAL/shared/math_32bit.h Vedi File

@@ -26,6 +26,6 @@
26 26
 /**
27 27
  * Math helper functions for 32 bit CPUs
28 28
  */
29
-static FORCE_INLINE uint32_t MultiU32X24toH32(uint32_t longIn1, uint32_t longIn2) {
29
+FORCE_INLINE static uint32_t MultiU32X24toH32(uint32_t longIn1, uint32_t longIn2) {
30 30
   return ((uint64_t)longIn1 * longIn2 + 0x00800000) >> 24;
31 31
 }

+ 21
- 27
Marlin/src/MarlinCore.cpp Vedi File

@@ -790,7 +790,7 @@ void idle(bool no_stepper_sleep/*=false*/) {
790 790
   #endif
791 791
 
792 792
   // Run HAL idle tasks
793
-  TERN_(HAL_IDLETASK, HAL_idletask());
793
+  hal.idletask();
794 794
 
795 795
   // Check network connection
796 796
   TERN_(HAS_ETHERNET, ethernet.check());
@@ -929,7 +929,7 @@ void minkill(const bool steppers_off/*=false*/) {
929 929
       watchdog_refresh();
930 930
 
931 931
     // Reboot the board
932
-    HAL_reboot();
932
+    hal.reboot();
933 933
 
934 934
   #else
935 935
 
@@ -1041,7 +1041,7 @@ inline void tmc_standby_setup() {
1041 1041
  *    • L64XX Stepper Drivers (SPI)
1042 1042
  *    • Stepper Driver Reset: DISABLE
1043 1043
  *    • TMC Stepper Drivers (SPI)
1044
- *    • Run BOARD_INIT if defined
1044
+ *    • Run hal.init_board() for additional pins setup
1045 1045
  *    • ESP WiFi
1046 1046
  *  - Get the Reset Reason and report it
1047 1047
  *  - Print startup messages and diagnostics
@@ -1119,8 +1119,8 @@ void setup() {
1119 1119
   tmc_standby_setup();  // TMC Low Power Standby pins must be set early or they're not usable
1120 1120
 
1121 1121
   // Check startup - does nothing if bootloader sets MCUSR to 0
1122
-  const byte mcu = HAL_get_reset_source();
1123
-  HAL_clear_reset_source();
1122
+  const byte mcu = hal.get_reset_source();
1123
+  hal.clear_reset_source();
1124 1124
 
1125 1125
   #if ENABLED(MARLIN_DEV_MODE)
1126 1126
     auto log_current_ms = [&](PGM_P const msg) {
@@ -1181,23 +1181,20 @@ void setup() {
1181 1181
     JTAGSWD_RESET();
1182 1182
   #endif
1183 1183
 
1184
-  #if EITHER(DISABLE_DEBUG, DISABLE_JTAG)
1184
+  // Disable any hardware debug to free up pins for IO
1185
+  #if ENABLED(DISABLE_DEBUG) && defined(JTAGSWD_DISABLE)
1185 1186
     delay(10);
1186
-    // Disable any hardware debug to free up pins for IO
1187
-    #if ENABLED(DISABLE_DEBUG) && defined(JTAGSWD_DISABLE)
1188
-      SETUP_LOG("JTAGSWD_DISABLE");
1189
-      JTAGSWD_DISABLE();
1190
-    #elif defined(JTAG_DISABLE)
1191
-      SETUP_LOG("JTAG_DISABLE");
1192
-      JTAG_DISABLE();
1193
-    #else
1194
-      #error "DISABLE_(DEBUG|JTAG) is not supported for the selected MCU/Board."
1195
-    #endif
1187
+    SETUP_LOG("JTAGSWD_DISABLE");
1188
+    JTAGSWD_DISABLE();
1189
+  #elif ENABLED(DISABLE_JTAG) && defined(JTAG_DISABLE)
1190
+    delay(10);
1191
+    SETUP_LOG("JTAG_DISABLE");
1192
+    JTAG_DISABLE();
1196 1193
   #endif
1197 1194
 
1198 1195
   TERN_(DYNAMIC_VECTORTABLE, hook_cpu_exceptions()); // If supported, install Marlin exception handlers at runtime
1199 1196
 
1200
-  SETUP_RUN(HAL_init());
1197
+  SETUP_RUN(hal.init());
1201 1198
 
1202 1199
   // Init and disable SPI thermocouples; this is still needed
1203 1200
   #if TEMP_SENSOR_0_IS_MAX_TC || (TEMP_SENSOR_REDUNDANT_IS_MAX_TC && REDUNDANT_TEMP_MATCH(SOURCE, E0))
@@ -1243,19 +1240,16 @@ void setup() {
1243 1240
     SETUP_RUN(tmc_init_cs_pins());
1244 1241
   #endif
1245 1242
 
1246
-  #ifdef BOARD_INIT
1247
-    SETUP_LOG("BOARD_INIT");
1248
-    BOARD_INIT();
1249
-  #endif
1243
+  SETUP_RUN(hal.init_board());
1250 1244
 
1251 1245
   SETUP_RUN(esp_wifi_init());
1252 1246
 
1253 1247
   // Report Reset Reason
1254
-  if (mcu & RST_POWER_ON) SERIAL_ECHOLNPGM(STR_POWERUP);
1255
-  if (mcu & RST_EXTERNAL) SERIAL_ECHOLNPGM(STR_EXTERNAL_RESET);
1248
+  if (mcu & RST_POWER_ON)  SERIAL_ECHOLNPGM(STR_POWERUP);
1249
+  if (mcu & RST_EXTERNAL)  SERIAL_ECHOLNPGM(STR_EXTERNAL_RESET);
1256 1250
   if (mcu & RST_BROWN_OUT) SERIAL_ECHOLNPGM(STR_BROWNOUT_RESET);
1257
-  if (mcu & RST_WATCHDOG) SERIAL_ECHOLNPGM(STR_WATCHDOG_RESET);
1258
-  if (mcu & RST_SOFTWARE) SERIAL_ECHOLNPGM(STR_SOFTWARE_RESET);
1251
+  if (mcu & RST_WATCHDOG)  SERIAL_ECHOLNPGM(STR_WATCHDOG_RESET);
1252
+  if (mcu & RST_SOFTWARE)  SERIAL_ECHOLNPGM(STR_SOFTWARE_RESET);
1259 1253
 
1260 1254
   // Identify myself as Marlin x.x.x
1261 1255
   SERIAL_ECHOLNPGM("Marlin " SHORT_BUILD_VERSION);
@@ -1266,7 +1260,7 @@ void setup() {
1266 1260
     );
1267 1261
   #endif
1268 1262
   SERIAL_ECHO_MSG(" Compiled: " __DATE__);
1269
-  SERIAL_ECHO_MSG(STR_FREE_MEMORY, freeMemory(), STR_PLANNER_BUFFER_BYTES, sizeof(block_t) * (BLOCK_BUFFER_SIZE));
1263
+  SERIAL_ECHO_MSG(STR_FREE_MEMORY, hal.freeMemory(), STR_PLANNER_BUFFER_BYTES, sizeof(block_t) * (BLOCK_BUFFER_SIZE));
1270 1264
 
1271 1265
   // Some HAL need precise delay adjustment
1272 1266
   calibrate_delay_loop();
@@ -1542,7 +1536,7 @@ void setup() {
1542 1536
   #endif
1543 1537
 
1544 1538
   #if ENABLED(USE_WATCHDOG)
1545
-    SETUP_RUN(watchdog_init());       // Reinit watchdog after HAL_get_reset_source call
1539
+    SETUP_RUN(watchdog_init());       // Reinit watchdog after hal.get_reset_source call
1546 1540
   #endif
1547 1541
 
1548 1542
   #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)

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

@@ -79,7 +79,7 @@ void CaseLight::update(const bool sflag) {
79 79
 
80 80
     #if CASELIGHT_USES_BRIGHTNESS
81 81
       if (pin_is_pwm())
82
-        set_pwm_duty(pin_t(CASE_LIGHT_PIN), (
82
+        hal.set_pwm_duty(pin_t(CASE_LIGHT_PIN), (
83 83
           #if CASE_LIGHT_MAX_PWM == 255
84 84
             n10ct
85 85
           #else

+ 1
- 1
Marlin/src/feature/controllerfan.cpp Vedi File

@@ -76,7 +76,7 @@ void ControllerFan::update() {
76 76
       thermalManager.soft_pwm_controller_speed = speed;
77 77
     #else
78 78
       if (PWM_PIN(CONTROLLER_FAN_PIN))
79
-        set_pwm_duty(pin_t(CONTROLLER_FAN_PIN), speed);
79
+        hal.set_pwm_duty(pin_t(CONTROLLER_FAN_PIN), speed);
80 80
       else
81 81
         WRITE(CONTROLLER_FAN_PIN, speed > 0);
82 82
     #endif

+ 3
- 1
Marlin/src/feature/e_parser.h Vedi File

@@ -41,7 +41,9 @@ extern bool wait_for_user, wait_for_heatup;
41 41
   void quickresume_stepper();
42 42
 #endif
43 43
 
44
-void HAL_reboot();
44
+#if ENABLED(SOFT_RESET_VIA_SERIAL)
45
+  void HAL_reboot();
46
+#endif
45 47
 
46 48
 class EmergencyParser {
47 49
 

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

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

+ 4
- 4
Marlin/src/feature/spindle_laser.cpp Vedi File

@@ -66,10 +66,10 @@ void SpindleLaser::init() {
66 66
   #endif
67 67
   #if ENABLED(SPINDLE_LASER_USE_PWM)
68 68
     SET_PWM(SPINDLE_LASER_PWM_PIN);
69
-    set_pwm_duty(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Set to lowest speed
69
+    hal.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) && SPINDLE_LASER_FREQUENCY
72
-    set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_FREQUENCY);
72
+    hal.set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_FREQUENCY);
73 73
     TERN_(MARLIN_DEV_MODE, frequency = SPINDLE_LASER_FREQUENCY);
74 74
   #endif
75 75
   #if ENABLED(AIR_EVACUATION)
@@ -89,9 +89,9 @@ void SpindleLaser::init() {
89 89
    */
90 90
   void SpindleLaser::_set_ocr(const uint8_t ocr) {
91 91
     #if ENABLED(HAL_CAN_SET_PWM_FREQ) && SPINDLE_LASER_FREQUENCY
92
-      set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), TERN(MARLIN_DEV_MODE, frequency, SPINDLE_LASER_FREQUENCY));
92
+      hal.set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), TERN(MARLIN_DEV_MODE, frequency, SPINDLE_LASER_FREQUENCY));
93 93
     #endif
94
-    set_pwm_duty(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF);
94
+    hal.set_pwm_duty(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF);
95 95
   }
96 96
 
97 97
   void SpindleLaser::set_ocr(const uint8_t ocr) {

+ 1
- 1
Marlin/src/feature/spindle_laser.h Vedi File

@@ -103,7 +103,7 @@ public:
103 103
   static void init();
104 104
 
105 105
   #if ENABLED(MARLIN_DEV_MODE)
106
-    static void refresh_frequency() { set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), frequency); }
106
+    static void refresh_frequency() { hal.set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), frequency); }
107 107
   #endif
108 108
 
109 109
   // Modifying this function should update everywhere

+ 2
- 2
Marlin/src/gcode/control/M42.cpp Vedi 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 set_pwm_duty()
129
+    // A simple I/O will be set to 0 by hal.set_pwm_duty()
130 130
     if (pin_status <= 1 && !PWM_PIN(pin)) return;
131 131
   #endif
132
-  set_pwm_duty(pin, pin_status);
132
+  hal.set_pwm_duty(pin, pin_status);
133 133
 }
134 134
 
135 135
 #endif // DIRECT_PIN_CONTROL

+ 5
- 5
Marlin/src/gcode/gcode_d.cpp Vedi File

@@ -38,7 +38,7 @@
38 38
 #include "../sd/cardreader.h"
39 39
 #include "../MarlinCore.h" // for kill
40 40
 
41
-extern void dump_delay_accuracy_check();
41
+void dump_delay_accuracy_check();
42 42
 
43 43
 /**
44 44
  * Dn: G-code for development and testing
@@ -54,7 +54,7 @@ void GcodeSuite::D(const int16_t dcode) {
54 54
       for (;;) { /* loop forever (watchdog reset) */ }
55 55
 
56 56
     case 0:
57
-      HAL_reboot();
57
+      hal.reboot();
58 58
       break;
59 59
 
60 60
     case 10:
@@ -74,7 +74,7 @@ void GcodeSuite::D(const int16_t dcode) {
74 74
         settings.reset();
75 75
         settings.save();
76 76
       #endif
77
-      HAL_reboot();
77
+      hal.reboot();
78 78
     } break;
79 79
 
80 80
     case 2: { // D2 Read / Write SRAM
@@ -189,12 +189,12 @@ void GcodeSuite::D(const int16_t dcode) {
189 189
       SERIAL_ECHOLNPGM("(USE_WATCHDOG " TERN(USE_WATCHDOG, "ENABLED", "DISABLED") ")");
190 190
       thermalManager.disable_all_heaters();
191 191
       delay(1000); // Allow time to print
192
-      DISABLE_ISRS();
192
+      hal.isr_off();
193 193
       // Use a low-level delay that does not rely on interrupts to function
194 194
       // Do not spin forever, to avoid thermal risks if heaters are enabled and
195 195
       // watchdog does not work.
196 196
       for (int i = 10000; i--;) DELAY_US(1000UL);
197
-      ENABLE_ISRS();
197
+      hal.isr_on();
198 198
       SERIAL_ECHOLNPGM("FAILURE: Watchdog did not trigger board reset.");
199 199
     } break;
200 200
 

+ 1
- 0
Marlin/src/inc/MarlinConfig.h Vedi File

@@ -29,6 +29,7 @@
29 29
 
30 30
 #ifndef __MARLIN_DEPS__
31 31
   #include "../HAL/HAL.h"
32
+  extern MarlinHAL hal;
32 33
 #endif
33 34
 
34 35
 #include "../pins/pins.h"

+ 7
- 0
Marlin/src/inc/SanityCheck.h Vedi File

@@ -3891,3 +3891,10 @@ static_assert(_PLUS_TEST(4), "HOMING_FEEDRATE_MM_M values must be positive.");
3891 3891
 #undef _TEST_PWM
3892 3892
 #undef _LINEAR_AXES_STR
3893 3893
 #undef _LOGICAL_AXES_STR
3894
+
3895
+// JTAG support in the HAL
3896
+#if ENABLED(DISABLE_DEBUG) && !defined(JTAGSWD_DISABLE)
3897
+  #error "DISABLE_DEBUG is not supported for the selected MCU/Board."
3898
+#elif ENABLED(DISABLE_JTAG) && !defined(JTAG_DISABLE)
3899
+  #error "DISABLE_JTAG is not supported for the selected MCU/Board."
3900
+#endif

+ 2
- 2
Marlin/src/lcd/dogm/marlinui_DOGM.cpp Vedi File

@@ -282,9 +282,9 @@ void MarlinUI::init_lcd() {
282 282
   #if PIN_EXISTS(LCD_RESET)
283 283
     // Perform a clean hardware reset with needed delays
284 284
     OUT_WRITE(LCD_RESET_PIN, LOW);
285
-    _delay_ms(5);
285
+    hal.delay_ms(5);
286 286
     WRITE(LCD_RESET_PIN, HIGH);
287
-    _delay_ms(5);
287
+    hal.delay_ms(5);
288 288
     u8g.begin();
289 289
   #endif
290 290
 

+ 1
- 1
Marlin/src/lcd/e3v2/proui/dwin.cpp Vedi File

@@ -2206,7 +2206,7 @@ void RebootPrinter() {
2206 2206
   thermalManager.disable_all_heaters();
2207 2207
   planner.finish_and_disable();
2208 2208
   DWIN_RebootScreen();
2209
-  HAL_reboot();
2209
+  hal.reboot();
2210 2210
 }
2211 2211
 
2212 2212
 void Goto_Info_Menu(){

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

@@ -1348,7 +1348,7 @@ void Endstops::update() {
1348 1348
         ES_REPORT_CHANGE(K_MAX);
1349 1349
       #endif
1350 1350
       SERIAL_ECHOLNPGM("\n");
1351
-      set_pwm_duty(pin_t(LED_PIN), local_LED_status);
1351
+      hal.set_pwm_duty(pin_t(LED_PIN), local_LED_status);
1352 1352
       local_LED_status ^= 255;
1353 1353
       old_live_state_local = live_state_local;
1354 1354
     }

+ 4
- 4
Marlin/src/module/planner.cpp Vedi File

@@ -703,7 +703,7 @@ void Planner::init() {
703 703
     // All other 32-bit MPUs can easily do inverse using hardware division,
704 704
     // so we don't need to reduce precision or to use assembly language at all.
705 705
     // This routine, for all other archs, returns 0x100000000 / d ~= 0xFFFFFFFF / d
706
-    static FORCE_INLINE uint32_t get_period_inverse(const uint32_t d) {
706
+    FORCE_INLINE static uint32_t get_period_inverse(const uint32_t d) {
707 707
       return d ? 0xFFFFFFFF / d : 0xFFFFFFFF;
708 708
     }
709 709
   #endif
@@ -1260,7 +1260,7 @@ void Planner::recalculate() {
1260 1260
     #if ENABLED(FAN_SOFT_PWM)
1261 1261
       #define _FAN_SET(F) thermalManager.soft_pwm_amount_fan[F] = CALC_FAN_SPEED(F);
1262 1262
     #else
1263
-      #define _FAN_SET(F) set_pwm_duty(pin_t(FAN##F##_PIN), CALC_FAN_SPEED(F));
1263
+      #define _FAN_SET(F) hal.set_pwm_duty(pin_t(FAN##F##_PIN), CALC_FAN_SPEED(F));
1264 1264
     #endif
1265 1265
     #define FAN_SET(F) do{ kickstart_fan(fan_speed, ms, F); _FAN_SET(F); }while(0)
1266 1266
 
@@ -1397,8 +1397,8 @@ void Planner::check_axes_activity() {
1397 1397
   TERN_(AUTOTEMP, autotemp_task());
1398 1398
 
1399 1399
   #if ENABLED(BARICUDA)
1400
-    TERN_(HAS_HEATER_1, set_pwm_duty(pin_t(HEATER_1_PIN), tail_valve_pressure));
1401
-    TERN_(HAS_HEATER_2, set_pwm_duty(pin_t(HEATER_2_PIN), tail_e_to_p_pressure));
1400
+    TERN_(HAS_HEATER_1, hal.set_pwm_duty(pin_t(HEATER_1_PIN), tail_valve_pressure));
1401
+    TERN_(HAS_HEATER_2, hal.set_pwm_duty(pin_t(HEATER_2_PIN), tail_e_to_p_pressure));
1402 1402
   #endif
1403 1403
 }
1404 1404
 

+ 1
- 1
Marlin/src/module/servo.cpp Vedi File

@@ -30,7 +30,7 @@
30 30
 
31 31
 #include "servo.h"
32 32
 
33
-HAL_SERVO_LIB servo[NUM_SERVOS];
33
+hal_servo_t servo[NUM_SERVOS];
34 34
 
35 35
 #if ENABLED(EDITABLE_SERVO_ANGLES)
36 36
   uint16_t servo_angles[NUM_SERVOS][2];

+ 1
- 1
Marlin/src/module/servo.h Vedi File

@@ -112,5 +112,5 @@
112 112
 #define MOVE_SERVO(I, P) servo[I].move(P)
113 113
 #define DETACH_SERVO(I) servo[I].detach()
114 114
 
115
-extern HAL_SERVO_LIB servo[NUM_SERVOS];
115
+extern hal_servo_t servo[NUM_SERVOS];
116 116
 void servo_init();

+ 13
- 13
Marlin/src/module/stepper.cpp Vedi File

@@ -1474,7 +1474,7 @@ void Stepper::isr() {
1474 1474
   #ifndef __AVR__
1475 1475
     // Disable interrupts, to avoid ISR preemption while we reprogram the period
1476 1476
     // (AVR enters the ISR with global interrupts disabled, so no need to do it here)
1477
-    DISABLE_ISRS();
1477
+    hal.isr_off();
1478 1478
   #endif
1479 1479
 
1480 1480
   // Program timer compare for the maximum period, so it does NOT
@@ -1492,7 +1492,7 @@ void Stepper::isr() {
1492 1492
   hal_timer_t min_ticks;
1493 1493
   do {
1494 1494
     // Enable ISRs to reduce USART processing latency
1495
-    ENABLE_ISRS();
1495
+    hal.isr_on();
1496 1496
 
1497 1497
     if (!nextMainISR) pulse_phase_isr();                            // 0 = Do coordinated axes Stepper pulses
1498 1498
 
@@ -1576,7 +1576,7 @@ void Stepper::isr() {
1576 1576
      * is less than the current count due to something preempting between the
1577 1577
      * read and the write of the new period value).
1578 1578
      */
1579
-    DISABLE_ISRS();
1579
+    hal.isr_off();
1580 1580
 
1581 1581
     /**
1582 1582
      * Get the current tick value + margin
@@ -1611,7 +1611,7 @@ void Stepper::isr() {
1611 1611
   HAL_timer_set_compare(MF_TIMER_STEP, hal_timer_t(next_isr_ticks));
1612 1612
 
1613 1613
   // Don't forget to finally reenable interrupts
1614
-  ENABLE_ISRS();
1614
+  hal.isr_on();
1615 1615
 }
1616 1616
 
1617 1617
 #if MINIMUM_STEPPER_PULSE || MAXIMUM_STEPPER_RATE
@@ -3260,33 +3260,33 @@ void Stepper::report_positions() {
3260 3260
 
3261 3261
       #elif HAS_MOTOR_CURRENT_PWM
3262 3262
 
3263
-        #define _WRITE_CURRENT_PWM_DUTY(P) set_pwm_duty(pin_t(MOTOR_CURRENT_PWM_## P ##_PIN), 255L * current / (MOTOR_CURRENT_PWM_RANGE))
3263
+        #define _WRITE_CURRENT_PWM(P) hal.set_pwm_duty(pin_t(MOTOR_CURRENT_PWM_## P ##_PIN), 255L * current / (MOTOR_CURRENT_PWM_RANGE))
3264 3264
         switch (driver) {
3265 3265
           case 0:
3266 3266
             #if PIN_EXISTS(MOTOR_CURRENT_PWM_X)
3267
-              _WRITE_CURRENT_PWM_DUTY(X);
3267
+              _WRITE_CURRENT_PWM(X);
3268 3268
             #endif
3269 3269
             #if PIN_EXISTS(MOTOR_CURRENT_PWM_Y)
3270
-              _WRITE_CURRENT_PWM_DUTY(Y);
3270
+              _WRITE_CURRENT_PWM(Y);
3271 3271
             #endif
3272 3272
             #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
3273
-              _WRITE_CURRENT_PWM_DUTY(XY);
3273
+              _WRITE_CURRENT_PWM(XY);
3274 3274
             #endif
3275 3275
             break;
3276 3276
           case 1:
3277 3277
             #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
3278
-              _WRITE_CURRENT_PWM_DUTY(Z);
3278
+              _WRITE_CURRENT_PWM(Z);
3279 3279
             #endif
3280 3280
             break;
3281 3281
           case 2:
3282 3282
             #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
3283
-              _WRITE_CURRENT_PWM_DUTY(E);
3283
+              _WRITE_CURRENT_PWM(E);
3284 3284
             #endif
3285 3285
             #if PIN_EXISTS(MOTOR_CURRENT_PWM_E0)
3286
-              _WRITE_CURRENT_PWM_DUTY(E0);
3286
+              _WRITE_CURRENT_PWM(E0);
3287 3287
             #endif
3288 3288
             #if PIN_EXISTS(MOTOR_CURRENT_PWM_E1)
3289
-              _WRITE_CURRENT_PWM_DUTY(E1);
3289
+              _WRITE_CURRENT_PWM(E1);
3290 3290
             #endif
3291 3291
             break;
3292 3292
         }
@@ -3308,7 +3308,7 @@ void Stepper::report_positions() {
3308 3308
         #ifdef __SAM3X8E__
3309 3309
           #define _RESET_CURRENT_PWM_FREQ(P) NOOP
3310 3310
         #else
3311
-          #define _RESET_CURRENT_PWM_FREQ(P) set_pwm_frequency(pin_t(P), MOTOR_CURRENT_PWM_FREQUENCY)
3311
+          #define _RESET_CURRENT_PWM_FREQ(P) hal.set_pwm_frequency(pin_t(P), MOTOR_CURRENT_PWM_FREQUENCY)
3312 3312
         #endif
3313 3313
         #define INIT_CURRENT_PWM(P) do{ SET_PWM(MOTOR_CURRENT_PWM_## P ##_PIN); _RESET_CURRENT_PWM_FREQ(MOTOR_CURRENT_PWM_## P ##_PIN); }while(0)
3314 3314
 

+ 57
- 98
Marlin/src/module/temperature.cpp Vedi File

@@ -327,7 +327,7 @@ PGMSTR(str_t_heating_failed, STR_T_HEATING_FAILED);
327 327
   #define _INIT_FAN_PIN(P) do{ if (PWM_PIN(P)) SET_PWM(P); else _INIT_SOFT_FAN(P); }while(0)
328 328
 #endif
329 329
 #if ENABLED(FAST_PWM_FAN)
330
-  #define SET_FAST_PWM_FREQ(P) set_pwm_frequency(P, FAST_PWM_FAN_FREQUENCY)
330
+  #define SET_FAST_PWM_FREQ(P) hal.set_pwm_frequency(pin_t(P), FAST_PWM_FAN_FREQUENCY)
331 331
 #else
332 332
   #define SET_FAST_PWM_FREQ(P) NOOP
333 333
 #endif
@@ -818,7 +818,7 @@ volatile bool Temperature::raw_temps_ready = false;
818 818
       }
819 819
 
820 820
       // Run HAL idle tasks
821
-      TERN_(HAL_IDLETASK, HAL_idletask());
821
+      hal.idletask();
822 822
 
823 823
       // Run UI update
824 824
       TERN(HAS_DWIN_E3V2_BASIC, DWIN_Update(), ui.update());
@@ -907,7 +907,7 @@ int16_t Temperature::getHeaterPower(const heater_id_t heater_id) {
907 907
 
908 908
     #define _UPDATE_AUTO_FAN(P,D,A) do{                   \
909 909
       if (PWM_PIN(P##_AUTO_FAN_PIN) && A < 255)           \
910
-        set_pwm_duty(pin_t(P##_AUTO_FAN_PIN), D ? A : 0); \
910
+        hal.set_pwm_duty(pin_t(P##_AUTO_FAN_PIN), D ? A : 0); \
911 911
       else                                                \
912 912
         WRITE(P##_AUTO_FAN_PIN, D);                       \
913 913
     }while(0)
@@ -2321,74 +2321,33 @@ void Temperature::init() {
2321 2321
 
2322 2322
   TERN_(HAS_MAXTC_SW_SPI, max_tc_spi.init());
2323 2323
 
2324
-  HAL_adc_init();
2324
+  hal.adc_init();
2325
+
2326
+  TERN_(HAS_TEMP_ADC_0,         hal.adc_enable(TEMP_0_PIN));
2327
+  TERN_(HAS_TEMP_ADC_1,         hal.adc_enable(TEMP_1_PIN));
2328
+  TERN_(HAS_TEMP_ADC_2,         hal.adc_enable(TEMP_2_PIN));
2329
+  TERN_(HAS_TEMP_ADC_3,         hal.adc_enable(TEMP_3_PIN));
2330
+  TERN_(HAS_TEMP_ADC_4,         hal.adc_enable(TEMP_4_PIN));
2331
+  TERN_(HAS_TEMP_ADC_5,         hal.adc_enable(TEMP_5_PIN));
2332
+  TERN_(HAS_TEMP_ADC_6,         hal.adc_enable(TEMP_6_PIN));
2333
+  TERN_(HAS_TEMP_ADC_7,         hal.adc_enable(TEMP_7_PIN));
2334
+  TERN_(HAS_JOY_ADC_X,          hal.adc_enable(JOY_X_PIN));
2335
+  TERN_(HAS_JOY_ADC_Y,          hal.adc_enable(JOY_Y_PIN));
2336
+  TERN_(HAS_JOY_ADC_Z,          hal.adc_enable(JOY_Z_PIN));
2337
+  TERN_(HAS_TEMP_ADC_BED,       hal.adc_enable(TEMP_BED_PIN));
2338
+  TERN_(HAS_TEMP_ADC_CHAMBER,   hal.adc_enable(TEMP_CHAMBER_PIN));
2339
+  TERN_(HAS_TEMP_ADC_PROBE,     hal.adc_enable(TEMP_PROBE_PIN));
2340
+  TERN_(HAS_TEMP_ADC_COOLER,    hal.adc_enable(TEMP_COOLER_PIN));
2341
+  TERN_(HAS_TEMP_ADC_BOARD,     hal.adc_enable(TEMP_BOARD_PIN));
2342
+  TERN_(HAS_TEMP_ADC_REDUNDANT, hal.adc_enable(TEMP_REDUNDANT_PIN));
2343
+  TERN_(FILAMENT_WIDTH_SENSOR,  hal.adc_enable(FILWIDTH_PIN));
2344
+  TERN_(HAS_ADC_BUTTONS,        hal.adc_enable(ADC_KEYPAD_PIN));
2345
+  TERN_(POWER_MONITOR_CURRENT,  hal.adc_enable(POWER_MONITOR_CURRENT_PIN));
2346
+  TERN_(POWER_MONITOR_VOLTAGE,  hal.adc_enable(POWER_MONITOR_VOLTAGE_PIN));
2325 2347
 
2326
-  #if HAS_TEMP_ADC_0
2327
-    HAL_ANALOG_SELECT(TEMP_0_PIN);
2328
-  #endif
2329
-  #if HAS_TEMP_ADC_1
2330
-    HAL_ANALOG_SELECT(TEMP_1_PIN);
2331
-  #endif
2332
-  #if HAS_TEMP_ADC_2
2333
-    HAL_ANALOG_SELECT(TEMP_2_PIN);
2334
-  #endif
2335
-  #if HAS_TEMP_ADC_3
2336
-    HAL_ANALOG_SELECT(TEMP_3_PIN);
2337
-  #endif
2338
-  #if HAS_TEMP_ADC_4
2339
-    HAL_ANALOG_SELECT(TEMP_4_PIN);
2340
-  #endif
2341
-  #if HAS_TEMP_ADC_5
2342
-    HAL_ANALOG_SELECT(TEMP_5_PIN);
2343
-  #endif
2344
-  #if HAS_TEMP_ADC_6
2345
-    HAL_ANALOG_SELECT(TEMP_6_PIN);
2346
-  #endif
2347
-  #if HAS_TEMP_ADC_7
2348
-    HAL_ANALOG_SELECT(TEMP_7_PIN);
2349
-  #endif
2350
-  #if HAS_JOY_ADC_X
2351
-    HAL_ANALOG_SELECT(JOY_X_PIN);
2352
-  #endif
2353
-  #if HAS_JOY_ADC_Y
2354
-    HAL_ANALOG_SELECT(JOY_Y_PIN);
2355
-  #endif
2356
-  #if HAS_JOY_ADC_Z
2357
-    HAL_ANALOG_SELECT(JOY_Z_PIN);
2358
-  #endif
2359 2348
   #if HAS_JOY_ADC_EN
2360 2349
     SET_INPUT_PULLUP(JOY_EN_PIN);
2361 2350
   #endif
2362
-  #if HAS_TEMP_ADC_BED
2363
-    HAL_ANALOG_SELECT(TEMP_BED_PIN);
2364
-  #endif
2365
-  #if HAS_TEMP_ADC_CHAMBER
2366
-    HAL_ANALOG_SELECT(TEMP_CHAMBER_PIN);
2367
-  #endif
2368
-  #if HAS_TEMP_ADC_PROBE
2369
-    HAL_ANALOG_SELECT(TEMP_PROBE_PIN);
2370
-  #endif
2371
-  #if HAS_TEMP_ADC_COOLER
2372
-    HAL_ANALOG_SELECT(TEMP_COOLER_PIN);
2373
-  #endif
2374
-  #if HAS_TEMP_ADC_BOARD
2375
-    HAL_ANALOG_SELECT(TEMP_BOARD_PIN);
2376
-  #endif
2377
-  #if HAS_TEMP_ADC_REDUNDANT
2378
-    HAL_ANALOG_SELECT(TEMP_REDUNDANT_PIN);
2379
-  #endif
2380
-  #if ENABLED(FILAMENT_WIDTH_SENSOR)
2381
-    HAL_ANALOG_SELECT(FILWIDTH_PIN);
2382
-  #endif
2383
-  #if HAS_ADC_BUTTONS
2384
-    HAL_ANALOG_SELECT(ADC_KEYPAD_PIN);
2385
-  #endif
2386
-  #if ENABLED(POWER_MONITOR_CURRENT)
2387
-    HAL_ANALOG_SELECT(POWER_MONITOR_CURRENT_PIN);
2388
-  #endif
2389
-  #if ENABLED(POWER_MONITOR_VOLTAGE)
2390
-    HAL_ANALOG_SELECT(POWER_MONITOR_VOLTAGE_PIN);
2391
-  #endif
2392 2351
 
2393 2352
   HAL_timer_start(MF_TIMER_TEMP, TEMP_TIMER_FREQUENCY);
2394 2353
   ENABLE_TEMPERATURE_INTERRUPT();
@@ -3364,8 +3323,8 @@ void Temperature::isr() {
3364 3323
    * This gives each ADC 0.9765ms to charge up.
3365 3324
    */
3366 3325
   #define ACCUMULATE_ADC(obj) do{ \
3367
-    if (!HAL_ADC_READY()) next_sensor_state = adc_sensor_state; \
3368
-    else obj.sample(HAL_READ_ADC()); \
3326
+    if (!hal.adc_ready()) next_sensor_state = adc_sensor_state; \
3327
+    else obj.sample(hal.adc_value()); \
3369 3328
   }while(0)
3370 3329
 
3371 3330
   ADCSensorState next_sensor_state = adc_sensor_state < SensorsReady ? (ADCSensorState)(int(adc_sensor_state) + 1) : StartSampling;
@@ -3397,115 +3356,115 @@ void Temperature::isr() {
3397 3356
       break;
3398 3357
 
3399 3358
     #if HAS_TEMP_ADC_0
3400
-      case PrepareTemp_0: HAL_START_ADC(TEMP_0_PIN); break;
3359
+      case PrepareTemp_0: hal.adc_start(TEMP_0_PIN); break;
3401 3360
       case MeasureTemp_0: ACCUMULATE_ADC(temp_hotend[0]); break;
3402 3361
     #endif
3403 3362
 
3404 3363
     #if HAS_TEMP_ADC_BED
3405
-      case PrepareTemp_BED: HAL_START_ADC(TEMP_BED_PIN); break;
3364
+      case PrepareTemp_BED: hal.adc_start(TEMP_BED_PIN); break;
3406 3365
       case MeasureTemp_BED: ACCUMULATE_ADC(temp_bed); break;
3407 3366
     #endif
3408 3367
 
3409 3368
     #if HAS_TEMP_ADC_CHAMBER
3410
-      case PrepareTemp_CHAMBER: HAL_START_ADC(TEMP_CHAMBER_PIN); break;
3369
+      case PrepareTemp_CHAMBER: hal.adc_start(TEMP_CHAMBER_PIN); break;
3411 3370
       case MeasureTemp_CHAMBER: ACCUMULATE_ADC(temp_chamber); break;
3412 3371
     #endif
3413 3372
 
3414 3373
     #if HAS_TEMP_ADC_COOLER
3415
-      case PrepareTemp_COOLER: HAL_START_ADC(TEMP_COOLER_PIN); break;
3374
+      case PrepareTemp_COOLER: hal.adc_start(TEMP_COOLER_PIN); break;
3416 3375
       case MeasureTemp_COOLER: ACCUMULATE_ADC(temp_cooler); break;
3417 3376
     #endif
3418 3377
 
3419 3378
     #if HAS_TEMP_ADC_PROBE
3420
-      case PrepareTemp_PROBE: HAL_START_ADC(TEMP_PROBE_PIN); break;
3379
+      case PrepareTemp_PROBE: hal.adc_start(TEMP_PROBE_PIN); break;
3421 3380
       case MeasureTemp_PROBE: ACCUMULATE_ADC(temp_probe); break;
3422 3381
     #endif
3423 3382
 
3424 3383
     #if HAS_TEMP_ADC_BOARD
3425
-      case PrepareTemp_BOARD: HAL_START_ADC(TEMP_BOARD_PIN); break;
3384
+      case PrepareTemp_BOARD: hal.adc_start(TEMP_BOARD_PIN); break;
3426 3385
       case MeasureTemp_BOARD: ACCUMULATE_ADC(temp_board); break;
3427 3386
     #endif
3428 3387
 
3429 3388
     #if HAS_TEMP_ADC_REDUNDANT
3430
-      case PrepareTemp_REDUNDANT: HAL_START_ADC(TEMP_REDUNDANT_PIN); break;
3389
+      case PrepareTemp_REDUNDANT: hal.adc_start(TEMP_REDUNDANT_PIN); break;
3431 3390
       case MeasureTemp_REDUNDANT: ACCUMULATE_ADC(temp_redundant); break;
3432 3391
     #endif
3433 3392
 
3434 3393
     #if HAS_TEMP_ADC_1
3435
-      case PrepareTemp_1: HAL_START_ADC(TEMP_1_PIN); break;
3394
+      case PrepareTemp_1: hal.adc_start(TEMP_1_PIN); break;
3436 3395
       case MeasureTemp_1: ACCUMULATE_ADC(temp_hotend[1]); break;
3437 3396
     #endif
3438 3397
 
3439 3398
     #if HAS_TEMP_ADC_2
3440
-      case PrepareTemp_2: HAL_START_ADC(TEMP_2_PIN); break;
3399
+      case PrepareTemp_2: hal.adc_start(TEMP_2_PIN); break;
3441 3400
       case MeasureTemp_2: ACCUMULATE_ADC(temp_hotend[2]); break;
3442 3401
     #endif
3443 3402
 
3444 3403
     #if HAS_TEMP_ADC_3
3445
-      case PrepareTemp_3: HAL_START_ADC(TEMP_3_PIN); break;
3404
+      case PrepareTemp_3: hal.adc_start(TEMP_3_PIN); break;
3446 3405
       case MeasureTemp_3: ACCUMULATE_ADC(temp_hotend[3]); break;
3447 3406
     #endif
3448 3407
 
3449 3408
     #if HAS_TEMP_ADC_4
3450
-      case PrepareTemp_4: HAL_START_ADC(TEMP_4_PIN); break;
3409
+      case PrepareTemp_4: hal.adc_start(TEMP_4_PIN); break;
3451 3410
       case MeasureTemp_4: ACCUMULATE_ADC(temp_hotend[4]); break;
3452 3411
     #endif
3453 3412
 
3454 3413
     #if HAS_TEMP_ADC_5
3455
-      case PrepareTemp_5: HAL_START_ADC(TEMP_5_PIN); break;
3414
+      case PrepareTemp_5: hal.adc_start(TEMP_5_PIN); break;
3456 3415
       case MeasureTemp_5: ACCUMULATE_ADC(temp_hotend[5]); break;
3457 3416
     #endif
3458 3417
 
3459 3418
     #if HAS_TEMP_ADC_6
3460
-      case PrepareTemp_6: HAL_START_ADC(TEMP_6_PIN); break;
3419
+      case PrepareTemp_6: hal.adc_start(TEMP_6_PIN); break;
3461 3420
       case MeasureTemp_6: ACCUMULATE_ADC(temp_hotend[6]); break;
3462 3421
     #endif
3463 3422
 
3464 3423
     #if HAS_TEMP_ADC_7
3465
-      case PrepareTemp_7: HAL_START_ADC(TEMP_7_PIN); break;
3424
+      case PrepareTemp_7: hal.adc_start(TEMP_7_PIN); break;
3466 3425
       case MeasureTemp_7: ACCUMULATE_ADC(temp_hotend[7]); break;
3467 3426
     #endif
3468 3427
 
3469 3428
     #if ENABLED(FILAMENT_WIDTH_SENSOR)
3470
-      case Prepare_FILWIDTH: HAL_START_ADC(FILWIDTH_PIN); break;
3429
+      case Prepare_FILWIDTH: hal.adc_start(FILWIDTH_PIN); break;
3471 3430
       case Measure_FILWIDTH:
3472
-        if (!HAL_ADC_READY()) next_sensor_state = adc_sensor_state; // Redo this state
3473
-        else filwidth.accumulate(HAL_READ_ADC());
3431
+        if (!hal.adc_ready()) next_sensor_state = adc_sensor_state; // Redo this state
3432
+        else filwidth.accumulate(hal.adc_value());
3474 3433
       break;
3475 3434
     #endif
3476 3435
 
3477 3436
     #if ENABLED(POWER_MONITOR_CURRENT)
3478 3437
       case Prepare_POWER_MONITOR_CURRENT:
3479
-        HAL_START_ADC(POWER_MONITOR_CURRENT_PIN);
3438
+        hal.adc_start(POWER_MONITOR_CURRENT_PIN);
3480 3439
         break;
3481 3440
       case Measure_POWER_MONITOR_CURRENT:
3482
-        if (!HAL_ADC_READY()) next_sensor_state = adc_sensor_state; // Redo this state
3483
-        else power_monitor.add_current_sample(HAL_READ_ADC());
3441
+        if (!hal.adc_ready()) next_sensor_state = adc_sensor_state; // Redo this state
3442
+        else power_monitor.add_current_sample(hal.adc_value());
3484 3443
         break;
3485 3444
     #endif
3486 3445
 
3487 3446
     #if ENABLED(POWER_MONITOR_VOLTAGE)
3488 3447
       case Prepare_POWER_MONITOR_VOLTAGE:
3489
-        HAL_START_ADC(POWER_MONITOR_VOLTAGE_PIN);
3448
+        hal.adc_start(POWER_MONITOR_VOLTAGE_PIN);
3490 3449
         break;
3491 3450
       case Measure_POWER_MONITOR_VOLTAGE:
3492
-        if (!HAL_ADC_READY()) next_sensor_state = adc_sensor_state; // Redo this state
3493
-        else power_monitor.add_voltage_sample(HAL_READ_ADC());
3451
+        if (!hal.adc_ready()) next_sensor_state = adc_sensor_state; // Redo this state
3452
+        else power_monitor.add_voltage_sample(hal.adc_value());
3494 3453
         break;
3495 3454
     #endif
3496 3455
 
3497 3456
     #if HAS_JOY_ADC_X
3498
-      case PrepareJoy_X: HAL_START_ADC(JOY_X_PIN); break;
3457
+      case PrepareJoy_X: hal.adc_start(JOY_X_PIN); break;
3499 3458
       case MeasureJoy_X: ACCUMULATE_ADC(joystick.x); break;
3500 3459
     #endif
3501 3460
 
3502 3461
     #if HAS_JOY_ADC_Y
3503
-      case PrepareJoy_Y: HAL_START_ADC(JOY_Y_PIN); break;
3462
+      case PrepareJoy_Y: hal.adc_start(JOY_Y_PIN); break;
3504 3463
       case MeasureJoy_Y: ACCUMULATE_ADC(joystick.y); break;
3505 3464
     #endif
3506 3465
 
3507 3466
     #if HAS_JOY_ADC_Z
3508
-      case PrepareJoy_Z: HAL_START_ADC(JOY_Z_PIN); break;
3467
+      case PrepareJoy_Z: hal.adc_start(JOY_Z_PIN); break;
3509 3468
       case MeasureJoy_Z: ACCUMULATE_ADC(joystick.z); break;
3510 3469
     #endif
3511 3470
 
@@ -3513,12 +3472,12 @@ void Temperature::isr() {
3513 3472
       #ifndef ADC_BUTTON_DEBOUNCE_DELAY
3514 3473
         #define ADC_BUTTON_DEBOUNCE_DELAY 16
3515 3474
       #endif
3516
-      case Prepare_ADC_KEY: HAL_START_ADC(ADC_KEYPAD_PIN); break;
3475
+      case Prepare_ADC_KEY: hal.adc_start(ADC_KEYPAD_PIN); break;
3517 3476
       case Measure_ADC_KEY:
3518
-        if (!HAL_ADC_READY())
3477
+        if (!hal.adc_ready())
3519 3478
           next_sensor_state = adc_sensor_state; // redo this state
3520 3479
         else if (ADCKey_count < ADC_BUTTON_DEBOUNCE_DELAY) {
3521
-          raw_ADCKey_value = HAL_READ_ADC();
3480
+          raw_ADCKey_value = hal.adc_value();
3522 3481
           if (raw_ADCKey_value <= 900UL * HAL_ADC_RANGE / 1024UL) {
3523 3482
             NOMORE(current_ADCKey_raw, raw_ADCKey_value);
3524 3483
             ADCKey_count++;

+ 1
- 1
ini/features.ini Vedi File

@@ -233,7 +233,7 @@ HAS_SERVOS                             = src_filter=+<src/module/servo.cpp> +<sr
233 233
 MORGAN_SCARA                           = src_filter=+<src/gcode/scara>
234 234
 HAS_MICROSTEPS                         = src_filter=+<src/gcode/control/M350_M351.cpp>
235 235
 (ESP3D_)?WIFISUPPORT                   = AsyncTCP, ESP Async WebServer
236
-                                         ESP3DLib=https://github.com/luc-github/ESP3DLib/archive/master.zip
236
+                                         ESP3DLib=https://github.com/luc-github/ESP3DLib/archive/master-2.0.7.zip
237 237
                                          arduinoWebSockets=links2004/WebSockets@2.3.4
238 238
                                          luc-github/ESP32SSDP@^1.1.1
239 239
                                          lib_ignore=ESPAsyncTCP

+ 2
- 2
ini/native.ini Vedi File

@@ -34,14 +34,14 @@ src_filter      = ${common.default_src_filter} +<src/HAL/LINUX>
34 34
 [simulator_common]
35 35
 platform          = native
36 36
 framework         =
37
-build_flags       = ${common.build_flags} -std=gnu++17 -D__PLAT_NATIVE_SIM__ -DU8G_HAL_LINKS -I/usr/include/SDL2 -IMarlin -IMarlin/src/HAL/NATIVE_SIM/include -IMarlin/src/HAL/NATIVE_SIM/u8g
37
+build_flags       = ${common.build_flags} -std=gnu++17 -D__PLAT_NATIVE_SIM__ -DU8G_HAL_LINKS -I/usr/include/SDL2 -IMarlin -IMarlin/src/HAL/NATIVE_SIM/u8g
38 38
 src_build_flags   = -Wall -Wno-expansion-to-defined -Wcast-align
39 39
 release_flags     = -g0 -O3 -flto
40 40
 debug_build_flags = -fstack-protector-strong -g -g3 -ggdb
41 41
 lib_compat_mode   = off
42 42
 src_filter        = ${common.default_src_filter} +<src/HAL/NATIVE_SIM>
43 43
 lib_deps          = ${common.lib_deps}
44
-  MarlinSimUI=https://github.com/p3p/MarlinSimUI/archive/master.zip
44
+  MarlinSimUI=https://github.com/p3p/MarlinSimUI/archive/0.0.2.zip
45 45
   Adafruit NeoPixel=https://github.com/p3p/Adafruit_NeoPixel/archive/marlin_sim_native.zip
46 46
   LiquidCrystal=https://github.com/p3p/LiquidCrystal/archive/master.zip
47 47
 extra_scripts = ${common.extra_scripts}

Loading…
Annulla
Salva