Skip to content

Commit c03dd3b

Browse files
Ryan Shawdpgeorge
authored andcommitted
stmhal: Fix uart off by 1 circular buffer size.
1 parent 54a1d9e commit c03dd3b

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

stmhal/uart.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,8 @@ STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k
429429
}
430430
mp_printf(print, ", stop=%u, timeout=%u, timeout_char=%u, read_buf_len=%u)",
431431
self->uart.Init.StopBits == UART_STOPBITS_1 ? 1 : 2,
432-
self->timeout, self->timeout_char, self->read_buf_len);
432+
self->timeout, self->timeout_char,
433+
self->read_buf_len == 0 ? 0 : self->read_buf_len - 1); // -1 to adjust for usable length of buffer
433434
}
434435
}
435436

@@ -538,8 +539,8 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, con
538539
__HAL_UART_DISABLE_IT(&self->uart, UART_IT_RXNE);
539540
} else {
540541
// read buffer using interrupts
541-
self->read_buf_len = args[7].u_int;
542-
self->read_buf = m_new(byte, args[7].u_int << self->char_width);
542+
self->read_buf_len = args[7].u_int + 1; // +1 to adjust for usable length of buffer
543+
self->read_buf = m_new(byte, self->read_buf_len << self->char_width);
543544
__HAL_UART_ENABLE_IT(&self->uart, UART_IT_RXNE);
544545
HAL_NVIC_SetPriority(self->irqn, IRQ_PRI_UART, IRQ_SUBPRI_UART);
545546
HAL_NVIC_EnableIRQ(self->irqn);

0 commit comments

Comments
 (0)