Skip to content

Commit e8cf91c

Browse files
authored
Merge pull request #3 from ARMmbed/dev_reconnect
Add reset for re-connect
2 parents fb7c868 + dde4c11 commit e8cf91c

File tree

4 files changed

+121
-9
lines changed

4 files changed

+121
-9
lines changed

ESP32/ESP32.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
using namespace mbed;
2626
using namespace rtos;
27+
using namespace std::chrono;
28+
using std::milli;
2729

2830
ESP32 * ESP32::instESP32 = NULL;
2931

@@ -191,7 +193,7 @@ void ESP32::_startup_common()
191193
}
192194
if (_p_wifi_en != NULL) {
193195
_p_wifi_en->write(0);
194-
ThisThread::sleep_for(10);
196+
ThisThread::sleep_for(10ms);
195197
_p_wifi_en->write(1);
196198
_parser.recv("ready");
197199
} else {
@@ -351,7 +353,7 @@ bool ESP32::accept(int * p_id)
351353
}
352354
_smutex.unlock();
353355
if (!ret) {
354-
ThisThread::sleep_for(5);
356+
ThisThread::sleep_for(5ms);
355357
}
356358
}
357359

@@ -392,7 +394,7 @@ bool ESP32::reset(void)
392394
#endif
393395
}
394396

395-
ThisThread::sleep_for(5);
397+
ThisThread::sleep_for(5ms);
396398

397399
uint8_t wk_ver[4+1]; /* It needs 1 byte extra. */
398400

@@ -654,7 +656,7 @@ int ESP32::scan(WiFiAccessPoint *res, unsigned limit)
654656
_smutex.lock();
655657
_startup_wifi();
656658
_smutex.unlock();
657-
ThisThread::sleep_for(1500);
659+
ThisThread::sleep_for(1500ms);
658660
}
659661

660662
_smutex.lock();
@@ -1109,6 +1111,14 @@ int8_t ESP32::get_wifi_status() const
11091111
return _wifi_status;
11101112
}
11111113

