Skip to content

Commit c10d276

Browse files
Veijo PesonenCruz Monrreal II
authored andcommitted
Adds support for controlling HW reset of the modem from the driver
While connecting will run HW reset for the modem if reset wire is attached to a know pin.
1 parent 04377fa commit c10d276

File tree

4 files changed

+77
-4
lines changed

4 files changed

+77
-4
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,18 @@ ESP8266::ESP8266(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
9191

9292
bool ESP8266::at_available()
9393
{
94+
bool ready = false;
95+
9496
_smutex.lock();
95-
bool ready = _parser.send("AT")
96-
&& _parser.recv("OK\n");
97+
// Might take a while to respond after HW reset
98+
for(int i = 0; i < 5; i++) {
99+
ready = _parser.send("AT")
100+
&& _parser.recv("OK\n");
101+
if (ready) {
102+
break;
103+
}
104+
tr_debug("waiting AT response");
105+
}
97106
_smutex.unlock();
98107

99108
return ready;

components/wifi/esp8266-driver/ESP8266Interface.cpp

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "mbed_trace.h"
2828
#include "platform/Callback.h"
2929
#include "platform/mbed_debug.h"
30+
#include "platform/mbed_wait_api.h"
3031

3132
#ifndef MBED_CONF_ESP8266_DEBUG
3233
#define MBED_CONF_ESP8266_DEBUG false
@@ -40,13 +41,18 @@
4041
#define MBED_CONF_ESP8266_CTS NC
4142
#endif
4243

44+
#ifndef MBED_CONF_ESP8266_RST
45+
#define MBED_CONF_ESP8266_RST NC
46+
#endif
47+
4348
#define TRACE_GROUP "ESPI" // ESP8266 Interface
4449

4550
using namespace mbed;
4651

4752
#if defined MBED_CONF_ESP8266_TX && defined MBED_CONF_ESP8266_RX
4853
ESP8266Interface::ESP8266Interface()
4954
: _esp(MBED_CONF_ESP8266_TX, MBED_CONF_ESP8266_RX, MBED_CONF_ESP8266_DEBUG, MBED_CONF_ESP8266_RTS, MBED_CONF_ESP8266_CTS),
55+
_rst_pin(MBED_CONF_ESP8266_RST), // Notice that Pin7 CH_EN cannot be left floating if used as reset
5056
_ap_sec(NSAPI_SECURITY_UNKNOWN),
5157
_initialized(false),
5258
_started(false),
@@ -71,8 +77,9 @@ ESP8266Interface::ESP8266Interface()
7177
#endif
7278

7379
// ESP8266Interface implementation
74-
ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
80+
ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName rts, PinName cts, PinName rst)
7581
: _esp(tx, rx, debug, rts, cts),
82+
_rst_pin(rst),
7683
_ap_sec(NSAPI_SECURITY_UNKNOWN),
7784
_initialized(false),
7885
_started(false),
@@ -102,6 +109,35 @@ ESP8266Interface::~ESP8266Interface()
102109
}
103110
}
104111

