@@ -66,6 +66,8 @@ typedef struct _machine_uart_obj_t {
66
66
uint16_t tx_status ;
67
67
uint8_t * txbuf ;
68
68
uint16_t txbuf_len ;
69
+ uint8_t * rxbuf ;
70
+ uint16_t rxbuf_len ;
69
71
bool new ;
70
72
uint16_t mp_irq_trigger ; // user IRQ trigger mask
71
73
uint16_t mp_irq_flags ; // user IRQ active IRQ flags
@@ -197,7 +199,7 @@ static void mp_machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_
197
199
self -> id , self -> config .baudRate_Bps , 8 - self -> config .dataBitsCount ,
198
200
_parity_name [self -> config .parityMode ], self -> config .stopBitCount + 1 ,
199
201
_flow_name [(self -> config .enableTxCTS << 1 ) | self -> config .enableRxRTS ],
200
- self -> handle . rxRingBufferSize , self -> txbuf_len , self -> timeout , self -> timeout_char ,
202
+ self -> rxbuf_len , self -> txbuf_len , self -> timeout , self -> timeout_char ,
201
203
_invert_name [self -> invert ], self -> mp_irq_trigger );
202
204
}
203
205
@@ -291,25 +293,33 @@ static void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
291
293
self -> config .enableRx = true;
292
294
293
295
// Set the RX buffer size if configured.
294
- size_t rxbuf_len = DEFAULT_BUFFER_SIZE ;
295
296
if (args [ARG_rxbuf ].u_int > 0 ) {
296
- rxbuf_len = args [ARG_rxbuf ].u_int ;
297
+ size_t rxbuf_len = args [ARG_rxbuf ].u_int ;
297
298
if (rxbuf_len < MIN_BUFFER_SIZE ) {
298
299
rxbuf_len = MIN_BUFFER_SIZE ;
299
300
} else if (rxbuf_len > MAX_BUFFER_SIZE ) {
300
301
mp_raise_ValueError (MP_ERROR_TEXT ("rxbuf too large" ));
301
302
}
303
+ // Force re-allocting of the buffer if the size changed
304
+ if (rxbuf_len != self -> rxbuf_len ) {
305
+ self -> rxbuf = NULL ;
306
+ self -> rxbuf_len = rxbuf_len ;
307
+ }
302
308
}
303
309
304
310
// Set the TX buffer size if configured.
305
- size_t txbuf_len = DEFAULT_BUFFER_SIZE ;
306
311
if (args [ARG_txbuf ].u_int > 0 ) {
307
- txbuf_len = args [ARG_txbuf ].u_int ;
312
+ size_t txbuf_len = args [ARG_txbuf ].u_int ;
308
313
if (txbuf_len < MIN_BUFFER_SIZE ) {
309
314
txbuf_len = MIN_BUFFER_SIZE ;
310
315
} else if (txbuf_len > MAX_BUFFER_SIZE ) {
311
316
mp_raise_ValueError (MP_ERROR_TEXT ("txbuf too large" ));
312
317
}
318
+ // Force re-allocting of the buffer if the size is changed
319
+ if (txbuf_len != self -> txbuf_len ) {
320
+ self -> txbuf = NULL ;
321
+ self -> txbuf_len = txbuf_len ;
322
+ }
313
323
}
314
324
315
325
// Initialise the UART peripheral if any arguments given, or it was not initialised previously.
@@ -335,10 +345,13 @@ static void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
335
345
LPUART_Init (self -> lpuart , & self -> config , CLOCK_GetClockRootFreq (kCLOCK_UartClkRoot ));
336
346
#endif
337
347
LPUART_TransferCreateHandle (self -> lpuart , & self -> handle , LPUART_UserCallback , self );
338
- uint8_t * buffer = m_new (uint8_t , rxbuf_len + 1 );
339
- LPUART_TransferStartRingBuffer (self -> lpuart , & self -> handle , buffer , rxbuf_len );
340
- self -> txbuf = m_new (uint8_t , txbuf_len ); // Allocate the TX buffer.
341
- self -> txbuf_len = txbuf_len ;
348
+ if (self -> rxbuf == NULL ) {
349
+ self -> rxbuf = m_new (uint8_t , self -> rxbuf_len + 1 );
350
+ }
351
+ LPUART_TransferStartRingBuffer (self -> lpuart , & self -> handle , self -> rxbuf , self -> rxbuf_len );
352
+ if (self -> txbuf == NULL ) {
353
+ self -> txbuf = m_new (uint8_t , self -> txbuf_len ); // Allocate the TX buffer.
354
+ }
342
355
343
356
#if MICROPY_PY_MACHINE_UART_IRQ
344
357
LPUART_EnableInterrupts (self -> lpuart , kLPUART_IdleLineInterruptEnable );
@@ -380,6 +393,10 @@ static mp_obj_t mp_machine_uart_make_new(const mp_obj_type_t *type, size_t n_arg
380
393
self -> invert = false;
381
394
self -> timeout = 1 ;
382
395
self -> timeout_char = 1 ;
396
+ self -> rxbuf = NULL ;
397
+ self -> rxbuf_len = DEFAULT_BUFFER_SIZE ;
398
+ self -> txbuf = NULL ;
399
+ self -> txbuf_len = DEFAULT_BUFFER_SIZE ;
383
400
self -> new = true;
384
401
self -> mp_irq_obj = NULL ;
385
402
self -> mp_irq_trigger = 0 ;
0 commit comments