@@ -101,7 +101,8 @@ void uart_disarm_tx_interrupt(uart_t* uart);
101
101
void uart_set_baudrate (uart_t * uart, int baud_rate);
102
102
int uart_get_baudrate (uart_t * uart);
103
103
104
- uart_t * uart_init (int uart_nr, int baudrate, byte config);
104
+ uart_t * uart_start_init (int uart_nr, int baudrate, byte config);
105
+ void uart_finish_init (uart_t * uart);
105
106
void uart_uninit (uart_t * uart);
106
107
void uart_swap (uart_t * uart);
107
108
@@ -273,9 +274,8 @@ int uart_get_baudrate(uart_t* uart) {
273
274
return uart->baud_rate ;
274
275
}
275
276
276
- uart_t * uart_init (int uart_nr, int baudrate, byte config, byte mode) {
277
+ uart_t * uart_start_init (int uart_nr, int baudrate, byte config, byte mode) {
277
278
278
- uint32_t conf1 = 0x00000000 ;
279
279
uart_t * uart = (uart_t *) os_malloc (sizeof (uart_t ));
280
280
281
281
if (uart == 0 ) {
@@ -311,6 +311,12 @@ uart_t* uart_init(int uart_nr, int baudrate, byte config, byte mode) {
311
311
uart_set_baudrate (uart, baudrate);
312
312
USC0 (uart->uart_nr ) = config;
313
313
314
+ return uart;
315
+ }
316
+
317
+ void uart_finish_init (uart_t * uart) {
318
+ uint32_t conf1 = 0x00000000 ;
319
+
314
320
uart_flush (uart);
315
321
uart_interrupt_enable (uart);
316
322
@@ -323,8 +329,6 @@ uart_t* uart_init(int uart_nr, int baudrate, byte config, byte mode) {
323
329
}
324
330
325
331
USC1 (uart->uart_nr ) = conf1;
326
-
327
- return uart;
328
332
}
329
333
330
334
void uart_uninit (uart_t * uart) {
@@ -485,31 +489,45 @@ HardwareSerial::HardwareSerial(int uart_nr) :
485
489
}
486
490
487
491
void HardwareSerial::begin (unsigned long baud, byte config, byte mode) {
492
+ InterruptLock il;
488
493
489
494
// disable debug for this interface
490
495
if (uart_get_debug () == _uart_nr) {
491
496
uart_set_debug (UART_NO);
492
497
}
493
498
494
- _uart = uart_init (_uart_nr, baud, config, mode);
499
+ if (_uart) {
500
+ os_free (_uart);
501
+ }
502
+ _uart = uart_start_init (_uart_nr, baud, config, mode);
495
503
496
504
if (_uart == 0 ) {
497
505
return ;
498
506
}
499
507
500
- if (_uart->rxEnabled ) {
501
- if (!_rx_buffer)
502
- _rx_buffer = new cbuf (SERIAL_RX_BUFFER_SIZE);
508
+ // Disable the RX and/or TX functions if we fail to allocate circular buffers.
509
+ // The user can confirm they are enabled with isRxEnabled() and isTxEnabled().
510
+ if (_uart->rxEnabled && !_rx_buffer) {
511
+ _rx_buffer = new cbuf (SERIAL_RX_BUFFER_SIZE);
512
+ if (!_rx_buffer) {
513
+ _uart->rxEnabled = false ;
514
+ }
503
515
}
504
- if (_uart->txEnabled ) {
505
- if (!_tx_buffer)
506
- _tx_buffer = new cbuf (SERIAL_TX_BUFFER_SIZE);
516
+ if (_uart->txEnabled && !_tx_buffer) {
517
+ _tx_buffer = new cbuf (SERIAL_TX_BUFFER_SIZE);
518
+ if (!_tx_buffer) {
519
+ _uart->txEnabled = false ;
520
+ }
507
521
}
508
522
_written = false ;
509
523
delay (1 );
524
+
525
+ uart_finish_init (_uart);
510
526
}
511
527
512
528
void HardwareSerial::end () {
529
+ InterruptLock il;
530
+
513
531
if (uart_get_debug () == _uart_nr) {
514
532
uart_set_debug (UART_NO);
515
533
}
@@ -660,16 +678,10 @@ HardwareSerial::operator bool() const {
660
678
}
661
679
662
680
void ICACHE_RAM_ATTR HardwareSerial::_rx_complete_irq (char c) {
663
- if (_rx_buffer) {
664
- _rx_buffer->write (c);
665
- }
681
+ _rx_buffer->write (c);
666
682
}
667
683
668
684
void ICACHE_RAM_ATTR HardwareSerial::_tx_empty_irq (void ) {
669
- if (_uart == 0 )
670
- return ;
671
- if (_tx_buffer == 0 )
672
- return ;
673
685
const int uart_nr = _uart->uart_nr ;
674
686
size_t queued = _tx_buffer->getSize ();
675
687
if (!queued) {
0 commit comments