27
27
#include " mbed_trace.h"
28
28
#include " platform/Callback.h"
29
29
#include " platform/mbed_debug.h"
30
+ #include " platform/mbed_wait_api.h"
30
31
31
32
#ifndef MBED_CONF_ESP8266_DEBUG
32
33
#define MBED_CONF_ESP8266_DEBUG false
40
41
#define MBED_CONF_ESP8266_CTS NC
41
42
#endif
42
43
44
+ #ifndef MBED_CONF_ESP8266_RST
45
+ #define MBED_CONF_ESP8266_RST NC
46
+ #endif
47
+
43
48
#define TRACE_GROUP " ESPI" // ESP8266 Interface
44
49
45
50
using namespace mbed ;
46
51
47
52
#if defined MBED_CONF_ESP8266_TX && defined MBED_CONF_ESP8266_RX
48
53
ESP8266Interface::ESP8266Interface ()
49
54
: _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
50
56
_ap_sec(NSAPI_SECURITY_UNKNOWN),
51
57
_initialized(false ),
52
58
_started(false ),
@@ -71,8 +77,9 @@ ESP8266Interface::ESP8266Interface()
71
77
#endif
72
78
73
79
// 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 )
75
81
: _esp(tx, rx, debug, rts, cts),
82
+ _rst_pin(rst),
76
83
_ap_sec(NSAPI_SECURITY_UNKNOWN),
77
84
_initialized(false ),
78
85
_started(false ),
@@ -102,6 +109,35 @@ ESP8266Interface::~ESP8266Interface()
102
109
}
103
110
}
104
111
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
+
105
141
int ESP8266Interface::connect (const char *ssid, const char *pass, nsapi_security_t security,
106
142
uint8_t channel)
107
143
{
@@ -224,6 +260,8 @@ int ESP8266Interface::set_channel(uint8_t channel)
224
260
225
261
int ESP8266Interface::disconnect ()
226
262
{
263
+ _initialized = false ;
264
+
227
265
if (_conn_stat == NSAPI_STATUS_DISCONNECTED)
228
266
{
229
267
return NSAPI_ERROR_NO_CONNECTION;
@@ -313,6 +351,8 @@ bool ESP8266Interface::_get_firmware_ok()
313
351
nsapi_error_t ESP8266Interface::_init (void )
314
352
{
315
353
if (!_initialized) {
354
+ _hw_reset ();
355
+
316
356
if (!_esp.at_available ()) {
317
357
return NSAPI_ERROR_DEVICE_ERROR;
318
358
}
@@ -343,6 +383,12 @@ nsapi_error_t ESP8266Interface::_init(void)
343
383
return NSAPI_ERROR_OK;
344
384
}
345
385
386
+ void ESP8266Interface::_hw_reset ()
387
+ {
388
+ _rst_pin.assert ();
389
+ _rst_pin.deassert ();
390
+ }
391
+
346
392
nsapi_error_t ESP8266Interface::_startup (const int8_t wifi_mode)
347
393
{
348
394
if (!_started) {
0 commit comments