Преглед на файлове

♻️ Refactor HAL as singleton (#23295)

Scott Lahteine преди 2 години
родител
ревизия
e211ff148c
No account linked to committer's email address
променени са 69 файла, в които са добавени 1756 реда и са изтрити 1267 реда
  1. 7
    7
      Marlin/src/HAL/AVR/HAL.cpp
  2. 103
    67
      Marlin/src/HAL/AVR/HAL.h
  3. 2
    2
      Marlin/src/HAL/AVR/MarlinSerial.cpp
  4. 5
    6
      Marlin/src/HAL/AVR/fast_pwm.cpp
  5. 2
    2
      Marlin/src/HAL/AVR/timers.h
  6. 11
    34
      Marlin/src/HAL/DUE/HAL.cpp
  7. 87
    45
      Marlin/src/HAL/DUE/HAL.h
  8. 2
    2
      Marlin/src/HAL/DUE/MarlinSerial.cpp
  9. 1
    1
      Marlin/src/HAL/DUE/timers.h
  10. 17
    18
      Marlin/src/HAL/ESP32/HAL.cpp
  11. 88
    54
      Marlin/src/HAL/ESP32/HAL.h
  12. 2
    2
      Marlin/src/HAL/ESP32/timers.h
  13. 12
    27
      Marlin/src/HAL/LINUX/HAL.cpp
  14. 96
    49
      Marlin/src/HAL/LINUX/HAL.h
  15. 1
    3
      Marlin/src/HAL/LINUX/arduino.cpp
  16. 2
    3
      Marlin/src/HAL/LINUX/include/Arduino.h
  17. 2
    2
      Marlin/src/HAL/LINUX/timers.h
  18. 20
    22
      Marlin/src/HAL/LPC1768/HAL.cpp
  19. 102
    58
      Marlin/src/HAL/LPC1768/HAL.h
  20. 2
    1
      Marlin/src/HAL/LPC1768/Servo.h
  21. 5
    9
      Marlin/src/HAL/LPC1768/fast_pwm.cpp
  22. 3
    3
      Marlin/src/HAL/LPC1768/main.cpp
  23. 1
    1
      Marlin/src/HAL/LPC1768/timers.h
  24. 108
    65
      Marlin/src/HAL/NATIVE_SIM/HAL.h
  25. 2
    2
      Marlin/src/HAL/NATIVE_SIM/timers.h
  26. 9
    17
      Marlin/src/HAL/SAMD51/HAL.cpp
  27. 81
    44
      Marlin/src/HAL/SAMD51/HAL.h
  28. 10
    18
      Marlin/src/HAL/STM32/HAL.cpp
  29. 103
    76
      Marlin/src/HAL/STM32/HAL.h
  30. 4
    4
      Marlin/src/HAL/STM32/HAL_SPI.cpp
  31. 4
    4
      Marlin/src/HAL/STM32/eeprom_flash.cpp
  32. 2
    2
      Marlin/src/HAL/STM32/fast_pwm.cpp
  33. 5
    1
      Marlin/src/HAL/STM32/pinsDebug.h
  34. 2
    2
      Marlin/src/HAL/STM32/timers.h
  35. 91
    99
      Marlin/src/HAL/STM32F1/HAL.cpp
  36. 105
    79
      Marlin/src/HAL/STM32F1/HAL.h
  37. 2
    1
      Marlin/src/HAL/STM32F1/Servo.h
  38. 3
    5
      Marlin/src/HAL/STM32F1/fast_pwm.cpp
  39. 1
    1
      Marlin/src/HAL/STM32F1/timers.h
  40. 41
    38
      Marlin/src/HAL/TEENSY31_32/HAL.cpp
  41. 94
    37
      Marlin/src/HAL/TEENSY31_32/HAL.h
  42. 1
    1
      Marlin/src/HAL/TEENSY31_32/timers.h
  43. 57
    53
      Marlin/src/HAL/TEENSY35_36/HAL.cpp
  44. 95
    38
      Marlin/src/HAL/TEENSY35_36/HAL.h
  45. 1
    1
      Marlin/src/HAL/TEENSY35_36/timers.h
  46. 100
    94
      Marlin/src/HAL/TEENSY40_41/HAL.cpp
  47. 102
    46
      Marlin/src/HAL/TEENSY40_41/HAL.h
  48. 1
    1
      Marlin/src/HAL/TEENSY40_41/timers.h
  49. 36
    0
      Marlin/src/HAL/shared/HAL.cpp
  50. 4
    4
      Marlin/src/HAL/shared/HAL_spi_L6470.cpp
  51. 21
    27
      Marlin/src/MarlinCore.cpp
  52. 1
    1
      Marlin/src/feature/caselight.cpp
  53. 1
    1
      Marlin/src/feature/controllerfan.cpp
  54. 3
    1
      Marlin/src/feature/e_parser.h
  55. 1
    1
      Marlin/src/feature/leds/leds.cpp
  56. 5
    5
      Marlin/src/feature/spindle_laser.cpp
  57. 1
    1
      Marlin/src/feature/spindle_laser.h
  58. 2
    2
      Marlin/src/gcode/control/M42.cpp
  59. 5
    5
      Marlin/src/gcode/gcode_d.cpp
  60. 7
    0
      Marlin/src/inc/SanityCheck.h
  61. 2
    2
      Marlin/src/lcd/dogm/marlinui_DOGM.cpp
  62. 1
    1
      Marlin/src/lcd/e3v2/enhanced/dwin.cpp
  63. 1
    1
      Marlin/src/module/endstops.cpp
  64. 3
    3
      Marlin/src/module/planner.cpp
  65. 1
    1
      Marlin/src/module/servo.cpp
  66. 1
    1
      Marlin/src/module/servo.h
  67. 5
    5
      Marlin/src/module/stepper.cpp
  68. 56
    56
      Marlin/src/module/temperature.cpp
  69. 2
    2
      ini/native.ini

+ 7
- 7
Marlin/src/HAL/AVR/HAL.cpp Целия файл

@@ -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
@@ -77,7 +77,7 @@ void HAL_init() {
77 77
   #endif
78 78
 }
79 79
 
80
-void HAL_reboot() {
80
+void MarlinHAL::reboot() {
81 81
   #if ENABLED(USE_WATCHDOG)
82 82
     while (1) { /* run out the watchdog */ }
83 83
   #else

+ 103
- 67
Marlin/src/HAL/AVR/HAL.h Целия файл

@@ -74,9 +74,8 @@
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
80 79
 
81 80
 // ------------------------
82 81
 // Types
@@ -84,16 +83,16 @@
84 83
 
85 84
 typedef int8_t pin_t;
86 85
 
86
+// Use shared/servos.cpp
87 87
 #define SHARED_SERVOS HAS_SERVOS
88
-#define HAL_SERVO_LIB Servo
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,63 +176,89 @@ 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 inline 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
+  static inline bool isr_state() { return TEST(SREG, SREG_I); }
194
+  static inline void isr_on()  { sei(); }
195
+  static inline void isr_off() { cli(); }
199 196
 
200
-#define HAL_SENSITIVE_PINS 0, 1,
197
+  static inline void delay_ms(const int ms) { _delay_ms(ms); }
201 198
 
202
-#ifdef __AVR_AT90USB1286__
203
-  #define JTAG_DISABLE() do{ MCUCR = 0x80; MCUCR = 0x80; }while(0)
204
-#endif
199
+  // Tasks, called from idle()
200
+  static inline void idletask() {}
205 201
 
206
-// AVR compatibility
207
-#define strtof strtod
202
+  // Reset
203
+  static uint8_t reset_reason;
204
+  static inline uint8_t get_reset_source() { return reset_reason; }
205
+  static inline void clear_reset_source() { MCUSR = 0; }
208 206
 
209
-#define HAL_CAN_SET_PWM_FREQ   // This HAL supports PWM Frequency adjustment
207
+  // Free SRAM
208
+  static inline int freeMemory() { return freeMemory(); }
210 209
 
211
-/**
212
- *  set_pwm_frequency
213
- *  Sets the frequency of the timer corresponding to the provided pin
214
- *  as close as possible to the provided desired frequency. Internally
215
- *  calculates the required waveform generation mode, prescaler and
216
- *  resolution values required and sets the timer registers accordingly.
217
- *  NOTE that the frequency is applied to all pins on the timer (Ex OC3A, OC3B and OC3B)
218
- *  NOTE that there are limitations, particularly if using TIMER2. (see Configuration_adv.h -> FAST FAN PWM Settings)
219
- */
220
-void set_pwm_frequency(const pin_t pin, int f_desired);
210
+  //
211
+  // ADC Methods
212
+  //
221 213
 
222
-/**
223
- * set_pwm_duty
224
- *  Set the PWM duty cycle of the provided pin to the provided value
225
- *  Optionally allows inverting the duty cycle [default = false]
226
- *  Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
227
- */
228
-void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);
214
+  // Called by Temperature::init once at startup
215
+  static inline void adc_init() {
216
+    ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
217
+    DIDR0 = 0;
218
+    #ifdef DIDR2
219
+      DIDR2 = 0;
220
+    #endif
221
+  }
222
+
223
+  // Called by Temperature::init for each sensor at startup
224
+  static inline void adc_enable(const uint8_t ch) {
225
+    #ifdef DIDR2
226
+      if (ch > 7) { SBI(DIDR2, ch & 0x07); return; }
227
+    #endif
228
+    SBI(DIDR0, ch);
229
+  }
230
+
231
+  // Begin ADC sampling on the given channel
232
+  static inline void adc_start(const pin_t ch) {
233
+    #ifdef MUX5
234
+      if (ch > 7) { ADCSRB = _BV(MUX5); return; }
235
+    #endif
236
+    ADCSRB = 0;
237
+    ADMUX = _BV(REFS0) | (ch & 0x07); SBI(ADCSRA, ADSC);
238
+  }
239
+
240
+  // Is the ADC ready for reading?
241
+  static inline bool adc_ready() { return !TEST(ADCSRA, ADSC); }
242
+
243
+  // The current value of the ADC register
244
+  static inline __typeof__(ADC) adc_value() { return ADC; }
245
+
246
+  /**
247
+   * Set the PWM duty cycle for the pin to the given value.
248
+   * Optionally invert the duty cycle [default = false]
249
+   * Optionally change the scale of the provided value to enable finer PWM duty control [default = 255]
250
+   */
251
+  static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);
252
+
253
+  /**
254
+   * Set the frequency of the timer for the given pin as close as
255
+   * possible to the provided desired frequency. Internally calculate
256
+   * the required waveform generation mode, prescaler, and resolution
257
+   * values and set timer registers accordingly.
258
+   * NOTE that the frequency is applied to all pins on the timer (Ex OC3A, OC3B and OC3B)
259
+   * NOTE that there are limitations, particularly if using TIMER2. (see Configuration_adv.h -> FAST FAN PWM Settings)
260
+   */
261
+  static void set_pwm_frequency(const pin_t pin, int f_desired);
262
+};
263
+
264
+extern MarlinHAL hal;

