8000 clean up uses of ringbuf; fix UART uses of ringbuf · adafruit/circuitpython@8dcbfb3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8dcbfb3

Browse files
committed
clean up uses of ringbuf; fix UART uses of ringbuf
1 parent 9ee6ec6 commit 8dcbfb3

File tree

11 files changed

+59
-34
lines changed

11 files changed

+59
-34
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,18 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
213213
self->sigint_enabled = sigint_enabled;
214214

215215
if (rx != NULL) {
216+
self->allocated_ringbuf = true;
217+
// Use the provided buffer when given.
216218
if (receiver_buffer != NULL) {
217-
self->ringbuf = (ringbuf_t) { receiver_buffer, receiver_buffer_size };
219+
ringbuf_init(&self->ringbuf, receiver_buffer, receiver_buffer_size);
220+
self->allocated_ringbuf = false;
218221
} else {
219222
// Initially allocate the UART's buffer in the long-lived part of the
220223
// heap. UARTs are generally long-lived objects, but the "make long-
221224
// lived" machinery is incapable of moving internal pointers like
222225
// self->buffer, so do it manually. (However, as long as internal
223226
// pointers like this are NOT moved, allocating the buffer
224227
// in the long-lived pool is not strictly necessary)
225-
// (This is a macro.)
226228
if (!ringbuf_alloc(&self->ringbuf, receiver_buffer_size, true)) {
227229
mp_raise_msg(&mp_type_MemoryError, translate("Failed to allocate RX buffer"));
228230
}
@@ -342,7 +344,9 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
342344
pl011->CR = 0;
343345
}
344346
active_uart[self->uart_id] = NULL;
345-
ringbuf_free(&self->ringbuf);
347+
if (self->allocated_ringbuf) {
348+
ringbuf_free(&self->ringbuf);
349+
}
346350
uart_status[self->uart_id] = STATUS_FREE;
347351
common_hal_reset_pin(self->tx_pin);
348352
common_hal_reset_pin(< 8000 span class=pl-s1>self->rx_pin);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ typedef struct {
3636
const mcu_pin_obj_t *rx_pin;
3737
const mcu_pin_obj_t *cts_pin;
3838
const mcu_pin_obj_t *rts_pin;
39-
uint8_t uart_id;
4039
uint32_t baudrate;
4140
uint32_t timeout_ms;
41+
uint8_t uart_id;
4242
bool sigint_enabled;
43+
bool allocated_ringbuf;
4344
ringbuf_t ringbuf;
4445
} busio_uart_obj_t;
4546

ports/nrf/common-hal/_bleio/PacketBuffer.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff lin 6D40 e change
@@ -233,10 +233,7 @@ void _common_hal_bleio_packet_buffer_construct(
233233
}
234234

235235
if (incoming) {
236-
self->ringbuf.buf = (uint8_t *)incoming_buffer;
237-
self->ringbuf.size = incoming_buffer_size;
238-
self->ringbuf.iget = 0;
239-
self->ringbuf.iput = 0;
236+
ringbuf_init(&self->ringbuf, incoming_buffer, incoming_buffer_size);
240237
}
241238

242239
self->packet_queued = false;

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,21 +220,19 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
220220
self->allocated_ringbuf = true;
221221
// Use the provided buffer when given.
222222
if (receiver_buffer != NULL) {
223-
self->ringbuf.buf = receiver_buffer;
224-
self->ringbuf.size = receiver_buffer_size - 1;
225-
self->ringbuf.iput = 0;
226-
self->ringbuf.iget = 0;
223+
ringbuf_init(&self->ringbuf, receiver_buffer, receiver_buffer_size);
227224
self->allocated_ringbuf = false;
225+
} else {
228226
// Initially allocate the UART's buffer in the long-lived part of the
229227
// heap. UARTs are generally long-lived objects, but the "make long-
230228
// lived" machinery is incapable of moving internal pointers like
231229
// self->buffer, so do it manually. (However, as long as internal
232230
// pointers like this are NOT moved, allocating the buffer
233231
// in the long-lived pool is not strictly necessary)
234-
// (This is a macro.)
235-
} else if (!ringbuf_alloc(&self->ringbuf, receiver_buffer_size, true)) {
236-
nrfx_uarte_uninit(self->uarte);
237-
mp_raise_msg(&mp_type_MemoryError, translate("Failed to allocate RX buffer"));
232+
if (!ringbuf_alloc(&self->ringbuf, receiver_buffer_size, true)) {
233+
nrfx_uarte_uninit(self->uarte);
234+
mp_raise_msg(&mp_type_MemoryError, translate("Failed to allocate RX buffer"));
235+
}
238236
}
239237

240238
self->rx_pin_number = rx->number;

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,23 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
140140
uart_set_hw_flow(self->uart, (cts != NULL), (rts != NULL));
141141

142142
if (rx != NULL) {
143-
// Initially allocate the UART's buffer in the long-lived part of the
144-
// heap. UARTs are generally long-lived objects, but the "make long-
145-
// lived" machinery is incapable of moving internal pointers like
146-
// self->buffer, so do it manually. (However, as long as internal
147-
// pointers like this are NOT moved, allocating the buffer
148-
// in the long-lived pool is not strictly necessary)
149-
// (This is a macro.)
150-
if (!ringbuf_alloc(&self->ringbuf, receiver_buffer_size, true)) {
151-
mp_raise_msg(&mp_type_MemoryError, translate("Failed to allocate RX buffer"));
143+
self->allocated_ringbuf = true;
144+
// Use the provided buffer when given.
145+
if (receiver_buffer != NULL) {
146+
ringbuf_init(&self->ringbuf, receiver_buffer, receiver_buffer_size);
147+
self->allocated_ringbuf = false;
148+
} else {
149+
// Initially allocate the UART's buffer in the long-lived part of the
150+
// heap. UARTs are generally long-lived objects, but the "make long-
151+
// lived" machinery is incapable of moving internal pointers like
152+
// self->buffer, so do it manually. (However, as long as internal
153+
// pointers like this are NOT moved, allocating the buffer
154+
// in the long-lived pool is not strictly necessary)
155+
if (!ringbuf_alloc(&self->ringbuf, receiver_buffer_size, true)) {
156+
mp_raise_msg(&mp_type_MemoryError, translate("Failed to allocate RX buffer"));
157+
}
152158
}
159+
153160
active_uarts[uart_id] = self;
154161
if (uart_id == 1) {
155162
self->uart_irq_id = UART1_IRQ;
@@ -172,7 +179,9 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
172179
return;
173180
}
174181
uart_deinit(self->uart);
175-
ringbuf_free(&self->ringbuf);
182+
if (self->allocated_ringbuf) {
183+
ringbuf_free(&self->ringbuf);
184+
}
176185
active_uarts[self->uart_id] = NULL;
177186
uart_status[self->uart_id] = STATUS_FREE;
178187
reset_pin_number(self->tx_pin);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ typedef struct {
4040
uint8_t rts_pin;
4141
uint8_t uart_id;
4242
uint8_t uart_irq_id;
43+
bool allocated_ringbuf;
4344
uint32_t baudrate;
4445
uint32_t timeout_ms;
4546
uart_inst_t *uart;

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,17 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
217217

218218
// Init buffer for rx and claim pins
219219
if (self->rx != NULL) {
220+
self->allocated_ringbuf = true;
220221
if (receiver_buffer != NULL) {
221-
self->ringbuf = (ringbuf_t) { receiver_buffer, receiver_buffer_size };
222+
ringbuf_init(&self->ringbuf, receiver_buffer, receiver_buffer_size);
223+
self->allocated_ringbuf = false;
222224
} else {
225+
// Initially allocate the UART's buffer in the long-lived part of the
226+
// heap. UARTs are generally long-lived objects, but the "make long-
227+
// lived" machinery is incapable of moving internal pointers like
228+
// self->buffer, so do it manually. (However, as long as internal
229+
// pointers like this are NOT moved, allocating the buffer
230+
// in the long-lived pool is not strictly necessary)
223231
if (!ringbuf_alloc(&self->ringbuf, receiver_buffer_size, true)) {
224232
mp_raise_ValueError(translate("UART Buffer allocation error"));
225233
}
@@ -284,7 +292,9 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
284292
self->rx = NULL;
285293
}
286294

287-
ringbuf_free(&self->ringbuf);
295+
if (self->allocated_ringbuf) {
296+
ringbuf_free(&self->ringbuf);
297+
}
288298
}
289299

290300
size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ typedef struct {
4848
const mcu_periph_obj_t *rx;
4949

5050
ringbuf_t ringbuf;
51-
uint8_t rx_char;
52-
5351
uint32_t baudrate;
5452
uint32_t timeout_ms;
5553

54+
uint8_t rx_char;
5655
bool sigint_enabled;
56+
bool allocated_ringbuf;
5757
} busio_uart_obj_t;
5858

5959
void uart_reset(void);

ports/unix/coverage.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,8 @@ STATIC mp_obj_t extra_coverage(void) {
528528
{
529529
byte buf[100];
530530
ringbuf_t ringbuf;
531+
// Capacity of ringbuf is 100, one less than actual memory size.
532+
// We pass the actual size to ringbuf_init().
531533
ringbuf_init(&ringbuf, &buf[0], sizeof(buf));
532534

533535
mp_printf(&mp_plat_print, "# ringbuf\n");

py/ringbuf.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727

2828
#include "ringbuf.h"
2929

30-
bool ringbuf_init(ringbuf_t *r, uint8_t *buf, size_t capacity) {
30+
// Note that ringbuf_init() takes the actual buffer size, but
31+
// ringbuf_alloc() takes the capacity. The capacity is one less than the size.
32+
bool ringbuf_init(ringbuf_t *r, uint8_t *buf, size_t buf_size) {
3133
r->heap = false;
3234
r->buf = buf;
33-
r->size = capacity;
35+
r->size = buf_size;
3436
r->iget = r->iput = 0;
3537
return r->buf != NULL;
3638
}

py/ringbuf.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ typedef struct _ringbuf_t {
4242

4343
// Note that the capacity of the buffer is N-1!
4444

45-
// For static initialization use ringbuf_init()
46-
bool ringbuf_init(ringbuf_t *r, uint8_t *buf, size_t capacity);
45+
// For static initialization use ringbuf_init(), which takes actual size instead of capacity.
46+
bool ringbuf_init(ringbuf_t *r, uint8_t *buf, size_t buf_size);
47+
// For heap allocation, use ringbuf_alloc, which takes capacity.
4748
bool ringbuf_alloc(ringbuf_t *r, size_t capacity, bool long_lived);
4849
void ringbuf_free(ringbuf_t *r);
4950
size_t ringbuf_capacity(ringbuf_t *r);

0 commit comments

Comments
 (0)
0