esp_eth_test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. typedef struct {
  18. uint8_t dest[6];
  19. uint8_t src[6];
  20. uint16_t proto;
  21. uint8_t data[];
  22. } __attribute__((__packed__)) emac_frame_t;
  23. TEST_CASE("start_and_stop", "[esp_eth]")
  24. {
  25. void eth_event_handler(void *arg, esp_event_base_t event_base,
  26. int32_t event_id, void *event_data){
  27. EventGroupHandle_t eth_event_group = (EventGroupHandle_t)arg;
  28. switch (event_id) {
  29. case ETHERNET_EVENT_CONNECTED:
  30. xEventGroupSetBits(eth_event_group, ETH_CONNECT_BIT);
  31. break;
  32. case ETHERNET_EVENT_START:
  33. xEventGroupSetBits(eth_event_group, ETH_START_BIT);
  34. break;
  35. case ETHERNET_EVENT_STOP:
  36. xEventGroupSetBits(eth_event_group, ETH_STOP_BIT);
  37. break;
  38. default:
  39. break;
  40. }
  41. }
  42. EventGroupHandle_t eth_event_group = xEventGroupCreate();
  43. TEST_ASSERT(eth_event_group != NULL);
  44. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); // apply default MAC configuration
  45. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config); // create MAC instance
  46. TEST_ASSERT_NOT_NULL(mac);
  47. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); // apply default PHY configuration
  48. #if defined(CONFIG_TARGET_ETH_PHY_DEVICE_IP101)
  49. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config); // create PHY instance
  50. #elif defined(CONFIG_TARGET_ETH_PHY_DEVICE_LAN8720)
  51. esp_eth_phy_t *phy = esp_eth_phy_new_lan8720(&phy_config);
  52. #endif
  53. TEST_ASSERT_NOT_NULL(phy);
  54. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); // apply default driver configuration
  55. esp_eth_handle_t eth_handle = NULL; // after driver installed, we will get the handle of the driver
  56. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_install(&config, &eth_handle)); // install driver
  57. TEST_ASSERT_NOT_NULL(eth_handle);
  58. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create_default());
  59. TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_event_group));
  60. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_start(eth_handle)); // start Ethernet driver state machine
  61. EventBits_t bits = 0;
  62. bits = xEventGroupWaitBits(eth_event_group, ETH_START_BIT, true, true, pdMS_TO_TICKS(3000));
  63. TEST_ASSERT((bits & ETH_START_BIT) == ETH_START_BIT);
  64. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_stop(eth_handle));
  65. bits = xEventGroupWaitBits(eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(3000));
  66. TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT);
  67. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete_default());
  68. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_uninstall(eth_handle));
  69. phy->del(phy);
  70. mac->del(mac);
  71. vEventGroupDelete(eth_event_group);
  72. }
  73. TEST_CASE("get_set_mac", "[esp_eth]")
  74. {
  75. void eth_event_handler(void *arg, esp_event_base_t event_base,
  76. int32_t event_id, void *event_data){
  77. SemaphoreHandle_t mutex = (SemaphoreHandle_t)arg;
  78. switch (event_id) {
  79. case ETHERNET_EVENT_CONNECTED:
  80. xSemaphoreGive(mutex);
  81. break;
  82. default:
  83. break;
  84. }
  85. }
  86. SemaphoreHandle_t mutex = xSemaphoreCreateBinary();
  87. TEST_ASSERT_NOT_NULL(mutex);
  88. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); // apply default MAC configuration
  89. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config); // create MAC instance
  90. TEST_ASSERT_NOT_NULL(mac);
  91. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); // apply default PHY configuration
  92. #if defined(CONFIG_TARGET_ETH_PHY_DEVICE_IP101)
  93. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config); // create PHY instance
  94. #elif defined(CONFIG_TARGET_ETH_PHY_DEVICE_LAN8720)
  95. esp_eth_phy_t *phy = esp_eth_phy_new_lan8720(&phy_config);
  96. #endif
  97. TEST_ASSERT_NOT_NULL(phy);
  98. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); // apply default driver configuration
  99. esp_eth_handle_t eth_handle = NULL; // after driver installed, we will get the handle of the driver
  100. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_install(&config, &eth_handle)); // install driver
  101. TEST_ASSERT_NOT_NULL(eth_handle);
  102. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create_default());
  103. TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, mutex));
  104. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_start(eth_handle)); // start Ethernet driver state machine
  105. TEST_ASSERT(xSemaphoreTake(mutex, pdMS_TO_TICKS(3000)));
  106. uint8_t mac_addr[6] = {};
  107. TEST_ASSERT_EQUAL(ESP_OK, mac->get_addr(mac, mac_addr));
  108. TEST_ASSERT_BITS(0b00000011, 0b00, mac_addr[0]); // Check UL&IG, should be UI
  109. mac_addr[5] ^= mac_addr[4];
  110. TEST_ASSERT_EQUAL(ESP_OK, mac->set_addr(mac, mac_addr));
  111. uint8_t new_mac_addr[6] = {};
  112. TEST_ASSERT_EQUAL(ESP_OK, mac->get_addr(mac, new_mac_addr));
  113. TEST_ASSERT_EQUAL(0, memcmp(mac_addr, new_mac_addr, 6));
  114. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_stop(eth_handle));
  115. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete_default());
  116. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_uninstall(eth_handle));
  117. phy->del(phy);
  118. mac->del(mac);
  119. vSemaphoreDelete(mutex);
  120. }
  121. TEST_CASE("ethernet_broadcast_transmit", "[esp_eth]")
  122. {
  123. void eth_event_handler(void *arg, esp_event_base_t event_base,
  124. int32_t event_id, void *event_data){
  125. SemaphoreHandle_t mutex = (SemaphoreHandle_t)arg;
  126. switch (event_id) {
  127. case ETHERNET_EVENT_CONNECTED:
  128. xSemaphoreGive(mutex);
  129. break;
  130. default:
  131. break;
  132. }
  133. }
  134. SemaphoreHandle_t mutex = xSemaphoreCreateBinary();
  135. TEST_ASSERT_NOT_NULL(mutex);
  136. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); // apply default MAC configuration
  137. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config); // create MAC instance
  138. TEST_ASSERT_NOT_NULL(mac);
  139. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); // apply default PHY configuration
  140. #if defined(CONFIG_TARGET_ETH_PHY_DEVICE_IP101)
  141. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config); // create PHY instance
  142. #elif defined(CONFIG_TARGET_ETH_PHY_DEVICE_LAN8720)
  143. esp_eth_phy_t *phy = esp_eth_phy_new_lan8720(&phy_config);
  144. #endif
  145. TEST_ASSERT_NOT_NULL(phy);
  146. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); // apply default driver configuration
  147. esp_eth_handle_t eth_handle = NULL; // after driver installed, we will get the handle of the driver
  148. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_install(&config, &eth_handle)); // install driver
  149. TEST_ASSERT_NOT_NULL(eth_handle);
  150. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create_default());
  151. TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, mutex));
  152. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_start(eth_handle)); // start Ethernet driver state machine
  153. TEST_ASSERT(xSemaphoreTake(mutex, pdMS_TO_TICKS(3000)));
  154. emac_frame_t *pkt = malloc(1024);
  155. pkt->proto = 0x2222;
  156. memset(pkt->dest, 0xff, 6); // broadcast addr
  157. for (int i = 128; i < 1024; ++i){
  158. ((uint8_t*)pkt)[i] = i & 0xff;
  159. }
  160. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_transmit(eth_handle, pkt, 1024));
  161. vTaskDelay(pdMS_TO_TICKS(100));
  162. free(pkt);
  163. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_stop(eth_handle));
  164. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete_default());
  165. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_uninstall(eth_handle));
  166. phy->del(phy);
  167. mac->del(mac);
  168. vSemaphoreDelete(mutex);
  169. }
  170. static uint8_t local_mac_addr[6] = {};
  171. esp_err_t l2_packet_txrx_test_cb(esp_eth_handle_t hdl, uint8_t *buffer, uint32_t length, void *priv) {
  172. EventGroupHandle_t eth_event_group = (EventGroupHandle_t)priv;
  173. emac_frame_t *pkt = (emac_frame_t *) buffer;
  174. // check header
  175. if (pkt->proto == 0x2222 && length == 1024) {
  176. // check content
  177. for (int i = 128; i < 1024; ++i) {
  178. if (buffer[i] != (i & 0xff)) {
  179. return ESP_OK;
  180. }
  181. }
  182. if (memcmp(pkt->dest, "\xff\xff\xff\xff\xff\xff", 6) == 0) {
  183. xEventGroupSetBits(eth_event_group, ETH_BROADCAST_RECV_BIT);
  184. }
  185. if (pkt->dest[0] & 0x1) {
  186. xEventGroupSetBits(eth_event_group, ETH_MULTICAST_RECV_BIT);
  187. }
  188. if (memcmp(pkt->dest, local_mac_addr, 6) == 0) {
  189. xEventGroupSetBits(eth_event_group, ETH_UNICAST_RECV_BIT);
  190. }
  191. }
  192. return ESP_OK;
  193. };
  194. TEST_CASE("recv_pkt", "[esp_eth]")
  195. {
  196. EventGroupHandle_t eth_event_group = xEventGroupCreate();
  197. TEST_ASSERT(eth_event_group != NULL);
  198. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); // apply default MAC configuration
  199. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config); // create MAC instance
  200. TEST_ASSERT_NOT_NULL(mac);
  201. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); // apply default PHY configuration
  202. #if defined(CONFIG_TARGET_ETH_PHY_DEVICE_IP101)
  203. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config); // create PHY instance
  204. #elif defined(CONFIG_TARGET_ETH_PHY_DEVICE_LAN8720)
  205. esp_eth_phy_t *phy = esp_eth_phy_new_lan8720(&phy_config);
  206. #endif
  207. TEST_ASSERT_NOT_NULL(phy);
  208. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); // apply default driver configuration
  209. esp_eth_handle_t eth_handle = NULL; // after driver installed, we will get the handle of the driver
  210. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_install(&config, &eth_handle)); // install driver
  211. TEST_ASSERT_NOT_NULL(eth_handle);
  212. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create_default());
  213. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_start(eth_handle)); // start Ethernet driver state machine
  214. TEST_ASSERT_EQUAL(ESP_OK, mac->get_addr(mac, local_mac_addr));
  215. // test app will parse the DUT MAC from this line of log output
  216. printf("DUT MAC: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", local_mac_addr[0], local_mac_addr[1], local_mac_addr[2],
  217. local_mac_addr[3], local_mac_addr[4], local_mac_addr[5]);
  218. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_update_input_path(eth_handle, l2_packet_txrx_test_cb, eth_event_group));
  219. EventBits_t bits = 0;
  220. bits = xEventGroupWaitBits(eth_event_group, ETH_BROADCAST_RECV_BIT | ETH_MULTICAST_RECV_BIT | ETH_UNICAST_RECV_BIT,
  221. true, true, pdMS_TO_TICKS(3000));
  222. TEST_ASSERT((bits & (ETH_BROADCAST_RECV_BIT | ETH_MULTICAST_RECV_BIT | ETH_UNICAST_RECV_BIT)) ==
  223. (ETH_BROADCAST_RECV_BIT | ETH_MULTICAST_RECV_BIT | ETH_UNICAST_RECV_BIT));
  224. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_stop(eth_handle));
  225. TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_delete_default());
  226. TEST_ASSERT_EQUAL(ESP_OK, esp_eth_driver_uninstall(eth_handle));
  227. phy->del(phy);
  228. mac->del(mac);
  229. vEventGroupDelete(eth_event_group);
  230. }
  231. void app_main(void)
  232. {
  233. unity_run_menu();
  234. }