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 4.4KB

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