+ 2
- 2
Marlin/src/HAL/AVR/MarlinSerial.cpp Целия файл

@@ -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) {

+ 5
- 6
Marlin/src/HAL/AVR/fast_pwm.cpp Целия файл

@@ -35,10 +35,9 @@ struct Timer {
35 35
 };
36 36
 
37 37
 /**
38
- * get_pwm_timer
39
- *  Get the timer information and register of the provided pin.
40
- *  Return a Timer struct containing this information.
41
- *  Used by set_pwm_frequency, set_pwm_duty
38
+ * Get the timer information and register for a pin.
39
+ * Return a Timer struct containing this information.
40
+ * Used by set_pwm_frequency, set_pwm_duty
42 41
  */
43 42
 Timer get_pwm_timer(const pin_t pin) {
44 43
   uint8_t q = 0;
@@ -150,7 +149,7 @@ Timer get_pwm_timer(const pin_t pin) {
150 149
   return timer;
151 150
 }
152 151
 
153
-void set_pwm_frequency(const pin_t pin, int f_desired) {
152
+void MarlinHAL::set_pwm_frequency(const pin_t pin, int f_desired) {
154 153
   Timer timer = get_pwm_timer(pin);
155 154
   if (timer.n == 0) return; // Don't proceed if protected timer or not recognized
156 155
   uint16_t size;
@@ -230,7 +229,7 @@ void set_pwm_frequency(const pin_t pin, int f_desired) {
230 229
 
231 230
 #endif // NEEDS_HARDWARE_PWM
232 231
 
233
-void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
232
+void MarlinHAL::set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
234 233
   #if NEEDS_HARDWARE_PWM
235 234
 
236 235
     // If v is 0 or v_size (max), digitalWrite to LOW or HIGH.

+ 2
- 2
Marlin/src/HAL/AVR/timers.h Целия файл

@@ -109,8 +109,8 @@ 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 115
 /* 18 cycles maximum latency */
116 116
 #ifndef HAL_STEP_TIMER_ISR

+ 11
- 34
Marlin/src/HAL/DUE/HAL.cpp Целия файл

@@ -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,17 @@ 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() {
61
+  usb_task_idle();    // Perform USB stack housekeeping
62
+}
68 63
 
69
-uint8_t HAL_get_reset_source() {
64
+uint8_t MarlinHAL::get_reset_source() {
70 65
   switch ((RSTC->RSTC_SR >> 8) & 0x07) {
71 66
     case 0: return RST_POWER_ON;
72 67
     case 1: return RST_BACKUP;
@@ -77,12 +72,7 @@ uint8_t HAL_get_reset_source() {
77 72
   }
78 73
 }
79 74
 
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
-}
75
+void MarlinHAL::reboot() { rstc_start_software_reset(RSTC); }
86 76
 
87 77
 extern "C" {
88 78
   extern unsigned int _ebss; // end of bss section
@@ -94,19 +84,6 @@ int freeMemory() {
94 84
   return (int)&free_memory - (heap_end ?: (int)&_ebss);
95 85
 }
96 86
 
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 87
 // Forward the default serial ports
111 88
 #if USING_HW_SERIAL0
112 89
   DefaultSerial1 MSerial0(false, Serial);

+ 87
- 45
Marlin/src/HAL/DUE/HAL.h Целия файл

@@ -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,35 +101,31 @@ 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
 
110
+// Use shared/servos.cpp
105 111
 #define SHARED_SERVOS HAS_SERVOS
106
-#define HAL_SERVO_LIB Servo
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
119
-
120
-void HAL_clear_reset_source();  // clear reset reason
121
-uint8_t HAL_get_reset_source(); // get reset reason
118
+#define sei() noInterrupts()
119
+#define cli() interrupts()
122 120
 
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)
@@ -133,24 +133,8 @@ extern uint16_t HAL_adc_result;     // result of last ADC conversion
133 133
 
134 134
 #define HAL_ANALOG_SELECT(ch)
135 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 136
 //
148
-// PWM
149
-//
150
-inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); }
151
-
152
-//
153
-// Pin Map
137
+// Pin Mapping for M42, M43, M226
154 138
 //
155 139
 #define GET_PIN_MAP_PIN(index) index
156 140
 #define GET_PIN_MAP_INDEX(pin) pin
@@ -159,27 +143,18 @@ inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255,
159 143
 //
160 144
 // Tone
161 145
 //
162
-void toneInit();
163 146
 void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration=0);
164 147
 void noTone(const pin_t _pin);
165 148
 
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);
149
+// ------------------------
150
+// Class Utilities
151
+// ------------------------
175 152
 
176 153
 #pragma GCC diagnostic push
177 154
 #if GCC_VERSION <= 50000
178 155
   #pragma GCC diagnostic ignored "-Wunused-function"
179 156
 #endif
180 157
 
181
-int freeMemory();
182
-
183 158
 #pragma GCC diagnostic pop
184 159
 
185 160
 #ifdef __cplusplus
@@ -189,3 +164,70 @@ char *dtostrf(double __val, signed char __width, unsigned char __prec, char *__s
189 164
 #ifdef __cplusplus
190 165
   }
191 166
 #endif
167
+
168
+// Return free RAM between end of heap (or end bss) and whatever is current
169
+int freeMemory();
170
+
171
+// ------------------------
172
+// MarlinHAL Class
173
+// ------------------------
174
+
175
+class MarlinHAL {
176
+public:
177
+
178
+  // Earliest possible init, before setup()
179
+  MarlinHAL() {}
180
+
181
+  static void init();       // Called early in setup()
182
+  static void init_board(); // Called less early in setup()
183
+  static void reboot();     // Software reset
184
+
185
+  static inline bool isr_state() { return !__get_PRIMASK(); }
186
+  static inline void isr_on()  { __enable_irq(); }
187
+  static inline void isr_off() { __disable_irq(); }
188
+
189
+  static inline void delay_ms(const int ms) { delay(ms); }
190
+
191
+  // Tasks, called from idle()
192
+  static void idletask();
193
+
194
+  // Reset
195
+  static uint8_t get_reset_source();
196
+  static inline void clear_reset_source() {}
197
+
198
+  // Free SRAM
199
+  static inline int freeMemory() { return freeMemory(); }
200
+
201
+  //
202
+  // ADC Methods
203
+  //
204
+
205
+  static uint16_t adc_result;
206
+
207
+  // Called by Temperature::init once at startup
208
+  static inline void adc_init() {}
209
+
210
+  // Called by Temperature::init for each sensor at startup
211
+  static inline void adc_enable(const int ch) {}
212
+
213
+  // Begin ADC sampling on the given channel
214
+  static inline void adc_start(const uint8_t ch) { adc_result = analogRead(ch); }
215
+
216
+  // Is the ADC ready for reading?
217
+  static inline bool adc_ready() { return true; }
218
+
219
+  // The current value of the ADC register
220
+  static inline uint16_t adc_value() { return adc_result; }
221
+
222
+  /**
223
+   * Set the PWM duty cycle for the pin to the given value.
224
+   * No inverting the duty cycle in this HAL.
225
+   * No changing the maximum size of the provided value to enable finer PWM duty control in this HAL.
226
+   */
227
+  static inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) {
228
+    analogWrite(pin, v);
229
+  }
230
+
231
+};
232
+
233
+extern MarlinHAL hal;

+ 2
- 2
Marlin/src/HAL/DUE/MarlinSerial.cpp Целия файл

@@ -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/timers.h Целия файл

@@ -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 Целия файл

@@ -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
 
@@ -226,11 +225,11 @@ void HAL_adc_init() {
226 225
   }
227 226
 }
228 227
 
229
-void HAL_adc_start_conversion(const uint8_t adc_pin) {
230
-  const adc1_channel_t chan = get_channel(adc_pin);
228
+void MarlinHAL::adc_start(const pin_t pin) {
229
+  const adc1_channel_t chan = get_channel(pin);
231 230
   uint32_t mv;
232 231
   esp_adc_cal_get_voltage((adc_channel_t)chan, &characteristics[attenuations[chan]], &mv);
233
-  HAL_adc_result = mv * 1023.0 / 3300.0;
232
+  adc_result = mv * 1023.0 / 3300.0;
234 233
 
235 234
   // Change the attenuation level based on the new reading
236 235
   adc_atten_t atten;

+ 88
- 54
Marlin/src/HAL/ESP32/HAL.h Целия файл

@@ -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,21 @@ 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 88
 // ADC
120 89
 #define HAL_ANALOG_SELECT(pin)
121 90
 
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
91
+//
92
+// Pin Mapping for M42, M43, M226
93
+//
136 94
 #define GET_PIN_MAP_PIN(index) index
137 95
 #define GET_PIN_MAP_INDEX(pin) pin
138 96
 #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
139 97
 
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 98
 #if ENABLED(USE_ESP32_EXIO)
148 99
   void Write_EXIO(uint8_t IO, uint8_t v);
149 100
 #endif
@@ -188,3 +139,86 @@ FORCE_INLINE static void DELAY_CYCLES(uint32_t x) {
188 139
   }
189 140
 
190 141
 }
142
+
143
+// ------------------------
144
+// Class Utilities
145
+// ------------------------
146
+
147
+#pragma GCC diagnostic push
148
+#if GCC_VERSION <= 50000
149
+  #pragma GCC diagnostic ignored "-Wunused-function"
150
+#endif
151
+
152
+int freeMemory();
153
+
154
+#pragma GCC diagnostic pop
155
+
156
+void _delay_ms(const int ms);
157
+
158
+// ------------------------
159
+// MarlinHAL Class
160
+// ------------------------
161
+
162
+#define HAL_ADC_VREF         3.3
163
+#define HAL_ADC_RESOLUTION  10
164
+
165
+class MarlinHAL {
166
+public:
167
+
168
+  // Earliest possible init, before setup()
169
+  MarlinHAL() {}
170
+
171
+  static inline void init() {}  // Called early in setup()
172
+  static void init_board();     // Called less early in setup()
173
+  static void reboot();         // Restart the firmware
174
+
175
+  static portMUX_TYPE spinlock;
176
+  static inline bool isr_state() { return spinlock.owner == portMUX_FREE_VAL; }
177
+  static inline void isr_on()  { if (spinlock.owner != portMUX_FREE_VAL) portEXIT_CRITICAL(&spinlock); }
178
+  static inline void isr_off() { portENTER_CRITICAL(&spinlock); }
179
+
180
+  static inline void delay_ms(const int ms) { _delay_ms(ms); }
181
+
182
+  // Tasks, called from idle()
183
+  static void idletask();
184
+
185
+  // Reset
186
+  static uint8_t get_reset_source();
187
+  static inline void clear_reset_source() {}
188
+
189
+  // Free SRAM
190
+  static int freeMemory();
191
+
192
+  //
193
+  // ADC Methods
194
+  //
195
+
196
+  static uint16_t adc_result;
197
+
198
+  // Called by Temperature::init once at startup
199
+  static void adc_init();
200
+
201
+  // Called by Temperature::init for each sensor at startup
202
+  static inline void adc_enable(const pin_t pin) {}
203
+
204
+  // Begin ADC sampling on the given channel
205
+  static void adc_start(const pin_t pin);
206
+
207
+  // Is the ADC ready for reading?
208
+  static inline bool adc_ready() { return true; }
209
+
210
+  // The current value of the ADC register
211
+  static inline uint16_t adc_value() { return adc_result; }
212
+
213
+  /**
214
+   * Set the PWM duty cycle for the pin to the given value.
215
+   * No inverting the duty cycle in this HAL.
216
+   * No changing the maximum size of the provided value to enable finer PWM duty control in this HAL.
217
+   */
218
+  static inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) {
219
+    analogWrite(pin, v);
220
+  }
221
+
222
+};
223
+
224
+extern MarlinHAL hal;

