stm32/main: Make board-defined UART REPL use a static object and buffer.

This way the UART REPL does not need the MicroPython heap and exists
outside the MicroPython runtime, allowing characters to still be received
during a soft reset.
pull/1/head
Damien George 2019-01-28 22:56:55 +11:00
parent f431795caf
commit 38022ddb2c
1 changed files with 27 additions and 16 deletions

View File

@ -73,6 +73,14 @@ STATIC pyb_thread_t pyb_thread_main;
STATIC fs_user_mount_t fs_user_mount_flash;
#endif
#if defined(MICROPY_HW_UART_REPL)
#ifndef MICROPY_HW_UART_REPL_RXBUF
#define MICROPY_HW_UART_REPL_RXBUF (64)
#endif
STATIC pyb_uart_obj_t pyb_uart_repl_obj;
STATIC uint8_t pyb_uart_repl_rxbuf[MICROPY_HW_UART_REPL_RXBUF];
#endif
void flash_error(int n) {
for (int i = 0; i < n; i++) {
led_state(PYB_LED_RED, 1);
@ -541,6 +549,19 @@ void stm32_main(uint32_t reset_mode) {
lwip_init();
#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
soft_reset:
#if defined(MICROPY_HW_LED2)
@ -588,27 +609,17 @@ soft_reset:
// 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();
timer_init0();
// Define MICROPY_HW_UART_REPL to be PYB_UART_6 and define
// MICROPY_HW_UART_REPL_BAUD in your mpconfigboard.h file if you want a
// REPL on a hardware UART as well as on USB VCP
#if defined(MICROPY_HW_UART_REPL)
{
mp_obj_t args[2] = {
MP_OBJ_NEW_SMALL_INT(MICROPY_HW_UART_REPL),
MP_OBJ_NEW_SMALL_INT(MICROPY_HW_UART_REPL_BAUD),
};
MP_STATE_PORT(pyb_stdio_uart) = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type, MP_ARRAY_SIZE(args), 0, args);
uart_attach_to_repl(MP_STATE_PORT(pyb_stdio_uart), true);
}
#else
MP_STATE_PORT(pyb_stdio_uart) = NULL;
#endif
#if MICROPY_HW_ENABLE_CAN
can_init0();
#endif