1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-22 21:23:07 +03:00

Bufferless and Interruptless HardwareSerial

Let's use the onboard buffers :)
This commit is contained in:
Me No Dev 2016-01-23 13:02:01 +02:00 committed by Ivan Grokhotkov
parent aeb9597ccf
commit e255f25cfd
2 changed files with 265 additions and 558 deletions

View File

@ -82,683 +82,396 @@ static const int UART_NO = -1;
HardwareSerial Serial(UART0); HardwareSerial Serial(UART0);
HardwareSerial Serial1(UART1); HardwareSerial Serial1(UART1);
// ####################################################################################################
// ####################################################################################################
// ####################################################################################################
void uart_interrupt_handler(uart_t* uart); void uart_write_char(uart_t* uart, char c) {
void uart_wait_for_tx_fifo(uart_t* uart, size_t size_needed); if(uart == 0 || !uart->txEnabled)
return;
size_t uart_get_tx_fifo_room(uart_t* uart); while((USS(uart->uart_nr) >> USTXC) >= 0x7f);
void uart_wait_for_transmit(uart_t* uart); USF(uart->uart_nr) = c;
void uart_transmit_char(uart_t* uart, char c);
void uart_transmit(uart_t* uart, const char* buf, size_t size);
void uart_flush(uart_t* uart);
void uart_interrupt_enable(uart_t* uart);
void uart_interrupt_disable(uart_t* uart);
void uart_arm_tx_interrupt(uart_t* uart);
void uart_disarm_tx_interrupt(uart_t* uart);
void uart_set_baudrate(uart_t* uart, int baud_rate);
int uart_get_baudrate(uart_t* uart);
uart_t* uart_start_init(int uart_nr, int baudrate, byte config, uint8_t use_tx);
void uart_finish_init(uart_t* uart);
void uart_uninit(uart_t* uart);
void uart_swap(uart_t* uart, uint8_t use_tx);
void uart_set_tx(uart_t* uart, uint8_t use_tx);
void uart_set_pins(uart_t* uart, uint8_t tx, uint8_t rx);
void uart_ignore_char(char c);
void uart0_write_char(char c);
void uart1_write_char(char c);
void uart_set_debug(int uart_nr);
int uart_get_debug();
// ####################################################################################################
// ####################################################################################################
// ####################################################################################################
// These function internals can be used from interrupt handlers to ensure they
// are in instruction RAM, or anywhere that the uart_nr has been validated.
#define UART_GET_TX_FIFO_ROOM(uart_nr) (UART_TX_FIFO_SIZE - ((USS(uart_nr) >> USTXC) & 0xff))
#define UART_TRANSMIT_CHAR(uart_nr, c) do { USF(uart_nr) = (c); } while(0)
#define UART_ARM_TX_INTERRUPT(uart_nr) do { USIE(uart_nr) |= (1 << UIFE); } while(0)
#define UART_DISARM_TX_INTERRUPT(uart_nr) do { USIE(uart_nr) &= ~(1 << UIFE); } while(0)
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));
U0IC = (1 << UIFF);
}
}
if(Serial.isTxEnabled()) {
if(U0IS & (1 << UIFE)) {
U0IC = (1 << UIFE);
Serial._tx_empty_irq();
}
}
// -------------- UART 1 --------------
// Note: only TX is supported on UART 1.
if(Serial1.isTxEnabled()) {
if(U1IS & (1 << UIFE)) {
U1IC = (1 << UIFE);
Serial1._tx_empty_irq();
}
}
} }
// #################################################################################################### void uart_write(uart_t* uart, const char* buf, size_t size) {
if(uart == 0 || !uart->txEnabled)
void uart_wait_for_tx_fifo(uart_t* uart, size_t size_needed) { return;
if(uart == 0) while(size--)
return; uart_write_char(uart, *buf++);
if(uart->txEnabled) {
while(true) {
size_t tx_count = (USS(uart->uart_nr) >> USTXC) & 0xff;
if(tx_count <= (UART_TX_FIFO_SIZE - size_needed))
break;
}
}
} }
size_t uart_get_tx_fifo_room(uart_t* uart) { uint8_t uart_read_char(uart_t* uart){
if(uart == 0) if(uart == 0 || !uart->rxEnabled)
return 0;
if(uart->txEnabled) {
return UART_GET_TX_FIFO_ROOM(uart->uart_nr);
}
return 0; return 0;
return USF(uart->uart_nr) & 0xff;
} }
void uart_wait_for_transmit(uart_t* uart) { uint8_t uart_rx_available(uart_t* uart){
if(uart == 0) if(uart == 0 || !uart->rxEnabled)
return; return 0;
if(uart->txEnabled) { return (USS(uart->uart_nr) >> USRXC) & 0xff;
uart_wait_for_tx_fifo(uart, UART_TX_FIFO_SIZE);
}
} }
void uart_transmit_char(uart_t* uart, char c) { uint8_t uart_tx_free(uart_t* uart){
if(uart == 0) if(uart == 0 || !uart->txEnabled)
return; return 0;
if(uart->txEnabled) { return UART_TX_FIFO_SIZE - ((USS(uart->uart_nr) >> USTXC) & 0xff);
UART_TRANSMIT_CHAR(uart->uart_nr, c);
}
} }
void uart_transmit(uart_t* uart, const char* buf, size_t size) { void uart_wait_tx_empty(uart_t* uart){
if(uart == 0) if(uart == 0 || !uart->txEnabled)
return; return;
if(uart->txEnabled) { while(((USS(uart->uart_nr) >> USTXC) & 0xff) > 0) delay(0);
while(size) {
size_t part_size = (size > UART_TX_FIFO_SIZE) ? UART_TX_FIFO_SIZE : size;
size -= part_size;
uart_wait_for_tx_fifo(uart, part_size);
for(; part_size; --part_size, ++buf)
USF(uart->uart_nr) = *buf;
}
}
} }
void uart_flush(uart_t* uart) { void uart_flush(uart_t* uart) {
uint32_t tmp = 0x00000000; if(uart == 0)
return;
if(uart == 0) uint32_t tmp = 0x00000000;
return; if(uart->rxEnabled) {
tmp |= (1 << UCRXRST);
}
if(uart->rxEnabled) { if(uart->txEnabled) {
tmp |= (1 << UCRXRST); tmp |= (1 << UCTXRST);
} }
if(uart->txEnabled) { USC0(uart->uart_nr) |= (tmp);
tmp |= (1 << UCTXRST); USC0(uart->uart_nr) &= ~(tmp);
}
USC0(uart->uart_nr) |= (tmp);
USC0(uart->uart_nr) &= ~(tmp);
}
void uart_interrupt_enable(uart_t* uart) {
if(uart == 0)
return;
USIC(uart->uart_nr) = 0x1ff;
ETS_UART_INTR_ATTACH(&uart_interrupt_handler, uart); // uart parameter is not osed in irq function!
if(uart->rxEnabled) {
USIE(uart->uart_nr) |= (1 << UIFF);
}
ETS_UART_INTR_ENABLE();
}
void uart_interrupt_disable(uart_t* uart) {
if(uart == 0)
return;
if(uart->rxEnabled) {
USIE(uart->uart_nr) &= ~(1 << UIFF);
}
if(uart->txEnabled) {
USIE(uart->uart_nr) &= ~(1 << UIFE);
}
//ETS_UART_INTR_DISABLE(); // never disable irq complete may its needed by the other Serial Interface!
}
void uart_arm_tx_interrupt(uart_t* uart) {
if(uart == 0)
return;
if(uart->txEnabled) {
UART_ARM_TX_INTERRUPT(uart->uart_nr);
}
}
void uart_disarm_tx_interrupt(uart_t* uart) {
if(uart == 0)
return;
if(uart->txEnabled) {
UART_DISARM_TX_INTERRUPT(uart->uart_nr);
}
} }
void uart_set_baudrate(uart_t* uart, int baud_rate) { void uart_set_baudrate(uart_t* uart, int baud_rate) {
if(uart == 0) if(uart == 0)
return; return;
uart->baud_rate = baud_rate; uart->baud_rate = baud_rate;
USD(uart->uart_nr) = (ESP8266_CLOCK / uart->baud_rate); USD(uart->uart_nr) = (ESP8266_CLOCK / uart->baud_rate);
} }
int uart_get_baudrate(uart_t* uart) { int uart_get_baudrate(uart_t* uart) {
if(uart == 0) if(uart == 0)
return 0; return 0;
return uart->baud_rate; return uart->baud_rate;
} }
uart_t* uart_start_init(int uart_nr, int baudrate, byte config, byte mode, uint8_t use_tx) { // ####################################################################################################
// ####################################################################################################
// ####################################################################################################
uart_t* uart = (uart_t*) os_malloc(sizeof(uart_t)); uart_t* uart_init(int uart_nr, int baudrate, byte config, byte mode, uint8_t use_tx) {
if(uart == 0) { uart_t* uart = (uart_t*) os_malloc(sizeof(uart_t));
return 0;
}
uart->uart_nr = uart_nr; if(uart == 0) {
return 0;
}
switch(uart->uart_nr) { uart->uart_nr = uart_nr;
case UART0:
uart->rxEnabled = (mode != SERIAL_TX_ONLY);
uart->txEnabled = (mode != SERIAL_RX_ONLY);
uart->rxPin = (uart->rxEnabled)?3:255;
if(uart->rxEnabled) {
if (use_tx == 2) {
uart->txPin = 2;
pinMode(uart->rxPin, FUNCTION_4);
} else {
uart->txPin = 1;
pinMode(uart->rxPin, SPECIAL);
}
} else uart->txPin = 255;
if(uart->txEnabled) pinMode(uart->txPin, SPECIAL);
IOSWAP &= ~(1 << IOSWAPU0);
break;
case UART1:
// Note: uart_interrupt_handler does not support RX on UART 1.
uart->rxEnabled = false;
uart->txEnabled = (mode != SERIAL_RX_ONLY);
uart->rxPin = 255;
uart->txPin = (uart->txEnabled)?2:255; // GPIO7 as TX not possible! See GPIO pins used by UART
if(uart->txEnabled) pinMode(uart->txPin, SPECIAL);
break;
case UART_NO:
default:
// big fail!
os_free(uart);
return 0;
}
uart_set_baudrate(uart, baudrate);
USC0(uart->uart_nr) = config;
return uart; switch(uart->uart_nr) {
} case UART0:
uart->rxEnabled = (mode != SERIAL_TX_ONLY);
uart->txEnabled = (mode != SERIAL_RX_ONLY);
uart->rxPin = (uart->rxEnabled)?3:255;
if(uart->rxEnabled) {
if (use_tx == 2) {
uart->txPin = 2;
pinMode(uart->rxPin, FUNCTION_4);
} else {
uart->txPin = 1;
pinMode(uart->rxPin, SPECIAL);
}
} else uart->txPin = 255;
if(uart->txEnabled) pinMode(uart->txPin, SPECIAL);
IOSWAP &= ~(1 << IOSWAPU0);
break;
case UART1:
// Note: uart_interrupt_handler does not support RX on UART 1.
uart->rxEnabled = false;
uart->txEnabled = (mode != SERIAL_RX_ONLY);
uart->rxPin = 255;
uart->txPin = (uart->txEnabled)?2:255; // GPIO7 as TX not possible! See GPIO pins used by UART
if(uart->txEnabled) pinMode(uart->txPin, SPECIAL);
break;
case UART_NO:
default:
// big fail!
os_free(uart);
return 0;
}
void uart_finish_init(uart_t* uart) { uart_set_baudrate(uart, baudrate);
uint32_t conf1 = 0x00000000; USC0(uart->uart_nr) = config;
uart_flush(uart);
USC1(uart->uart_nr) = 0;
uart_flush(uart); return uart;
uart_interrupt_enable(uart);
if(uart->rxEnabled) {
conf1 |= (0x01 << UCFFT);
}
if(uart->txEnabled) {
conf1 |= (0x20 << UCFET);
}
USC1(uart->uart_nr) = conf1;
} }
void uart_uninit(uart_t* uart) { void uart_uninit(uart_t* uart) {
if(uart == 0) if(uart == 0)
return; return;
uart_interrupt_disable(uart);
switch(uart->rxPin) { switch(uart->rxPin) {
case 3: case 3:
pinMode(3, INPUT); pinMode(3, INPUT);
break; break;
case 13: case 13:
pinMode(13, INPUT); pinMode(13, INPUT);
break; break;
} }
switch(uart->txPin) { switch(uart->txPin) {
case 1: case 1:
pinMode(1, INPUT); pinMode(1, INPUT);
break; break;
case 2: case 2:
pinMode(2, INPUT); pinMode(2, INPUT);
break; break;
case 15: case 15:
pinMode(15, INPUT); pinMode(15, INPUT);
break; break;
} }
os_free(uart); os_free(uart);
} }
void uart_swap(uart_t* uart, uint8_t use_tx) { void uart_swap(uart_t* uart, uint8_t use_tx) {
if(uart == 0) if(uart == 0)
return; return;
switch(uart->uart_nr) { switch(uart->uart_nr) {
case UART0: case UART0:
if(((uart->txPin == 1 || uart->txPin == 2) && uart->txEnabled) || (uart->rxPin == 3 && uart->rxEnabled)) { if(((uart->txPin == 1 || uart->txPin == 2) && uart->txEnabled) || (uart->rxPin == 3 && uart->rxEnabled)) {
if(uart->txEnabled){ //TX if(uart->txEnabled){ //TX
pinMode(uart->txPin, INPUT); pinMode(uart->txPin, INPUT);
uart->txPin = 15; uart->txPin = 15;
} }
if(uart->rxEnabled){ //RX if(uart->rxEnabled){ //RX
pinMode(uart->rxPin, INPUT); pinMode(uart->rxPin, INPUT);
uart->rxPin = 13; uart->rxPin = 13;
} }
if(uart->txEnabled) pinMode(uart->txPin, FUNCTION_4); //TX if(uart->txEnabled) pinMode(uart->txPin, FUNCTION_4); //TX
if(uart->rxEnabled) pinMode(uart->rxPin, FUNCTION_4); //RX if(uart->rxEnabled) pinMode(uart->rxPin, FUNCTION_4); //RX
IOSWAP |= (1 << IOSWAPU0); IOSWAP |= (1 << IOSWAPU0);
} else { } else {
if(uart->txEnabled){ //TX if(uart->txEnabled){ //TX
pinMode(uart->txPin, INPUT); pinMode(uart->txPin, INPUT);
uart->txPin = (use_tx == 2)?2:1; uart->txPin = (use_tx == 2)?2:1;
} }
if(uart->rxEnabled){ //RX if(uart->rxEnabled){ //RX
pinMode(uart->rxPin, INPUT); pinMode(uart->rxPin, INPUT);
uart->rxPin = 3; uart->rxPin = 3;
} }
if(uart->txEnabled) pinMode(uart->txPin, (use_tx == 2)?FUNCTION_4:SPECIAL); //TX if(uart->txEnabled) pinMode(uart->txPin, (use_tx == 2)?FUNCTION_4:SPECIAL); //TX
if(uart->rxEnabled) pinMode(3, SPECIAL); //RX if(uart->rxEnabled) pinMode(3, SPECIAL); //RX
IOSWAP &= ~(1 << IOSWAPU0); IOSWAP &= ~(1 << IOSWAPU0);
} }
break; break;
case UART1: case UART1:
// Currently no swap possible! See GPIO pins used by UART // Currently no swap possible! See GPIO pins used by UART
break; break;
default: default:
break; break;
} }
} }
void uart_set_tx(uart_t* uart, uint8_t use_tx) { void uart_set_tx(uart_t* uart, uint8_t use_tx) {
if(uart == 0) if(uart == 0)
return; return;
switch(uart->uart_nr) { switch(uart->uart_nr) {
case UART0: case UART0:
if(uart->txEnabled) { if(uart->txEnabled) {
if (uart->txPin == 1 && use_tx == 2) { if (uart->txPin == 1 && use_tx == 2) {
pinMode(uart->txPin, INPUT); pinMode(uart->txPin, INPUT);
uart->txPin = 2; uart->txPin = 2;
pinMode(uart->txPin, FUNCTION_4); pinMode(uart->txPin, FUNCTION_4);
} else if (uart->txPin == 2 && use_tx != 2) { } else if (uart->txPin == 2 && use_tx != 2) {
pinMode(uart->txPin, INPUT); pinMode(uart->txPin, INPUT);
uart->txPin = 1; uart->txPin = 1;
pinMode(uart->txPin, SPECIAL); pinMode(uart->txPin, SPECIAL);
} }
} }
break; break;
case UART1: case UART1:
// GPIO7 as TX not possible! See GPIO pins used by UART // GPIO7 as TX not possible! See GPIO pins used by UART
break; break;
default: default:
break; break;
} }
} }
void uart_set_pins(uart_t* uart, uint8_t tx, uint8_t rx) { void uart_set_pins(uart_t* uart, uint8_t tx, uint8_t rx) {
if(uart == 0) if(uart == 0)
return; return;
if(uart->uart_nr == UART0) { // Only UART0 allows pin changes if(uart->uart_nr == UART0) { // Only UART0 allows pin changes
if(uart->txEnabled && uart->txPin != tx) { if(uart->txEnabled && uart->txPin != tx) {
if( rx == 13 && tx == 15) { if( rx == 13 && tx == 15) {
uart_swap(uart, 15); uart_swap(uart, 15);
} else if (rx == 3 && (tx == 1 || tx == 2)) { } else if (rx == 3 && (tx == 1 || tx == 2)) {
if (uart->rxPin != rx) uart_swap(uart, tx); if (uart->rxPin != rx) uart_swap(uart, tx);
else uart_set_tx(uart, tx); else uart_set_tx(uart, tx);
} }
}
if(uart->rxEnabled && uart->rxPin != rx && rx == 13 && tx == 15) {
uart_swap(uart, 15);
}
} }
if(uart->rxEnabled && uart->rxPin != rx && rx == 13 && tx == 15) {
uart_swap(uart, 15);
}
}
} }
// #################################################################################################### // ####################################################################################################
// #################################################################################################### // ####################################################################################################
// #################################################################################################### // ####################################################################################################
void uart_ignore_char(char c) { void uart_ignore_char(char c) {}
}
void uart0_write_char(char c) { void uart0_write_char(char c) {
if(&Serial != NULL && Serial.isTxEnabled()) { while(((USS(0) >> USTXC) & 0xff) >= 0x7F) delay(0);
if(Serial.availableForWrite() > 0) { USF(0) = c;
if(c == '\n') {
Serial.write('\r');
}
Serial.write(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';
}
USF(0) = c;
} }
void uart1_write_char(char c) { void uart1_write_char(char c) {
if(&Serial1 != NULL && Serial1.isTxEnabled()) { while(((USS(1) >> USTXC) & 0xff) >= 0x7F) delay(0);
if(Serial1.availableForWrite() > 0) { USF(1) = c;
if(c == '\n') {
Serial1.write('\r');
}
Serial1.write(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';
}
USF(1) = c;
} }
static int s_uart_debug_nr = UART0; static int s_uart_debug_nr = UART0;
void uart_set_debug(int uart_nr) { void uart_set_debug(int uart_nr) {
s_uart_debug_nr = uart_nr; s_uart_debug_nr = uart_nr;
switch(s_uart_debug_nr) { switch(s_uart_debug_nr) {
case UART0: case UART0:
system_set_os_print(1); system_set_os_print(1);
ets_install_putc1((void *) &uart0_write_char); ets_install_putc1((void *) &uart0_write_char);
break; break;
case UART1: case UART1:
system_set_os_print(1); system_set_os_print(1);
ets_install_putc1((void *) &uart1_write_char); ets_install_putc1((void *) &uart1_write_char);
break; break;
case UART_NO: case UART_NO:
default: default:
system_set_os_print(0); system_set_os_print(0);
ets_install_putc1((void *) &uart_ignore_char); ets_install_putc1((void *) &uart_ignore_char);
break; break;
} }
} }
int uart_get_debug() { int uart_get_debug() {
return s_uart_debug_nr; return s_uart_debug_nr;
} }
// #################################################################################################### // ####################################################################################################
// #################################################################################################### // ####################################################################################################
// #################################################################################################### // ####################################################################################################
HardwareSerial::HardwareSerial(int uart_nr) : HardwareSerial::HardwareSerial(int uart_nr)
_uart_nr(uart_nr), _uart(0), _tx_buffer(0), _rx_buffer(0) { : _uart_nr(uart_nr)
} , _uart(0)
{}
void HardwareSerial::begin(unsigned long baud, byte config, byte mode, uint8_t use_tx) { void HardwareSerial::begin(unsigned long baud, byte config, byte mode, uint8_t use_tx) {
InterruptLock il; if(uart_get_debug() == _uart_nr)
uart_set_debug(UART_NO);
// disable debug for this interface if (_uart)
if(uart_get_debug() == _uart_nr) { os_free(_uart);
uart_set_debug(UART_NO);
}
if (_uart) { _uart = uart_init(_uart_nr, baud, config, mode, use_tx);
os_free(_uart);
}
_uart = uart_start_init(_uart_nr, baud, config, mode, use_tx);
if(_uart == 0) {
return;
}
// Disable the RX and/or TX functions if we fail to allocate circular buffers.
// The user can confirm they are enabled with isRxEnabled() and isTxEnabled().
if(_uart->rxEnabled && !_rx_buffer) {
_rx_buffer = new cbuf(SERIAL_RX_BUFFER_SIZE);
if(!_rx_buffer) {
_uart->rxEnabled = false;
}
}
if(_uart->txEnabled && !_tx_buffer) {
_tx_buffer = new cbuf(SERIAL_TX_BUFFER_SIZE);
if(!_tx_buffer) {
_uart->txEnabled = false;
}
}
delay(1);
uart_finish_init(_uart);
} }
void HardwareSerial::end() { void HardwareSerial::end() {
InterruptLock il; if(uart_get_debug() == _uart_nr)
uart_set_debug(UART_NO);
if(uart_get_debug() == _uart_nr) { uart_uninit(_uart);
uart_set_debug(UART_NO);
}
uart_uninit(_uart);
delete _rx_buffer;
delete _tx_buffer;
_uart = 0;
_rx_buffer = 0;
_tx_buffer = 0;
} }
void HardwareSerial::swap(uint8_t use_tx) { void HardwareSerial::swap(uint8_t use_tx) {
if(_uart == 0) if(_uart == 0)
return; return;
uart_swap(_uart, use_tx); uart_swap(_uart, use_tx);
} }
void HardwareSerial::set_tx(uint8_t use_tx) { void HardwareSerial::set_tx(uint8_t use_tx) {
if(_uart == 0) if(_uart == 0)
return; return;
uart_set_tx(_uart, use_tx); uart_set_tx(_uart, use_tx);
} }
void HardwareSerial::pins(uint8_t tx, uint8_t rx) { void HardwareSerial::pins(uint8_t tx, uint8_t rx) {
if(_uart == 0) if(_uart == 0)
return; return;
uart_set_pins(_uart, tx, rx); uart_set_pins(_uart, tx, rx);
} }
void HardwareSerial::setDebugOutput(bool en) { void HardwareSerial::setDebugOutput(bool en) {
if(_uart == 0) if(_uart == 0)
return; return;
if(en) { if(en) {
if(_uart->txEnabled) if(_uart->txEnabled)
uart_set_debug(_uart->uart_nr); uart_set_debug(_uart->uart_nr);
else else
uart_set_debug(UART_NO); uart_set_debug(UART_NO);
} else { } else {
// disable debug for this interface // disable debug for this interface
if(uart_get_debug() == _uart_nr) { if(uart_get_debug() == _uart_nr) {
uart_set_debug(UART_NO); uart_set_debug(UART_NO);
}
} }
}
} }
bool ICACHE_RAM_ATTR HardwareSerial::isTxEnabled(void) { bool ICACHE_RAM_ATTR HardwareSerial::isTxEnabled(void) {
if(_uart == 0) return _uart != 0 && _uart->txEnabled;
return false;
return _uart->txEnabled;
} }
bool ICACHE_RAM_ATTR HardwareSerial::isRxEnabled(void) { bool ICACHE_RAM_ATTR HardwareSerial::isRxEnabled(void) {
if(_uart == 0) return _uart != 0 && _uart->rxEnabled;
return false;
return _uart->rxEnabled;
} }
int HardwareSerial::available(void) { int HardwareSerial::available(void) {
int result = 0; if(_uart == 0 || !_uart->rxEnabled)
return 0;
if (_uart != NULL && _uart->rxEnabled) { int result = static_cast<int>(uart_rx_available(_uart));
InterruptLock il; if (!result) {
result = static_cast<int>(_rx_buffer->available()); optimistic_yield(USD(_uart->uart_nr) / 128);
} }
return result;
if (!result) {
optimistic_yield(USD(_uart->uart_nr) / 128);
}
return result;
} }
int HardwareSerial::peek(void) { int HardwareSerial::peek(void) {
if(_uart == 0) return -1;
return -1;
if(_uart->rxEnabled) {
InterruptLock il;
return _rx_buffer->peek();
} else {
return -1;
}
} }
int HardwareSerial::read(void) { int HardwareSerial::read(void) {
if(_uart == 0) if(_uart == 0 || !_uart->rxEnabled)
return -1; return -1;
if(_uart->rxEnabled) {
InterruptLock il; return static_cast<int>(uart_read_char(_uart));
return _rx_buffer->read();
} else {
return -1;
}
} }
int HardwareSerial::availableForWrite(void) { int HardwareSerial::availableForWrite(void) {
if(_uart == 0) if(_uart == 0 || !_uart->txEnabled)
return 0; return 0;
if(_uart->txEnabled) {
InterruptLock il; return static_cast<int>(uart_tx_free(_uart));
return static_cast<int>(_tx_buffer->room());
} else {
return 0;
}
} }
void HardwareSerial::flush() { void HardwareSerial::flush() {
if(_uart == 0) if(_uart == 0 || !_uart->txEnabled)
return; return;
if(!_uart->txEnabled)
return;
const int uart_nr = _uart->uart_nr; uart_wait_tx_empty(_uart);
while(true) {
{
InterruptLock il;
if(_tx_buffer->available() == 0 &&
UART_GET_TX_FIFO_ROOM(uart_nr) >= UART_TX_FIFO_SIZE) {
break;
} else if(il.savedInterruptLevel() > 0) {
_tx_empty_irq();
continue;
}
}
yield();
}
} }
size_t HardwareSerial::write(uint8_t c) { size_t HardwareSerial::write(uint8_t c) {
if(_uart == 0 || !_uart->txEnabled) if(_uart == 0 || !_uart->txEnabled)
return 0; return 0;
bool tx_now = false; uart_write_char(_uart, c);
const int uart_nr = _uart->uart_nr; return 1;
while(true) {
{
InterruptLock il;
if(_tx_buffer->empty()) {
if(UART_GET_TX_FIFO_ROOM(uart_nr) > 0) {
tx_now = true;
} else {
_tx_buffer->write(c);
UART_ARM_TX_INTERRUPT(uart_nr);
}
break;
} else if(_tx_buffer->write(c)) {
break;
} else if(il.savedInterruptLevel() > 0) {
_tx_empty_irq();
continue;
}
}
yield();
}
if (tx_now) {
UART_TRANSMIT_CHAR(uart_nr, c);
}
return 1;
} }
HardwareSerial::operator bool() const { HardwareSerial::operator bool() const {
return _uart != 0; return _uart != 0;
}
void ICACHE_RAM_ATTR HardwareSerial::_rx_complete_irq(char c) {
_rx_buffer->write(c);
}
void ICACHE_RAM_ATTR HardwareSerial::_tx_empty_irq(void) {
const int uart_nr = _uart->uart_nr;
size_t queued = _tx_buffer->available();
if(!queued) {
UART_DISARM_TX_INTERRUPT(uart_nr);
return;
}
size_t room = UART_GET_TX_FIFO_ROOM(uart_nr);
int n = static_cast<int>((queued < room) ? queued : room);
while(n--) {
UART_TRANSMIT_CHAR(uart_nr, _tx_buffer->read());
}
} }

View File

@ -64,14 +64,13 @@
#define SERIAL_RX_ONLY 1 #define SERIAL_RX_ONLY 1
#define SERIAL_TX_ONLY 2 #define SERIAL_TX_ONLY 2
class cbuf;
struct uart_; struct uart_;
typedef struct uart_ uart_t; typedef struct uart_ uart_t;
class HardwareSerial: public Stream { class HardwareSerial: public Stream {
public: public:
HardwareSerial(int uart_nr); HardwareSerial(int uart_nr);
virtual ~HardwareSerial(){}
void begin(unsigned long baud) { void begin(unsigned long baud) {
begin(baud, SERIAL_8N1, SERIAL_FULL, 1); begin(baud, SERIAL_8N1, SERIAL_FULL, 1);
@ -127,15 +126,10 @@ class HardwareSerial: public Stream {
bool isRxEnabled(void); bool isRxEnabled(void);
protected: protected:
friend void uart_interrupt_handler(uart_t* uart);
void _rx_complete_irq(char c);
void _tx_empty_irq(void);
protected: protected:
int _uart_nr; int _uart_nr;
uart_t* _uart; uart_t* _uart;
cbuf* _tx_buffer;
cbuf* _rx_buffer;
}; };
extern HardwareSerial Serial; extern HardwareSerial Serial;