+ 2
- 2
Marlin/src/HAL/ESP32/timers.h Целия файл

@@ -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

+ 12
- 27
Marlin/src/HAL/LINUX/HAL.cpp Целия файл

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

+ 96
- 49
Marlin/src/HAL/LINUX/HAL.h Целия файл

@@ -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,28 @@ 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
+#define HAL_ANALOG_SELECT(ch) hal.adc_enable(ch)
86
+
87
+// ------------------------
88
+// Class Utilities
89
+// ------------------------
80 90
 
81
-// Utility functions
82 91
 #pragma GCC diagnostic push
83 92
 #if GCC_VERSION <= 50000
84 93
   #pragma GCC diagnostic ignored "-Wunused-function"
@@ -88,29 +97,67 @@ int freeMemory();
88 97
 
89 98
 #pragma GCC diagnostic pop
90 99
 
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
100
+// ------------------------
101
+// MarlinHAL Class
102
+// ------------------------
103
+
104
+class MarlinHAL {
105
+public:
106
+
107
+  // Earliest possible init, before setup()
108
+  MarlinHAL() {}
109
+
110
+  static inline void init() {}        // Called early in setup()
111
+  static inline void init_board() {}  // Called less early in setup()
112
+  static void reboot();               // Reset the application state and GPIO
113
+
114
+  static inline bool isr_state() { return true; }
115
+  static inline void isr_on()  {}
116
+  static inline void isr_off() {}
117
+
118
+  static inline void delay_ms(const int ms) { _delay_ms(ms); }
119
+
120
+  // Tasks, called from idle()
121
+  static inline void idletask() {}
122
+
123
+  // Reset
124
+  static constexpr uint8_t reset_reason = RST_POWER_ON;
125
+  static inline uint8_t get_reset_source() { return reset_reason; }
126
+  static inline void clear_reset_source() {}
127
+
128
+  // Free SRAM
129
+  static inline 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 inline void adc_init() {}
139
+
140
+  // Called by Temperature::init for each sensor at startup
141
+  static inline void adc_enable(const uint8_t) {}
142
+
143
+  // Begin ADC sampling on the given channel
144
+  static inline void adc_start(const pin_t ch) { active_ch = ch; }
98 145
 
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();
146
+  // Is the ADC ready for reading?
147
+  static inline bool adc_ready() { return true; }
103 148
 
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); }
149
+  // The current value of the ADC register
150
+  static uint16_t adc_value();
106 151
 
107
-// Reset source
108
-inline void HAL_clear_reset_source(void) {}
109
-inline uint8_t HAL_get_reset_source(void) { return RST_POWER_ON; }
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 inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) {
157
+    analogWrite(pin, v);
158
+  }
110 159
 
111
-void HAL_reboot(); // Reset the application state and GPIO
160
+  static inline void set_pwm_frequency(const pin_t, int) {}
161
+};
112 162
 
113
-/* ---------------- Delay in cycles */
114
-FORCE_INLINE static void DELAY_CYCLES(uint64_t x) {
115
-  Clock::delayCycles(x);
116
-}
163
+extern MarlinHAL hal;

+ 1
- 3
Marlin/src/HAL/LINUX/arduino.cpp Целия файл

@@ -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();

+ 2
- 3
Marlin/src/HAL/LINUX/include/Arduino.h Целия файл

@@ -59,7 +59,6 @@ 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 64
 //Interrupts
@@ -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 Целия файл

@@ -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 Целия файл

@@ -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

+ 102
- 58
Marlin/src/HAL/LPC1768/HAL.h Целия файл

@@ -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,11 @@ 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
-}
136
+#define HAL_ANALOG_SELECT(pin) hal.adc_enable(pin)
163 137
 
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)
138
+//
139
+// Pin Mapping for M42, M43, M226
140
+//
169 141
 
170 142
 // Test whether the pin is valid
171 143
 constexpr bool VALID_PIN(const pin_t pin) {
@@ -192,32 +164,104 @@ int16_t PARSED_PIN_INDEX(const char code, const int16_t dval);
192 164
 // P0.6 thru P0.9 are for the onboard SD card
193 165
 #define HAL_SENSITIVE_PINS P0_06, P0_07, P0_08, P0_09,
194 166
 
195
-#define HAL_IDLETASK 1
196
-void HAL_idletask();
167
+// ------------------------
168
+// Defines
169
+// ------------------------
197 170
 
198 171
 #define PLATFORM_M997_SUPPORT
199 172
 void flashFirmware(const int16_t);
200 173
 
201 174
 #define HAL_CAN_SET_PWM_FREQ   // This HAL supports PWM Frequency adjustment
202 175
 
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, int f_desired);
176
+// Default graphical display delays
177
+#define CPU_ST7920_DELAY_1 600
178
+#define CPU_ST7920_DELAY_2 750
179
+#define CPU_ST7920_DELAY_3 750
210 180
 
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);
181
+// ------------------------
182
+// Class Utilities
183
+// ------------------------
184
+
185
+#pragma GCC diagnostic push
186
+#if GCC_VERSION <= 50000
187
+  #pragma GCC diagnostic ignored "-Wunused-function"
188
+#endif
189
+
190
+int freeMemory();
191
+
192
+#pragma GCC diagnostic pop
193
+
194
+// ------------------------
195
+// MarlinHAL Class
196
+// ------------------------
197
+
198
+class MarlinHAL {
199
+public:
200
+
201
+  // Earliest possible init, before setup()
202
+  MarlinHAL() {}
203
+
204
+  static void init();                 // Called early in setup()
205
+  static inline void init_board() {}  // Called less early in setup()
206
+  static void reboot();               // Restart the firmware from 0x0
207
+
208
+  static inline bool isr_state() { return !__get_PRIMASK(); }
209
+  static inline void isr_on()  { __enable_irq(); }
210
+  static inline void isr_off() { __disable_irq(); }
211
+
212
+  static inline void delay_ms(const int ms) { _delay_ms(ms); }
213
+
214
+  // Tasks, called from idle()
215
+  static void idletask();
216
+
217
+  // Reset
218
+  static uint8_t get_reset_source();
219
+  static void clear_reset_source();
220
+
221
+  // Free SRAM
222
+  static inline int freeMemory() { return freeMemory(); }
223
+
224
+  //
225
+  // ADC Methods
226
+  //
227
+
228
+  using FilteredADC = LPC176x::ADC<ADC_LOWPASS_K_VALUE, ADC_MEDIAN_FILTER_SIZE>;
229
+
230
+  // Called by Temperature::init once at startup
231
+  static inline void adc_init() {}
232
+
233
+  // Called by Temperature::init for each sensor at startup
234
+  static inline void adc_enable(const pin_t pin) {
235
+    FilteredADC::enable_channel(pin);
236
+  }
237
+
238
+  // Begin ADC sampling on the given pin
239
+  static uint32_t adc_result;
240
+  FORCE_INLINE static void adc_start(const pin_t pin) {
241
+    adc_result = FilteredADC::read(pin) >> (16 - HAL_ADC_RESOLUTION); // returns 16bit value, reduce to required bits
242
+  }
243
+
244
+  // Is the ADC ready for reading?
245
+  static inline bool adc_ready() { return true; }
246
+
247
+  // The current value of the ADC register
248
+  FORCE_INLINE static uint16_t adc_value() {
249
+    return (uint16_t)adc_result;
250
+  }
251
+
252
+  /**
253
+   * Set the PWM duty cycle for the pin to the given value.
254
+   * Optionally invert the duty cycle [default = false]
255
+   * Optionally change the scale of the provided value to enable finer PWM duty control [default = 255]
256
+   */
257
+  static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);
218 258
 
219
-// Reset source
220
-void HAL_clear_reset_source(void);
221
-uint8_t HAL_get_reset_source(void);
259
+  /**
260
+   * Set the frequency of the timer corresponding to the provided pin
261
+   * All Hardware PWM pins will run at the same frequency and
262
+   * All Software PWM pins will run at the same frequency
263
+   */
264
+  static void set_pwm_frequency(const pin_t pin, int f_desired);
265
+};
222 266
 
223
-void HAL_reboot();
267
+extern MarlinHAL hal;

+ 2
- 1
Marlin/src/HAL/LPC1768/Servo.h Целия файл

@@ -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;

+ 5
- 9
Marlin/src/HAL/LPC1768/fast_pwm.cpp Целия файл

@@ -21,21 +21,17 @@
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
-#if NEEDS_HARDWARE_PWM // Specific meta-flag for features that mandate PWM
34
-
35
-  void set_pwm_frequency(const pin_t pin, int f_desired) {
36
-    LPC176x::pwm_set_frequency(pin, f_desired);
37
-  }
38
-
39
-#endif
33
+void MarlinHAL::set_pwm_frequency(const pin_t pin, int f_desired) {
34
+  LPC176x::pwm_set_frequency(pin, f_desired);
35
+}
40 36
 
