2121ESP32Interface::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() :
3840ESP32Interface::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
5559ESP32Interface::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
202210int 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
264273int 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+ }
0 commit comments