esp_eth_mac.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdbool.h>
  8. #include "esp_eth_com.h"
  9. #include "sdkconfig.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /**
  14. * @brief Ethernet MAC
  15. *
  16. */
  17. typedef struct esp_eth_mac_s esp_eth_mac_t;
  18. /**
  19. * @brief Ethernet MAC
  20. *
  21. */
  22. struct esp_eth_mac_s {
  23. /**
  24. * @brief Set mediator for Ethernet MAC
  25. *
  26. * @param[in] mac: Ethernet MAC instance
  27. * @param[in] eth: Ethernet mediator
  28. *
  29. * @return
  30. * - ESP_OK: set mediator for Ethernet MAC successfully
  31. * - ESP_ERR_INVALID_ARG: set mediator for Ethernet MAC failed because of invalid argument
  32. *
  33. */
  34. esp_err_t (*set_mediator)(esp_eth_mac_t *mac, esp_eth_mediator_t *eth);
  35. /**
  36. * @brief Initialize Ethernet MAC
  37. *
  38. * @param[in] mac: Ethernet MAC instance
  39. *
  40. * @return
  41. * - ESP_OK: initialize Ethernet MAC successfully
  42. * - ESP_ERR_TIMEOUT: initialize Ethernet MAC failed because of timeout
  43. * - ESP_FAIL: initialize Ethernet MAC failed because some other error occurred
  44. *
  45. */
  46. esp_err_t (*init)(esp_eth_mac_t *mac);
  47. /**
  48. * @brief Deinitialize Ethernet MAC
  49. *
  50. * @param[in] mac: Ethernet MAC instance
  51. *
  52. * @return
  53. * - ESP_OK: deinitialize Ethernet MAC successfully
  54. * - ESP_FAIL: deinitialize Ethernet MAC failed because some error occurred
  55. *
  56. */
  57. esp_err_t (*deinit)(esp_eth_mac_t *mac);
  58. /**
  59. * @brief Start Ethernet MAC
  60. *
  61. * @param[in] mac: Ethernet MAC instance
  62. *
  63. * @return
  64. * - ESP_OK: start Ethernet MAC successfully
  65. * - ESP_FAIL: start Ethernet MAC failed because some other error occurred
  66. *
  67. */
  68. esp_err_t (*start)(esp_eth_mac_t *mac);
  69. /**
  70. * @brief Stop Ethernet MAC
  71. *
  72. * @param[in] mac: Ethernet MAC instance
  73. *
  74. * @return
  75. * - ESP_OK: stop Ethernet MAC successfully
  76. * - ESP_FAIL: stop Ethernet MAC failed because some error occurred
  77. *
  78. */
  79. esp_err_t (*stop)(esp_eth_mac_t *mac);
  80. /**
  81. * @brief Transmit packet from Ethernet MAC
  82. *
  83. * @param[in] mac: Ethernet MAC instance
  84. * @param[in] buf: packet buffer to transmit
  85. * @param[in] length: length of packet
  86. *
  87. * @return
  88. * - ESP_OK: transmit packet successfully
  89. * - ESP_ERR_INVALID_SIZE: number of actually sent bytes differs to expected
  90. * - ESP_FAIL: transmit packet failed because some other error occurred
  91. *
  92. * @note Returned error codes may differ for each specific MAC chip.
  93. *
  94. */
  95. esp_err_t (*transmit)(esp_eth_mac_t *mac, uint8_t *buf, uint32_t length);
  96. /**
  97. * @brief Transmit packet from Ethernet MAC constructed with special parameters at Layer2.
  98. *
  99. * @param[in] mac: Ethernet MAC instance
  100. * @param[in] argc: number variable arguments
  101. * @param[in] args: variable arguments
  102. *
  103. * @note Typical intended use case is to make possible to construct a frame from multiple higher layer
  104. * buffers without a need of buffer reallocations. However, other use cases are not limited.
  105. *
  106. * @return
  107. * - ESP_OK: transmit packet successfully
  108. * - ESP_ERR_INVALID_SIZE: number of actually sent bytes differs to expected
  109. * - ESP_FAIL: transmit packet failed because some other error occurred
  110. *
  111. * @note Returned error codes may differ for each specific MAC chip.
  112. *
  113. */
  114. esp_err_t (*transmit_vargs)(esp_eth_mac_t *mac, uint32_t argc, va_list args);
  115. /**
  116. * @brief Receive packet from Ethernet MAC
  117. *
  118. * @param[in] mac: Ethernet MAC instance
  119. * @param[out] buf: packet buffer which will preserve the received frame
  120. * @param[out] length: length of the received packet
  121. *
  122. * @note Memory of buf is allocated in the Layer2, make sure it get free after process.
  123. * @note Before this function got invoked, the value of "length" should set by user, equals the size of buffer.
  124. * After the function returned, the value of "length" means the real length of received data.
  125. *
  126. * @return
  127. * - ESP_OK: receive packet successfully
  128. * - ESP_ERR_INVALID_ARG: receive packet failed because of invalid argument
  129. * - ESP_ERR_INVALID_SIZE: input buffer size is not enough to hold the incoming data.
  130. * in this case, value of returned "length" indicates the real size of incoming data.
  131. * - ESP_FAIL: receive packet failed because some other error occurred
  132. *
  133. */
  134. esp_err_t (*receive)(esp_eth_mac_t *mac, uint8_t *buf, uint32_t *length);
  135. /**
  136. * @brief Read PHY register
  137. *
  138. * @param[in] mac: Ethernet MAC instance
  139. * @param[in] phy_addr: PHY chip address (0~31)
  140. * @param[in] phy_reg: PHY register index code
  141. * @param[out] reg_value: PHY register value
  142. *
  143. * @return
  144. * - ESP_OK: read PHY register successfully
  145. * - ESP_ERR_INVALID_ARG: read PHY register failed because of invalid argument
  146. * - ESP_ERR_INVALID_STATE: read PHY register failed because of wrong state of MAC
  147. * - ESP_ERR_TIMEOUT: read PHY register failed because of timeout
  148. * - ESP_FAIL: read PHY register failed because some other error occurred
  149. *
  150. */
  151. esp_err_t (*read_phy_reg)(esp_eth_mac_t *mac, uint32_t phy_addr, uint32_t phy_reg, uint32_t *reg_value);
  152. /**
  153. * @brief Write PHY register
  154. *
  155. * @param[in] mac: Ethernet MAC instance
  156. * @param[in] phy_addr: PHY chip address (0~31)
  157. * @param[in] phy_reg: PHY register index code
  158. * @param[in] reg_value: PHY register value
  159. *
  160. * @return
  161. * - ESP_OK: write PHY register successfully
  162. * - ESP_ERR_INVALID_STATE: write PHY register failed because of wrong state of MAC
  163. * - ESP_ERR_TIMEOUT: write PHY register failed because of timeout
  164. * - ESP_FAIL: write PHY register failed because some other error occurred
  165. *
  166. */
  167. esp_err_t (*write_phy_reg)(esp_eth_mac_t *mac, uint32_t phy_addr, uint32_t phy_reg, uint32_t reg_value);
  168. /**
  169. * @brief Set MAC address
  170. *
  171. * @param[in] mac: Ethernet MAC instance
  172. * @param[in] addr: MAC address
  173. *
  174. * @return
  175. * - ESP_OK: set MAC address successfully
  176. * - ESP_ERR_INVALID_ARG: set MAC address failed because of invalid argument
  177. * - ESP_FAIL: set MAC address failed because some other error occurred
  178. *
  179. */
  180. esp_err_t (*set_addr)(esp_eth_mac_t *mac, uint8_t *addr);
  181. /**
  182. * @brief Get MAC address
  183. *
  184. * @param[in] mac: Ethernet MAC instance
  185. * @param[out] addr: MAC address
  186. *
  187. * @return
  188. * - ESP_OK: get MAC address successfully
  189. * - ESP_ERR_INVALID_ARG: get MAC address failed because of invalid argument
  190. * - ESP_FAIL: get MAC address failed because some other error occurred
  191. *
  192. */
  193. esp_err_t (*get_addr)(esp_eth_mac_t *mac, uint8_t *addr);
  194. /**
  195. * @brief Set speed of MAC
  196. *
  197. * @param[in] ma:c Ethernet MAC instance
  198. * @param[in] speed: MAC speed
  199. *
  200. * @return
  201. * - ESP_OK: set MAC speed successfully
  202. * - ESP_ERR_INVALID_ARG: set MAC speed failed because of invalid argument
  203. * - ESP_FAIL: set MAC speed failed because some other error occurred
  204. *
  205. */
  206. esp_err_t (*set_speed)(esp_eth_mac_t *mac, eth_speed_t speed);
  207. /**
  208. * @brief Set duplex mode of MAC
  209. *
  210. * @param[in] mac: Ethernet MAC instance
  211. * @param[in] duplex: MAC duplex
  212. *
  213. * @return
  214. * - ESP_OK: set MAC duplex mode successfully
  215. * - ESP_ERR_INVALID_ARG: set MAC duplex failed because of invalid argument
  216. * - ESP_FAIL: set MAC duplex failed because some other error occurred
  217. *
  218. */
  219. esp_err_t (*set_duplex)(esp_eth_mac_t *mac, eth_duplex_t duplex);
  220. /**
  221. * @brief Set link status of MAC
  222. *
  223. * @param[in] mac: Ethernet MAC instance
  224. * @param[in] link: Link status
  225. *
  226. * @return
  227. * - ESP_OK: set link status successfully
  228. * - ESP_ERR_INVALID_ARG: set link status failed because of invalid argument
  229. * - ESP_FAIL: set link status failed because some other error occurred
  230. *
  231. */
  232. esp_err_t (*set_link)(esp_eth_mac_t *mac, eth_link_t link);
  233. /**
  234. * @brief Set promiscuous of MAC
  235. *
  236. * @param[in] mac: Ethernet MAC instance
  237. * @param[in] enable: set true to enable promiscuous mode; set false to disable promiscuous mode
  238. *
  239. * @return
  240. * - ESP_OK: set promiscuous mode successfully
  241. * - ESP_FAIL: set promiscuous mode failed because some error occurred
  242. *
  243. */
  244. esp_err_t (*set_promiscuous)(esp_eth_mac_t *mac, bool enable);
  245. /**
  246. * @brief Enable flow control on MAC layer or not
  247. *
  248. * @param[in] mac: Ethernet MAC instance
  249. * @param[in] enable: set true to enable flow control; set false to disable flow control
  250. *
  251. * @return
  252. * - ESP_OK: set flow control successfully
  253. * - ESP_FAIL: set flow control failed because some error occurred
  254. *
  255. */
  256. esp_err_t (*enable_flow_ctrl)(esp_eth_mac_t *mac, bool enable);
  257. /**
  258. * @brief Set the PAUSE ability of peer node
  259. *
  260. * @param[in] mac: Ethernet MAC instance
  261. * @param[in] ability: zero indicates that pause function is supported by link partner; non-zero indicates that pause function is not supported by link partner
  262. *
  263. * @return
  264. * - ESP_OK: set peer pause ability successfully
  265. * - ESP_FAIL: set peer pause ability failed because some error occurred
  266. */
  267. esp_err_t (*set_peer_pause_ability)(esp_eth_mac_t *mac, uint32_t ability);
  268. /**
  269. * @brief Custom IO function of MAC driver. This function is intended to extend common options of esp_eth_ioctl to cover specifics of MAC chip.
  270. *
  271. * @note This function may not be assigned when the MAC chip supports only most common set of configuration options.
  272. *
  273. * @param[in] mac: Ethernet MAC instance
  274. * @param[in] cmd: IO control command
  275. * @param[in, out] data: address of data for `set` command or address where to store the data when used with `get` command
  276. *
  277. * @return
  278. * - ESP_OK: process io command successfully
  279. * - ESP_ERR_INVALID_ARG: process io command failed because of some invalid argument
  280. * - ESP_FAIL: process io command failed because some other error occurred
  281. * - ESP_ERR_NOT_SUPPORTED: requested feature is not supported
  282. */
  283. esp_err_t (*custom_ioctl)(esp_eth_mac_t *mac, uint32_t cmd, void *data);
  284. /**
  285. * @brief Free memory of Ethernet MAC
  286. *
  287. * @param[in] mac: Ethernet MAC instance
  288. *
  289. * @return
  290. * - ESP_OK: free Ethernet MAC instance successfully
  291. * - ESP_FAIL: free Ethernet MAC instance failed because some error occurred
  292. *
  293. */
  294. esp_err_t (*del)(esp_eth_mac_t *mac);
  295. };
  296. /**
  297. * @brief RMII Clock Mode Options
  298. *
  299. */
  300. typedef enum {
  301. /**
  302. * @brief Default values configured using Kconfig are going to be used when "Default" selected.
  303. *
  304. */
  305. EMAC_CLK_DEFAULT,
  306. /**
  307. * @brief Input RMII Clock from external. EMAC Clock GPIO number needs to be configured when this option is selected.
  308. *
  309. * @note MAC will get RMII clock from outside. Note that ESP32 only supports GPIO0 to input the RMII clock.
  310. *
  311. */
  312. EMAC_CLK_EXT_IN,
  313. /**
  314. * @brief Output RMII Clock from internal APLL Clock. EMAC Clock GPIO number needs to be configured when this option is selected.
  315. *
  316. */
  317. EMAC_CLK_OUT
  318. } emac_rmii_clock_mode_t;
  319. /**
  320. * @brief RMII Clock GPIO number Options
  321. *
  322. */
  323. typedef enum {
  324. /**
  325. * @brief MAC will get RMII clock from outside at this GPIO.
  326. *
  327. * @note ESP32 only supports GPIO0 to input the RMII clock.
  328. *
  329. */
  330. EMAC_CLK_IN_GPIO = 0,
  331. /**
  332. * @brief Output RMII Clock from internal APLL Clock available at GPIO0
  333. *
  334. * @note GPIO0 can be set to output a pre-divided PLL clock (test only!). Enabling this option will configure GPIO0 to output a 50MHz clock.
  335. * In fact this clock doesn’t have directly relationship with EMAC peripheral. Sometimes this clock won’t work well with your PHY chip.
  336. * You might need to add some extra devices after GPIO0 (e.g. inverter). Note that outputting RMII clock on GPIO0 is an experimental practice.
  337. * If you want the Ethernet to work with WiFi, don’t select GPIO0 output mode for stability.
  338. *
  339. */
  340. EMAC_APPL_CLK_OUT_GPIO = 0,
  341. /**
  342. * @brief Output RMII Clock from internal APLL Clock available at GPIO16
  343. *
  344. */
  345. EMAC_CLK_OUT_GPIO = 16,
  346. /**
  347. * @brief Inverted Output RMII Clock from internal APLL Clock available at GPIO17
  348. *
  349. */
  350. EMAC_CLK_OUT_180_GPIO = 17
  351. } emac_rmii_clock_gpio_t;
  352. /**
  353. * @brief Ethernet MAC Clock Configuration
  354. *
  355. */
  356. typedef union {
  357. struct {
  358. // MII interface is not fully implemented...
  359. // Reserved for GPIO number, clock source, etc. in MII mode
  360. } mii; /*!< EMAC MII Clock Configuration */
  361. struct {
  362. emac_rmii_clock_mode_t clock_mode; /*!< RMII Clock Mode Configuration */
  363. emac_rmii_clock_gpio_t clock_gpio; /*!< RMII Clock GPIO Configuration */
  364. } rmii; /*!< EMAC RMII Clock Configuration */
  365. } eth_mac_clock_config_t;
  366. /**
  367. * @brief Configuration of Ethernet MAC object
  368. *
  369. */
  370. typedef struct {
  371. uint32_t sw_reset_timeout_ms; /*!< Software reset timeout value (Unit: ms) */
  372. uint32_t rx_task_stack_size; /*!< Stack size of the receive task */
  373. uint32_t rx_task_prio; /*!< Priority of the receive task */
  374. uint32_t flags; /*!< Flags that specify extra capability for mac driver */
  375. } eth_mac_config_t;
  376. #define ETH_MAC_FLAG_WORK_WITH_CACHE_DISABLE (1 << 0) /*!< MAC driver can work when cache is disabled */
  377. #define ETH_MAC_FLAG_PIN_TO_CORE (1 << 1) /*!< Pin MAC task to the CPU core where driver installation happened */
  378. /**
  379. * @brief Default configuration for Ethernet MAC object
  380. *
  381. */
  382. #define ETH_MAC_DEFAULT_CONFIG() \
  383. { \
  384. .sw_reset_timeout_ms = 100, \
  385. .rx_task_stack_size = 2048, \
  386. .rx_task_prio = 15, \
  387. .flags = 0, \
  388. }
  389. #if CONFIG_ETH_USE_ESP32_EMAC
  390. /**
  391. * @brief EMAC specific configuration
  392. *
  393. */
  394. typedef struct {
  395. int smi_mdc_gpio_num; /*!< SMI MDC GPIO number, set to -1 could bypass the SMI GPIO configuration */
  396. int smi_mdio_gpio_num; /*!< SMI MDIO GPIO number, set to -1 could bypass the SMI GPIO configuration */
  397. eth_data_interface_t interface; /*!< EMAC Data interface to PHY (MII/RMII) */
  398. eth_mac_clock_config_t clock_config; /*!< EMAC Interface clock configuration */
  399. eth_mac_dma_burst_len_t dma_burst_len; /*!< EMAC DMA burst length for both Tx and Rx */
  400. } eth_esp32_emac_config_t;
  401. /**
  402. * @brief Default ESP32's EMAC specific configuration
  403. *
  404. */
  405. #define ETH_ESP32_EMAC_DEFAULT_CONFIG() \
  406. { \
  407. .smi_mdc_gpio_num = 23, \
  408. .smi_mdio_gpio_num = 18, \
  409. .interface = EMAC_DATA_INTERFACE_RMII, \
  410. .clock_config = \
  411. { \
  412. .rmii = \
  413. { \
  414. .clock_mode = EMAC_CLK_DEFAULT, \
  415. .clock_gpio = EMAC_CLK_IN_GPIO \
  416. } \
  417. }, \
  418. .dma_burst_len = ETH_DMA_BURST_LEN_32 \
  419. }
  420. /**
  421. * @brief Create ESP32 Ethernet MAC instance
  422. *
  423. * @param esp32_config: EMAC specific configuration
  424. * @param config: Ethernet MAC configuration
  425. *
  426. * @return
  427. * - instance: create MAC instance successfully
  428. * - NULL: create MAC instance failed because some error occurred
  429. */
  430. esp_eth_mac_t *esp_eth_mac_new_esp32(const eth_esp32_emac_config_t *esp32_config, const eth_mac_config_t *config);
  431. #endif // CONFIG_ETH_USE_ESP32_EMAC
  432. #if CONFIG_ETH_SPI_ETHERNET_DM9051
  433. /**
  434. * @brief DM9051 specific configuration
  435. *
  436. */
  437. typedef struct {
  438. void *spi_hdl; /*!< Handle of SPI device driver */
  439. int int_gpio_num; /*!< Interrupt GPIO number */
  440. } eth_dm9051_config_t;
  441. /**
  442. * @brief Default DM9051 specific configuration
  443. *
  444. */
  445. #define ETH_DM9051_DEFAULT_CONFIG(spi_device) \
  446. { \
  447. .spi_hdl = spi_device, \
  448. .int_gpio_num = 4, \
  449. }
  450. /**
  451. * @brief Create DM9051 Ethernet MAC instance
  452. *
  453. * @param dm9051_config: DM9051 specific configuration
  454. * @param mac_config: Ethernet MAC configuration
  455. *
  456. * @return
  457. * - instance: create MAC instance successfully
  458. * - NULL: create MAC instance failed because some error occurred
  459. */
  460. esp_eth_mac_t *esp_eth_mac_new_dm9051(const eth_dm9051_config_t *dm9051_config, const eth_mac_config_t *mac_config);
  461. #endif // CONFIG_ETH_SPI_ETHERNET_DM9051
  462. #if CONFIG_ETH_SPI_ETHERNET_W5500
  463. /**
  464. * @brief W5500 specific configuration
  465. *
  466. */
  467. typedef struct {
  468. void *spi_hdl; /*!< Handle of SPI device driver */
  469. int int_gpio_num; /*!< Interrupt GPIO number */
  470. } eth_w5500_config_t;
  471. /**
  472. * @brief Default W5500 specific configuration
  473. *
  474. */
  475. #define ETH_W5500_DEFAULT_CONFIG(spi_device) \
  476. { \
  477. .spi_hdl = spi_device, \
  478. .int_gpio_num = 4, \
  479. }
  480. /**
  481. * @brief Create W5500 Ethernet MAC instance
  482. *
  483. * @param w5500_config: W5500 specific configuration
  484. * @param mac_config: Ethernet MAC configuration
  485. *
  486. * @return
  487. * - instance: create MAC instance successfully
  488. * - NULL: create MAC instance failed because some error occurred
  489. */
  490. esp_eth_mac_t *esp_eth_mac_new_w5500(const eth_w5500_config_t *w5500_config, const eth_mac_config_t *mac_config);
  491. #endif // CONFIG_ETH_SPI_ETHERNET_W5500
  492. #if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL
  493. /**
  494. * @brief KSZ8851SNL specific configuration
  495. *
  496. */
  497. typedef struct {
  498. void *spi_hdl; /*!< Handle of SPI device driver */
  499. int int_gpio_num; /*!< Interrupt GPIO number */
  500. } eth_ksz8851snl_config_t;
  501. /**
  502. * @brief Default KSZ8851SNL specific configuration
  503. *
  504. */
  505. #define ETH_KSZ8851SNL_DEFAULT_CONFIG(spi_device) \
  506. { \
  507. .spi_hdl = spi_device, \
  508. .int_gpio_num = 14, \
  509. }
  510. /**
  511. * @brief Create KSZ8851SNL Ethernet MAC instance
  512. *
  513. * @param ksz8851snl_config: KSZ8851SNL specific configuration
  514. * @param mac_config: Ethernet MAC configuration
  515. *
  516. * @return
  517. * - instance: create MAC instance successfully
  518. * - NULL: create MAC instance failed because some error occurred
  519. */
  520. esp_eth_mac_t *esp_eth_mac_new_ksz8851snl(const eth_ksz8851snl_config_t *ksz8851snl_config, const eth_mac_config_t *mac_config);
  521. #endif // CONFIG_ETH_SPI_ETHERNET_KSZ8851
  522. #if CONFIG_ETH_USE_OPENETH
  523. /**
  524. * @brief Create OpenCores Ethernet MAC instance
  525. *
  526. * @param config: Ethernet MAC configuration
  527. *
  528. * @return
  529. * - instance: create MAC instance successfully
  530. * - NULL: create MAC instance failed because some error occurred
  531. */
  532. esp_eth_mac_t *esp_eth_mac_new_openeth(const eth_mac_config_t *config);
  533. #endif // CONFIG_ETH_USE_OPENETH
  534. #ifdef __cplusplus
  535. }
  536. #endif