Skip to content

Commit 52bc935

Browse files
committed
A few minor fixes for corner cases
* Always clear the peripheral interrupt so we don't hang when full * Store the ringbuf in the object so it gets collected when we're alive * Make UART objects have a finaliser so they are deinit when their memory is freed * Copy bytes into the ringbuf from the FIFO after we read to ensure the interrupt is enabled ASAP * Copy bytes into the ringbuf from the FIFO before measuring our rx available because the interrupt is based on a threshold (not > 0). For example, a single byte won't trigger an interrupt.
1 parent 8170e26 commit 52bc935

File tree

7 files changed

+72
-23
lines changed

7 files changed

+72
-23
lines changed

ports/raspberrypi/common-hal/busio/UART.c

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,27 @@ static uint8_t pin_init(const uint8_t uart, const mcu_pin_obj_t * pin, const uin
7373
return pin->number;
7474
}
7575

76-
static ringbuf_t ringbuf[NUM_UARTS];
76+
static busio_uart_obj_t* active_uarts[NUM_UARTS];
7777

78-
static void uart0_callback(void) {
79-
while (uart_is_readable(uart0) && ringbuf_num_empty(&ringbuf[0]) > 0) {
80-
ringbuf_put(&ringbuf[0], (uint8_t)uart_get_hw(uart0)->dr);
78+
static void _copy_into_ringbuf(ringbuf_t* r, uart_inst_t* uart) {
79+
while (uart_is_readable(uart) && ringbuf_num_empty(r) > 0) {
80+
ringbuf_put(r, (uint8_t) uart_get_hw(uart)->dr);
8181
}
8282
}
8383

84+
static void shared_callback(busio_uart_obj_t *self) {
85+
_copy_into_ringbuf(&self->ringbuf, self->uart);
86+
// We always clear the interrupt so it doesn't continue to fire because we
87+
// may not have read everything available.
88+
uart_get_hw(self->uart)->icr = UART_UARTICR_RXIC_BITS;
89+
}
90+
91+
static void uart0_callback(void) {
92+
shared_callback(active_uarts[0]);
93+
}
94+
8495
static void uart1_callback(void) {
85-
while (uart_is_readable(uart1) && ringbuf_num_empty(&ringbuf[1]) > 0) {
86-
ringbuf_put(&ringbuf[1], (uint8_t)uart_get_hw(uart1)->dr);
87-
}
96+
shared_callback(active_uarts[1]);
8897
}
8998

9099
void common_hal_busio_uart_construct(busio_uart_obj_t *self,
@@ -138,9 +147,10 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
138147
// pointers like this are NOT moved, allocating the buffer
139148
// in the long-lived pool is not strictly necessary)
140149
// (This is a macro.)
141-
if (!ringbuf_alloc(&ringbuf[uart_id], receiver_buffer_size, true)) {
150+
if (!ringbuf_alloc(&self->ringbuf, receiver_buffer_size, true)) {
142151
mp_raise_msg(&mp_type_MemoryError, translate("Failed to allocate RX buffer"));
143152
}
153+
active_uarts[uart_id] = self;
144154
if (uart_id == 1) {
145155
self->uart_irq_id = UART1_IRQ;
146156
irq_set_exclusive_handler(self->uart_irq_id, uart1_callback);
@@ -149,7 +159,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
149159
irq_set_exclusive_handler(self->uart_irq_id, uart0_callback);
150160
}
151161
irq_set_enabled(self->uart_irq_id, true);
152-
uart_set_irq_enables(self->uart, true, false);
162+
uart_set_irq_enables(self->uart, true /* rx has data */, false /* tx needs data */);
153163
}
154164
}
155165

@@ -162,7 +172,8 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
162172
return;
163173
}
164174
uart_deinit(self->uart);
165-
ringbuf_free(&ringbuf[self->uart_id]);
175+
ringbuf_free(&self->ringbuf);
176+
active_uarts[self->uart_id] = NULL;
166177
uart_status[self->uart_id] = STATUS_FREE;
167178
reset_pin_number(self->tx_pin);
168179
reset_pin_number(self->rx_pin);
@@ -208,7 +219,7 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
208219
irq_set_enabled(self->uart_irq_id, false);
209220

210221
// Copy as much received data as available, up to len bytes.
211-
size_t total_read = ringbuf_get_n(&ringbuf[self->uart_id], data, len);
222+
size_t total_read = ringbuf_get_n(&self->ringbuf, data, len);
212223

213224
// Check if we still need to read more data.
214225
if (len > total_read) {
@@ -218,7 +229,7 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
218229
while (len > 0 && (supervisor_ticks_ms64() - start_ticks < self->timeout_ms)) {
219230
if (uart_is_readable(self->uart)) {
220231
// Read and advance.
221-
*data++ = uart_get_hw(self->uart)->dr;
232+
data[total_read] = uart_get_hw(self->uart)->dr;
222233

223234
// Adjust the counters.
224235
len--;
@@ -230,11 +241,16 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
230241
RUN_BACKGROUND_TASKS;
231242
// Allow user to break out of a timeout with a KeyboardInterrupt.
232243
if (mp_hal_is_interrupted()) {
233-
return 0;
244+
break;
234245
}
235246
}
236247
}
237248

249+
// Now that we've emptied the ringbuf some, fill it up with anything in the
250+
// FIFO. This ensures that we'll empty the FIFO as much as possible and
251+
// reset the interrupt when we catch up.
252+
_copy_into_ringbuf(&self->ringbuf, self->uart);
253+
238254
// Re-enable irq.
239255
irq_set_enabled(self->uart_irq_id, true);
240256

@@ -264,13 +280,24 @@ void common_hal_busio_uart_set_timeout(busio_uart_obj_t *self, mp_float_t timeou
264280
}
265281

266282
uint32_t common_hal_busio_uart_rx_characters_available(busio_uart_obj_t *self) {
267-
return ringbuf_num_filled(&ringbuf[self->uart_id]);
283+
// Prevent conflict with uart irq.
284+
irq_set_enabled(self->uart_irq_id, false);
285+
// The UART only interrupts after a threshold so make sure to copy anything
286+
// out of its FIFO before measuring how many bytes we've received.
287+
_copy_into_ringbuf(&self->ringbuf, self->uart);
288+
irq_set_enabled(self->uart_irq_id, false);
289+
return ringbuf_num_filled(&self->ringbuf);
268290
}
269291

270292
void common_hal_busio_uart_clear_rx_buffer(busio_uart_obj_t *self) {
271293
// Prevent conflict with uart irq.
272294
irq_set_enabled(self->uart_irq_id, false);
273-
ringbuf_clear(&ringbuf[self->uart_id]);
295+
ringbuf_clear(&self->ringbuf);
296+
297+
// Throw away the FIFO contents too.
298+
while (uart_is_readable(self->uart)) {
299+
(void) uart_get_hw(self->uart)->dr;
300+
}
274301
irq_set_enabled(self->uart_irq_id, true);
275302
}
276303

ports/raspberrypi/common-hal/busio/UART.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ typedef struct {
4343
uint32_t baudrate;
4444
uint32_t timeout_ms;
4545
uart_inst_t * uart;
46+
ringbuf_t ringbuf;
4647
} busio_uart_obj_t;
4748

4849
extern void reset_uart(void);

py/gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void gc_init(void *start, void *end) {
192192
}
193193

194194
void gc_deinit(void) {
195-
// Run any finalizers before we stop using the heap.
195+
// Run any finalisers before we stop using the heap.
196196
gc_sweep_all();
197197

198198
MP_STATE_MEM(gc_pool_start) = 0;

py/malloc.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
#undef free
5858
#undef realloc
5959
#define malloc_ll(b, ll) gc_alloc((b), false, (ll))
60-
#define malloc_with_finaliser(b) gc_alloc((b), true, false)
60+
#define malloc_with_finaliser(b, ll) gc_alloc((b), true, (ll))
6161
#define free gc_free
6262
#define realloc(ptr, n) gc_realloc(ptr, n, true)
6363
#define realloc_ext(ptr, n, mv) gc_realloc(ptr, n, mv)
@@ -103,8 +103,8 @@ void *m_malloc_maybe(size_t num_bytes, bool long_lived) {
103103
}
104104

105105
#if MICROPY_ENABLE_FINALISER
106-
void *m_malloc_with_finaliser(size_t num_bytes) {
107-
void *ptr = malloc_with_finaliser(num_bytes);
106+
void *m_malloc_with_finaliser(size_t num_bytes, bool long_lived) {
107+
void *ptr = malloc_with_finaliser(num_bytes, long_lived);
108108
if (ptr == NULL && num_bytes != 0) {
109109
m_malloc_fail(num_bytes);
110110
}

py/misc.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,13 @@ typedef unsigned int uint;
7272
#define m_new_obj_var_maybe(obj_type, var_type, var_num) ((obj_type*)m_malloc_maybe(sizeof(obj_type) + sizeof(var_type) * (var_num), false))
7373
#define m_new_ll_obj_var_maybe(obj_type, var_type, var_num) ((obj_type*)m_malloc_maybe(sizeof(obj_type) + sizeof(var_type) * (var_num), true))
7474
#if MICROPY_ENABLE_FINALISER
75-
#define m_new_obj_with_finaliser(type) ((type*)(m_malloc_with_finaliser(sizeof(type))))
76-
#define m_new_obj_var_with_finaliser(type, var_type, var_num) ((type*)m_malloc_with_finaliser(sizeof(type) + sizeof(var_type) * (var_num)))
75+
#define m_new_obj_with_finaliser(type) ((type*)(m_malloc_with_finaliser(sizeof(type), false)))
76+
#define m_new_obj_var_with_finaliser(type, var_type, var_num) ((type*)m_malloc_with_finaliser(sizeof(type) + sizeof(var_type) * (var_num), false))
77+
#define m_new_ll_obj_with_finaliser(type) ((type*)(m_malloc_with_finaliser(sizeof(type), true)))
7778
#else
7879
#define m_new_obj_with_finaliser(type) m_new_obj(type)
7980
#define m_new_obj_var_with_finaliser(type, var_type, var_num) m_new_obj_var(type, var_type, var_num)
81+
#define m_new_ll_obj_with_finaliser(type) m_new_ll_obj(type)
8082
#endif
8183
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
8284
#define m_renew(type, ptr, old_num, new_num) ((type*)(m_realloc((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num))))
@@ -93,7 +95,7 @@ typedef unsigned int uint;
9395

9496
void *m_malloc(size_t num_bytes, bool long_lived);
9597
void *m_malloc_maybe(size_t num_bytes, bool long_lived);
96-
void *m_malloc_with_finaliser(size_t num_bytes);
98+
void *m_malloc_with_finaliser(size_t num_bytes, bool long_lived);
9799
void *m_malloc0(size_t num_bytes, bool long_lived);
98100
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
99101
void *m_realloc(void *ptr, size_t old_num_bytes, size_t new_num_bytes);

shared-bindings/busio/UART.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ STATIC mp_obj_t busio_uart_make_new(const mp_obj_type_t *type, size_t n_args, co
8282
// This is needed to avoid crashes with certain UART implementations which
8383
// cannot accomodate being moved after creation. (See
8484
// https://github.com/adafruit/circuitpython/issues/1056)
85-
busio_uart_obj_t *self = m_new_ll_obj(busio_uart_obj_t);
85+
busio_uart_obj_t *self = m_new_ll_obj_with_finaliser(busio_uart_obj_t);
8686
self->base.type = &busio_uart_type;
8787
enum { ARG_tx, ARG_rx, ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_timeout, ARG_receiver_buffer_size,
8888
ARG_rts, ARG_cts, ARG_rs485_dir,ARG_rs485_invert};
@@ -387,6 +387,7 @@ const mp_obj_type_t busio_uart_parity_type = {
387387
};
388388

389389
STATIC const mp_rom_map_elem_t busio_uart_locals_dict_table[] = {
390+
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&busio_uart_deinit_obj) },
390391
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&busio_uart_deinit_obj) },
391392
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
392393
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&busio_uart___exit___obj) },

tests/manual/busio/uart_echo.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import busio
2+
import board
3+
import time
4+
5+
i = 0
6+
7+
u = busio.UART(tx=board.TX, rx=board.RX)
8+
9+
while True:
10+
u.write(str(i).encode("utf-8"))
11+
time.sleep(0.1)
12+
print(i, u.in_waiting) # should be the number of digits
13+
time.sleep(0.1)
14+
print(i, u.in_waiting) # should be the number of digits
15+
r = u.read(64 + 10)
16+
print(i, u.in_waiting) # should be 0
17+
print(len(r), r)
18+
i += 1

0 commit comments

Comments
 (0)