Skip to content

Commit ab1a723

Browse files
authored
Merge pull request #8689 from VeijoPesonen/esp8266-driver_v1.7
Add ESP8266 driver v1.7
2 parents 37630b2 + 934472b commit ab1a723

File tree

8 files changed

+1156
-366
lines changed

8 files changed

+1156
-366
lines changed

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

Lines changed: 591 additions & 179 deletions
Large diffs are not rendered by default.

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

Lines changed: 204 additions & 55 deletions
Large diffs are not rendered by default.

components/wifi/esp8266-driver/ESP8266Interface.cpp

Lines changed: 188 additions & 99 deletions
Large diffs are not rendered by default.

components/wifi/esp8266-driver/ESP8266Interface.h

Lines changed: 67 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,53 @@
1717
#ifndef ESP8266_INTERFACE_H
1818
#define ESP8266_INTERFACE_H
1919

20-
#include "mbed.h"
21-
#include "ESP8266.h"
22-
20+
#include "ESP8266/ESP8266.h"
21+
#include "events/EventQueue.h"
22+
#include "events/mbed_shared_queues.h"
23+
#include "features/netsocket/NetworkInterface.h"
24+
#include "features/netsocket/NetworkStack.h"
25+
#include "features/netsocket/nsapi_types.h"
26+
#include "features/netsocket/SocketAddress.h"
27+
#include "features/netsocket/WiFiAccessPoint.h"
28+
#include "features/netsocket/WiFiInterface.h"
29+
#include "platform/Callback.h"
2330

2431
#define ESP8266_SOCKET_COUNT 5
2532

33+
#ifdef TARGET_FF_ARDUINO
34+
#ifndef MBED_CONF_ESP8266_TX
35+
#define MBED_CONF_ESP8266_TX D1
36+
#endif
37+
38+
#ifndef MBED_CONF_ESP8266_RX
39+
#define MBED_CONF_ESP8266_RX D0
40+
#endif
41+
#endif /* TARGET_FF_ARDUINO */
42+
2643
/** ESP8266Interface class
2744
* Implementation of the NetworkStack for the ESP8266
2845
*/
29-
class ESP8266Interface : public NetworkStack, public WiFiInterface
30-
{
46+
class ESP8266Interface : public NetworkStack, public WiFiInterface {
3147
public:
48+
#if defined MBED_CONF_ESP8266_TX && defined MBED_CONF_ESP8266_RX
3249
/**
3350
* @brief ESP8266Interface default constructor
3451
* Will use values defined in mbed_lib.json
3552
*/
3653
ESP8266Interface();
54+
#endif
3755

3856
/** ESP8266Interface lifetime
3957
* @param tx TX pin
4058
* @param rx RX pin
4159
* @param debug Enable debugging
4260
*/
43-
ESP8266Interface(PinName tx, PinName rx, bool debug = false);
61+
ESP8266Interface(PinName tx, PinName rx, bool debug = false, PinName rts = NC, PinName cts = NC);
62+
63+
/**
64+
* @brief ESP8266Interface default destructor
65+
*/
66+
virtual ~ESP8266Interface();
4467

4568
/** Start the interface
4669
*
@@ -62,7 +85,7 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
6285
* @return 0 on success, or error code on failure
6386
*/
6487
virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE,
65-
uint8_t channel = 0);
88+
uint8_t channel = 0);
6689

6790
/** Set the WiFi network credentials
6891
*
@@ -98,11 +121,11 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
98121
*/
99122
virtual const char *get_mac_address();
100123

101-
/** Get the local gateway
102-
*
103-
* @return Null-terminated representation of the local gateway
104-
* or null if no network mask has been recieved
105-
*/
124+
/** Get the local gateway
125+
*
126+
* @return Null-terminated representation of the local gateway
127+
* or null if no network mask has been recieved
128+
*/
106129
virtual const char *get_gateway();
107130

108131
/** Get the local network mask
@@ -156,12 +179,12 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
156179
/** @copydoc NetworkStack::setsockopt
157180
*/
158181
virtual nsapi_error_t setsockopt(nsapi_socket_t handle, int level,
159-
int optname, const void *optval, unsigned optlen);
182+
int optname, const void *optval, unsigned optlen);
160183

161184
/** @copydoc NetworkStack::getsockopt
162185
*/
163186
virtual nsapi_error_t getsockopt(nsapi_socket_t handle, int level, int optname,
164-
void *optval, unsigned *optlen);
187+
void *optval, unsigned *optlen);
165188

