esp_eth_mac.h 17 KB

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