test_emac.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "sdkconfig.h"
  4. #include "freertos/FreeRTOS.h"
  5. #include "freertos/task.h"
  6. #include "freertos/event_groups.h"
  7. #include "tcpip_adapter.h"
  8. #include "unity.h"
  9. #include "test_utils.h"
  10. #include "esp_event.h"
  11. #include "esp_eth.h"
  12. #include "esp_log.h"
  13. #include "lwip/inet.h"
  14. #include "lwip/netdb.h"
  15. #include "lwip/sockets.h"
  16. #include "ping/ping_sock.h"
  17. static const char *TAG = "esp_eth_test";
  18. #define ETH_START_BIT BIT(0)
  19. #define ETH_STOP_BIT BIT(1)
  20. #define ETH_CONNECT_BIT BIT(2)
  21. #define ETH_GOT_IP_BIT BIT(3)
  22. #define ETH_PING_END_BIT BIT(4)
  23. #define ETH_START_TIMEOUT_MS (10000)
  24. #define ETH_CONNECT_TIMEOUT_MS (40000)
  25. #define ETH_STOP_TIMEOUT_MS (10000)
  26. #define ETH_GET_IP_TIMEOUT_MS (60000)
  27. #define ETH_PING_DURATION_MS (5000)
  28. #define ETH_PING_END_TIMEOUT_MS (ETH_PING_DURATION_MS * 2)
  29. /** Event handler for Ethernet events */
  30. static void eth_event_handler(void *arg, esp_event_base_t event_base,
  31. int32_t event_id, void *event_data)
  32. {
  33. EventGroupHandle_t eth_event_group = (EventGroupHandle_t)arg;
  34. switch (event_id) {
  35. case ETHERNET_EVENT_CONNECTED:
  36. xEventGroupSetBits(eth_event_group, ETH_CONNECT_BIT);
  37. ESP_LOGI(TAG, "Ethernet Link Up");
  38. break;
  39. case ETHERNET_EVENT_DISCONNECTED:
  40. ESP_LOGI(TAG, "Ethernet Link Down");
  41. break;
  42. case ETHERNET_EVENT_START:
  43. xEventGroupSetBits(eth_event_group, ETH_START_BIT);
  44. ESP_LOGI(TAG, "Ethernet Started");
  45. break;
  46. case ETHERNET_EVENT_STOP:
  47. xEventGroupSetBits(eth_event_group, ETH_STOP_BIT);
  48. ESP_LOGI(TAG, "Ethernet Stopped");
  49. break;
  50. default:
  51. break;
  52. }
  53. }
  54. /** Event handler for IP_EVENT_ETH_GOT_IP */
  55. static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
  56. int32_t event_id, void *event_data)
  57. {
  58. EventGroupHandle_t eth_event_group = (EventGroupHandle_t)arg;
  59. ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
  60. const tcpip_adapter_ip_info_t *ip_info = &event->ip_info;
  61. ESP_LOGI(TAG, "Ethernet Got IP Address");
  62. ESP_LOGI(TAG, "~~~~~~~~~~~");
  63. ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
  64. ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
  65. ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
  66. ESP_LOGI(TAG, "~~~~~~~~~~~");
  67. xEventGroupSetBits(eth_event_group, ETH_GOT_IP_BIT);
  68. }
  69. static void test_on_ping_success(esp_ping_handle_t hdl, void *args)
  70. {
  71. uint8_t ttl;
  72. uint16_t seqno;
  73. uint32_t elapsed_time, recv_len;
  74. ip_addr_t target_addr;
  75. esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno));
  76. esp_ping_get_profile(hdl, ESP_PING_PROF_TTL, &ttl, sizeof(ttl));
  77. esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
  78. esp_ping_get_profile(hdl, ESP_PING_PROF_SIZE, &recv_len, sizeof(recv_len));
  79. esp_ping_get_profile(hdl, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time));
  80. printf("%d bytes from %s icmp_seq=%d ttl=%d time=%d ms\n",
  81. recv_len, inet_ntoa(target_addr.u_addr.ip4), seqno, ttl, elapsed_time);
  82. }
  83. static void test_on_ping_timeout(esp_ping_handle_t hdl, void *args)
  84. {
  85. uint16_t seqno;
  86. ip_addr_t target_addr;
  87. esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno));
  88. esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
  89. printf("From %s icmp_seq=%d timeout\n", inet_ntoa(target_addr.u_addr.ip4), seqno);
  90. }
  91. static void test_on_ping_end(esp_ping_handle_t hdl, void *args)
  92. {
  93. EventGroupHandle_t eth_event_group = (EventGroupHandle_t)args;
  94. uint32_t transmitted;
  95. uint32_t received;
  96. uint32_t total_time_ms;
  97. esp_ping_get_profile(hdl, ESP_PING_PROF_REQUEST, &transmitted, sizeof(transmitted));
  98. esp_ping_get_profile(hdl, ESP_PING_PROF_REPLY, &received, sizeof(received));
  99. esp_ping_get_profile(hdl, ESP_PING_PROF_DURATION, &total_time_ms, sizeof(total_time_ms));
  100. printf("%d packets transmitted, %d received, time %dms\n", transmitted, received, total_time_ms);
  101. if (transmitted == received) {
  102. xEventGroupSetBits(eth_event_group, ETH_PING_END_BIT);
  103. }
  104. }
  105. TEST_CASE("esp32 ethernet io test", "[ethernet][test_env=UT_T2_Ethernet]")
  106. {
  107. TEST_ESP_OK(esp_event_loop_create_default());
  108. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  109. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
  110. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  111. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  112. esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
  113. esp_eth_handle_t eth_handle = NULL;
  114. TEST_ESP_OK(esp_eth_driver_install(&eth_config, &eth_handle));
  115. vTaskDelay(pdMS_TO_TICKS(1000));
  116. /* get MAC address */
  117. uint8_t mac_addr[6];
  118. memset(mac_addr, 0, sizeof(mac_addr));
  119. TEST_ESP_OK(esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr));
  120. ESP_LOGI(TAG, "Ethernet MAC Address: %02x:%02x:%02x:%02x:%02x:%02x",
  121. mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  122. TEST_ASSERT(mac_addr[0] != 0);
  123. /* get PHY address */
  124. int phy_addr = -1;
  125. TEST_ESP_OK(esp_eth_ioctl(eth_handle, ETH_CMD_G_PHY_ADDR, &phy_addr));
  126. ESP_LOGI(TAG, "Ethernet PHY Address: %d", phy_addr);
  127. TEST_ASSERT(phy_addr >= 0 && phy_addr <= 31);
  128. TEST_ESP_OK(esp_eth_driver_uninstall(eth_handle));
  129. TEST_ESP_OK(phy->del(phy));
  130. TEST_ESP_OK(mac->del(mac));
  131. TEST_ESP_OK(esp_event_loop_delete_default());
  132. }
  133. TEST_CASE("esp32 ethernet event test", "[ethernet][test_env=UT_T2_Ethernet]")
  134. {
  135. EventBits_t bits = 0;
  136. EventGroupHandle_t eth_event_group = xEventGroupCreate();
  137. TEST_ASSERT(eth_event_group != NULL);
  138. TEST_ESP_OK(esp_event_loop_create_default());
  139. TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_event_group));
  140. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  141. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
  142. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  143. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  144. esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
  145. esp_eth_handle_t eth_handle = NULL;
  146. TEST_ESP_OK(esp_eth_driver_install(&eth_config, &eth_handle));
  147. /* wait for connection start */
  148. bits = xEventGroupWaitBits(eth_event_group, ETH_START_BIT, true, true, pdMS_TO_TICKS(ETH_START_TIMEOUT_MS));
  149. TEST_ASSERT((bits & ETH_START_BIT) == ETH_START_BIT);
  150. /* wait for connection establish */
  151. bits = xEventGroupWaitBits(eth_event_group, ETH_CONNECT_BIT, true, true, pdMS_TO_TICKS(ETH_CONNECT_TIMEOUT_MS));
  152. TEST_ASSERT((bits & ETH_CONNECT_BIT) == ETH_CONNECT_BIT);
  153. TEST_ESP_OK(esp_eth_driver_uninstall(eth_handle));
  154. /* wait for connection stop */
  155. bits = xEventGroupWaitBits(eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(ETH_STOP_TIMEOUT_MS));
  156. TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT);
  157. // "check link timer callback" might owned the reference of phy object, make sure it has release it
  158. vTaskDelay(pdMS_TO_TICKS(2000));
  159. TEST_ESP_OK(phy->del(phy));
  160. TEST_ESP_OK(mac->del(mac));
  161. TEST_ESP_OK(esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler));
  162. TEST_ESP_OK(esp_event_loop_delete_default());
  163. vEventGroupDelete(eth_event_group);
  164. }
  165. TEST_CASE("esp32 ethernet dhcp test", "[ethernet][test_env=UT_T2_Ethernet]")
  166. {
  167. EventBits_t bits = 0;
  168. EventGroupHandle_t eth_event_group = xEventGroupCreate();
  169. TEST_ASSERT(eth_event_group != NULL);
  170. test_case_uses_tcpip();
  171. TEST_ESP_OK(esp_event_loop_create_default());
  172. TEST_ESP_OK(tcpip_adapter_set_default_eth_handlers());
  173. TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_event_group));
  174. TEST_ESP_OK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, eth_event_group));
  175. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  176. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
  177. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  178. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  179. esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
  180. esp_eth_handle_t eth_handle = NULL;
  181. TEST_ESP_OK(esp_eth_driver_install(&eth_config, &eth_handle));
  182. /* wait for IP lease */
  183. bits = xEventGroupWaitBits(eth_event_group, ETH_GOT_IP_BIT, true, true, pdMS_TO_TICKS(ETH_GET_IP_TIMEOUT_MS));
  184. TEST_ASSERT((bits & ETH_GOT_IP_BIT) == ETH_GOT_IP_BIT);
  185. TEST_ESP_OK(esp_eth_driver_uninstall(eth_handle));
  186. /* wait for connection stop */
  187. bits = xEventGroupWaitBits(eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(ETH_STOP_TIMEOUT_MS));
  188. TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT);
  189. // "check link timer callback" might owned the reference of phy object, make sure it has release it
  190. vTaskDelay(pdMS_TO_TICKS(2000));
  191. TEST_ESP_OK(phy->del(phy));
  192. TEST_ESP_OK(mac->del(mac));
  193. TEST_ESP_OK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, got_ip_event_handler));
  194. TEST_ESP_OK(esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler));
  195. TEST_ESP_OK(tcpip_adapter_clear_default_eth_handlers());
  196. TEST_ESP_OK(esp_event_loop_delete_default());
  197. vEventGroupDelete(eth_event_group);
  198. }
  199. TEST_CASE("esp32 ethernet icmp test", "[ethernet][test_env=UT_T2_Ethernet]")
  200. {
  201. EventBits_t bits = 0;
  202. EventGroupHandle_t eth_event_group = xEventGroupCreate();
  203. TEST_ASSERT(eth_event_group != NULL);
  204. test_case_uses_tcpip();
  205. TEST_ESP_OK(esp_event_loop_create_default());
  206. TEST_ESP_OK(tcpip_adapter_set_default_eth_handlers());
  207. TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_event_group));
  208. TEST_ESP_OK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, eth_event_group));
  209. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  210. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
  211. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  212. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  213. esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
  214. esp_eth_handle_t eth_handle = NULL;
  215. TEST_ESP_OK(esp_eth_driver_install(&eth_config, &eth_handle));
  216. /* wait for IP lease */
  217. bits = xEventGroupWaitBits(eth_event_group, ETH_GOT_IP_BIT, true, true, pdMS_TO_TICKS(ETH_GET_IP_TIMEOUT_MS));
  218. TEST_ASSERT((bits & ETH_GOT_IP_BIT) == ETH_GOT_IP_BIT);
  219. // Parse IP address
  220. ip_addr_t target_addr;
  221. struct addrinfo hint;
  222. struct addrinfo *res = NULL;
  223. memset(&hint, 0, sizeof(hint));
  224. memset(&target_addr, 0, sizeof(target_addr));
  225. /* convert URL to IP */
  226. TEST_ASSERT(getaddrinfo("www.baidu.com", NULL, &hint, &res) == 0);
  227. struct in_addr addr4 = ((struct sockaddr_in *) (res->ai_addr))->sin_addr;
  228. inet_addr_to_ip4addr(ip_2_ip4(&target_addr), &addr4);
  229. freeaddrinfo(res);
  230. esp_ping_config_t ping_config = ESP_PING_DEFAULT_CONFIG();
  231. ping_config.target_addr = target_addr;
  232. ping_config.count = 0; // ping in infinite mode
  233. /* set callback functions */
  234. esp_ping_callbacks_t cbs;
  235. cbs.on_ping_success = test_on_ping_success;
  236. cbs.on_ping_timeout = test_on_ping_timeout;
  237. cbs.on_ping_end = test_on_ping_end;
  238. cbs.cb_args = eth_event_group;
  239. esp_ping_handle_t ping;
  240. TEST_ESP_OK(esp_ping_new_session(&ping_config, &cbs, &ping));
  241. /* start ping */
  242. TEST_ESP_OK(esp_ping_start(ping));
  243. /* ping for a while */
  244. vTaskDelay(pdMS_TO_TICKS(ETH_PING_DURATION_MS));
  245. /* stop ping */
  246. TEST_ESP_OK(esp_ping_stop(ping));
  247. /* wait for end of ping */
  248. bits = xEventGroupWaitBits(eth_event_group, ETH_PING_END_BIT, true, true, pdMS_TO_TICKS(ETH_PING_END_TIMEOUT_MS));
  249. TEST_ASSERT((bits & ETH_PING_END_BIT) == ETH_PING_END_BIT);
  250. /* restart ping */
  251. TEST_ESP_OK(esp_ping_start(ping));
  252. vTaskDelay(pdMS_TO_TICKS(ETH_PING_DURATION_MS));
  253. TEST_ESP_OK(esp_ping_stop(ping));
  254. bits = xEventGroupWaitBits(eth_event_group, ETH_PING_END_BIT, true, true, pdMS_TO_TICKS(ETH_PING_END_TIMEOUT_MS));
  255. TEST_ASSERT((bits & ETH_PING_END_BIT) == ETH_PING_END_BIT);
  256. /* de-initialize ping process */
  257. TEST_ESP_OK(esp_ping_delete_session(ping));
  258. TEST_ESP_OK(esp_eth_driver_uninstall(eth_handle));
  259. /* wait for connection stop */
  260. bits = xEventGroupWaitBits(eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(ETH_STOP_TIMEOUT_MS));
  261. TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT);
  262. // "check link timer callback" might owned the reference of phy object, make sure it has release it
  263. vTaskDelay(pdMS_TO_TICKS(2000));
  264. TEST_ESP_OK(phy->del(phy));
  265. TEST_ESP_OK(mac->del(mac));
  266. TEST_ESP_OK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, got_ip_event_handler));
  267. TEST_ESP_OK(tcpip_adapter_clear_default_eth_handlers());
  268. TEST_ESP_OK(esp_event_loop_delete_default());
  269. vEventGroupDelete(eth_event_group);
  270. }
  271. #if CONFIG_ETH_USE_SPI_ETHERNET
  272. TEST_CASE("dm9051 io test", "[ethernet][ignore]")
  273. {
  274. spi_device_handle_t spi_handle = NULL;
  275. spi_bus_config_t buscfg = {
  276. .miso_io_num = 25,
  277. .mosi_io_num = 23,
  278. .sclk_io_num = 19,
  279. .quadwp_io_num = -1,
  280. .quadhd_io_num = -1,
  281. };
  282. TEST_ESP_OK(spi_bus_initialize(HSPI_HOST, &buscfg, 1));
  283. spi_device_interface_config_t devcfg = {
  284. .command_bits = 1,
  285. .address_bits = 7,
  286. .mode = 0,
  287. .clock_speed_hz = 20 * 1000 * 1000,
  288. .spics_io_num = 22,
  289. .queue_size = 20
  290. };
  291. TEST_ESP_OK(spi_bus_add_device(HSPI_HOST, &devcfg, &spi_handle));
  292. gpio_install_isr_service(0);
  293. tcpip_adapter_init();
  294. TEST_ESP_OK(esp_event_loop_create_default());
  295. TEST_ESP_OK(tcpip_adapter_set_default_eth_handlers());
  296. TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
  297. TEST_ESP_OK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
  298. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  299. eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle);
  300. esp_eth_mac_t *mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
  301. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  302. esp_eth_phy_t *phy = esp_eth_phy_new_dm9051(&phy_config);
  303. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
  304. esp_eth_handle_t eth_handle = NULL;
  305. TEST_ESP_OK(esp_eth_driver_install(&config, &eth_handle));
  306. vTaskDelay(pdMS_TO_TICKS(portMAX_DELAY));
  307. TEST_ESP_OK(esp_eth_driver_uninstall(eth_handle));
  308. TEST_ESP_OK(phy->del(phy));
  309. TEST_ESP_OK(mac->del(mac));
  310. TEST_ESP_OK(esp_event_loop_delete_default());
  311. TEST_ESP_OK(spi_bus_remove_device(spi_handle));
  312. TEST_ESP_OK(spi_bus_free(HSPI_HOST));
  313. }
  314. #endif