esp_eth_mac.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 Configuration of Ethernet MAC object
  270. *
  271. */
  272. typedef struct {
  273. uint32_t sw_reset_timeout_ms; /*!< Software reset timeout value (Unit: ms) */
  274. uint32_t rx_task_stack_size; /*!< Stack size of the receive task */
  275. uint32_t rx_task_prio; /*!< Priority of the receive task */
  276. int smi_mdc_gpio_num; /*!< SMI MDC GPIO number, set to -1 could bypass the SMI GPIO configuration */
  277. int smi_mdio_gpio_num; /*!< SMI MDIO GPIO number, set to -1 could bypass the SMI GPIO configuration */
  278. uint32_t flags; /*!< Flags that specify extra capability for mac driver */
  279. } eth_mac_config_t;
  280. #define ETH_MAC_FLAG_WORK_WITH_CACHE_DISABLE (1 << 0) /*!< MAC driver can work when cache is disabled */
  281. #define ETH_MAC_FLAG_PIN_TO_CORE (1 << 1) /*!< Pin MAC task to the CPU core where driver installation happened */
  282. /**
  283. * @brief Default configuration for Ethernet MAC object
  284. *
  285. */
  286. #define ETH_MAC_DEFAULT_CONFIG() \
  287. { \
  288. .sw_reset_timeout_ms = 100, \
  289. .rx_task_stack_size = 4096, \
  290. .rx_task_prio = 15, \
  291. .smi_mdc_gpio_num = 23, \
  292. .smi_mdio_gpio_num = 18, \
  293. .flags = 0, \
  294. }
  295. #if CONFIG_ETH_USE_ESP32_EMAC
  296. /**
  297. * @brief Create ESP32 Ethernet MAC instance
  298. *
  299. * @param config: Ethernet MAC configuration
  300. *
  301. * @return
  302. * - instance: create MAC instance successfully
  303. * - NULL: create MAC instance failed because some error occurred
  304. */
  305. esp_eth_mac_t *esp_eth_mac_new_esp32(const eth_mac_config_t *config);
  306. #endif // CONFIG_ETH_USE_ESP32_EMAC
  307. #if CONFIG_ETH_SPI_ETHERNET_DM9051
  308. /**
  309. * @brief DM9051 specific configuration
  310. *
  311. */
  312. typedef struct {
  313. void *spi_hdl; /*!< Handle of SPI device driver */
  314. int int_gpio_num; /*!< Interrupt GPIO number */
  315. } eth_dm9051_config_t;
  316. /**
  317. * @brief Default DM9051 specific configuration
  318. *
  319. */
  320. #define ETH_DM9051_DEFAULT_CONFIG(spi_device) \
  321. { \
  322. .spi_hdl = spi_device, \
  323. .int_gpio_num = 4, \
  324. }
  325. /**
  326. * @brief Create DM9051 Ethernet MAC instance
  327. *
  328. * @param dm9051_config: DM9051 specific configuration
  329. * @param mac_config: Ethernet MAC configuration
  330. *
  331. * @return
  332. * - instance: create MAC instance successfully
  333. * - NULL: create MAC instance failed because some error occurred
  334. */
  335. esp_eth_mac_t *esp_eth_mac_new_dm9051(const eth_dm9051_config_t *dm9051_config, const eth_mac_config_t *mac_config);
  336. #endif // CONFIG_ETH_SPI_ETHERNET_DM9051
  337. #if CONFIG_ETH_SPI_ETHERNET_W5500
  338. /**
  339. * @brief W5500 specific configuration
  340. *
  341. */
  342. typedef struct {
  343. void *spi_hdl; /*!< Handle of SPI device driver */
  344. int int_gpio_num; /*!< Interrupt GPIO number */
  345. } eth_w5500_config_t;
  346. /**
  347. * @brief Default W5500 specific configuration
  348. *
  349. */
  350. #define ETH_W5500_DEFAULT_CONFIG(spi_device) \
  351. { \
  352. .spi_hdl = spi_device, \
  353. .int_gpio_num = 4, \
  354. }
  355. /**
  356. * @brief Create W5500 Ethernet MAC instance
  357. *
  358. * @param w5500_config: W5500 specific configuration
  359. * @param mac_config: Ethernet MAC configuration
  360. *
  361. * @return
  362. * - instance: create MAC instance successfully
  363. * - NULL: create MAC instance failed because some error occurred
  364. */
  365. esp_eth_mac_t *esp_eth_mac_new_w5500(const eth_w5500_config_t *w5500_config, const eth_mac_config_t *mac_config);
  366. #endif // CONFIG_ETH_SPI_ETHERNET_W5500
  367. #if CONFIG_ETH_USE_OPENETH
  368. /**
  369. * @brief Create OpenCores Ethernet MAC instance
  370. *
  371. * @param config: Ethernet MAC configuration
  372. *
  373. * @return
  374. * - instance: create MAC instance successfully
  375. * - NULL: create MAC instance failed because some error occurred
  376. */
  377. esp_eth_mac_t *esp_eth_mac_new_openeth(const eth_mac_config_t *config);
  378. #endif // CONFIG_ETH_USE_OPENETH
  379. #ifdef __cplusplus
  380. }
  381. #endif