Skip to content

Commit 78bc092

Browse files
committed
samd/machine_uart.c: Fix unintended UART buffer allocation on init().
The buffer was be reset on every call to uart.init(). If no sizes were given, the buffer was be set to the default size 256. That made problems e.g. with PPP. This PR fixes it, keeping the buffer size if not deliberately changed and allocating new buffers only if the size was changed. Signed-off-by: robert-hh <robert@hammelrath.com>
1 parent c764770 commit 78bc092

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

ports/samd/machine_uart.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,26 +414,34 @@ static void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
414414
}
415415

416416
// Set the RX buffer size if configured.
417-
size_t rxbuf_len = DEFAULT_BUFFER_SIZE;
417+
size_t rxbuf_len = self->read_buffer.size - 1;
418418
if (args[ARG_rxbuf].u_int > 0) {
419419
rxbuf_len = args[ARG_rxbuf].u_int;
420420
if (rxbuf_len < MIN_BUFFER_SIZE) {
421421
rxbuf_len = MIN_BUFFER_SIZE;
422422
} else if (rxbuf_len > MAX_BUFFER_SIZE) {
423423
mp_raise_ValueError(MP_ERROR_TEXT("rxbuf too large"));
424424
}
425+
// Force re-allocting of the buffer if the size changed
426+
if (rxbuf_len != (self->read_buffer.size - 1)) {
427+
self->read_buffer.buf = NULL;
428+
}
425429
}
426430

427431
#if MICROPY_HW_UART_TXBUF
428432
// Set the TX buffer size if configured.
429-
size_t txbuf_len = DEFAULT_BUFFER_SIZE;
433+
size_t txbuf_len = self->write_buffer.size - 1;
430434
if (args[ARG_txbuf].u_int > 0) {
431435
txbuf_len = args[ARG_txbuf].u_int;
432436
if (txbuf_len < MIN_BUFFER_SIZE) {
433437
txbuf_len = MIN_BUFFER_SIZE;
434438
} else if (txbuf_len > MAX_BUFFER_SIZE) {
435439
mp_raise_ValueError(MP_ERROR_TEXT("txbuf too large"));
436440
}
441+
// Force re-allocting of the buffer if the size changed
442+
if (txbuf_len != (self->write_buffer.size - 1)) {
443+
self->write_buffer.buf = NULL;
444+
}
437445
}
438446
#endif
439447

@@ -462,10 +470,14 @@ static void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
462470
}
463471

464472
// Allocate the RX/TX buffers.
465-
ringbuf_alloc(&(self->read_buffer), rxbuf_len + 1);
473+
if (self->read_buffer.buf == NULL) {
474+
ringbuf_alloc(&(self->read_buffer), rxbuf_len + 1);
475+
}
466476

467477
#if MICROPY_HW_UART_TXBUF
468-
ringbuf_alloc(&(self->write_buffer), txbuf_len + 1);
478+
if (self->write_buffer.buf == NULL) {
479+
ringbuf_alloc(&(self->write_buffer), txbuf_len + 1);
480+
}
469481
#endif
470482

471483
// Step 1: Configure the Pin mux.
@@ -503,6 +515,12 @@ static mp_obj_t mp_machine_uart_make_new(const mp_obj_type_t *type, size_t n_arg
503515
self->stop = 0;
504516
self->timeout = 1;
505517
self->timeout_char = 1;
518+
self->read_buffer.size = DEFAULT_BUFFER_SIZE + 1;
519+
self->read_buffer.buf = NULL;
520+
#if MICROPY_HW_UART_TXBUF
521+
self->write_buffer.size = DEFAULT_BUFFER_SIZE + 1;
522+
self->write_buffer.buf = NULL;
523+
#endif
506524
#if defined(pin_TX) && defined(pin_RX)
507525
// Initialize with the default pins
508526
self->tx = mp_hal_get_pin_obj((mp_obj_t)pin_TX);

0 commit comments

Comments
 (0)