esp_eth_mac.h 9.9 KB

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