network_ethernet.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include "network_ethernet.h"
  2. #include "freertos/timers.h"
  3. #include "globdefs.h"
  4. #include "messaging.h"
  5. #include "network_status.h"
  6. #include "platform_config.h"
  7. #include "trace.h"
  8. #define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
  9. #include "esp_log.h"
  10. //#include "dnserver.h"
  11. static char TAG[] = "network_ethernet";
  12. TimerHandle_t ETH_timer;
  13. esp_eth_handle_t eth_handle = NULL;
  14. esp_netif_t* eth_netif = NULL;
  15. EventGroupHandle_t ethernet_event_group;
  16. const int LINK_UP_BIT = BIT0;
  17. static const char* known_drivers[] = {"DM9051", "W5500", "LAN8720", NULL};
  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. static void network_manager_ethernet_ip_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data);
  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 eth_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 set_host_name() {
  83. ESP_LOGE(TAG, "TODO: Find a way to set the host name here!");
  84. // esp_err_t err;
  85. // ESP_LOGD(TAG, "Retrieving host name from nvs");
  86. // char* host_name = (char*)config_alloc_get(NVS_TYPE_STR, "host_name");
  87. // if (host_name == NULL) {
  88. // ESP_LOGE(TAG, "Could not retrieve host name from nvs");
  89. // } else {
  90. // if (!network_ethernet_enabled()) {
  91. // ESP_LOGE(TAG, "Cannot set name on a disabled interface");
  92. // } else {
  93. // ESP_LOGD(TAG, "Setting host name to : %s", host_name);
  94. // if ((err = esp_netif_set_hostname(eth_handle, host_name)) != ESP_OK) {
  95. // ESP_LOGE(TAG, "Unable to set host name. Error: %s", esp_err_to_name(err));
  96. // }
  97. // ESP_LOGD(TAG, "Done setting host name to : %s", host_name);
  98. // }
  99. // FREE_AND_NULL(host_name);
  100. // }
  101. }
  102. static void network_ethernet_print_config(const eth_config_t* eth_config) {
  103. // #if defined(CONFIG_ETH_PHY_INTERFACE_RMII)
  104. // if(eth_config->)
  105. // ESP_LOGI(TAG,
  106. // "Model: %s, rst=%d, mdc=%d, mdio=%d, host=%d, cs=%d, mosi=%d, miso=%d, intr=%d, clk=%d, speed=%d, tx_en=%d, tx0=%d, tx1=%d, rx0=%d, rx1=%d, crs_dv=%d",
  107. // eth_config->model, eth_config->rst, eth_config->mdc, eth_config->mdio, eth_config->host, eth_config->cs,
  108. // eth_config->mosi, eth_config->miso, eth_config->intr, eth_config->clk, eth_config->speed,
  109. // eth_config->tx_en, eth_config->tx0, eth_config->tx1, eth_config->rx0, eth_config->rx1, eth_config->crs_dv);
  110. // #else
  111. // ESP_LOGI(TAG, "Model: %s, rst=%d, mdc=%d, mdio=%d, host=%d, cs=%d, mosi=%d, miso=%d, intr=%d, clk=%d, speed=%d ",
  112. // eth_config->model, eth_config->rst, eth_config->mdc, eth_config->mdio, eth_config->host, eth_config->cs,
  113. // eth_config->mosi, eth_config->miso, eth_config->intr, eth_config->clk, eth_config->speed);
  114. // :
  115. // #endif
  116. }
  117. void init_network_ethernet() {
  118. esp_err_t err = ESP_OK;
  119. esp_eth_mac_t* mac;
  120. esp_eth_phy_t* phy;
  121. eth_config_t eth;
  122. config_eth_init(&eth);
  123. ESP_LOGD(TAG, "Attempting to initialize Ethernet");
  124. // quick check if we have a valid ethernet configuration
  125. if (!eth.valid) {
  126. ESP_LOGI(TAG, "No ethernet");
  127. return;
  128. }
  129. network_driver = network_ethernet_driver_autodetect(eth.model);
  130. if (!network_driver) {
  131. messaging_post_message(MESSAGING_ERROR, MESSAGING_CLASS_SYSTEM, "Invalid ethernet Ethernet chip %s", eth.model);
  132. return;
  133. }
  134. if (!network_driver->valid) {
  135. messaging_post_message(MESSAGING_ERROR, MESSAGING_CLASS_SYSTEM, "Code not compiled for Ethernet chip %s", eth.model);
  136. return;
  137. }
  138. network_driver->init_config(&eth);
  139. network_ethernet_print_config(&eth);
  140. esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
  141. eth_netif = esp_netif_new(&cfg);
  142. esp_eth_set_default_handlers(eth_netif);
  143. esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL);
  144. esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL);
  145. ethernet_event_group = xEventGroupCreate();
  146. xEventGroupClearBits(ethernet_event_group, LINK_UP_BIT);
  147. spi_device_handle_t spi_handle = NULL;
  148. if (network_driver->eth_config.spi) {
  149. spi_host_device_t host = SPI3_HOST;
  150. if (eth.host != -1) {
  151. // don't use system's shared SPI
  152. spi_bus_config_t buscfg = {
  153. .miso_io_num = eth.miso,
  154. .mosi_io_num = eth.mosi,
  155. .sclk_io_num = eth.clk,
  156. .quadwp_io_num = -1,
  157. .quadhd_io_num = -1,
  158. };
  159. // can't use SPI0
  160. if (eth.host == 1)
  161. host = SPI2_HOST;
  162. ESP_LOGI(TAG, "Initializing SPI bus on host %d with mosi %d and miso %d", host, eth.mosi, eth.miso);
  163. err = spi_bus_initialize(host, &buscfg, 1);
  164. if (err != ESP_OK) {
  165. ESP_LOGE(TAG, "SPI bus init failed : %s", esp_err_to_name(err));
  166. }
  167. } else {
  168. // when we use shared SPI, we assume it has been initialized
  169. host = spi_system_host;
  170. }
  171. if (err == ESP_OK) {
  172. spi_device_interface_config_t devcfg = {
  173. .command_bits = 1,
  174. .address_bits = 7,
  175. .mode = 0,
  176. .clock_speed_hz = eth.speed,
  177. .spics_io_num = eth.cs,
  178. .queue_size = 20};
  179. ESP_LOGI(TAG, "Adding ethernet SPI on host %d with mosi %d and miso %d", host, eth.mosi, eth.miso);
  180. err = spi_bus_add_device(host, &devcfg, &spi_handle);
  181. }
  182. if (err != ESP_OK) {
  183. ESP_LOGE(TAG, "SPI host failed : %s", esp_err_to_name(err));
  184. }
  185. }
  186. if (err == ESP_OK) {
  187. ESP_LOGD(TAG, "Setting up ethernet driver");
  188. mac = network_driver->mac_new(spi_handle, &eth);
  189. phy = network_driver->phy_new(&eth);
  190. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
  191. err = esp_eth_driver_install(&config, &eth_handle);
  192. }
  193. if (err == ESP_OK) {
  194. ESP_LOGD(TAG, "Attaching ethernet to network interface");
  195. err = esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle));
  196. }
  197. if (err == ESP_OK) {
  198. ESP_LOGI(TAG, "Starting ethernet network");
  199. err = esp_eth_start(eth_handle);
  200. }
  201. if (err != ESP_OK) {
  202. messaging_post_message(MESSAGING_ERROR, MESSAGING_CLASS_SYSTEM, "Configuring Ethernet failed: %s", esp_err_to_name(err));
  203. eth_handle = NULL;
  204. }
  205. }
  206. void network_ethernet_start_timer() {
  207. ETH_timer = xTimerCreate("ETH check", pdMS_TO_TICKS(ETH_TIMEOUT_MS), pdFALSE, NULL, ETH_Timeout);
  208. }
  209. /** Event handler for Ethernet events */
  210. static void eth_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
  211. uint8_t mac_addr[6] = {0};
  212. /* we can get the ethernet driver handle from event data */
  213. if (event_base == ETH_EVENT) {
  214. esp_eth_handle_t eth_handle = *(esp_eth_handle_t*)event_data;
  215. switch (event_id) {
  216. case ETHERNET_EVENT_CONNECTED:
  217. xEventGroupSetBits(ethernet_event_group, LINK_UP_BIT);
  218. esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
  219. ESP_LOGI(TAG, "");
  220. 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]);
  221. ESP_LOGD(TAG, "Sending EVENT_ETH_LINK_UP message to network manager");
  222. network_manager_async_link_up();
  223. break;
  224. case ETHERNET_EVENT_DISCONNECTED:
  225. ESP_LOGI(TAG, "Ethernet Link Down");
  226. xEventGroupClearBits(ethernet_event_group, LINK_UP_BIT);
  227. ESP_LOGD(TAG, "Sending EVENT_ETH_LINK_DOWN message to network manager");
  228. network_manager_async_link_down();
  229. break;
  230. case ETHERNET_EVENT_START:
  231. ESP_LOGI(TAG, "Ethernet Started. Setting host name");
  232. set_host_name();
  233. break;
  234. case ETHERNET_EVENT_STOP:
  235. ESP_LOGI(TAG, "Ethernet Stopped");
  236. break;
  237. default:
  238. break;
  239. }
  240. } else if (event_base == IP_EVENT && event_id == IP_EVENT_ETH_GOT_IP) {
  241. network_manager_ethernet_ip_event_handler(arg, event_base, event_id, event_data);
  242. }
  243. }
  244. static void ETH_Timeout(void* timer_id) {
  245. network_manager_async_fail();
  246. }
  247. static void network_manager_ethernet_ip_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
  248. if (event_base != IP_EVENT)
  249. return;
  250. ip_event_got_ip_t* event = (ip_event_got_ip_t*)event_data;
  251. ip_event_got_ip_t* s = event_data;
  252. tcpip_adapter_if_t index = s->if_index;
  253. esp_netif_ip_info_t* ip_info = &s->ip_info;
  254. ESP_LOGI(TAG, "Got an IP address from Ethernet interface #%i. IP=" IPSTR ", Gateway=" IPSTR ", NetMask=" IPSTR ", %s",
  255. index,
  256. IP2STR(&ip_info->ip),
  257. IP2STR(&ip_info->gw),
  258. IP2STR(&ip_info->netmask),
  259. s->ip_changed ? "Address was changed" : "Address unchanged");
  260. ip_event_got_ip_t* parm = malloc(sizeof(ip_event_got_ip_t));
  261. memcpy(parm, event_data, sizeof(ip_event_got_ip_t));
  262. network_manager_async_got_ip(parm);
  263. }