network_ethernet.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_LOGD(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: \n model: %s, valid: %s, type: %s, mdc:%d, mdio:%d, rst:%d, mosi:%d, miso:%d, intr:%d, cs:%d, speed:%d, clk:%d, host:%s(%d)",
  84. eth_config->model, eth_config->valid?"YES":"NO",eth_config->spi?"SPI":"RMII", eth_config->mdc, eth_config->mdio, eth_config->rst, eth_config->mosi, eth_config->miso, eth_config->intr, eth_config->cs, eth_config->speed, eth_config->clk, eth_config->host==0?"SPI1":eth_config->host==1?"SPI2":eth_config->host==2?"SPI3":"",eth_config->host);
  85. }
  86. void init_network_ethernet() {
  87. esp_err_t err = ESP_OK;
  88. eth_config_t eth;
  89. ESP_LOGI(TAG, "Attempting to initialize Ethernet");
  90. config_eth_init(&eth);
  91. if(!eth.valid){
  92. ESP_LOGI(TAG,"No Ethernet configuration, or configuration invalid");
  93. return;
  94. }
  95. network_driver->init_config(&eth);
  96. network_ethernet_print_config(&eth);
  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 (network_driver->spi) {
  104. if (err == ESP_OK) {
  105. ESP_LOGI(TAG, "Adding ethernet SPI on host %d (SPI%d) with mosi %d and miso %d", eth.host, eth.host+1, eth.mosi, eth.miso);
  106. err = spi_bus_add_device(eth.host, network_driver->devcfg, &spi_handle);
  107. }
  108. if (err != ESP_OK) {
  109. ESP_LOGE(TAG, "SPI host failed : %s", esp_err_to_name(err));
  110. }
  111. }
  112. if (err == ESP_OK) {
  113. err = network_driver->start(spi_handle,&eth);
  114. }
  115. if(err == ESP_OK){
  116. uint8_t mac_address[6];
  117. esp_read_mac(mac_address,ESP_MAC_ETH);
  118. char * mac_string=network_manager_alloc_get_mac_string(mac_address);
  119. ESP_LOGD(TAG,"Assigning mac address %s to ethernet interface", STR_OR_BLANK(mac_string));
  120. FREE_AND_NULL(mac_string);
  121. esp_eth_ioctl(network_driver->handle, ETH_CMD_S_MAC_ADDR, mac_address);
  122. }
  123. if (err == ESP_OK) {
  124. ESP_LOGD(TAG, "Attaching ethernet to network interface");
  125. err = esp_netif_attach(eth_netif, esp_eth_new_netif_glue(network_driver->handle));
  126. }
  127. if (err == ESP_OK) {
  128. ESP_LOGI(TAG, "Starting ethernet network");
  129. err = esp_eth_start(network_driver->handle);
  130. }
  131. if (err != ESP_OK) {
  132. messaging_post_message(MESSAGING_ERROR, MESSAGING_CLASS_SYSTEM, "Configuring Ethernet failed: %s", esp_err_to_name(err));
  133. if(spi_handle) {
  134. spi_bus_remove_device(spi_handle);
  135. }
  136. network_driver->handle = NULL;
  137. }
  138. }
  139. void network_ethernet_start_timer() {
  140. ETH_timer = xTimerCreate("ETH check", pdMS_TO_TICKS(ETH_TIMEOUT_MS), pdFALSE, NULL, ETH_Timeout);
  141. }
  142. /** Event handler for Ethernet events */
  143. static void eth_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
  144. uint8_t mac_addr[6] = {0};
  145. /* we can get the ethernet driver handle from event data */
  146. if (event_base == ETH_EVENT) {
  147. esp_eth_handle_t eth_handle = *(esp_eth_handle_t*)event_data;
  148. switch (event_id) {
  149. case ETHERNET_EVENT_CONNECTED:
  150. xEventGroupSetBits(ethernet_event_group, LINK_UP_BIT);
  151. esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
  152. ESP_LOGI(TAG, "");
  153. 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]);
  154. network_async_link_up();
  155. break;
  156. case ETHERNET_EVENT_DISCONNECTED:
  157. ESP_LOGI(TAG, "Ethernet Link Down");
  158. xEventGroupClearBits(ethernet_event_group, LINK_UP_BIT);
  159. network_async_link_down();
  160. break;
  161. case ETHERNET_EVENT_START:
  162. ESP_LOGI(TAG, "Ethernet Started. Setting host name");
  163. network_set_hostname(eth_netif);
  164. network_async_success();
  165. break;
  166. case ETHERNET_EVENT_STOP:
  167. ESP_LOGI(TAG, "Ethernet Stopped");
  168. break;
  169. default:
  170. break;
  171. }
  172. }
  173. }
  174. static void ETH_Timeout(void* timer_id) {
  175. network_async_fail();
  176. }