Skip to content

Commit 0a832dd

Browse files
author
Cruz Monrreal
authored
Merge pull request #9051 from michalpasztamobica/esp8266_busy_signal
ESP8266 send returns WOULD_BLOCK error when busy
2 parents f5fdbff + d6e385b commit 0a832dd

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

components/wifi/esp8266-driver/ESP8266/ESP8266.cpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ ESP8266::ESP8266(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
5050
_fail(false),
5151
_sock_already(false),
5252
_closed(false),
53+
_busy(false),
5354
_conn_status(NSAPI_STATUS_DISCONNECTED)
5455
{
5556
_serial.set_baud(ESP8266_DEFAULT_BAUD_RATE);
@@ -572,29 +573,33 @@ nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
572573
return NSAPI_ERROR_PARAMETER;
573574
}
574575

575-
//May take a second try if device is busy
576-
for (unsigned i = 0; i < 2; i++) {
577-
_smutex.lock();
578-
set_timeout(ESP8266_SEND_TIMEOUT);
579-
if (_parser.send("AT+CIPSEND=%d,%lu", id, amount)
580-
&& _parser.recv(">")
581-
&& _parser.write((char *)data, (int)amount) >= 0
582-
&& _parser.recv("SEND OK")) {
583-
// No flow control, data overrun is possible
584-
if (_serial_rts == NC) {
585-
while (_parser.process_oob()); // Drain USART receive register
586-
}
587-
_smutex.unlock();
588-
return NSAPI_ERROR_OK;
589-
}
590-
if (_error) {
591-
_error = false;
576+
_smutex.lock();
577+
set_timeout(ESP8266_SEND_TIMEOUT);
578+
_busy = false;
579+
if (_parser.send("AT+CIPSEND=%d,%lu", id, amount)
580+
&& _parser.recv(">")
581+
&& _parser.write((char *)data, (int)amount) >= 0
582+
&& _parser.recv("SEND OK")) {
583+
// No flow control, data overrun is possible
584+
if (_serial_rts == NC) {
585+
while (_parser.process_oob()); // Drain USART receive register
592586
}
593-
587+
_smutex.unlock();
588+
return NSAPI_ERROR_OK;
589+
}
590+
if (_error) {
591+
_error = false;
592+
}
593+
if (_busy) {
594594
set_timeout();
595595
_smutex.unlock();
596+
tr_debug("returning WOULD_BLOCK");
597+
return NSAPI_ERROR_WOULD_BLOCK;
596598
}
597599

600+
set_timeout();
601+
_smutex.unlock();
602+
598603
return NSAPI_ERROR_DEVICE_ERROR;
599604
}
600605

@@ -963,6 +968,7 @@ void ESP8266::_oob_busy()
963968
"ESP8266::_oob_busy() AT timeout\n");
964969
}
965970
_busy = true;
971+
_parser.abort();
966972
}
967973

968974
void ESP8266::_oob_tcp_data_hdlr()

components/wifi/esp8266-driver/ESP8266Interface.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "platform/Callback.h"
2929
#include "platform/mbed_debug.h"
3030
#include "platform/mbed_wait_api.h"
31+
#include "Kernel.h"
3132

3233
#ifndef MBED_CONF_ESP8266_DEBUG
3334
#define MBED_CONF_ESP8266_DEBUG false
@@ -518,7 +519,16 @@ int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
518519
return NSAPI_ERROR_NO_SOCKET;
519520
}
520521

521-
status = _esp.send(socket->id, data, size);
522+
unsigned long int sendStartTime = rtos::Kernel::get_ms_count();
523+
do {
524+
status = _esp.send(socket->id, data, size);
525+
} while ((sendStartTime - rtos::Kernel::get_ms_count() < 50)
526+
&& (status != NSAPI_ERROR_OK));
527+
528+
if (status == NSAPI_ERROR_WOULD_BLOCK) {
529+
debug("Enqueuing the event call");
530+
_global_event_queue->call_in(100, callback(this, &ESP8266Interface::event));
531+
}
522532

523533
return status != NSAPI_ERROR_OK ? status : size;
524534
}
@@ -720,4 +730,5 @@ void ESP8266Interface::proc_oob_evnt()
720730
{
721731
_esp.bg_process_oob(ESP8266_RECV_TIMEOUT, true);
722732
}
733+
723734
#endif

0 commit comments

Comments
 (0)