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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 Teensy 4.0 (IMXRT1062DVL6A) / 4.1 (IMXRT1062DVJ6A)
  25. */
  26. #define CPU_32_BIT
  27. #include "../shared/Marduino.h"
  28. #include "../shared/math_32bit.h"
  29. #include "../shared/HAL_SPI.h"
  30. #include "fastio.h"
  31. #include <stdint.h>
  32. #include <util/atomic.h>
  33. #if HAS_ETHERNET
  34. #include "../../feature/ethernet.h"
  35. #endif
  36. // ------------------------
  37. // Defines
  38. // ------------------------
  39. #define IS_32BIT_TEENSY 1
  40. #define IS_TEENSY_40_41 1
  41. #ifndef IS_TEENSY40
  42. #define IS_TEENSY41 1
  43. #endif
  44. #define CPU_ST7920_DELAY_1 600
  45. #define CPU_ST7920_DELAY_2 750
  46. #define CPU_ST7920_DELAY_3 750
  47. #undef sq
  48. #define sq(x) ((x)*(x))
  49. // Don't place string constants in PROGMEM
  50. #undef PSTR
  51. #define PSTR(str) ({static const char *data = (str); &data[0];})
  52. // ------------------------
  53. // Serial ports
  54. // ------------------------
  55. #include "../../core/serial_hook.h"
  56. #define Serial0 Serial
  57. #define _DECLARE_SERIAL(X) \
  58. typedef ForwardSerial1Class<decltype(Serial##X)> DefaultSerial##X; \
  59. extern DefaultSerial##X MSerial##X
  60. #define DECLARE_SERIAL(X) _DECLARE_SERIAL(X)
  61. typedef ForwardSerial1Class<decltype(SerialUSB)> USBSerialType;
  62. extern USBSerialType USBSerial;
  63. #define _MSERIAL(X) MSerial##X
  64. #define MSERIAL(X) _MSERIAL(X)
  65. #if SERIAL_PORT == -1
  66. #define MYSERIAL1 SerialUSB
  67. #elif WITHIN(SERIAL_PORT, 0, 8)
  68. DECLARE_SERIAL(SERIAL_PORT);
  69. #define MYSERIAL1 MSERIAL(SERIAL_PORT)
  70. #else
  71. #error "The required SERIAL_PORT must be from 0 to 8, or -1 for Native USB."
  72. #endif
  73. #ifdef SERIAL_PORT_2
  74. #if SERIAL_PORT_2 == -1
  75. #define MYSERIAL2 usbSerial
  76. #elif SERIAL_PORT_2 == -2
  77. #define MYSERIAL2 ethernet.telnetClient
  78. #elif WITHIN(SERIAL_PORT_2, 0, 8)
  79. #define MYSERIAL2 MSERIAL(SERIAL_PORT_2)
  80. #else
  81. #error "SERIAL_PORT_2 must be from 0 to 8, or -1 for Native USB, or -2 for Ethernet."
  82. #endif
  83. #endif
  84. // ------------------------
  85. // Types
  86. // ------------------------
  87. class libServo;
  88. typedef libServo hal_servo_t;
  89. typedef int8_t pin_t;
  90. // ------------------------
  91. // Interrupts
  92. // ------------------------
  93. #define CRITICAL_SECTION_START() const bool irqon = !__get_primask(); __disable_irq()
  94. #define CRITICAL_SECTION_END() if (irqon) __enable_irq()
  95. // ------------------------
  96. // ADC
  97. // ------------------------
  98. #ifndef analogInputToDigitalPin
  99. #define analogInputToDigitalPin(p) ((p < 12U) ? (p) + 54U : -1)
  100. #endif
  101. #define HAL_ADC_VREF 3.3
  102. #define HAL_ADC_RESOLUTION 10
  103. #define HAL_ADC_FILTERED // turn off ADC oversampling
  104. //
  105. // Pin Mapping for M42, M43, M226
  106. //
  107. #define GET_PIN_MAP_PIN(index) index
  108. #define GET_PIN_MAP_INDEX(pin) pin
  109. #define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
  110. // FastIO
  111. bool is_output(pin_t pin);
  112. // ------------------------
  113. // Free Memory Accessor
  114. // ------------------------
  115. #pragma GCC diagnostic push
  116. #if GCC_VERSION <= 50000
  117. #pragma GCC diagnostic ignored "-Wunused-function"
  118. #endif
  119. extern "C" uint32_t freeMemory();
  120. #pragma GCC diagnostic pop
  121. // ------------------------
  122. // MarlinHAL Class
  123. // ------------------------
  124. class MarlinHAL {
  125. public:
  126. // Earliest possible init, before setup()
  127. MarlinHAL() {}
  128. // Watchdog
  129. static void watchdog_init() IF_DISABLED(USE_WATCHDOG, {});
  130. static void watchdog_refresh() IF_DISABLED(USE_WATCHDOG, {});
  131. static void init() {} // Called early in setup()
  132. static void init_board() {} // Called less early in setup()
  133. static void reboot(); // Restart the firmware from 0x0
  134. // Interrupts
  135. static bool isr_state() { return !__get_primask(); }
  136. static void isr_on() { __enable_irq(); }
  137. static void isr_off() { __disable_irq(); }
  138. static void delay_ms(const int ms) { delay(ms); }
  139. // Tasks, called from idle()
  140. static void idletask() {}
  141. // Reset
  142. static uint8_t get_reset_source();
  143. static void clear_reset_source();
  144. // Free SRAM
  145. static int freeMemory() { return ::freeMemory(); }
  146. //
  147. // ADC Methods
  148. //
  149. static int8_t adc_select;
  150. // Called by Temperature::init once at startup
  151. static void adc_init();
  152. // Called by Temperature::init for each sensor at startup
  153. static void adc_enable(const pin_t pin) {}
  154. // Begin ADC sampling on the given pin. Called from Temperature::isr!
  155. static void adc_start(const pin_t pin);
  156. // Is the ADC ready for reading?
  157. static bool adc_ready() { return true; }
  158. // The current value of the ADC register
  159. static uint16_t adc_value();
  160. /**
  161. * Set the PWM duty cycle for the pin to the given value.
  162. * No option to invert the duty cycle [default = false]
  163. * No option to change the scale of the provided value to enable finer PWM duty control [default = 255]
  164. */
  165. static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) {
  166. analogWrite(pin, v);
  167. }
  168. };