esp_eth_test_common.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/event_groups.h"
  8. #include "esp_event.h"
  9. #include "esp_log.h"
  10. #if CONFIG_TARGET_USE_SPI_ETHERNET
  11. #include "driver/spi_master.h"
  12. #include "driver/gpio.h"
  13. #include "esp_mac.h"
  14. #endif // CONFIG_TARGET_USE_SPI_ETHERNET
  15. #include "sdkconfig.h"
  16. #include "esp_eth_test_common.h"
  17. #if CONFIG_TARGET_USE_SPI_ETHERNET
  18. #define DEFAULT_TARGET_SPI_HOST 1
  19. #define DEFAULT_TARGET_SPI_MISO_GPIO 12
  20. #define DEFAULT_TARGET_SPI_MOSI_GPIO 13
  21. #define DEFAULT_TARGET_SPI_SCLK_GPIO 14
  22. #define DEFAULT_TARGET_SPI_CS_GPIO 15
  23. #endif // CONFIG_TARGET_USE_SPI_ETHERNET
  24. static const char *TAG = "esp32_eth_test_common";
  25. esp_eth_mac_t *mac_init(void *vendor_emac_config, eth_mac_config_t *mac_config)
  26. {
  27. esp_eth_mac_t *mac = NULL;
  28. eth_mac_config_t mac_config_default = ETH_MAC_DEFAULT_CONFIG();
  29. if (mac_config == NULL) {
  30. mac_config = &mac_config_default;
  31. }
  32. #if CONFIG_TARGET_USE_INTERNAL_ETHERNET
  33. eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
  34. #if !CONFIG_TARGET_USE_DEFAULT_EMAC_CONFIG
  35. esp32_emac_config.smi_mdc_gpio_num = CONFIG_TARGET_IO_MDC;
  36. esp32_emac_config.smi_mdio_gpio_num = CONFIG_TARGET_IO_MDIO;
  37. #endif // CONFIG_TARGET_USE_DEFAULT_EMAC_CONFIG
  38. if (vendor_emac_config == NULL) {
  39. vendor_emac_config = &esp32_emac_config;
  40. }
  41. mac = esp_eth_mac_new_esp32(vendor_emac_config, mac_config);
  42. #elif CONFIG_TARGET_USE_SPI_ETHERNET
  43. // Install GPIO ISR handler to be able to service SPI Eth modlues interrupts
  44. gpio_install_isr_service(0);
  45. spi_bus_config_t buscfg = {
  46. .miso_io_num = DEFAULT_TARGET_SPI_MISO_GPIO,
  47. .mosi_io_num = DEFAULT_TARGET_SPI_MOSI_GPIO,
  48. .sclk_io_num = DEFAULT_TARGET_SPI_SCLK_GPIO,
  49. .quadwp_io_num = -1,
  50. .quadhd_io_num = -1,
  51. };
  52. TEST_ESP_OK(spi_bus_initialize(DEFAULT_TARGET_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));
  53. spi_device_interface_config_t devcfg = {
  54. .mode = 0,
  55. .spics_io_num = DEFAULT_TARGET_SPI_CS_GPIO,
  56. .clock_speed_hz = CONFIG_TARGET_SPI_CLOCK_MHZ * 1000 * 1000,
  57. .queue_size = 20
  58. };
  59. #if CONFIG_TARGET_ETH_PHY_DEVICE_W5500
  60. eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(DEFAULT_TARGET_SPI_HOST, &devcfg);
  61. if (vendor_emac_config == NULL) {
  62. vendor_emac_config = &w5500_config;
  63. }
  64. mac = esp_eth_mac_new_w5500(vendor_emac_config, mac_config);
  65. #elif CONFIG_TARGET_ETH_PHY_DEVICE_KSZ8851SNL
  66. eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(DEFAULT_TARGET_SPI_HOST, &devcfg);
  67. ksz8851snl_config.int_gpio_num = 4;
  68. if (vendor_emac_config == NULL) {
  69. vendor_emac_config = &ksz8851snl_config;
  70. }
  71. mac = esp_eth_mac_new_ksz8851snl(vendor_emac_config, mac_config);
  72. #elif CONFIG_TARGET_ETH_PHY_DEVICE_DM9051
  73. eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(DEFAULT_TARGET_SPI_HOST, &devcfg);
  74. if (vendor_emac_config == NULL) {
  75. vendor_emac_config = &dm9051_config ;
  76. }
  77. mac = esp_eth_mac_new_dm9051(vendor_emac_config, mac_config);
  78. #endif // CONFIG_TARGET_ETH_PHY_DEVICE_W5500
  79. #endif // CONFIG_TARGET_USE_INTERNAL_ETHERNET
  80. return mac;
  81. }
  82. esp_eth_phy_t *phy_init(eth_phy_config_t *phy_config)
  83. {
  84. esp_eth_phy_t *phy = NULL;
  85. eth_phy_config_t phy_config_default = ETH_PHY_DEFAULT_CONFIG();
  86. if (phy_config == NULL) {
  87. phy_config = &phy_config_default;
  88. }
  89. #if CONFIG_TARGET_USE_INTERNAL_ETHERNET
  90. phy_config->phy_addr = ESP_ETH_PHY_ADDR_AUTO;
  91. #if CONFIG_TARGET_ETH_PHY_DEVICE_IP101
  92. phy = esp_eth_phy_new_ip101(phy_config);
  93. #elif CONFIG_TARGET_ETH_PHY_DEVICE_LAN8720
  94. phy = esp_eth_phy_new_lan87xx(phy_config);
  95. #elif CONFIG_TARGET_ETH_PHY_DEVICE_KSZ8041
  96. phy = esp_eth_phy_new_ksz80xx(phy_config);
  97. #elif CONFIG_TARGET_ETH_PHY_DEVICE_RTL8201
  98. phy = esp_eth_phy_new_rtl8201(phy_config);
  99. #elif CONFIG_TARGET_ETH_PHY_DEVICE_DP83848
  100. phy = esp_eth_phy_new_dp83848(phy_config);
  101. #endif // CONFIG_TARGET_ETH_PHY_DEVICE_IP101
  102. #elif CONFIG_TARGET_USE_SPI_ETHERNET
  103. phy_config->reset_gpio_num = -1;
  104. #if CONFIG_TARGET_ETH_PHY_DEVICE_W5500
  105. phy = esp_eth_phy_new_w5500(phy_config);
  106. #elif CONFIG_TARGET_ETH_PHY_DEVICE_KSZ8851SNL
  107. phy = esp_eth_phy_new_ksz8851snl(phy_config);
  108. #elif CONFIG_TARGET_ETH_PHY_DEVICE_DM9051
  109. phy = esp_eth_phy_new_dm9051(phy_config);
  110. #endif // CONFIG_TARGET_ETH_PHY_DEVICE_W5500
  111. #endif // CONFIG_TARGET_USE_INTERNAL_ETHERNET
  112. return phy;
  113. }
  114. void extra_eth_config(esp_eth_handle_t eth_handle)
  115. {
  116. // *** SPI Ethernet modules deviation ***
  117. // Rationale: The SPI Ethernet modules don't have a burned default factory MAC address
  118. #if CONFIG_TARGET_USE_SPI_ETHERNET
  119. uint8_t base_mac_addr[ETH_ADDR_LEN];
  120. TEST_ESP_OK(esp_efuse_mac_get_default(base_mac_addr));
  121. uint8_t local_mac[ETH_ADDR_LEN];
  122. TEST_ESP_OK(esp_derive_local_mac(local_mac, base_mac_addr));
  123. TEST_ESP_OK(esp_eth_ioctl(eth_handle, ETH_CMD_S_MAC_ADDR, local_mac));
  124. #endif
  125. }
  126. void extra_cleanup(void)
  127. {
  128. #if CONFIG_TARGET_USE_SPI_ETHERNET
  129. TEST_ESP_OK(spi_bus_free(DEFAULT_TARGET_SPI_HOST));
  130. #endif
  131. }
  132. /** Event handler for Ethernet events */
  133. void eth_event_handler(void *arg, esp_event_base_t event_base,
  134. int32_t event_id, void *event_data)
  135. {
  136. EventGroupHandle_t eth_event_group = (EventGroupHandle_t)arg;
  137. switch (event_id) {
  138. case ETHERNET_EVENT_CONNECTED:
  139. xEventGroupSetBits(eth_event_group, ETH_CONNECT_BIT);
  140. break;
  141. case ETHERNET_EVENT_DISCONNECTED:
  142. break;
  143. case ETHERNET_EVENT_START:
  144. xEventGroupSetBits(eth_event_group, ETH_START_BIT);
  145. break;
  146. case ETHERNET_EVENT_STOP:
  147. xEventGroupSetBits(eth_event_group, ETH_STOP_BIT);
  148. break;
  149. default:
  150. break;
  151. }
  152. }
  153. /** Event handler for IP_EVENT_ETH_GOT_IP */
  154. void got_ip_event_handler(void *arg, esp_event_base_t event_base,
  155. int32_t event_id, void *event_data)
  156. {
  157. EventGroupHandle_t eth_event_group = (EventGroupHandle_t)arg;
  158. ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
  159. const esp_netif_ip_info_t *ip_info = &event->ip_info;
  160. ESP_LOGI(TAG, "Ethernet Got IP Address");
  161. ESP_LOGI(TAG, "~~~~~~~~~~~");
  162. ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
  163. ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
  164. ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
  165. ESP_LOGI(TAG, "~~~~~~~~~~~");
  166. xEventGroupSetBits(eth_event_group, ETH_GOT_IP_BIT);
  167. }