ethernet_example_main.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Ethernet Basic Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "tcpip_adapter.h"
  12. #include "esp_eth.h"
  13. #include "esp_event.h"
  14. #include "esp_log.h"
  15. #include "sdkconfig.h"
  16. static const char *TAG = "eth_example";
  17. /** Event handler for Ethernet events */
  18. static void eth_event_handler(void *arg, esp_event_base_t event_base,
  19. int32_t event_id, void *event_data)
  20. {
  21. uint8_t mac_addr[6] = {0};
  22. /* we can get the ethernet driver handle from event data */
  23. esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
  24. switch (event_id) {
  25. case ETHERNET_EVENT_CONNECTED:
  26. esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
  27. ESP_LOGI(TAG, "Ethernet Link Up");
  28. ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
  29. mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  30. break;
  31. case ETHERNET_EVENT_DISCONNECTED:
  32. ESP_LOGI(TAG, "Ethernet Link Down");
  33. break;
  34. case ETHERNET_EVENT_START:
  35. ESP_LOGI(TAG, "Ethernet Started");
  36. break;
  37. case ETHERNET_EVENT_STOP:
  38. ESP_LOGI(TAG, "Ethernet Stopped");
  39. break;
  40. default:
  41. break;
  42. }
  43. }
  44. /** Event handler for IP_EVENT_ETH_GOT_IP */
  45. static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
  46. int32_t event_id, void *event_data)
  47. {
  48. ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
  49. const tcpip_adapter_ip_info_t *ip_info = &event->ip_info;
  50. ESP_LOGI(TAG, "Ethernet Got IP Address");
  51. ESP_LOGI(TAG, "~~~~~~~~~~~");
  52. ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
  53. ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
  54. ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
  55. ESP_LOGI(TAG, "~~~~~~~~~~~");
  56. }
  57. void app_main(void)
  58. {
  59. tcpip_adapter_init();
  60. ESP_ERROR_CHECK(esp_event_loop_create_default());
  61. ESP_ERROR_CHECK(tcpip_adapter_set_default_eth_handlers());
  62. ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
  63. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
  64. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  65. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  66. #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  67. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
  68. #if CONFIG_EXAMPLE_ETH_PHY_IP101
  69. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  70. #elif CONFIG_EXAMPLE_ETH_PHY_RTL8201
  71. esp_eth_phy_t *phy = esp_eth_phy_new_rtl8201(&phy_config);
  72. #elif CONFIG_EXAMPLE_ETH_PHY_LAN8720
  73. esp_eth_phy_t *phy = esp_eth_phy_new_lan8720(&phy_config);
  74. #elif CONFIG_EXAMPLE_ETH_PHY_DP83848
  75. esp_eth_phy_t *phy = esp_eth_phy_new_dp83848(&phy_config);
  76. #endif
  77. #elif CONFIG_EXAMPLE_USE_SPI_ETHERNET
  78. gpio_install_isr_service(0);
  79. spi_device_handle_t spi_handle = NULL;
  80. spi_bus_config_t buscfg = {
  81. .miso_io_num = CONFIG_EXAMPLE_ETH_MISO_GPIO,
  82. .mosi_io_num = CONFIG_EXAMPLE_ETH_MOSI_GPIO,
  83. .sclk_io_num = CONFIG_EXAMPLE_ETH_SCLK_GPIO,
  84. .quadwp_io_num = -1,
  85. .quadhd_io_num = -1,
  86. };
  87. ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, 1));
  88. spi_device_interface_config_t devcfg = {
  89. .command_bits = 1,
  90. .address_bits = 7,
  91. .mode = 0,
  92. .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
  93. .spics_io_num = CONFIG_EXAMPLE_ETH_CS_GPIO,
  94. .queue_size = 20
  95. };
  96. ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle));
  97. /* dm9051 ethernet driver is based on spi driver, so need to specify the spi handle */
  98. mac_config.spi_hdl = spi_handle;
  99. esp_eth_mac_t *mac = esp_eth_mac_new_dm9051(&mac_config);
  100. esp_eth_phy_t *phy = esp_eth_phy_new_dm9051(&phy_config);
  101. #endif
  102. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
  103. esp_eth_handle_t eth_handle = NULL;
  104. ESP_ERROR_CHECK(esp_eth_driver_install(&config, &eth_handle));
  105. }