diff options
-rw-r--r-- | ports/stm32/main.c | 1 | ||||
-rw-r--r-- | ports/stm32/modpyb.c | 6 | ||||
-rw-r--r-- | ports/stm32/moduos.c | 14 | ||||
-rw-r--r-- | ports/stm32/uart.c | 8 | ||||
-rw-r--r-- | ports/stm32/uart.h | 1 |
5 files changed, 27 insertions, 3 deletions
diff --git a/ports/stm32/main.c b/ports/stm32/main.c index 4d0e7434b..eefe47490 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -543,6 +543,7 @@ soft_reset: 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; diff --git a/ports/stm32/modpyb.c b/ports/stm32/modpyb.c index c7f2844a4..59c6ba67c 100644 --- a/ports/stm32/modpyb.c +++ b/ports/stm32/modpyb.c @@ -116,9 +116,13 @@ STATIC mp_obj_t pyb_repl_uart(size_t n_args, const mp_obj_t *args) { } } else { if (args[0] == mp_const_none) { - MP_STATE_PORT(pyb_stdio_uart) = NULL; + if (MP_STATE_PORT(pyb_stdio_uart) != NULL) { + uart_attach_to_repl(MP_STATE_PORT(pyb_stdio_uart), false); + MP_STATE_PORT(pyb_stdio_uart) = NULL; + } } else if (mp_obj_get_type(args[0]) == &pyb_uart_type) { MP_STATE_PORT(pyb_stdio_uart) = args[0]; + uart_attach_to_repl(MP_STATE_PORT(pyb_stdio_uart), true); } else { mp_raise_ValueError("need a UART object"); } diff --git a/ports/stm32/moduos.c b/ports/stm32/moduos.c index f6e1483d3..5728f9c61 100644 --- a/ports/stm32/moduos.c +++ b/ports/stm32/moduos.c @@ -106,6 +106,18 @@ STATIC mp_obj_t os_urandom(mp_obj_t num) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom); #endif +STATIC mp_obj_t uos_dupterm(size_t n_args, const mp_obj_t *args) { + mp_obj_t prev_obj = mp_uos_dupterm_obj.fun.var(n_args, args); + if (mp_obj_get_type(prev_obj) == &pyb_uart_type) { + uart_attach_to_repl(MP_OBJ_TO_PTR(prev_obj), false); + } + if (mp_obj_get_type(args[0]) == &pyb_uart_type) { + uart_attach_to_repl(MP_OBJ_TO_PTR(args[0]), true); + } + return prev_obj; +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(uos_dupterm_obj, 1, 2, uos_dupterm); + STATIC const mp_rom_map_elem_t os_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uos) }, @@ -133,7 +145,7 @@ STATIC const mp_rom_map_elem_t os_module_globals_table[] = { #endif // these are MicroPython extensions - { MP_ROM_QSTR(MP_QSTR_dupterm), MP_ROM_PTR(&mp_uos_dupterm_obj) }, + { MP_ROM_QSTR(MP_QSTR_dupterm), MP_ROM_PTR(&uos_dupterm_obj) }, { MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&mp_vfs_mount_obj) }, { MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&mp_vfs_umount_obj) }, { MP_ROM_QSTR(MP_QSTR_VfsFat), MP_ROM_PTR(&mp_fat_vfs_type) }, diff --git a/ports/stm32/uart.c b/ports/stm32/uart.c index 0621dc725..677606940 100644 --- a/ports/stm32/uart.c +++ b/ports/stm32/uart.c @@ -81,6 +81,7 @@ struct _pyb_uart_obj_t { IRQn_Type irqn; pyb_uart_t uart_id : 8; bool is_enabled : 1; + bool attached_to_repl; // whether the UART is attached to REPL byte char_width; // 0 for 7,8 bit chars, 1 for 9 bit chars uint16_t char_mask; // 0x7f for 7 bit, 0xff for 8 bit, 0x1ff for 9 bit uint16_t timeout; // timeout waiting for first char @@ -320,10 +321,15 @@ STATIC bool uart_init2(pyb_uart_obj_t *uart_obj) { HAL_UART_Init(&uart_obj->uart); uart_obj->is_enabled = true; + uart_obj->attached_to_repl = false; return true; } +void uart_attach_to_repl(pyb_uart_obj_t *self, bool attached) { + self->attached_to_repl = attached; +} + /* obsolete and unused bool uart_init(pyb_uart_obj_t *uart_obj, uint32_t baudrate) { UART_HandleTypeDef *uh = &uart_obj->uart; @@ -509,7 +515,7 @@ void uart_irq_handler(mp_uint_t uart_id) { #endif data &= self->char_mask; // Handle interrupt coming in on a UART REPL - if (data == mp_interrupt_char && self == MP_STATE_PORT(pyb_stdio_uart)) { + if (self->attached_to_repl && data == mp_interrupt_char) { pendsv_kbd_intr(); return; } diff --git a/ports/stm32/uart.h b/ports/stm32/uart.h index d06508e8e..4ab18ff22 100644 --- a/ports/stm32/uart.h +++ b/ports/stm32/uart.h @@ -45,6 +45,7 @@ void uart_init0(void); void uart_deinit(void); void uart_irq_handler(mp_uint_t uart_id); +void uart_attach_to_repl(pyb_uart_obj_t *self, bool attached); mp_uint_t uart_rx_any(pyb_uart_obj_t *uart_obj); int uart_rx_char(pyb_uart_obj_t *uart_obj); void uart_tx_strn(pyb_uart_obj_t *uart_obj, const char *str, uint len); |