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
- _started(false ),
53
58
_conn_stat(NSAPI_STATUS_DISCONNECTED),
54
59
_conn_stat_cb(NULL ),
55
60
_global_event_queue(NULL ),
@@ -67,15 +72,17 @@ ESP8266Interface::ESP8266Interface()
67
72
_sock_i[i].open = false ;
68
73
_sock_i[i].sport = 0 ;
69
74
}
75
+
76
+ _oob2global_event_queue ();
70
77
}
71
78
#endif
72
79
73
80
// ESP8266Interface implementation
74
- ESP8266Interface::ESP8266Interface (PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
81
+ ESP8266Interface::ESP8266Interface (PinName tx, PinName rx, bool debug, PinName rts, PinName cts, PinName rst )
75
82
: _esp(tx, rx, debug, rts, cts),
83
+ _rst_pin(rst),
76
84
_ap_sec(NSAPI_SECURITY_UNKNOWN),
77
85
_initialized(false ),
78
- _started(false ),
79
86
_conn_stat(NSAPI_STATUS_DISCONNECTED),
80
87
_conn_stat_cb(NULL ),
81
88
_global_event_queue(NULL ),
@@ -93,13 +100,44 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
93
100
_sock_i[i].open = false ;
94
101
_sock_i[i].sport = 0 ;
95
102
}
103
+
104
+ _oob2global_event_queue ();
96
105
}
97
106
98
107
ESP8266Interface::~ESP8266Interface ()
99
108
{
100
109
if (_oob_event_id) {
101
110
_global_event_queue->cancel (_oob_event_id);
102
111
}
112
+
113
+ // Power down the modem
114
+ _rst_pin.rst_assert ();
115
+ }
116
+
117
+ ESP8266Interface::ResetPin::ResetPin (PinName rst_pin) : _rst_pin(mbed::DigitalOut(rst_pin, 1 ))
118
+ {
119
+ }
120
+
121
+ void ESP8266Interface::ResetPin::rst_assert ()
122
+ {
123
+ if (_rst_pin.is_connected ()) {
124
+ _rst_pin = 0 ;
125
+ tr_debug (" HW reset asserted" );
126
+ }
127
+ }
128
+
129
+ void ESP8266Interface::ResetPin::rst_deassert ()
130
+ {
131
+ if (_rst_pin.is_connected ()) {
132
+ // Notice that Pin7 CH_EN cannot be left floating if used as reset
133
+ _rst_pin = 1 ;
134
+ tr_debug (" HW reset deasserted" );
135
+ }
136
+ }
137
+
138
+ bool ESP8266Interface::ResetPin::is_connected ()
139
+ {
140
+ return _rst_pin.is_connected ();
103
141
}
104
142
105
143
int ESP8266Interface::connect (const char *ssid, const char *pass, nsapi_security_t security,
@@ -147,10 +185,6 @@ int ESP8266Interface::connect()
147
185
return status;
148
186
}
149
187
150
- if (!_oob_event_id) {
151
- _oob2global_event_queue ();
152
- }
153
-
154
188
if (get_ip_address ()) {
155
189
return NSAPI_ERROR_IS_CONNECTED;
156
190
}
@@ -159,22 +193,12 @@ int ESP8266Interface::connect()
159
193
if (status != NSAPI_ERROR_OK) {
160
194
return status;
161
195
}
162
- _started = true ;
163
196
164
197
if (!_esp.dhcp (true , 1 )) {
165
198
return NSAPI_ERROR_DHCP_FAILURE;
166
199
}
167
200
168
- int connect_error = _esp.connect (ap_ssid, ap_pass);
169
- if (connect_error) {
170
- return connect_error;
171
- }
172
-
173
- if (!get_ip_address ()) {
174
- return NSAPI_ERROR_DHCP_FAILURE;
175
- }
176
-
177
- return NSAPI_ERROR_OK;
201
+ return _esp.connect (ap_ssid, ap_pass);
178
202
}
179
203
180
204
int ESP8266Interface::set_credentials (const char *ssid, const char *pass, nsapi_security_t security)
@@ -224,6 +248,8 @@ int ESP8266Interface::set_channel(uint8_t channel)
224
248
225
249
int ESP8266Interface::disconnect ()
226
250
{
251
+ _initialized = false ;
252
+
227
253
if (_conn_stat == NSAPI_STATUS_DISCONNECTED)
228
254
{
229
255
return NSAPI_ERROR_NO_CONNECTION;
@@ -234,19 +260,23 @@ int ESP8266Interface::disconnect()
234
260
if (ret == NSAPI_ERROR_OK) {
235
261
// Try to lure the nw status update from ESP8266, might come later
236
262
_esp.bg_process_oob (ESP8266_RECV_TIMEOUT, true );
237
- // In case the status update arrives later
238
- _conn_stat = NSAPI_STATUS_DISCONNECTED;
263
+ // In case the status update arrives later inform upper layers manually
264
+ if (_conn_stat != NSAPI_STATUS_DISCONNECTED) {
265
+ _conn_stat = NSAPI_STATUS_DISCONNECTED;
266
+ if (_conn_stat_cb) {
267
+ _conn_stat_cb (NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _conn_stat);
268
+ }
269
+ }
239
270
}
240
271
272
+ // Power down the modem
273
+ _rst_pin.rst_assert ();
274
+
241
275
return ret;
242
276
}
243
277
244
278
const char *ESP8266Interface::get_ip_address ()
245
279
{
246
- if (!_started) {
247
- return NULL ;
248
- }
249
-
250
280
const char *ip_buff = _esp.ip_addr ();
251
281
if (!ip_buff || strcmp (ip_buff, " 0.0.0.0" ) == 0 ) {
252
282
return NULL ;
@@ -262,17 +292,17 @@ const char *ESP8266Interface::get_mac_address()
262
292
263
293
const char *ESP8266Interface::get_gateway ()
264
294
{
265
- return _started ? _esp.gateway () : NULL ;
295
+ return _conn_stat != NSAPI_STATUS_DISCONNECTED ? _esp.gateway () : NULL ;
266
296
}
267
297
268
298
const char *ESP8266Interface::get_netmask ()
269
299
{
270
- return _started ? _esp.netmask () : NULL ;
300
+ return _conn_stat != NSAPI_STATUS_DISCONNECTED ? _esp.netmask () : NULL ;
271
301
}
272
302
273
303
int8_t ESP8266Interface::get_rssi ()
274
304
{
275
- return _started ? _esp.rssi () : 0 ;
305
+ return _esp.rssi ();
276
306
}
277
307
278
308
int ESP8266Interface::scan (WiFiAccessPoint *res, unsigned count)
@@ -313,6 +343,8 @@ bool ESP8266Interface::_get_firmware_ok()
313
343
nsapi_error_t ESP8266Interface::_init (void )
314
344
{
315
345
if (!_initialized) {
346
+ _hw_reset ();
347
+
316
348
if (!_esp.at_available ()) {
317
349
return NSAPI_ERROR_DEVICE_ERROR;
318
350
}
@@ -343,9 +375,18 @@ nsapi_error_t ESP8266Interface::_init(void)
343
375
return NSAPI_ERROR_OK;
344
376
}
345
377
378
+ void ESP8266Interface::_hw_reset ()
379
+ {
380
+ _rst_pin.rst_assert ();
381
+ // If you happen to use Pin7 CH_EN as reset pin, not needed otherwise
382
+ // https://www.espressif.com/sites/default/files/documentation/esp8266_hardware_design_guidelines_en.pdf
383
+ wait_us (200 );
384
+ _rst_pin.rst_deassert ();
385
+ }
386
+
346
387
nsapi_error_t ESP8266Interface::_startup (const int8_t wifi_mode)
347
388
{
348
- if (!_started ) {
389
+ if (_conn_stat == NSAPI_STATUS_DISCONNECTED ) {
349
390
if (!_esp.startup (wifi_mode)) {
350
391
return NSAPI_ERROR_DEVICE_ERROR;
351
392
}
@@ -646,25 +687,26 @@ WiFiInterface *WiFiInterface::get_default_instance()
646
687
647
688
void ESP8266Interface::update_conn_state_cb ()
648
689
{
690
+ nsapi_connection_status_t prev_stat = _conn_stat;
649
691
_conn_stat = _esp.connection_status ();
650
692
693
+ if (prev_stat == _conn_stat) {
694
+ return ;
695
+ }
696
+
651
697
switch (_conn_stat) {
652
698
// Doesn't require changes
653
699
case NSAPI_STATUS_CONNECTING:
654
700
case NSAPI_STATUS_GLOBAL_UP:
655
701
break ;
656
702
// Start from scratch if connection drops/is dropped
657
703
case NSAPI_STATUS_DISCONNECTED:
658
- _started = false ;
659
704
break ;
660
705
// Handled on AT layer
661
706
case NSAPI_STATUS_LOCAL_UP:
662
707
case NSAPI_STATUS_ERROR_UNSUPPORTED:
663
708
default :
664
- _started = false ;
665
709
_initialized = false ;
666
- _global_event_queue->cancel (_oob_event_id);
667
- _oob_event_id = 0 ;
668
710
_conn_stat = NSAPI_STATUS_DISCONNECTED;
669
711
}
670
712
@@ -676,8 +718,6 @@ void ESP8266Interface::update_conn_state_cb()
676
718
677
719
void ESP8266Interface::proc_oob_evnt ()
678
720
{
679
- if (_initialized) {
680
721
_esp.bg_process_oob (ESP8266_RECV_TIMEOUT, true );
681
- }
682
722
}
683
723
#endif
0 commit comments