Skip to content

Commit

Permalink
Add ethernet support to all-clusters-app/esp32 (project-chip#22345)
Browse files Browse the repository at this point in the history
  • Loading branch information
PSONALl authored and isiu-apple committed Sep 16, 2022
1 parent e3a0118 commit 5a6244e
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 1 deletion.
51 changes: 51 additions & 0 deletions config/esp32/components/chip/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
11 changes: 11 additions & 0 deletions docs/guides/esp32/build_app_and_commission.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:<operational-dataset> 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)
Expand Down
5 changes: 5 additions & 0 deletions examples/all-clusters-app/esp32/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/platform/ESP32/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ static_library("ESP32") {
"ConfigurationManagerImpl.h",
"ConnectivityManagerImpl.cpp",
"ConnectivityManagerImpl.h",
"ConnectivityManagerImpl_Ethernet.cpp",
"DeviceNetworkProvisioningDelegateImpl.cpp",
"DeviceNetworkProvisioningDelegateImpl.h",
"DiagnosticDataProviderImpl.cpp",
Expand Down
5 changes: 4 additions & 1 deletion src/platform/ESP32/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/platform/ESP32/CHIPDevicePlatformEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/platform/ESP32/ConnectivityManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/platform/ESP32/ConnectivityManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
100 changes: 100 additions & 0 deletions src/platform/ESP32/ConnectivityManagerImpl_Ethernet.cpp
Original file line number Diff line number Diff line change
@@ -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 <platform/internal/CHIPDeviceLayerInternal.h>

#include <platform/CommissionableDataProvider.h>
#include <platform/ConnectivityManager.h>

#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/DeviceInstanceInfoProvider.h>
#include <platform/DiagnosticDataProvider.h>
#include <platform/ESP32/ESP32Utils.h>
#include <platform/ESP32/NetworkCommissioningDriver.h>
#include <platform/internal/BLEManager.h>

#include "esp_event.h"
#include "esp_netif.h"
#include "esp_wifi.h"

#include <lwip/dns.h>
#include <lwip/ip_addr.h>
#include <lwip/nd6.h>
#include <lwip/netif.h>

#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, &eth_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

0 comments on commit 5a6244e

Please sign in to comment.