112+
ESP8266Interface::ResetPin::ResetPin(PinName rst_pin) : _rst_pin(mbed::DigitalOut(rst_pin, 1))
113+
{
114+
}
115+
116+
void ESP8266Interface::ResetPin::assert()
117+
{
118+
if (_rst_pin.is_connected()) {
119+
_rst_pin = 0;
120+
// If you happen to use Pin7 CH_EN as reset pin, not needed otherwise
121+
// https://www.espressif.com/sites/default/files/documentation/esp8266_hardware_design_guidelines_en.pdf
122+
wait_us(200);
123+
tr_debug("HW reset asserted");
124+
}
125+
}
126+
127+
void ESP8266Interface::ResetPin::deassert()
128+
{
129+
if (_rst_pin.is_connected()) {
130+
// Notice that Pin7 CH_EN cannot be left floating if used as reset
131+
_rst_pin = 1;
132+
tr_debug("HW reset deasserted");
133+
}
134+
}
135+
136+
bool ESP8266Interface::ResetPin::is_connected()
137+
{
138+
return _rst_pin.is_connected();
139+
}
140+
105141
int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security_t security,
106142
uint8_t channel)
107143
{
@@ -224,6 +260,8 @@ int ESP8266Interface::set_channel(uint8_t channel)
224260

225261
int ESP8266Interface::disconnect()
226262
{
263+
_initialized = false;
264+
227265
if (_conn_stat == NSAPI_STATUS_DISCONNECTED)
228266
{
229267
return NSAPI_ERROR_NO_CONNECTION;
@@ -313,6 +351,8 @@ bool ESP8266Interface::_get_firmware_ok()
313351
nsapi_error_t ESP8266Interface::_init(void)
314352
{
315353
if (!_initialized) {
354+
_hw_reset();
355+
316356
if (!_esp.at_available()) {
317357
return NSAPI_ERROR_DEVICE_ERROR;
318358
}
@@ -343,6 +383,12 @@ nsapi_error_t ESP8266Interface::_init(void)
343383
return NSAPI_ERROR_OK;
344384
}
345385

386+
void ESP8266Interface::_hw_reset()
387+
{
388+
_rst_pin.assert();
389+
_rst_pin.deassert();
390+
}
391+
346392
nsapi_error_t ESP8266Interface::_startup(const int8_t wifi_mode)
347393
{
348394
if (!_started) {

components/wifi/esp8266-driver/ESP8266Interface.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define ESP8266_INTERFACE_H
1919

2020
#if DEVICE_SERIAL && defined(MBED_CONF_EVENTS_PRESENT) && defined(MBED_CONF_NSAPI_PRESENT) && defined(MBED_CONF_RTOS_PRESENT)
21+
#include "drivers/DigitalOut.h"
2122
#include "ESP8266/ESP8266.h"
2223
#include "events/EventQueue.h"
2324
#include "events/mbed_shared_queues.h"
@@ -59,7 +60,7 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
5960
* @param rx RX pin
6061
* @param debug Enable debugging
6162
*/
62-
ESP8266Interface(PinName tx, PinName rx, bool debug = false, PinName rts = NC, PinName cts = NC);
63+
ESP8266Interface(PinName tx, PinName rx, bool debug = false, PinName rts = NC, PinName cts = NC, PinName rst = NC);
6364

6465
/**
6566
* @brief ESP8266Interface default destructor
@@ -320,6 +321,18 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
320321
ESP8266 _esp;
321322
void update_conn_state_cb();
322323

324+
// HW reset pin
325+
class ResetPin {
326+
public:
327+
ResetPin(PinName rst_pin);
328+
void assert();
329+
void deassert();
330+
bool is_connected();
331+
private:
332+
mbed::DigitalOut _rst_pin;
333+
} _rst_pin;
334+
335+
323336
// Credentials
324337
static const int ESP8266_SSID_MAX_LENGTH = 32; /* 32 is what 802.11 defines as longest possible name */
325338
char ap_ssid[ESP8266_SSID_MAX_LENGTH + 1]; /* The longest possible name; +1 for the \0 */
@@ -339,6 +352,7 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
339352
int _initialized;
340353
bool _get_firmware_ok();
341354
nsapi_error_t _init(void);
355+
void _hw_reset();
342356
int _started;
343357
nsapi_error_t _startup(const int8_t wifi_mode);
344358

components/wifi/esp8266-driver/mbed_lib.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
"help": "CTS pin for serial connection, defaults to Not Connected",
1818
"value": null
1919
},
20+
"rst": {
21+
"help": "RESET pin for the modem, defaults to Not Connected",
22+
"value": null
23+
},
2024
"debug": {
2125
"help": "Enable debug logs. [true/false]",
2226
"value": false

0 commit comments

Comments
 (0)