My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HAL.h 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  5. * SAMD51 HAL developed by Giuliano Zaro (AKA GMagician)
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #pragma once
  22. #define CPU_32_BIT
  23. #include "../shared/Marduino.h"
  24. #include "../shared/math_32bit.h"
  25. #include "../shared/HAL_SPI.h"
  26. #include "fastio.h"
  27. #include "watchdog.h"
  28. #ifdef ADAFRUIT_GRAND_CENTRAL_M4
  29. #include "MarlinSerial_AGCM4.h"
  30. // Serial ports
  31. typedef ForwardSerial1Class< decltype(Serial) > DefaultSerial1;
  32. typedef ForwardSerial1Class< decltype(Serial1) > DefaultSerial2;
  33. typedef ForwardSerial1Class< decltype(Serial2) > DefaultSerial3;
  34. typedef ForwardSerial1Class< decltype(Serial3) > DefaultSerial4;
  35. typedef ForwardSerial1Class< decltype(Serial4) > DefaultSerial5;
  36. extern DefaultSerial1 MSerial0;
  37. extern DefaultSerial2 MSerial1;
  38. extern DefaultSerial3 MSerial2;
  39. extern DefaultSerial4 MSerial3;
  40. extern DefaultSerial5 MSerial4;
  41. #define __MSERIAL(X) MSerial##X
  42. #define _MSERIAL(X) __MSERIAL(X)
  43. #define MSERIAL(X) _MSERIAL(INCREMENT(X))
  44. #if SERIAL_PORT == -1
  45. #define MYSERIAL1 MSerial0
  46. #elif WITHIN(SERIAL_PORT, 0, 3)
  47. #define MYSERIAL1 MSERIAL(SERIAL_PORT)
  48. #else
  49. #error "SERIAL_PORT must be from 0 to 3. You can also use -1 if the board supports Native USB."
  50. #endif
  51. #ifdef SERIAL_PORT_2
  52. #if SERIAL_PORT_2 == -1
  53. #define MYSERIAL2 MSerial0
  54. #elif WITHIN(SERIAL_PORT_2, 0, 3)
  55. #define MYSERIAL2 MSERIAL(SERIAL_PORT_2)
  56. #else
  57. #error "SERIAL_PORT_2 must be from 0 to 3. You can also use -1 if the board supports Native USB."
  58. #endif
  59. #endif
  60. #ifdef MMU2_SERIAL_PORT
  61. #if MMU2_SERIAL_PORT == -1
  62. #define MMU2_SERIAL MSerial0
  63. #elif WITHIN(MMU2_SERIAL_PORT, 0, 3)
  64. #define MMU2_SERIAL MSERIAL(MMU2_SERIAL_PORT)
  65. #else
  66. #error "MMU2_SERIAL_PORT must be from 0 to 3. You can also use -1 if the board supports Native USB."
  67. #endif
  68. #endif
  69. #ifdef LCD_SERIAL_PORT
  70. #if LCD_SERIAL_PORT == -1
  71. #define LCD_SERIAL MSerial0
  72. #elif WITHIN(LCD_SERIAL_PORT, 0, 3)
  73. #define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT)
  74. #else
  75. #error "LCD_SERIAL_PORT must be from 0 to 3. You can also use -1 if the board supports Native USB."
  76. #endif
  77. #endif
  78. #endif // ADAFRUIT_GRAND_CENTRAL_M4
  79. typedef int8_t pin_t;
  80. #define SHARED_SERVOS HAS_SERVOS // Use shared/servos.cpp
  81. class Servo;
  82. typedef Servo hal_servo_t;
  83. //
  84. // Interrupts
  85. //
  86. #define CRITICAL_SECTION_START() const bool irqon = !__get_PRIMASK(); __disable_irq()
  87. #define CRITICAL_SECTION_END() if (irqon) __enable_irq()
  88. #define cli() __disable_irq() // Disable interrupts
  89. #define sei() __enable_irq() // Enable interrupts
  90. //
  91. // ADC
  92. //
  93. //#define HAL_ADC_FILTERED // Disable Marlin's oversampling. The HAL filters ADC values.
  94. #define HAL_ADC_VREF 3.3
  95. #define HAL_ADC_RESOLUTION 10 // ... 12
  96. //
  97. // Pin Mapping for M42, M43, M226
  98. //
  99. #define GET_PIN_MAP_PIN(index) index
  100. #define GET_PIN_MAP_INDEX(pin) pin
  101. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  102. //
  103. // Tone
  104. //
  105. void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration=0);
  106. void noTone(const pin_t _pin);
  107. // ------------------------
  108. // Class Utilities
  109. // ------------------------
  110. #pragma GCC diagnostic push
  111. #if GCC_VERSION <= 50000
  112. #pragma GCC diagnostic ignored "-Wunused-function"
  113. #endif
  114. #ifdef __cplusplus
  115. extern "C" {
  116. #endif
  117. char *dtostrf(double __val, signed char __width, unsigned char __prec, char *__s);
  118. extern "C" int freeMemory();
  119. #ifdef __cplusplus
  120. }
  121. #endif
  122. #pragma GCC diagnostic pop
  123. // ------------------------
  124. // MarlinHAL Class
  125. // ------------------------
  126. class MarlinHAL {
  127. public:
  128. // Earliest possible init, before setup()
  129. MarlinHAL() {}
  130. static void init(); // Called early in setup()
  131. static void init_board() {} // Called less early in setup()
  132. static void reboot(); // Restart the firmware from 0x0
  133. // Interrupts
  134. static bool isr_state() { return !__get_PRIMASK(); }
  135. static void isr_on() { sei(); }
  136. static void isr_off() { cli(); }
  137. static void delay_ms(const int ms) { delay(ms); }
  138. // Tasks, called from idle()
  139. static void idletask() {}
  140. // Reset
  141. static uint8_t get_reset_source();
  142. static void clear_reset_source() {}
  143. // Free SRAM
  144. static int freeMemory() { return ::freeMemory(); }
  145. //
  146. // ADC Methods
  147. //
  148. static uint16_t adc_result;
  149. // Called by Temperature::init once at startup
  150. static void adc_init();
  151. // Called by Temperature::init for each sensor at startup
  152. static void adc_enable(const uint8_t ch) {}
  153. // Begin ADC sampling on the given channel
  154. static void adc_start(const pin_t pin);
  155. // Is the ADC ready for reading?
  156. static bool adc_ready() { return true; }
  157. // The current value of the ADC register
  158. static uint16_t adc_value() { return adc_result; }
  159. /**
  160. * Set the PWM duty cycle for the pin to the given value.
  161. * No option to invert the duty cycle [default = false]
  162. * No option to change the scale of the provided value to enable finer PWM duty control [default = 255]
  163. */
  164. static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) {
  165. analogWrite(pin, v);
  166. }
  167. private:
  168. static void dma_init();
  169. };