diff --git a/config/esp32/components/chip/Kconfig b/config/esp32/components/chip/Kconfig index 402d9ad64fe7fc..c127e732853105 100644 --- a/config/esp32/components/chip/Kconfig +++ b/config/esp32/components/chip/Kconfig @@ -516,6 +516,51 @@ menu "CHIP Device Layer" endmenu + menu "Ethernet Options" + visible if ENABLE_ETHERNET_TELEMETRY + + config GPIO_RANGE_MIN + int + default 0 + + config GPIO_RANGE_MAX + int + default 33 if IDF_TARGET_ESP32 + default 46 if IDF_TARGET_ESP32S2 + default 19 if IDF_TARGET_ESP32C3 + default 48 if IDF_TARGET_ESP32S3 + + config ETH_MDC_GPIO + int "SMI MDC GPIO number" + range GPIO_RANGE_MIN GPIO_RANGE_MAX + default 23 if ENABLE_ETHERNET_TELEMETRY + help + Set the GPIO number used by SMI MDC. + + config ETH_MDIO_GPIO + int "SMI MDIO GPIO number" + range GPIO_RANGE_MIN GPIO_RANGE_MAX + default 18 if ENABLE_ETHERNET_TELEMETRY + help + Set the GPIO number used by SMI MDIO. + + config ETH_PHY_RST_GPIO + int "PHY Reset GPIO number" + range -1 GPIO_RANGE_MAX + default 5 if ENABLE_ETHERNET_TELEMETRY + help + Set the GPIO number used to reset PHY chip. + Set to -1 to disable PHY chip hardware reset. + + config ETH_PHY_ADDR + int "PHY Address" + range 0 31 + default 1 if ENABLE_ETHERNET_TELEMETRY + help + Set PHY address according your board schematic. + + endmenu + menu "CHIP Trait Manager" config ENABLE_TRAIT_MANAGER @@ -724,6 +769,12 @@ menu "CHIP Device Layer" help Enable automatically uploading CHIP tunnel telemetry via trait on an interval. + config ENABLE_ETHERNET_TELEMETRY + bool "Enable Ethernet Telemetry" + default n + help + Enable ethernet support for boards with Internal Ethernet + endmenu menu "Event Logging Options" diff --git a/docs/guides/esp32/build_app_and_commission.md b/docs/guides/esp32/build_app_and_commission.md index cefbdf72cbd521..dc2d517803967e 100644 --- a/docs/guides/esp32/build_app_and_commission.md +++ b/docs/guides/esp32/build_app_and_commission.md @@ -26,6 +26,7 @@ the [ESP32-WROVER-KIT_V4.1](https://www.espressif.com/en/products/hardware/esp-wrover-kit/overview), the [M5Stack](http://m5stack.com), the [ESP32C3-DevKitM](https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/hw-reference/esp32c3/user-guide-devkitm-1.html), +[ESP32-Ethernet-Kit](https://docs.espressif.com/projects/esp-idf/en/latest/hw-reference/get-started-ethernet-kit.html) and the ESP32S3. All the applications support variants of ESP32, ESP32C3, ESP32S3 chips. @@ -190,6 +191,16 @@ $ out/debug/chip-tool pairing ble-wifi 12345 MY_SSID MY_PASSWORD 20202021 3840 $ ./out/debug/chip-tool pairing ble-thread 12345 hex: 20202021 3840 ``` +#### Commissioning the Ethernet device (ESP32-Ethernet-Kit) + +``` +$ out/debug/chip-tool pairing ethernet 12345 20202021 3840 device-remote-ip 5540 +``` + +Note: In order to commission an ethernet device, from all-clusters-app enable +these config options: select `ESP32-Ethernet-Kit` under `Demo->Device Type` and +select `On-Network` rendezvous mode under `Demo->Rendezvous Mode` + #### Commissioning Parameters - Node Id : 12345 (you can assign any node id) diff --git a/examples/all-clusters-app/esp32/main/Kconfig.projbuild b/examples/all-clusters-app/esp32/main/Kconfig.projbuild index dbf11ecaab5a57..43fcf6c47219ea 100644 --- a/examples/all-clusters-app/esp32/main/Kconfig.projbuild +++ b/examples/all-clusters-app/esp32/main/Kconfig.projbuild @@ -42,6 +42,10 @@ menu "Demo" config DEVICE_TYPE_ESP32_C3_DEVKITM bool "ESP32C3-DevKitM" depends on IDF_TARGET_ESP32C3 + config DEVICE_TYPE_ESP32_ETHERNET_KIT + bool "ESP32-Ethernet-Kit_A_V1.0" + select ENABLE_ETHERNET_TELEMETRY + depends on IDF_TARGET_ESP32 endchoice choice @@ -97,6 +101,7 @@ menu "Demo" int range 0 40 default 2 if DEVICE_TYPE_ESP32_DEVKITC #Use LED1 (blue LED) as status LED on DevKitC + default 2 if DEVICE_TYPE_ESP32_ETHERNET_KIT default 8 if DEVICE_TYPE_ESP32_C3_DEVKITM default 26 if DEVICE_TYPE_ESP32_WROVER_KIT default 40 if DEVICE_TYPE_M5STACK diff --git a/src/platform/ESP32/BUILD.gn b/src/platform/ESP32/BUILD.gn index 1100de4948e5ed..d3f8026ec43859 100644 --- a/src/platform/ESP32/BUILD.gn +++ b/src/platform/ESP32/BUILD.gn @@ -40,6 +40,7 @@ static_library("ESP32") { "ConfigurationManagerImpl.h", "ConnectivityManagerImpl.cpp", "ConnectivityManagerImpl.h", + "ConnectivityManagerImpl_Ethernet.cpp", "DeviceNetworkProvisioningDelegateImpl.cpp", "DeviceNetworkProvisioningDelegateImpl.h", "DiagnosticDataProviderImpl.cpp", diff --git a/src/platform/ESP32/CHIPDevicePlatformConfig.h b/src/platform/ESP32/CHIPDevicePlatformConfig.h index d2777360ee3922..47bb3f7b45fdf8 100644 --- a/src/platform/ESP32/CHIPDevicePlatformConfig.h +++ b/src/platform/ESP32/CHIPDevicePlatformConfig.h @@ -51,7 +51,10 @@ #define CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT CONFIG_OPENTHREAD_SRP_CLIENT #define CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT CONFIG_OPENTHREAD_SRP_CLIENT -#if CONFIG_IDF_TARGET_ESP32H2 +#if CONFIG_ENABLE_ETHERNET_TELEMETRY +#define CHIP_DEVICE_CONFIG_ENABLE_WIFI 0 +#define CHIP_DEVICE_CONFIG_ENABLE_ETHERNET 1 +#elif CONFIG_IDF_TARGET_ESP32H2 #define CHIP_DEVICE_CONFIG_ENABLE_WIFI 0 #else #define CHIP_DEVICE_CONFIG_WIFI_STATION_RECONNECT_INTERVAL CONFIG_WIFI_STATION_RECONNECT_INTERVAL diff --git a/src/platform/ESP32/CHIPDevicePlatformEvent.h b/src/platform/ESP32/CHIPDevicePlatformEvent.h index ae0908de40fded..c4e0df18f4accf 100644 --- a/src/platform/ESP32/CHIPDevicePlatformEvent.h +++ b/src/platform/ESP32/CHIPDevicePlatformEvent.h @@ -59,6 +59,7 @@ struct ChipDevicePlatformEvent final int32_t Id; union { + ip_event_t EthGotIp; ip_event_got_ip_t IpGotIp; ip_event_got_ip6_t IpGotIp6; ip_event_ap_staipassigned_t IpApStaIpAssigned; diff --git a/src/platform/ESP32/ConnectivityManagerImpl.cpp b/src/platform/ESP32/ConnectivityManagerImpl.cpp index 3c2eb0da17e43b..783ef4b99678be 100644 --- a/src/platform/ESP32/ConnectivityManagerImpl.cpp +++ b/src/platform/ESP32/ConnectivityManagerImpl.cpp @@ -63,6 +63,9 @@ CHIP_ERROR ConnectivityManagerImpl::_Init() #endif #if CHIP_DEVICE_CONFIG_ENABLE_WIFI InitWiFi(); +#endif +#if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET + InitEthernet(); #endif return CHIP_NO_ERROR; } @@ -75,6 +78,9 @@ void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event) #if CHIP_DEVICE_CONFIG_ENABLE_WIFI OnWiFiPlatformEvent(event); #endif +#if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET + OnEthernetPlatformEvent(event); +#endif } } // namespace DeviceLayer diff --git a/src/platform/ESP32/ConnectivityManagerImpl.h b/src/platform/ESP32/ConnectivityManagerImpl.h index 9c7f7774317318..3065180a372d94 100644 --- a/src/platform/ESP32/ConnectivityManagerImpl.h +++ b/src/platform/ESP32/ConnectivityManagerImpl.h @@ -96,6 +96,11 @@ class ConnectivityManagerImpl final : public ConnectivityManager, CHIP_ERROR _Init(void); void _OnPlatformEvent(const ChipDeviceEvent * event); +#if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET + CHIP_ERROR InitEthernet(void); + void OnEthernetPlatformEvent(const ChipDeviceEvent * event); +#endif + #if CHIP_DEVICE_CONFIG_ENABLE_WIFI using Flags = GenericConnectivityManagerImpl_WiFi::ConnectivityFlags; // ===== Members that implement the ConnectivityManager abstract interface. diff --git a/src/platform/ESP32/ConnectivityManagerImpl_Ethernet.cpp b/src/platform/ESP32/ConnectivityManagerImpl_Ethernet.cpp new file mode 100644 index 00000000000000..ed0a5f059f4106 --- /dev/null +++ b/src/platform/ESP32/ConnectivityManagerImpl_Ethernet.cpp @@ -0,0 +1,100 @@ +/* + * + * Copyright (c) 2020-2021 Project CHIP Authors + * Copyright (c) 2018 Nest Labs, Inc. + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* this file behaves like a config.h, comes first */ +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "esp_event.h" +#include "esp_netif.h" +#include "esp_wifi.h" + +#include +#include +#include +#include + +#if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET + +using namespace ::chip; +using namespace ::chip::Inet; +using namespace ::chip::System; +using namespace ::chip::TLV; +using chip::DeviceLayer::Internal::ESP32Utils; +namespace chip { +namespace DeviceLayer { + +CHIP_ERROR ConnectivityManagerImpl::InitEthernet() +{ + esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH(); + esp_netif_t * eth_netif = esp_netif_new(&cfg); + + // Init MAC and PHY configs to default + eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); + eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); + phy_config.phy_addr = CONFIG_ETH_PHY_ADDR; + phy_config.reset_gpio_num = CONFIG_ETH_PHY_RST_GPIO; + mac_config.smi_mdc_gpio_num = CONFIG_ETH_MDC_GPIO; + mac_config.smi_mdio_gpio_num = CONFIG_ETH_MDIO_GPIO; + esp_eth_mac_t * mac = esp_eth_mac_new_esp32(&mac_config); + esp_eth_phy_t * phy = esp_eth_phy_new_ip101(&phy_config); + + esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); + esp_eth_handle_t eth_handle = NULL; + ESP_ERROR_CHECK(esp_eth_driver_install(&config, ð_handle)); + /* attach Ethernet driver to TCP/IP stack */ + ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle))); + + ESP_ERROR_CHECK(esp_eth_start(eth_handle)); + return CHIP_NO_ERROR; +} + +void ConnectivityManagerImpl::OnEthernetPlatformEvent(const ChipDeviceEvent * event) +{ + switch (event->Platform.ESPSystemEvent.Id) + { + case IP_EVENT_ETH_GOT_IP: + ChipLogProgress(DeviceLayer, "Ethernet Link Up"); + break; + case IP_EVENT_ETH_LOST_IP: + ChipLogProgress(DeviceLayer, "Ethernet Link Down"); + break; + case ETHERNET_EVENT_START: + ChipLogProgress(DeviceLayer, "Ethernet Started"); + break; + case ETHERNET_EVENT_STOP: + ChipLogProgress(DeviceLayer, "Ethernet Stopped"); + break; + default: + break; + } +} + +} // namespace DeviceLayer +} // namespace chip +#endif // CHIP_DEVICE_CONFIG_ENABLE_ETHERNET