2
0

network_ethernet.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 "globdefs.h"
  7. #include "messaging.h"
  8. #include "network_status.h"
  9. #include "platform_config.h"
  10. #include "trace.h"
  11. #include "accessors.h"
  12. #include "esp_log.h"
  13. //#include "dnserver.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 const char* known_drivers[] = {"DM9051", "W5500", "LAN8720", NULL};
  20. static network_ethernet_driver_t* network_driver = NULL;
  21. extern network_ethernet_detect_func_t DM9051_Detect, W5500_Detect, LAN8720_Detect;
  22. static network_ethernet_detect_func_t* drivers[] = {DM9051_Detect, W5500_Detect, LAN8720_Detect, NULL};
  23. #define ETH_TIMEOUT_MS (30 * 1000)
  24. /****************************************************************************************
  25. *
  26. */
  27. const char* network_ethernet_conf_get_driver_name(const char* driver) {
  28. for (uint8_t i = 0; known_drivers[i] != NULL && strlen(known_drivers[i]) > 0; i++) {
  29. if (strcasestr(driver, known_drivers[i])) {
  30. return known_drivers[i];
  31. }
  32. }
  33. return NULL;
  34. }
  35. /****************************************************************************************
  36. *
  37. */
  38. bool network_ethernet_is_valid_driver(const char* driver) {
  39. return network_ethernet_conf_get_driver_name(driver) != NULL;
  40. }
  41. network_ethernet_driver_t* network_ethernet_driver_autodetect(const char* Driver) {
  42. if (!Driver)
  43. return NULL;
  44. for (int i = 0; drivers[i]; i++) {
  45. network_ethernet_driver_t* found_driver = drivers[i](Driver);
  46. if (found_driver) {
  47. ESP_LOGD(TAG, "Detected driver %s ", Driver);
  48. network_driver = found_driver;
  49. return found_driver;
  50. }
  51. }
  52. return NULL;
  53. }
  54. static void eth_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data);
  55. esp_netif_t *network_ethernet_get_interface(){
  56. return eth_netif;
  57. }
  58. bool network_ethernet_is_up() {
  59. return (xEventGroupGetBits(ethernet_event_group) & LINK_UP_BIT)!=0;
  60. }
  61. bool network_ethernet_enabled() {
  62. return network_driver !=NULL && network_driver->handle != NULL;
  63. }
  64. bool network_ethernet_wait_for_link(uint16_t max_wait_ms){
  65. if(!network_ethernet_enabled()) return false;
  66. bool link_up=(xEventGroupGetBits(ethernet_event_group) & LINK_UP_BIT)!=0;
  67. if(!link_up){
  68. ESP_LOGD(TAG,"Waiting for Ethernet link to be established...");
  69. link_up = (xEventGroupWaitBits(ethernet_event_group, LINK_UP_BIT,pdFALSE, pdTRUE, max_wait_ms / portTICK_PERIOD_MS)& LINK_UP_BIT)!=0;
  70. if(!link_up){
  71. ESP_LOGW(TAG,"Ethernet Link timeout.");
  72. }
  73. else
  74. {
  75. ESP_LOGI(TAG,"Ethernet Link Up!");
  76. }
  77. }
  78. return link_up;
  79. }
  80. static void ETH_Timeout(void* timer_id);
  81. void destroy_network_ethernet() {
  82. }
  83. static void network_ethernet_print_config(const eth_config_t* eth_config) {
  84. 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)",
  85. 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);
  86. }
  87. void init_network_ethernet() {
  88. esp_err_t err = ESP_OK;
  89. eth_config_t eth;
  90. ESP_LOGI(TAG, "Attempting to initialize Ethernet");
  91. config_eth_init(&eth);
  92. if(!eth.valid){
  93. ESP_LOGI(TAG,"No Ethernet configuration, or configuration invalid");
  94. return;
  95. }
  96. network_driver->init_config(&eth);
  97. network_ethernet_print_config(&eth);
  98. eth_netif = esp_netif_new(network_driver->cfg_netif);
  99. esp_eth_set_default_handlers(eth_netif);
  100. esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL);
  101. ethernet_event_group = xEventGroupCreate();
  102. xEventGroupClearBits(ethernet_event_group, LINK_UP_BIT);
  103. spi_device_handle_t spi_handle = NULL;
  104. if (network_driver->spi) {
  105. spi_host_device_t host = SPI3_HOST;
  106. if (eth.host != -1) {
  107. // don't use system's shared SPI
  108. spi_bus_config_t buscfg = {
  109. .miso_io_num = eth.miso,
  110. .mosi_io_num = eth.mosi,
  111. .sclk_io_num = eth.clk,
  112. .quadwp_io_num = -1,
  113. .quadhd_io_num = -1,
  114. };
  115. // can't use SPI0
  116. if (eth.host == 0)
  117. {
  118. ESP_LOGW(TAG,"Cannot use SPI1 host. Defaulting to SPI2");
  119. host = SPI2_HOST;
  120. }
  121. else {
  122. host = eth.host;
  123. }
  124. ESP_LOGI(TAG, "Initializing SPI bus on host %d (SPI%d) with mosi %d and miso %d", host,host+1, eth.mosi, eth.miso);
  125. err = spi_bus_initialize(host, &buscfg, SPI_DMA_CH_AUTO);
  126. if (err != ESP_OK) {
  127. ESP_LOGE(TAG, "SPI bus init failed : %s", esp_err_to_name(err));
  128. }
  129. } else {
  130. // when we use shared SPI, we assume it has been initialized
  131. host = spi_system_host;
  132. }
  133. if (err == ESP_OK) {
  134. ESP_LOGI(TAG, "Adding ethernet SPI on host %d (SPI%d) with mosi %d and miso %d", host,host+1, eth.mosi, eth.miso);
  135. err = spi_bus_add_device(host, network_driver->devcfg, &spi_handle);
  136. }
  137. if (err != ESP_OK) {
  138. ESP_LOGE(TAG, "SPI host failed : %s", esp_err_to_name(err));
  139. }
  140. }
  141. if (err == ESP_OK) {
  142. err = network_driver->start(spi_handle,&eth);
  143. }
  144. if(err == ESP_OK){
  145. uint8_t mac_address[6];
  146. esp_read_mac(mac_address,ESP_MAC_ETH);
  147. char * mac_string=network_manager_alloc_get_mac_string(mac_address);
  148. ESP_LOGD(TAG,"Assigning mac address %s to ethernet interface", STR_OR_BLANK(mac_string));
  149. FREE_AND_NULL(mac_string);
  150. esp_eth_ioctl(network_driver->handle, ETH_CMD_S_MAC_ADDR, mac_address);
  151. }
  152. if (err == ESP_OK) {
  153. ESP_LOGD(TAG, "Attaching ethernet to network interface");
  154. err = esp_netif_attach(eth_netif, esp_eth_new_netif_glue(network_driver->handle));
  155. }
  156. if (err == ESP_OK) {
  157. ESP_LOGI(TAG, "Starting ethernet network");
  158. err = esp_eth_start(network_driver->handle);
  159. }
  160. if (err != ESP_OK) {
  161. messaging_post_message(MESSAGING_ERROR, MESSAGING_CLASS_SYSTEM, "Configuring Ethernet failed: %s", esp_err_to_name(err));
  162. if(spi_handle) {
  163. spi_bus_remove_device(spi_handle);
  164. }
  165. network_driver->handle = NULL;
  166. }
  167. }
  168. void network_ethernet_start_timer() {
  169. ETH_timer = xTimerCreate("ETH check", pdMS_TO_TICKS(ETH_TIMEOUT_MS), pdFALSE, NULL, ETH_Timeout);
  170. }
  171. /** Event handler for Ethernet events */
  172. static void eth_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
  173. uint8_t mac_addr[6] = {0};
  174. /* we can get the ethernet driver handle from event data */
  175. if (event_base == ETH_EVENT) {
  176. esp_eth_handle_t eth_handle = *(esp_eth_handle_t*)event_data;
  177. switch (event_id) {
  178. case ETHERNET_EVENT_CONNECTED:
  179. xEventGroupSetBits(ethernet_event_group, LINK_UP_BIT);
  180. esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
  181. ESP_LOGI(TAG, "");
  182. 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]);
  183. network_async_link_up();
  184. break;
  185. case ETHERNET_EVENT_DISCONNECTED:
  186. ESP_LOGI(TAG, "Ethernet Link Down");
  187. xEventGroupClearBits(ethernet_event_group, LINK_UP_BIT);
  188. network_async_link_down();
  189. break;
  190. case ETHERNET_EVENT_START:
  191. ESP_LOGI(TAG, "Ethernet Started. Setting host name");
  192. network_set_hostname(eth_netif);
  193. network_async_success();
  194. break;
  195. case ETHERNET_EVENT_STOP:
  196. ESP_LOGI(TAG, "Ethernet Stopped");
  197. break;
  198. default:
  199. break;
  200. }
  201. }
  202. }
  203. static void ETH_Timeout(void* timer_id) {
  204. network_async_fail();
  205. }