Skip to content

Commit

Permalink
Add RMII Ethernet support for ESP32-P4 (#10479)
Browse files Browse the repository at this point in the history
* fix(eth): Add ETH support for ESP32-P4

Also adds configuration to pins_arduino.h

* fix(eth): Enable TLK110 Example on P4

* fix(eth): Fix Ethernet examples

* ci(pre-commit): Apply automatic fixes

---------

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
  • Loading branch information
me-no-dev and pre-commit-ci-lite[bot] authored Oct 17, 2024
1 parent 612fc4c commit ee1bc6b
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 16 deletions.
7 changes: 5 additions & 2 deletions boards.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ esp32p4.build.variant=esp32p4
esp32p4.build.board=ESP32P4_DEV
esp32p4.build.bootloader_addr=0x2000

esp32p4.build.usb_mode=0
esp32p4.build.cdc_on_boot=0
esp32p4.build.msc_on_boot=0
esp32p4.build.dfu_on_boot=0
esp32p4.build.f_cpu=360000000L
esp32p4.build.flash_size=4MB
esp32p4.build.flash_freq=80m
Expand Down Expand Up @@ -216,10 +219,10 @@ esp32p4.menu.PSRAM.disabled.build.defines=
esp32p4.menu.PSRAM.enabled=Enabled
esp32p4.menu.PSRAM.enabled.build.defines=-DBOARD_HAS_PSRAM

esp32p4.menu.USBMode.hwcdc=Hardware CDC and JTAG
esp32p4.menu.USBMode.hwcdc.build.usb_mode=1
esp32p4.menu.USBMode.default=USB-OTG (TinyUSB)
esp32p4.menu.USBMode.default.build.usb_mode=0
esp32p4.menu.USBMode.hwcdc=Hardware CDC and JTAG
esp32p4.menu.USBMode.hwcdc.build.usb_mode=1

esp32p4.menu.CDCOnBoot.default=Disabled
esp32p4.menu.CDCOnBoot.default.build.cdc_on_boot=0
Expand Down
12 changes: 10 additions & 2 deletions libraries/Ethernet/examples/ETH_LAN8720/ETH_LAN8720.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@

// Important to be defined BEFORE including ETH.h for ETH.begin() to work.
// Example RMII LAN8720 (Olimex, etc.)
#ifndef ETH_PHY_TYPE
#define ETH_PHY_TYPE ETH_PHY_LAN8720
#ifndef ETH_PHY_MDC
#define ETH_PHY_TYPE ETH_PHY_LAN8720
#if CONFIG_IDF_TARGET_ESP32
#define ETH_PHY_ADDR 0
#define ETH_PHY_MDC 23
#define ETH_PHY_MDIO 18
#define ETH_PHY_POWER -1
#define ETH_CLK_MODE ETH_CLOCK_GPIO0_IN
#elif CONFIG_IDF_TARGET_ESP32P4
#define ETH_PHY_ADDR 0
#define ETH_PHY_MDC 31
#define ETH_PHY_MDIO 52
#define ETH_PHY_POWER 51
#define ETH_CLK_MODE EMAC_CLK_EXT_IN
#endif
#endif

#include <ETH.h>
Expand Down
12 changes: 10 additions & 2 deletions libraries/Ethernet/examples/ETH_TLK110/ETH_TLK110.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@

#include <ETH.h>

#ifndef ETH_PHY_TYPE
#define ETH_PHY_TYPE ETH_PHY_TLK110
#ifndef ETH_PHY_MDC
#define ETH_PHY_TYPE ETH_PHY_TLK110
#if CONFIG_IDF_TARGET_ESP32
#define ETH_PHY_ADDR 31
#define ETH_PHY_MDC 23
#define ETH_PHY_MDIO 18
#define ETH_PHY_POWER 17
#define ETH_CLK_MODE ETH_CLOCK_GPIO0_IN
#elif CONFIG_IDF_TARGET_ESP32P4
#define ETH_PHY_ADDR 1
#define ETH_PHY_MDC 31
#define ETH_PHY_MDIO 52
#define ETH_PHY_POWER 51
#define ETH_CLK_MODE EMAC_CLK_EXT_IN
#endif
#endif

static bool eth_connected = false;
Expand Down
5 changes: 1 addition & 4 deletions libraries/Ethernet/examples/ETH_TLK110/ci.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
{
"requires": [
"CONFIG_ETH_USE_ESP32_EMAC=y"
],
"targets": {
"esp32p4": false
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// Set this to 1 to enable dual Ethernet support
#define USE_TWO_ETH_PORTS 0

#ifndef ETH_PHY_TYPE
#ifndef ETH_PHY_CS
#define ETH_PHY_TYPE ETH_PHY_W5500
#define ETH_PHY_ADDR 1
#define ETH_PHY_CS 15
Expand All @@ -24,7 +24,7 @@

#if USE_TWO_ETH_PORTS
// Second port on shared SPI bus
#ifndef ETH1_PHY_TYPE
#ifndef ETH1_PHY_CS
#define ETH1_PHY_TYPE ETH_PHY_W5500
#define ETH1_PHY_ADDR 1
#define ETH1_PHY_CS 32
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// Set this to 1 to enable dual Ethernet support
#define USE_TWO_ETH_PORTS 0

#ifndef ETH_PHY_TYPE
#ifndef ETH_PHY_CS
#define ETH_PHY_TYPE ETH_PHY_W5500
#define ETH_PHY_ADDR 1
#define ETH_PHY_CS 15
Expand All @@ -22,7 +22,7 @@

#if USE_TWO_ETH_PORTS
// Second port on shared SPI bus
#ifndef ETH1_PHY_TYPE
#ifndef ETH1_PHY_CS
#define ETH1_PHY_TYPE ETH_PHY_W5500
#define ETH1_PHY_ADDR 1
#define ETH1_PHY_CS 32
Expand Down
28 changes: 26 additions & 2 deletions libraries/Ethernet/src/ETH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,27 @@ void ETHClass::setTaskStackSize(size_t size) {
_task_stack_size = size;
}

#if (CONFIG_ETH_USE_ESP32_EMAC && !defined(CONFIG_IDF_TARGET_ESP32P4))
#if CONFIG_ETH_USE_ESP32_EMAC
#if CONFIG_IDF_TARGET_ESP32
#define ETH_EMAC_DEFAULT_CONFIG() ETH_ESP32_EMAC_DEFAULT_CONFIG()
#elif CONFIG_IDF_TARGET_ESP32P4
#define ETH_EMAC_DEFAULT_CONFIG() \
{ \
.smi_gpio = {.mdc_num = 31, .mdio_num = 52}, .interface = EMAC_DATA_INTERFACE_RMII, \
.clock_config = {.rmii = {.clock_mode = EMAC_CLK_EXT_IN, .clock_gpio = (emac_rmii_clock_gpio_t)ETH_RMII_CLK}}, .dma_burst_len = ETH_DMA_BURST_LEN_32, \
.intr_priority = 0, \
.emac_dataif_gpio = \
{.rmii = \
{.tx_en_num = ETH_RMII_TX_EN, \
.txd0_num = ETH_RMII_TX0, \
.txd1_num = ETH_RMII_TX1, \
.crs_dv_num = ETH_RMII_CRS_DV, \
.rxd0_num = ETH_RMII_RX0, \
.rxd1_num = ETH_RMII_RX1_EN}}, \
.clock_config_out_in = {.rmii = {.clock_mode = EMAC_CLK_EXT_IN, .clock_gpio = (emac_rmii_clock_gpio_t) - 1}}, \
}
#endif

bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, int power, eth_clock_mode_t clock_mode) {
esp_err_t ret = ESP_OK;
if (_eth_index > 2) {
Expand Down Expand Up @@ -177,12 +197,16 @@ bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, i
#define DEFAULT_RMII_CLK_GPIO (emac_rmii_clock_gpio_t)(CONFIG_ETH_RMII_CLK_IN_GPIO)
#endif

eth_esp32_emac_config_t mac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
eth_esp32_emac_config_t mac_config = ETH_EMAC_DEFAULT_CONFIG();
#if CONFIG_IDF_TARGET_ESP32
mac_config.clock_config.rmii.clock_mode = (clock_mode) ? EMAC_CLK_OUT : EMAC_CLK_EXT_IN;
mac_config.clock_config.rmii.clock_gpio = (1 == clock_mode) ? EMAC_APPL_CLK_OUT_GPIO
: (2 == clock_mode) ? EMAC_CLK_OUT_GPIO
: (3 == clock_mode) ? EMAC_CLK_OUT_180_GPIO
: EMAC_CLK_IN_GPIO;
#elif CONFIG_IDF_TARGET_ESP32P4
mac_config.clock_config.rmii.clock_mode = (emac_rmii_clock_mode_t)clock_mode;
#endif
mac_config.smi_gpio.mdc_num = digitalPinToGPIONumber(mdc);
mac_config.smi_gpio.mdio_num = digitalPinToGPIONumber(mdio);

Expand Down
26 changes: 26 additions & 0 deletions libraries/Ethernet/src/ETH.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@

#if CONFIG_ETH_USE_ESP32_EMAC
#define ETH_PHY_IP101 ETH_PHY_TLK110
#if CONFIG_IDF_TARGET_ESP32
typedef enum {
ETH_CLOCK_GPIO0_IN,
ETH_CLOCK_GPIO0_OUT,
Expand All @@ -88,6 +89,31 @@ typedef enum {
#define ETH_RMII_RX0 25
#define ETH_RMII_RX1_EN 26
#define ETH_RMII_CRS_DV 27
#elif CONFIG_IDF_TARGET_ESP32P4
typedef emac_rmii_clock_mode_t eth_clock_mode_t;
#include "pins_arduino.h"
#ifndef ETH_RMII_TX_EN
#define ETH_RMII_TX_EN 49
#endif
#ifndef ETH_RMII_TX0
#define ETH_RMII_TX0 34
#endif
#ifndef ETH_RMII_TX1
#define ETH_RMII_TX1 35
#endif
#ifndef ETH_RMII_RX0
#define ETH_RMII_RX0 29
#endif
#ifndef ETH_RMII_RX1_EN
#define ETH_RMII_RX1_EN 30
#endif
#ifndef ETH_RMII_CRS_DV
#define ETH_RMII_CRS_DV 28
#endif
#ifndef ETH_RMII_CLK
#define ETH_RMII_CLK 50
#endif
#endif
#endif /* CONFIG_ETH_USE_ESP32_EMAC */

#ifndef ETH_PHY_SPI_FREQ_MHZ
Expand Down
14 changes: 14 additions & 0 deletions variants/esp32p4/pins_arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,18 @@ static const uint8_t T11 = 13;
static const uint8_t T12 = 14;
static const uint8_t T13 = 15;

#define ETH_PHY_TYPE ETH_PHY_TLK110
#define ETH_PHY_ADDR 1
#define ETH_PHY_MDC 31
#define ETH_PHY_MDIO 52
#define ETH_PHY_POWER 51
#define ETH_RMII_TX_EN 49
#define ETH_RMII_TX0 34
#define ETH_RMII_TX1 35
#define ETH_RMII_RX0 29
#define ETH_RMII_RX1_EN 30
#define ETH_RMII_CRS_DV 28
#define ETH_RMII_CLK 50
#define ETH_CLK_MODE EMAC_CLK_EXT_IN

#endif /* Pins_Arduino_h */

0 comments on commit ee1bc6b

Please sign in to comment.