connect_lan.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #if defined(BOARD_ESP32_S3_ALEKSEI)
  2. #include "connect_lan.h"
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <fstream>
  6. #include <vector>
  7. #include <sstream>
  8. #include <iostream>
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "freertos/event_groups.h"
  12. #include "driver/gpio.h"
  13. #include "esp_system.h"
  14. #include "esp_wifi.h"
  15. #include "esp_wnm.h"
  16. #include "esp_rrm.h"
  17. #include "esp_mbo.h"
  18. #include "esp_mac.h"
  19. #include "esp_netif.h"
  20. #include <netdb.h>
  21. #include "esp_log.h"
  22. #include "nvs_flash.h"
  23. #include "lwip/err.h"
  24. #include "lwip/sys.h"
  25. #ifdef ENABLE_MQTT
  26. #include "interface_mqtt.h"
  27. #endif //ENABLE_MQTT
  28. #include "ClassLogFile.h"
  29. #include "read_lanini.h"
  30. #include "Helper.h"
  31. #include "statusled.h"
  32. #include "../../include/defines.h"
  33. #if (ESP_IDF_VERSION_MAJOR >= 5)
  34. #include "soc/periph_defs.h"
  35. #include "esp_private/periph_ctrl.h"
  36. #include "soc/gpio_sig_map.h"
  37. #include "soc/gpio_periph.h"
  38. #include "soc/io_mux_reg.h"
  39. #include "esp_rom_gpio.h"
  40. #define gpio_pad_select_gpio esp_rom_gpio_pad_select_gpio
  41. #define gpio_matrix_in(a,b,c) esp_rom_gpio_connect_in_signal(a,b,c)
  42. #define gpio_matrix_out(a,b,c,d) esp_rom_gpio_connect_out_signal(a,b,c,d)
  43. #define ets_delay_us(a) esp_rom_delay_us(a)
  44. #endif
  45. #include "../esp-protocols/components/mdns/include/mdns.h"
  46. #include <driver/spi_master.h>
  47. #include <esp_eth.h>
  48. #include <esp_netif.h>
  49. static const char *TAG = "LAN";
  50. extern bool WIFIConnected;
  51. static int LanReconnectCnt = 0;
  52. std::string* getLanIPAddress()
  53. {
  54. return &wlan_config.ipaddress;
  55. }
  56. static void eth_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
  57. {
  58. if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
  59. {
  60. WIFIConnected = false;
  61. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Ethernet Started");
  62. }
  63. else if (event_base == ETH_EVENT && event_id == ETHERNET_EVENT_DISCONNECTED)
  64. {
  65. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Ethernet Link Down");
  66. // Optionally, try to reconnect or handle fallback LED:
  67. StatusLED(WLAN_CONN, 1, false);
  68. LanReconnectCnt++;
  69. WIFIConnected = false;
  70. }
  71. else if (event_base == ETH_EVENT && event_id == ETHERNET_EVENT_CONNECTED)
  72. {
  73. uint8_t mac_addr[6] = {0};
  74. esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
  75. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Ethernet Link Up");
  76. esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
  77. ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
  78. mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  79. }
  80. else if (event_base == ETH_EVENT && event_id == ETHERNET_EVENT_STOP) {
  81. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Ethernet Stopped");
  82. WIFIConnected = false;
  83. }
  84. }
  85. static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
  86. int32_t event_id, void *event_data)
  87. {
  88. WIFIConnected = true;
  89. LanReconnectCnt = 0;
  90. ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
  91. wlan_config.ipaddress = std::string(ip4addr_ntoa((const ip4_addr*) &event->ip_info.ip));
  92. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Assigned IP: " + wlan_config.ipaddress);
  93. #ifdef ENABLE_MQTT
  94. if (getMQTTisEnabled()) {
  95. vTaskDelay(5000 / portTICK_PERIOD_MS);
  96. MQTT_Init(); // Init when WIFI is getting connected
  97. }
  98. #endif //ENABLE_MQTT
  99. // LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Assigned IP: " + WIFIConnected);
  100. // LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Assigned IP: " + getWIFIisConnected());
  101. }
  102. esp_eth_handle_t eth_handle = NULL;
  103. esp_netif_t *eth_netif = NULL;
  104. int lan_init(void)
  105. {
  106. esp_err_t retval = esp_netif_init();
  107. if (retval != ESP_OK) {
  108. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "esp_netif_init: Error: " + std::to_string(retval));
  109. return retval;
  110. }
  111. int retVal = esp_event_loop_create_default();
  112. if (retVal != ESP_OK && retVal != ESP_ERR_INVALID_STATE) {
  113. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "esp_event_loop_create_default, Error");
  114. return retVal;
  115. }
  116. gpio_set_direction(ETH_ENABLE, GPIO_MODE_OUTPUT);
  117. gpio_set_level(ETH_ENABLE, 1);
  118. gpio_set_direction(ETH_INT, GPIO_MODE_INPUT);
  119. gpio_set_pull_mode(ETH_INT, GPIO_PULLUP_ONLY);
  120. esp_log_level_set("netif", ESP_LOG_DEBUG);
  121. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "SPI init");
  122. // 1) SPI bus init
  123. spi_bus_config_t buscfg = { 0 };
  124. buscfg.mosi_io_num = ETH_MOSI;
  125. buscfg.miso_io_num = ETH_MISO;
  126. buscfg.sclk_io_num = ETH_CLK;
  127. ESP_ERROR_CHECK(spi_bus_initialize(W5500_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));
  128. // 2) Prepare a `spi_device_interface_config_t` but DO NOT call spi_bus_add_device manually
  129. spi_device_interface_config_t devcfg = {
  130. .mode = 0, // SPI mode 0
  131. .clock_speed_hz = 40 * 1000 * 1000, // 20MHz
  132. .spics_io_num = ETH_CS,
  133. .queue_size = 30,
  134. // the rest zero-initialized
  135. };
  136. eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(W5500_SPI_HOST, &devcfg);
  137. w5500_config.int_gpio_num = ETH_INT;
  138. // 4) Standard MAC/PHY config
  139. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  140. esp_eth_mac_t *mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
  141. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  142. phy_config.phy_addr = 1; // typical W5500
  143. esp_eth_phy_t *phy = esp_eth_phy_new_w5500(&phy_config);
  144. esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
  145. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Driver install");
  146. ESP_ERROR_CHECK(esp_eth_driver_install(&eth_config, &eth_handle));
  147. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Driver installed");
  148. uint8_t base_mac_addr[6];
  149. esp_err_t ret = esp_efuse_mac_get_default(base_mac_addr);
  150. if (ret != ESP_OK) {
  151. ESP_LOGE(TAG, "Failed to get efuse base MAC, error=0x%x", ret);
  152. }
  153. uint8_t local_mac[6];
  154. esp_derive_local_mac(local_mac, base_mac_addr);
  155. ret = esp_eth_ioctl(eth_handle, ETH_CMD_S_MAC_ADDR, local_mac);
  156. if (ret != ESP_OK) {
  157. ESP_LOGE(TAG, "Failed to set W5500 MAC, error=0x%x", ret);
  158. }
  159. // 5) Attach netif + start
  160. esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
  161. eth_netif = esp_netif_new(&netif_cfg);
  162. // Register event handlers
  163. ESP_ERROR_CHECK(esp_event_handler_register(
  164. ETH_EVENT, ESP_EVENT_ANY_ID,
  165. &eth_event_handler, NULL));
  166. ESP_ERROR_CHECK(esp_event_handler_register(
  167. IP_EVENT, IP_EVENT_ETH_GOT_IP,
  168. &got_ip_event_handler, NULL));
  169. esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle));
  170. esp_eth_start(eth_handle);
  171. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "W5500 Ethernet init done");
  172. return ESP_OK;
  173. }
  174. #endif