Skip to content

Commit cac760a

Browse files
authored
Merge pull request adafruit#1080 from jepler/uart-ll
UART: Always allocate UART objects in the long-lived pool
2 parents 2e80f37 + b0e33f6 commit cac760a

File tree

2 files changed

+12
-2
lines changed
  • ports/atmel-samd/common-hal/busio
  • shared-bindings/busio

2 files changed

+12
-2
lines changed

ports/atmel-samd/common-hal/busio/UART.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,13 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
132132

133133
if (rx && receiver_buffer_size > 0) {
134134
self->buffer_length = receiver_buffer_size;
135-
self->buffer = (uint8_t *) gc_alloc(self->buffer_length * sizeof(uint8_t), false, false);
135+
// Initially allocate the UART's buffer in the long-lived part of the
136+
// heap. UARTs are generally long-lived objects, but the "make long-
137+
// lived" machinery is incapable of moving internal pointers like
138+
// self->buffer, so do it manually. (However, as long as internal
139+
// pointers like this are NOT moved, allocating the buffer
140+
// in the long-lived pool is not strictly necessary)
141+
self->buffer = (uint8_t *) gc_alloc(self->buffer_length * sizeof(uint8_t), false, true);
136142
if (self->buffer == NULL) {
137143
common_hal_busio_uart_deinit(self);
138144
mp_raise_msg(&mp_type_MemoryError, "Failed to allocate RX buffer");

shared-bindings/busio/UART.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ extern const busio_uart_parity_obj_t busio_uart_parity_odd_obj;
6666

6767
STATIC mp_obj_t busio_uart_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) {
6868
mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true);
69-
busio_uart_obj_t *self = m_new_obj(busio_uart_obj_t);
69+
// Always initially allocate the UART object within the long-lived heap.
70+
// This is needed to avoid crashes with certain UART implementations which
71+
// cannot accomodate being moved after creation. (See
72+
// https://github.com/adafruit/circuitpython/issues/1056)
73+
busio_uart_obj_t *self = m_new_ll_obj(busio_uart_obj_t);
7074
self->base.type = &busio_uart_type;
7175
mp_map_t kw_args;
7276
mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args);

0 commit comments

Comments
 (0)