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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. // Class Utilities
  42. // ------------------------
  43. #define __bss_end _ebss
  44. extern "C" {
  45. extern char __bss_end;
  46. extern char __heap_start;
  47. extern void* __brkval;
  48. // Doesn't work on Teensy 4.x
  49. uint32_t freeMemory() {
  50. uint32_t free_memory;
  51. free_memory = ((uint32_t)&free_memory) - (((uint32_t)__brkval) ?: ((uint32_t)&__bss_end));
  52. return free_memory;
  53. }
  54. }
  55. // ------------------------
  56. // FastIO
  57. // ------------------------
  58. bool is_output(pin_t pin) {
  59. const struct digital_pin_bitband_and_config_table_struct *p;
  60. p = digital_pin_to_info_PGM + pin;
  61. return (*(p->reg + 1) & p->mask);
  62. }
  63. // ------------------------
  64. // MarlinHAL Class
  65. // ------------------------
  66. void MarlinHAL::reboot() { _reboot_Teensyduino_(); }
  67. uint8_t MarlinHAL::get_reset_source() {
  68. switch (SRC_SRSR & 0xFF) {
  69. case 1: return RST_POWER_ON; break;
  70. case 2: return RST_SOFTWARE; break;
  71. case 4: return RST_EXTERNAL; break;
  72. //case 8: return RST_BROWN_OUT; break;
  73. case 16: return RST_WATCHDOG; break;
  74. case 64: return RST_JTAG; break;
  75. //case 128: return RST_OVERTEMP; break;
  76. }
  77. return 0;
  78. }
  79. void MarlinHAL::clear_reset_source() {
  80. uint32_t reset_source = SRC_SRSR;
  81. SRC_SRSR = reset_source;
  82. }
  83. // ADC
  84. int8_t MarlinHAL::adc_select;
  85. void MarlinHAL::adc_init() {
  86. analog_init();
  87. while (ADC1_GC & ADC_GC_CAL) { /* wait */ }
  88. while (ADC2_GC & ADC_GC_CAL) { /* wait */ }
  89. }
  90. void MarlinHAL::adc_start(const pin_t adc_pin) {
  91. static const uint8_t pin2sc1a[] = {
  92. 0x07, // 0/A0 AD_B1_02
  93. 0x08, // 1/A1 AD_B1_03
  94. 0x0C, // 2/A2 AD_B1_07
  95. 0x0B, // 3/A3 AD_B1_06
  96. 0x06, // 4/A4 AD_B1_01
  97. 0x05, // 5/A5 AD_B1_00
  98. 0x0F, // 6/A6 AD_B1_10
  99. 0x00, // 7/A7 AD_B1_11
  100. 0x0D, // 8/A8 AD_B1_08
  101. 0x0E, // 9/A9 AD_B1_09
  102. 0x01, // 24/A10 AD_B0_12
  103. 0x02, // 25/A11 AD_B0_13
  104. 0x83, // 26/A12 AD_B1_14 - only on ADC2, 3
  105. 0x84, // 27/A13 AD_B1_15 - only on ADC2, 4
  106. 0x07, // 14/A0 AD_B1_02
  107. 0x08, // 15/A1 AD_B1_03
  108. 0x0C, // 16/A2 AD_B1_07
  109. 0x0B, // 17/A3 AD_B1_06
  110. 0x06, // 18/A4 AD_B1_01
  111. 0x05, // 19/A5 AD_B1_00
  112. 0x0F, // 20/A6 AD_B1_10
  113. 0x00, // 21/A7 AD_B1_11
  114. 0x0D, // 22/A8 AD_B1_08
  115. 0x0E, // 23/A9 AD_B1_09
  116. 0x01, // 24/A10 AD_B0_12
  117. 0x02, // 25/A11 AD_B0_13
  118. 0x83, // 26/A12 AD_B1_14 - only on ADC2, 3
  119. 0x84, // 27/A13 AD_B1_15 - only on ADC2, 4
  120. #ifdef ARDUINO_TEENSY41
  121. 0xFF, // 28
  122. 0xFF, // 29
  123. 0xFF, // 30
  124. 0xFF, // 31
  125. 0xFF, // 32
  126. 0xFF, // 33
  127. 0xFF, // 34
  128. 0xFF, // 35
  129. 0xFF, // 36
  130. 0xFF, // 37
  131. 0x81, // 38/A14 AD_B1_12 - only on ADC2, 1
  132. 0x82, // 39/A15 AD_B1_13 - only on ADC2, 2
  133. 0x09, // 40/A16 AD_B1_04
  134. 0x0A, // 41/A17 AD_B1_05
  135. #endif
  136. };
  137. const uint16_t pin = pin2sc1a[adc_pin];
  138. if (pin == 0xFF) {
  139. adc_select = -1; // Digital only
  140. }
  141. else if (pin & 0x80) {
  142. adc_select = 1;
  143. ADC2_HC0 = pin & 0x7F;
  144. }
  145. else {
  146. adc_select = 0;
  147. ADC1_HC0 = pin;
  148. }
  149. }
  150. uint16_t MarlinHAL::adc_value() {
  151. switch (adc_select) {
  152. case 0:
  153. while (!(ADC1_HS & ADC_HS_COCO0)) { /* wait */ }
  154. return ADC1_R0;
  155. case 1:
  156. while (!(ADC2_HS & ADC_HS_COCO0)) { /* wait */ }
  157. return ADC2_R0;
  158. }
  159. return 0;
  160. }
  161. #endif // __IMXRT1062__