diff options
| -rw-r--r-- | ports/stm32/machine_uart.c | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/ports/stm32/machine_uart.c b/ports/stm32/machine_uart.c index 1cf5817e0..39a45a2a1 100644 --- a/ports/stm32/machine_uart.c +++ b/ports/stm32/machine_uart.c @@ -237,11 +237,6 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t *)&args); - // static UARTs are used for internal purposes and shouldn't be reconfigured - if (self->is_static) { - mp_raise_ValueError(MP_ERROR_TEXT("UART is static and can't be init'd")); - } - // baudrate uint32_t baudrate = args.baudrate.u_int; @@ -306,20 +301,28 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const self->timeout_char = min_timeout_char; } - // setup the read buffer - m_del(byte, self->read_buf, self->read_buf_len << self->char_width); - if (args.rxbuf.u_int >= 0) { - // rxbuf overrides legacy read_buf_len - args.read_buf_len.u_int = args.rxbuf.u_int; - } - if (args.read_buf_len.u_int <= 0) { - // no read buffer - uart_set_rxbuf(self, 0, NULL); + if (self->is_static) { + // Static UARTs have fixed memory for the rxbuf and can't be reconfigured. + if (args.rxbuf.u_int >= 0) { + mp_raise_ValueError(MP_ERROR_TEXT("UART is static and rxbuf can't be changed")); + } + uart_set_rxbuf(self, self->read_buf_len, self->read_buf); } else { - // read buffer using interrupts - size_t len = args.read_buf_len.u_int + 1; // +1 to adjust for usable length of buffer - uint8_t *buf = m_new(byte, len << self->char_width); - uart_set_rxbuf(self, len, buf); + // setup the read buffer + m_del(byte, self->read_buf, self->read_buf_len << self->char_width); + if (args.rxbuf.u_int >= 0) { + // rxbuf overrides legacy read_buf_len + args.read_buf_len.u_int = args.rxbuf.u_int; + } + if (args.read_buf_len.u_int <= 0) { + // no read buffer + uart_set_rxbuf(self, 0, NULL); + } else { + // read buffer using interrupts + size_t len = args.read_buf_len.u_int + 1; // +1 to adjust for usable length of buffer + uint8_t *buf = m_new(byte, len << self->char_width); + uart_set_rxbuf(self, len, buf); + } } // compute actual baudrate that was configured |
