network_ethernet.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #ifdef NETWORK_ETHERNET_LOG_LEVEL
  2. #define LOG_LOCAL_LEVEL NETWORK_ETHERNET_LOG_LEVEL
  3. #endif
  4. #include "network_ethernet.h"
  5. #include "Config.h"
  6. #include "accessors.h"
  7. #include "esp_log.h"
  8. #include "freertos/timers.h"
  9. #include "globdefs.h"
  10. #include "messaging.h"
  11. #include "network_status.h"
  12. #include "tools.h"
  13. static char TAG[] = "network_ethernet";
  14. TimerHandle_t ETH_timer;
  15. esp_netif_t* eth_netif = NULL;
  16. EventGroupHandle_t ethernet_event_group;
  17. const int LINK_UP_BIT = BIT0;
  18. static network_ethernet_driver_t* network_driver = NULL;
  19. extern network_ethernet_detect_func_t DM9051_Detect, W5500_Detect, LAN8720_Detect;
  20. static network_ethernet_detect_func_t* drivers[] = {DM9051_Detect, W5500_Detect, LAN8720_Detect, NULL};
  21. #define ETH_TIMEOUT_MS (30 * 1000)
  22. network_ethernet_driver_t* network_ethernet_driver_autodetect() {
  23. sys_dev_eth_config * eth_config;
  24. sys_dev_eth_common* eth_common;
  25. if (!SYS_ETH(eth_config) || !SYS_ETH_COMMON(eth_common)) {
  26. ESP_LOGD(TAG, "Ethernet not configured");
  27. return NULL;
  28. }
  29. for (uint8_t i = _sys_dev_eth_models_MIN; i < _sys_dev_eth_models_MAX; i++) {
  30. network_ethernet_driver_t* found_driver = drivers[i](eth_config);
  31. if (found_driver) {
  32. ESP_LOGI(TAG, "Detected driver %s ", sys_dev_eth_models_name(eth_common->model));
  33. network_driver = found_driver;
  34. return found_driver;
  35. }
  36. }
  37. return NULL;
  38. }
  39. static void eth_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data);
  40. esp_netif_t* network_ethernet_get_interface() { return eth_netif; }
  41. bool network_ethernet_is_up() { return (xEventGroupGetBits(ethernet_event_group) & LINK_UP_BIT) != 0; }
  42. bool network_ethernet_enabled() { return network_driver != NULL && network_driver->handle != NULL; }
  43. bool network_ethernet_wait_for_link(uint16_t max_wait_ms) {
  44. if (!network_ethernet_enabled()) return false;
  45. bool link_up = (xEventGroupGetBits(ethernet_event_group) & LINK_UP_BIT) != 0;
  46. if (!link_up) {
  47. ESP_LOGD(TAG, "Waiting for Ethernet link to be established...");
  48. link_up = (xEventGroupWaitBits(ethernet_event_group, LINK_UP_BIT, pdFALSE, pdTRUE, max_wait_ms / portTICK_PERIOD_MS) & LINK_UP_BIT) != 0;
  49. if (!link_up) {
  50. ESP_LOGW(TAG, "Ethernet Link timeout.");
  51. } else {
  52. ESP_LOGI(TAG, "Ethernet Link Up!");
  53. }
  54. }
  55. return link_up;
  56. }
  57. static void ETH_Timeout(void* timer_id);
  58. void destroy_network_ethernet() {}
  59. static void network_ethernet_print_config(const network_ethernet_driver_t* eth_config) {
  60. sys_dev_eth_config * sys_eth;
  61. int mdc = -1, mdio = -1, rst = -1, intr = -1, cs = -1;
  62. uint32_t speed = 0;
  63. int8_t host = 0;
  64. if (SYS_ETH(sys_eth)) {
  65. rst = sys_eth->common.rst;
  66. if (sys_eth->which_ethType == sys_dev_eth_config_spi_tag) {
  67. cs = sys_eth->ethType.spi.cs;
  68. intr = sys_eth->ethType.spi.intr;
  69. speed = sys_eth->ethType.spi.speed;
  70. host = sys_eth->ethType.spi.host - sys_dev_common_hosts_Host0;
  71. } else if (sys_eth->which_ethType == sys_dev_eth_config_rmii_tag) {
  72. mdc = sys_eth->ethType.rmii.mdc;
  73. mdio = sys_eth->ethType.rmii.mdio;
  74. }
  75. }
  76. ESP_LOGI(TAG,
  77. "Ethernet config => model: %s, valid: %s, type: %s, mdc:%d, mdio:%d, rst:%d, intr:%d, "
  78. "cs:%d, speed:%d, host:%d",
  79. sys_dev_eth_models_name(eth_config->model), eth_config->valid ? "YES" : "NO", eth_config->spi ? "SPI" : "RMII", mdc, mdio, rst, intr, cs, speed,
  80. host);
  81. }
  82. void init_network_ethernet() {
  83. esp_err_t err = ESP_OK;
  84. ESP_LOGI(TAG, "Attempting to initialize Ethernet");
  85. sys_dev_eth_config * sys_eth;
  86. if (!SYS_ETH(sys_eth)) {
  87. ESP_LOGD(TAG, "No ethernet configured");
  88. return;
  89. }
  90. network_ethernet_driver_t* driver = network_ethernet_driver_autodetect();
  91. if (!driver || !driver->valid) {
  92. ESP_LOGI(TAG, "No Ethernet configuration, or configuration invalid");
  93. return;
  94. }
  95. network_driver->init_config(&platform->dev.eth);
  96. network_ethernet_print_config(driver);
  97. eth_netif = esp_netif_new(network_driver->cfg_netif);
  98. // esp_eth_set_default_handlers(eth_netif);
  99. esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL);
  100. ethernet_event_group = xEventGroupCreate();
  101. xEventGroupClearBits(ethernet_event_group, LINK_UP_BIT);
  102. spi_device_handle_t spi_handle = NULL;
  103. if (sys_eth->which_ethType == sys_dev_eth_config_spi_tag) {
  104. err = spi_bus_add_device(sys_eth->ethType.spi.host - sys_dev_common_hosts_Host0, network_driver->devcfg, &spi_handle);
  105. if (err != ESP_OK) {
  106. ESP_LOGE(TAG, "SPI host failed : %s", esp_err_to_name(err));
  107. }
  108. }
  109. if (err == ESP_OK) {
  110. err = network_driver->start(spi_handle, sys_eth);
  111. }
  112. if (err == ESP_OK) {
  113. uint8_t mac_address[6];
  114. esp_read_mac(mac_address, ESP_MAC_ETH);
  115. char* mac_string = alloc_get_formatted_mac_string(mac_address);
  116. ESP_LOGD(TAG, "Assigning mac address %s to ethernet interface", STR_OR_BLANK(mac_string));
  117. FREE_AND_NULL(mac_string);
  118. esp_eth_ioctl(network_driver->handle, ETH_CMD_S_MAC_ADDR, mac_address);
  119. }
  120. if (err == ESP_OK) {
  121. ESP_LOGD(TAG, "Attaching ethernet to network interface");
  122. err = esp_netif_attach(eth_netif, esp_eth_new_netif_glue(network_driver->handle));
  123. }
  124. if (err == ESP_OK) {
  125. ESP_LOGI(TAG, "Starting ethernet network");
  126. err = esp_eth_start(network_driver->handle);
  127. }
  128. if (err != ESP_OK) {
  129. messaging_post_message(MESSAGING_ERROR, MESSAGING_CLASS_SYSTEM, "Configuring Ethernet failed: %s", esp_err_to_name(err));
  130. if (spi_handle) {
  131. spi_bus_remove_device(spi_handle);
  132. }
  133. network_driver->handle = NULL;
  134. }
  135. }
  136. void network_ethernet_start_timer() { ETH_timer = xTimerCreate("ETH check", pdMS_TO_TICKS(ETH_TIMEOUT_MS), pdFALSE, NULL, ETH_Timeout); }
  137. /** Event handler for Ethernet events */
  138. static void eth_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
  139. uint8_t mac_addr[6] = {0};
  140. /* we can get the ethernet driver handle from event data */
  141. if (event_base == ETH_EVENT) {
  142. esp_eth_handle_t eth_handle = *(esp_eth_handle_t*)event_data;
  143. switch (event_id) {
  144. case ETHERNET_EVENT_CONNECTED:
  145. xEventGroupSetBits(ethernet_event_group, LINK_UP_BIT);
  146. esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
  147. ESP_LOGI(TAG, "");
  148. ESP_LOGI(TAG, "Ethernet Link Up, HW Addr %02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4],
  149. mac_addr[5]);
  150. network_async_link_up();
  151. break;
  152. case ETHERNET_EVENT_DISCONNECTED:
  153. ESP_LOGI(TAG, "Ethernet Link Down");
  154. xEventGroupClearBits(ethernet_event_group, LINK_UP_BIT);
  155. network_async_link_down();
  156. break;
  157. case ETHERNET_EVENT_START:
  158. ESP_LOGI(TAG, "Ethernet Started. Setting host name");
  159. network_set_hostname(eth_netif);
  160. network_async_success();
  161. break;
  162. case ETHERNET_EVENT_STOP:
  163. ESP_LOGI(TAG, "Ethernet Stopped");
  164. break;
  165. default:
  166. break;
  167. }
  168. }
  169. }
  170. static void ETH_Timeout(void* timer_id) { network_async_fail(); }