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:
parent
aeb9597ccf
commit
e255f25cfd
@ -82,136 +82,50 @@ static const int UART_NO = -1;
|
||||
HardwareSerial Serial(UART0);
|
||||
HardwareSerial Serial1(UART1);
|
||||
|
||||
// ####################################################################################################
|
||||
// ####################################################################################################
|
||||
// ####################################################################################################
|
||||
|
||||
void uart_interrupt_handler(uart_t* uart);
|
||||
void uart_wait_for_tx_fifo(uart_t* uart, size_t size_needed);
|
||||
|
||||
size_t uart_get_tx_fifo_room(uart_t* uart);
|
||||
void uart_wait_for_transmit(uart_t* uart);
|
||||
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_wait_for_tx_fifo(uart_t* uart, size_t size_needed) {
|
||||
if(uart == 0)
|
||||
void uart_write_char(uart_t* uart, char c) {
|
||||
if(uart == 0 || !uart->txEnabled)
|
||||
return;
|
||||
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;
|
||||
}
|
||||
}
|
||||
while((USS(uart->uart_nr) >> USTXC) >= 0x7f);
|
||||
USF(uart->uart_nr) = c;
|
||||
}
|
||||
|
||||
size_t uart_get_tx_fifo_room(uart_t* uart) {
|
||||
if(uart == 0)
|
||||
void uart_write(uart_t* uart, const char* buf, size_t size) {
|
||||
if(uart == 0 || !uart->txEnabled)
|
||||
return;
|
||||
while(size--)
|
||||
uart_write_char(uart, *buf++);
|
||||
}
|
||||
|
||||
uint8_t uart_read_char(uart_t* uart){
|
||||
if(uart == 0 || !uart->rxEnabled)
|
||||
return 0;
|
||||
if(uart->txEnabled) {
|
||||
return UART_GET_TX_FIFO_ROOM(uart->uart_nr);
|
||||
return USF(uart->uart_nr) & 0xff;
|
||||
}
|
||||
|
||||
uint8_t uart_rx_available(uart_t* uart){
|
||||
if(uart == 0 || !uart->rxEnabled)
|
||||
return 0;
|
||||
return (USS(uart->uart_nr) >> USRXC) & 0xff;
|
||||
}
|
||||
|
||||
void uart_wait_for_transmit(uart_t* uart) {
|
||||
if(uart == 0)
|
||||
uint8_t uart_tx_free(uart_t* uart){
|
||||
if(uart == 0 || !uart->txEnabled)
|
||||
return 0;
|
||||
return UART_TX_FIFO_SIZE - ((USS(uart->uart_nr) >> USTXC) & 0xff);
|
||||
}
|
||||
|
||||
void uart_wait_tx_empty(uart_t* uart){
|
||||
if(uart == 0 || !uart->txEnabled)
|
||||
return;
|
||||
if(uart->txEnabled) {
|
||||
uart_wait_for_tx_fifo(uart, UART_TX_FIFO_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
void uart_transmit_char(uart_t* uart, char c) {
|
||||
if(uart == 0)
|
||||
return;
|
||||
if(uart->txEnabled) {
|
||||
UART_TRANSMIT_CHAR(uart->uart_nr, c);
|
||||
}
|
||||
}
|
||||
|
||||
void uart_transmit(uart_t* uart, const char* buf, size_t size) {
|
||||
if(uart == 0)
|
||||
return;
|
||||
if(uart->txEnabled) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
while(((USS(uart->uart_nr) >> USTXC) & 0xff) > 0) delay(0);
|
||||
}
|
||||
|
||||
void uart_flush(uart_t* uart) {
|
||||
uint32_t tmp = 0x00000000;
|
||||
|
||||
if(uart == 0)
|
||||
return;
|
||||
|
||||
uint32_t tmp = 0x00000000;
|
||||
if(uart->rxEnabled) {
|
||||
tmp |= (1 << UCRXRST);
|
||||
}
|
||||
@ -224,45 +138,6 @@ void uart_flush(uart_t* uart) {
|
||||
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) {
|
||||
if(uart == 0)
|
||||
return;
|
||||
@ -276,7 +151,11 @@ int uart_get_baudrate(uart_t* uart) {
|
||||
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_init(int uart_nr, int baudrate, byte config, byte mode, uint8_t use_tx) {
|
||||
|
||||
uart_t* uart = (uart_t*) os_malloc(sizeof(uart_t));
|
||||
|
||||
@ -317,33 +196,18 @@ uart_t* uart_start_init(int uart_nr, int baudrate, byte config, byte mode, uint8
|
||||
os_free(uart);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uart_set_baudrate(uart, baudrate);
|
||||
USC0(uart->uart_nr) = config;
|
||||
uart_flush(uart);
|
||||
USC1(uart->uart_nr) = 0;
|
||||
|
||||
return uart;
|
||||
}
|
||||
|
||||
void uart_finish_init(uart_t* uart) {
|
||||
uint32_t conf1 = 0x00000000;
|
||||
|
||||
uart_flush(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) {
|
||||
if(uart == 0)
|
||||
return;
|
||||
uart_interrupt_disable(uart);
|
||||
|
||||
switch(uart->rxPin) {
|
||||
case 3:
|
||||
@ -458,54 +322,15 @@ void uart_set_pins(uart_t* uart, uint8_t tx, uint8_t rx) {
|
||||
// ####################################################################################################
|
||||
// ####################################################################################################
|
||||
|
||||
void uart_ignore_char(char c) {
|
||||
}
|
||||
void uart_ignore_char(char c) {}
|
||||
|
||||
void uart0_write_char(char c) {
|
||||
if(&Serial != NULL && Serial.isTxEnabled()) {
|
||||
if(Serial.availableForWrite() > 0) {
|
||||
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';
|
||||
}
|
||||
while(((USS(0) >> USTXC) & 0xff) >= 0x7F) delay(0);
|
||||
USF(0) = c;
|
||||
}
|
||||
|
||||
void uart1_write_char(char c) {
|
||||
if(&Serial1 != NULL && Serial1.isTxEnabled()) {
|
||||
if(Serial1.availableForWrite() > 0) {
|
||||
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';
|
||||
}
|
||||
while(((USS(1) >> USTXC) & 0xff) >= 0x7F) delay(0);
|
||||
USF(1) = c;
|
||||
}
|
||||
|
||||
@ -534,62 +359,31 @@ int uart_get_debug() {
|
||||
return s_uart_debug_nr;
|
||||
}
|
||||
|
||||
|
||||
// ####################################################################################################
|
||||
// ####################################################################################################
|
||||
// ####################################################################################################
|
||||
|
||||
HardwareSerial::HardwareSerial(int uart_nr) :
|
||||
_uart_nr(uart_nr), _uart(0), _tx_buffer(0), _rx_buffer(0) {
|
||||
}
|
||||
HardwareSerial::HardwareSerial(int uart_nr)
|
||||
: _uart_nr(uart_nr)
|
||||
, _uart(0)
|
||||
{}
|
||||
|
||||
void HardwareSerial::begin(unsigned long baud, byte config, byte mode, uint8_t use_tx) {
|
||||
InterruptLock il;
|
||||
|
||||
// disable debug for this interface
|
||||
if(uart_get_debug() == _uart_nr) {
|
||||
if(uart_get_debug() == _uart_nr)
|
||||
uart_set_debug(UART_NO);
|
||||
}
|
||||
|
||||
if (_uart) {
|
||||
if (_uart)
|
||||
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);
|
||||
_uart = uart_init(_uart_nr, baud, config, mode, use_tx);
|
||||
}
|
||||
|
||||
void HardwareSerial::end() {
|
||||
InterruptLock il;
|
||||
|
||||
if(uart_get_debug() == _uart_nr) {
|
||||
if(uart_get_debug() == _uart_nr)
|
||||
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) {
|
||||
@ -627,138 +421,57 @@ void HardwareSerial::setDebugOutput(bool en) {
|
||||
}
|
||||
|
||||
bool ICACHE_RAM_ATTR HardwareSerial::isTxEnabled(void) {
|
||||
if(_uart == 0)
|
||||
return false;
|
||||
return _uart->txEnabled;
|
||||
return _uart != 0 && _uart->txEnabled;
|
||||
}
|
||||
|
||||
bool ICACHE_RAM_ATTR HardwareSerial::isRxEnabled(void) {
|
||||
if(_uart == 0)
|
||||
return false;
|
||||
return _uart->rxEnabled;
|
||||
return _uart != 0 && _uart->rxEnabled;
|
||||
}
|
||||
|
||||
int HardwareSerial::available(void) {
|
||||
int result = 0;
|
||||
|
||||
if (_uart != NULL && _uart->rxEnabled) {
|
||||
InterruptLock il;
|
||||
result = static_cast<int>(_rx_buffer->available());
|
||||
}
|
||||
if(_uart == 0 || !_uart->rxEnabled)
|
||||
return 0;
|
||||
|
||||
int result = static_cast<int>(uart_rx_available(_uart));
|
||||
if (!result) {
|
||||
optimistic_yield(USD(_uart->uart_nr) / 128);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int HardwareSerial::peek(void) {
|
||||
if(_uart == 0)
|
||||
return -1;
|
||||
if(_uart->rxEnabled) {
|
||||
InterruptLock il;
|
||||
return _rx_buffer->peek();
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int HardwareSerial::read(void) {
|
||||
if(_uart == 0)
|
||||
if(_uart == 0 || !_uart->rxEnabled)
|
||||
return -1;
|
||||
if(_uart->rxEnabled) {
|
||||
InterruptLock il;
|
||||
return _rx_buffer->read();
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return static_cast<int>(uart_read_char(_uart));
|
||||
}
|
||||
|
||||
int HardwareSerial::availableForWrite(void) {
|
||||
if(_uart == 0)
|
||||
if(_uart == 0 || !_uart->txEnabled)
|
||||
return 0;
|
||||
if(_uart->txEnabled) {
|
||||
InterruptLock il;
|
||||
return static_cast<int>(_tx_buffer->room());
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return static_cast<int>(uart_tx_free(_uart));
|
||||
}
|
||||
|
||||
void HardwareSerial::flush() {
|
||||
if(_uart == 0)
|
||||
return;
|
||||
if(!_uart->txEnabled)
|
||||
if(_uart == 0 || !_uart->txEnabled)
|
||||
return;
|
||||
|
||||
const int uart_nr = _uart->uart_nr;
|
||||
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();
|
||||
}
|
||||
uart_wait_tx_empty(_uart);
|
||||
}
|
||||
|
||||
size_t HardwareSerial::write(uint8_t c) {
|
||||
if(_uart == 0 || !_uart->txEnabled)
|
||||
return 0;
|
||||
|
||||
bool tx_now = false;
|
||||
const int uart_nr = _uart->uart_nr;
|
||||
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);
|
||||
}
|
||||
uart_write_char(_uart, c);
|
||||
return 1;
|
||||
}
|
||||
|
||||
HardwareSerial::operator bool() const {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
@ -64,14 +64,13 @@
|
||||
#define SERIAL_RX_ONLY 1
|
||||
#define SERIAL_TX_ONLY 2
|
||||
|
||||
class cbuf;
|
||||
|
||||
struct uart_;
|
||||
typedef struct uart_ uart_t;
|
||||
|
||||
class HardwareSerial: public Stream {
|
||||
public:
|
||||
HardwareSerial(int uart_nr);
|
||||
virtual ~HardwareSerial(){}
|
||||
|
||||
void begin(unsigned long baud) {
|
||||
begin(baud, SERIAL_8N1, SERIAL_FULL, 1);
|
||||
@ -127,15 +126,10 @@ class HardwareSerial: public Stream {
|
||||
bool isRxEnabled(void);
|
||||
|
||||
protected:
|
||||
friend void uart_interrupt_handler(uart_t* uart);
|
||||
void _rx_complete_irq(char c);
|
||||
void _tx_empty_irq(void);
|
||||
|
||||
protected:
|
||||
int _uart_nr;
|
||||
uart_t* _uart;
|
||||
cbuf* _tx_buffer;
|
||||
cbuf* _rx_buffer;
|
||||
};
|
||||
|
||||
extern HardwareSerial Serial;
|
||||
|
Loading…
x
Reference in New Issue
Block a user