network_ethernet.c 6.9 KB

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