Skip to content

Commit 7bb1d8e

Browse files
author
Veijo Pesonen
authored
Initial UART HW flow control support (#79)
* Initial UART HW flow control support * fixup! Initial UART HW flow control support * Updates README.md to include information how to enable UART HW flow control * fixup! Initial UART HW flow control support * fixup! Updates README.md to include information how to enable UART HW flow control
1 parent 4c58514 commit 7bb1d8e

File tree

8 files changed

+184
-25
lines changed

8 files changed

+184
-25
lines changed

ESP8266/ESP8266.cpp

Lines changed: 93 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,29 @@
1515
*/
1616

1717
#include "ESP8266.h"
18-
#include "mbed_debug.h"
18+
#include "Callback.h"
19+
#include "mbed_error.h"
1920
#include "nsapi_types.h"
21+
#include "PinNames.h"
2022

2123
#include <cstring>
2224

2325
#define ESP8266_DEFAULT_BAUD_RATE 115200
2426
#define ESP8266_ALL_SOCKET_IDS -1
2527

26-
ESP8266::ESP8266(PinName tx, PinName rx, bool debug)
27-
: _serial(tx, rx, ESP8266_DEFAULT_BAUD_RATE),
28-
_parser(&_serial),
29-
_packets(0),
28+
ESP8266::ESP8266(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
29+
: _serial(tx, rx, ESP8266_DEFAULT_BAUD_RATE),
30+
_serial_rts(rts),
31+
_serial_cts(cts),
32+
_parser(&_serial),
33+
_packets(0),
3034
_packets_end(&_packets),
3135
_connect_error(0),
3236
_fail(false),
3337
_closed(false),
3438
_socket_open(),
35-
_connection_status(NSAPI_STATUS_DISCONNECTED)
39+
_connection_status(NSAPI_STATUS_DISCONNECTED),
40+
_heap_usage(0)
3641
{
3742
_serial.set_baud( ESP8266_DEFAULT_BAUD_RATE );
3843
_parser.debug_on(debug);
@@ -70,6 +75,46 @@ int ESP8266::get_firmware_version()
7075
}
7176
}
7277

78+
bool ESP8266::stop_uart_hw_flow_ctrl(void)
79+
{
80+
// Stop board's flow control
81+
_serial.set_flow_control(SerialBase::Disabled, _serial_rts, _serial_cts);
82+
83+
// Stop ESP8266's flow control
84+
bool done = _parser.send("AT+UART_CUR=%u,8,1,0,0", ESP8266_DEFAULT_BAUD_RATE)
85+
&& _parser.recv("OK\n");
86+
87+
return done;
88+
}
89+
90+
bool ESP8266::start_uart_hw_flow_ctrl(void)
91+
{
92+
bool done = true;
93+
94+
if (_serial_rts != NC && _serial_cts != NC) {
95+
// Start board's flow control
96+
_serial.set_flow_control(SerialBase::RTSCTS, _serial_rts, _serial_cts);
97+
98+
// Start ESP8266's flow control
99+
done = _parser.send("AT+UART_CUR=%u,8,1,0,3", ESP8266_DEFAULT_BAUD_RATE)
100+
&& _parser.recv("OK\n");
101+
102+
} else if (_serial_rts != NC) {
103+
_serial.set_flow_control(SerialBase::RTS, _serial_rts, NC);
104+
105+
done = _parser.send("AT+UART_CUR=%u,8,1,0,2", ESP8266_DEFAULT_BAUD_RATE)
106+
&& _parser.recv("OK\n");
107+
108+
} else if (_serial_cts != NC) {
109+
done = _parser.send("AT+UART_CUR=%u,8,1,0,1", ESP8266_DEFAULT_BAUD_RATE)
110+
&& _parser.recv("OK\n");
111+
112+
_serial.set_flow_control(SerialBase::CTS, NC, _serial_cts);
113+
}
114+
115+
return done;
116+
}
117+
73118
bool ESP8266::startup(int mode)
74119
{
75120
if (!(mode == WIFIMODE_STATION || mode == WIFIMODE_SOFTAP
@@ -361,7 +406,10 @@ nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
361406
if (_parser.send("AT+CIPSEND=%d,%lu", id, amount)
362407
&& _parser.recv(">")
363408
&& _parser.write((char*)data, (int)amount) >= 0) {
364-
while (_parser.process_oob()); // multiple sends in a row require this
409+
// No flow control, data overrun is possible
410+
if (_serial_rts == NC) {
411+
while (_parser.process_oob()); // Drain USART receive register
412+
}
365413
_smutex.unlock();
366414
return NSAPI_ERROR_OK;
367415
}
@@ -376,25 +424,37 @@ void ESP8266::_packet_handler()
376424
{
377425
int id;
378426
int amount;
427+
int pdu_len;
379428

380429
// parse out the packet
381430
if (!_parser.recv(",%d,%d:", &id, &amount)) {
382431
return;
383432
}
384433

385-
struct packet *packet = (struct packet*)malloc(
386-
sizeof(struct packet) + amount);
434+
pdu_len = sizeof(struct packet) + amount;
435+
436+
if ((_heap_usage + pdu_len) > MBED_CONF_ESP8266_SOCKET_BUFSIZE) {
437+
MBED_WARNING(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOBUFS), \
438+
"ESP8266::_packet_handler(): \"esp8266.socket-bufsize\"-limit exceeded, packet dropped");
439+
return;
440+
}
441+
442+
struct packet *packet = (struct packet*)malloc(pdu_len);
387443
if (!packet) {
388-
debug("ESP8266: could not allocate memory for RX data\n");
444+
MBED_WARNING(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \
445+
"ESP8266::_packet_handler(): Could not allocate memory for RX data");
389446
return;
390447
}
448+
_heap_usage += pdu_len;
391449

392450
packet->id = id;
393451
packet->len = amount;
452+
packet->alloc_len = amount;
394453
packet->next = 0;
395454

396455
if (_parser.read((char*)(packet + 1), amount) < amount) {
397456
free(packet);
457+
_heap_usage -= pdu_len;
398458
return;
399459
}
400460

@@ -403,17 +463,23 @@ void ESP8266::_packet_handler()
403463
_packets_end = &packet->next;
404464
}
405465

466+
void ESP8266::process_oob(uint32_t timeout, bool all) {
467+
setTimeout(timeout);
468+
// Poll for inbound packets
469+
while (_parser.process_oob() && all) {
470+
}
471+
setTimeout();
472+
}
473+
406474
int32_t ESP8266::recv_tcp(int id, void *data, uint32_t amount, uint32_t timeout)
407475
{
408476
_smutex.lock();
409-
setTimeout(timeout);
410477

411-
// Poll for inbound packets
412-
while (_parser.process_oob()) {
478+
// No flow control, drain the USART receive register ASAP to avoid data overrun
479+
if (_serial_rts == NC) {
480+
process_oob(timeout, true);
413481
}
414482

415-
setTimeout();
416-
417483
// check if any packets are ready for us
418484
for (struct packet **p = &_packets; *p; p = &(*p)->next) {
419485
if ((*p)->id == id) {
@@ -426,10 +492,13 @@ int32_t ESP8266::recv_tcp(int id, void *data, uint32_t amount, uint32_t timeout)
426492
_packets_end = p;
427493
}
428494
*p = (*p)->next;
495+
429496
_smutex.unlock();
430497

498+
uint32_t pdu_len = sizeof(struct packet) + q->alloc_len;
431499
uint32_t len = q->len;
432500
free(q);
501+
_heap_usage -= pdu_len;
433502
return len;
434503
} else { // return only partial packet
435504
memcpy(data, q+1, amount);
@@ -446,6 +515,11 @@ int32_t ESP8266::recv_tcp(int id, void *data, uint32_t amount, uint32_t timeout)
446515
_smutex.unlock();
447516
return 0;
448517
}
518+
519+
// Flow control, read from USART receive register only when no more data is buffered, and as little as possible
520+
if (_serial_rts != NC) {
521+
process_oob(timeout, false);
522+
}
449523
_smutex.unlock();
450524

451525
return NSAPI_ERROR_WOULD_BLOCK;
@@ -477,7 +551,9 @@ int32_t ESP8266::recv_udp(int id, void *data, uint32_t amount, uint32_t timeout)
477551
*p = (*p)->next;
478552
_smutex.unlock();
479553

554+
uint32_t pdu_len = sizeof(struct packet) + q->alloc_len;
480555
free(q);
556+
_heap_usage -= pdu_len;
481557
return len;
482558
}
483559
}
@@ -493,13 +569,14 @@ void ESP8266::_clear_socket_packets(int id)
493569
while (*p) {
494570
if ((*p)->id == id || id == ESP8266_ALL_SOCKET_IDS) {
495571
struct packet *q = *p;
572+
int pdu_len = sizeof(struct packet) + q->alloc_len;
496573

497574
if (_packets_end == &(*p)->next) {
498575
_packets_end = p; // Set last packet next field/_packets
499576
}
500577
*p = (*p)->next;
501-
502578
free(q);
579+
_heap_usage -= pdu_len;
503580
} else {
504581
// Point to last packet next field
505582
p = &(*p)->next;

ESP8266/ESP8266.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
class ESP8266
4242
{
4343
public:
44-
ESP8266(PinName tx, PinName rx, bool debug=false);
44+
ESP8266(PinName tx, PinName rx, bool debug=false, PinName rts=NC, PinName cts=NC);
4545

4646
/**
4747
* Check firmware version of ESP8266
@@ -265,20 +265,37 @@ class ESP8266
265265
*/
266266
nsapi_connection_status_t get_connection_status() const;
267267

268+
/**
269+
* Start board's and ESP8266's UART flow control
270+
*
271+
* @return true if started
272+
*/
273+
bool start_uart_hw_flow_ctrl();
274+
275+
/**
276+
* Stop board's and ESP8266's UART flow control
277+
*
278+
* @return true if started
279+
*/
280+
bool stop_uart_hw_flow_ctrl();
281+
268282
static const int8_t WIFIMODE_STATION = 1;
269283
static const int8_t WIFIMODE_SOFTAP = 2;
270284
static const int8_t WIFIMODE_STATION_SOFTAP = 3;
271285
static const int8_t SOCKET_COUNT = 5;
272286

273287
private:
274288
UARTSerial _serial;
289+
PinName _serial_rts;
290+
PinName _serial_cts;
275291
ATCmdParser _parser;
276292
Mutex _smutex; // Protect serial port access
277293

278294
struct packet {
279295
struct packet *next;
280296
int id;
281-
uint32_t len;
297+
uint32_t len; // Remaining length
298+
uint32_t alloc_len; // Original length
282299
// data follows
283300
} *_packets, **_packets_end;
284301
void _packet_handler();
@@ -292,6 +309,7 @@ class ESP8266
292309
void _connection_status_handler();
293310
void _oob_socket_close_error();
294311
void _clear_socket_packets(int id);
312+
void process_oob(uint32_t timeout, bool all);
295313

296314
char _ip_buffer[16];
297315
char _gateway_buffer[16];
@@ -304,6 +322,7 @@ class ESP8266
304322
int _socket_open[SOCKET_COUNT];
305323
nsapi_connection_status_t _connection_status;
306324
Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
325+
size_t _heap_usage;
307326
};
308327

309328
#endif

ESP8266Interface.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#define ESP8266_VERSION 2
4242

4343
ESP8266Interface::ESP8266Interface()
44-
: _esp(MBED_CONF_ESP8266_TX, MBED_CONF_ESP8266_RX, MBED_CONF_ESP8266_DEBUG),
44+
: _esp(MBED_CONF_ESP8266_TX, MBED_CONF_ESP8266_RX, MBED_CONF_ESP8266_DEBUG, MBED_CONF_ESP8266_RTS, MBED_CONF_ESP8266_CTS),
4545
_initialized(false),
4646
_started(false)
4747
{
@@ -57,8 +57,8 @@ ESP8266Interface::ESP8266Interface()
5757
}
5858

5959
// ESP8266Interface implementation
60-
ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug)
61-
: _esp(tx, rx, debug),
60+
ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
61+
: _esp(tx, rx, debug, rts, cts),
6262
_initialized(false),
6363
_started(false)
6464
{
@@ -267,9 +267,15 @@ bool ESP8266Interface::_disable_default_softap()
267267
nsapi_error_t ESP8266Interface::_init(void)
268268
{
269269
if (!_initialized) {
270+
if (!_esp.stop_uart_hw_flow_ctrl()) {
271+
return NSAPI_ERROR_DEVICE_ERROR;
272+
}
270273
if (!_esp.reset()) {
271274
return NSAPI_ERROR_DEVICE_ERROR;
272275
}
276+
if (!_esp.start_uart_hw_flow_ctrl()) {
277+
return NSAPI_ERROR_DEVICE_ERROR;
278+
}
273279
if (!_get_firmware_ok()) {
274280
return NSAPI_ERROR_DEVICE_ERROR;
275281
}

ESP8266Interface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
4040
* @param rx RX pin
4141
* @param debug Enable debugging
4242
*/
43-
ESP8266Interface(PinName tx, PinName rx, bool debug = false);
43+
ESP8266Interface(PinName tx, PinName rx, bool debug=false, PinName rts=NC, PinName cts=NC);
4444

4545
/** Start the interface
4646
*

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,45 @@ ESP8266 modules come in different shapes and formats, but the most important fac
1010

1111
- The ESP8266 WiFi module does not allow the TCP client to bind on a specific port.
1212
- Setting up a UDP server is not possible.
13-
- The serial port does not have hardware flow control enabled. The AT command set does not either have a way to limit the download rate. Therefore, downloading anything larger than the serial port input buffer is unreliable. An application should be able to read fast enough to stay ahead of the network. This affects mostly the TCP protocol where data would be lost with no notification. On UDP, this would lead to only packet losses which the higher layer protocol should recover from.
13+
- The serial port does not have hardware flow control enabled by default. The AT command set does not either have a way to limit the download rate. Therefore, downloading anything larger than the serial port input buffer is unreliable. An application should be able to read fast enough to stay ahead of the network. This affects mostly the TCP protocol where data would be lost with no notification. On UDP, this would lead to only packet losses which the higher layer protocol should recover from.
14+
15+
## UART HW flow control
16+
17+
UART HW flow control requires you to additionally wire the CTS and RTS flow control pins between your board and your ESP8266 module. Once this is done remember to add configuration option for flow control in your app config file. Here a [ST NUCLEO-F429ZI](https://os.mbed.com/platforms/ST-Nucleo-F429ZI/) board and [ESPBee XBee Module](https://www.cascologix.com/product/espbee/) are used as an example.
18+
19+
**NOTE** Not all modules expose ESP8266's RTS and CTS pins so beware when you are choosing one.
20+
21+
Once you have your HW set up add configuration like this in your app config - Arduino pins D1 and D0 assumed for TX and RX:
22+
23+
``` javascript
24+
"target_overrides": {
25+
"NUCLEO_F429ZI": {
26+
"esp8266.rts": "PG_12",
27+
"esp8266.cts": "PG_15"
28+
}
29+
```
30+
31+
### Example board pins
32+
1. TX - D1 (Arduino Uno Revision 3 connectivity headers)
33+
2. RX - D0 (Arduino Uno Revision 3 connectivity headers)
34+
3. RTS - PG_12 (STMicroelectronics Morpho extension pin headers)
35+
4. CTS - PG_15 (STMicroelectronics Morpho extension pin headers)
36+
37+
### Example ESP8266 pins
38+
1. TX - D1 (Arduino Wireless Protoshield headers)/ TX (ESPBee XBee headers)
39+
2. RX - D0 (Arduino Wireless Protoshield headers)/ RX (ESPBee XBee headers)
40+
3. RTS - RTS (ESPBee XBee headers)
41+
4. CTS - CTS (ESPBee XBee headers)
42+
43+
### Connections
44+
With these pictures only consider the green and yellow wires which are connected to ESP8266. Pink wire is for reset and the rest for firmware update. TX and RX go through Arduino pins D1 and D0.
45+
46+
**NOTE** GPIO15(ESPBee RTS) needs to be pulled down during startup to boot from flash, instead of firmware update or boot from SD card. Once the software is running the same pin is used as the RTS pin.
47+
48+
1. Board TX - ESP8266 RX
49+
2. Board RX - ESP8266 TX
50+
3. Board RTS(grey) - ESP8266 CTS(yellow)
51+
4. Board CTS(white) - ESP8266 RTS(green)
52+
53+
![RTS,CTS](nucleo_esp8266_hw_fc1.jpg)
54+
![RTS,CTS](nucleo_esp8266_hw_fc2.jpg)

0 commit comments

Comments
 (0)