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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC)
  24. #include "HAL.h"
  25. #include "usb_serial.h"
  26. #include "../../inc/MarlinConfig.h"
  27. #include "../shared/Delay.h"
  28. #ifdef USBCON
  29. DefaultSerial MSerial(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. // ------------------------
  41. // Public Variables
  42. // ------------------------
  43. uint16_t HAL_adc_result;
  44. // ------------------------
  45. // Public functions
  46. // ------------------------
  47. // Needed for DELAY_NS() / DELAY_US() on CORTEX-M7
  48. #if (defined(__arm__) || defined(__thumb__)) && __CORTEX_M == 7
  49. // HAL pre-initialization task
  50. // Force the preinit function to run between the premain() and main() function
  51. // of the STM32 arduino core
  52. __attribute__((constructor (102)))
  53. void HAL_preinit() {
  54. enableCycleCounter();
  55. }
  56. #endif
  57. // HAL initialization task
  58. void HAL_init() {
  59. FastIO_init();
  60. #if ENABLED(SDSUPPORT) && DISABLED(SDIO_SUPPORT) && (defined(SDSS) && SDSS != -1)
  61. OUT_WRITE(SDSS, HIGH); // Try to set SDSS inactive before any other SPI users start up
  62. #endif
  63. #if PIN_EXISTS(LED)
  64. OUT_WRITE(LED_PIN, LOW);
  65. #endif
  66. #if ENABLED(SRAM_EEPROM_EMULATION)
  67. __HAL_RCC_PWR_CLK_ENABLE();
  68. HAL_PWR_EnableBkUpAccess(); // Enable access to backup SRAM
  69. __HAL_RCC_BKPSRAM_CLK_ENABLE();
  70. LL_PWR_EnableBkUpRegulator(); // Enable backup regulator
  71. while (!LL_PWR_IsActiveFlag_BRR()); // Wait until backup regulator is initialized
  72. #endif
  73. SetTimerInterruptPriorities();
  74. #if ENABLED(EMERGENCY_PARSER) && USBD_USE_CDC
  75. USB_Hook_init();
  76. #endif
  77. }
  78. void HAL_clear_reset_source() { __HAL_RCC_CLEAR_RESET_FLAGS(); }
  79. uint8_t HAL_get_reset_source() {
  80. return
  81. #ifdef RCC_FLAG_IWDGRST // Some sources may not exist...
  82. RESET != __HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) ? RST_WATCHDOG :
  83. #endif
  84. #ifdef RCC_FLAG_IWDG1RST
  85. RESET != __HAL_RCC_GET_FLAG(RCC_FLAG_IWDG1RST) ? RST_WATCHDOG :
  86. #endif
  87. #ifdef RCC_FLAG_IWDG2RST
  88. RESET != __HAL_RCC_GET_FLAG(RCC_FLAG_IWDG2RST) ? RST_WATCHDOG :
  89. #endif
  90. #ifdef RCC_FLAG_SFTRST
  91. RESET != __HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST) ? RST_SOFTWARE :
  92. #endif
  93. #ifdef RCC_FLAG_PINRST
  94. RESET != __HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) ? RST_EXTERNAL :
  95. #endif
  96. #ifdef RCC_FLAG_PORRST
  97. RESET != __HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) ? RST_POWER_ON :
  98. #endif
  99. 0
  100. ;
  101. }
  102. void _delay_ms(const int delay_ms) { delay(delay_ms); }
  103. extern "C" {
  104. extern unsigned int _ebss; // end of bss section
  105. }
  106. // ------------------------
  107. // ADC
  108. // ------------------------
  109. // TODO: Make sure this doesn't cause any delay
  110. void HAL_adc_start_conversion(const uint8_t adc_pin) { HAL_adc_result = analogRead(adc_pin); }
  111. uint16_t HAL_adc_get_result() { return HAL_adc_result; }
  112. // Reset the system (to initiate a firmware flash)
  113. void flashFirmware(const int16_t) { NVIC_SystemReset(); }
  114. // Maple Compatibility
  115. volatile uint32_t systick_uptime_millis = 0;
  116. systickCallback_t systick_user_callback;
  117. void systick_attach_callback(systickCallback_t cb) { systick_user_callback = cb; }
  118. void HAL_SYSTICK_Callback() {
  119. systick_uptime_millis++;
  120. if (systick_user_callback) systick_user_callback();
  121. }
  122. #endif // ARDUINO_ARCH_STM32 && !STM32GENERIC