Skip to content

Commit

Permalink
stm32/main: Start UART REPL as early as possible.
Browse files Browse the repository at this point in the history
For debugging purposes, to see output from other peripherals.

Also reset the pyb_stdio_uart state at the end of soft reset, in case it
points to a heap-allocated object.

Signed-off-by: Damien George <damien@micropython.org>
  • Loading branch information
dpgeorge committed Jun 15, 2023
1 parent fb1bdf0 commit b839acc
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions ports/stm32/main.c
Expand Up @@ -411,6 +411,21 @@ void stm32_main(uint32_t reset_mode) {
rtc_init_start(false);
#endif
uart_init0();

#if defined(MICROPY_HW_UART_REPL)
// Set up a UART REPL using a statically allocated object
pyb_uart_repl_obj.base.type = &pyb_uart_type;
pyb_uart_repl_obj.uart_id = MICROPY_HW_UART_REPL;
pyb_uart_repl_obj.is_static = true;
pyb_uart_repl_obj.timeout = 0;
pyb_uart_repl_obj.timeout_char = 2;
uart_init(&pyb_uart_repl_obj, MICROPY_HW_UART_REPL_BAUD, UART_WORDLENGTH_8B, UART_PARITY_NONE, UART_STOPBITS_1, 0);
uart_set_rxbuf(&pyb_uart_repl_obj, sizeof(pyb_uart_repl_rxbuf), pyb_uart_repl_rxbuf);
uart_attach_to_repl(&pyb_uart_repl_obj, true);
MP_STATE_PORT(pyb_uart_obj_all)[MICROPY_HW_UART_REPL - 1] = &pyb_uart_repl_obj;
MP_STATE_PORT(pyb_stdio_uart) = &pyb_uart_repl_obj;
#endif

spi_init0();
#if MICROPY_PY_PYB_LEGACY && MICROPY_HW_ENABLE_HW_I2C
i2c_init0();
Expand Down Expand Up @@ -446,19 +461,6 @@ void stm32_main(uint32_t reset_mode) {
}
#endif

#if defined(MICROPY_HW_UART_REPL)
// Set up a UART REPL using a statically allocated object
pyb_uart_repl_obj.base.type = &pyb_uart_type;
pyb_uart_repl_obj.uart_id = MICROPY_HW_UART_REPL;
pyb_uart_repl_obj.is_static = true;
pyb_uart_repl_obj.timeout = 0;
pyb_uart_repl_obj.timeout_char = 2;
uart_init(&pyb_uart_repl_obj, MICROPY_HW_UART_REPL_BAUD, UART_WORDLENGTH_8B, UART_PARITY_NONE, UART_STOPBITS_1, 0);
uart_set_rxbuf(&pyb_uart_repl_obj, sizeof(pyb_uart_repl_rxbuf), pyb_uart_repl_rxbuf);
uart_attach_to_repl(&pyb_uart_repl_obj, true);
MP_STATE_PORT(pyb_uart_obj_all)[MICROPY_HW_UART_REPL - 1] = &pyb_uart_repl_obj;
#endif

boardctrl_state_t state;
state.reset_mode = reset_mode;
state.log_soft_reset = false;
Expand Down Expand Up @@ -496,12 +498,6 @@ void stm32_main(uint32_t reset_mode) {
// we can run Python scripts (eg boot.py), but anything that is configurable
// by boot.py must be set after boot.py is run.

#if defined(MICROPY_HW_UART_REPL)
MP_STATE_PORT(pyb_stdio_uart) = &pyb_uart_repl_obj;
#else
MP_STATE_PORT(pyb_stdio_uart) = NULL;
#endif

readline_init0();
pin_init0();
extint_init0();
Expand Down Expand Up @@ -659,6 +655,12 @@ void stm32_main(uint32_t reset_mode) {
pyb_thread_deinit();
#endif

#if defined(MICROPY_HW_UART_REPL)
MP_STATE_PORT(pyb_stdio_uart) = &pyb_uart_repl_obj;
#else
MP_STATE_PORT(pyb_stdio_uart) = NULL;
#endif

MICROPY_BOARD_END_SOFT_RESET(&state);

gc_sweep_all();
Expand Down

0 comments on commit b839acc

Please sign in to comment.