Skip to content

Commit 62bd7c1

Browse files
author
Veijo Pesonen
committed
fixup! Initial UART HW flow control support
1 parent d101284 commit 62bd7c1

File tree

4 files changed

+26
-62
lines changed

4 files changed

+26
-62
lines changed

ESP8266/ESP8266.cpp

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
#include "ESP8266.h"
1818
#include "Callback.h"
19-
#include "mbed_debug.h"
19+
#include "mbed_error.h"
2020
#include "nsapi_types.h"
2121
#include "PinNames.h"
2222

@@ -25,11 +25,6 @@
2525
#define ESP8266_DEFAULT_BAUD_RATE 115200
2626
#define ESP8266_ALL_SOCKET_IDS -1
2727

28-
// Big enough, target specific
29-
#ifndef MBED_CONF_ESP8266_SOCKET_BUFSIZE
30-
#define MBED_CONF_ESP8266_SOCKET_BUFSIZE 102400
31-
#endif
32-
3328
ESP8266::ESP8266(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
3429
: _serial(tx, rx, ESP8266_DEFAULT_BAUD_RATE),
3530
_serial_rts(rts),
@@ -83,33 +78,24 @@ int ESP8266::get_firmware_version()
8378
bool ESP8266::start_uart_hw_flow_ctrl(void)
8479
{
8580
bool done = true;
86-
static const int DATABITS_8 = 8; // 8-bit data
87-
static const int STOPBITS_1 = 1; // 1-bit stop bit
88-
static const int PARITY_NONE = 0; // None
89-
static const int FC_RTS = 1; // Flow Control RTS
90-
static const int FC_CTS = 2; // Flow Control CTS
91-
static const int FC_RTSCTS = 3; // Flow Control RTS&CTS
9281

9382
if (_serial_rts != NC && _serial_cts != NC) {
9483
// Start board's flow control
9584
_serial.set_flow_control(SerialBase::RTSCTS, _serial_rts, _serial_cts);
9685

9786
// Start ESP8266's flow control
98-
done = _parser.send("AT+UART_CUR=%u,%u,%u,%u,%u", ESP8266_DEFAULT_BAUD_RATE, DATABITS_8, STOPBITS_1,
99-
PARITY_NONE, FC_RTSCTS)
100-
&& _parser.recv("OK\n");
87+
done = _parser.send("AT+UART_CUR=%u,8,1,0,3", ESP8266_DEFAULT_BAUD_RATE)
88+
&& _parser.recv("OK\n");
89+
10190
} else if (_serial_rts != NC) {
10291
_serial.set_flow_control(SerialBase::RTS, _serial_rts, NC);
10392

104-
// Start ESP8266's flow control
105-
done = _parser.send("AT+UART_CUR=%u,%u,%u,%u,%u", ESP8266_DEFAULT_BAUD_RATE, DATABITS_8, STOPBITS_1,
106-
PARITY_NONE, FC_CTS)
107-
&& _parser.recv("OK\n");
93+
done = _parser.send("AT+UART_CUR=%u,8,1,0,2", ESP8266_DEFAULT_BAUD_RATE)
94+
&& _parser.recv("OK\n");
95+
10896
} else if (_serial_cts != NC) {
109-
// Start ESP8266's flow control
110-
done = _parser.send("AT+UART_CUR=%u,%u,%u,%u,%u", ESP8266_DEFAULT_BAUD_RATE, DATABITS_8, STOPBITS_1,
111-
PARITY_NONE, FC_RTS)
112-
&& _parser.recv("OK\n");
97+
done = _parser.send("AT+UART_CUR=%u,8,1,0,1", ESP8266_DEFAULT_BAUD_RATE)
98+
&& _parser.recv("OK\n");
11399

114100
_serial.set_flow_control(SerialBase::CTS, NC, _serial_cts);
115101
}
@@ -435,16 +421,16 @@ void ESP8266::_packet_handler()
435421

436422
pdu_len = sizeof(struct packet) + amount;
437423

438-
if ((_heap_usage + pdu_len) > _MAX_HEAP_USAGE) {
439-
debug("ESP8266: socket buffer max memory limit exceeded, either increase socket buffer size from mbed_lib.json or enable UART HW flow control\n");
440-
MBED_ASSERT(false);
424+
if ((_heap_usage + pdu_len) > MBED_CONF_ESP8266_SOCKET_BUFSIZE) {
425+
MBED_WARNING(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOBUFS), \
426+
"ESP8266::_packet_handler(): \"esp8266.socket-bufsize\"-limit exceeded, packet dropped");
441427
return;
442428
}
443429

444430
struct packet *packet = (struct packet*)malloc(pdu_len);
445431
if (!packet) {
446-
debug("ESP8266: could not allocate memory for RX data\n");
447-
MBED_ASSERT(packet);
432+
MBED_WARNING(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \
433+
"ESP8266::_packet_handler(): Could not allocate memory for RX data");
448434
return;
449435
}
450436
_heap_usage += pdu_len;
@@ -457,7 +443,6 @@ void ESP8266::_packet_handler()
457443
if (_parser.read((char*)(packet + 1), amount) < amount) {
458444
free(packet);
459445
_heap_usage -= pdu_len;
460-
MBED_ASSERT(_heap_usage >= 0);
461446
return;
462447
}
463448

@@ -498,11 +483,10 @@ int32_t ESP8266::recv_tcp(int id, void *data, uint32_t amount, uint32_t timeout)
498483

499484
_smutex.unlock();
500485

501-
uint32_t alloc_len = q->alloc_len;
486+
uint32_t pdu_len = sizeof(struct packet) + q->alloc_len;
502487
uint32_t len = q->len;
503488
free(q);
504-
_heap_usage -= sizeof(struct packet) + alloc_len;
505-
MBED_ASSERT(_heap_usage >= 0);
489+
_heap_usage -= pdu_len;
506490
return len;
507491
} else { // return only partial packet
508492
memcpy(data, q+1, amount);
@@ -555,11 +539,9 @@ int32_t ESP8266::recv_udp(int id, void *data, uint32_t amount, uint32_t timeout)
555539
*p = (*p)->next;
556540
_smutex.unlock();
557541

558-
uint32_t alloc_len = q->alloc_len;
542+
uint32_t pdu_len = sizeof(struct packet) + q->alloc_len;
559543
free(q);
560-
_heap_usage -= sizeof(struct packet) + alloc_len;
561-
MBED_ASSERT(_heap_usage >= 0);
562-
544+
_heap_usage -= pdu_len;
563545
return len;
564546
}
565547
}
@@ -575,23 +557,19 @@ void ESP8266::_clear_socket_packets(int id)
575557
while (*p) {
576558
if ((*p)->id == id || id == ESP8266_ALL_SOCKET_IDS) {
577559
struct packet *q = *p;
578-
int alloc_len = q->alloc_len;
560+
int pdu_len = sizeof(struct packet) + q->alloc_len;
579561

580562
if (_packets_end == &(*p)->next) {
581563
_packets_end = p; // Set last packet next field/_packets
582564
}
583565
*p = (*p)->next;
584-
_heap_usage -= sizeof(struct packet) + alloc_len;
585-
586566
free(q);
567+
_heap_usage -= pdu_len;
587568
} else {
588569
// Point to last packet next field
589570
p = &(*p)->next;
590571
}
591572
}
592-
if (id == ESP8266_ALL_SOCKET_IDS) {
593-
MBED_ASSERT(_heap_usage == 0);
594-
}
595573
}
596574

