esp_eth_test.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/event_groups.h"
  5. #include "esp_event.h"
  6. #include "unity.h"
  7. #include "esp_netif.h"
  8. #include "esp_eth.h"
  9. #include "sdkconfig.h"
  10. #include "lwip/sockets.h"
  11. #define ETH_START_BIT BIT(0)
  12. #define ETH_STOP_BIT BIT(1)
  13. #define ETH_CONNECT_BIT BIT(2)
  14. #define ETH_BROADCAST_RECV_BIT BIT(0)
  15. #define ETH_MULTICAST_RECV_BIT BIT(1)
  16. #define ETH_UNICAST_RECV_BIT BIT(2)
  17. #define POKE_REQ 0xFA
  18. #define POKE_RESP 0xFB
  19. #define DUMMY_TRAFFIC 0xFF
  20. typedef struct {
  21. uint8_t dest[6];
  22. uint8_t src[6];
  23. uint16_t proto;
  24. uint8_t data[];
  25. } __attribute__((__packed__)) emac_frame_t;
  26. static void eth_event_handler(void *arg, esp_event_base_t event_base,
  27. int32_t event_id, void *event_data){
  28. EventGroupHandle_t eth_event_group = (EventGroupHandle_t)arg;
  29. switch (event_id) {
  30. case ETHERNET_EVENT_CONNECTED:
  31. xEventGroupSetBits(eth_event_group, ETH_CONNECT_BIT);
  32. break;
  33. case ETHERNET_EVENT_DISCONNECTED:
  34. break;
  35. case ETHERNET_EVENT_START:
  36. xEventGroupSetBits(eth_event_group, ETH_START_BIT);
  37. break;
  38. case ETHERNET_EVENT_STOP:
  39. xEventGroupSetBits(eth_event_group, ETH_STOP_BIT);
  40. break;
  41. default:
  42. break;
  43. }
  44. }
  45. TEST_CASE("start_and_stop", "[esp_eth]")
  46. {
  47. EventGroupHandle_t eth_event_group = xEventGroupCreate();
  48. TEST_ASSERT(eth_event_group != NULL);
  49. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); // apply default MAC configuration
  50. eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
  51. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config); // create MAC instance
  52. TEST_ASSERT_NOT_NULL(mac);
  53. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); // apply default PHY configuration
  54. #if defined(CONFIG_TARGET_ETH_PHY_DEVICE_IP101)
  55. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config); // create PHY instance
  56. #elif defined(CONFIG_TARGET_ETH_PHY_DEVICE_LAN87XX)
  57. esp_eth_phy_t *phy = esp_eth_phy_new_lan87xx(&phy_config);
  58. #endif
  59. TEST_ASSERT_NOT_NULL(phy);
  60. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); // apply default driver configuration
  61. esp_eth_handle_t eth_handle = NULL; // after driver installed, we will get the handle of the driver
  62. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_install(&config, &eth_handle)); // install driver
  63. TEST_ASSERT_NOT_NULL(eth_handle);
  64. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create_default());
  65. TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_event_group));
  66. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_start(eth_handle)); // start Ethernet driver state machine
  67. EventBits_t bits = 0;
  68. bits = xEventGroupWaitBits(eth_event_group, ETH_START_BIT, true, true, pdMS_TO_TICKS(3000));
  69. TEST_ASSERT((bits & ETH_START_BIT) == ETH_START_BIT);
  70. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_stop(eth_handle));
  71. bits = xEventGroupWaitBits(eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(3000));
  72. TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT);
  73. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete_default());
  74. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_uninstall(eth_handle));
  75. phy->del(phy);
  76. mac->del(mac);
  77. vEventGroupDelete(eth_event_group);
  78. }
  79. TEST_CASE("get_set_mac", "[esp_eth]")
  80. {
  81. EventGroupHandle_t eth_event_group = xEventGroupCreate();
  82. TEST_ASSERT(eth_event_group != NULL);
  83. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); // apply default MAC configuration
  84. eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
  85. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config); // create MAC instance
  86. TEST_ASSERT_NOT_NULL(mac);
  87. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); // apply default PHY configuration
  88. #if defined(CONFIG_TARGET_ETH_PHY_DEVICE_IP101)
  89. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config); // create PHY instance
  90. #elif defined(CONFIG_TARGET_ETH_PHY_DEVICE_LAN87XX)
  91. esp_eth_phy_t *phy = esp_eth_phy_new_lan87xx(&phy_config);
  92. #endif
  93. TEST_ASSERT_NOT_NULL(phy);
  94. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); // apply default driver configuration
  95. esp_eth_handle_t eth_handle = NULL; // after driver installed, we will get the handle of the driver
  96. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_install(&config, &eth_handle)); // install driver
  97. TEST_ASSERT_NOT_NULL(eth_handle);
  98. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create_default());
  99. TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_event_group));
  100. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_start(eth_handle)); // start Ethernet driver state machine
  101. EventBits_t bits = 0;
  102. bits = xEventGroupWaitBits(eth_event_group, ETH_CONNECT_BIT, true, true, pdMS_TO_TICKS(3000));
  103. TEST_ASSERT((bits & ETH_CONNECT_BIT) == ETH_CONNECT_BIT);
  104. uint8_t mac_addr[6] = {};
  105. TEST_ASSERT_EQUAL(ESP_OK, mac->get_addr(mac, mac_addr));
  106. TEST_ASSERT_BITS(0b00000011, 0b00, mac_addr[0]); // Check UL&IG, should be UI
  107. mac_addr[5] ^= mac_addr[4];
  108. TEST_ASSERT_EQUAL(ESP_OK, mac->set_addr(mac, mac_addr));
  109. uint8_t new_mac_addr[6] = {};
  110. TEST_ASSERT_EQUAL(ESP_OK, mac->get_addr(mac, new_mac_addr));
  111. TEST_ASSERT_EQUAL(0, memcmp(mac_addr, new_mac_addr, 6));
  112. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_stop(eth_handle));
  113. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete_default());
  114. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_uninstall(eth_handle));
  115. phy->del(phy);
  116. mac->del(mac);
  117. vEventGroupDelete(eth_event_group);
  118. }
  119. TEST_CASE("ethernet_broadcast_transmit", "[esp_eth]")
  120. {
  121. EventGroupHandle_t eth_event_group = xEventGroupCreate();
  122. TEST_ASSERT(eth_event_group != NULL);
  123. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); // apply default MAC configuration
  124. eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
  125. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config); // create MAC instance
  126. TEST_ASSERT_NOT_NULL(mac);
  127. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); // apply default PHY configuration
  128. #if defined(CONFIG_TARGET_ETH_PHY_DEVICE_IP101)
  129. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config); // create PHY instance
  130. #elif defined(CONFIG_TARGET_ETH_PHY_DEVICE_LAN87XX)
  131. esp_eth_phy_t *phy = esp_eth_phy_new_lan87xx(&phy_config);
  132. #endif
  133. TEST_ASSERT_NOT_NULL(phy);
  134. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); // apply default driver configuration
  135. esp_eth_handle_t eth_handle = NULL; // after driver installed, we will get the handle of the driver
  136. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_install(&config, &eth_handle)); // install driver
  137. TEST_ASSERT_NOT_NULL(eth_handle);
  138. TEST_ASSERT_EQUAL(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. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_start(eth_handle)); // start Ethernet driver state machine
  141. EventBits_t bits = 0;
  142. bits = xEventGroupWaitBits(eth_event_group, ETH_CONNECT_BIT, true, true, pdMS_TO_TICKS(3000));
  143. TEST_ASSERT((bits & ETH_CONNECT_BIT) == ETH_CONNECT_BIT);
  144. // even if PHY (IP101) indicates autonegotiation done and link up, it sometimes may miss few packets after atonego reset, hence wait a bit
  145. vTaskDelay(pdMS_TO_TICKS(100));
  146. emac_frame_t *pkt = malloc(1024);
  147. pkt->proto = 0x2222;
  148. memset(pkt->dest, 0xff, 6); // broadcast addr
  149. for (int i = 0; i < (1024 - ETH_HEADER_LEN); ++i){
  150. pkt->data[i] = i & 0xff;
  151. }
  152. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_transmit(eth_handle, pkt, 1024));
  153. vTaskDelay(pdMS_TO_TICKS(100));
  154. free(pkt);
  155. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_stop(eth_handle));
  156. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete_default());
  157. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_uninstall(eth_handle));
  158. phy->del(phy);
  159. mac->del(mac);
  160. vEventGroupDelete(eth_event_group);
  161. }
  162. static uint8_t local_mac_addr[6] = {};
  163. esp_err_t l2_packet_txrx_test_cb(esp_eth_handle_t hdl, uint8_t *buffer, uint32_t length, void *priv) {
  164. EventGroupHandle_t eth_event_group = (EventGroupHandle_t)priv;
  165. emac_frame_t *pkt = (emac_frame_t *) buffer;
  166. // check header
  167. if (pkt->proto == 0x2222 && length == 1024) {
  168. // check content
  169. for (int i = 0; i < (length - ETH_HEADER_LEN); ++i) {
  170. if (pkt->data[i] != (i & 0xff)) {
  171. printf("payload mismatch\n");
  172. return ESP_OK;
  173. }
  174. }
  175. if (memcmp(pkt->dest, "\xff\xff\xff\xff\xff\xff", 6) == 0) {
  176. printf("broadcast received...\n");
  177. xEventGroupSetBits(eth_event_group, ETH_BROADCAST_RECV_BIT);
  178. } else if (pkt->dest[0] & 0x1) {
  179. printf("multicast received...\n");
  180. xEventGroupSetBits(eth_event_group, ETH_MULTICAST_RECV_BIT);
  181. } else if (memcmp(pkt->dest, local_mac_addr, 6) == 0) {
  182. printf("unicast received...\n");
  183. xEventGroupSetBits(eth_event_group, ETH_UNICAST_RECV_BIT);
  184. }
  185. } else {
  186. printf("unexpected frame (protocol: 0x%x, length: %u)\n", pkt->proto, length);
  187. }
  188. return ESP_OK;
  189. };
  190. TEST_CASE("recv_pkt", "[esp_eth]")
  191. {
  192. EventGroupHandle_t eth_event_group = xEventGroupCreate();
  193. TEST_ASSERT(eth_event_group != NULL);
  194. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); // apply default MAC configuration
  195. eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
  196. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config); // create MAC instance
  197. TEST_ASSERT_NOT_NULL(mac);
  198. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); // apply default PHY configuration
  199. #if defined(CONFIG_TARGET_ETH_PHY_DEVICE_IP101)
  200. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config); // create PHY instance
  201. #elif defined(CONFIG_TARGET_ETH_PHY_DEVICE_LAN87XX)
  202. esp_eth_phy_t *phy = esp_eth_phy_new_lan87xx(&phy_config);
  203. #endif
  204. TEST_ASSERT_NOT_NULL(phy);
  205. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); // apply default driver configuration
  206. esp_eth_handle_t eth_handle = NULL; // after driver installed, we will get the handle of the driver
  207. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_install(&config, &eth_handle)); // install driver
  208. TEST_ASSERT_NOT_NULL(eth_handle);
  209. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create_default());
  210. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_start(eth_handle)); // start Ethernet driver state machine
  211. TEST_ASSERT_EQUAL(ESP_OK, mac->get_addr(mac, local_mac_addr));
  212. // test app will parse the DUT MAC from this line of log output
  213. printf("DUT MAC: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", local_mac_addr[0], local_mac_addr[1], local_mac_addr[2],
  214. local_mac_addr[3], local_mac_addr[4], local_mac_addr[5]);
  215. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_update_input_path(eth_handle, l2_packet_txrx_test_cb, eth_event_group));
  216. EventBits_t bits = 0;
  217. bits = xEventGroupWaitBits(eth_event_group, ETH_BROADCAST_RECV_BIT | ETH_MULTICAST_RECV_BIT | ETH_UNICAST_RECV_BIT,
  218. true, true, pdMS_TO_TICKS(5000));
  219. TEST_ASSERT((bits & (ETH_BROADCAST_RECV_BIT | ETH_MULTICAST_RECV_BIT | ETH_UNICAST_RECV_BIT)) ==
  220. (ETH_BROADCAST_RECV_BIT | ETH_MULTICAST_RECV_BIT | ETH_UNICAST_RECV_BIT));
  221. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_stop(eth_handle));
  222. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete_default());
  223. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_uninstall(eth_handle));
  224. phy->del(phy);
  225. mac->del(mac);
  226. vEventGroupDelete(eth_event_group);
  227. }
  228. typedef struct
  229. {
  230. SemaphoreHandle_t mutex;
  231. int rx_pkt_cnt;
  232. } recv_info_t;
  233. static esp_err_t eth_recv_cb(esp_eth_handle_t hdl, uint8_t *buffer, uint32_t length, void *priv)
  234. {
  235. emac_frame_t *pkt = (emac_frame_t *)buffer;
  236. recv_info_t *recv_info = (recv_info_t *)priv;
  237. if (pkt->proto == 0x2222) {
  238. switch (pkt->data[0])
  239. {
  240. case POKE_RESP:
  241. xSemaphoreGive(recv_info->mutex);
  242. break;
  243. case DUMMY_TRAFFIC:
  244. (recv_info->rx_pkt_cnt)++;
  245. break;
  246. default:
  247. break;
  248. }
  249. }
  250. free(buffer);
  251. return ESP_OK;
  252. }
  253. TEST_CASE("start_stop_stress_test", "[esp_eth]")
  254. {
  255. recv_info_t recv_info;
  256. recv_info.mutex = xSemaphoreCreateBinary();
  257. TEST_ASSERT_NOT_NULL(recv_info.mutex);
  258. recv_info.rx_pkt_cnt = 0;
  259. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); // apply default MAC configuration
  260. eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
  261. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config); // create MAC instance
  262. TEST_ASSERT_NOT_NULL(mac);
  263. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); // apply default PHY configuration
  264. #if defined(CONFIG_TARGET_ETH_PHY_DEVICE_IP101)
  265. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config); // create PHY instance
  266. #elif defined(CONFIG_TARGET_ETH_PHY_DEVICE_LAN87XX)
  267. esp_eth_phy_t *phy = esp_eth_phy_new_lan87xx(&phy_config);
  268. #endif
  269. TEST_ASSERT_NOT_NULL(phy);
  270. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); // apply default driver configuration
  271. esp_eth_handle_t eth_handle = NULL; // after driver installed, we will get the handle of the driver
  272. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_install(&config, &eth_handle)); // install driver
  273. TEST_ASSERT_NOT_NULL(eth_handle);
  274. TEST_ASSERT_EQUAL(ESP_OK, mac->get_addr(mac, local_mac_addr));
  275. // test app will parse the DUT MAC from this line of log output
  276. printf("DUT MAC: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", local_mac_addr[0], local_mac_addr[1], local_mac_addr[2],
  277. local_mac_addr[3], local_mac_addr[4], local_mac_addr[5]);
  278. TEST_ESP_OK(esp_eth_update_input_path(eth_handle, eth_recv_cb, &recv_info));
  279. EventBits_t bits = 0;
  280. EventGroupHandle_t eth_event_group = xEventGroupCreate();
  281. TEST_ASSERT(eth_event_group != NULL);
  282. TEST_ESP_OK(esp_event_loop_create_default());
  283. TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_event_group));
  284. // create a control frame to control test flow between the UT and the Python test script
  285. emac_frame_t *ctrl_pkt = calloc(1, 60);
  286. ctrl_pkt->proto = 0x2222;
  287. memset(ctrl_pkt->dest, 0xff, 6); // broadcast addr
  288. memcpy(ctrl_pkt->src, local_mac_addr, 6);
  289. // create dummy data packet used for traffic generation
  290. emac_frame_t *pkt = calloc(1, 1500);
  291. pkt->proto = 0x2222;
  292. // we don't care about dest MAC address much, however it is better to not be broadcast or multifcast to not flood
  293. // other network nodes
  294. memset(pkt->dest, 0xBA, 6);
  295. memcpy(pkt->src, local_mac_addr, 6);
  296. printf("EMAC start/stop stress test under heavy Tx traffic\n");
  297. for (int tx_i = 0; tx_i < 10; tx_i++) {
  298. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_start(eth_handle)); // start Ethernet driver state machine
  299. bits = xEventGroupWaitBits(eth_event_group, ETH_CONNECT_BIT, true, true, pdMS_TO_TICKS(3000));
  300. TEST_ASSERT((bits & ETH_CONNECT_BIT) == ETH_CONNECT_BIT);
  301. // even if PHY (IP101) indicates autonegotiation done and link up, it sometimes may miss few packets after atonego reset, hence wait a bit
  302. vTaskDelay(pdMS_TO_TICKS(100));
  303. // at first, check that Tx/Rx path works as expected by poking the test script
  304. // this also serves as main PASS/FAIL criteria
  305. ctrl_pkt->data[0] = POKE_REQ;
  306. ctrl_pkt->data[1] = tx_i;
  307. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_transmit(eth_handle, ctrl_pkt, 60));
  308. TEST_ASSERT(xSemaphoreTake(recv_info.mutex, pdMS_TO_TICKS(3000)));
  309. printf("Tx Test iteration %d\n", tx_i);
  310. // generate heavy Tx traffic
  311. printf("Note: transmit errors are expected...\n");
  312. for (int j = 0; j < 150; j++) {
  313. // return value is not checked on purpose since it is expected that it may fail time to time because
  314. // we may try to queue more packets than hardware is able to handle
  315. pkt->data[0] = j & 0xFF;
  316. esp_eth_transmit(eth_handle, pkt, 1500);
  317. }
  318. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_stop(eth_handle));
  319. bits = xEventGroupWaitBits(eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(3000));
  320. TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT);
  321. printf("Ethernet stopped\n");
  322. }
  323. printf("EMAC start/stop stress test under heavy Rx traffic\n");
  324. for (int rx_i = 0; rx_i < 10; rx_i++) {
  325. recv_info.rx_pkt_cnt = 0;
  326. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_start(eth_handle)); // start Ethernet driver state machine
  327. bits = xEventGroupWaitBits(eth_event_group, ETH_CONNECT_BIT, true, true, pdMS_TO_TICKS(3000));
  328. TEST_ASSERT((bits & ETH_CONNECT_BIT) == ETH_CONNECT_BIT);
  329. // even if PHY (IP101) indicates autonegotiation done and link up, it sometimes may miss few packets after atonego reset, hence wait a bit
  330. vTaskDelay(pdMS_TO_TICKS(100));
  331. ctrl_pkt->data[0] = POKE_REQ;
  332. ctrl_pkt->data[1] = rx_i;
  333. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_transmit(eth_handle, ctrl_pkt, 60));
  334. TEST_ASSERT(xSemaphoreTake(recv_info.mutex, pdMS_TO_TICKS(3000)));
  335. printf("Rx Test iteration %d\n", rx_i);
  336. vTaskDelay(pdMS_TO_TICKS(500));
  337. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_stop(eth_handle));
  338. bits = xEventGroupWaitBits(eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(3000));
  339. TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT);
  340. printf("Recv packets: %d\n", recv_info.rx_pkt_cnt);
  341. TEST_ASSERT_GREATER_THAN_INT32(0, recv_info.rx_pkt_cnt);
  342. printf("Ethernet stopped\n");
  343. }
  344. free(ctrl_pkt);
  345. free(pkt);
  346. TEST_ESP_OK(esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler));
  347. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete_default());
  348. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_uninstall(eth_handle));
  349. phy->del(phy);
  350. mac->del(mac);
  351. vEventGroupDelete(eth_event_group);
  352. vSemaphoreDelete(recv_info.mutex);
  353. }
  354. void app_main(void)
  355. {
  356. unity_run_menu();
  357. }