My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

HAL.cpp 4.1KB

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