Skip to content

Commit 85f06c9

Browse files
committed
Support KSZ8851SNL SPI Ethernet MAC+PHY in Arduino.
1 parent f55b91e commit 85f06c9

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

libraries/Ethernet/src/ETH.cpp

+48-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
#include "esp_eth_phy.h"
3030
#include "esp_eth_mac.h"
3131
#include "esp_eth_com.h"
32+
#if CONFIG_ETH_USE_SPI_ETHERNET
33+
#include "driver/spi_master.h"
34+
#endif
3235
#if CONFIG_IDF_TARGET_ESP32
3336
#include "soc/emac_ext_struct.h"
3437
#include "soc/rtc.h"
@@ -255,6 +258,31 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ
255258
return false;//todo
256259
} else {
257260
#endif
261+
#if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL
262+
if(type == ETH_PHY_KSZ8851SNL){
263+
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); // apply default common MAC configuration
264+
spi_bus_config_t buscfg = {
265+
.mosi_io_num = MOSI,
266+
.miso_io_num = MISO,
267+
.sclk_io_num = SCK,
268+
.quadwp_io_num = -1,
269+
.quadhd_io_num = -1,
270+
};
271+
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO));
272+
// Configure SPI device
273+
spi_device_interface_config_t spi_devcfg = {
274+
.mode = 0,
275+
.clock_speed_hz = ETH_SPI_SPEED_MHZ * 1000 * 1000,
276+
.spics_io_num = SS,
277+
.queue_size = 20
278+
};
279+
spi_device_handle_t spi_handle;
280+
ESP_ERROR_CHECK(spi_bus_add_device(SPI2_HOST, &spi_devcfg, &spi_handle));
281+
eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(spi_handle);
282+
ksz8851snl_config.int_gpio_num = mdio;
283+
eth_mac = esp_eth_mac_new_ksz8851snl(&ksz8851snl_config, &mac_config);
284+
} else {
285+
#endif
258286
#if CONFIG_ETH_USE_ESP32_EMAC
259287
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
260288
mac_config.clock_config.rmii.clock_mode = (eth_clock_mode) ? EMAC_CLK_OUT : EMAC_CLK_EXT_IN;
@@ -264,6 +292,9 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ
264292
mac_config.sw_reset_timeout_ms = 1000;
265293
eth_mac = esp_eth_mac_new_esp32(&mac_config);
266294
#endif
295+
#if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL
296+
}
297+
#endif
267298
#if CONFIG_ETH_SPI_ETHERNET_DM9051
268299
}
269300
#endif
@@ -297,6 +328,12 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ
297328
case ETH_PHY_DM9051:
298329
eth_phy = esp_eth_phy_new_dm9051(&phy_config);
299330
break;
331+
#endif
332+
#if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL
333+
case ETH_PHY_KSZ8851SNL:
334+
gpio_install_isr_service(0);
335+
eth_phy = esp_eth_phy_new_ksz8851snl(&phy_config);
336+
break;
300337
#endif
301338
case ETH_PHY_KSZ8041:
302339
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4,4,0)
@@ -328,7 +365,17 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ
328365
log_e("esp_eth_driver_install failed");
329366
return false;
330367
}
331-
368+
369+
#if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL
370+
if (type == ETH_PHY_KSZ8851SNL && !use_mac_from_efuse)
371+
{
372+
uint8_t mac[] = { 0x02, 0x00, 0x00, 0x00, 0x00, 0x00 };
373+
esp_efuse_mac_get_default(mac);
374+
mac[5] += 3;
375+
ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle, ETH_CMD_S_MAC_ADDR, mac));
376+
}
377+
#endif
378+
332379
/* attach Ethernet driver to TCP/IP stack */
333380
if(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)) != ESP_OK){
334381
log_e("esp_netif_attach failed");

libraries/Ethernet/src/ETH.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@
5252
#define ETH_CLK_MODE ETH_CLOCK_GPIO0_IN
5353
#endif
5454

55+
#ifndef ETH_SPI_SPEED_MHZ
56+
#define ETH_SPI_SPEED_MHZ 40
57+
#endif
58+
5559
#if ESP_IDF_VERSION_MAJOR > 3
5660
typedef enum { ETH_CLOCK_GPIO0_IN, ETH_CLOCK_GPIO0_OUT, ETH_CLOCK_GPIO16_OUT, ETH_CLOCK_GPIO17_OUT } eth_clock_mode_t;
5761
#endif
5862

59-
typedef enum { ETH_PHY_LAN8720, ETH_PHY_LAN8742, ETH_PHY_TLK110, ETH_PHY_RTL8201, ETH_PHY_DP83848, ETH_PHY_DM9051, ETH_PHY_KSZ8041, ETH_PHY_KSZ8081, ETH_PHY_MAX } eth_phy_type_t;
63+
typedef enum { ETH_PHY_LAN8720, ETH_PHY_LAN8742, ETH_PHY_TLK110, ETH_PHY_RTL8201, ETH_PHY_DP83848, ETH_PHY_DM9051, ETH_PHY_KSZ8041, ETH_PHY_KSZ8081, ETH_PHY_KSZ8851SNL, ETH_PHY_MAX } eth_phy_type_t;
6064
#define ETH_PHY_IP101 ETH_PHY_TLK110
6165

6266
class ETHClass {

0 commit comments

Comments
 (0)