41 37
 #endif // TARGET_LPC1768

+ 3
- 3
Marlin/src/HAL/LPC1768/main.cpp Целия файл

@@ -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 Целия файл

@@ -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

+ 108
- 65
Marlin/src/HAL/NATIVE_SIM/HAL.h Целия файл

@@ -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,20 @@ 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();
120
+#define HAL_ANALOG_SELECT(ch) hal.adc_enable(ch)
144 121
 
145 122
 /* ---------------- Delay in cycles */
146 123
 
@@ -159,29 +136,22 @@ constexpr inline std::size_t strlen_constexpr(const char* str) {
159 136
   // https://github.com/gcc-mirror/gcc/blob/5c7634a0e5f202935aa6c11b6ea953b8bf80a00a/libstdc%2B%2B-v3/include/bits/char_traits.h#L329
160 137
   if (str != nullptr) {
161 138
     std::size_t i = 0;
162
-    while (str[i] != '\0') {
163
-      ++i;
164
-    }
165
-
139
+    while (str[i] != '\0') ++i;
166 140
     return i;
167 141
   }
168
-
169 142
   return 0;
170 143
 }
171 144
 
172 145
 constexpr inline int strncmp_constexpr(const char* lhs, const char* rhs, std::size_t count) {
173 146
   // https://github.com/gcc-mirror/gcc/blob/13b9cbfc32fe3ac4c81c4dd9c42d141c8fb95db4/libstdc%2B%2B-v3/include/bits/char_traits.h#L655
174
-  if (lhs == nullptr || rhs == nullptr) {
147
+  if (lhs == nullptr || rhs == nullptr)
175 148
     return rhs != nullptr ? -1 : 1;
176
-  }
177 149
 
178
-  for (std::size_t i = 0; i < count; ++i) {
150
+  for (std::size_t i = 0; i < count; ++i)
179 151
     if (lhs[i] != rhs[i]) {
180 152
       return lhs[i] < rhs[i] ? -1 : 1;
181
-    } else if (lhs[i] == '\0') {
153
+    else if (lhs[i] == '\0')
182 154
       return 0;
183
-    }
184
-  }
185 155
 
186 156
   return 0;
187 157
 }
@@ -193,14 +163,11 @@ constexpr inline const char* strstr_constexpr(const char* str, const char* targe
193 163
     do {
194 164
       char sc = {};
195 165
       do {
196
-        if ((sc = *str++) == '\0') {
197
-          return nullptr;
198
-        }
166
+        if ((sc = *str++) == '\0') return nullptr;
199 167
       } while (sc != c);
200 168
     } while (strncmp_constexpr(str, target, len) != 0);
201 169
     --str;
202 170
   }
203
-
204 171
   return str;
205 172
 }
206 173
 
@@ -211,12 +178,88 @@ constexpr inline char* strstr_constexpr(char* str, const char* target) {
211 178
     do {
212 179
       char sc = {};
213 180
       do {
214
-        if ((sc = *str++) == '\0') {
215
-          return nullptr;
216
-        }
181
+        if ((sc = *str++) == '\0') return nullptr;
217 182
       } while (sc != c);
218 183
     } while (strncmp_constexpr(str, target, len) != 0);
219 184
     --str;
220 185
   }
221 186
   return str;
222 187
 }
188
+
189
+// ------------------------
190
+// Class Utilities
191
+// ------------------------
192
+
193
+#pragma GCC diagnostic push
194
+#if GCC_VERSION <= 50000
195
+  #pragma GCC diagnostic ignored "-Wunused-function"
196
+#endif
197
+
198
+int freeMemory();
199
+
200
+#pragma GCC diagnostic pop
201
+
202
+// ------------------------
203
+// MarlinHAL Class
204
+// ------------------------
205
+
206
+class MarlinHAL {
207
+public:
208
+
209
+  // Earliest possible init, before setup()
210
+  MarlinHAL() {}
211
+
212
+  static inline void init() {}        // Called early in setup()
213
+  static inline void init_board() {}  // Called less early in setup()
214
+  static void reboot();               // Restart the firmware from 0x0
215
+
216
+  static inline bool isr_state() { return true; }
217
+  static inline void isr_on()  {}
218
+  static inline void isr_off() {}
219
+
220
+  static inline 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 inline uint8_t get_reset_source() { return reset_reason; }
228
+  static inline void clear_reset_source() {}
229
+
230
+  // Free SRAM
231
+  static inline 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 inline 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
+};
264
+
265
+extern MarlinHAL hal;

+ 2
- 2
Marlin/src/HAL/NATIVE_SIM/timers.h Целия файл

@@ -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

+ 9
- 17
Marlin/src/HAL/SAMD51/HAL.cpp Целия файл

@@ -106,7 +106,7 @@
106 106
 // Private Variables
107 107
 // ------------------------
108 108
 
109
-uint16_t HAL_adc_result;
109
+uint16_t MarlinHAL::adc_result;
110 110
 
111 111
 #if ADC_IS_REQUIRED
112 112
 
@@ -402,7 +402,7 @@ uint16_t HAL_adc_result;
402 402
 // ------------------------
403 403
 
404 404
 // HAL initialization task
