test_emac.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. #include "tcpip_adapter.h"
  6. #include "esp_event.h"
  7. #include "unity.h"
  8. #include "test_utils.h"
  9. #include "esp_eth.h"
  10. #include "esp_log.h"
  11. #include "sdkconfig.h"
  12. static const char *TAG = "esp_eth_test";
  13. /** Event handler for Ethernet events */
  14. static void eth_event_handler(void *arg, esp_event_base_t event_base,
  15. int32_t event_id, void *event_data)
  16. {
  17. switch (event_id) {
  18. case ETHERNET_EVENT_CONNECTED:
  19. ESP_LOGI(TAG, "Ethernet Link Up");
  20. break;
  21. case ETHERNET_EVENT_DISCONNECTED:
  22. ESP_LOGI(TAG, "Ethernet Link Down");
  23. break;
  24. case ETHERNET_EVENT_START:
  25. ESP_LOGI(TAG, "Ethernet Started");
  26. break;
  27. case ETHERNET_EVENT_STOP:
  28. ESP_LOGI(TAG, "Ethernet Stopped");
  29. break;
  30. default:
  31. break;
  32. }
  33. }
  34. /** Event handler for IP_EVENT_ETH_GOT_IP */
  35. static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
  36. int32_t event_id, void *event_data)
  37. {
  38. ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
  39. const tcpip_adapter_ip_info_t *ip_info = &event->ip_info;
  40. ESP_LOGI(TAG, "Ethernet Got IP Address");
  41. ESP_LOGI(TAG, "~~~~~~~~~~~");
  42. ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
  43. ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
  44. ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
  45. ESP_LOGI(TAG, "~~~~~~~~~~~");
  46. }
  47. TEST_CASE("esp32 emac io test", "[ethernet][ignore]")
  48. {
  49. TEST_ESP_OK(esp_event_loop_create_default());
  50. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  51. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
  52. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  53. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  54. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
  55. esp_eth_handle_t eth_handle = NULL;
  56. TEST_ESP_OK(esp_eth_driver_install(&config, &eth_handle));
  57. vTaskDelay(pdMS_TO_TICKS(1000));
  58. /* get MAC address */
  59. uint8_t mac_addr[6];
  60. memset(mac_addr, 0, sizeof(mac_addr));
  61. TEST_ESP_OK(esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr));
  62. TEST_ASSERT(mac_addr[0] != 0);
  63. /* get PHY address */
  64. int phy_addr = -1;
  65. TEST_ESP_OK(esp_eth_ioctl(eth_handle, ETH_CMD_G_PHY_ADDR, &phy_addr));
  66. TEST_ASSERT(phy_addr >= 0 && phy_addr <= 31);
  67. TEST_ESP_OK(esp_eth_driver_uninstall(eth_handle));
  68. TEST_ESP_OK(phy->del(phy));
  69. TEST_ESP_OK(mac->del(mac));
  70. TEST_ESP_OK(esp_event_loop_delete_default());
  71. }
  72. TEST_CASE("ethernet tcpip_adapter", "[ethernet][ignore]")
  73. {
  74. test_case_uses_tcpip();
  75. TEST_ESP_OK(esp_event_loop_create_default());
  76. TEST_ESP_OK(tcpip_adapter_set_default_eth_handlers());
  77. TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
  78. TEST_ESP_OK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
  79. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  80. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
  81. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  82. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  83. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
  84. esp_eth_handle_t eth_handle = NULL;
  85. TEST_ESP_OK(esp_eth_driver_install(&config, &eth_handle));
  86. vTaskDelay(portMAX_DELAY);
  87. }
  88. #if CONFIG_ETH_USE_SPI_ETHERNET
  89. TEST_CASE("dm9051 io test", "[ethernet][ignore]")
  90. {
  91. spi_device_handle_t spi_handle = NULL;
  92. spi_bus_config_t buscfg = {
  93. .miso_io_num = 25,
  94. .mosi_io_num = 23,
  95. .sclk_io_num = 19,
  96. .quadwp_io_num = -1,
  97. .quadhd_io_num = -1,
  98. };
  99. TEST_ESP_OK(spi_bus_initialize(HSPI_HOST, &buscfg, 1));
  100. spi_device_interface_config_t devcfg = {
  101. .command_bits = 1,
  102. .address_bits = 7,
  103. .mode = 0,
  104. .clock_speed_hz = 20 * 1000 * 1000,
  105. .spics_io_num = 22,
  106. .queue_size = 20
  107. };
  108. TEST_ESP_OK(spi_bus_add_device(HSPI_HOST, &devcfg, &spi_handle));
  109. gpio_install_isr_service(0);
  110. tcpip_adapter_init();
  111. TEST_ESP_OK(esp_event_loop_create_default());
  112. TEST_ESP_OK(tcpip_adapter_set_default_eth_handlers());
  113. TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
  114. TEST_ESP_OK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
  115. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  116. mac_config.spi_hdl = spi_handle;
  117. esp_eth_mac_t *mac = esp_eth_mac_new_dm9051(&mac_config);
  118. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  119. esp_eth_phy_t *phy = esp_eth_phy_new_dm9051(&phy_config);
  120. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
  121. esp_eth_handle_t eth_handle = NULL;
  122. TEST_ESP_OK(esp_eth_driver_install(&config, &eth_handle));
  123. vTaskDelay(pdMS_TO_TICKS(portMAX_DELAY));
  124. TEST_ESP_OK(esp_eth_driver_uninstall(eth_handle));
  125. TEST_ESP_OK(phy->del(phy));
  126. TEST_ESP_OK(mac->del(mac));
  127. TEST_ESP_OK(esp_event_loop_delete_default());
  128. TEST_ESP_OK(spi_bus_remove_device(spi_handle));
  129. TEST_ESP_OK(spi_bus_free(HSPI_HOST));
  130. }
  131. #endif