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).
@@ -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,6 +301,13 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const
self->timeout_char = min_timeout_char;
}
+ 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 {
// setup the read buffer
m_del(byte, self->read_buf, self->read_buf_len << self->char_width);
if (args.rxbuf.u_int >= 0) {
@@ -321,6 +323,7 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const
uint8_t *buf = m_new(byte, len << self->char_width);
uart_set_rxbuf(self, len, buf);
}
+ }
// compute actual baudrate that was configured
uint32_t actual_baudrate = uart_get_baudrate(self);
