@@ -88,8 +88,10 @@ typedef struct _machine_uart_obj_t {
88
88
uint16_t timeout ; // timeout waiting for first char (in ms)
89
89
uint16_t timeout_char ; // timeout waiting between chars (in ms)
90
90
bool new ;
91
+ uint16_t rxbuf_len ;
91
92
ringbuf_t read_buffer ;
92
93
#if MICROPY_HW_UART_TXBUF
94
+ uint16_t txbuf_len ;
93
95
ringbuf_t write_buffer ;
94
96
#endif
95
97
#if MICROPY_PY_MACHINE_UART_IRQ
@@ -154,7 +156,8 @@ void common_uart_irq_handler(int uart_id) {
154
156
}
155
157
}
156
158
#endif
157
- } else if (uart -> USART .INTFLAG .bit .DRE != 0 ) {
159
+ }
160
+ if (uart -> USART .INTFLAG .bit .DRE != 0 ) {
158
161
#if MICROPY_HW_UART_TXBUF
159
162
// handle the outgoing data
160
163
if (ringbuf_avail (& self -> write_buffer ) > 0 ) {
@@ -287,16 +290,6 @@ void machine_uart_set_baudrate(mp_obj_t self_in, uint32_t baudrate) {
287
290
288
291
static void mp_machine_uart_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
289
292
machine_uart_obj_t * self = MP_OBJ_TO_PTR (self_in );
290
- size_t rxbuf_len = self -> read_buffer .size - 1 ;
291
- #if MICROPY_HW_UART_TXBUF
292
- size_t txbuf_len = self -> write_buffer .size - 1 ;
293
- #endif
294
- if (self -> bits > 8 ) {
295
- rxbuf_len /= 2 ;
296
- #if MICROPY_HW_UART_TXBUF
297
- txbuf_len /= 2 ;
298
- #endif
299
- }
300
293
301
294
mp_printf (print , "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, "
302
295
"tx=\"%q\", rx=\"%q\", timeout=%u, timeout_char=%u, rxbuf=%d"
@@ -312,9 +305,9 @@ static void mp_machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_
312
305
")" ,
313
306
self -> id , self -> baudrate , self -> bits , _parity_name [self -> parity ],
314
307
self -> stop + 1 , pin_find_by_id (self -> tx )-> name , pin_find_by_id (self -> rx )-> name ,
315
- self -> timeout , self -> timeout_char , rxbuf_len
308
+ self -> timeout , self -> timeout_char , self -> rxbuf_len
316
309
#if MICROPY_HW_UART_TXBUF
317
- , txbuf_len
310
+ , self -> txbuf_len
318
311
#endif
319
312
#if MICROPY_HW_UART_RTSCTS
320
313
, self -> rts != 0xff ? pin_find_by_id (self -> rts )-> name : MP_QSTR_None
@@ -358,6 +351,11 @@ static void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
358
351
// Set bits if configured.
359
352
if (args [ARG_bits ].u_int > 0 ) {
360
353
self -> bits = args [ARG_bits ].u_int ;
354
+ // Invalidate the buffers since the size may have to be changed
355
+ self -> read_buffer .buf = NULL ;
356
+ #if MICROPY_HW_UART_TXBUF
357
+ self -> write_buffer .buf = NULL ;
358
+ #endif
361
359
}
362
360
363
361
// Set parity if configured.
@@ -414,26 +412,36 @@ static void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
414
412
}
415
413
416
414
// Set the RX buffer size if configured.
417
- size_t rxbuf_len = DEFAULT_BUFFER_SIZE ;
415
+ size_t rxbuf_len = self -> rxbuf_len ;
418
416
if (args [ARG_rxbuf ].u_int > 0 ) {
419
417
rxbuf_len = args [ARG_rxbuf ].u_int ;
420
418
if (rxbuf_len < MIN_BUFFER_SIZE ) {
421
419
rxbuf_len = MIN_BUFFER_SIZE ;
422
420
} else if (rxbuf_len > MAX_BUFFER_SIZE ) {
423
421
mp_raise_ValueError (MP_ERROR_TEXT ("rxbuf too large" ));
424
422
}
423
+ // Force re-allocting of the buffer if the size changed
424
+ if (rxbuf_len != self -> rxbuf_len ) {
425
+ self -> read_buffer .buf = NULL ;
426
+ self -> rxbuf_len = rxbuf_len ;
427
+ }
425
428
}
426
429
427
430
#if MICROPY_HW_UART_TXBUF
428
431
// Set the TX buffer size if configured.
429
- size_t txbuf_len = DEFAULT_BUFFER_SIZE ;
432
+ size_t txbuf_len = self -> txbuf_len ;
430
433
if (args [ARG_txbuf ].u_int > 0 ) {
431
434
txbuf_len = args [ARG_txbuf ].u_int ;
432
435
if (txbuf_len < MIN_BUFFER_SIZE ) {
433
436
txbuf_len = MIN_BUFFER_SIZE ;
434
437
} else if (txbuf_len > MAX_BUFFER_SIZE ) {
435
438
mp_raise_ValueError (MP_ERROR_TEXT ("txbuf too large" ));
436
439
}
440
+ // Force re-allocting of the buffer if the size changed
441
+ if (txbuf_len != self -> txbuf_len ) {
442
+ self -> write_buffer .buf = NULL ;
443
+ self -> txbuf_len = txbuf_len ;
444
+ }
437
445
}
438
446
#endif
439
447
@@ -462,10 +470,14 @@ static void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
462
470
}
463
471
464
472
// 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
+ }
466
476
467
477
#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
+ }
469
481
#endif
470
482
471
483
// 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
503
515
self -> stop = 0 ;
504
516
self -> timeout = 1 ;
505
517
self -> timeout_char = 1 ;
518
+ self -> rxbuf_len = DEFAULT_BUFFER_SIZE ;
519
+ self -> read_buffer .buf = NULL ;
520
+ #if MICROPY_HW_UART_TXBUF
521
+ self -> txbuf_len = DEFAULT_BUFFER_SIZE ;
522
+ self -> write_buffer .buf = NULL ;
523
+ #endif
506
524
#if defined(pin_TX ) && defined(pin_RX )
507
525
// Initialize with the default pins
508
526
self -> tx = mp_hal_get_pin_obj ((mp_obj_t )pin_TX );
0 commit comments