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

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