The driver for the ESP32 WiFi module
The ESP32 WiFi driver for Mbed OS
The Mbed OS driver for the ESP32 WiFi module.
Firmware version
How to write mbed-os compatible firmware : https://github.com/d-kato/GR-Boards_ESP32_Serial_Bridge
Restrictions
- Setting up an UDP server is not possible
- The serial port does not have hardware flow control enabled. The AT command set does not either have a way to limit the download rate. Therefore, downloading anything larger than the serial port input buffer is unreliable. An application should be able to read fast enough to stay ahead of the network. This affects mostly the TCP protocol where data would be lost with no notification. On UDP, this would lead to only packet losses which the higher layer protocol should recover from.
Note
Revision 2:cb5c0d3fa776, committed 2018-07-09
- Comitter:
- dkato
- Date:
- Mon Jul 09 10:26:03 2018 +0000
- Parent:
- 1:5d78eedef723
- Commit message:
- Synchronized with git revision 78a139f3bce17e23f2b4bd9342dd7adca023d248
Changed in this revision
ESP32/ESP32.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/ESP32/ESP32.cpp Mon Jul 02 02:21:21 2018 +0000 +++ b/ESP32/ESP32.cpp Mon Jul 09 10:26:03 2018 +0000 @@ -38,7 +38,7 @@ ESP32::ESP32(PinName en, PinName io0, PinName tx, PinName rx, bool debug, PinName rts, PinName cts, int baudrate) : _p_wifi_en(NULL), _p_wifi_io0(NULL), init_end(false) - , _serial(tx, rx, ESP32_DEFAULT_BAUD_RATE), _parser(&_serial, "\r\n", 512) + , _serial(tx, rx, ESP32_DEFAULT_BAUD_RATE), _parser(&_serial, "\r\n") , _packets(0), _packets_end(&_packets) , _id_bits(0), _id_bits_close(0), _server_act(false) , _wifi_status(STATUS_DISCONNECTED) @@ -214,8 +214,6 @@ void ESP32::socket_handler(bool connect, int id) { - _smutex.lock(); - startup(); _cbs[id].Notified = 0; if (connect) { _id_bits |= (1 << id); @@ -233,7 +231,6 @@ } } } - _smutex.unlock(); } bool ESP32::accept(int * p_id) @@ -289,7 +286,7 @@ _parser.recv("ready"); _clear_socket_packets(ESP32_ALL_SOCKET_IDS); - if (_parser.send("AT+UART_CUR=%d,8,1,0,%d", _baudrate, 0) + if (_parser.send("AT+UART_CUR=%d,8,1,0,%d", _baudrate, _flow_control) && _parser.recv("OK")) { _serial.set_baud(_baudrate); #if DEVICE_SERIAL_FC @@ -632,33 +629,35 @@ } //May take a second try if device is busy + _smutex.lock(); while (error_cnt < 2) { if (((_id_bits & (1 << id)) == 0) || ((_id_bits_close & (1 << id)) != 0)) { + _smutex.unlock(); return false; } send_size = amount; if (send_size > 2048) { send_size = 2048; } - _smutex.lock(); startup(); ret = _parser.send("AT+CIPSEND=%d,%d", id, send_size) && _parser.recv(">") && (_parser.write((char*)data + index, (int)send_size) >= 0) && _parser.recv("SEND OK"); - _smutex.unlock(); if (ret) { amount -= send_size; index += send_size; error_cnt = 0; if (amount == 0) { + _smutex.unlock(); return true; } } else { error_cnt++; } } + _smutex.unlock(); return false; } @@ -700,48 +699,69 @@ int32_t ESP32::recv(int id, void *data, uint32_t amount, uint32_t timeout) { + struct packet **p; + uint32_t retry_cnt = 0; + uint32_t idx = 0; + _cbs[id].Notified = 0; - _smutex.lock(); - setTimeout(timeout); - _parser.process_oob(); // Poll for inbound packets - setTimeout(); - - // check if any packets are ready for us - for (struct packet **p = &_packets; *p; p = &(*p)->next) { - if ((*p)->id == id) { - struct packet *q = *p; - - if (q->len <= amount) { // Return and remove full packet - memcpy(data, (uint8_t*)(q+1) + q->index, q->len); + while (1) { + _smutex.lock(); + if (_rts == NC) { + setTimeout(1); + while (_parser.process_oob()); // Poll for inbound packets + setTimeout(); + } else if ((retry_cnt != 0) ||(_packets == NULL)) { + setTimeout(1); + _parser.process_oob(); // Poll for inbound packets + setTimeout(); + } else { + // do nothing + } - if (_packets_end == &(*p)->next) { - _packets_end = p; - } - *p = (*p)->next; - _smutex.unlock(); + // check if any packets are ready for us + p = &_packets; + while (*p) { + if ((*p)->id == id) { + struct packet *q = *p; - uint32_t len = q->len; - free(q); - return len; - } else { // return only partial packet - memcpy(data, (uint8_t*)(q+1) + q->index, amount); - - q->len -= amount; - q->index += amount; - - _smutex.unlock(); - return amount; + if (q->len <= amount) { // Return and remove full packet + memcpy(&(((uint8_t *)data)[idx]), (uint8_t*)(q+1) + q->index, q->len); + if (_packets_end == &(*p)->next) { + _packets_end = p; + } + *p = (*p)->next; + idx += q->len; + amount -= q->len; + free(q); + } else { // return only partial packet + memcpy(&(((uint8_t *)data)[idx]), (uint8_t*)(q+1) + q->index, amount); + q->len -= amount; + q->index += amount; + idx += amount; + break; + } + } else { + p = &(*p)->next; } } - } - _smutex.unlock(); - - if (((_id_bits & (1 << id)) == 0) - || ((_id_bits_close & (1 << id)) != 0)) { - return -2; - } else { - return -1; + if (idx > 0) { + _smutex.unlock(); + return idx; + } + if (retry_cnt >= timeout) { + if (((_id_bits & (1 << id)) == 0) + || ((_id_bits_close & (1 << id)) != 0)) { + _smutex.unlock(); + return -2; + } else { + _smutex.unlock(); + return -1; + } + } + retry_cnt++; + _smutex.unlock(); + Thread::wait(1); } }