29
29
#include " esp_eth_phy.h"
30
30
#include " esp_eth_mac.h"
31
31
#include " esp_eth_com.h"
32
+ #if CONFIG_ETH_USE_SPI_ETHERNET
33
+ #include " driver/spi_master.h"
34
+ #endif
32
35
#if CONFIG_IDF_TARGET_ESP32
33
36
#include " soc/emac_ext_struct.h"
34
37
#include " soc/rtc.h"
@@ -255,6 +258,31 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ
255
258
return false ;// todo
256
259
} else {
257
260
#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
258
286
#if CONFIG_ETH_USE_ESP32_EMAC
259
287
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG ();
260
288
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
264
292
mac_config.sw_reset_timeout_ms = 1000 ;
265
293
eth_mac = esp_eth_mac_new_esp32 (&mac_config);
266
294
#endif
295
+ #if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL
296
+ }
297
+ #endif
267
298
#if CONFIG_ETH_SPI_ETHERNET_DM9051
268
299
}
269
300
#endif
@@ -297,6 +328,12 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ
297
328
case ETH_PHY_DM9051:
298
329
eth_phy = esp_eth_phy_new_dm9051 (&phy_config);
299
330
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 ;
300
337
#endif
301
338
case ETH_PHY_KSZ8041:
302
339
#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
328
365
log_e (" esp_eth_driver_install failed" );
329
366
return false ;
330
367
}
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
+
332
379
/* attach Ethernet driver to TCP/IP stack */
333
380
if (esp_netif_attach (eth_netif, esp_eth_new_netif_glue (eth_handle)) != ESP_OK){
334
381
log_e (" esp_netif_attach failed" );
0 commit comments