From 5b5deb5a7784702b0c71a5bea1cfa15bac9b7256 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Sun, 17 May 2015 13:43:49 +0200 Subject: [PATCH] improve os_printf handling when buffer full. - wait for free buffer in hw fifo --- cores/esp8266/HardwareSerial.cpp | 46 +++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/cores/esp8266/HardwareSerial.cpp b/cores/esp8266/HardwareSerial.cpp index bdcd45716..ac56919ca 100644 --- a/cores/esp8266/HardwareSerial.cpp +++ b/cores/esp8266/HardwareSerial.cpp @@ -42,12 +42,12 @@ extern "C" { #define UART_TX_FIFO_SIZE 0x80 struct uart_ { - int uart_nr; + int uart_nr; int baud_rate; bool rxEnabled; bool txEnabled; - uint8_t rxPin; - uint8_t txPin; + uint8_t rxPin; + uint8_t txPin; }; static const int UART0 = 0; @@ -120,7 +120,7 @@ void ICACHE_RAM_ATTR uart_interrupt_handler(uart_t* uart) { // -------------- UART 0 -------------- if(Serial.isRxEnabled()) { while(U0IS & (1 << UIFF)) { - Serial._rx_complete_irq((char)(U0F & 0xff)); + Serial._rx_complete_irq((char) (U0F & 0xff)); U0IC = (1 << UIFF); } } @@ -135,7 +135,7 @@ void ICACHE_RAM_ATTR uart_interrupt_handler(uart_t* uart) { if(Serial1.isRxEnabled()) { while(U1IS & (1 << UIFF)) { - Serial1._rx_complete_irq((char)(U1F & 0xff)); + Serial1._rx_complete_irq((char) (U1F & 0xff)); U1IC = (1 << UIFF); } } @@ -357,19 +357,19 @@ void uart_swap(uart_t* uart) { switch(uart->uart_nr) { case UART0: if(uart->txPin == 1 && uart->rxPin == 3) { - pinMode(15, FUNCTION_4);//TX - pinMode(13, FUNCTION_4);//RX + pinMode(15, FUNCTION_4); //TX + pinMode(13, FUNCTION_4); //RX USWAP |= (1 << USWAP0); - pinMode(1, INPUT);//TX - pinMode(3, INPUT);//RX + pinMode(1, INPUT); //TX + pinMode(3, INPUT); //RX uart->rxPin = 13; uart->txPin = 15; } else { - pinMode(1, SPECIAL);//TX - pinMode(3, SPECIAL);//RX + pinMode(1, SPECIAL); //TX + pinMode(3, SPECIAL); //RX USWAP &= ~(1 << USWAP0); - pinMode(15, INPUT);//TX - pinMode(13, INPUT);//RX + pinMode(15, INPUT); //TX + pinMode(13, INPUT); //RX uart->rxPin = 3; uart->txPin = 1; } @@ -400,6 +400,14 @@ void uart0_write_char(char c) { return; } } + + // wait for the Hardware FIFO + while(true) { + if(((USS(0) >> USTXC) & 0xff) <= (UART_TX_FIFO_SIZE - 2)) { + break; + } + } + if(c == '\n') { USF(0) = '\r'; } @@ -416,6 +424,14 @@ void uart1_write_char(char c) { return; } } + + // wait for the Hardware FIFO + while(true) { + if(((USS(1) >> USTXC) & 0xff) <= (UART_TX_FIFO_SIZE - 2)) { + break; + } + } + if(c == '\n') { USF(1) = '\r'; } @@ -469,11 +485,11 @@ void HardwareSerial::begin(unsigned long baud, byte config) { } if(_uart->rxEnabled) { - if (!_rx_buffer) + if(!_rx_buffer) _rx_buffer = new cbuf(SERIAL_RX_BUFFER_SIZE); } if(_uart->txEnabled) { - if (!_tx_buffer) + if(!_tx_buffer) _tx_buffer = new cbuf(SERIAL_TX_BUFFER_SIZE); } _written = false;