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

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