405
-void HAL_init() {
405
+void MarlinHAL::init() {
406 406
   TERN_(DMA_IS_REQUIRED, dma_init());
407 407
   #if ENABLED(SDSUPPORT)
408 408
     #if SD_CONNECTION_IS(ONBOARD) && PIN_EXISTS(SD_DETECT)
@@ -412,17 +412,9 @@ void HAL_init() {
412 412
   #endif
413 413
 }
414 414
 
415
-// HAL idle task
416
-/*
417
-void HAL_idletask() {
418
-}
419
-*/
420
-
421
-void HAL_clear_reset_source() { }
422
-
423 415
 #pragma push_macro("WDT")
424 416
 #undef WDT    // Required to be able to use '.bit.WDT'. Compiler wrongly replace struct field with WDT define
425
-uint8_t HAL_get_reset_source() {
417
+uint8_t MarlinHAL::get_reset_source() {
426 418
   RSTC_RCAUSE_Type resetCause;
427 419
 
428 420
   resetCause.reg = REG_RSTC_RCAUSE;
@@ -436,7 +428,7 @@ uint8_t HAL_get_reset_source() {
436 428
 }
437 429
 #pragma pop_macro("WDT")
438 430
 
439
-void HAL_reboot() { NVIC_SystemReset(); }
431
+void MarlinHAL::reboot() { NVIC_SystemReset(); }
440 432
 
441 433
 extern "C" {
442 434
   void * _sbrk(int incr);
@@ -454,7 +446,7 @@ int freeMemory() {
454 446
 // ADC
455 447
 // ------------------------
456 448
 
457
-void HAL_adc_init() {
449
+void MarlinHAL::adc_init() {
458 450
   #if ADC_IS_REQUIRED
459 451
     memset(HAL_adc_results, 0xFF, sizeof(HAL_adc_results));                 // Fill result with invalid values
460 452
 
@@ -491,17 +483,17 @@ void HAL_adc_init() {
491 483
   #endif // ADC_IS_REQUIRED
492 484
 }
493 485
 
494
-void HAL_adc_start_conversion(const uint8_t adc_pin) {
486
+void MarlinHAL::adc_start(const pin_t pin) {
495 487
   #if ADC_IS_REQUIRED
496 488
     LOOP_L_N(pi, COUNT(adc_pins)) {
497
-      if (adc_pin == adc_pins[pi]) {
498
-        HAL_adc_result = HAL_adc_results[pi];
489
+      if (pin == adc_pins[pi]) {
490
+        adc_result = HAL_adc_results[pi];
499 491
         return;
500 492
       }
501 493
     }
502 494
   #endif
503 495
 
504
-  HAL_adc_result = 0xFFFF;
496
+  adc_result = 0xFFFF;
505 497
 }
506 498
 
507 499
 #endif // __SAMD51__

+ 81
- 44
Marlin/src/HAL/SAMD51/HAL.h Целия файл

@@ -89,51 +89,31 @@
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
+class Servo;
94
+typedef Servo hal_servo_t;
94 95
 
95 96
 //
96 97
 // Interrupts
97 98
 //
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()
99
+#define CRITICAL_SECTION_START()  const bool irqon = !__get_PRIMASK(); __disable_irq()
100
+#define CRITICAL_SECTION_END()    if (irqon) __enable_irq()
103 101
 
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
109
-
110
-void HAL_reboot();
102
+#define cli() __disable_irq() // Disable interrupts
103
+#define sei() __enable_irq()  // Enable interrupts
111 104
 
112 105
 //
113 106
 // ADC
114 107
 //
115
-extern uint16_t HAL_adc_result;     // Most recent ADC conversion
116 108
 
117 109
 #define HAL_ANALOG_SELECT(pin)
118 110
 
119
-void HAL_adc_init();
120
-
121 111
 //#define HAL_ADC_FILTERED          // Disable Marlin's oversampling. The HAL filters ADC values.
122 112
 #define HAL_ADC_VREF         3.3
123 113
 #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
-
128
-void HAL_adc_start_conversion(const uint8_t adc_pin);
129
-
130
-//
131
-// PWM
132
-//
133
-inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); }
134 114
 
135 115
 //
136
-// Pin Map
116
+// Pin Mapping for M42, M43, M226
137 117
 //
138 118
 #define GET_PIN_MAP_PIN(index) index
139 119
 #define GET_PIN_MAP_INDEX(pin) pin
@@ -142,35 +122,92 @@ inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255,
142 122
 //
143 123
 // Tone
144 124
 //
145
-void toneInit();
146 125
 void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration=0);
147 126
 void noTone(const pin_t _pin);
148 127
 
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); }
128
+// ------------------------
129
+// Class Utilities
130
+// ------------------------
160 131
 
161 132
 #pragma GCC diagnostic push
162 133
 #if GCC_VERSION <= 50000
163 134
   #pragma GCC diagnostic ignored "-Wunused-function"
164 135
 #endif
165 136
 
166
-int freeMemory();
167
-
168
-#pragma GCC diagnostic pop
169
-
170 137
 #ifdef __cplusplus
171 138
   extern "C" {
172 139
 #endif
140
+
173 141
 char *dtostrf(double __val, signed char __width, unsigned char __prec, char *__s);
142
+
143
+extern "C" int freeMemory();
144
+
174 145
 #ifdef __cplusplus
175 146
   }
176 147
 #endif
148
+
149
+#pragma GCC diagnostic pop
150
+
151
+// ------------------------
152
+// MarlinHAL Class
153
+// ------------------------
154
+
155
+class MarlinHAL {
156
+public:
157
+
158
+  // Earliest possible init, before setup()
159
+  MarlinHAL() {}
160
+
161
+  static void init();                 // Called early in setup()
162
+  static inline void init_board() {}  // Called less early in setup()
163
+  static void reboot();               // Restart the firmware from 0x0
164
+
165
+  static inline bool isr_state() { return !__get_PRIMASK(); }
166
+  static inline void isr_on()  { sei(); }
167
+  static inline void isr_off() { cli(); }
168
+
169
+  static inline void delay_ms(const int ms) { delay(ms); }
170
+
171
+  // Tasks, called from idle()
172
+  static inline void idletask() {}
173
+
174
+  // Reset
175
+  static uint8_t get_reset_source();
176
+  static inline void clear_reset_source() {}
177
+
178
+  // Free SRAM
179
+  static inline 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 inline 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 inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false) {
208
+    analogWrite(pin, v);
209
+  }
210
+
211
+};
212
+
213
+extern MarlinHAL hal;

+ 10
- 18
Marlin/src/HAL/STM32/HAL.cpp Целия файл

@@ -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;

+ 103
- 76
Marlin/src/HAL/STM32/HAL.h Целия файл

@@ -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,54 +133,14 @@
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
145
-// ------------------------
146
-
147
-// result of last ADC conversion
148
-extern uint16_t HAL_adc_result;
149
-
150
-// ------------------------
151
-// Public functions
152
-// ------------------------
153
-
154
-// Memory related
155
-#define __bss_end __bss_end__
156
-
157
-// Enable hooks into  setup for HAL
158
-void HAL_init();
159
-#define HAL_IDLETASK 1
160
-void HAL_idletask();
161
-
162
-// Clear reset reason
163
-void HAL_clear_reset_source();
164
-
165
-// Reset reason
166
-uint8_t HAL_get_reset_source();
167
-
168
-void HAL_reboot();
169
-
170
-void _delay_ms(const int delay);
171
-
172
-extern "C" char* _sbrk(int incr);
173
-
174
-#pragma GCC diagnostic push
175
-#pragma GCC diagnostic ignored "-Wunused-function"
176
-
177
-static inline int freeMemory() {
178
-  volatile char top;
179
-  return &top - reinterpret_cast<char*>(_sbrk(0));
180
-}
181
-
182
-#pragma GCC diagnostic pop
183
-
184
-//
185 142
 // ADC
186
-//
143
+// ------------------------
187 144
 
188 145
 #define HAL_ANALOG_SELECT(pin) pinMode(pin, INPUT)
189 146
 
@@ -194,16 +151,10 @@ static inline int freeMemory() {
194 151
 #endif
195 152
 
196 153
 #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
200
-
201
-inline void HAL_adc_init() { analogReadResolution(HAL_ADC_RESOLUTION); }
202
-
203
-void HAL_adc_start_conversion(const uint8_t adc_pin);
204
-
205
-uint16_t HAL_adc_get_result();
206 154
 
155
+//
156
+// Pin Mapping for M42, M43, M226
157
+//
207 158
 #define GET_PIN_MAP_PIN(index) index
208 159
 #define GET_PIN_MAP_INDEX(pin) pin
209 160
 #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
@@ -226,17 +177,93 @@ extern volatile uint32_t systick_uptime_millis;
226 177
 
227 178
 #define HAL_CAN_SET_PWM_FREQ   // This HAL supports PWM Frequency adjustment
228 179
 
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, int f_desired);
180
+// ------------------------
181
+// Class Utilities
182
+// ------------------------
235 183
 
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);
184
+// Memory related
185
+#define __bss_end __bss_end__
186
+
187
+extern "C" char* _sbrk(int incr);
188
+
189
+#pragma GCC diagnostic push
190
+#if GCC_VERSION <= 50000
191
+  #pragma GCC diagnostic ignored "-Wunused-function"
192
+#endif
193
+
194
+static inline int freeMemory() {
195
+  volatile char top;
196
+  return &top - reinterpret_cast<char*>(_sbrk(0));
197
+}
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 inline void init_board() {}  // Called less early in setup()
213
+  static void reboot();               // Restart the firmware from 0x0
214
+
215
+  static inline bool isr_state() { return !__get_PRIMASK(); }
216
+  static inline void isr_on()  { sei(); }
217
+  static inline void isr_off() { cli(); }
218
+
219
+  static inline void delay_ms(const int ms) { delay(ms); }
220
+
221
+  // Tasks, called from idle()
222
+  static void idletask();
223
+
224
+  // Reset
225
+  static uint8_t get_reset_source();
226
+  static void clear_reset_source();
227
+
228
+  // Free SRAM
229
+  static inline int freeMemory() { return freeMemory(); }
230
+
231
+  //
232
+  // ADC Methods
233
+  //
234
+
235
+  static uint16_t adc_result;
236
+
237
+  // Called by Temperature::init once at startup
238
+  static inline void adc_init() {
239
+    analogReadResolution(HAL_ADC_RESOLUTION);
240
+  }
241
+
242
+  // Called by Temperature::init for each sensor at startup
243
+  static void adc_enable(const pin_t pin);
244
+
245
+  // Begin ADC sampling on the given channel
246
+  static void adc_start(const pin_t pin) { adc_result = analogRead(pin); }
247
+
248
+  // Is the ADC ready for reading?
249
+  static inline bool adc_ready() { return true; }
250
+
251
+  // The current value of the ADC register
252
+  static uint16_t adc_value() { return adc_result; }
253
+
254
+  /**
255
+   * Set the PWM duty cycle for the pin to the given value.
256
+   * Optionally invert the duty cycle [default = false]
257
+   * Optionally change the maximum size 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 v_size=255, const bool invert=false);
260
+
261
+  /**
262
+   * Set the frequency of the timer for the given pin.
263
+   * All Timer PWM pins run at the same frequency.
264
+   */
265
+  static void set_pwm_frequency(const pin_t pin, int f_desired);
266
+
267
+};
268
+
269
+extern MarlinHAL hal;

+ 4
- 4
Marlin/src/HAL/STM32/HAL_SPI.cpp Целия файл

@@ -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_off();                                                // 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 Целия файл

@@ -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 Целия файл

@@ -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
   if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
34 34
   const PinName pin_name = digitalPinToPinName(pin);
35 35
   TIM_TypeDef * const Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM);
@@ -56,7 +56,7 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255
56 56
   if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) HT->resume();
57 57
 }
58 58
 
