2018-10-03 03:26:07 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
|
|
|
*
|
2020-02-03 08:00:57 -06:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2018-10-03 03:26:07 -05:00
|
|
|
* Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
|
|
|
|
* Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com
|
|
|
|
* Copyright (c) 2017 Victor Perez
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2020-07-22 22:20:14 -05:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-10-03 03:26:07 -05:00
|
|
|
*
|
|
|
|
*/
|
2018-12-03 06:55:49 -06:00
|
|
|
#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC)
|
|
|
|
|
2018-10-03 03:26:07 -05:00
|
|
|
#include "HAL.h"
|
2020-05-26 00:43:29 -05:00
|
|
|
#include "usb_serial.h"
|
2018-10-03 03:26:07 -05:00
|
|
|
|
2018-10-23 15:24:58 -05:00
|
|
|
#include "../../inc/MarlinConfig.h"
|
2018-11-01 16:11:51 -05:00
|
|
|
#include "../shared/Delay.h"
|
2018-10-23 15:24:58 -05:00
|
|
|
|
2021-01-28 02:02:06 -06:00
|
|
|
#ifdef USBCON
|
|
|
|
DefaultSerial MSerial(false, SerialUSB);
|
|
|
|
#endif
|
|
|
|
|
2019-07-09 21:30:07 -05:00
|
|
|
#if ENABLED(SRAM_EEPROM_EMULATION)
|
2018-10-03 03:26:07 -05:00
|
|
|
#if STM32F7xx
|
2019-11-12 22:16:54 -06:00
|
|
|
#include <stm32f7xx_ll_pwr.h>
|
2019-04-05 15:30:19 -05:00
|
|
|
#elif STM32F4xx
|
2019-11-12 22:16:54 -06:00
|
|
|
#include <stm32f4xx_ll_pwr.h>
|
2018-10-03 03:26:07 -05:00
|
|
|
#else
|
2019-07-09 21:30:07 -05:00
|
|
|
#error "SRAM_EEPROM_EMULATION is currently only supported for STM32F4xx and STM32F7xx"
|
2018-10-03 03:26:07 -05:00
|
|
|
#endif
|
2019-06-28 23:04:33 -05:00
|
|
|
#endif
|
2018-10-03 03:26:07 -05:00
|
|
|
|
2021-02-02 14:55:11 -06:00
|
|
|
#if HAS_SD_HOST_DRIVE
|
|
|
|
#include "msc_sd.h"
|
|
|
|
#include "usbd_cdc_if.h"
|
|
|
|
#endif
|
|
|
|
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2018-10-03 03:26:07 -05:00
|
|
|
// Public Variables
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2018-10-03 03:26:07 -05:00
|
|
|
|
|
|
|
uint16_t HAL_adc_result;
|
|
|
|
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2018-10-03 03:26:07 -05:00
|
|
|
// Public functions
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2018-10-03 03:26:07 -05:00
|
|
|
|
2021-02-20 20:22:20 -06:00
|
|
|
TERN_(POSTMORTEM_DEBUGGING, extern void install_min_serial());
|
|
|
|
|
2018-11-18 01:30:46 -06:00
|
|
|
// HAL initialization task
|
2019-09-16 20:31:08 -05:00
|
|
|
void HAL_init() {
|
2018-10-30 19:25:44 -05:00
|
|
|
FastIO_init();
|
|
|
|
|
2021-02-26 17:59:28 -06:00
|
|
|
// Ensure F_CPU is a constant expression.
|
|
|
|
// If the compiler breaks here, it means that delay code that should compute at compile time will not work.
|
|
|
|
// So better safe than sorry here.
|
|
|
|
constexpr int cpuFreq = F_CPU;
|
|
|
|
UNUSED(cpuFreq);
|
|
|
|
|
2020-10-01 20:52:42 -05:00
|
|
|
#if ENABLED(SDSUPPORT) && DISABLED(SDIO_SUPPORT) && (defined(SDSS) && SDSS != -1)
|
2018-10-03 03:26:07 -05:00
|
|
|
OUT_WRITE(SDSS, HIGH); // Try to set SDSS inactive before any other SPI users start up
|
|
|
|
#endif
|
|
|
|
|
2018-10-30 19:25:44 -05:00
|
|
|
#if PIN_EXISTS(LED)
|
|
|
|
OUT_WRITE(LED_PIN, LOW);
|
|
|
|
#endif
|
|
|
|
|
2019-07-09 21:30:07 -05:00
|
|
|
#if ENABLED(SRAM_EEPROM_EMULATION)
|
2020-04-11 21:29:52 -05:00
|
|
|
__HAL_RCC_PWR_CLK_ENABLE();
|
2020-04-22 16:35:03 -05:00
|
|
|
HAL_PWR_EnableBkUpAccess(); // Enable access to backup SRAM
|
2020-04-11 21:29:52 -05:00
|
|
|
__HAL_RCC_BKPSRAM_CLK_ENABLE();
|
2020-04-22 16:35:03 -05:00
|
|
|
LL_PWR_EnableBkUpRegulator(); // Enable backup regulator
|
|
|
|
while (!LL_PWR_IsActiveFlag_BRR()); // Wait until backup regulator is initialized
|
2020-04-11 21:29:52 -05:00
|
|
|
#endif
|
2019-11-12 19:23:02 -06:00
|
|
|
|
2020-07-19 17:41:36 -05:00
|
|
|
SetTimerInterruptPriorities();
|
2020-05-26 00:43:29 -05:00
|
|
|
|
2020-11-01 20:38:10 -06:00
|
|
|
#if ENABLED(EMERGENCY_PARSER) && USBD_USE_CDC
|
|
|
|
USB_Hook_init();
|
|
|
|
#endif
|
2021-02-02 14:55:11 -06:00
|
|
|
|
2021-02-20 20:22:20 -06:00
|
|
|
TERN_(POSTMORTEM_DEBUGGING, install_min_serial()); // Install the min serial handler
|
|
|
|
|
2021-02-02 14:55:11 -06:00
|
|
|
#if HAS_SD_HOST_DRIVE
|
|
|
|
MSC_SD_init(); // Enable USB SD card access
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// HAL idle task
|
|
|
|
void HAL_idletask() {
|
|
|
|
#if HAS_SHARED_MEDIA
|
|
|
|
// Stm32duino currently doesn't have a "loop/idle" method
|
|
|
|
CDC_resume_receive();
|
|
|
|
CDC_continue_transmit();
|
|
|
|
#endif
|
2018-10-03 03:26:07 -05:00
|
|
|
}
|
|
|
|
|
2019-09-16 20:31:08 -05:00
|
|
|
void HAL_clear_reset_source() { __HAL_RCC_CLEAR_RESET_FLAGS(); }
|
2018-10-03 03:26:07 -05:00
|
|
|
|
2019-09-16 20:31:08 -05:00
|
|
|
uint8_t HAL_get_reset_source() {
|
2020-01-14 22:33:22 -06:00
|
|
|
return
|
|
|
|
#ifdef RCC_FLAG_IWDGRST // Some sources may not exist...
|
|
|
|
RESET != __HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) ? RST_WATCHDOG :
|
|
|
|
#endif
|
|
|
|
#ifdef RCC_FLAG_IWDG1RST
|
|
|
|
RESET != __HAL_RCC_GET_FLAG(RCC_FLAG_IWDG1RST) ? RST_WATCHDOG :
|
|
|
|
#endif
|
|
|
|
#ifdef RCC_FLAG_IWDG2RST
|
|
|
|
RESET != __HAL_RCC_GET_FLAG(RCC_FLAG_IWDG2RST) ? RST_WATCHDOG :
|
|
|
|
#endif
|
|
|
|
#ifdef RCC_FLAG_SFTRST
|
|
|
|
RESET != __HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST) ? RST_SOFTWARE :
|
|
|
|
#endif
|
|
|
|
#ifdef RCC_FLAG_PINRST
|
|
|
|
RESET != __HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) ? RST_EXTERNAL :
|
|
|
|
#endif
|
|
|
|
#ifdef RCC_FLAG_PORRST
|
2020-01-14 23:59:56 -06:00
|
|
|
RESET != __HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) ? RST_POWER_ON :
|
2020-01-14 22:33:22 -06:00
|
|
|
#endif
|
|
|
|
0
|
|
|
|
;
|
2018-10-03 03:26:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void _delay_ms(const int delay_ms) { delay(delay_ms); }
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
extern unsigned int _ebss; // end of bss section
|
|
|
|
}
|
|
|
|
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2018-10-03 03:26:07 -05:00
|
|
|
// ADC
|
2019-07-09 22:30:06 -05:00
|
|
|
// ------------------------
|
2018-10-03 03:26:07 -05:00
|
|
|
|
2019-09-30 21:44:07 -05:00
|
|
|
// TODO: Make sure this doesn't cause any delay
|
|
|
|
void HAL_adc_start_conversion(const uint8_t adc_pin) { HAL_adc_result = analogRead(adc_pin); }
|
|
|
|
uint16_t HAL_adc_get_result() { return HAL_adc_result; }
|
2018-10-03 03:26:07 -05:00
|
|
|
|
2020-10-01 20:52:42 -05:00
|
|
|
// Reset the system (to initiate a firmware flash)
|
2020-03-23 17:11:00 -05:00
|
|
|
void flashFirmware(const int16_t) { NVIC_SystemReset(); }
|
2019-09-28 00:54:30 -05:00
|
|
|
|
2020-10-01 20:52:42 -05:00
|
|
|
// Maple Compatibility
|
2020-11-01 20:38:10 -06:00
|
|
|
volatile uint32_t systick_uptime_millis = 0;
|
2020-10-01 20:52:42 -05:00
|
|
|
systickCallback_t systick_user_callback;
|
|
|
|
void systick_attach_callback(systickCallback_t cb) { systick_user_callback = cb; }
|
2020-11-01 20:38:10 -06:00
|
|
|
void HAL_SYSTICK_Callback() {
|
|
|
|
systick_uptime_millis++;
|
|
|
|
if (systick_user_callback) systick_user_callback();
|
|
|
|
}
|
2020-10-01 20:52:42 -05:00
|
|
|
|
2019-06-28 23:04:33 -05:00
|
|
|
#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC
|