597575
bool ESP8266::close(int id)

ESP8266/ESP8266.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "ATCmdParser.h"
2121
#include "nsapi_types.h"
2222
#include "rtos.h"
23-
#include "UARTSerial.h"
2423

2524
// Various timeouts for different ESP8266 operations
2625
#ifndef ESP8266_CONNECT_TIMEOUT
@@ -316,8 +315,7 @@ class ESP8266
316315
int _socket_open[SOCKET_COUNT];
317316
nsapi_connection_status_t _connection_status;
318317
Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
319-
static const int32_t _MAX_HEAP_USAGE = MBED_CONF_ESP8266_SOCKET_BUFSIZE;
320-
int32_t _heap_usage;
318+
size_t _heap_usage;
321319
};
322320

323321
#endif

ESP8266Interface.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,6 @@
3737
#endif
3838
#endif
3939

40-
#ifndef MBED_CONF_ESP8266_DEBUG
41-
#define MBED_CONF_ESP8266_DEBUG false
42-
#endif
43-
44-
#ifndef MBED_CONF_ESP8266_RTS
45-
#define MBED_CONF_ESP8266_RTS NC
46-
#endif
47-
48-
#ifndef MBED_CONF_ESP8266_CTS
49-
#define MBED_CONF_ESP8266_CTS NC
50-
#endif
51-
5240
// Firmware version
5341
#define ESP8266_VERSION 2
5442

mbed_lib.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@
33
"config": {
44
"tx": {
55
"help": "TX pin for serial connection",
6-
"value": null
6+
"value": "D1"
77
},
88
"rx": {
99
"help": "RX pin for serial connection",
10-
"value": null
10+
"value": "D0"
1111
},
1212
"rts": {
1313
"help": "RTS pin for serial connection",
14-
"value": null
14+
"value": "NC"
1515
},
1616
"cts": {
1717
"help": "CTS pin for serial connection",
18-
"value": null
18+
"value": "NC"
1919
},
2020
"debug": {
2121
"help": "Enable debug logs",
2222
"value": false
2323
},
2424
"socket-bufsize": {
2525
"help": "Size of the socket data buffer",
26-
"value": 30720
26+
"value": 102400
2727
}
2828
},
2929
"target_overrides": {

0 commit comments

Comments
 (0)