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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. */
  20. /**
  21. * HAL for Arduino Due and compatible (SAM3X8E)
  22. */
  23. #ifdef ARDUINO_ARCH_SAM
  24. #include "../../inc/MarlinConfig.h"
  25. #include "../../MarlinCore.h"
  26. #include <Wire.h>
  27. #include "usb/usb_task.h"
  28. // ------------------------
  29. // Public Variables
  30. // ------------------------
  31. uint16_t MarlinHAL::adc_result;
  32. // ------------------------
  33. // Public functions
  34. // ------------------------
  35. #if ENABLED(POSTMORTEM_DEBUGGING)
  36. extern void install_min_serial();
  37. #endif
  38. void MarlinHAL::init() {
  39. #if ENABLED(SDSUPPORT)
  40. OUT_WRITE(SDSS, HIGH); // Try to set SDSS inactive before any other SPI users start up
  41. #endif
  42. usb_task_init(); // Initialize the USB stack
  43. TERN_(POSTMORTEM_DEBUGGING, install_min_serial()); // Install the min serial handler
  44. }
  45. void MarlinHAL::init_board() {
  46. #ifdef BOARD_INIT
  47. BOARD_INIT();
  48. #endif
  49. }
  50. void MarlinHAL::idletask() { usb_task_idle(); } // Perform USB stack housekeeping
  51. uint8_t MarlinHAL::get_reset_source() {
  52. switch ((RSTC->RSTC_SR >> 8) & 0x07) {
  53. case 0: return RST_POWER_ON;
  54. case 1: return RST_BACKUP;
  55. case 2: return RST_WATCHDOG;
  56. case 3: return RST_SOFTWARE;
  57. case 4: return RST_EXTERNAL;
  58. default: return 0;
  59. }
  60. }
  61. void MarlinHAL::reboot() { rstc_start_software_reset(RSTC); }
  62. // ------------------------
  63. // Watchdog Timer
  64. // ------------------------
  65. #if ENABLED(USE_WATCHDOG)
  66. // Initialize watchdog - On SAM3X, Watchdog was already configured
  67. // and enabled or disabled at startup, so no need to reconfigure it
  68. // here.
  69. void MarlinHAL::watchdog_init() { WDT_Restart(WDT); } // Reset watchdog to start clean
  70. // Reset watchdog. MUST be called at least every 4 seconds after the
  71. // first watchdog_init or AVR will go into emergency procedures.
  72. void MarlinHAL::watchdog_refresh() { watchdogReset(); }
  73. #endif
  74. // Override Arduino runtime to either config or disable the watchdog
  75. //
  76. // We need to configure the watchdog as soon as possible in the boot
  77. // process, because watchdog initialization at hardware reset on SAM3X8E
  78. // is unreliable, and there is risk of unintended resets if we delay
  79. // that initialization to a later time.
  80. void watchdogSetup() {
  81. #if ENABLED(USE_WATCHDOG)
  82. // 4 seconds timeout
  83. uint32_t timeout = TERN(WATCHDOG_DURATION_8S, 8000, 4000);
  84. // Calculate timeout value in WDT counter ticks: This assumes
  85. // the slow clock is running at 32.768 kHz watchdog
  86. // frequency is therefore 32768 / 128 = 256 Hz
  87. timeout = (timeout << 8) / 1000;
  88. if (timeout == 0)
  89. timeout = 1;
  90. else if (timeout > 0xFFF)
  91. timeout = 0xFFF;
  92. // We want to enable the watchdog with the specified timeout
  93. uint32_t value =
  94. WDT_MR_WDV(timeout) | // With the specified timeout
  95. WDT_MR_WDD(timeout) | // and no invalid write window
  96. #if !(SAMV70 || SAMV71 || SAME70 || SAMS70)
  97. WDT_MR_WDRPROC | // WDT fault resets processor only - We want
  98. // to keep PIO controller state
  99. #endif
  100. WDT_MR_WDDBGHLT | // WDT stops in debug state.
  101. WDT_MR_WDIDLEHLT; // WDT stops in idle state.
  102. #if ENABLED(WATCHDOG_RESET_MANUAL)
  103. // We enable the watchdog timer, but only for the interrupt.
  104. // Configure WDT to only trigger an interrupt
  105. value |= WDT_MR_WDFIEN; // Enable WDT fault interrupt.
  106. // Disable WDT interrupt (just in case, to avoid triggering it!)
  107. NVIC_DisableIRQ(WDT_IRQn);
  108. // We NEED memory barriers to ensure Interrupts are actually disabled!
  109. // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
  110. __DSB();
  111. __ISB();
  112. // Initialize WDT with the given parameters
  113. WDT_Enable(WDT, value);
  114. // Configure and enable WDT interrupt.
  115. NVIC_ClearPendingIRQ(WDT_IRQn);
  116. NVIC_SetPriority(WDT_IRQn, 0); // Use highest priority, so we detect all kinds of lockups
  117. NVIC_EnableIRQ(WDT_IRQn);
  118. #else
  119. // a WDT fault triggers a reset
  120. value |= WDT_MR_WDRSTEN;
  121. // Initialize WDT with the given parameters
  122. WDT_Enable(WDT, value);
  123. #endif
  124. // Reset the watchdog
  125. WDT_Restart(WDT);
  126. #else
  127. // Make sure to completely disable the Watchdog
  128. WDT_Disable(WDT);
  129. #endif
  130. }
  131. // ------------------------
  132. // Free Memory Accessor
  133. // ------------------------
  134. extern "C" {
  135. extern unsigned int _ebss; // end of bss section
  136. }
  137. // Return free memory between end of heap (or end bss) and whatever is current
  138. int freeMemory() {
  139. int free_memory, heap_end = (int)_sbrk(0);
  140. return (int)&free_memory - (heap_end ?: (int)&_ebss);
  141. }
  142. // ------------------------
  143. // Serial Ports
  144. // ------------------------
  145. // Forward the default serial ports
  146. #if USING_HW_SERIAL0
  147. DefaultSerial1 MSerial0(false, Serial);
  148. #endif
  149. #if USING_HW_SERIAL1
  150. DefaultSerial2 MSerial1(false, Serial1);
  151. #endif
  152. #if USING_HW_SERIAL2
  153. DefaultSerial3 MSerial2(false, Serial2);
  154. #endif
  155. #if USING_HW_SERIAL3
  156. DefaultSerial4 MSerial3(false, Serial3);
  157. #endif
  158. #endif // ARDUINO_ARCH_SAM