1114+
void ESP32::flush()
1115+
{
1116+
_smutex.lock();
1117+
_parser.flush();
1118+
_smutex.unlock();
1119+
}
1120+
1121+
11121122
#if defined(TARGET_ESP32AT_BLE)
11131123
bool ESP32::ble_set_role(int role)
11141124
{

ESP32/ESP32.h

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,22 @@
2424
#include <stdlib.h>
2525
#include "drivers/DigitalOut.h"
2626
#include "drivers/SerialBase.h"
27-
#include "drivers/BufferedSerial.h"
2827
#include "features/netsocket/nsapi_types.h"
2928
#include "features/netsocket/WiFiAccessPoint.h"
3029
#include "PinNames.h"
3130
#include "platform/ATCmdParser.h"
3231
#include "platform/Callback.h"
3332
#include "platform/mbed_error.h"
33+
#include "platform/mbed_version.h"
3434
#include "rtos/Mutex.h"
3535
#include "rtos/ThisThread.h"
3636

37+
#if (MBED_MAJOR_VERSION < 6)
38+
#include "drivers/UARTSerial.h"
39+
#else
40+
#include "drivers/BufferedSerial.h"
41+
#endif
42+
3743
#ifndef ESP32_CONNECT_TIMEOUT
3844
#define ESP32_CONNECT_TIMEOUT 15000
3945
#endif
@@ -246,6 +252,15 @@ class ESP32
246252
*/
247253
int8_t get_wifi_status() const;
248254

255+
/**
256+
* Flush the serial port input buffers.
257+
*
258+
* If you do HW reset for ESP module, you should
259+
* flush the input buffers from existing responses
260+
* from the device.
261+
*/
262+
void flush();
263+
249264
static const int8_t WIFIMODE_STATION = 1;
250265
static const int8_t WIFIMODE_SOFTAP = 2;
251266
static const int8_t WIFIMODE_STATION_SOFTAP = 3;
@@ -260,7 +275,11 @@ class ESP32
260275
mbed::DigitalOut * _p_wifi_io0;
261276
bool _init_end_common;
262277
bool _init_end_wifi;
278+
#if (MBED_MAJOR_VERSION < 6)
279+
mbed::UARTSerial _serial;
280+
#else
263281
mbed::BufferedSerial _serial;
282+
#endif
264283
mbed::ATCmdParser _parser;
265284
struct packet {
266285
struct packet *next;
@@ -352,11 +371,11 @@ class ESP32
352371
typedef struct {
353372
uint16_t adv_int_min; /**< minimum value of advertising interval; range: 0x0020 ~ 0x4000 */
354373
uint16_t adv_int_max; /**< maximum value of advertising interval; range: 0x0020 ~ 0x4000 */
355-
uint8_t adv_type; /**< 0FADV_TYPE_IND, 2FADV_TYPE_SCAN_IND, 3FADV_TYPE_NONCONN_IND */
356-
uint8_t own_addr_type; /**< own BLE address type; 0FBLE_ADDR_TYPE_PUBLIC, 1FBLE_ADDR_TYPE_RANDOM */
374+
uint8_t adv_type; /**< 0:FADV_TYPE_IND, 2:FADV_TYPE_SCAN_IND, 3:FADV_TYPE_NONCONN_IND */
375+
uint8_t own_addr_type; /**< own BLE address type; 0:FBLE_ADDR_TYPE_PUBLIC, 1:FBLE_ADDR_TYPE_RANDOM */
357376
uint8_t channel_map; /**< channel of advertising; ADV_CHNL_~ */
358377
uint8_t adv_filter_policy; /**< filter policy of advertising; ADV_FILTER_ALLOW_SCAN_~ */
359-
uint8_t peer_addr_type; /**< remote BLE address type; 0FPUBLIC, 1FRANDOM */
378+
uint8_t peer_addr_type; /**< remote BLE address type; 0:FPUBLIC, 1:FRANDOM */
360379
uint8_t peer_addr[6]; /**< remote BLE address */
361380
} advertising_param_t;
362381

ESP32Interface.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
ESP32Interface::ESP32Interface() :
2222
ESP32Stack(MBED_CONF_ESP32_WIFI_EN, MBED_CONF_ESP32_WIFI_IO0, MBED_CONF_ESP32_WIFI_TX, MBED_CONF_ESP32_WIFI_RX, MBED_CONF_ESP32_WIFI_DEBUG,
2323
MBED_CONF_ESP32_WIFI_RTS, MBED_CONF_ESP32_WIFI_CTS, MBED_CONF_ESP32_WIFI_BAUDRATE, 0),
24+
_rst_pin(MBED_CONF_ESP32_WIFI_EN),
25+
_initialized(false),
2426
_dhcp(true),
2527
_ap_ssid(),
2628
_ap_pass(),
@@ -38,6 +40,8 @@ ESP32Interface::ESP32Interface() :
3840
ESP32Interface::ESP32Interface(PinName en, PinName io0, PinName tx, PinName rx, bool debug,
3941
PinName rts, PinName cts, int baudrate) :
4042
ESP32Stack(en, io0, tx, rx, debug, rts, cts, baudrate, 0),
43+
_rst_pin(en),
44+
_initialized(false),
4145
_dhcp(true),
4246
_ap_ssid(),
4347
_ap_pass(),
@@ -54,6 +58,8 @@ ESP32Interface::ESP32Interface(PinName en, PinName io0, PinName tx, PinName rx,
5458

5559
ESP32Interface::ESP32Interface(PinName tx, PinName rx, bool debug) :
5660
ESP32Stack(NC, NC, tx, rx, debug, NC, NC, 230400, 0),
61+
_rst_pin(MBED_CONF_ESP32_WIFI_EN),
62+
_initialized(false),
5763
_dhcp(true),
5864
_ap_ssid(),
5965
_ap_pass(),
@@ -108,6 +114,8 @@ int ESP32Interface::connect(const char *ssid, const char *pass, nsapi_security_t
108114
if (ret != NSAPI_ERROR_OK) {
109115
return ret;
110116
}
117+
118+
_init();
111119
return connect();
112120
}
113121

@@ -201,6 +209,7 @@ int ESP32Interface::set_channel(uint8_t channel)
201209

202210
int ESP32Interface::disconnect()
203211
{
212+
_initialized = false;
204213
if (_connection_status == NSAPI_STATUS_DISCONNECTED) {
205214
return NSAPI_ERROR_NO_CONNECTION;
206215
}
@@ -263,6 +272,7 @@ int8_t ESP32Interface::get_rssi()
263272

264273
int ESP32Interface::scan(WiFiAccessPoint *res, unsigned count)
265274
{
275+
_init();
266276
return _esp->scan(res, count);
267277
}
268278

@@ -311,3 +321,56 @@ WiFiInterface *WiFiInterface::get_default_instance() {
311321

312322
#endif
313323

324+
ESP32Interface::~ESP32Interface()
325+
{
326+
// Power down the modem
327+
_rst_pin.rst_assert();
328+
}
329+
330+
ESP32Interface::ResetPin::ResetPin(PinName rst_pin) : _rst_pin(mbed::DigitalOut(rst_pin, 1))
331+
{
332+
}
333+
334+
void ESP32Interface::ResetPin::rst_assert()
335+
{
336+
if (_rst_pin.is_connected()) {
337+
_rst_pin = 0;
338+
//tr_debug("rst_assert(): HW reset asserted.");
339+
}
340+
}
341+
342+
void ESP32Interface::ResetPin::rst_deassert()
343+
{
344+
if (_rst_pin.is_connected()) {
345+
_rst_pin = 1;
346+
//tr_debug("rst_deassert(): HW reset deasserted.");
347+
}
348+
}
349+
350+
bool ESP32Interface::ResetPin::is_connected()
351+
{
352+
return _rst_pin.is_connected();
353+
}
354+
355+
nsapi_error_t ESP32Interface::_init(void)
356+
{
357+
if (!_initialized) {
358+
if (_reset() != NSAPI_ERROR_OK) {
359+
return NSAPI_ERROR_DEVICE_ERROR;
360+
}
361+
_initialized = true;
362+
}
363+
return NSAPI_ERROR_OK;
364+
}
365+
366+
nsapi_error_t ESP32Interface::_reset(void)
367+
{
368+
if (_rst_pin.is_connected()) {
369+
_rst_pin.rst_assert();
370+
rtos::ThisThread::sleep_for(2ms);
371+
_esp->flush();
372+
_rst_pin.rst_deassert();
373+
}
374+
375+
return NSAPI_ERROR_OK;
376+
}

ESP32Interface.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ class ESP32Interface : public ESP32Stack, public WiFiInterface
4444
ESP32Interface(PinName en, PinName io0, PinName tx, PinName rx, bool debug = false,
4545
PinName rts = NC, PinName cts = NC, int baudrate = 230400);
4646

47+
/**
48+
* @brief ESP32Interface default destructor
49+
*/
50+
virtual ~ESP32Interface();
51+
4752
/** ESP32Interface lifetime
4853
* @param tx TX pin
4954
* @param rx RX pin
@@ -241,6 +246,19 @@ class ESP32Interface : public ESP32Stack, public WiFiInterface
241246
}
242247

243248
private:
249+
250+
// HW reset pin
251+
class ResetPin {
252+
public:
253+
ResetPin(PinName rst_pin);
254+
void rst_assert();
255+
void rst_deassert();
256+
bool is_connected();
257+
private:
258+
mbed::DigitalOut _rst_pin;
259+
} _rst_pin;
260+
261+
int _initialized;
244262
bool _dhcp;
245263
char _ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
246264
char _ap_pass[64]; /* The longest allowed passphrase */
@@ -249,10 +267,12 @@ class ESP32Interface : public ESP32Stack, public WiFiInterface
249267
SocketAddress _netmask;
250268
SocketAddress _gateway;
251269
nsapi_connection_status_t _connection_status;
252-
Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
270+
mbed::Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
253271

254272
void set_connection_status(nsapi_connection_status_t connection_status);
255273
void wifi_status_cb(int8_t wifi_status);
274+
nsapi_error_t _init(void);
275+
nsapi_error_t _reset(void);
256276
};
257277

258278
#endif

0 commit comments

Comments
 (0)