network_driver_W5500.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "esp_eth.h"
  2. #include "network_ethernet.h"
  3. static EXT_RAM_ATTR network_ethernet_driver_t W5500;
  4. static EXT_RAM_ATTR spi_device_interface_config_t devcfg;
  5. static EXT_RAM_ATTR esp_netif_config_t cfg_spi;
  6. static EXT_RAM_ATTR esp_netif_inherent_config_t esp_netif_config;
  7. static esp_err_t start(spi_device_handle_t spi_handle, sys_Eth * ethernet_config) {
  8. #ifdef CONFIG_ETH_SPI_ETHERNET_W5500
  9. eth_w5500_config_t eth_config = ETH_W5500_DEFAULT_CONFIG(spi_handle);
  10. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  11. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  12. eth_config.int_gpio_num = ethernet_config->ethType.spi.has_intr?ethernet_config->ethType.spi.intr.pin:-1;
  13. phy_config.phy_addr = -1; // let the system automatically find out the phy address
  14. phy_config.reset_gpio_num = ethernet_config->common.has_rst?ethernet_config->common.rst.pin:-1;
  15. esp_eth_mac_t* mac = esp_eth_mac_new_w5500(&eth_config, &mac_config);
  16. esp_eth_phy_t* phy = esp_eth_phy_new_w5500(&phy_config);
  17. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
  18. return esp_eth_driver_install(&config, &W5500.handle);
  19. #else
  20. return ESP_ERR_NOT_SUPPORTED;
  21. #endif
  22. }
  23. static void init_config(sys_Eth * ethernet_config) {
  24. // This function is called when the network interface is started
  25. // and performs any initialization that requires a valid ethernet
  26. // configuration .
  27. esp_netif_inherent_config_t loc_esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
  28. devcfg.command_bits = 16; // Actually it's the address phase in W5500 SPI frame
  29. devcfg.address_bits = 8; // Actually it's the control phase in W5500 SPI frame
  30. devcfg.mode = 0;
  31. devcfg.clock_speed_hz = ethernet_config->ethType.spi.speed > 0 ? ethernet_config->ethType.spi.speed : SPI_MASTER_FREQ_20M; // default speed
  32. devcfg.queue_size = 20;
  33. devcfg.spics_io_num = ethernet_config->ethType.spi.has_cs?ethernet_config->ethType.spi.cs.pin:-1;
  34. memcpy(&esp_netif_config, &loc_esp_netif_config, sizeof(loc_esp_netif_config));
  35. cfg_spi.base = &esp_netif_config,
  36. cfg_spi.stack = ESP_NETIF_NETSTACK_DEFAULT_ETH;
  37. W5500.cfg_netif = &cfg_spi;
  38. W5500.devcfg = &devcfg;
  39. W5500.start = start;
  40. }
  41. network_ethernet_driver_t* W5500_Detect(sys_Eth * ethernet_config) {
  42. if (ethernet_config->common.model != sys_EthModelEnum_W5500 ||
  43. ethernet_config->which_ethType != sys_Eth_spi_tag )
  44. return NULL;
  45. W5500.init_config = init_config;
  46. W5500.spi = true;
  47. W5500.rmii = false;
  48. W5500.model = ethernet_config->common.model;
  49. #ifdef CONFIG_ETH_SPI_ETHERNET_W5500
  50. W5500.valid = true;
  51. #else
  52. W5500.valid = false;
  53. #endif
  54. return &W5500;
  55. }