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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #ifdef __AVR__
  23. #include "../../inc/MarlinConfig.h"
  24. #include "HAL.h"
  25. #include <avr/wdt.h>
  26. #ifdef USBCON
  27. DefaultSerial1 MSerial0(false, Serial);
  28. #ifdef BLUETOOTH
  29. BTSerial btSerial(false, bluetoothSerial);
  30. #endif
  31. #endif
  32. // ------------------------
  33. // Public Variables
  34. // ------------------------
  35. // Don't initialize/override variable (which would happen in .init4)
  36. uint8_t MarlinHAL::reset_reason __attribute__((section(".noinit")));
  37. // ------------------------
  38. // Public functions
  39. // ------------------------
  40. __attribute__((naked)) // Don't output function pro- and epilogue
  41. __attribute__((used)) // Output the function, even if "not used"
  42. __attribute__((section(".init3"))) // Put in an early user definable section
  43. void save_reset_reason() {
  44. #if ENABLED(OPTIBOOT_RESET_REASON)
  45. __asm__ __volatile__(
  46. A("STS %0, r2")
  47. : "=m"(hal.reset_reason)
  48. );
  49. #else
  50. hal.reset_reason = MCUSR;
  51. #endif
  52. // Clear within 16ms since WDRF bit enables a 16ms watchdog timer -> Boot loop
  53. hal.clear_reset_source();
  54. wdt_disable();
  55. }
  56. void MarlinHAL::init() {
  57. // Init Servo Pins
  58. #define INIT_SERVO(N) OUT_WRITE(SERVO##N##_PIN, LOW)
  59. #if HAS_SERVO_0
  60. INIT_SERVO(0);
  61. #endif
  62. #if HAS_SERVO_1
  63. INIT_SERVO(1);
  64. #endif
  65. #if HAS_SERVO_2
  66. INIT_SERVO(2);
  67. #endif
  68. #if HAS_SERVO_3
  69. INIT_SERVO(3);
  70. #endif
  71. init_pwm_timers(); // Init user timers to default frequency - 1000HZ
  72. }
  73. void MarlinHAL::reboot() {
  74. #if ENABLED(USE_WATCHDOG)
  75. while (1) { /* run out the watchdog */ }
  76. #else
  77. void (*resetFunc)() = 0; // Declare resetFunc() at address 0
  78. resetFunc(); // Jump to address 0
  79. #endif
  80. }
  81. // ------------------------
  82. // Watchdog Timer
  83. // ------------------------
  84. #if ENABLED(USE_WATCHDOG)
  85. #include <avr/wdt.h>
  86. #include "../../MarlinCore.h"
  87. // Initialize watchdog with 8s timeout, if possible. Otherwise, make it 4s.
  88. void MarlinHAL::watchdog_init() {
  89. #if ENABLED(WATCHDOG_DURATION_8S) && defined(WDTO_8S)
  90. #define WDTO_NS WDTO_8S
  91. #else
  92. #define WDTO_NS WDTO_4S
  93. #endif
  94. #if ENABLED(WATCHDOG_RESET_MANUAL)
  95. // Enable the watchdog timer, but only for the interrupt.
  96. // Take care, as this requires the correct order of operation, with interrupts disabled.
  97. // See the datasheet of any AVR chip for details.
  98. wdt_reset();
  99. cli();
  100. _WD_CONTROL_REG = _BV(_WD_CHANGE_BIT) | _BV(WDE);
  101. _WD_CONTROL_REG = _BV(WDIE) | (WDTO_NS & 0x07) | ((WDTO_NS & 0x08) << 2); // WDTO_NS directly does not work. bit 0-2 are consecutive in the register but the highest value bit is at bit 5
  102. // So worked for up to WDTO_2S
  103. sei();
  104. wdt_reset();
  105. #else
  106. wdt_enable(WDTO_NS); // The function handles the upper bit correct.
  107. #endif
  108. //delay(10000); // test it!
  109. }
  110. //===========================================================================
  111. //=================================== ISR ===================================
  112. //===========================================================================
  113. // Watchdog timer interrupt, called if main program blocks >4sec and manual reset is enabled.
  114. #if ENABLED(WATCHDOG_RESET_MANUAL)
  115. ISR(WDT_vect) {
  116. sei(); // With the interrupt driven serial we need to allow interrupts.
  117. SERIAL_ERROR_MSG(STR_WATCHDOG_FIRED);
  118. minkill(); // interrupt-safe final kill and infinite loop
  119. }
  120. #endif
  121. // Reset watchdog. MUST be called at least every 4 seconds after the
  122. // first watchdog_init or AVR will go into emergency procedures.
  123. void MarlinHAL::watchdog_refresh() { wdt_reset(); }
  124. #endif // USE_WATCHDOG
  125. // ------------------------
  126. // Free Memory Accessor
  127. // ------------------------
  128. #if ENABLED(SDSUPPORT)
  129. #include "../../sd/SdFatUtil.h"
  130. int freeMemory() { return SdFatUtil::FreeRam(); }
  131. #else // !SDSUPPORT
  132. extern "C" {
  133. extern char __bss_end;
  134. extern char __heap_start;
  135. extern void* __brkval;
  136. int freeMemory() {
  137. int free_memory;
  138. if ((int)__brkval == 0)
  139. free_memory = ((int)&free_memory) - ((int)&__bss_end);
  140. else
  141. free_memory = ((int)&free_memory) - ((int)__brkval);
  142. return free_memory;
  143. }
  144. }
  145. #endif // !SDSUPPORT
  146. #endif // __AVR__