stm32/uart: Allow ctrl-C to work with UARTs put on REPL via os.dupterm.

This commit is contained in:
Damien George 2018-04-23 20:44:30 +10:00
parent 513e537215
commit a60efa8202
5 changed files with 27 additions and 3 deletions

View File

@ -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;

View File

@ -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");
}

View File

@ -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) },

View File

@ -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;
}

View File

@ -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);