esp_eth_mac.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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_ARG: transmit packet failed because of invalid argument
  90. * - ESP_ERR_INVALID_STATE: transmit packet failed because of wrong state of MAC
  91. * - ESP_FAIL: transmit packet failed because some other error occurred
  92. *
  93. */
  94. esp_err_t (*transmit)(esp_eth_mac_t *mac, uint8_t *buf, uint32_t length);
  95. /**
  96. * @brief Receive packet from Ethernet MAC
  97. *
  98. * @param[in] mac: Ethernet MAC instance
  99. * @param[out] buf: packet buffer which will preserve the received frame
  100. * @param[out] length: length of the received packet
  101. *
  102. * @note Memory of buf is allocated in the Layer2, make sure it get free after process.
  103. * @note Before this function got invoked, the value of "length" should set by user, equals the size of buffer.
  104. * After the function returned, the value of "length" means the real length of received data.
  105. *
  106. * @return
  107. * - ESP_OK: receive packet successfully
  108. * - ESP_ERR_INVALID_ARG: receive packet failed because of invalid argument
  109. * - ESP_ERR_INVALID_SIZE: input buffer size is not enough to hold the incoming data.
  110. * in this case, value of returned "length" indicates the real size of incoming data.
  111. * - ESP_FAIL: receive packet failed because some other error occurred
  112. *
  113. */
  114. esp_err_t (*receive)(esp_eth_mac_t *mac, uint8_t *buf, uint32_t *length);
  115. /**
  116. * @brief Read PHY register
  117. *
  118. * @param[in] mac: Ethernet MAC instance
  119. * @param[in] phy_addr: PHY chip address (0~31)
  120. * @param[in] phy_reg: PHY register index code
  121. * @param[out] reg_value: PHY register value
  122. *
  123. * @return
  124. * - ESP_OK: read PHY register successfully
  125. * - ESP_ERR_INVALID_ARG: read PHY register failed because of invalid argument
  126. * - ESP_ERR_INVALID_STATE: read PHY register failed because of wrong state of MAC
  127. * - ESP_ERR_TIMEOUT: read PHY register failed because of timeout
  128. * - ESP_FAIL: read PHY register failed because some other error occurred
  129. *
  130. */
  131. esp_err_t (*read_phy_reg)(esp_eth_mac_t *mac, uint32_t phy_addr, uint32_t phy_reg, uint32_t *reg_value);
  132. /**
  133. * @brief Write PHY register
  134. *
  135. * @param[in] mac: Ethernet MAC instance
  136. * @param[in] phy_addr: PHY chip address (0~31)
  137. * @param[in] phy_reg: PHY register index code
  138. * @param[in] reg_value: PHY register value
  139. *
  140. * @return
  141. * - ESP_OK: write PHY register successfully
  142. * - ESP_ERR_INVALID_STATE: write PHY register failed because of wrong state of MAC
  143. * - ESP_ERR_TIMEOUT: write PHY register failed because of timeout
  144. * - ESP_FAIL: write PHY register failed because some other error occurred
  145. *
  146. */
  147. esp_err_t (*write_phy_reg)(esp_eth_mac_t *mac, uint32_t phy_addr, uint32_t phy_reg, uint32_t reg_value);
  148. /**
  149. * @brief Set MAC address
  150. *
  151. * @param[in] mac: Ethernet MAC instance
  152. * @param[in] addr: MAC address
  153. *
  154. * @return
  155. * - ESP_OK: set MAC address successfully
  156. * - ESP_ERR_INVALID_ARG: set MAC address failed because of invalid argument
  157. * - ESP_FAIL: set MAC address failed because some other error occurred
  158. *
  159. */
  160. esp_err_t (*set_addr)(esp_eth_mac_t *mac, uint8_t *addr);
  161. /**
  162. * @brief Get MAC address
  163. *
  164. * @param[in] mac: Ethernet MAC instance
  165. * @param[out] addr: MAC address
  166. *
  167. * @return
  168. * - ESP_OK: get MAC address successfully
  169. * - ESP_ERR_INVALID_ARG: get MAC address failed because of invalid argument
  170. * - ESP_FAIL: get MAC address failed because some other error occurred
  171. *
  172. */
  173. esp_err_t (*get_addr)(esp_eth_mac_t *mac, uint8_t *addr);
  174. /**
  175. * @brief Set speed of MAC
  176. *
  177. * @param[in] ma:c Ethernet MAC instance
  178. * @param[in] speed: MAC speed
  179. *
  180. * @return
  181. * - ESP_OK: set MAC speed successfully
  182. * - ESP_ERR_INVALID_ARG: set MAC speed failed because of invalid argument
  183. * - ESP_FAIL: set MAC speed failed because some other error occurred
  184. *
  185. */
  186. esp_err_t (*set_speed)(esp_eth_mac_t *mac, eth_speed_t speed);
  187. /**
  188. * @brief Set duplex mode of MAC
  189. *
  190. * @param[in] mac: Ethernet MAC instance
  191. * @param[in] duplex: MAC duplex
  192. *
  193. * @return
  194. * - ESP_OK: set MAC duplex mode successfully
  195. * - ESP_ERR_INVALID_ARG: set MAC duplex failed because of invalid argument
  196. * - ESP_FAIL: set MAC duplex failed because some other error occurred
  197. *
  198. */
  199. esp_err_t (*set_duplex)(esp_eth_mac_t *mac, eth_duplex_t duplex);
  200. /**
  201. * @brief Set link status of MAC
  202. *
  203. * @param[in] mac: Ethernet MAC instance
  204. * @param[in] link: Link status
  205. *
  206. * @return
  207. * - ESP_OK: set link status successfully
  208. * - ESP_ERR_INVALID_ARG: set link status failed because of invalid argument
  209. * - ESP_FAIL: set link status failed because some other error occurred
  210. *
  211. */
  212. esp_err_t (*set_link)(esp_eth_mac_t *mac, eth_link_t link);
  213. /**
  214. * @brief Set promiscuous of MAC
  215. *
  216. * @param[in] mac: Ethernet MAC instance
  217. * @param[in] enable: set true to enable promiscuous mode; set false to disable promiscuous mode
  218. *
  219. * @return
  220. * - ESP_OK: set promiscuous mode successfully
  221. * - ESP_FAIL: set promiscuous mode failed because some error occurred
  222. *
  223. */
  224. esp_err_t (*set_promiscuous)(esp_eth_mac_t *mac, bool enable);
  225. /**
  226. * @brief Enable flow control on MAC layer or not
  227. *
  228. * @param[in] mac: Ethernet MAC instance
  229. * @param[in] enable: set true to enable flow control; set false to disable flow control
  230. *
  231. * @return
  232. * - ESP_OK: set flow control successfully
  233. * - ESP_FAIL: set flow control failed because some error occurred
  234. *
  235. */
  236. esp_err_t (*enable_flow_ctrl)(esp_eth_mac_t *mac, bool enable);
  237. /**
  238. * @brief Set the PAUSE ability of peer node
  239. *
  240. * @param[in] mac: Ethernet MAC instance
  241. * @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
  242. *
  243. * @return
  244. * - ESP_OK: set peer pause ability successfully
  245. * - ESP_FAIL: set peer pause ability failed because some error occurred
  246. */
  247. esp_err_t (*set_peer_pause_ability)(esp_eth_mac_t *mac, uint32_t ability);
  248. /**
  249. * @brief Free memory of Ethernet MAC
  250. *
  251. * @param[in] mac: Ethernet MAC instance
  252. *
  253. * @return
  254. * - ESP_OK: free Ethernet MAC instance successfully
  255. * - ESP_FAIL: free Ethernet MAC instance failed because some error occurred
  256. *
  257. */
  258. esp_err_t (*del)(esp_eth_mac_t *mac);
  259. };
  260. /**
  261. * @brief RMII Clock Mode Options
  262. *
  263. */
  264. typedef enum {
  265. /**
  266. * @brief Default values configured using Kconfig are going to be used when "Default" selected.
  267. *
  268. */
  269. EMAC_CLK_DEFAULT,
  270. /**
  271. * @brief Input RMII Clock from external. EMAC Clock GPIO number needs to be configured when this option is selected.
  272. *
  273. * @note MAC will get RMII clock from outside. Note that ESP32 only supports GPIO0 to input the RMII clock.
  274. *
  275. */
  276. EMAC_CLK_EXT_IN,
  277. /**
  278. * @brief Output RMII Clock from internal APLL Clock. EMAC Clock GPIO number needs to be configured when this option is selected.
  279. *
  280. */
  281. EMAC_CLK_OUT
  282. } emac_rmii_clock_mode_t;
  283. /**
  284. * @brief RMII Clock GPIO number Options
  285. *
  286. */
  287. typedef enum {
  288. /**
  289. * @brief MAC will get RMII clock from outside at this GPIO.
  290. *
  291. * @note ESP32 only supports GPIO0 to input the RMII clock.
  292. *
  293. */
  294. EMAC_CLK_IN_GPIO = 0,
  295. /**
  296. * @brief Output RMII Clock from internal APLL Clock available at GPIO0
  297. *
  298. * @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.
  299. * In fact this clock doesn’t have directly relationship with EMAC peripheral. Sometimes this clock won’t work well with your PHY chip.
  300. * You might need to add some extra devices after GPIO0 (e.g. inverter). Note that outputting RMII clock on GPIO0 is an experimental practice.
  301. * If you want the Ethernet to work with WiFi, don’t select GPIO0 output mode for stability.
  302. *
  303. */
  304. EMAC_APPL_CLK_OUT_GPIO = 0,
  305. /**
  306. * @brief Output RMII Clock from internal APLL Clock available at GPIO16
  307. *
  308. */
  309. EMAC_CLK_OUT_GPIO = 16,
  310. /**
  311. * @brief Inverted Output RMII Clock from internal APLL Clock available at GPIO17
  312. *
  313. */
  314. EMAC_CLK_OUT_180_GPIO = 17
  315. } emac_rmii_clock_gpio_t;
  316. /**
  317. * @brief Ethernet MAC Clock Configuration
  318. *
  319. */
  320. typedef union {
  321. struct {
  322. // MII interface is not fully implemented...
  323. // Reserved for GPIO number, clock source, etc. in MII mode
  324. } mii; /*!< EMAC MII Clock Configuration */
  325. struct {
  326. emac_rmii_clock_mode_t clock_mode; /*!< RMII Clock Mode Configuration */
  327. emac_rmii_clock_gpio_t clock_gpio; /*!< RMII Clock GPIO Configuration */
  328. } rmii; /*!< EMAC RMII Clock Configuration */
  329. } eth_mac_clock_config_t;
  330. /**
  331. * @brief Configuration of Ethernet MAC object
  332. *
  333. */
  334. typedef struct {
  335. uint32_t sw_reset_timeout_ms; /*!< Software reset timeout value (Unit: ms) */
  336. uint32_t rx_task_stack_size; /*!< Stack size of the receive task */
  337. uint32_t rx_task_prio; /*!< Priority of the receive task */
  338. uint32_t flags; /*!< Flags that specify extra capability for mac driver */
  339. } eth_mac_config_t;
  340. #define ETH_MAC_FLAG_WORK_WITH_CACHE_DISABLE (1 << 0) /*!< MAC driver can work when cache is disabled */
  341. #define ETH_MAC_FLAG_PIN_TO_CORE (1 << 1) /*!< Pin MAC task to the CPU core where driver installation happened */
  342. /**
  343. * @brief Default configuration for Ethernet MAC object
  344. *
  345. */
  346. #define ETH_MAC_DEFAULT_CONFIG() \
  347. { \
  348. .sw_reset_timeout_ms = 100, \
  349. .rx_task_stack_size = 2048, \
  350. .rx_task_prio = 15, \
  351. .flags = 0, \
  352. }
  353. #if CONFIG_ETH_USE_ESP32_EMAC
  354. /**
  355. * @brief EMAC specific configuration
  356. *
  357. */
  358. typedef struct {
  359. int smi_mdc_gpio_num; /*!< SMI MDC GPIO number, set to -1 could bypass the SMI GPIO configuration */
  360. int smi_mdio_gpio_num; /*!< SMI MDIO GPIO number, set to -1 could bypass the SMI GPIO configuration */
  361. eth_data_interface_t interface; /*!< EMAC Data interface to PHY (MII/RMII) */
  362. eth_mac_clock_config_t clock_config; /*!< EMAC Interface clock configuration */
  363. eth_mac_dma_burst_len_t dma_burst_len; /*!< EMAC DMA burst length for both Tx and Rx */
  364. } eth_esp32_emac_config_t;
  365. /**
  366. * @brief Default ESP32's EMAC specific configuration
  367. *
  368. */
  369. #define ETH_ESP32_EMAC_DEFAULT_CONFIG() \
  370. { \
  371. .smi_mdc_gpio_num = 23, \
  372. .smi_mdio_gpio_num = 18, \
  373. .interface = EMAC_DATA_INTERFACE_RMII, \
  374. .clock_config = \
  375. { \
  376. .rmii = \
  377. { \
  378. .clock_mode = EMAC_CLK_DEFAULT, \
  379. .clock_gpio = EMAC_CLK_IN_GPIO \
  380. } \
  381. }, \
  382. .dma_burst_len = ETH_DMA_BURST_LEN_32 \
  383. }
  384. /**
  385. * @brief Create ESP32 Ethernet MAC instance
  386. *
  387. * @param config: Ethernet MAC configuration
  388. *
  389. * @return
  390. * - instance: create MAC instance successfully
  391. * - NULL: create MAC instance failed because some error occurred
  392. */
  393. esp_eth_mac_t *esp_eth_mac_new_esp32(const eth_esp32_emac_config_t *esp32_config, const eth_mac_config_t *config);
  394. #endif // CONFIG_ETH_USE_ESP32_EMAC
  395. #if CONFIG_ETH_SPI_ETHERNET_DM9051
  396. /**
  397. * @brief DM9051 specific configuration
  398. *
  399. */
  400. typedef struct {
  401. void *spi_hdl; /*!< Handle of SPI device driver */
  402. int int_gpio_num; /*!< Interrupt GPIO number */
  403. } eth_dm9051_config_t;
  404. /**
  405. * @brief Default DM9051 specific configuration
  406. *
  407. */
  408. #define ETH_DM9051_DEFAULT_CONFIG(spi_device) \
  409. { \
  410. .spi_hdl = spi_device, \
  411. .int_gpio_num = 4, \
  412. }
  413. /**
  414. * @brief Create DM9051 Ethernet MAC instance
  415. *
  416. * @param dm9051_config: DM9051 specific configuration
  417. * @param mac_config: Ethernet MAC configuration
  418. *
  419. * @return
  420. * - instance: create MAC instance successfully
  421. * - NULL: create MAC instance failed because some error occurred
  422. */
  423. esp_eth_mac_t *esp_eth_mac_new_dm9051(const eth_dm9051_config_t *dm9051_config, const eth_mac_config_t *mac_config);
  424. #endif // CONFIG_ETH_SPI_ETHERNET_DM9051
  425. #if CONFIG_ETH_SPI_ETHERNET_W5500
  426. /**
  427. * @brief W5500 specific configuration
  428. *
  429. */
  430. typedef struct {
  431. void *spi_hdl; /*!< Handle of SPI device driver */
  432. int int_gpio_num; /*!< Interrupt GPIO number */
  433. } eth_w5500_config_t;
  434. /**
  435. * @brief Default W5500 specific configuration
  436. *
  437. */
  438. #define ETH_W5500_DEFAULT_CONFIG(spi_device) \
  439. { \
  440. .spi_hdl = spi_device, \
  441. .int_gpio_num = 4, \
  442. }
  443. /**
  444. * @brief Create W5500 Ethernet MAC instance
  445. *
  446. * @param w5500_config: W5500 specific configuration
  447. * @param mac_config: Ethernet MAC configuration
  448. *
  449. * @return
  450. * - instance: create MAC instance successfully
  451. * - NULL: create MAC instance failed because some error occurred
  452. */
  453. esp_eth_mac_t *esp_eth_mac_new_w5500(const eth_w5500_config_t *w5500_config, const eth_mac_config_t *mac_config);
  454. #endif // CONFIG_ETH_SPI_ETHERNET_W5500
  455. #if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL
  456. /**
  457. * @brief KSZ8851SNL specific configuration
  458. *
  459. */
  460. typedef struct {
  461. void *spi_hdl; /*!< Handle of SPI device driver */
  462. int int_gpio_num; /*!< Interrupt GPIO number */
  463. } eth_ksz8851snl_config_t;
  464. /**
  465. * @brief Default KSZ8851SNL specific configuration
  466. *
  467. */
  468. #define ETH_KSZ8851SNL_DEFAULT_CONFIG(spi_device) \
  469. { \
  470. .spi_hdl = spi_device, \
  471. .int_gpio_num = 14, \
  472. }
  473. /**
  474. * @brief Create KSZ8851SNL Ethernet MAC instance
  475. *
  476. * @param ksz8851snl_config: KSZ8851SNL specific configuration
  477. * @param mac_config: Ethernet MAC configuration
  478. *
  479. * @return
  480. * - instance: create MAC instance successfully
  481. * - NULL: create MAC instance failed because some error occurred
  482. */
  483. esp_eth_mac_t *esp_eth_mac_new_ksz8851snl(const eth_ksz8851snl_config_t *ksz8851snl_config, const eth_mac_config_t *mac_config);
  484. #endif // CONFIG_ETH_SPI_ETHERNET_KSZ8851
  485. #if CONFIG_ETH_USE_OPENETH
  486. /**
  487. * @brief Create OpenCores Ethernet MAC instance
  488. *
  489. * @param config: Ethernet MAC configuration
  490. *
  491. * @return
  492. * - instance: create MAC instance successfully
  493. * - NULL: create MAC instance failed because some error occurred
  494. */
  495. esp_eth_mac_t *esp_eth_mac_new_openeth(const eth_mac_config_t *config);
  496. #endif // CONFIG_ETH_USE_OPENETH
  497. #ifdef __cplusplus
  498. }
  499. #endif