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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  5. * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
  6. * Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com
  7. * Copyright (c) 2017 Victor Perez
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. *
  22. */
  23. #include "../platforms.h"
  24. #ifdef HAL_STM32
  25. #include "../../inc/MarlinConfig.h"
  26. #include "../shared/Delay.h"
  27. #include "usb_serial.h"
  28. #ifdef USBCON
  29. DefaultSerial1 MSerialUSB(false, SerialUSB);
  30. #endif
  31. #if ENABLED(SRAM_EEPROM_EMULATION)
  32. #if STM32F7xx
  33. #include <stm32f7xx_ll_pwr.h>
  34. #elif STM32F4xx
  35. #include <stm32f4xx_ll_pwr.h>
  36. #else
  37. #error "SRAM_EEPROM_EMULATION is currently only supported for STM32F4xx and STM32F7xx"
  38. #endif
  39. #endif
  40. #if HAS_SD_HOST_DRIVE
  41. #include "msc_sd.h"
  42. #include "usbd_cdc_if.h"
  43. #endif
  44. // ------------------------
  45. // Public Variables
  46. // ------------------------
  47. uint16_t MarlinHAL::adc_result;
  48. // ------------------------
  49. // Public functions
  50. // ------------------------
  51. #if ENABLED(POSTMORTEM_DEBUGGING)
  52. extern void install_min_serial();
  53. #endif
  54. // HAL initialization task
  55. void MarlinHAL::init() {
  56. // Ensure F_CPU is a constant expression.
  57. // If the compiler breaks here, it means that delay code that should compute at compile time will not work.
  58. // So better safe than sorry here.
  59. constexpr int cpuFreq = F_CPU;
  60. UNUSED(cpuFreq);
  61. #if ENABLED(SDSUPPORT) && DISABLED(SDIO_SUPPORT) && (defined(SDSS) && SDSS != -1)
  62. OUT_WRITE(SDSS, HIGH); // Try to set SDSS inactive before any other SPI users start up
  63. #endif
  64. #if PIN_EXISTS(LED)
  65. OUT_WRITE(LED_PIN, LOW);
  66. #endif
  67. #if ENABLED(SRAM_EEPROM_EMULATION)
  68. __HAL_RCC_PWR_CLK_ENABLE();
  69. HAL_PWR_EnableBkUpAccess(); // Enable access to backup SRAM
  70. __HAL_RCC_BKPSRAM_CLK_ENABLE();
  71. LL_PWR_EnableBkUpRegulator(); // Enable backup regulator
  72. while (!LL_PWR_IsActiveFlag_BRR()); // Wait until backup regulator is initialized
  73. #endif
  74. SetTimerInterruptPriorities();
  75. #if ENABLED(EMERGENCY_PARSER) && (USBD_USE_CDC || USBD_USE_CDC_MSC)
  76. USB_Hook_init();
  77. #endif
  78. TERN_(POSTMORTEM_DEBUGGING, install_min_serial()); // Install the min serial handler
  79. TERN_(HAS_SD_HOST_DRIVE, MSC_SD_init()); // Enable USB SD card access
  80. #if PIN_EXISTS(USB_CONNECT)
  81. OUT_WRITE(USB_CONNECT_PIN, !USB_CONNECT_INVERTING); // USB clear connection
  82. delay(1000); // Give OS time to notice
  83. WRITE(USB_CONNECT_PIN, USB_CONNECT_INVERTING);
  84. #endif
  85. }
  86. // HAL idle task
  87. void MarlinHAL::idletask() {
  88. #if HAS_SHARED_MEDIA
  89. // Stm32duino currently doesn't have a "loop/idle" method
  90. CDC_resume_receive();
  91. CDC_continue_transmit();
  92. #endif
  93. }
  94. void MarlinHAL::reboot() { NVIC_SystemReset(); }
  95. uint8_t MarlinHAL::get_reset_source() {
  96. return
  97. #ifdef RCC_FLAG_IWDGRST // Some sources may not exist...
  98. RESET != __HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) ? RST_WATCHDOG :
  99. #endif
  100. #ifdef RCC_FLAG_IWDG1RST
  101. RESET != __HAL_RCC_GET_FLAG(RCC_FLAG_IWDG1RST) ? RST_WATCHDOG :
  102. #endif
  103. #ifdef RCC_FLAG_IWDG2RST
  104. RESET != __HAL_RCC_GET_FLAG(RCC_FLAG_IWDG2RST) ? RST_WATCHDOG :
  105. #endif
  106. #ifdef RCC_FLAG_SFTRST
  107. RESET != __HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST) ? RST_SOFTWARE :
  108. #endif
  109. #ifdef RCC_FLAG_PINRST
  110. RESET != __HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) ? RST_EXTERNAL :
  111. #endif
  112. #ifdef RCC_FLAG_PORRST
  113. RESET != __HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) ? RST_POWER_ON :
  114. #endif
  115. 0
  116. ;
  117. }
  118. void MarlinHAL::clear_reset_source() { __HAL_RCC_CLEAR_RESET_FLAGS(); }
  119. // ------------------------
  120. // Watchdog Timer
  121. // ------------------------
  122. #if ENABLED(USE_WATCHDOG)
  123. #define WDT_TIMEOUT_US TERN(WATCHDOG_DURATION_8S, 8000000, 4000000) // 4 or 8 second timeout
  124. #include <IWatchdog.h>
  125. void MarlinHAL::watchdog_init() {
  126. IF_DISABLED(DISABLE_WATCHDOG_INIT, IWatchdog.begin(WDT_TIMEOUT_US));
  127. }
  128. void MarlinHAL::watchdog_refresh() {
  129. IWatchdog.reload();
  130. #if DISABLED(PINS_DEBUGGING) && PIN_EXISTS(LED)
  131. TOGGLE(LED_PIN); // heartbeat indicator
  132. #endif
  133. }
  134. #endif
  135. extern "C" {
  136. extern unsigned int _ebss; // end of bss section
  137. }
  138. // Reset the system to initiate a firmware flash
  139. WEAK void flashFirmware(const int16_t) { hal.reboot(); }
  140. // Maple Compatibility
  141. volatile uint32_t systick_uptime_millis = 0;
  142. systickCallback_t systick_user_callback;
  143. void systick_attach_callback(systickCallback_t cb) { systick_user_callback = cb; }
  144. void HAL_SYSTICK_Callback() {
  145. systick_uptime_millis++;
  146. if (systick_user_callback) systick_user_callback();
  147. }
  148. #endif // HAL_STM32