Skip to content

Commit f8f205d

Browse files
authored
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
1 parent 2013af1 commit f8f205d

File tree

5 files changed

+81
-107
lines changed

5 files changed

+81
-107
lines changed

cores/esp8266/HardwareSerial.cpp

+2-83
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,8 @@ void HardwareSerial::end()
5555
uart_set_debug(UART_NO);
5656
}
5757

58-
if (_uart) {
59-
uart_uninit(_uart);
60-
_uart = NULL;
61-
}
58+
uart_uninit(_uart);
59+
_uart = NULL;
6260
}
6361

6462
size_t HardwareSerial::setRxBufferSize(size_t size){
@@ -70,30 +68,6 @@ size_t HardwareSerial::setRxBufferSize(size_t size){
7068
return _rx_size;
7169
}
7270

73-
void HardwareSerial::swap(uint8_t tx_pin)
74-
{
75-
if(!_uart) {
76-
return;
77-
}
78-
uart_swap(_uart, tx_pin);
79-
}
80-
81-
void HardwareSerial::set_tx(uint8_t tx_pin)
82-
{
83-
if(!_uart) {
84-
return;
85-
}
86-
uart_set_tx(_uart, tx_pin);
87-
}
88-
89-
void HardwareSerial::pins(uint8_t tx, uint8_t rx)
90-
{
91-
if(!_uart) {
92-
return;
93-
}
94-
uart_set_pins(_uart, tx, rx);
95-
}
96-
9771
void HardwareSerial::setDebugOutput(bool en)
9872
{
9973
if(!_uart) {
@@ -113,16 +87,6 @@ void HardwareSerial::setDebugOutput(bool en)
11387
}
11488
}
11589

116-
bool HardwareSerial::isTxEnabled(void)
117-
{
118-
return _uart && uart_tx_enabled(_uart);
119-
}
120-
121-
bool HardwareSerial::isRxEnabled(void)
122-
{
123-
return _uart && uart_rx_enabled(_uart);
124-
}
125-
12690
int HardwareSerial::available(void)
12791
{
12892
int result = static_cast<int>(uart_rx_available(_uart));
@@ -132,27 +96,6 @@ int HardwareSerial::available(void)
13296
return result;
13397
}
13498

135-
int HardwareSerial::peek(void)
136-
{
137-
// this may return -1, but that's okay
138-
return uart_peek_char(_uart);
139-
}
140-
141-
int HardwareSerial::read(void)
142-
{
143-
// this may return -1, but that's okay
144-
return uart_read_char(_uart);
145-
}
146-
147-
int HardwareSerial::availableForWrite(void)
148-
{
149-
if(!_uart || !uart_tx_enabled(_uart)) {
150-
return 0;
151-
}
152-
153-
return static_cast<int>(uart_tx_free(_uart));
154-
}
155-
15699
void HardwareSerial::flush()
157100
{
158101
if(!_uart || !uart_tx_enabled(_uart)) {
@@ -165,33 +108,9 @@ void HardwareSerial::flush()
165108
delayMicroseconds(11000000 / uart_get_baudrate(_uart) + 1);
166109
}
167110

168-
size_t HardwareSerial::write(uint8_t c)
169-
{
170-
if(!_uart || !uart_tx_enabled(_uart)) {
171-
return 0;
172-
}
173-
174-
uart_write_char(_uart, c);
175-
return 1;
176-
}
177-
178-
int HardwareSerial::baudRate(void)
179-
{
180-
// Null pointer on _uart is checked by SDK
181-
return uart_get_baudrate(_uart);
182-
}
183-
184-
185-
HardwareSerial::operator bool() const
186-
{
187-
return _uart != 0;
188-
}
189-
190-
191111
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
192112
HardwareSerial Serial(UART0);
193113
#endif
194114
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL1)
195115
HardwareSerial Serial1(UART1);
196116
#endif
197-

cores/esp8266/HardwareSerial.h

+51-13
Original file line numberDiff line numberDiff line change
@@ -93,26 +93,50 @@ class HardwareSerial: public Stream
9393
{
9494
swap(1);
9595
}
96-
void swap(uint8_t tx_pin); //toggle between use of GPIO13/GPIO15 or GPIO3/GPIO(1/2) as RX and TX
96+
void swap(uint8_t tx_pin) //toggle between use of GPIO13/GPIO15 or GPIO3/GPIO(1/2) as RX and TX
97+
{
98+
uart_swap(_uart, tx_pin);
99+
}
97100

98101
/*
99102
* Toggle between use of GPIO1 and GPIO2 as TX on UART 0.
100103
* Note: UART 1 can't be used if GPIO2 is used with UART 0!
101104
*/
102-
void set_tx(uint8_t tx_pin);
105+
void set_tx(uint8_t tx_pin)
106+
{
107+
uart_set_tx(_uart, tx_pin);
108+
}
103109

104110
/*
105111
* UART 0 possible options are (1, 3), (2, 3) or (15, 13)
106112
* UART 1 allows only TX on 2 if UART 0 is not (2, 3)
107113
*/
108-
void pins(uint8_t tx, uint8_t rx);
114+
void pins(uint8_t tx, uint8_t rx)
115+
{
116+
uart_set_pins(_uart, tx, rx);
117+
}
109118

110119
int available(void) override;
111-
int peek(void) override;
112-
int read(void) override;
113-
int availableForWrite(void);
120+
121+
int peek(void) override
122+
{
123+
// this may return -1, but that's okay
124+
return uart_peek_char(_uart);
125+
}
126+
int read(void) override
127+
{
128+
// this may return -1, but that's okay
129+
return uart_read_char(_uart);
130+
}
131+
int availableForWrite(void)
132+
{
133+
return static_cast<int>(uart_tx_free(_uart));
134+
}
114135
void flush(void) override;
115-
size_t write(uint8_t) override;
136+
size_t write(uint8_t c) override
137+
{
138+
return uart_write_char(_uart, c);
139+
}
116140
inline size_t write(unsigned long n)
117141
{
118142
return write((uint8_t) n);
@@ -129,13 +153,27 @@ class HardwareSerial: public Stream
129153
{
130154
return write((uint8_t) n);
131155
}
132-
using Print::write; // pull in write(str) and write(buf, size) from Print
133-
operator bool() const;
134-
156+
size_t write(const uint8_t *buffer, size_t size)
157+
{
158+
return uart_write(_uart, (const char*)buffer, size);
159+
}
160+
operator bool() const
161+
{
162+
return _uart != 0;
163+
}
135164
void setDebugOutput(bool);
136-
bool isTxEnabled(void);
137-
bool isRxEnabled(void);
138-
int baudRate(void);
165+
bool isTxEnabled(void)
166+
{
167+
return uart_tx_enabled(_uart);
168+
}
169+
bool isRxEnabled(void)
170+
{
171+
return _uart && uart_rx_enabled(_uart);
172+
}
173+
int baudRate(void)
174+
{
175+
return uart_get_baudrate(_uart);
176+
}
139177

140178
bool hasOverrun(void)
141179
{

cores/esp8266/Print.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@
3333

3434
/* default implementation: may be overridden */
3535
size_t Print::write(const uint8_t *buffer, size_t size) {
36+
37+
#ifdef DEBUG_ESP_CORE
38+
static char not_the_best_way [] ICACHE_RODATA_ATTR STORE_ATTR = "Print::write(data,len) should be overridden for better efficiency\r\n";
39+
static bool once = false;
40+
if (!once) {
41+
once = true;
42+
os_printf_plus(not_the_best_way);
43+
}
44+
#endif
45+
3646
size_t n = 0;
3747
while (size--) {
3848
size_t ret = write(*buffer++);

cores/esp8266/uart.c

+16-9
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ inline size_t uart_rx_fifo_available(uart_t* uart) {
103103
return (USS(uart->uart_nr) >> USRXC) & 0x7F;
104104
}
105105

106-
char overrun_str [] ICACHE_RODATA_ATTR STORE_ATTR = "uart input full!\r\n";
106+
const char overrun_str [] ICACHE_RODATA_ATTR STORE_ATTR = "uart input full!\r\n";
107107

108108
// Copy all the rx fifo bytes that fit into the rx buffer
109109
inline void uart_rx_copy_fifo_to_buffer(uart_t* uart) {
@@ -214,24 +214,31 @@ void uart_stop_isr(uart_t* uart)
214214
ETS_UART_INTR_ATTACH(NULL, NULL);
215215
}
216216

217+
static void uart_do_write_char(uart_t* uart, char c)
218+
{
219+
while((USS(uart->uart_nr) >> USTXC) >= 0x7f);
220+
USF(uart->uart_nr) = c;
221+
}
217222

218-
void uart_write_char(uart_t* uart, char c)
223+
size_t uart_write_char(uart_t* uart, char c)
219224
{
220225
if(uart == NULL || !uart->tx_enabled) {
221-
return;
226+
return 0;
222227
}
223-
while((USS(uart->uart_nr) >> USTXC) >= 0x7f);
224-
USF(uart->uart_nr) = c;
228+
uart_do_write_char(uart, c);
229+
return 1;
225230
}
226231

227-
void uart_write(uart_t* uart, const char* buf, size_t size)
232+
size_t uart_write(uart_t* uart, const char* buf, size_t size)
228233
{
229234
if(uart == NULL || !uart->tx_enabled) {
230-
return;
235+
return 0;
231236
}
232-
while(size--) {
233-
uart_write_char(uart, *buf++);
237+
size_t ret = size;
238+
while (size--) {
239+
uart_do_write_char(uart, *buf++);
234240
}
241+
return ret;
235242
}
236243

237244
size_t uart_tx_free(uart_t* uart)

cores/esp8266/uart.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ int uart_get_baudrate(uart_t* uart);
127127

128128
size_t uart_resize_rx_buffer(uart_t* uart, size_t new_size);
129129

130-
void uart_write_char(uart_t* uart, char c);
131-
void uart_write(uart_t* uart, const char* buf, size_t size);
130+
size_t uart_write_char(uart_t* uart, char c);
131+
size_t uart_write(uart_t* uart, const char* buf, size_t size);
132132
int uart_read_char(uart_t* uart);
133133
int uart_peek_char(uart_t* uart);
134134
size_t uart_rx_available(uart_t* uart);

0 commit comments

Comments
 (0)