network_ethernet.c 7.6 KB

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