esp_eth_mac.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include <stdbool.h>
  19. #include <stdatomic.h>
  20. #include "sdkconfig.h"
  21. #include "esp_eth_com.h"
  22. #if CONFIG_ETH_USE_SPI_ETHERNET
  23. #include "driver/spi_master.h"
  24. #endif
  25. /**
  26. * @brief Ethernet MAC
  27. *
  28. */
  29. typedef struct esp_eth_mac_s esp_eth_mac_t;
  30. /**
  31. * @brief Ethernet MAC
  32. *
  33. */
  34. struct esp_eth_mac_s {
  35. /**
  36. * @brief Reference count of MAC instance
  37. *
  38. */
  39. atomic_int ref_count;
  40. /**
  41. * @brief Set mediator for Ethernet MAC
  42. *
  43. * @param[in] mac: Ethernet MAC instance
  44. * @param[in] eth: Ethernet mediator
  45. *
  46. * @return
  47. * - ESP_OK: set mediator for Ethernet MAC successfully
  48. * - ESP_ERR_INVALID_ARG: set mediator for Ethernet MAC failed because of invalid argument
  49. *
  50. */
  51. esp_err_t (*set_mediator)(esp_eth_mac_t *mac, esp_eth_mediator_t *eth);
  52. /**
  53. * @brief Initialize Ethernet MAC
  54. *
  55. * @param[in] mac: Ethernet MAC instance
  56. *
  57. * @return
  58. * - ESP_OK: initialize Ethernet MAC successfully
  59. * - ESP_ERR_TIMEOUT: initialize Ethernet MAC failed because of timeout
  60. * - ESP_FAIL: initialize Ethernet MAC failed because some other error occurred
  61. *
  62. */
  63. esp_err_t (*init)(esp_eth_mac_t *mac);
  64. /**
  65. * @brief Deinitialize Ethernet MAC
  66. *
  67. * @param[in] mac: Ethernet MAC instance
  68. *
  69. * @return
  70. * - ESP_OK: deinitialize Ethernet MAC successfully
  71. * - ESP_FAIL: deinitialize Ethernet MAC failed because some error occurred
  72. *
  73. */
  74. esp_err_t (*deinit)(esp_eth_mac_t *mac);
  75. /**
  76. * @brief Transmit packet from Ethernet MAC
  77. *
  78. * @param[in] mac: Ethernet MAC instance
  79. * @param[in] buf: packet buffer to transmit
  80. * @param[in] length: length of packet
  81. *
  82. * @return
  83. * - ESP_OK: transmit packet successfully
  84. * - ESP_ERR_INVALID_ARG: transmit packet failed because of invalid argument
  85. * - ESP_ERR_INVALID_STATE: transmit packet failed because of wrong state of MAC
  86. * - ESP_FAIL: transmit packet failed because some other error occurred
  87. *
  88. */
  89. esp_err_t (*transmit)(esp_eth_mac_t *mac, uint8_t *buf, uint32_t length);
  90. /**
  91. * @brief Receive packet from Ethernet MAC
  92. *
  93. * @param[in] mac: Ethernet MAC instance
  94. * @param[out] buf: packet buffer which will preserve the received frame
  95. * @param[out] length: length of the received packet
  96. *
  97. * @note Memory of buf is allocated in the Layer2, make sure it get free after process.
  98. * @note Before this function got invoked, the value of "length" should set by user, equals the size of buffer.
  99. * After the function returned, the value of "length" means the real length of received data.
  100. *
  101. * @return
  102. * - ESP_OK: receive packet successfully
  103. * - ESP_ERR_INVALID_ARG: receive packet failed because of invalid argument
  104. * - ESP_ERR_INVALID_SIZE: input buffer size is not enough to hold the incoming data.
  105. * in this case, value of returned "length" indicates the real size of incoming data.
  106. * - ESP_FAIL: receive packet failed because some other error occurred
  107. *
  108. */
  109. esp_err_t (*receive)(esp_eth_mac_t *mac, uint8_t *buf, uint32_t *length);
  110. /**
  111. * @brief Read PHY register
  112. *
  113. * @param[in] mac: Ethernet MAC instance
  114. * @param[in] phy_addr: PHY chip address (0~31)
  115. * @param[in] phy_reg: PHY register index code
  116. * @param[out] reg_value: PHY register value
  117. *
  118. * @return
  119. * - ESP_OK: read PHY register successfully
  120. * - ESP_ERR_INVALID_ARG: read PHY register failed because of invalid argument
  121. * - ESP_ERR_INVALID_STATE: read PHY register failed because of wrong state of MAC
  122. * - ESP_ERR_TIMEOUT: read PHY register failed because of timeout
  123. * - ESP_FAIL: read PHY register failed because some other error occurred
  124. *
  125. */
  126. esp_err_t (*read_phy_reg)(esp_eth_mac_t *mac, uint32_t phy_addr, uint32_t phy_reg, uint32_t *reg_value);
  127. /**
  128. * @brief Write PHY register
  129. *
  130. * @param[in] mac: Ethernet MAC instance
  131. * @param[in] phy_addr: PHY chip address (0~31)
  132. * @param[in] phy_reg: PHY register index code
  133. * @param[in] reg_value: PHY register value
  134. *
  135. * @return
  136. * - ESP_OK: write PHY register successfully
  137. * - ESP_ERR_INVALID_STATE: write PHY register failed because of wrong state of MAC
  138. * - ESP_ERR_TIMEOUT: write PHY register failed because of timeout
  139. * - ESP_FAIL: write PHY register failed because some other error occurred
  140. *
  141. */
  142. esp_err_t (*write_phy_reg)(esp_eth_mac_t *mac, uint32_t phy_addr, uint32_t phy_reg, uint32_t reg_value);
  143. /**
  144. * @brief Set MAC address
  145. *
  146. * @param[in] mac: Ethernet MAC instance
  147. * @param[in] addr: MAC address
  148. *
  149. * @return
  150. * - ESP_OK: set MAC address successfully
  151. * - ESP_ERR_INVALID_ARG: set MAC address failed because of invalid argument
  152. * - ESP_FAIL: set MAC address failed because some other error occurred
  153. *
  154. */
  155. esp_err_t (*set_addr)(esp_eth_mac_t *mac, uint8_t *addr);
  156. /**
  157. * @brief Get MAC address
  158. *
  159. * @param[in] mac: Ethernet MAC instance
  160. * @param[out] addr: MAC address
  161. *
  162. * @return
  163. * - ESP_OK: get MAC address successfully
  164. * - ESP_ERR_INVALID_ARG: get MAC address failed because of invalid argument
  165. * - ESP_FAIL: get MAC address failed because some other error occurred
  166. *
  167. */
  168. esp_err_t (*get_addr)(esp_eth_mac_t *mac, uint8_t *addr);
  169. /**
  170. * @brief Set speed of MAC
  171. *
  172. * @param[in] ma:c Ethernet MAC instance
  173. * @param[in] speed: MAC speed
  174. *
  175. * @return
  176. * - ESP_OK: set MAC speed successfully
  177. * - ESP_ERR_INVALID_ARG: set MAC speed failed because of invalid argument
  178. * - ESP_FAIL: set MAC speed failed because some other error occurred
  179. *
  180. */
  181. esp_err_t (*set_speed)(esp_eth_mac_t *mac, eth_speed_t speed);
  182. /**
  183. * @brief Set duplex mode of MAC
  184. *
  185. * @param[in] mac: Ethernet MAC instance
  186. * @param[in] duplex: MAC duplex
  187. *
  188. * @return
  189. * - ESP_OK: set MAC duplex mode successfully
  190. * - ESP_ERR_INVALID_ARG: set MAC duplex failed because of invalid argument
  191. * - ESP_FAIL: set MAC duplex failed because some other error occurred
  192. *
  193. */
  194. esp_err_t (*set_duplex)(esp_eth_mac_t *mac, eth_duplex_t duplex);
  195. /**
  196. * @brief Set link status of MAC
  197. *
  198. * @param[in] mac: Ethernet MAC instance
  199. * @param[in] link: Link status
  200. *
  201. * @return
  202. * - ESP_OK: set link status successfully
  203. * - ESP_ERR_INVALID_ARG: set link status failed because of invalid argument
  204. * - ESP_FAIL: set link status failed because some other error occurred
  205. *
  206. */
  207. esp_err_t (*set_link)(esp_eth_mac_t *mac, eth_link_t link);
  208. /**
  209. * @brief Set promiscuous of MAC
  210. *
  211. * @param[in] mac: Ethernet MAC instance
  212. * @param[in] enable: set true to enable promiscuous mode; set false to disable promiscuous mode
  213. *
  214. * @return
  215. * - ESP_OK: set promiscuous mode successfully
  216. * - ESP_FAIL: set promiscuous mode failed because some error occurred
  217. *
  218. */
  219. esp_err_t (*set_promiscuous)(esp_eth_mac_t *mac, bool enable);
  220. /**
  221. * @brief Free memory of Ethernet MAC
  222. *
  223. * @param[in] mac: Ethernet MAC instance
  224. *
  225. * @return
  226. * - ESP_OK: free Ethernet MAC instance successfully
  227. * - ESP_FAIL: free Ethernet MAC instance failed because some error occurred
  228. *
  229. */
  230. esp_err_t (*del)(esp_eth_mac_t *mac);
  231. };
  232. /**
  233. * @brief Configuration of Ethernet MAC object
  234. *
  235. */
  236. typedef struct {
  237. uint32_t sw_reset_timeout_ms; /*!< Software reset timeout value (Unit: ms) */
  238. uint32_t rx_task_stack_size; /*!< Stack size of the receive task */
  239. uint32_t rx_task_prio; /*!< Priority of the receive task */
  240. int smi_mdc_gpio_num; /*!< SMI MDC GPIO number */
  241. int smi_mdio_gpio_num; /*!< SMI MDIO GPIO number */
  242. } eth_mac_config_t;
  243. /**
  244. * @brief Default configuration for Ethernet MAC object
  245. *
  246. */
  247. #define ETH_MAC_DEFAULT_CONFIG() \
  248. { \
  249. .sw_reset_timeout_ms = 100, \
  250. .rx_task_stack_size = 4096, \
  251. .rx_task_prio = 15, \
  252. .smi_mdc_gpio_num = 23, \
  253. .smi_mdio_gpio_num = 18, \
  254. }
  255. #if CONFIG_ETH_USE_ESP32_EMAC
  256. /**
  257. * @brief Create ESP32 Ethernet MAC instance
  258. *
  259. * @param config: Ethernet MAC configuration
  260. *
  261. * @return
  262. * - instance: create MAC instance successfully
  263. * - NULL: create MAC instance failed because some error occurred
  264. */
  265. esp_eth_mac_t *esp_eth_mac_new_esp32(const eth_mac_config_t *config);
  266. #endif
  267. #if CONFIG_ETH_SPI_ETHERNET_DM9051
  268. /**
  269. * @brief DM9051 specific configuration
  270. *
  271. */
  272. typedef struct {
  273. spi_device_handle_t spi_hdl; /*!< Handle of SPI device driver */
  274. int int_gpio_num; /*!< Interrupt GPIO number */
  275. } eth_dm9051_config_t;
  276. /**
  277. * @brief Default DM9051 specific configuration
  278. *
  279. */
  280. #define ETH_DM9051_DEFAULT_CONFIG(spi_device) \
  281. { \
  282. .spi_hdl = spi_device, \
  283. .int_gpio_num = 4, \
  284. }
  285. /**
  286. * @brief Create DM9051 Ethernet MAC instance
  287. *
  288. * @param dm9051_config: DM9051 specific configuration
  289. * @param mac_config: Ethernet MAC configuration
  290. *
  291. * @return
  292. * - instance: create MAC instance successfully
  293. * - NULL: create MAC instance failed because some error occurred
  294. */
  295. esp_eth_mac_t *esp_eth_mac_new_dm9051(const eth_dm9051_config_t *dm9051_config, const eth_mac_config_t *mac_config);
  296. #endif
  297. #if CONFIG_ETH_USE_OPENETH
  298. /**
  299. * @brief Create OpenETH MAC instance
  300. *
  301. * @note This API is only used for qemu simulation
  302. *
  303. * @param config: Ethernet MAC configuration
  304. *
  305. * @return
  306. * - instance: create MAC instance successfully
  307. * - NULL: create MAC instance failed because some error occurred
  308. */
  309. esp_eth_mac_t *esp_eth_mac_new_openeth(const eth_mac_config_t *config);
  310. #endif // CONFIG_ETH_USE_OPENETH
  311. #ifdef __cplusplus
  312. }
  313. #endif