1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-21 10:26:06 +03:00

Inefficient Print::write(data,len) shows message if used (only in debug mode) (#4537)

* inefficient Print::write(data,len) shows message if used (only in debug mode)
* make HardwareSerial's write(data,len) efficient
* HardwareSerial: remove duplicate tests, move trivial code from .cpp to .h
This commit is contained in:
david gauchard 2018-03-22 01:23:58 +01:00 committed by GitHub
parent 2013af1b19
commit f8f205d54a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 82 additions and 108 deletions

View File

@ -55,10 +55,8 @@ void HardwareSerial::end()
uart_set_debug(UART_NO); uart_set_debug(UART_NO);
} }
if (_uart) { uart_uninit(_uart);
uart_uninit(_uart); _uart = NULL;
_uart = NULL;
}
} }
size_t HardwareSerial::setRxBufferSize(size_t size){ size_t HardwareSerial::setRxBufferSize(size_t size){
@ -70,30 +68,6 @@ size_t HardwareSerial::setRxBufferSize(size_t size){
return _rx_size; return _rx_size;
} }
void HardwareSerial::swap(uint8_t tx_pin)
{
if(!_uart) {
return;
}
uart_swap(_uart, tx_pin);
}
void HardwareSerial::set_tx(uint8_t tx_pin)
{
if(!_uart) {
return;
}
uart_set_tx(_uart, tx_pin);
}
void HardwareSerial::pins(uint8_t tx, uint8_t rx)
{
if(!_uart) {
return;
}
uart_set_pins(_uart, tx, rx);
}
void HardwareSerial::setDebugOutput(bool en) void HardwareSerial::setDebugOutput(bool en)
{ {
if(!_uart) { if(!_uart) {
@ -113,16 +87,6 @@ void HardwareSerial::setDebugOutput(bool en)
} }
} }
bool HardwareSerial::isTxEnabled(void)
{
return _uart && uart_tx_enabled(_uart);
}
bool HardwareSerial::isRxEnabled(void)
{
return _uart && uart_rx_enabled(_uart);
}
int HardwareSerial::available(void) int HardwareSerial::available(void)
{ {
int result = static_cast<int>(uart_rx_available(_uart)); int result = static_cast<int>(uart_rx_available(_uart));
@ -132,27 +96,6 @@ int HardwareSerial::available(void)
return result; return result;
} }
int HardwareSerial::peek(void)
{
// this may return -1, but that's okay
return uart_peek_char(_uart);
}
int HardwareSerial::read(void)
{
// this may return -1, but that's okay
return uart_read_char(_uart);
}
int HardwareSerial::availableForWrite(void)
{
if(!_uart || !uart_tx_enabled(_uart)) {
return 0;
}
return static_cast<int>(uart_tx_free(_uart));
}
void HardwareSerial::flush() void HardwareSerial::flush()
{ {
if(!_uart || !uart_tx_enabled(_uart)) { if(!_uart || !uart_tx_enabled(_uart)) {
@ -165,33 +108,9 @@ void HardwareSerial::flush()
delayMicroseconds(11000000 / uart_get_baudrate(_uart) + 1); delayMicroseconds(11000000 / uart_get_baudrate(_uart) + 1);
} }
size_t HardwareSerial::write(uint8_t c)
{
if(!_uart || !uart_tx_enabled(_uart)) {
return 0;
}
uart_write_char(_uart, c);
return 1;
}
int HardwareSerial::baudRate(void)
{
// Null pointer on _uart is checked by SDK
return uart_get_baudrate(_uart);
}
HardwareSerial::operator bool() const
{
return _uart != 0;
}
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL) #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
HardwareSerial Serial(UART0); HardwareSerial Serial(UART0);
#endif #endif
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL1) #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL1)
HardwareSerial Serial1(UART1); HardwareSerial Serial1(UART1);
#endif #endif

View File

@ -93,26 +93,50 @@ public:
{ {
swap(1); swap(1);
} }
void swap(uint8_t tx_pin); //toggle between use of GPIO13/GPIO15 or GPIO3/GPIO(1/2) as RX and TX void swap(uint8_t tx_pin) //toggle between use of GPIO13/GPIO15 or GPIO3/GPIO(1/2) as RX and TX
{
uart_swap(_uart, tx_pin);
}
/* /*
* Toggle between use of GPIO1 and GPIO2 as TX on UART 0. * Toggle between use of GPIO1 and GPIO2 as TX on UART 0.
* Note: UART 1 can't be used if GPIO2 is used with UART 0! * Note: UART 1 can't be used if GPIO2 is used with UART 0!
*/ */
void set_tx(uint8_t tx_pin); void set_tx(uint8_t tx_pin)
{
uart_set_tx(_uart, tx_pin);
}
/* /*
* UART 0 possible options are (1, 3), (2, 3) or (15, 13) * UART 0 possible options are (1, 3), (2, 3) or (15, 13)
* UART 1 allows only TX on 2 if UART 0 is not (2, 3) * UART 1 allows only TX on 2 if UART 0 is not (2, 3)
*/ */
void pins(uint8_t tx, uint8_t rx); void pins(uint8_t tx, uint8_t rx)
{
uart_set_pins(_uart, tx, rx);
}
int available(void) override; int available(void) override;
int peek(void) override;
int read(void) override; int peek(void) override
int availableForWrite(void); {
// this may return -1, but that's okay
return uart_peek_char(_uart);
}
int read(void) override
{
// this may return -1, but that's okay
return uart_read_char(_uart);
}
int availableForWrite(void)
{
return static_cast<int>(uart_tx_free(_uart));
}
void flush(void) override; void flush(void) override;
size_t write(uint8_t) override; size_t write(uint8_t c) override
{
return uart_write_char(_uart, c);
}
inline size_t write(unsigned long n) inline size_t write(unsigned long n)
{ {
return write((uint8_t) n); return write((uint8_t) n);
@ -129,13 +153,27 @@ public:
{ {
return write((uint8_t) n); return write((uint8_t) n);
} }
using Print::write; // pull in write(str) and write(buf, size) from Print size_t write(const uint8_t *buffer, size_t size)
operator bool() const; {
return uart_write(_uart, (const char*)buffer, size);
}
operator bool() const
{
return _uart != 0;
}
void setDebugOutput(bool); void setDebugOutput(bool);
bool isTxEnabled(void); bool isTxEnabled(void)
bool isRxEnabled(void); {
int baudRate(void); return uart_tx_enabled(_uart);
}
bool isRxEnabled(void)
{
return _uart && uart_rx_enabled(_uart);
}
int baudRate(void)
{
return uart_get_baudrate(_uart);
}
bool hasOverrun(void) bool hasOverrun(void)
{ {

View File

@ -33,6 +33,16 @@
/* default implementation: may be overridden */ /* default implementation: may be overridden */
size_t Print::write(const uint8_t *buffer, size_t size) { size_t Print::write(const uint8_t *buffer, size_t size) {
#ifdef DEBUG_ESP_CORE
static char not_the_best_way [] ICACHE_RODATA_ATTR STORE_ATTR = "Print::write(data,len) should be overridden for better efficiency\r\n";
static bool once = false;
if (!once) {
once = true;
os_printf_plus(not_the_best_way);
}
#endif
size_t n = 0; size_t n = 0;
while (size--) { while (size--) {
size_t ret = write(*buffer++); size_t ret = write(*buffer++);

View File

@ -103,7 +103,7 @@ inline size_t uart_rx_fifo_available(uart_t* uart) {
return (USS(uart->uart_nr) >> USRXC) & 0x7F; return (USS(uart->uart_nr) >> USRXC) & 0x7F;
} }
char overrun_str [] ICACHE_RODATA_ATTR STORE_ATTR = "uart input full!\r\n"; const char overrun_str [] ICACHE_RODATA_ATTR STORE_ATTR = "uart input full!\r\n";
// Copy all the rx fifo bytes that fit into the rx buffer // Copy all the rx fifo bytes that fit into the rx buffer
inline void uart_rx_copy_fifo_to_buffer(uart_t* uart) { inline void uart_rx_copy_fifo_to_buffer(uart_t* uart) {
@ -214,24 +214,31 @@ void uart_stop_isr(uart_t* uart)
ETS_UART_INTR_ATTACH(NULL, NULL); ETS_UART_INTR_ATTACH(NULL, NULL);
} }
static void uart_do_write_char(uart_t* uart, char c)
void uart_write_char(uart_t* uart, char c)
{ {
if(uart == NULL || !uart->tx_enabled) {
return;
}
while((USS(uart->uart_nr) >> USTXC) >= 0x7f); while((USS(uart->uart_nr) >> USTXC) >= 0x7f);
USF(uart->uart_nr) = c; USF(uart->uart_nr) = c;
} }
void uart_write(uart_t* uart, const char* buf, size_t size) size_t uart_write_char(uart_t* uart, char c)
{ {
if(uart == NULL || !uart->tx_enabled) { if(uart == NULL || !uart->tx_enabled) {
return; return 0;
} }
while(size--) { uart_do_write_char(uart, c);
uart_write_char(uart, *buf++); return 1;
}
size_t uart_write(uart_t* uart, const char* buf, size_t size)
{
if(uart == NULL || !uart->tx_enabled) {
return 0;
} }
size_t ret = size;
while (size--) {
uart_do_write_char(uart, *buf++);
}
return ret;
} }
size_t uart_tx_free(uart_t* uart) size_t uart_tx_free(uart_t* uart)

View File

@ -127,8 +127,8 @@ int uart_get_baudrate(uart_t* uart);
size_t uart_resize_rx_buffer(uart_t* uart, size_t new_size); size_t uart_resize_rx_buffer(uart_t* uart, size_t new_size);
void uart_write_char(uart_t* uart, char c); size_t uart_write_char(uart_t* uart, char c);
void uart_write(uart_t* uart, const char* buf, size_t size); size_t uart_write(uart_t* uart, const char* buf, size_t size);
int uart_read_char(uart_t* uart); int uart_read_char(uart_t* uart);
int uart_peek_char(uart_t* uart); int uart_peek_char(uart_t* uart);
size_t uart_rx_available(uart_t* uart); size_t uart_rx_available(uart_t* uart);