ethernet_example_main.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "esp_netif.h"
  12. #include "esp_eth.h"
  13. #include "esp_event.h"
  14. #include "esp_log.h"
  15. #include "ethernet_init.h"
  16. #include "sdkconfig.h"
  17. static const char *TAG = "eth_example";
  18. /** Event handler for Ethernet events */
  19. static void eth_event_handler(void *arg, esp_event_base_t event_base,
  20. int32_t event_id, void *event_data)
  21. {
  22. uint8_t mac_addr[6] = {0};
  23. /* we can get the ethernet driver handle from event data */
  24. esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
  25. switch (event_id) {
  26. case ETHERNET_EVENT_CONNECTED:
  27. esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
  28. ESP_LOGI(TAG, "Ethernet Link Up");
  29. ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
  30. mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  31. break;
  32. case ETHERNET_EVENT_DISCONNECTED:
  33. ESP_LOGI(TAG, "Ethernet Link Down");
  34. break;
  35. case ETHERNET_EVENT_START:
  36. ESP_LOGI(TAG, "Ethernet Started");
  37. break;
  38. case ETHERNET_EVENT_STOP:
  39. ESP_LOGI(TAG, "Ethernet Stopped");
  40. break;
  41. default:
  42. break;
  43. }
  44. }
  45. /** Event handler for IP_EVENT_ETH_GOT_IP */
  46. static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
  47. int32_t event_id, void *event_data)
  48. {
  49. ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
  50. const esp_netif_ip_info_t *ip_info = &event->ip_info;
  51. ESP_LOGI(TAG, "Ethernet Got IP Address");
  52. ESP_LOGI(TAG, "~~~~~~~~~~~");
  53. ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
  54. ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
  55. ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
  56. ESP_LOGI(TAG, "~~~~~~~~~~~");
  57. }
  58. void app_main(void)
  59. {
  60. // Initialize Ethernet driver
  61. uint8_t eth_port_cnt = 0;
  62. esp_eth_handle_t *eth_handles;
  63. ESP_ERROR_CHECK(example_eth_init(&eth_handles, &eth_port_cnt));
  64. // Initialize TCP/IP network interface aka the esp-netif (should be called only once in application)
  65. ESP_ERROR_CHECK(esp_netif_init());
  66. // Create default event loop that running in background
  67. ESP_ERROR_CHECK(esp_event_loop_create_default());
  68. // Create instance(s) of esp-netif for Ethernet(s)
  69. if (eth_port_cnt == 1) {
  70. // Use ESP_NETIF_DEFAULT_ETH when just one Ethernet interface is used and you don't need to modify
  71. // default esp-netif configuration parameters.
  72. esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
  73. esp_netif_t *eth_netif = esp_netif_new(&cfg);
  74. // Attach Ethernet driver to TCP/IP stack
  75. ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handles[0])));
  76. } else {
  77. // Use ESP_NETIF_INHERENT_DEFAULT_ETH when multiple Ethernet interfaces are used and so you need to modify
  78. // esp-netif configuration parameters for each interface (name, priority, etc.).
  79. esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
  80. esp_netif_config_t cfg_spi = {
  81. .base = &esp_netif_config,
  82. .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH
  83. };
  84. char if_key_str[10];
  85. char if_desc_str[10];
  86. char num_str[3];
  87. for (int i = 0; i < eth_port_cnt; i++) {
  88. itoa(i, num_str, 10);
  89. strcat(strcpy(if_key_str, "ETH_"), num_str);
  90. strcat(strcpy(if_desc_str, "eth"), num_str);
  91. esp_netif_config.if_key = if_key_str;
  92. esp_netif_config.if_desc = if_desc_str;
  93. esp_netif_config.route_prio -= i*5;
  94. esp_netif_t *eth_netif = esp_netif_new(&cfg_spi);
  95. // Attach Ethernet driver to TCP/IP stack
  96. ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handles[i])));
  97. }
  98. }
  99. // Register user defined event handers
  100. ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
  101. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
  102. // Start Ethernet driver state machine
  103. for (int i = 0; i < eth_port_cnt; i++) {
  104. ESP_ERROR_CHECK(esp_eth_start(eth_handles[i]));
  105. }
  106. }