ethernet_init.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * SPDX-FileCopyrightText: 2022 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. // Create new ESP32 Ethernet MAC instance
  65. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
  66. // Create new PHY instance based on board configuration
  67. #if CONFIG_EXAMPLE_ETH_PHY_IP101
  68. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  69. #elif CONFIG_EXAMPLE_ETH_PHY_RTL8201
  70. esp_eth_phy_t *phy = esp_eth_phy_new_rtl8201(&phy_config);
  71. #elif CONFIG_EXAMPLE_ETH_PHY_LAN87XX
  72. esp_eth_phy_t *phy = esp_eth_phy_new_lan87xx(&phy_config);
  73. #elif CONFIG_EXAMPLE_ETH_PHY_DP83848
  74. esp_eth_phy_t *phy = esp_eth_phy_new_dp83848(&phy_config);
  75. #elif CONFIG_EXAMPLE_ETH_PHY_KSZ80XX
  76. esp_eth_phy_t *phy = esp_eth_phy_new_ksz80xx(&phy_config);
  77. #endif
  78. // Init Ethernet driver to default and install it
  79. esp_eth_handle_t eth_handle = NULL;
  80. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
  81. ESP_GOTO_ON_FALSE(esp_eth_driver_install(&config, &eth_handle) == ESP_OK, NULL,
  82. err, TAG, "Ethernet driver install failed");
  83. if (mac_out != NULL) {
  84. *mac_out = mac;
  85. }
  86. if (phy_out != NULL) {
  87. *phy_out = phy;
  88. }
  89. return eth_handle;
  90. err:
  91. if (eth_handle != NULL) {
  92. esp_eth_driver_uninstall(eth_handle);
  93. }
  94. if (mac != NULL) {
  95. mac->del(mac);
  96. }
  97. if (phy != NULL) {
  98. phy->del(phy);
  99. }
  100. return ret;
  101. }
  102. #endif // CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  103. #if CONFIG_EXAMPLE_USE_SPI_ETHERNET
  104. /**
  105. * @brief SPI bus initialization (to be used by Ethernet SPI modules)
  106. *
  107. * @return
  108. * - ESP_OK on success
  109. */
  110. static esp_err_t spi_bus_init(void)
  111. {
  112. esp_err_t ret = ESP_OK;
  113. // Install GPIO ISR handler to be able to service SPI Eth modules interrupts
  114. ret = gpio_install_isr_service(0);
  115. if (ret != ESP_OK) {
  116. if (ret == ESP_ERR_INVALID_STATE) {
  117. ESP_LOGW(TAG, "GPIO ISR handler has been already installed");
  118. ret = ESP_OK; // ISR handler has been already installed so no issues
  119. } else {
  120. ESP_LOGE(TAG, "GPIO ISR handler install failed");
  121. goto err;
  122. }
  123. }
  124. // Init SPI bus
  125. spi_bus_config_t buscfg = {
  126. .miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO,
  127. .mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO,
  128. .sclk_io_num = CONFIG_EXAMPLE_ETH_SPI_SCLK_GPIO,
  129. .quadwp_io_num = -1,
  130. .quadhd_io_num = -1,
  131. };
  132. ESP_GOTO_ON_ERROR(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO),
  133. err, TAG, "SPI host #%d init failed", CONFIG_EXAMPLE_ETH_SPI_HOST);
  134. err:
  135. return ret;
  136. }
  137. /**
  138. * @brief Ethernet SPI modules initialization
  139. *
  140. * @param[in] spi_eth_module_config specific SPI Ethernet module configuration
  141. * @param[out] mac_out optionally returns Ethernet MAC object
  142. * @param[out] phy_out optionally returns Ethernet PHY object
  143. * @return
  144. * - esp_eth_handle_t if init succeeded
  145. * - NULL if init failed
  146. */
  147. 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)
  148. {
  149. esp_eth_handle_t ret = NULL;
  150. // Init common MAC and PHY configs to default
  151. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  152. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  153. // Update PHY config based on board specific configuration
  154. phy_config.phy_addr = spi_eth_module_config->phy_addr;
  155. phy_config.reset_gpio_num = spi_eth_module_config->phy_reset_gpio;
  156. // Configure SPI interface for specific SPI module
  157. spi_device_interface_config_t spi_devcfg = {
  158. .mode = 0,
  159. .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
  160. .queue_size = 20,
  161. .spics_io_num = spi_eth_module_config->spi_cs_gpio
  162. };
  163. // Init vendor specific MAC config to default, and create new SPI Ethernet MAC instance
  164. // and new PHY instance based on board configuration
  165. #if CONFIG_EXAMPLE_USE_KSZ8851SNL
  166. eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg);
  167. ksz8851snl_config.int_gpio_num = spi_eth_module_config->int_gpio;
  168. esp_eth_mac_t *mac = esp_eth_mac_new_ksz8851snl(&ksz8851snl_config, &mac_config);
  169. esp_eth_phy_t *phy = esp_eth_phy_new_ksz8851snl(&phy_config);
  170. #elif CONFIG_EXAMPLE_USE_DM9051
  171. eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg);
  172. dm9051_config.int_gpio_num = spi_eth_module_config->int_gpio;
  173. esp_eth_mac_t *mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
  174. esp_eth_phy_t *phy = esp_eth_phy_new_dm9051(&phy_config);
  175. #elif CONFIG_EXAMPLE_USE_W5500
  176. eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg);
  177. w5500_config.int_gpio_num = spi_eth_module_config->int_gpio;
  178. esp_eth_mac_t *mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
  179. esp_eth_phy_t *phy = esp_eth_phy_new_w5500(&phy_config);
  180. #endif //CONFIG_EXAMPLE_USE_W5500
  181. // Init Ethernet driver to default and install it
  182. esp_eth_handle_t eth_handle = NULL;
  183. esp_eth_config_t eth_config_spi = ETH_DEFAULT_CONFIG(mac, phy);
  184. ESP_GOTO_ON_FALSE(esp_eth_driver_install(&eth_config_spi, &eth_handle) == ESP_OK, NULL, err, TAG, "SPI Ethernet driver install failed");
  185. // The SPI Ethernet module might not have a burned factory MAC address, we can set it manually.
  186. if (spi_eth_module_config->mac_addr != NULL) {
  187. ESP_GOTO_ON_FALSE(esp_eth_ioctl(eth_handle, ETH_CMD_S_MAC_ADDR, spi_eth_module_config->mac_addr) == ESP_OK,
  188. NULL, err, TAG, "SPI Ethernet MAC address config failed");
  189. }
  190. if (mac_out != NULL) {
  191. *mac_out = mac;
  192. }
  193. if (phy_out != NULL) {
  194. *phy_out = phy;
  195. }
  196. return eth_handle;
  197. err:
  198. if (eth_handle != NULL) {
  199. esp_eth_driver_uninstall(eth_handle);
  200. }
  201. if (mac != NULL) {
  202. mac->del(mac);
  203. }
  204. if (phy != NULL) {
  205. phy->del(phy);
  206. }
  207. return ret;
  208. }
  209. #endif // CONFIG_EXAMPLE_USE_SPI_ETHERNET
  210. esp_err_t example_eth_init(esp_eth_handle_t *eth_handles_out[], uint8_t *eth_cnt_out)
  211. {
  212. esp_err_t ret = ESP_OK;
  213. esp_eth_handle_t *eth_handles = NULL;
  214. uint8_t eth_cnt = 0;
  215. #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET || CONFIG_EXAMPLE_USE_SPI_ETHERNET
  216. ESP_GOTO_ON_FALSE(eth_handles_out != NULL && eth_cnt_out != NULL, ESP_ERR_INVALID_ARG,
  217. err, TAG, "invalid arguments: initialized handles array or number of interfaces");
  218. eth_handles = calloc(SPI_ETHERNETS_NUM + INTERNAL_ETHERNETS_NUM, sizeof(esp_eth_handle_t));
  219. ESP_GOTO_ON_FALSE(eth_handles != NULL, ESP_ERR_NO_MEM, err, TAG, "no memory");
  220. #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  221. eth_handles[eth_cnt] = eth_init_internal(NULL, NULL);
  222. ESP_GOTO_ON_FALSE(eth_handles[eth_cnt], ESP_FAIL, err, TAG, "internal Ethernet init failed");
  223. eth_cnt++;
  224. #endif //CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  225. #if CONFIG_EXAMPLE_USE_SPI_ETHERNET
  226. ESP_GOTO_ON_ERROR(spi_bus_init(), err, TAG, "SPI bus init failed");
  227. // Init specific SPI Ethernet module configuration from Kconfig (CS GPIO, Interrupt GPIO, etc.)
  228. spi_eth_module_config_t spi_eth_module_config[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM] = { 0 };
  229. INIT_SPI_ETH_MODULE_CONFIG(spi_eth_module_config, 0);
  230. // The SPI Ethernet module(s) might not have a burned factory MAC address, hence use manually configured address(es).
  231. // In this example, Locally Administered MAC address derived from ESP32x base MAC address is used.
  232. // Note that Locally Administered OUI range should be used only when testing on a LAN under your control!
  233. uint8_t base_mac_addr[ETH_ADDR_LEN];
  234. ESP_GOTO_ON_ERROR(esp_efuse_mac_get_default(base_mac_addr), err, TAG, "get EFUSE MAC failed");
  235. uint8_t local_mac_1[ETH_ADDR_LEN];
  236. esp_derive_local_mac(local_mac_1, base_mac_addr);
  237. spi_eth_module_config[0].mac_addr = local_mac_1;
  238. #if CONFIG_EXAMPLE_SPI_ETHERNETS_NUM > 1
  239. INIT_SPI_ETH_MODULE_CONFIG(spi_eth_module_config, 1);
  240. uint8_t local_mac_2[ETH_ADDR_LEN];
  241. base_mac_addr[ETH_ADDR_LEN - 1] += 1;
  242. esp_derive_local_mac(local_mac_2, base_mac_addr);
  243. spi_eth_module_config[1].mac_addr = local_mac_2;
  244. #endif
  245. #if CONFIG_EXAMPLE_SPI_ETHERNETS_NUM > 2
  246. #error Maximum number of supported SPI Ethernet devices is currently limited to 2 by this example.
  247. #endif
  248. for (int i = 0; i < CONFIG_EXAMPLE_SPI_ETHERNETS_NUM; i++) {
  249. eth_handles[eth_cnt] = eth_init_spi(&spi_eth_module_config[i], NULL, NULL);
  250. ESP_GOTO_ON_FALSE(eth_handles[eth_cnt], ESP_FAIL, err, TAG, "SPI Ethernet init failed");
  251. eth_cnt++;
  252. }
  253. #endif // CONFIG_ETH_USE_SPI_ETHERNET
  254. #else
  255. ESP_LOGD(TAG, "no Ethernet device selected to init");
  256. #endif // CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET || CONFIG_EXAMPLE_USE_SPI_ETHERNET
  257. *eth_handles_out = eth_handles;
  258. *eth_cnt_out = eth_cnt;
  259. return ret;
  260. #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET || CONFIG_EXAMPLE_USE_SPI_ETHERNET
  261. err:
  262. free(eth_handles);
  263. return ret;
  264. #endif
  265. }