Skip to content

Commit 4616b77

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

File tree

4 files changed

+28
-61
lines changed

4 files changed

+28
-61
lines changed

ESP8266/ESP8266.cpp

Lines changed: 21 additions & 41 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
}
@@ -436,15 +422,15 @@ void ESP8266::_packet_handler()
436422
pdu_len = sizeof(struct packet) + amount;
437423

438424
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);
425+
MBED_WARNING1(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOBUFS), \
426+
"ESP8266::_packet_handler(), Buffer size %u exceeded", _MAX_HEAP_USAGE);
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;
@@ -456,8 +442,9 @@ void ESP8266::_packet_handler()
456442

457443
if (_parser.read((char*)(packet + 1), amount) < amount) {
458444
free(packet);
445+
MBED_WARNING(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_INVALID_DATA_DETECTED), \
446+
"ESP8266::_packet_handler(), AT parser returning less data than expected");
459447
_heap_usage -= pdu_len;
460-
MBED_ASSERT(_heap_usage >= 0);
461448
return;
462449
}
463450

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

499486
_smutex.unlock();
500487

501-
uint32_t alloc_len = q->alloc_len;
488+
uint32_t pdu_len = sizeof(struct packet) + q->alloc_len;
502489
uint32_t len = q->len;
503490
free(q);
504-
_heap_usage -= sizeof(struct packet) + alloc_len;
505-
MBED_ASSERT(_heap_usage >= 0);
491+
_heap_usage -= pdu_len;
506492
return len;
507493
} else { // return only partial packet
508494
memcpy(data, q+1, amount);
@@ -555,11 +541,9 @@ int32_t ESP8266::recv_udp(int id, void *data, uint32_t amount, uint32_t timeout)
555541
*p = (*p)->next;
556542
_smutex.unlock();
557543

558-
uint32_t alloc_len = q->alloc_len;
544+
uint32_t pdu_len = sizeof(struct packet) + q->alloc_len;
559545
free(q);
560-
_heap_usage -= sizeof(struct packet) + alloc_len;
561-
MBED_ASSERT(_heap_usage >= 0);
562-
546+
_heap_usage -= pdu_len;
563547
return len;
564548
}
565549
}
@@ -575,23 +559,19 @@ void ESP8266::_clear_socket_packets(int id)
575559
while (*p) {
576560
if ((*p)->id == id || id == ESP8266_ALL_SOCKET_IDS) {
577561
struct packet *q = *p;
578-
int alloc_len = q->alloc_len;
562+
int pdu_len = sizeof(struct packet) + q->alloc_len;
579563

580564
if (_packets_end == &(*p)->next) {
581565
_packets_end = p; // Set last packet next field/_packets
582566
}
583567
*p = (*p)->next;
584-
_heap_usage -= sizeof(struct packet) + alloc_len;
585-
586568
free(q);
569+
_heap_usage -= pdu_len;
587570
} else {
588571
// Point to last packet next field
589572
p = &(*p)->next;
590573
}
591574
}
592-
if (id == ESP8266_ALL_SOCKET_IDS) {
593-
MBED_ASSERT(_heap_usage == 0);
594-
}
595575
}
596576

597577
bool ESP8266::close(int id)

ESP8266/ESP8266.h

Lines changed: 2 additions & 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,8 @@ 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+
static const size_t _MAX_HEAP_USAGE = MBED_CONF_ESP8266_SOCKET_BUFSIZE;
319+
size_t _heap_usage;
321320
};
322321

323322
#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)