Skip to content

Commit 1275289

Browse files
author
Veijo Pesonen
authored
Merge branch 'master' into feature-recover_from_wdt_reset
2 parents fccb98f + 471beb5 commit 1275289

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

ESP8266/ESP8266.cpp

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ ESP8266::ESP8266(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
6565
// Don't see a reason to make distiction between software(Software WDT reset) and hardware(wdt reset) watchdog treatment
6666
//https://github.com/esp8266/Arduino/blob/4897e0006b5b0123a2fa31f67b14a3fff65ce561/doc/faq/a02-my-esp-crashes.md#watchdog
6767
_parser.oob("Soft WDT reset", callback(this, &ESP8266::_oob_watchdog_reset));
68+
_parser.oob("ERROR", callback(this, &ESP8266::_oob_err));
6869

6970
for(int i= 0; i < SOCKET_COUNT; i++) {
7071
_sock_i[i].open = false;
@@ -424,6 +425,10 @@ nsapi_error_t ESP8266::open_udp(int id, const char* addr, int port, int local_po
424425
"ESP8266::_open_udp: device refused to close socket");
425426
}
426427
}
428+
if (_error) {
429+
_error = false;
430+
done = false;
431+
}
427432
continue;
428433
}
429434
_sock_i[id].open = true;
@@ -466,6 +471,10 @@ nsapi_error_t ESP8266::open_tcp(int id, const char* addr, int port, int keepaliv
466471
"ESP8266::_open_tcp: device refused to close socket");
467472
}
468473
}
474+
if (_error) {
475+
_error = false;
476+
done = false;
477+
}
469478
continue;
470479
}
471480
_sock_i[id].open = true;
@@ -497,14 +506,19 @@ nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
497506
set_timeout(ESP8266_SEND_TIMEOUT);
498507
if (_parser.send("AT+CIPSEND=%d,%lu", id, amount)
499508
&& _parser.recv(">")
500-
&& _parser.write((char*)data, (int)amount) >= 0) {
509+
&& _parser.write((char*)data, (int)amount) >= 0
510+
&& _parser.recv("SEND OK")) {
501511
// No flow control, data overrun is possible
502512
if (_serial_rts == NC) {
503513
while (_parser.process_oob()); // Drain USART receive register
504514
}
505515
_smutex.unlock();
506516
return NSAPI_ERROR_OK;
507517
}
518+
if (_error) {
519+
_error = false;
520+
}
521+
508522
set_timeout();
509523
_smutex.unlock();
510524
}
@@ -581,6 +595,11 @@ int32_t ESP8266::_recv_tcp_passive(int id, void *data, uint32_t amount, uint32_t
581595
int32_t len;
582596
int32_t ret = (int32_t)NSAPI_ERROR_WOULD_BLOCK;
583597

598+
// No flow control, drain the USART receive register ASAP to avoid data overrun
599+
if (_serial_rts == NC) {
600+
_process_oob(timeout, true);
601+
}
602+
584603
_smutex.lock();
585604

586605
// NOTE: documentation v3.0 says '+CIPRECVDATA:<data_len>,' but it's not how the FW responds...
@@ -604,6 +623,10 @@ int32_t ESP8266::_recv_tcp_passive(int id, void *data, uint32_t amount, uint32_t
604623
ret = done ? len : 0;
605624
}
606625

626+
// Flow control, read from USART receive register only when no more data is buffered, and as little as possible
627+
if (_serial_rts != NC) {
628+
_process_oob(timeout, false);
629+
}
607630
_smutex.unlock();
608631
return ret;
609632
}
@@ -831,12 +854,18 @@ void ESP8266::_oob_conn_already()
831854
_parser.abort();
832855
}
833856

857+
void ESP8266::_oob_err()
858+
{
859+
_error = true;
860+
_parser.abort();
861+
}
862+
834863
void ESP8266::_oob_socket_close_err()
835864
{
836-
if (_parser.recv("ERROR\n")) {
837-
_closed = true; // Not possible to pinpoint to a certain socket
838-
_parser.abort();
865+
if (_error) {
866+
_error = false;
839867
}
868+
_closed = true; // Not possible to pinpoint to a certain socket
840869
}
841870

842871
void ESP8266::_oob_socket0_closed()

ESP8266/ESP8266.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ class ESP8266
388388
void _oob_packet_hdlr();
389389
void _oob_connect_err();
390390
void _oob_conn_already();
391+
void _oob_err();
391392
void _oob_socket0_closed();
392393
void _oob_socket1_closed();
393394
void _oob_socket2_closed();
@@ -402,6 +403,7 @@ class ESP8266
402403
bool _fail;
403404
bool _sock_already;
404405
bool _closed;
406+
bool _error;
405407

406408
// Modem's address info
407409
char _ip_buffer[16];

0 commit comments

Comments
 (0)