2
0

network_driver_W5500.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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, eth_config_t* 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->intr;
  13. phy_config.phy_addr = -1; // let the system automatically find out the phy address
  14. phy_config.reset_gpio_num = ethernet_config->rst;
  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(eth_config_t* 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->speed > 0 ? ethernet_config->speed : SPI_MASTER_FREQ_20M; // default speed
  32. devcfg.queue_size = 20;
  33. devcfg.spics_io_num = ethernet_config->cs;
  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(char* Driver, network_ethernet_driver_t* Device) {
  42. if (!strcasestr(Driver, "W5500"))
  43. return NULL;
  44. W5500.init_config = init_config;
  45. W5500.spi = true;
  46. W5500.rmii = false;
  47. #ifdef CONFIG_ETH_SPI_ETHERNET_W5500
  48. W5500.valid = true;
  49. #else
  50. W5500.valid = false;
  51. #endif
  52. return &W5500;
  53. }