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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  5. * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
  6. * Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com
  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. #pragma once
  23. /**
  24. * HAL for Arduino Due and compatible (SAM3X8E)
  25. */
  26. #define CPU_32_BIT
  27. #include "../shared/Marduino.h"
  28. #include "../shared/eeprom_if.h"
  29. #include "../shared/math_32bit.h"
  30. #include "../shared/HAL_SPI.h"
  31. #include "fastio.h"
  32. #include "watchdog.h"
  33. #include <stdint.h>
  34. #include "../../core/serial_hook.h"
  35. // ------------------------
  36. // Serial ports
  37. // ------------------------
  38. typedef ForwardSerial1Class< decltype(Serial) > DefaultSerial1;
  39. typedef ForwardSerial1Class< decltype(Serial1) > DefaultSerial2;
  40. typedef ForwardSerial1Class< decltype(Serial2) > DefaultSerial3;
  41. typedef ForwardSerial1Class< decltype(Serial3) > DefaultSerial4;
  42. extern DefaultSerial1 MSerial0;
  43. extern DefaultSerial2 MSerial1;
  44. extern DefaultSerial3 MSerial2;
  45. extern DefaultSerial4 MSerial3;
  46. #define _MSERIAL(X) MSerial##X
  47. #define MSERIAL(X) _MSERIAL(X)
  48. #if SERIAL_PORT == -1 || ENABLED(EMERGENCY_PARSER)
  49. #define MYSERIAL1 customizedSerial1
  50. #elif WITHIN(SERIAL_PORT, 0, 3)
  51. #define MYSERIAL1 MSERIAL(SERIAL_PORT)
  52. #else
  53. #error "The required SERIAL_PORT must be from 0 to 3, or -1 for USB Serial."
  54. #endif
  55. #ifdef SERIAL_PORT_2
  56. #if SERIAL_PORT_2 == -1 || ENABLED(EMERGENCY_PARSER)
  57. #define MYSERIAL2 customizedSerial2
  58. #elif WITHIN(SERIAL_PORT_2, 0, 3)
  59. #define MYSERIAL2 MSERIAL(SERIAL_PORT_2)
  60. #else
  61. #error "SERIAL_PORT_2 must be from 0 to 3, or -1 for USB Serial."
  62. #endif
  63. #endif
  64. #ifdef SERIAL_PORT_3
  65. #if SERIAL_PORT_3 == -1 || ENABLED(EMERGENCY_PARSER)
  66. #define MYSERIAL3 customizedSerial3
  67. #elif WITHIN(SERIAL_PORT_3, 0, 3)
  68. #define MYSERIAL3 MSERIAL(SERIAL_PORT_3)
  69. #else
  70. #error "SERIAL_PORT_3 must be from 0 to 3, or -1 for USB Serial."
  71. #endif
  72. #endif
  73. #ifdef MMU2_SERIAL_PORT
  74. #if WITHIN(MMU2_SERIAL_PORT, 0, 3)
  75. #define MMU2_SERIAL MSERIAL(MMU2_SERIAL_PORT)
  76. #else
  77. #error "MMU2_SERIAL_PORT must be from 0 to 3."
  78. #endif
  79. #endif
  80. #ifdef LCD_SERIAL_PORT
  81. #if WITHIN(LCD_SERIAL_PORT, 0, 3)
  82. #define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT)
  83. #else
  84. #error "LCD_SERIAL_PORT must be from 0 to 3."
  85. #endif
  86. #endif
  87. #include "MarlinSerial.h"
  88. #include "MarlinSerialUSB.h"
  89. // ------------------------
  90. // Types
  91. // ------------------------
  92. typedef int8_t pin_t;
  93. #define SHARED_SERVOS HAS_SERVOS // Use shared/servos.cpp
  94. class Servo;
  95. typedef Servo hal_servo_t;
  96. //
  97. // Interrupts
  98. //
  99. #define sei() noInterrupts()
  100. #define cli() interrupts()
  101. #define CRITICAL_SECTION_START() const bool _irqon = hal.isr_state(); hal.isr_off()
  102. #define CRITICAL_SECTION_END() if (_irqon) hal.isr_on()
  103. //
  104. // ADC
  105. //
  106. #define HAL_ADC_VREF 3.3
  107. #define HAL_ADC_RESOLUTION 10
  108. #ifndef analogInputToDigitalPin
  109. #define analogInputToDigitalPin(p) ((p < 12U) ? (p) + 54U : -1)
  110. #endif
  111. //
  112. // Pin Mapping for M42, M43, M226
  113. //
  114. #define GET_PIN_MAP_PIN(index) index
  115. #define GET_PIN_MAP_INDEX(pin) pin
  116. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  117. //
  118. // Tone
  119. //
  120. void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration=0);
  121. void noTone(const pin_t _pin);
  122. // ------------------------
  123. // Class Utilities
  124. // ------------------------
  125. #pragma GCC diagnostic push
  126. #if GCC_VERSION <= 50000
  127. #pragma GCC diagnostic ignored "-Wunused-function"
  128. #endif
  129. #pragma GCC diagnostic pop
  130. #ifdef __cplusplus
  131. extern "C" {
  132. #endif
  133. char *dtostrf(double __val, signed char __width, unsigned char __prec, char *__s);
  134. #ifdef __cplusplus
  135. }
  136. #endif
  137. // Return free RAM between end of heap (or end bss) and whatever is current
  138. int freeMemory();
  139. // ------------------------
  140. // MarlinHAL Class
  141. // ------------------------
  142. class MarlinHAL {
  143. public:
  144. // Earliest possible init, before setup()
  145. MarlinHAL() {}
  146. static void init(); // Called early in setup()
  147. static void init_board(); // Called less early in setup()
  148. static void reboot(); // Software reset
  149. // Interrupts
  150. static bool isr_state() { return !__get_PRIMASK(); }
  151. static void isr_on() { __enable_irq(); }
  152. static void isr_off() { __disable_irq(); }
  153. static void delay_ms(const int ms) { delay(ms); }
  154. // Tasks, called from idle()
  155. static void idletask();
  156. // Reset
  157. static uint8_t get_reset_source();
  158. static void clear_reset_source() {}
  159. // Free SRAM
  160. static int freeMemory() { return ::freeMemory(); }
  161. //
  162. // ADC Methods
  163. //
  164. static uint16_t adc_result;
  165. // Called by Temperature::init once at startup
  166. static void adc_init() {}
  167. // Called by Temperature::init for each sensor at startup
  168. static void adc_enable(const uint8_t ch) {}
  169. // Begin ADC sampling on the given channel
  170. static void adc_start(const uint8_t ch) { adc_result = analogRead(ch); }
  171. // Is the ADC ready for reading?
  172. static bool adc_ready() { return true; }
  173. // The current value of the ADC register
  174. static uint16_t adc_value() { return adc_result; }
  175. /**
  176. * Set the PWM duty cycle for the pin to the given value.
  177. * No inverting the duty cycle in this HAL.
  178. * No changing the maximum size of the provided value to enable finer PWM duty control in this HAL.
  179. */
  180. static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) {
  181. analogWrite(pin, v);
  182. }
  183. };