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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 3.5 (MK64FX512) and Teensy 3.6 (MK66FX1M0)
  24. */
  25. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  26. #include "HAL.h"
  27. #include "../shared/Delay.h"
  28. #include <Wire.h>
  29. // ------------------------
  30. // Serial ports
  31. // ------------------------
  32. #define _IMPLEMENT_SERIAL(X) DefaultSerial##X MSerial##X(false, Serial##X)
  33. #define IMPLEMENT_SERIAL(X) _IMPLEMENT_SERIAL(X)
  34. #if WITHIN(SERIAL_PORT, 0, 3)
  35. IMPLEMENT_SERIAL(SERIAL_PORT);
  36. #endif
  37. USBSerialType USBSerial(false, SerialUSB);
  38. // ------------------------
  39. // MarlinHAL Class
  40. // ------------------------
  41. void MarlinHAL::reboot() { _reboot_Teensyduino_(); }
  42. uint8_t MarlinHAL::get_reset_source() {
  43. switch (RCM_SRS0) {
  44. case 128: return RST_POWER_ON; break;
  45. case 64: return RST_EXTERNAL; break;
  46. case 32: return RST_WATCHDOG; break;
  47. // case 8: return RST_LOSS_OF_LOCK; break;
  48. // case 4: return RST_LOSS_OF_CLOCK; break;
  49. // case 2: return RST_LOW_VOLTAGE; break;
  50. }
  51. return 0;
  52. }
  53. // ------------------------
  54. // Watchdog Timer
  55. // ------------------------
  56. #if ENABLED(USE_WATCHDOG)
  57. #define WDT_TIMEOUT_MS TERN(WATCHDOG_DURATION_8S, 8000, 4000) // 4 or 8 second timeout
  58. void MarlinHAL::watchdog_init() {
  59. WDOG_TOVALH = 0;
  60. WDOG_TOVALL = WDT_TIMEOUT_MS;
  61. WDOG_STCTRLH = WDOG_STCTRLH_WDOGEN;
  62. }
  63. void MarlinHAL::watchdog_refresh() {
  64. // Watchdog refresh sequence
  65. WDOG_REFRESH = 0xA602;
  66. WDOG_REFRESH = 0xB480;
  67. }
  68. #endif
  69. // ------------------------
  70. // ADC
  71. // ------------------------
  72. int8_t MarlinHAL::adc_select;
  73. void MarlinHAL::adc_init() {
  74. analog_init();
  75. while (ADC0_SC3 & ADC_SC3_CAL) { /* Wait for calibration to finish */ }
  76. while (ADC1_SC3 & ADC_SC3_CAL) { /* Wait for calibration to finish */ }
  77. NVIC_ENABLE_IRQ(IRQ_FTM1);
  78. }
  79. void MarlinHAL::adc_start(const pin_t adc_pin) {
  80. static const uint8_t pin2sc1a[] = {
  81. 5, 14, 8, 9, 13, 12, 6, 7, 15, 4, 3, 19+128, 14+128, 15+128, // 0-13 -> A0-A13
  82. 5, 14, 8, 9, 13, 12, 6, 7, 15, 4, // 14-23 are A0-A9
  83. 255, 255, 255, 255, 255, 255, 255, // 24-30 are digital only
  84. 14+128, 15+128, 17, 18, 4+128, 5+128, 6+128, 7+128, 17+128, // 31-39 are A12-A20
  85. 255, 255, 255, 255, 255, 255, 255, 255, 255, // 40-48 are digital only
  86. 10+128, 11+128, // 49-50 are A23-A24
  87. 255, 255, 255, 255, 255, 255, 255, // 51-57 are digital only
  88. 255, 255, 255, 255, 255, 255, // 58-63 (sd card pins) are digital only
  89. 3, 19+128, // 64-65 are A10-A11
  90. 23, 23+128,// 66-67 are A21-A22 (DAC pins)
  91. 1, 1+128, // 68-69 are A25-A26 (unused USB host port on Teensy 3.5)
  92. 26, // 70 is Temperature Sensor
  93. 18+128 // 71 is Vref
  94. };
  95. const uint16_t pin = pin2sc1a[adc_pin];
  96. if (pin == 0xFF) {
  97. adc_select = -1; // Digital only
  98. }
  99. else if (pin & 0x80) {
  100. adc_select = 1;
  101. ADC1_SC1A = pin & 0x7F;
  102. }
  103. else {
  104. adc_select = 0;
  105. ADC0_SC1A = pin;
  106. }
  107. }
  108. uint16_t MarlinHAL::adc_value() {
  109. switch (adc_select) {
  110. case 0: return ADC0_RA;
  111. case 1: return ADC1_RA;
  112. }
  113. return 0;
  114. }
  115. // ------------------------
  116. // Free Memory Accessor
  117. // ------------------------
  118. extern "C" {
  119. extern char __bss_end;
  120. extern char __heap_start;
  121. extern void* __brkval;
  122. int freeMemory() {
  123. int free_memory;
  124. if ((int)__brkval == 0)
  125. free_memory = ((int)&free_memory) - ((int)&__bss_end);
  126. else
  127. free_memory = ((int)&free_memory) - ((int)__brkval);
  128. return free_memory;
  129. }
  130. }
  131. #endif // __MK64FX512__ || __MK66FX1M0__