166189
/** Register callback for status reporting
167190
*
@@ -292,31 +315,49 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
292315
}
293316

294317
private:
318+
// AT layer
319+
ESP8266 _esp;
320+
void update_conn_state_cb();
321+
322+
// Credentials
295323
static const int ESP8266_SSID_MAX_LENGTH = 32; /* 32 is what 802.11 defines as longest possible name */
324+
char ap_ssid[ESP8266_SSID_MAX_LENGTH + 1]; /* The longest possible name; +1 for the \0 */
296325
static const int ESP8266_PASSPHRASE_MAX_LENGTH = 63; /* The longest allowed passphrase */
297326
static const int ESP8266_PASSPHRASE_MIN_LENGTH = 8; /* The shortest allowed passphrase */
327+
char ap_pass[ESP8266_PASSPHRASE_MAX_LENGTH + 1]; /* The longest possible passphrase; +1 for the \0 */
328+
nsapi_security_t _ap_sec;
298329

299-
ESP8266 _esp;
300-
bool _ids[ESP8266_SOCKET_COUNT];
301-
int _initialized;
302-
int _started;
303-
304-
char ap_ssid[ESP8266_SSID_MAX_LENGTH + 1]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
305-
nsapi_security_t ap_sec;
306-
uint8_t ap_ch;
307-
char ap_pass[ESP8266_PASSPHRASE_MAX_LENGTH + 1];
308-
uint16_t _local_ports[ESP8266_SOCKET_COUNT];
330+
// Drivers's socket info
331+
struct _sock_info {
332+
bool open;
333+
uint16_t sport;
334+
};
335+
struct _sock_info _sock_i[ESP8266_SOCKET_COUNT];
309336

310-
bool _disable_default_softap();
311-
void event();
337+
// Driver's state
338+
int _initialized;
312339
bool _get_firmware_ok();
313340
nsapi_error_t _init(void);
341+
int _started;
314342
nsapi_error_t _startup(const int8_t wifi_mode);
315343

344+
//sigio
316345
struct {
317346
void (*callback)(void *);
318347
void *data;
319348
} _cbs[ESP8266_SOCKET_COUNT];
349+
void event();
350+
351+
// Connection state reporting to application
352+
nsapi_connection_status_t _conn_stat;
353+
mbed::Callback<void(nsapi_event_t, intptr_t)> _conn_stat_cb;
354+
355+
// Background OOB processing
356+
// Use global EventQueue
357+
events::EventQueue *_global_event_queue;
358+
int _oob_event_id;
359+
void proc_oob_evnt();
360+
void _oob2global_event_queue();
320361
};
321362

322363
#endif

components/wifi/esp8266-driver/README.md

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,97 @@ The Mbed OS driver for the ESP8266 WiFi module.
44

55
## Firmware version
66

