Skip to content

Commit

Permalink
Reorder setup, with serial early
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Apr 22, 2020
1 parent 0fec478 commit 5a5be7e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 53 deletions.
20 changes: 10 additions & 10 deletions Marlin/src/HAL/STM32/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ void HAL_init() {
#endif

#if ENABLED(SRAM_EEPROM_EMULATION)
// Enable access to backup SRAM
__HAL_RCC_PWR_CLK_ENABLE();
HAL_PWR_EnableBkUpAccess();
__HAL_RCC_BKPSRAM_CLK_ENABLE();

// Enable backup regulator
LL_PWR_EnableBkUpRegulator();
// Wait until backup regulator is initialized
while (!LL_PWR_IsActiveFlag_BRR());
#endif // EEPROM_EMULATED_SRAM
// Enable access to backup SRAM
__HAL_RCC_PWR_CLK_ENABLE();
HAL_PWR_EnableBkUpAccess();
__HAL_RCC_BKPSRAM_CLK_ENABLE();

// Enable backup regulator
LL_PWR_EnableBkUpRegulator();
// Wait until backup regulator is initialized
while (!LL_PWR_IsActiveFlag_BRR());
#endif

#if HAS_TMC_SW_SERIAL
SoftwareSerial::setInterruptPriority(SWSERIAL_TIMER_IRQ_PRIO, 0);
Expand Down
77 changes: 34 additions & 43 deletions Marlin/src/MarlinCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -889,68 +889,59 @@ void setup() {
#endif
#define SETUP_RUN(C) do{ SETUP_LOG(STRINGIFY(C)); C; }while(0)

HAL_init();

#if HAS_L64XX
L64xxManager.init(); // Set up SPI, init drivers
#endif

#if ENABLED(SMART_EFFECTOR) && PIN_EXISTS(SMART_EFFECTOR_MOD)
OUT_WRITE(SMART_EFFECTOR_MOD_PIN, LOW); // Put Smart Effector into NORMAL mode
#endif

#if ENABLED(DISABLE_DEBUG)
#if EITHER(DISABLE_DEBUG, DISABLE_JTAG)
// Disable any hardware debug to free up pins for IO
#ifdef JTAGSWD_DISABLE
#if ENABLED(DISABLE_DEBUG) && defined(JTAGSWD_DISABLE)
JTAGSWD_DISABLE();
#elif defined(JTAG_DISABLE)
JTAG_DISABLE();
#else
#error "DISABLE_DEBUG is not supported for the selected MCU/Board"
#error "DISABLE_(DEBUG|JTAG) is not supported for the selected MCU/Board."
#endif
#elif ENABLED(DISABLE_JTAG)
// Disable JTAG to free up pins for IO
#ifdef JTAG_DISABLE
JTAG_DISABLE();
#else
#error "DISABLE_JTAG is not supported for the selected MCU/Board"
#endif

#if NUM_SERIAL > 0
MYSERIAL0.begin(BAUDRATE);
uint32_t serial_connect_timeout = millis() + 1000UL;
while (!MYSERIAL0 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
#if NUM_SERIAL > 1
MYSERIAL1.begin(BAUDRATE);
serial_connect_timeout = millis() + 1000UL;
while (!MYSERIAL1 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
#endif
SERIAL_ECHO_MSG("start");
#endif

SETUP_RUN(HAL_init());

#if HAS_L64XX
SETUP_RUN(L64xxManager.init()); // Set up SPI, init drivers
#endif

#if ENABLED(SMART_EFFECTOR) && PIN_EXISTS(SMART_EFFECTOR_MOD)
OUT_WRITE(SMART_EFFECTOR_MOD_PIN, LOW); // Put Smart Effector into NORMAL mode
#endif

#if HAS_FILAMENT_SENSOR
runout.setup();
SETUP_RUN(runout.setup());
#endif

#if ENABLED(POWER_LOSS_RECOVERY)
recovery.setup();
SETUP_RUN(recovery.setup());
#endif

setup_killpin();
SETUP_RUN(setup_killpin());

#if HAS_TMC220x
tmc_serial_begin();
SETUP_RUN(tmc_serial_begin());
#endif

setup_powerhold();
SETUP_RUN(setup_powerhold());

#if HAS_STEPPER_RESET
disableStepperDrivers();
SETUP_RUN(disableStepperDrivers());
#endif

#if NUM_SERIAL > 0
MYSERIAL0.begin(BAUDRATE);
uint32_t serial_connect_timeout = millis() + 1000UL;
while (!MYSERIAL0 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
#if NUM_SERIAL > 1
MYSERIAL1.begin(BAUDRATE);
serial_connect_timeout = millis() + 1000UL;
while (!MYSERIAL1 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
#endif
#endif

SERIAL_ECHOLNPGM("start");
SERIAL_ECHO_START();

#if HAS_TMC_SPI
#if DISABLED(TMC_USE_SW_SPI)
SETUP_RUN(SPI.begin());
Expand All @@ -966,7 +957,7 @@ void setup() {
SETUP_RUN(esp_wifi_init());

// Check startup - does nothing if bootloader sets MCUSR to 0
byte mcu = HAL_get_reset_source();
const byte mcu = HAL_get_reset_source();
if (mcu & 1) SERIAL_ECHOLNPGM(STR_POWERUP);
if (mcu & 2) SERIAL_ECHOLNPGM(STR_EXTERNAL_RESET);
if (mcu & 4) SERIAL_ECHOLNPGM(STR_BROWNOUT_RESET);
Expand All @@ -991,9 +982,6 @@ void setup() {
SERIAL_ECHO_START();
SERIAL_ECHOLNPAIR(STR_FREE_MEMORY, freeMemory(), STR_PLANNER_BUFFER_BYTES, (int)sizeof(block_t) * (BLOCK_BUFFER_SIZE));

// UI must be initialized before EEPROM
// (because EEPROM code calls the UI).

// Set up LEDs early
#if HAS_COLOR_LEDS
SETUP_RUN(leds.setup());
Expand All @@ -1003,6 +991,9 @@ void setup() {
SETUP_RUN(controllerFan.setup());
#endif

// UI must be initialized before EEPROM
// (because EEPROM code calls the UI).

SETUP_RUN(ui.init());
SETUP_RUN(ui.reset_status()); // Load welcome message early. (Retained if no errors exist.)

Expand Down

0 comments on commit 5a5be7e

Please sign in to comment.