diff options
author | Damien George <damien.p.george@gmail.com> | 2020-06-01 21:22:32 +1000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2020-06-01 21:41:54 +1000 |
commit | 9ae50d22c90f05bb03c6b6e97df7dffa69628fe7 (patch) | |
tree | d9c9fe8e5cff037c89c41524fd68e3f9664fb4e8 | |
parent | 88971342b17f48fb91619620f4c4c1b5da40aaa4 (diff) |
stm32/machine_uart: Allow re-init'ing a static UART object.
Just disallow changing the rxbuf which will be some static RAM (can't free
it and soft-reset would lose any dynamically allocated buffer).
-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 |