mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-03 19:38:30 +02:00
Initial Support for RP2040 platform (#3284)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
@@ -267,7 +267,7 @@ CONFIG_SCHEMA = cv.All(
|
||||
CONF_REBOOT_TIMEOUT, default="15min"
|
||||
): cv.positive_time_period_milliseconds,
|
||||
cv.SplitDefault(
|
||||
CONF_POWER_SAVE_MODE, esp8266="none", esp32="light"
|
||||
CONF_POWER_SAVE_MODE, esp8266="none", esp32="light", rp2040="light"
|
||||
): cv.enum(WIFI_POWER_SAVE_MODES, upper=True),
|
||||
cv.Optional(CONF_FAST_CONNECT, default=False): cv.boolean,
|
||||
cv.Optional(CONF_USE_ADDRESS): cv.string_strict,
|
||||
@@ -386,6 +386,8 @@ async def to_code(config):
|
||||
cg.add_library("ESP8266WiFi", None)
|
||||
elif CORE.is_esp32 and CORE.using_arduino:
|
||||
cg.add_library("WiFi", None)
|
||||
elif CORE.is_rp2040:
|
||||
cg.add_library("WiFi", None)
|
||||
|
||||
if CORE.is_esp32 and CORE.using_esp_idf:
|
||||
if config[CONF_ENABLE_BTM] or config[CONF_ENABLE_RRM]:
|
||||
|
||||
@@ -710,6 +710,8 @@ int8_t WiFiScanResult::get_rssi() const { return this->rssi_; }
|
||||
bool WiFiScanResult::get_with_auth() const { return this->with_auth_; }
|
||||
bool WiFiScanResult::get_is_hidden() const { return this->is_hidden_; }
|
||||
|
||||
bool WiFiScanResult::operator==(const WiFiScanResult &rhs) const { return this->bssid_ == rhs.bssid_; }
|
||||
|
||||
WiFiComponent *global_wifi_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
} // namespace wifi
|
||||
|
||||
@@ -24,6 +24,16 @@ extern "C" {
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef USE_RP2040
|
||||
extern "C" {
|
||||
#include "cyw43.h"
|
||||
#include "cyw43_country.h"
|
||||
#include "pico/cyw43_arch.h"
|
||||
}
|
||||
|
||||
#include <WiFi.h>
|
||||
#endif
|
||||
|
||||
namespace esphome {
|
||||
namespace wifi {
|
||||
|
||||
@@ -138,6 +148,8 @@ class WiFiScanResult {
|
||||
float get_priority() const { return priority_; }
|
||||
void set_priority(float priority) { priority_ = priority; }
|
||||
|
||||
bool operator==(const WiFiScanResult &rhs) const;
|
||||
|
||||
protected:
|
||||
bool matches_{false};
|
||||
bssid_t bssid_;
|
||||
@@ -310,6 +322,11 @@ class WiFiComponent : public Component {
|
||||
void wifi_process_event_(IDFWiFiEvent *data);
|
||||
#endif
|
||||
|
||||
#ifdef USE_RP2040
|
||||
static int s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result);
|
||||
void wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result);
|
||||
#endif
|
||||
|
||||
std::string use_address_;
|
||||
std::vector<WiFiAP> sta_;
|
||||
std::vector<WiFiSTAPriority> sta_priorities_;
|
||||
|
||||
@@ -0,0 +1,204 @@
|
||||
|
||||
#include "wifi_component.h"
|
||||
|
||||
#ifdef USE_RP2040
|
||||
|
||||
#include "lwip/dns.h"
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/netif.h"
|
||||
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/util.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace wifi {
|
||||
|
||||
static const char *const TAG = "wifi_pico_w";
|
||||
|
||||
bool WiFiComponent::wifi_mode_(optional<bool> sta, optional<bool> ap) {
|
||||
if (sta.has_value()) {
|
||||
if (sta.value()) {
|
||||
cyw43_wifi_set_up(&cyw43_state, CYW43_ITF_STA, true, CYW43_COUNTRY_WORLDWIDE);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WiFiComponent::wifi_apply_power_save_() {
|
||||
uint32_t pm;
|
||||
switch (this->power_save_) {
|
||||
case WIFI_POWER_SAVE_NONE:
|
||||
pm = CYW43_PERFORMANCE_PM;
|
||||
break;
|
||||
case WIFI_POWER_SAVE_LIGHT:
|
||||
pm = CYW43_DEFAULT_PM;
|
||||
break;
|
||||
case WIFI_POWER_SAVE_HIGH:
|
||||
pm = CYW43_AGGRESSIVE_PM;
|
||||
break;
|
||||
}
|
||||
int ret = cyw43_wifi_pm(&cyw43_state, pm);
|
||||
return ret == 0;
|
||||
}
|
||||
|
||||
// TODO: The driver doesnt seem to have an API for this
|
||||
bool WiFiComponent::wifi_apply_output_power_(float output_power) { return true; }
|
||||
|
||||
bool WiFiComponent::wifi_sta_connect_(const WiFiAP &ap) {
|
||||
if (!this->wifi_sta_ip_config_(ap.get_manual_ip()))
|
||||
return false;
|
||||
|
||||
auto ret = WiFi.begin(ap.get_ssid().c_str(), ap.get_password().c_str());
|
||||
if (ret != WL_CONNECTED)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WiFiComponent::wifi_sta_pre_setup_() { return this->wifi_mode_(true, {}); }
|
||||
|
||||
bool WiFiComponent::wifi_sta_ip_config_(optional<ManualIP> manual_ip) {
|
||||
if (!manual_ip.has_value()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
IPAddress ip_address = IPAddress(manual_ip->static_ip);
|
||||
IPAddress gateway = IPAddress(manual_ip->gateway);
|
||||
IPAddress subnet = IPAddress(manual_ip->subnet);
|
||||
|
||||
IPAddress dns = IPAddress(manual_ip->dns1);
|
||||
|
||||
WiFi.config(ip_address, dns, gateway, subnet);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WiFiComponent::wifi_apply_hostname_() {
|
||||
WiFi.setHostname(App.get_name().c_str());
|
||||
return true;
|
||||
}
|
||||
const char *get_auth_mode_str(uint8_t mode) {
|
||||
// TODO:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
const char *get_disconnect_reason_str(uint8_t reason) {
|
||||
// TODO:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
|
||||
WiFiSTAConnectStatus WiFiComponent::wifi_sta_connect_status_() {
|
||||
int status = cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA);
|
||||
switch (status) {
|
||||
case CYW43_LINK_JOIN:
|
||||
case CYW43_LINK_NOIP:
|
||||
return WiFiSTAConnectStatus::CONNECTING;
|
||||
case CYW43_LINK_UP:
|
||||
return WiFiSTAConnectStatus::CONNECTED;
|
||||
case CYW43_LINK_FAIL:
|
||||
case CYW43_LINK_BADAUTH:
|
||||
return WiFiSTAConnectStatus::ERROR_CONNECT_FAILED;
|
||||
case CYW43_LINK_NONET:
|
||||
return WiFiSTAConnectStatus::ERROR_NETWORK_NOT_FOUND;
|
||||
}
|
||||
return WiFiSTAConnectStatus::IDLE;
|
||||
}
|
||||
|
||||
int WiFiComponent::s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result) {
|
||||
global_wifi_component->wifi_scan_result(env, result);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void WiFiComponent::wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result) {
|
||||
bssid_t bssid;
|
||||
std::copy(result->bssid, result->bssid + 6, bssid.begin());
|
||||
std::string ssid(reinterpret_cast<const char *>(result->ssid));
|
||||
WiFiScanResult res(bssid, ssid, result->channel, result->rssi, result->auth_mode != CYW43_AUTH_OPEN, ssid.empty());
|
||||
if (std::find(this->scan_result_.begin(), this->scan_result_.end(), res) == this->scan_result_.end()) {
|
||||
this->scan_result_.push_back(res);
|
||||
}
|
||||
}
|
||||
|
||||
bool WiFiComponent::wifi_scan_start_() {
|
||||
this->scan_result_.clear();
|
||||
this->scan_done_ = false;
|
||||
cyw43_wifi_scan_options_t scan_options = {0};
|
||||
int err = cyw43_wifi_scan(&cyw43_state, &scan_options, nullptr, &s_wifi_scan_result);
|
||||
if (err) {
|
||||
ESP_LOGV(TAG, "cyw43_wifi_scan failed!");
|
||||
}
|
||||
return err == 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WiFiComponent::wifi_ap_ip_config_(optional<ManualIP> manual_ip) {
|
||||
// TODO:
|
||||
return false;
|
||||
}
|
||||
|
||||
bool WiFiComponent::wifi_start_ap_(const WiFiAP &ap) {
|
||||
if (!this->wifi_mode_({}, true))
|
||||
return false;
|
||||
|
||||
if (ap.get_channel().has_value()) {
|
||||
cyw43_wifi_ap_set_channel(&cyw43_state, ap.get_channel().value());
|
||||
}
|
||||
|
||||
const char *ssid = ap.get_ssid().c_str();
|
||||
|
||||
cyw43_wifi_ap_set_ssid(&cyw43_state, strlen(ssid), (const uint8_t *) ssid);
|
||||
|
||||
if (!ap.get_password().empty()) {
|
||||
const char *password = ap.get_password().c_str();
|
||||
cyw43_wifi_ap_set_password(&cyw43_state, strlen(password), (const uint8_t *) password);
|
||||
cyw43_wifi_ap_set_auth(&cyw43_state, CYW43_AUTH_WPA2_MIXED_PSK);
|
||||
} else {
|
||||
cyw43_wifi_ap_set_auth(&cyw43_state, CYW43_AUTH_OPEN);
|
||||
}
|
||||
cyw43_wifi_set_up(&cyw43_state, CYW43_ITF_AP, true, CYW43_COUNTRY_WORLDWIDE);
|
||||
|
||||
return true;
|
||||
}
|
||||
network::IPAddress WiFiComponent::wifi_soft_ap_ip() { return {WiFi.localIP()}; }
|
||||
|
||||
bool WiFiComponent::wifi_disconnect_() {
|
||||
int err = cyw43_wifi_leave(&cyw43_state, CYW43_ITF_STA);
|
||||
return err == 0;
|
||||
}
|
||||
// NOTE: The driver does not provide an interface to get this
|
||||
bssid_t WiFiComponent::wifi_bssid() {
|
||||
bssid_t bssid{};
|
||||
uint8_t raw_bssid[6];
|
||||
WiFi.BSSID(raw_bssid);
|
||||
for (size_t i = 0; i < bssid.size(); i++)
|
||||
bssid[i] = raw_bssid[i];
|
||||
return bssid;
|
||||
}
|
||||
// NOTE: The driver does not provide an interface to get this
|
||||
std::string WiFiComponent::wifi_ssid() { return WiFi.SSID(); }
|
||||
// NOTE: The driver does not provide an interface to get this
|
||||
int8_t WiFiComponent::wifi_rssi() { return WiFi.RSSI(); }
|
||||
// NOTE: The driver does not provide an interface to get this
|
||||
int32_t WiFiComponent::wifi_channel_() { return 0; }
|
||||
network::IPAddress WiFiComponent::wifi_sta_ip() { return {WiFi.localIP()}; }
|
||||
network::IPAddress WiFiComponent::wifi_subnet_mask_() { return {WiFi.subnetMask()}; }
|
||||
network::IPAddress WiFiComponent::wifi_gateway_ip_() { return {WiFi.gatewayIP()}; }
|
||||
network::IPAddress WiFiComponent::wifi_dns_ip_(int num) {
|
||||
const ip_addr_t *dns_ip = dns_getserver(num);
|
||||
return {dns_ip->addr};
|
||||
}
|
||||
|
||||
void WiFiComponent::wifi_loop_() {
|
||||
if (this->state_ == WIFI_COMPONENT_STATE_STA_SCANNING && !cyw43_wifi_scan_active(&cyw43_state)) {
|
||||
this->scan_done_ = true;
|
||||
ESP_LOGV(TAG, "Scan done!");
|
||||
}
|
||||
}
|
||||
|
||||
void WiFiComponent::wifi_pre_setup_() {}
|
||||
|
||||
} // namespace wifi
|
||||
} // namespace esphome
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user