Skip to content

Commit 7180ca3

Browse files
authored
Add ESP32-based WiFi support via lwIP_ESPHost library (#1950)
1 parent c64a4a5 commit 7180ca3

File tree

8 files changed

+554
-2
lines changed

8 files changed

+554
-2
lines changed

libraries/WiFi/src/WiFiClass.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#ifdef ARDUINO_RASPBERRY_PI_PICO_W
3333
#include <pico/cyw43_arch.h>
3434
static CYW43lwIP _wifi(1);
35+
#elif defined(ESPHOSTSPI)
36+
static ESPHostLwIP _wifi;
3537
#else
3638
static NoDriverLwIP _wifi;
3739
#endif
@@ -100,11 +102,11 @@ int WiFiClass::begin(const char* ssid, const char *passphrase, const uint8_t *bs
100102
} else {
101103
bzero(_bssid, sizeof(_bssid));
102104
}
105+
_wifi.setSTA();
103106
_wifi.setSSID(_ssid.c_str());
104107
_wifi.setBSSID(_bssid);
105108
_wifi.setPassword(passphrase);
106109
_wifi.setTimeout(_timeout);
107-
_wifi.setSTA();
108110
_apMode = false;
109111
_wifiHWInitted = true;
110112
uint32_t start = millis(); // The timeout starts from network init, not network link up
@@ -139,10 +141,10 @@ uint8_t WiFiClass::beginAP(const char *ssid, const char* passphrase) {
139141

140142
_ssid = ssid;
141143
_password = passphrase;
144+
_wifi.setAP();
142145
_wifi.setSSID(_ssid.c_str());
143146
_wifi.setPassword(passphrase);
144147
_wifi.setTimeout(_timeout);
145-
_wifi.setAP();
146148
_apMode = true;
147149
IPAddress gw = _wifi.gatewayIP();
148150
if (!gw.isSet()) {

libraries/WiFi/src/WiFiClass.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <Arduino.h>
2626
#ifdef ARDUINO_RASPBERRY_PI_PICO_W
2727
#include <lwIP_CYW43.h>
28+
#elif defined(ESPHOSTSPI)
29+
#include <lwIP_ESPHost.h>
2830
#else
2931
#include "utility/lwIP_nodriver.h"
3032
#endif

libraries/lwIP_ESPHost/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# lwIP_ESPHost library
2+
3+
RP2040 Arduino integration with the ESPHost library.
4+
5+
[ESPHost library](https://github.com/JAndrassy/ESPHost) is a driver for communication with Espressif's esp-hosted-fg firmware.
6+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=lwIP_ESPHost
2+
version=1
3+
author=Juraj Andrassy
4+
maintainer=Earle F. Philhower, III <[email protected]>
5+
sentence=RP2040 ESPHost wifi driver
6+
paragraph=Driver for the esp-hosted-fg firmware with the ESPHosted library
7+
category=Communication
8+
url=https://github.com/earlephilhower/arduino-pico
9+
architectures=rp2040
10+
dot_a_linkage=true
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
WiFi <-> LWIP for ESPHost library in RP2040 Core
3+
4+
Copyright (c) 2024 Juraj Andrassy
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#include "ESPHost.h"
22+
#include "CEspControl.h"
23+
24+
ESPHost::ESPHost(int8_t cs, arduino::SPIClass &spi, int8_t intrpin) {
25+
(void) cs;
26+
(void) spi;
27+
(void) intrpin;
28+
// CS and SPI are configured in ESPHost library
29+
}
30+
31+
bool ESPHost::begin(const uint8_t *address, netif *netif) {
32+
(void) address;
33+
(void) netif;
34+
35+
// WiFi is initialized in ESPHostLwIP
36+
37+
return true;
38+
}
39+
40+
void ESPHost::end() {
41+
// WiFi is ended in ESPHostLwIP
42+
}
43+
44+
uint16_t ESPHost::sendFrame(const uint8_t *data, uint16_t datalen) {
45+
int res = CEspControl::getInstance().sendBuffer(apMode ? ESP_AP_IF : ESP_STA_IF, 0, (uint8_t*) data, datalen);
46+
CEspControl::getInstance().communicateWithEsp();
47+
return (res == ESP_CONTROL_OK) ? datalen : 0;
48+
}
49+
50+
uint16_t ESPHost::readFrameData(uint8_t *buffer, uint16_t bufsize) {
51+
uint8_t ifn = 0;
52+
uint16_t res;
53+
if (apMode) {
54+
res = CEspControl::getInstance().getSoftApRx(ifn, buffer, bufsize);
55+
} else {
56+
res = CEspControl::getInstance().getStationRx(ifn, buffer, bufsize);
57+
}
58+
return res;
59+
}
60+
61+
uint16_t ESPHost::readFrameSize() {
62+
CEspControl::getInstance().communicateWithEsp();
63+
uint16_t res;
64+
if (apMode) {
65+
res = CEspControl::getInstance().peekSoftApRxPayloadLen();
66+
} else {
67+
res = CEspControl::getInstance().peekStationRxPayloadLen();
68+
}
69+
return res;
70+
}

libraries/lwIP_ESPHost/src/ESPHost.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
WiFi <-> LWIP for ESPHost library in RP2040 Core
3+
4+
Copyright (c) 2024 Juraj Andrassy
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#pragma once
22+
23+
#include <Arduino.h>
24+
#include <SPI.h>
25+
26+
class ESPHost {
27+
public:
28+
29+
// constructor and methods as required by LwipIntfDev
30+
31+
ESPHost(int8_t cs, arduino::SPIClass &spi, int8_t intrpin);
32+
33+
bool begin(const uint8_t *address, netif *netif);
34+
void end();
35+
36+
uint16_t sendFrame(const uint8_t *data, uint16_t datalen);
37+
38+
uint16_t readFrameData(uint8_t *buffer, uint16_t bufsize);
39+
40+
uint16_t readFrameSize();
41+
42+
void discardFrame(uint16_t ign) {
43+
(void) ign;
44+
}
45+
46+
bool interruptIsPossible() {
47+
return false;
48+
}
49+
50+
constexpr bool needsSPI() const {
51+
return false;
52+
}
53+
54+
protected:
55+
bool apMode = false;
56+
57+
};

0 commit comments

Comments
 (0)