ethernet_init.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include "ethernet_init.h"
  7. #include "esp_log.h"
  8. #include "esp_check.h"
  9. #include "esp_mac.h"
  10. #include "driver/gpio.h"
  11. #include "sdkconfig.h"
  12. #if CONFIG_ETH_USE_SPI_ETHERNET
  13. #include "driver/spi_master.h"
  14. #endif // CONFIG_ETH_USE_SPI_ETHERNET
  15. static const char *TAG = "example_eth_init";
  16. #if CONFIG_EXAMPLE_SPI_ETHERNETS_NUM
  17. #define SPI_ETHERNETS_NUM CONFIG_EXAMPLE_SPI_ETHERNETS_NUM
  18. #else
  19. #define SPI_ETHERNETS_NUM 0
  20. #endif
  21. #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  22. #define INTERNAL_ETHERNETS_NUM 1
  23. #else
  24. #define INTERNAL_ETHERNETS_NUM 0
  25. #endif
  26. #define INIT_SPI_ETH_MODULE_CONFIG(eth_module_config, num) \
  27. do { \
  28. eth_module_config[num].spi_cs_gpio = CONFIG_EXAMPLE_ETH_SPI_CS ##num## _GPIO; \
  29. eth_module_config[num].int_gpio = CONFIG_EXAMPLE_ETH_SPI_INT ##num## _GPIO; \
  30. eth_module_config[num].phy_reset_gpio = CONFIG_EXAMPLE_ETH_SPI_PHY_RST ##num## _GPIO; \
  31. eth_module_config[num].phy_addr = CONFIG_EXAMPLE_ETH_SPI_PHY_ADDR ##num; \
  32. } while(0)
  33. typedef struct {
  34. uint8_t spi_cs_gpio;
  35. uint8_t int_gpio;
  36. int8_t phy_reset_gpio;
  37. uint8_t phy_addr;
  38. uint8_t *mac_addr;
  39. }spi_eth_module_config_t;
  40. #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  41. /**
  42. * @brief Internal ESP32 Ethernet initialization
  43. *
  44. * @param[out] mac_out optionally returns Ethernet MAC object
  45. * @param[out] phy_out optionally returns Ethernet PHY object
  46. * @return
  47. * - esp_eth_handle_t if init succeeded
  48. * - NULL if init failed
  49. */
  50. static esp_eth_handle_t eth_init_internal(esp_eth_mac_t **mac_out, esp_eth_phy_t **phy_out)
  51. {
  52. esp_eth_handle_t ret = NULL;
  53. // Init common MAC and PHY configs to default
  54. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  55. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  56. // Update PHY config based on board specific configuration
  57. phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR;
  58. phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO;
  59. // Init vendor specific MAC config to default
  60. eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
  61. // Update vendor specific MAC config based on board configuration
  62. esp32_emac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO;
  63. esp32_emac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO;
  64. #if CONFIG_EXAMPLE_USE_SPI_ETHERNET
  65. // The DMA is shared resource between EMAC and the SPI. Therefore, adjust
  66. // EMAC DMA burst length when SPI Ethernet is used along with EMAC.
  67. esp32_emac_config.dma_burst_len = ETH_DMA_BURST_LEN_4;
  68. #endif // CONFIG_EXAMPLE_USE_SPI_ETHERNET
  69. // Create new ESP32 Ethernet MAC instance
  70. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
  71. // Create new PHY instance based on board configuration
  72. #if CONFIG_EXAMPLE_ETH_PHY_IP101
  73. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  74. #elif CONFIG_EXAMPLE_ETH_PHY_RTL8201
  75. esp_eth_phy_t *phy = esp_eth_phy_new_rtl8201(&phy_config);
  76. #elif CONFIG_EXAMPLE_ETH_PHY_LAN87XX
  77. esp_eth_phy_t *phy = esp_eth_phy_new_lan87xx(&phy_config);
  78. #elif CONFIG_EXAMPLE_ETH_PHY_DP83848
  79. esp_eth_phy_t *phy = esp_eth_phy_new_dp83848(&phy_config);
  80. #elif CONFIG_EXAMPLE_ETH_PHY_KSZ80XX
  81. esp_eth_phy_t *phy = esp_eth_phy_new_ksz80xx(&phy_config);
  82. #endif
  83. // Init Ethernet driver to default and install it
  84. esp_eth_handle_t eth_handle = NULL;
  85. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
  86. ESP_GOTO_ON_FALSE(esp_eth_driver_install(&config, &eth_handle) == ESP_OK, NULL,
  87. err, TAG, "Ethernet driver install failed");
  88. if (mac_out != NULL) {
  89. *mac_out = mac;
  90. }
  91. if (phy_out != NULL) {
  92. *phy_out = phy;
  93. }
  94. return eth_handle;
  95. err:
  96. if (eth_handle != NULL) {
  97. esp_eth_driver_uninstall(eth_handle);
  98. }
  99. if (mac != NULL) {
  100. mac->del(mac);
  101. }
  102. if (phy != NULL) {
  103. phy->del(phy);
  104. }
  105. return ret;
  106. }
  107. #endif // CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  108. #if CONFIG_EXAMPLE_USE_SPI_ETHERNET
  109. /**
  110. * @brief SPI bus initialization (to be used by Ethernet SPI modules)
  111. *
  112. * @return
  113. * - ESP_OK on success
  114. */
  115. static esp_err_t spi_bus_init(void)
  116. {
  117. esp_err_t ret = ESP_OK;
  118. // Install GPIO ISR handler to be able to service SPI Eth modules interrupts
  119. ret = gpio_install_isr_service(0);
  120. if (ret != ESP_OK) {
  121. if (ret == ESP_ERR_INVALID_STATE) {
  122. ESP_LOGW(TAG, "GPIO ISR handler has been already installed");
  123. ret = ESP_OK; // ISR handler has been already installed so no issues
  124. } else {
  125. ESP_LOGE(TAG, "GPIO ISR handler install failed");
  126. goto err;
  127. }
  128. }
  129. // Init SPI bus
  130. spi_bus_config_t buscfg = {
  131. .miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO,
  132. .mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO,
  133. .sclk_io_num = CONFIG_EXAMPLE_ETH_SPI_SCLK_GPIO,
  134. .quadwp_io_num = -1,
  135. .quadhd_io_num = -1,
  136. };
  137. ESP_GOTO_ON_ERROR(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO),
  138. err, TAG, "SPI host #%d init failed", CONFIG_EXAMPLE_ETH_SPI_HOST);
  139. err:
  140. return ret;
  141. }
  142. /**
  143. * @brief Ethernet SPI modules initialization
  144. *
  145. * @param[in] spi_eth_module_config specific SPI Ethernet module configuration
  146. * @param[out] mac_out optionally returns Ethernet MAC object
  147. * @param[out] phy_out optionally returns Ethernet PHY object
  148. * @return
  149. * - esp_eth_handle_t if init succeeded
  150. * - NULL if init failed
  151. */
  152. static esp_eth_handle_t eth_init_spi(spi_eth_module_config_t *spi_eth_module_config, esp_eth_mac_t **mac_out, esp_eth_phy_t **phy_out)
  153. {
  154. esp_eth_handle_t ret = NULL;
  155. // Init common MAC and PHY configs to default
  156. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  157. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  158. // Update PHY config based on board specific configuration
  159. phy_config.phy_addr = spi_eth_module_config->phy_addr;
  160. phy_config.reset_gpio_num = spi_eth_module_config->phy_reset_gpio;
  161. // Configure SPI interface for specific SPI module
  162. spi_device_interface_config_t spi_devcfg = {
  163. .mode = 0,
  164. .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
  165. .queue_size = 20,
  166. .spics_io_num = spi_eth_module_config->spi_cs_gpio
  167. };
  168. // Init vendor specific MAC config to default, and create new SPI Ethernet MAC instance
  169. // and new PHY instance based on board configuration
  170. #if CONFIG_EXAMPLE_USE_KSZ8851SNL
  171. eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg);
  172. ksz8851snl_config.int_gpio_num = spi_eth_module_config->int_gpio;
  173. esp_eth_mac_t *mac = esp_eth_mac_new_ksz8851snl(&ksz8851snl_config, &mac_config);
  174. esp_eth_phy_t *phy = esp_eth_phy_new_ksz8851snl(&phy_config);
  175. #elif CONFIG_EXAMPLE_USE_DM9051
  176. eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg);
  177. dm9051_config.int_gpio_num = spi_eth_module_config->int_gpio;
  178. esp_eth_mac_t *mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
  179. esp_eth_phy_t *phy = esp_eth_phy_new_dm9051(&phy_config);
  180. #elif CONFIG_EXAMPLE_USE_W5500
  181. eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg);
  182. w5500_config.int_gpio_num = spi_eth_module_config->int_gpio;
  183. esp_eth_mac_t *mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
  184. esp_eth_phy_t *phy = esp_eth_phy_new_w5500(&phy_config);
  185. #endif //CONFIG_EXAMPLE_USE_W5500
  186. // Init Ethernet driver to default and install it
  187. esp_eth_handle_t eth_handle = NULL;
  188. esp_eth_config_t eth_config_spi = ETH_DEFAULT_CONFIG(mac, phy);
  189. ESP_GOTO_ON_FALSE(esp_eth_driver_install(&eth_config_spi, &eth_handle) == ESP_OK, NULL, err, TAG, "SPI Ethernet driver install failed");
  190. // The SPI Ethernet module might not have a burned factory MAC address, we can set it manually.
  191. if (spi_eth_module_config->mac_addr != NULL) {
  192. ESP_GOTO_ON_FALSE(esp_eth_ioctl(eth_handle, ETH_CMD_S_MAC_ADDR, spi_eth_module_config->mac_addr) == ESP_OK,
  193. NULL, err, TAG, "SPI Ethernet MAC address config failed");
  194. }
  195. if (mac_out != NULL) {
  196. *mac_out = mac;
  197. }
  198. if (phy_out != NULL) {
  199. *phy_out = phy;
  200. }
  201. return eth_handle;
  202. err:
  203. if (eth_handle != NULL) {
  204. esp_eth_driver_uninstall(eth_handle);
  205. }
  206. if (mac != NULL) {
  207. mac->del(mac);
  208. }
  209. if (phy != NULL) {
  210. phy->del(phy);
  211. }
  212. return ret;
  213. }
  214. #endif // CONFIG_EXAMPLE_USE_SPI_ETHERNET
  215. esp_err_t example_eth_init(esp_eth_handle_t *eth_handles_out[], uint8_t *eth_cnt_out)
  216. {
  217. esp_err_t ret = ESP_OK;
  218. esp_eth_handle_t *eth_handles = NULL;
  219. uint8_t eth_cnt = 0;
  220. #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET || CONFIG_EXAMPLE_USE_SPI_ETHERNET
  221. ESP_GOTO_ON_FALSE(eth_handles_out != NULL && eth_cnt_out != NULL, ESP_ERR_INVALID_ARG,
  222. err, TAG, "invalid arguments: initialized handles array or number of interfaces");
  223. eth_handles = calloc(SPI_ETHERNETS_NUM + INTERNAL_ETHERNETS_NUM, sizeof(esp_eth_handle_t));
  224. ESP_GOTO_ON_FALSE(eth_handles != NULL, ESP_ERR_NO_MEM, err, TAG, "no memory");
  225. #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  226. eth_handles[eth_cnt] = eth_init_internal(NULL, NULL);
  227. ESP_GOTO_ON_FALSE(eth_handles[eth_cnt], ESP_FAIL, err, TAG, "internal Ethernet init failed");
  228. eth_cnt++;
  229. #endif //CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  230. #if CONFIG_EXAMPLE_USE_SPI_ETHERNET
  231. ESP_GOTO_ON_ERROR(spi_bus_init(), err, TAG, "SPI bus init failed");
  232. // Init specific SPI Ethernet module configuration from Kconfig (CS GPIO, Interrupt GPIO, etc.)
  233. spi_eth_module_config_t spi_eth_module_config[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM] = { 0 };
  234. INIT_SPI_ETH_MODULE_CONFIG(spi_eth_module_config, 0);
  235. // The SPI Ethernet module(s) might not have a burned factory MAC address, hence use manually configured address(es).
  236. // In this example, Locally Administered MAC address derived from ESP32x base MAC address is used.
  237. // Note that Locally Administered OUI range should be used only when testing on a LAN under your control!
  238. uint8_t base_mac_addr[ETH_ADDR_LEN];
  239. ESP_GOTO_ON_ERROR(esp_efuse_mac_get_default(base_mac_addr), err, TAG, "get EFUSE MAC failed");
  240. uint8_t local_mac_1[ETH_ADDR_LEN];
  241. esp_derive_local_mac(local_mac_1, base_mac_addr);
  242. spi_eth_module_config[0].mac_addr = local_mac_1;
  243. #if CONFIG_EXAMPLE_SPI_ETHERNETS_NUM > 1
  244. INIT_SPI_ETH_MODULE_CONFIG(spi_eth_module_config, 1);
  245. uint8_t local_mac_2[ETH_ADDR_LEN];
  246. base_mac_addr[ETH_ADDR_LEN - 1] += 1;
  247. esp_derive_local_mac(local_mac_2, base_mac_addr);
  248. spi_eth_module_config[1].mac_addr = local_mac_2;
  249. #endif
  250. #if CONFIG_EXAMPLE_SPI_ETHERNETS_NUM > 2
  251. #error Maximum number of supported SPI Ethernet devices is currently limited to 2 by this example.
  252. #endif
  253. for (int i = 0; i < CONFIG_EXAMPLE_SPI_ETHERNETS_NUM; i++) {
  254. eth_handles[eth_cnt] = eth_init_spi(&spi_eth_module_config[i], NULL, NULL);
  255. ESP_GOTO_ON_FALSE(eth_handles[eth_cnt], ESP_FAIL, err, TAG, "SPI Ethernet init failed");
  256. eth_cnt++;
  257. }
  258. #endif // CONFIG_ETH_USE_SPI_ETHERNET
  259. #else
  260. ESP_LOGD(TAG, "no Ethernet device selected to init");
  261. #endif // CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET || CONFIG_EXAMPLE_USE_SPI_ETHERNET
  262. *eth_handles_out = eth_handles;
  263. *eth_cnt_out = eth_cnt;
  264. return ret;
  265. #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET || CONFIG_EXAMPLE_USE_SPI_ETHERNET
  266. err:
  267. free(eth_handles);
  268. return ret;
  269. #endif
  270. }