2
0

network_driver_DM9051.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "esp_eth.h"
  2. #include "network_ethernet.h"
  3. static EXT_RAM_ATTR network_ethernet_driver_t DM9051;
  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_DM9051
  9. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  10. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  11. eth_dm9051_config_t eth_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle);
  12. // we assume that isr has been installed already
  13. eth_config.int_gpio_num = ethernet_config->ethType.spi.has_intr?ethernet_config->ethType.spi.intr.pin:-1;
  14. phy_config.phy_addr = -1;
  15. phy_config.reset_gpio_num = ethernet_config->common.has_rst?ethernet_config->common.rst.pin:-1;
  16. esp_eth_mac_t* mac = esp_eth_mac_new_dm9051(&eth_config, &mac_config);
  17. esp_eth_phy_t* phy = esp_eth_phy_new_dm9051(&phy_config);
  18. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
  19. return esp_eth_driver_install(&config, &DM9051.handle);
  20. #else
  21. return ESP_ERR_NOT_SUPPORTED;
  22. #endif
  23. }
  24. static void init_config(sys_Eth* ethernet_config) {
  25. esp_netif_inherent_config_t loc_esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
  26. devcfg.command_bits = 1;
  27. devcfg.address_bits = 7;
  28. devcfg.mode = 0;
  29. devcfg.clock_speed_hz = ethernet_config->ethType.spi.speed > 0 ? ethernet_config->ethType.spi.speed : SPI_MASTER_FREQ_20M; // default speed
  30. devcfg.queue_size = 20;
  31. devcfg.spics_io_num = ethernet_config->ethType.spi.has_cs?ethernet_config->ethType.spi.cs.pin:-1;
  32. memcpy(&esp_netif_config, &loc_esp_netif_config, sizeof(loc_esp_netif_config));
  33. cfg_spi.base = &esp_netif_config,
  34. cfg_spi.stack = ESP_NETIF_NETSTACK_DEFAULT_ETH;
  35. DM9051.cfg_netif = &cfg_spi;
  36. DM9051.devcfg = &devcfg;
  37. DM9051.start = start;
  38. }
  39. network_ethernet_driver_t* DM9051_Detect(sys_Eth* ethernet_config) {
  40. if (ethernet_config->common.model != sys_EthModelEnum_DM9051 ||
  41. ethernet_config->which_ethType != sys_Eth_spi_tag )
  42. return NULL;
  43. DM9051.rmii = false;
  44. DM9051.spi = true;
  45. #ifdef CONFIG_ETH_SPI_ETHERNET_DM9051
  46. DM9051.valid = true;
  47. #else
  48. DM9051.valid = false;
  49. #endif
  50. DM9051.init_config = init_config;
  51. DM9051.model = ethernet_config->common.model;
  52. return &DM9051;
  53. }