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.cpp 5.3KB

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