esp_eth_mac.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. /**
  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 Transmit packet from Ethernet MAC
  68. *
  69. * @param[in] mac: Ethernet MAC instance
  70. * @param[in] buf: packet buffer to transmit
  71. * @param[in] length: length of packet
  72. *
  73. * @return
  74. * - ESP_OK: transmit packet successfully
  75. * - ESP_ERR_INVALID_ARG: transmit packet failed because of invalid argument
  76. * - ESP_ERR_INVALID_STATE: transmit packet failed because of wrong state of MAC
  77. * - ESP_FAIL: transmit packet failed because some other error occurred
  78. *
  79. */
  80. esp_err_t (*transmit)(esp_eth_mac_t *mac, uint8_t *buf, uint32_t length);
  81. /**
  82. * @brief Receive packet from Ethernet MAC
  83. *
  84. * @param[in] mac: Ethernet MAC instance
  85. * @param[out] buf: packet buffer which will preserve the received frame
  86. * @param[out] length: length of the received packet
  87. *
  88. * @note Memory of buf is allocated in the Layer2, make sure it get free after process.
  89. *
  90. * @return
  91. * - ESP_OK: receive packet successfully
  92. * - ESP_ERR_INVALID_ARG: receive packet failed because of invalid argument
  93. * - ESP_FAIL: receive packet failed because some other error occurred
  94. *
  95. */
  96. esp_err_t (*receive)(esp_eth_mac_t *mac, uint8_t *buf, uint32_t *length);
  97. /**
  98. * @brief Read PHY register
  99. *
  100. * @param[in] mac: Ethernet MAC instance
  101. * @param[in] phy_addr: PHY chip address (0~31)
  102. * @param[in] phy_reg: PHY register index code
  103. * @param[out] reg_value: PHY register value
  104. *
  105. * @return
  106. * - ESP_OK: read PHY register successfully
  107. * - ESP_ERR_INVALID_ARG: read PHY register failed because of invalid argument
  108. * - ESP_ERR_INVALID_STATE: read PHY register failed because of wrong state of MAC
  109. * - ESP_FAIL: read PHY register failed because some other error occurred
  110. *
  111. */
  112. esp_err_t (*read_phy_reg)(esp_eth_mac_t *mac, uint32_t phy_addr, uint32_t phy_reg, uint32_t *reg_value);
  113. /**
  114. * @brief Write PHY register
  115. *
  116. * @param[in] mac: Ethernet MAC instance
  117. * @param[in] phy_addr: PHY chip address (0~31)
  118. * @param[in] phy_reg: PHY register index code
  119. * @param[in] reg_value: PHY register value
  120. *
  121. * @return
  122. * - ESP_OK: write PHY register successfully
  123. * - ESP_ERR_INVALID_STATE: write PHY register failed because of wrong state of MAC
  124. * - ESP_FAIL: write PHY register failed because some other error occurred
  125. *
  126. */
  127. esp_err_t (*write_phy_reg)(esp_eth_mac_t *mac, uint32_t phy_addr, uint32_t phy_reg, uint32_t reg_value);
  128. /**
  129. * @brief Set MAC address
  130. *
  131. * @param[in] mac: Ethernet MAC instance
  132. * @param[in] addr: MAC address
  133. *
  134. * @return
  135. * - ESP_OK: set MAC address successfully
  136. * - ESP_ERR_INVALID_ARG: set MAC address failed because of invalid argument
  137. * - ESP_FAIL: set MAC address failed because some other error occurred
  138. *
  139. */
  140. esp_err_t (*set_addr)(esp_eth_mac_t *mac, uint8_t *addr);
  141. /**
  142. * @brief Get MAC address
  143. *
  144. * @param[in] mac: Ethernet MAC instance
  145. * @param[out] addr: MAC address
  146. *
  147. * @return
  148. * - ESP_OK: get MAC address successfully
  149. * - ESP_ERR_INVALID_ARG: get MAC address failed because of invalid argument
  150. * - ESP_FAIL: get MAC address failed because some other error occurred
  151. *
  152. */
  153. esp_err_t (*get_addr)(esp_eth_mac_t *mac, uint8_t *addr);
  154. /**
  155. * @brief Set speed of MAC
  156. *
  157. * @param[in] ma:c Ethernet MAC instance
  158. * @param[in] speed: MAC speed
  159. *
  160. * @return
  161. * - ESP_OK: set MAC speed successfully
  162. * - ESP_ERR_INVALID_ARG: set MAC speed failed because of invalid argument
  163. * - ESP_FAIL: set MAC speed failed because some other error occurred
  164. *
  165. */
  166. esp_err_t (*set_speed)(esp_eth_mac_t *mac, eth_speed_t speed);
  167. /**
  168. * @brief Set duplex mode of MAC
  169. *
  170. * @param[in] mac: Ethernet MAC instance
  171. * @param[in] duplex: MAC duplex
  172. *
  173. * @return
  174. * - ESP_OK: set MAC duplex mode successfully
  175. * - ESP_ERR_INVALID_ARG: set MAC duplex failed because of invalid argument
  176. * - ESP_FAIL: set MAC duplex failed because some other error occurred
  177. *
  178. */
  179. esp_err_t (*set_duplex)(esp_eth_mac_t *mac, eth_duplex_t duplex);
  180. /**
  181. * @brief Set link status of MAC
  182. *
  183. * @param[in] mac: Ethernet MAC instance
  184. * @param[in] link: Link status
  185. *
  186. * @return
  187. * - ESP_OK: set link status successfully
  188. * - ESP_ERR_INVALID_ARG: set link status failed because of invalid argument
  189. * - ESP_FAIL: set link status failed because some other error occurred
  190. *
  191. */
  192. esp_err_t (*set_link)(esp_eth_mac_t *mac, eth_link_t link);
  193. /**
  194. * @brief Set promiscuous of MAC
  195. *
  196. * @param[in] mac: Ethernet MAC instance
  197. * @param[in] enable: set true to enable promiscuous mode; set false to disable promiscuous mode
  198. *
  199. * @return
  200. * - ESP_OK: set promiscuous mode successfully
  201. * - ESP_FAIL: set promiscuous mode failed because some error occurred
  202. *
  203. */
  204. esp_err_t (*set_promiscuous)(esp_eth_mac_t *mac, bool enable);
  205. /**
  206. * @brief Free memory of Ethernet MAC
  207. *
  208. * @param[in] mac: Ethernet MAC instance
  209. *
  210. * @return
  211. * - ESP_OK: free Ethernet MAC instance successfully
  212. * - ESP_FAIL: free Ethernet MAC instance failed because some error occurred
  213. *
  214. */
  215. esp_err_t (*del)(esp_eth_mac_t *mac);
  216. };
  217. /**
  218. * @brief Configuration of Ethernet MAC object
  219. *
  220. */
  221. typedef struct {
  222. uint32_t sw_reset_timeout_ms; /*!< Software reset timeout value (Unit: ms) */
  223. uint32_t rx_task_stack_size; /*!< Stack size of the receive task */
  224. uint32_t rx_task_prio; /*!< Priority of the receive task */
  225. uint32_t queue_len; /*!< Length of the transaction queue */
  226. } eth_mac_config_t;
  227. /**
  228. * @brief Default configuration for Ethernet MAC object
  229. *
  230. */
  231. #define ETH_MAC_DEFAULT_CONFIG() \
  232. { \
  233. .sw_reset_timeout_ms = 100, \
  234. .rx_task_stack_size = 4096, \
  235. .rx_task_prio = 15, \
  236. .queue_len = 100, \
  237. }
  238. /**
  239. * @brief Create ESP32 Ethernet MAC instance
  240. *
  241. * @param config: Ethernet MAC configuration
  242. *
  243. * @return
  244. * - instance: create MAC instance successfully
  245. * - NULL: create MAC instance failed because some error occurred
  246. */
  247. esp_eth_mac_t *esp_eth_mac_new_esp32(const eth_mac_config_t *config);
  248. #ifdef __cplusplus
  249. }
  250. #endif