59
-void set_pwm_frequency(const pin_t pin, int f_desired) {
59
+void MarlinHAL::set_pwm_frequency(const pin_t pin, int f_desired) {
60 60
   if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
61 61
   const PinName pin_name = digitalPinToPinName(pin);
62 62
   TIM_TypeDef * const Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM); // Get HAL timer instance

+ 5
- 1
Marlin/src/HAL/STM32/pinsDebug.h Целия файл

@@ -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 Целия файл

@@ -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

+ 91
- 99
Marlin/src/HAL/STM32F1/HAL.cpp Целия файл

@@ -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,72 +112,21 @@
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 118
 
122
-const uint8_t adc_pins[] = {
123
-  #if HAS_TEMP_ADC_0
124
-    TEMP_0_PIN,
125
-  #endif
126
-  #if HAS_TEMP_ADC_PROBE
127
-    TEMP_PROBE_PIN,
128
-  #endif
129
-  #if HAS_HEATED_BED
130
-    TEMP_BED_PIN,
131
-  #endif
132
-  #if HAS_TEMP_CHAMBER
133
-    TEMP_CHAMBER_PIN,
134
-  #endif
135
-  #if HAS_TEMP_COOLER
136
-    TEMP_COOLER_PIN,
137
-  #endif
138
-  #if HAS_TEMP_ADC_1
139
-    TEMP_1_PIN,
140
-  #endif
141
-  #if HAS_TEMP_ADC_2
142
-    TEMP_2_PIN,
143
-  #endif
144
-  #if HAS_TEMP_ADC_3
145
-    TEMP_3_PIN,
146
-  #endif
147
-  #if HAS_TEMP_ADC_4
148
-    TEMP_4_PIN,
149
-  #endif
150
-  #if HAS_TEMP_ADC_5
151
-    TEMP_5_PIN,
152
-  #endif
153
-  #if HAS_TEMP_ADC_6
154
-    TEMP_6_PIN,
155
-  #endif
156
-  #if HAS_TEMP_ADC_7
157
-    TEMP_7_PIN,
158
-  #endif
159
-  #if ENABLED(FILAMENT_WIDTH_SENSOR)
160
-    FILWIDTH_PIN,
161
-  #endif
162
-  #if HAS_ADC_BUTTONS
163
-    ADC_KEYPAD_PIN,
164
-  #endif
165
-  #if HAS_JOY_ADC_X
166
-    JOY_X_PIN,
167
-  #endif
168
-  #if HAS_JOY_ADC_Y
169
-    JOY_Y_PIN,
170
-  #endif
171
-  #if HAS_JOY_ADC_Z
172
-    JOY_Z_PIN,
173
-  #endif
174
-  #if ENABLED(POWER_MONITOR_CURRENT)
175
-    POWER_MONITOR_CURRENT_PIN,
176
-  #endif
177
-  #if ENABLED(POWER_MONITOR_VOLTAGE)
178
-    POWER_MONITOR_VOLTAGE_PIN,
179
-  #endif
180
-};
119
+uint16_t analogRead(pin_t pin) {
120
+  const bool is_analog = _GET_MODE(pin) == GPIO_INPUT_ANALOG;
121
+  return is_analog ? analogRead(uint8_t(pin)) : 0;
122
+}
123
+
124
+// Wrapper to maple unprotected analogWrite
125
+void analogWrite(pin_t pin, int pwm_val8) {
126
+  if (PWM_PIN(pin)) analogWrite(uint8_t(pin), pwm_val8);
127
+}
128
+
129
+uint16_t MarlinHAL::adc_result;
181 130
 
182 131
 enum TempPinIndex : char {
183 132
   #if HAS_TEMP_ADC_0
@@ -245,15 +194,16 @@ uint16_t HAL_adc_results[ADC_PIN_COUNT];
245 194
 // ------------------------
246 195
 // Private functions
247 196
 // ------------------------
197
+
248 198
 static void NVIC_SetPriorityGrouping(uint32_t PriorityGroup) {
249 199
   uint32_t reg_value;
250
-  uint32_t PriorityGroupTmp = (PriorityGroup & (uint32_t)0x07);               /* only values 0..7 are used          */
200
+  uint32_t PriorityGroupTmp = (PriorityGroup & (uint32_t)0x07);               // only values 0..7 are used
251 201
 
252
-  reg_value  =  SCB->AIRCR;                                                   /* read old register configuration    */
253
-  reg_value &= ~(SCB_AIRCR_VECTKEY_Msk | SCB_AIRCR_PRIGROUP_Msk);             /* clear bits to change               */
202
+  reg_value  =  SCB->AIRCR;                                                   // read old register configuration
203
+  reg_value &= ~(SCB_AIRCR_VECTKEY_Msk | SCB_AIRCR_PRIGROUP_Msk);             // clear bits to change
254 204
   reg_value  =  (reg_value                                 |
255 205
                 ((uint32_t)0x5FA << SCB_AIRCR_VECTKEY_Pos) |
256
-                (PriorityGroupTmp << 8));                                     /* Insert write key & priority group  */
206
+                (PriorityGroupTmp << 8));                                     // Insert write key & priority group
257 207
   SCB->AIRCR =  reg_value;
258 208
 }
259 209
 
@@ -261,6 +211,8 @@ static void NVIC_SetPriorityGrouping(uint32_t PriorityGroup) {
261 211
 // Public functions
262 212
 // ------------------------
263 213
 
214
+void flashFirmware(const int16_t) { hal.reboot(); }
215
+
264 216
 //
265 217
 // Leave PA11/PA12 intact if USBSerial is not used
266 218
 //
@@ -280,7 +232,11 @@ static void NVIC_SetPriorityGrouping(uint32_t PriorityGroup) {
280 232
 
281 233
 TERN_(POSTMORTEM_DEBUGGING, extern void install_min_serial());
282 234
 
283
-void HAL_init() {
235
+// ------------------------
236
+// MarlinHAL class
237
+// ------------------------
238
+
239
+void MarlinHAL::init() {
284 240
   NVIC_SetPriorityGrouping(0x3);
285 241
   #if PIN_EXISTS(LED)
286 242
     OUT_WRITE(LED_PIN, LOW);
@@ -299,7 +255,7 @@ void HAL_init() {
299 255
 }
300 256
 
301 257
 // HAL idle task
302
-void HAL_idletask() {
258
+void MarlinHAL::idletask() {
303 259
   #if HAS_SHARED_MEDIA
304 260
     // If Marlin is using the SD card we need to lock it to prevent access from
305 261
     // a PC via USB.
@@ -314,14 +270,7 @@ void HAL_idletask() {
314 270
   #endif
315 271
 }
316 272
 
317
-void HAL_clear_reset_source() { }
318
-
319
-/**
320
- * TODO: Check this and change or remove.
321
- */
322
-uint8_t HAL_get_reset_source() { return RST_POWER_ON; }
323
-
324
-void _delay_ms(const int delay_ms) { delay(delay_ms); }
273
+void MarlinHAL::reboot() { nvic_sys_reset(); }
325 274
 
326 275
 extern "C" {
327 276
   extern unsigned int _ebss; // end of bss section
@@ -355,11 +304,70 @@ extern "C" {
355 304
 }
356 305
 */
357 306
 
358
-// ------------------------
359 307
 // ADC
360
-// ------------------------
308
+
361 309
 // Init the AD in continuous capture mode
362
-void HAL_adc_init() {
310
+void MarlinHAL::adc_init() {
311
+  static const uint8_t adc_pins[] = {
312
+    #if HAS_TEMP_ADC_0
313
+      TEMP_0_PIN,
314
+    #endif
315
+    #if HAS_TEMP_ADC_PROBE
316
+      TEMP_PROBE_PIN,
317
+    #endif
318
+    #if HAS_HEATED_BED
319
+      TEMP_BED_PIN,
320
+    #endif
321
+    #if HAS_TEMP_CHAMBER
322
+      TEMP_CHAMBER_PIN,
323
+    #endif
324
+    #if HAS_TEMP_COOLER
325
+      TEMP_COOLER_PIN,
326
+    #endif
327
+    #if HAS_TEMP_ADC_1
328
+      TEMP_1_PIN,
329
+    #endif
330
+    #if HAS_TEMP_ADC_2
331
+      TEMP_2_PIN,
332
+    #endif
333
+    #if HAS_TEMP_ADC_3
334
+      TEMP_3_PIN,
335
+    #endif
336
+    #if HAS_TEMP_ADC_4
337
+      TEMP_4_PIN,
338
+    #endif
339
+    #if HAS_TEMP_ADC_5
340
+      TEMP_5_PIN,
341
+    #endif
342
+    #if HAS_TEMP_ADC_6
343
+      TEMP_6_PIN,
344
+    #endif
345
+    #if HAS_TEMP_ADC_7
346
+      TEMP_7_PIN,
347
+    #endif
348
+    #if ENABLED(FILAMENT_WIDTH_SENSOR)
349
+      FILWIDTH_PIN,
350
+    #endif
351
+    #if HAS_ADC_BUTTONS
352
+      ADC_KEYPAD_PIN,
353
+    #endif
354
+    #if HAS_JOY_ADC_X
355
+      JOY_X_PIN,
356
+    #endif
357
+    #if HAS_JOY_ADC_Y
358
+      JOY_Y_PIN,
359
+    #endif
360
+    #if HAS_JOY_ADC_Z
361
+      JOY_Z_PIN,
362
+    #endif
363
+    #if ENABLED(POWER_MONITOR_CURRENT)
364
+      POWER_MONITOR_CURRENT_PIN,
365
+    #endif
366
+    #if ENABLED(POWER_MONITOR_VOLTAGE)
367
+      POWER_MONITOR_VOLTAGE_PIN,
368
+    #endif
369
+  };
370
+  static STM32ADC adc(ADC1);
363 371
   // configure the ADC
364 372
   adc.calibrate();
365 373
   #if F_CPU > 72000000
@@ -374,10 +382,10 @@ void HAL_adc_init() {
374 382
   adc.startConversion();
375 383
 }
376 384
 
377
-void HAL_adc_start_conversion(const uint8_t adc_pin) {
385
+void MarlinHAL::adc_start(const pin_t pin) {
378 386
   //TEMP_PINS pin_index;
379 387
   TempPinIndex pin_index;
380
-  switch (adc_pin) {
388
+  switch (pin) {
381 389
     default: return;
382 390
     #if HAS_TEMP_ADC_0
383 391
       case TEMP_0_PIN: pin_index = TEMP_0; break;
@@ -440,20 +448,4 @@ void HAL_adc_start_conversion(const uint8_t adc_pin) {
440 448
   HAL_adc_result = HAL_adc_results[(int)pin_index] >> (12 - HAL_ADC_RESOLUTION); // shift out unused bits
441 449
 }
442 450
 
443
-uint16_t HAL_adc_get_result() { return HAL_adc_result; }
444
-
445
-uint16_t analogRead(pin_t pin) {
446
-  const bool is_analog = _GET_MODE(pin) == GPIO_INPUT_ANALOG;
447
-  return is_analog ? analogRead(uint8_t(pin)) : 0;
448
-}
449
-
450
-// Wrapper to maple unprotected analogWrite
451
-void analogWrite(pin_t pin, int pwm_val8) {
452
-  if (PWM_PIN(pin)) analogWrite(uint8_t(pin), pwm_val8);
453
-}
454
-
455
-void HAL_reboot() { nvic_sys_reset(); }
456
-
457
-void flashFirmware(const int16_t) { HAL_reboot(); }
458
-
459 451
 #endif // __STM32F1__

+ 105
- 79
Marlin/src/HAL/STM32F1/HAL.h Целия файл

@@ -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
@@ -181,62 +172,24 @@ void HAL_idletask();
181 172
 
182 173
 typedef int8_t pin_t;
183 174
 
184
-// ------------------------
185
-// Public Variables
186
-// ------------------------
187
-
188 175
 // Result of last ADC conversion
189 176
 extern uint16_t HAL_adc_result;
190 177
 
191 178
 // ------------------------
192
-// Public functions
179
+// Interrupts
193 180
 // ------------------------
194 181
 
195
-// Disable interrupts
182
+#define CRITICAL_SECTION_START()  const bool irqon = !__get_primask(); (void)__iCliRetVal()
183
+#define CRITICAL_SECTION_END()    if (!primask) (void)__iSeiRetVal()
196 184
 #define cli() noInterrupts()
197
-
198
-// Enable interrupts
199 185
 #define sei() interrupts()
200 186
 
201
-// Memory related
202
-#define __bss_end __bss_end__
203
-
204
-// Clear reset reason
205
-void HAL_clear_reset_source();
206
-
207
-// Reset reason
208
-uint8_t HAL_get_reset_source();
209
-
210
-void HAL_reboot();
211
-
212
-void _delay_ms(const int delay);
213
-
214
-#pragma GCC diagnostic push
215
-#pragma GCC diagnostic ignored "-Wunused-function"
216
-
217
-/*
218
-extern "C" {
219
-  int freeMemory();
220
-}
221
-*/
222
-
223
-extern "C" char* _sbrk(int incr);
224
-
225
-static inline int freeMemory() {
226
-  volatile char top;
227
-  return &top - _sbrk(0);
228
-}
229
-
230
-#pragma GCC diagnostic pop
231
-
232
-//
187
+// ------------------------
233 188
 // ADC
234
-//
189
+// ------------------------
235 190
 
236 191
 #define HAL_ANALOG_SELECT(pin) pinMode(pin, INPUT_ANALOG);
237 192
 
238
-void HAL_adc_init();
239
-
240 193
 #ifdef ADC_RESOLUTION
241 194
   #define HAL_ADC_RESOLUTION ADC_RESOLUTION
242 195
 #else
@@ -244,43 +197,116 @@ void HAL_adc_init();
244 197
 #endif
245 198
 
246 199
 #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
250
-
251
-void HAL_adc_start_conversion(const uint8_t adc_pin);
252
-uint16_t HAL_adc_get_result();
253 200
 
254 201
 uint16_t analogRead(pin_t pin); // need HAL_ANALOG_SELECT() first
255 202
 void analogWrite(pin_t pin, int pwm_val8); // PWM only! mul by 257 in maple!?
256 203
 
204
+//
205
+// Pin Mapping for M42, M43, M226
206
+//
257 207
 #define GET_PIN_MAP_PIN(index) index
258 208
 #define GET_PIN_MAP_INDEX(pin) pin
259 209
 #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
260 210
 
261
-#define JTAG_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY)
211
+#define JTAG_DISABLE()    afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY)
262 212
 #define JTAGSWD_DISABLE() afio_cfg_debug_ports(AFIO_DEBUG_NONE)
263 213
 
264 214
 #define PLATFORM_M997_SUPPORT
265 215
 void flashFirmware(const int16_t);
266 216
 
217
+#define HAL_CAN_SET_PWM_FREQ      // This HAL supports PWM Frequency adjustment
267 218
 #ifndef PWM_FREQUENCY
268 219
   #define PWM_FREQUENCY      1000 // Default PWM Frequency
269 220
 #endif
270
-#define HAL_CAN_SET_PWM_FREQ      // This HAL supports PWM Frequency adjustment
271 221
 
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, int f_desired);
222
+// ------------------------
223
+// Class Utilities
224
+// ------------------------
278 225
 
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);
226
+// Memory related
227
+#define __bss_end __bss_end__
228
+
229
+void _delay_ms(const int ms);
230
+
231
+extern "C" char* _sbrk(int incr);
232
+
233
+#pragma GCC diagnostic push
234
+#if GCC_VERSION <= 50000
235
+  #pragma GCC diagnostic ignored "-Wunused-function"
236
+#endif
237
+
238
+static inline int freeMemory() {
239
+  volatile char top;
240
+  return &top - _sbrk(0);
241
+}
242
+
243
+#pragma GCC diagnostic pop
244
+
245
+// ------------------------
246
+// MarlinHAL Class
247
+// ------------------------
248
+
249
+class MarlinHAL {
250
+public:
251
+
252
+  // Earliest possible init, before setup()
253
+  MarlinHAL() {}
254
+
255
+  static void init();                 // Called early in setup()
256
+  static inline void init_board() {}  // Called less early in setup()
257
+  static void reboot();               // Restart the firmware from 0x0
258
+
259
+  static inline bool isr_state() { return !__get_primask(); }
260
+  static inline void isr_on()  { ((void)__iSeiRetVal()); }
261
+  static inline void isr_off() { ((void)__iCliRetVal()); }
262
+
263
+  static inline void delay_ms(const int ms) { delay(ms); }
264
+
265
+  // Tasks, called from idle()
266
+  static void idletask();
267
+
268
+  // Reset
269
+  static inline uint8_t get_reset_source() { return RST_POWER_ON; }
270
+  static inline void clear_reset_source() {}
271
+
272
+  // Free SRAM
273
+  static inline int freeMemory() { return freeMemory(); }
274
+
275
+  //
276
+  // ADC Methods
277
+  //
278
+
279
+  static uint16_t adc_result;
280
+
281
+  // Called by Temperature::init once at startup
282
+  static void adc_init();
283
+
284
+  // Called by Temperature::init for each sensor at startup
285
+  static void adc_enable(const pin_t pin);
286
+
287
+  // Begin ADC sampling on the given channel
288
+  static void adc_start(const pin_t pin);
289
+
290
+  // Is the ADC ready for reading?
291
+  static inline bool adc_ready() { return true; }
292
+
293
+  // The current value of the ADC register
294
+  static uint16_t adc_value() { return adc_result; }
295
+
296
+  /**
297
+   * Set the PWM duty cycle for the pin to the given value.
298
+   * Optionally invert the duty cycle [default = false]
299
+   * Optionally change the maximum size of the provided value to enable finer PWM duty control [default = 255]
300
+   * The timer must be pre-configured with set_pwm_frequency() if the default frequency is not desired.
301
+   */
302
+  static inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false);
303
+
304
+  /**
305
+   * Set the frequency of the timer for the given pin.
306
+   * All Timer PWM pins run at the same frequency.
307
+   */
308
+  static void set_pwm_frequency(const pin_t pin, int f_desired);
309
+
310
+};
311
+
312
+extern MarlinHAL hal;

+ 2
- 1
Marlin/src/HAL/STM32F1/Servo.h Целия файл

@@ -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 Целия файл

@@ -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
   if (!PWM_PIN(pin)) return;
43 41
 
44 42
   timer_dev *timer; UNUSED(timer);
@@ -51,7 +49,7 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255
51 49
   timer_set_mode(timer, channel, TIMER_PWM); // PWM Output Mode
52 50
 }
53 51
 
54
-void set_pwm_frequency(const pin_t pin, int f_desired) {
52
+void MarlinHAL::set_pwm_frequency(const pin_t pin, int f_desired) {
55 53
   if (!PWM_PIN(pin)) return;                    // Don't proceed if no hardware timer
56 54
 
57 55
   timer_dev *timer; UNUSED(timer);

+ 1
- 1
Marlin/src/HAL/STM32F1/timers.h Целия файл

@@ -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 Целия файл

@@ -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__

+ 94
- 37
Marlin/src/HAL/TEENSY31_32/HAL.h Целия файл

@@ -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,46 @@ 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
 
89
+// ------------------------
90
+// Interrupts
91
+// ------------------------
92
+
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()
96
+
97
+// ------------------------
98
+// ADC
99
+// ------------------------
100
+
79 101
 #ifndef analogInputToDigitalPin
80 102
   #define analogInputToDigitalPin(p) ((p < 12U) ? (p) + 54U : -1)
81 103
 #endif
82 104
 
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()
88
-
89
-inline void HAL_init() {}
90
-
91
-// Clear the reset reason
92
-void HAL_clear_reset_source();
105
+#define HAL_ADC_VREF         3.3
106
+#define HAL_ADC_RESOLUTION  10
93 107
 
94
-// Get the reason for the reset
95
-uint8_t HAL_get_reset_source();
108
+#define HAL_ANALOG_SELECT(pin)
96 109
 
97
-void HAL_reboot();
110
+//
111
+// Pin Mapping for M42, M43, M226
112
+//
113
+#define GET_PIN_MAP_PIN(index) index
114
+#define GET_PIN_MAP_INDEX(pin) pin
115
+#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
98 116
 
99
-FORCE_INLINE void _delay_ms(const int delay_ms) { delay(delay_ms); }
117
+// ------------------------
118
+// Class Utilities
119
+// ------------------------
100 120
 
101 121
 #pragma GCC diagnostic push
102 122
 #if GCC_VERSION <= 50000
@@ -107,27 +127,64 @@ extern "C" int freeMemory();
107 127
 
108 128
 #pragma GCC diagnostic pop
109 129
 
110
-// ADC
130
+// ------------------------
131
+// MarlinHAL Class
132
+// ------------------------
111 133
 
112
-void HAL_adc_init();
134
+class MarlinHAL {
135
+public:
113 136
 
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
137
+  // Earliest possible init, before setup()
138
+  MarlinHAL() {}
119 139
 
120
-#define HAL_ANALOG_SELECT(pin)
140
+  static inline void init() {}        // Called early in setup()
141
+  static inline void init_board() {}  // Called less early in setup()
142
+  static void reboot();               // Restart the firmware from 0x0
121 143
 
122
-void HAL_adc_start_conversion(const uint8_t adc_pin);
123
-uint16_t HAL_adc_get_result();
144
+  static inline bool isr_state() { return !__get_PRIMASK(); }
145
+  static inline void isr_on()  { __enable_irq(); }
146
+  static inline void isr_off() { __disable_irq(); }
124 147
 
125
-// PWM
148
+  static inline void delay_ms(const int ms) { delay(ms); }
126 149
 
127
-inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); }
150
+  // Tasks, called from idle()
151
+  static inline void idletask() {}
128 152
 
129
-// Pin Map
153
+  // Reset
154
+  static uint8_t get_reset_source();
155
+  static inline void clear_reset_source() {}
130 156
 
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)
157
+  // Free SRAM
158
+  static inline int freeMemory() { return freeMemory(); }
159
+
160
+  //
161
+  // ADC Methods
162
+  //
163
+
164
+  // Called by Temperature::init once at startup
165
+  static void adc_init();
166
+
167
+  // Called by Temperature::init for each sensor at startup
168
+  static void adc_enable(const pin_t ch);
169
+
170
+  // Begin ADC sampling on the given channel
171
+  static void adc_start(const pin_t ch);
172
+
173
+  // Is the ADC ready for reading?
174
+  static inline bool adc_ready() { return true; }
175
+
176
+  // The current value of the ADC register
177
+  static uint16_t adc_value();
178
+
179
+  /**
180
+   * Set the PWM duty cycle for the pin to the given value.
181
+   * No option to invert the duty cycle [default = false]
182
+   * No option to change the scale of the provided value to enable finer PWM duty control [default = 255]
183
+   */
184
+  static inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) {
185
+    analogWrite(pin, v);
186
+  }
187
+
188
+};
189
+
190
+extern MarlinHAL hal;

+ 1
- 1
Marlin/src/HAL/TEENSY31_32/timers.h Целия файл

@@ -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 Целия файл

@@ -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
   }

+ 95
- 38
Marlin/src/HAL/TEENSY35_36/HAL.h Целия файл

@@ -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,45 @@ 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
+// ------------------------
89
+
90
+class libServo;
91
+typedef libServo hal_servo_t;
80 92
 
81 93
 typedef int8_t pin_t;
82 94
 
83
-#ifndef analogInputToDigitalPin
84
-  #define analogInputToDigitalPin(p) ((p < 12U) ? (p) + 54U : -1)
85
-#endif
95
+// ------------------------
96
+// Interrupts
97
+// ------------------------
86 98
 
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()
99
+#define CRITICAL_SECTION_START()  const bool irqon = !__get_primask(); __disable_irq()
100
+#define CRITICAL_SECTION_END()    if (irqon) __enable_irq()
92 101
 
93
-#undef sq
94
-#define sq(x) ((x)*(x))
102
+// ------------------------
103
+// ADC
104
+// ------------------------
95 105
 
96
-inline void HAL_init() {}
106
+#ifndef analogInputToDigitalPin
107
+  #define analogInputToDigitalPin(p) ((p < 12U) ? (p) + 54U : -1)
108
+#endif
97 109
 
98
-// Clear reset reason
99
-void HAL_clear_reset_source();
110
+#define HAL_ADC_VREF         3.3
111
+#define HAL_ADC_RESOLUTION  10
100 112
 
101
-// Reset reason
102
-uint8_t HAL_get_reset_source();
113
+#define HAL_ANALOG_SELECT(pin)
103 114
 
104
-void HAL_reboot();
115
+//
116
+// Pin Mapping for M42, M43, M226
117
+//
118
+#define GET_PIN_MAP_PIN(index) index
119
+#define GET_PIN_MAP_INDEX(pin) pin
120
+#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
105 121
 
106
-FORCE_INLINE void _delay_ms(const int delay_ms) { delay(delay_ms); }
122
+// ------------------------
123
+// Class Utilities
124
+// ------------------------
107 125
 
108 126
 #pragma GCC diagnostic push
109 127
 #if GCC_VERSION <= 50000
@@ -114,27 +132,66 @@ extern "C" int freeMemory();
114 132
 
115 133
 #pragma GCC diagnostic pop
116 134
 
117
-// ADC
135
+// ------------------------
136
+// MarlinHAL Class
137
+// ------------------------
118 138
 
119
-void HAL_adc_init();
139
+class MarlinHAL {
140
+public:
120 141
 
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
142
+  // Earliest possible init, before setup()
143
+  MarlinHAL() {}
126 144
 
127
-#define HAL_ANALOG_SELECT(pin)
145
+  static inline void init() {}        // Called early in setup()
146
+  static inline void init_board() {}  // Called less early in setup()
147
+  static void reboot();               // Restart the firmware from 0x0
128 148
 
129
-void HAL_adc_start_conversion(const uint8_t adc_pin);
130
-uint16_t HAL_adc_get_result();
149
+  static inline bool isr_state() { return true; }
150
+  static inline void isr_on()  { __enable_irq(); }
151
+  static inline void isr_off() { __disable_irq(); }
131 152
 
132
-// PWM
153
+  static inline void delay_ms(const int ms) { delay(ms); }
133 154
 
134
-inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); }
155
+  // Tasks, called from idle()
156
+  static inline void idletask() {}
135 157
 
136
-// Pin Map
158
+  // Reset
159
+  static uint8_t get_reset_source();
160
+  static inline void clear_reset_source() {}
137 161
 
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)
162
+  // Free SRAM
163
+  static inline int freeMemory() { return freeMemory(); }
164
+
165
+  //
166
+  // ADC Methods
167
+  //
168
+
169
+  static int8_t adc_select;
170
+
171
+  // Called by Temperature::init once at startup
172
+  static void adc_init();
173
+
174
+  // Called by Temperature::init for each sensor at startup
175
+  static inline void adc_enable(const pin_t) {}
176
+
177
+  // Begin ADC sampling on the given channel
178
+  static void adc_start(const pin_t pin);
179
+
180
+  // Is the ADC ready for reading?
181
+  static inline bool adc_ready() { return true; }
182
+
183
+  // The current value of the ADC register
184
+  static uint16_t adc_value();
185
+
186
+  /**
187
+   * Set the PWM duty cycle for the pin to the given value.
188
+   * No option to invert the duty cycle [default = false]
189
+   * No option to change the scale of the provided value to enable finer PWM duty control [default = 255]
190
+   */
191
+  static inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) {
192
+    analogWrite(pin, v);
193
+  }
194
+
195
+};
196
+
197
+extern MarlinHAL hal;

+ 1
- 1
Marlin/src/HAL/TEENSY35_36/timers.h Целия файл

@@ -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 Целия файл

@@ -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__

+ 102
- 46
Marlin/src/HAL/TEENSY40_41/HAL.h Целия файл

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

+ 1
- 1
Marlin/src/HAL/TEENSY40_41/timers.h Целия файл

@@ -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 Целия файл

@@ -0,0 +1,36 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (c) 2021 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 Целия файл

@@ -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
 

+ 21
- 27
Marlin/src/MarlinCore.cpp Целия файл

@@ -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();
@@ -1538,7 +1532,7 @@ void setup() {
1538 1532
   #endif
1539 1533
 
1540 1534
   #if ENABLED(USE_WATCHDOG)
1541
-    SETUP_RUN(watchdog_init());       // Reinit watchdog after HAL_get_reset_source call
1535
+    SETUP_RUN(watchdog_init());       // Reinit watchdog after hal.get_reset_source call
1542 1536
   #endif
1543 1537
 
1544 1538
   #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)

+ 1
- 1
Marlin/src/feature/caselight.cpp Целия файл

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

+ 1
- 1
Marlin/src/feature/controllerfan.cpp Целия файл

@@ -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 Целия файл

@@ -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
 

+ 1
- 1
Marlin/src/feature/leds/leds.cpp Целия файл

@@ -123,7 +123,7 @@ void LEDLights::set_color(const LEDColor &incol
123 123
     // If the pins can do PWM then their intensity will be set.
124 124
     #define _UPDATE_RGBW(C,c) do {                 \
125 125
       if (PWM_PIN(RGB_LED_##C##_PIN))              \
126
-        set_pwm_duty(pin_t(RGB_LED_##C##_PIN), c); \
126
+        hal.set_pwm_duty(pin_t(RGB_LED_##C##_PIN), c); \
127 127
       else                                         \
128 128
         WRITE(RGB_LED_##C##_PIN, c ? HIGH : LOW);  \
129 129
     }while(0)

+ 5
- 5
Marlin/src/feature/spindle_laser.cpp Целия файл

@@ -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) && defined(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)
@@ -90,10 +90,10 @@ void SpindleLaser::init() {
90 90
    * @param ocr Power value
91 91
    */
92 92
   void SpindleLaser::_set_ocr(const uint8_t ocr) {
93
-    #if NEEDS_HARDWARE_PWM && SPINDLE_LASER_FREQUENCY
94
-      set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), TERN(MARLIN_DEV_MODE, frequency, SPINDLE_LASER_FREQUENCY));
93
+    #if BOTH(NEEDS_HARDWARE_PWM, HAL_CAN_SET_PWM_FREQ) && SPINDLE_LASER_FREQUENCY
94
+      hal.set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), TERN(MARLIN_DEV_MODE, frequency, SPINDLE_LASER_FREQUENCY));
95 95
     #endif
96
-    set_pwm_duty(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF);
96
+    hal.set_pwm_duty(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF);
97 97
   }
98 98
 
99 99
   void SpindleLaser::set_ocr(const uint8_t ocr) {

+ 1
- 1
Marlin/src/feature/spindle_laser.h Целия файл

@@ -103,7 +103,7 @@ public:
103 103
   static void init();
104 104
 
105 105
   #if ENABLED(MARLIN_DEV_MODE)
106
-    static inline void refresh_frequency() { set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), frequency); }
106
+    static inline 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 Целия файл

@@ -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 Целия файл

@@ -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
 

+ 7
- 0
Marlin/src/inc/SanityCheck.h Целия файл

@@ -3850,3 +3850,10 @@ static_assert(_PLUS_TEST(4), "HOMING_FEEDRATE_MM_M values must be positive.");
3850 3850
 #undef _TEST_PWM
3851 3851
 #undef _LINEAR_AXES_STR
3852 3852
 #undef _LOGICAL_AXES_STR
3853
+
3854
+// JTAG support in the HAL
3855
+#if ENABLED(DISABLE_DEBUG) && !defined(JTAGSWD_DISABLE)
3856
+  #error "DISABLE_DEBUG is not supported for the selected MCU/Board."
3857
+#elif ENABLED(DISABLE_JTAG) && !defined(JTAG_DISABLE)
3858
+  #error "DISABLE_JTAG is not supported for the selected MCU/Board."
3859
+#endif

+ 2
- 2
Marlin/src/lcd/dogm/marlinui_DOGM.cpp Целия файл

@@ -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/enhanced/dwin.cpp Целия файл

@@ -2149,7 +2149,7 @@ void RebootPrinter() {
2149 2149
   thermalManager.disable_all_heaters();
2150 2150
   planner.finish_and_disable();
2151 2151
   DWIN_RebootScreen();
2152
-  HAL_reboot();
2152
+  hal.reboot();
2153 2153
 }
2154 2154
 
2155 2155
 void Goto_InfoMenu(){

+ 1
- 1
Marlin/src/module/endstops.cpp Целия файл

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

+ 3
- 3
Marlin/src/module/planner.cpp Целия файл

@@ -1264,7 +1264,7 @@ void Planner::recalculate() {
1264 1264
     #if ENABLED(FAN_SOFT_PWM)
1265 1265
       #define _FAN_SET(F) thermalManager.soft_pwm_amount_fan[F] = CALC_FAN_SPEED(F);
1266 1266
     #else
1267
-      #define _FAN_SET(F) set_pwm_duty(pin_t(FAN##F##_PIN), CALC_FAN_SPEED(F));
1267
+      #define _FAN_SET(F) hal.set_pwm_duty(pin_t(FAN##F##_PIN), CALC_FAN_SPEED(F));
1268 1268
     #endif
1269 1269
     #define FAN_SET(F) do{ kickstart_fan(fan_speed, ms, F); _FAN_SET(F); }while(0)
1270 1270
 
@@ -1400,8 +1400,8 @@ void Planner::check_axes_activity() {
1400 1400
   TERN_(AUTOTEMP, autotemp_task());
1401 1401
 
1402 1402
   #if ENABLED(BARICUDA)
1403
-    TERN_(HAS_HEATER_1, set_pwm_duty(pin_t(HEATER_1_PIN), tail_valve_pressure));
1404
-    TERN_(HAS_HEATER_2, set_pwm_duty(pin_t(HEATER_2_PIN), tail_e_to_p_pressure));
1403
+    TERN_(HAS_HEATER_1, hal.set_pwm_duty(pin_t(HEATER_1_PIN), tail_valve_pressure));
1404
+    TERN_(HAS_HEATER_2, hal.set_pwm_duty(pin_t(HEATER_2_PIN), tail_e_to_p_pressure));
1405 1405
   #endif
1406 1406
 }
1407 1407
 

+ 1
- 1
Marlin/src/module/servo.cpp Целия файл

@@ -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 Целия файл

@@ -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();

+ 5
- 5
Marlin/src/module/stepper.cpp Целия файл

@@ -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
@@ -3261,7 +3261,7 @@ void Stepper::report_positions() {
3261 3261
 
3262 3262
       #elif HAS_MOTOR_CURRENT_PWM
3263 3263
 
3264
-        #define _WRITE_CURRENT_PWM(P) set_pwm_duty(pin_t(MOTOR_CURRENT_PWM_## P ##_PIN), 255L * current / (MOTOR_CURRENT_PWM_RANGE))
3264
+        #define _WRITE_CURRENT_PWM(P) hal.set_pwm_duty(pin_t(MOTOR_CURRENT_PWM_## P ##_PIN), 255L * current / (MOTOR_CURRENT_PWM_RANGE))
3265 3265
         switch (driver) {
3266 3266
           case 0:
3267 3267
             #if PIN_EXISTS(MOTOR_CURRENT_PWM_X)

+ 56
- 56
Marlin/src/module/temperature.cpp Целия файл

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

+ 2
- 2
ini/native.ini Целия файл

@@ -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/thinkyhead/MarlinSimUI/archive/updated_marlin_hal_2093.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…
Отказ
Запис