network_driver_LAN8720.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "esp_eth.h"
  2. #include "network_ethernet.h"
  3. static EXT_RAM_ATTR network_ethernet_driver_t LAN8720;
  4. static EXT_RAM_ATTR esp_netif_config_t cfg_rmii;
  5. static EXT_RAM_ATTR esp_netif_inherent_config_t esp_netif_config;
  6. static esp_err_t start(spi_device_handle_t spi_handle, sys_Eth * ethernet_config) {
  7. #ifdef CONFIG_ETH_PHY_INTERFACE_RMII
  8. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  9. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  10. mac_config.smi_mdc_gpio_num = ethernet_config->ethType.rmii.has_mdc?ethernet_config->ethType.rmii.mdc.pin:-1;
  11. mac_config.smi_mdio_gpio_num = ethernet_config->ethType.rmii.has_mdio?ethernet_config->ethType.rmii.mdio.pin:-1;
  12. phy_config.phy_addr = 1;
  13. phy_config.reset_gpio_num = ethernet_config->common.has_rst?ethernet_config->common.rst.pin:-1;
  14. esp_eth_mac_t* mac = esp_eth_mac_new_esp32(&mac_config);
  15. esp_eth_phy_t* phy = esp_eth_phy_new_lan8720(&phy_config);
  16. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
  17. return esp_eth_driver_install(&config, &LAN8720.handle);
  18. #else
  19. return ESP_ERR_NOT_SUPPORTED;
  20. #endif
  21. }
  22. static void init_config(sys_Eth * ethernet_config) {
  23. esp_netif_inherent_config_t loc_esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
  24. memcpy(&esp_netif_config, &loc_esp_netif_config, sizeof(loc_esp_netif_config));
  25. cfg_rmii.base = &esp_netif_config,
  26. cfg_rmii.stack = ESP_NETIF_NETSTACK_DEFAULT_ETH;
  27. LAN8720.cfg_netif = &cfg_rmii;
  28. LAN8720.start = start;
  29. }
  30. network_ethernet_driver_t* LAN8720_Detect(sys_Eth * ethernet_config) {
  31. if (ethernet_config->common.model != sys_EthModelEnum_LAN8720 ||
  32. ethernet_config->which_ethType != sys_Eth_rmii_tag)
  33. return NULL;
  34. #ifdef CONFIG_ETH_PHY_INTERFACE_RMII
  35. LAN8720.valid = true;
  36. #else
  37. LAN8720.valid = false;
  38. #endif
  39. LAN8720.rmii = true;
  40. LAN8720.spi = false;
  41. LAN8720.model = ethernet_config->common.model;
  42. LAN8720.init_config = init_config;
  43. return &LAN8720;
  44. }