@@ -65,6 +65,7 @@ ESP8266::ESP8266(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
65
65
// Don't see a reason to make distiction between software(Software WDT reset) and hardware(wdt reset) watchdog treatment
66
66
// https://github.com/esp8266/Arduino/blob/4897e0006b5b0123a2fa31f67b14a3fff65ce561/doc/faq/a02-my-esp-crashes.md#watchdog
67
67
_parser.oob (" Soft WDT reset" , callback (this , &ESP8266::_oob_watchdog_reset));
68
+ _parser.oob (" ERROR" , callback (this , &ESP8266::_oob_err));
68
69
69
70
for (int i= 0 ; i < SOCKET_COUNT; i++) {
70
71
_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
424
425
" ESP8266::_open_udp: device refused to close socket" );
425
426
}
426
427
}
428
+ if (_error) {
429
+ _error = false ;
430
+ done = false ;
431
+ }
427
432
continue ;
428
433
}
429
434
_sock_i[id].open = true ;
@@ -466,6 +471,10 @@ nsapi_error_t ESP8266::open_tcp(int id, const char* addr, int port, int keepaliv
466
471
" ESP8266::_open_tcp: device refused to close socket" );
467
472
}
468
473
}
474
+ if (_error) {
475
+ _error = false ;
476
+ done = false ;
477
+ }
469
478
continue ;
470
479
}
471
480
_sock_i[id].open = true ;
@@ -497,14 +506,19 @@ nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
497
506
set_timeout (ESP8266_SEND_TIMEOUT);
498
507
if (_parser.send (" AT+CIPSEND=%d,%lu" , id, amount)
499
508
&& _parser.recv (" >" )
500
- && _parser.write ((char *)data, (int )amount) >= 0 ) {
509
+ && _parser.write ((char *)data, (int )amount) >= 0
510
+ && _parser.recv (" SEND OK" )) {
501
511
// No flow control, data overrun is possible
502
512
if (_serial_rts == NC) {
503
513
while (_parser.process_oob ()); // Drain USART receive register
504
514
}
505
515
_smutex.unlock ();
506
516
return NSAPI_ERROR_OK;
507
517
}
518
+ if (_error) {
519
+ _error = false ;
520
+ }
521
+
508
522
set_timeout ();
509
523
_smutex.unlock ();
510
524
}
@@ -581,6 +595,11 @@ int32_t ESP8266::_recv_tcp_passive(int id, void *data, uint32_t amount, uint32_t
581
595
int32_t len;
582
596
int32_t ret = (int32_t )NSAPI_ERROR_WOULD_BLOCK;
583
597
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
+
584
603
_smutex.lock ();
585
604
586
605
// 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
604
623
ret = done ? len : 0 ;
605
624
}
606
625
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
+ }
607
630
_smutex.unlock ();
608
631
return ret;
609
632
}
@@ -831,12 +854,18 @@ void ESP8266::_oob_conn_already()
831
854
_parser.abort ();
832
855
}
833
856
857
+ void ESP8266::_oob_err ()
858
+ {
859
+ _error = true ;
860
+ _parser.abort ();
861
+ }
862
+
834
863
void ESP8266::_oob_socket_close_err ()
835
864
{
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 ;
839
867
}
868
+ _closed = true ; // Not possible to pinpoint to a certain socket
840
869
}
841
870
842
871
void ESP8266::_oob_socket0_closed ()
0 commit comments