test_emac_deinit.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * @brief This test has just run in the ESP32_Ethernet_V3 board, which featured
  3. * in PoE submodule and TLK110 PHY. The 50MHz clock used by MAC and PHY is
  4. * supplied by external oscillator through GPIO0.
  5. *
  6. * @file test_emac_deinit.c
  7. * @author morris
  8. * @date 2018-08-24
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "freertos/FreeRTOS.h"
  13. #include "freertos/task.h"
  14. #include "freertos/event_groups.h"
  15. #include "esp_system.h"
  16. #include "esp_err.h"
  17. #include "esp_event_loop.h"
  18. #include "esp_event.h"
  19. #include "esp_log.h"
  20. #include "esp_eth.h"
  21. #include "unity.h"
  22. #include "rom/gpio.h"
  23. #include "tcpip_adapter.h"
  24. #include "driver/gpio.h"
  25. #include "driver/periph_ctrl.h"
  26. #include "eth_phy/phy_tlk110.h"
  27. #define DEFAULT_ETHERNET_PHY_CONFIG phy_tlk110_default_ethernet_config
  28. static const char *TAG = "eth_test_deinit";
  29. #define PIN_PHY_POWER 17
  30. #define PIN_SMI_MDC 23
  31. #define PIN_SMI_MDIO 18
  32. #define CONFIG_PHY_ADDRESS 31
  33. #define CONFIG_PHY_CLOCK_MODE 0
  34. static EventGroupHandle_t eth_event_group = NULL;
  35. static const int GOTIP_BIT = BIT0;
  36. static void phy_device_power_enable_via_gpio(bool enable)
  37. {
  38. if (!enable) {
  39. DEFAULT_ETHERNET_PHY_CONFIG.phy_power_enable(false);
  40. }
  41. gpio_pad_select_gpio(PIN_PHY_POWER);
  42. gpio_set_direction(PIN_PHY_POWER, GPIO_MODE_OUTPUT);
  43. if (enable == true) {
  44. gpio_set_level(PIN_PHY_POWER, 1);
  45. ESP_LOGI(TAG, "power on ethernet phy");
  46. } else {
  47. gpio_set_level(PIN_PHY_POWER, 0);
  48. ESP_LOGI(TAG, "power off ethernet phy");
  49. }
  50. vTaskDelay(1); // Allow the power up/down to take effect, min 300us
  51. if (enable) {
  52. /* operates the default phy-specific power on function */
  53. DEFAULT_ETHERNET_PHY_CONFIG.phy_power_enable(true);
  54. }
  55. }
  56. static void eth_gpio_config_rmii(void)
  57. {
  58. phy_rmii_configure_data_interface_pins();
  59. phy_rmii_smi_configure_pins(PIN_SMI_MDC, PIN_SMI_MDIO);
  60. }
  61. static esp_err_t eth_event_handler(void *ctx, system_event_t *event)
  62. {
  63. tcpip_adapter_ip_info_t ip;
  64. switch (event->event_id) {
  65. case SYSTEM_EVENT_ETH_CONNECTED:
  66. ESP_LOGI(TAG, "Ethernet Link Up");
  67. break;
  68. case SYSTEM_EVENT_ETH_DISCONNECTED:
  69. ESP_LOGI(TAG, "Ethernet Link Down");
  70. break;
  71. case SYSTEM_EVENT_ETH_START:
  72. ESP_LOGI(TAG, "Ethernet Started");
  73. break;
  74. case SYSTEM_EVENT_ETH_GOT_IP:
  75. memset(&ip, 0, sizeof(tcpip_adapter_ip_info_t));
  76. ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip));
  77. ESP_LOGI(TAG, "Ethernet Got IP Addr");
  78. ESP_LOGI(TAG, "~~~~~~~~~~~");
  79. ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip.ip));
  80. ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip.netmask));
  81. ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip.gw));
  82. ESP_LOGI(TAG, "~~~~~~~~~~~");
  83. xEventGroupSetBits(eth_event_group, GOTIP_BIT);
  84. break;
  85. case SYSTEM_EVENT_ETH_STOP:
  86. ESP_LOGI(TAG, "Ethernet Stopped");
  87. break;
  88. default:
  89. break;
  90. }
  91. return ESP_OK;
  92. }
  93. TEST_CASE("start event loop", "[ethernet][ignore]")
  94. {
  95. eth_event_group = xEventGroupCreate();
  96. tcpip_adapter_init();
  97. ESP_ERROR_CHECK(esp_event_loop_init(eth_event_handler, NULL));
  98. }
  99. TEST_CASE("test emac deinit", "[ethernet][ignore]")
  100. {
  101. eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG;
  102. config.phy_addr = CONFIG_PHY_ADDRESS;
  103. config.gpio_config = eth_gpio_config_rmii;
  104. config.tcpip_input = tcpip_adapter_eth_input;
  105. config.clock_mode = CONFIG_PHY_CLOCK_MODE;
  106. config.phy_power_enable = phy_device_power_enable_via_gpio;
  107. ESP_ERROR_CHECK(esp_eth_init(&config));
  108. ESP_ERROR_CHECK(esp_eth_enable());
  109. xEventGroupWaitBits(eth_event_group, GOTIP_BIT, true, true, portMAX_DELAY);
  110. vTaskDelay(15000 / portTICK_RATE_MS);
  111. ESP_ERROR_CHECK(esp_eth_disable());
  112. ESP_ERROR_CHECK(esp_eth_deinit());
  113. }