7-
ESP8266 modules come in different shapes and formats, but the most important factor is the firmware version in it. To make sure that the firmware in your module is compatible with Mbed OS, follow the [Update guide](https://developer.mbed.org/teams/ESP8266/wiki/Firmware-Update).
7+
ESP8266 modules come in different shapes and formats, but the firmware version is the most important factor. To
8+
make sure that the firmware in your module is compatible with Mbed OS, follow the
9+
[Update guide](https://developer.mbed.org/teams/ESP8266/wiki/Firmware-Update).
10+
11+
This driver supports AT firmware versions 1.3.0 to 1.7.0. We advise updating the
12+
[AT firmware](https://www.espressif.com/en/support/download/at?keys=) to at least version 1.7.0.
813

914
## Restrictions
1015

11-
- The ESP8266 WiFi module does not allow the TCP client to bind on a specific port.
12-
- Setting up a UDP server is not possible.
13-
- The serial port does not have hardware flow control enabled. The AT command set does not either have a way to limit the download rate. Therefore, downloading anything larger than the serial port input buffer is unreliable. An application should be able to read fast enough to stay ahead of the network. This affects mostly the TCP protocol where data would be lost with no notification. On UDP, this would lead to only packet losses which the higher layer protocol should recover from.
16+
* The ESP8266 Wi-Fi module does not allow the TCP client to bind to a specific port.
17+
* Setting up a UDP server is not possible.
18+
* The serial port does not have hardware flow control enabled by default. Additionally, the AT command set does not have a method for limiting the download rate. Therefore, downloading anything larger than the serial port input buffer is unreliable
19+
unless you use [AT firmware](https://www.espressif.com/en/support/download/at?keys=) version 1.7.0 or later. With older
20+
firmware, an application should be able to read fast enough to stay ahead of the network. This applies mostly to TCP
21+
protocol, where data would be lost without notification. On UDP using all firmware versions, the higher-layer protocol should recover from packet loss.
22+
23+
## Mandatory configuration
24+
25+
![mbed_lib.json](mbed_lib.json) configuration assumes Arduino form factor. Please adjust according to your board. You can override parameters from your app config file.
26+
27+
At minimum, check the following configuration parameters:
28+
29+
```javascript
30+
{
31+
"name": "esp8266",
32+
"config": {
33+
"tx": {
34+
"help": "TX pin for serial connection",
35+
"value": null <- 'D1' by default if Arduino, adjust based on your board
36+
},
37+
"rx": {
38+
"help": "RX pin for serial connection",
39+
"value": null <- 'D0' by default if Arduino, adjust based on your board
40+
},
41+
"provide-default": {
42+
"help": "Provide default WifiInterface. [true/false]",
43+
"value": false <- Set to 'true' if this is the interface you are using
44+
},
45+
"socket-bufsize": {
46+
"help": "Max socket data heap usage",
47+
"value": 8192 <- Without HW flow control more is better. Once the limit is reached packets are
48+
dropped - does not matter is it TCP or UDP.
49+
}
50+
}
51+
}
52+
```
53+
54+
## UART HW flow control
55+
56+
UART HW flow control requires you to additionally wire the CTS and RTS flow control pins between your board and your
57+
ESP8266 module. After this, remember to add the configuration option for flow control to your app configuration file. This example uses the [ST NUCLEO-F429ZI](https://os.mbed.com/platforms/ST-Nucleo-F429ZI/) board and
58+
[ESPBee XBee Module](https://www.cascologix.com/product/espbee/).
59+
60+
**Note:** Not all modules expose ESP8266's RTS and CTS pins, so choose modules carefully.
61+
62+
Once you have your hardware set up, add a configuration like the following to your app configuration file. Arduino pins D1 and D0 are used as TX and RX:
63+
64+
``` javascript
65+
"target_overrides": {
66+
"NUCLEO_F429ZI": {
67+
"esp8266.rts": "PG_12",
68+
"esp8266.cts": "PG_15"
69+
}
70+
```
71+
72+
### Example board pins
73+
74+
* TX: D1 (Arduino Uno Revision 3 connectivity headers)
75+
* RX: D0 (Arduino Uno Revision 3 connectivity headers)
76+
* RTS: PG_12 (STMicroelectronics Morpho extension pin headers)
77+
* CTS: PG_15 (STMicroelectronics Morpho extension pin headers)
78+
79+
### Example ESP8266 pins
80+
81+
* TX: D1 (Arduino Wireless Protoshield headers)/ TX (ESPBee XBee headers)
82+
* RX: D0 (Arduino Wireless Protoshield headers)/ RX (ESPBee XBee headers)
83+
* RTS: RTS (ESPBee XBee headers)
84+
* CTS: CTS (ESPBee XBee headers)
85+
86+
### Connections
87+
88+
With these pictures only consider the green and yellow wires which are connected to ESP8266. The pink wire is for reset and
89+
the rest are for firmware update. TX and RX go through Arduino pins D1 and D0.
90+
91+
**Note:** Pull down GPIO15(ESPBee RTS) during startup to **boot from flash**, instead of **firmware update** or
92+
**boot from SD card**. Once the software is running, the same pin is used as the RTS pin:
93+
94+
* Board TX: ESP8266 RX
95+
* Board RX: ESP8266 TX
96+
* Board RTS (grey): ESP8266 CTS(yellow)
97+
* Board CTS (white): ESP8266 RTS(green)
98+
99+
![RTS,CTS](nucleo_esp8266_hw_fc1.jpg)
100+
![RTS,CTS](nucleo_esp8266_hw_fc2.jpg)

components/wifi/esp8266-driver/mbed_lib.json

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,32 @@
22
"name": "esp8266",
33
"config": {
44
"tx": {
5-
"help": "TX pin for serial connection",
5+
"help": "TX pin for serial connection. D1 assumed if Arduino Form Factor, needs to be set/overwritten otherwise",
66
"value": null
77
},
88
"rx": {
9-
"help": "RX pin for serial connection",
9+
"help": "RX pin for serial connection. D0 assumed if Arduino Form Factor, needs to be set/overwritten otherwise",
10+
"value": null
11+
},
12+
"rts": {
13+
"help": "RTS pin for serial connection, defaults to Not Connected",
14+
"value": null
15+
},
16+
"cts": {
17+
"help": "CTS pin for serial connection, defaults to Not Connected",
1018
"value": null
1119
},
1220
"debug": {
13-
"help": "Enable debug logs",
21+
"help": "Enable debug logs. [true/false]",
1422
"value": false
1523
},
1624
"provide-default": {
1725
"help": "Provide default WifiInterface. [true/false]",
1826
"value": false
27+
},
28+
"socket-bufsize": {
29+
"help": "Max socket data heap usage",
30+
"value": 8192
1931
}
2032
},
2133
"target_overrides": {
Loading
Loading

0 commit comments

Comments
 (0)