esp_eth_phy_802_3.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdbool.h>
  8. #include "esp_eth.h"
  9. #include "sdkconfig.h"
  10. #include "eth_phy_802_3_regs.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /**
  15. * @brief IEEE 802.3 PHY object infostructure
  16. *
  17. */
  18. typedef struct {
  19. esp_eth_phy_t parent; /*!< Parent Ethernet PHY instance */
  20. esp_eth_mediator_t *eth; /*!< Mediator of Ethernet driver */
  21. int addr; /*!< PHY address */
  22. uint32_t reset_timeout_ms; /*!< Reset timeout value (Unit: ms) */
  23. uint32_t autonego_timeout_ms; /*!< Auto-negotiation timeout value (Unit: ms) */
  24. eth_link_t link_status; /*!< Current Link status */
  25. int reset_gpio_num; /*!< Reset GPIO number, -1 means no hardware reset */
  26. } phy_802_3_t;
  27. /**
  28. * @brief Set Ethernet mediator
  29. *
  30. * @param phy_802_3 IEEE 802.3 PHY object infostructure
  31. * @param eth Ethernet mediator pointer
  32. * @return
  33. * - ESP_OK: Ethermet mediator set successfuly
  34. * - ESP_ERR_INVALID_ARG: if @c eth is @c NULL
  35. */
  36. esp_err_t esp_eth_phy_802_3_set_mediator(phy_802_3_t *phy_802_3, esp_eth_mediator_t *eth);
  37. /**
  38. * @brief Reset PHY
  39. *
  40. * @param phy_802_3 IEEE 802.3 PHY object infostructure
  41. * @return
  42. * - ESP_OK: Ethernet PHY reset successfuly
  43. * - ESP_FAIL: reset Ethernet PHY failed because some error occured
  44. */
  45. esp_err_t esp_eth_phy_802_3_reset(phy_802_3_t *phy_802_3);
  46. /**
  47. * @brief Control autonegotiation mode of Ethernet PHY
  48. *
  49. * @param phy_802_3 IEEE 802.3 PHY object infostructure
  50. * @param cmd autonegotiation command enumeration
  51. * @param[out] autonego_en_stat autonegotiation enabled flag
  52. * @return
  53. * - ESP_OK: Ethernet PHY autonegotiation configured successfuly
  54. * - ESP_FAIL: Ethernet PHY autonegotiation configuration fail because some error occured
  55. * - ESP_ERR_INVALID_ARG: invalid value of @c cmd
  56. */
  57. esp_err_t esp_eth_phy_802_3_autonego_ctrl(phy_802_3_t *phy_802_3, eth_phy_autoneg_cmd_t cmd, bool *autonego_en_stat);
  58. /**
  59. * @brief Power control of Ethernet PHY
  60. *
  61. * @param phy_802_3 IEEE 802.3 PHY object infostructure
  62. * @param enable set true to power ON Ethernet PHY; set false to power OFF Ethernet PHY
  63. * @return
  64. * - ESP_OK: Ethernet PHY power down mode set successfuly
  65. * - ESP_FAIL: Ethernet PHY power up or power down failed because some error occured
  66. */
  67. esp_err_t esp_eth_phy_802_3_pwrctl(phy_802_3_t *phy_802_3, bool enable);
  68. /**
  69. * @brief Set Ethernet PHY address
  70. *
  71. * @param phy_802_3 IEEE 802.3 PHY object infostructure
  72. * @param addr new PHY address
  73. * @return
  74. * - ESP_OK: Ethernet PHY address set
  75. */
  76. esp_err_t esp_eth_phy_802_3_set_addr(phy_802_3_t *phy_802_3, uint32_t addr);
  77. /**
  78. * @brief Get Ethernet PHY address
  79. *
  80. * @param phy_802_3 IEEE 802.3 PHY object infostructure
  81. * @param[out] addr Ethernet PHY address
  82. * @return
  83. * - ESP_OK: Ethernet PHY address read successfuly
  84. * - ESP_ERR_INVALID_ARG: @c addr pointer is @c NULL
  85. */
  86. esp_err_t esp_eth_phy_802_3_get_addr(phy_802_3_t *phy_802_3, uint32_t *addr);
  87. /**
  88. * @brief Advertise pause function ability
  89. *
  90. * @param phy_802_3 IEEE 802.3 PHY object infostructure
  91. * @param ability enable or disable pause ability
  92. * @return
  93. * - ESP_OK: pause ability set successfuly
  94. * - ESP_FAIL: Advertise pause function ability failed because some error occured
  95. */
  96. esp_err_t esp_eth_phy_802_3_advertise_pause_ability(phy_802_3_t *phy_802_3, uint32_t ability);
  97. /**
  98. * @brief Set Ethernet PHY loopback mode
  99. *
  100. * @param phy_802_3 IEEE 802.3 PHY object infostructure
  101. * @param enable set true to enable loopback; set false to disable loopback
  102. * @return
  103. * - ESP_OK: Ethernet PHY loopback mode set successfuly
  104. * - ESP_FAIL: Ethernet PHY loopback configuration failed because some error occured
  105. */
  106. esp_err_t esp_eth_phy_802_3_loopback(phy_802_3_t *phy_802_3, bool enable);
  107. /**
  108. * @brief Set Ethernet PHY speed
  109. *
  110. * @param phy_802_3 IEEE 802.3 PHY object infostructure
  111. * @param speed new speed of Ethernet PHY link
  112. * @return
  113. * - ESP_OK: Ethernet PHY speed set successfuly
  114. * - ESP_FAIL: Set Ethernet PHY speed failed because some error occured
  115. */
  116. esp_err_t esp_eth_phy_802_3_set_speed(phy_802_3_t *phy_802_3, eth_speed_t speed);
  117. /**
  118. * @brief Set Ethernet PHY duplex mode
  119. *
  120. * @param phy_802_3 IEEE 802.3 PHY object infostructure
  121. * @param duplex new duplex mode for Ethernet PHY link
  122. * @return
  123. * - ESP_OK: Ethernet PHY duplex mode set successfuly
  124. * - ESP_ERR_INVALID_STATE: unable to set duplex mode to Half if loopback is enabled
  125. * - ESP_FAIL: Set Ethernet PHY duplex mode failed because some error occured
  126. */
  127. esp_err_t esp_eth_phy_802_3_set_duplex(phy_802_3_t *phy_802_3, eth_duplex_t duplex);
  128. /**
  129. * @brief Initialize Ethernet PHY
  130. *
  131. * @param phy_802_3 IEEE 802.3 PHY object infostructure
  132. * @return
  133. * - ESP_OK: Ethernet PHY initialized successfuly
  134. */
  135. esp_err_t esp_eth_phy_802_3_init(phy_802_3_t *phy_802_3);
  136. /**
  137. * @brief Power off Eternet PHY
  138. *
  139. * @param phy_802_3 IEEE 802.3 PHY object infostructure
  140. * @return
  141. * - ESP_OK: Ethernet PHY powered off successfuly
  142. */
  143. esp_err_t esp_eth_phy_802_3_deinit(phy_802_3_t *phy_802_3);
  144. /**
  145. * @brief Delete Ethernet PHY infostructure
  146. *
  147. * @param phy_802_3 IEEE 802.3 PHY object infostructure
  148. * @return
  149. * - ESP_OK: Ethrnet PHY infostructure deleted
  150. */
  151. esp_err_t esp_eth_phy_802_3_del(phy_802_3_t *phy_802_3);
  152. /**
  153. * @brief Performs hardware reset with specific reset pin assertion time
  154. *
  155. * @param phy_802_3 IEEE 802.3 PHY object infostructure
  156. * @param reset_assert_us Hardware reset pin assertion time
  157. * @return
  158. * - ESP_OK: reset Ethernet PHY successfully
  159. */
  160. esp_err_t esp_eth_phy_802_3_reset_hw(phy_802_3_t *phy_802_3, uint32_t reset_assert_us);
  161. /**
  162. * @brief Detect PHY address
  163. *
  164. * @param eth Mediator of Ethernet driver
  165. * @param[out] detected_addr: a valid address after detection
  166. * @return
  167. * - ESP_OK: detect phy address successfully
  168. * - ESP_ERR_INVALID_ARG: invalid parameter
  169. * - ESP_ERR_NOT_FOUND: can't detect any PHY device
  170. * - ESP_FAIL: detect phy address failed because some error occurred
  171. */
  172. esp_err_t esp_eth_phy_802_3_detect_phy_addr(esp_eth_mediator_t *eth, int *detected_addr);
  173. /**
  174. * @brief Performs basic PHY chip initialization
  175. *
  176. * @note It should be called as the first function in PHY specific driver instance
  177. *
  178. * @param phy_802_3 IEEE 802.3 PHY object infostructure
  179. * @return
  180. * - ESP_OK: initialized Ethernet PHY successfully
  181. * - ESP_FAIL: initialization of Ethernet PHY failed because some error occurred
  182. * - ESP_ERR_INVALID_ARG: invalid argument
  183. * - ESP_ERR_NOT_FOUND: PHY device not detected
  184. * - ESP_ERR_TIMEOUT: MII Management read/write operation timeout
  185. * - ESP_ERR_INVALID_STATE: PHY is in invalid state to perform requested operation
  186. */
  187. esp_err_t esp_eth_phy_802_3_basic_phy_init(phy_802_3_t *phy_802_3);
  188. /**
  189. * @brief Performs basic PHY chip de-initialization
  190. *
  191. * @note It should be called as the last function in PHY specific driver instance
  192. *
  193. * @param phy_802_3 IEEE 802.3 PHY object infostructure
  194. * @return
  195. * - ESP_OK: de-initialized Ethernet PHY successfully
  196. * - ESP_FAIL: de-initialization of Ethernet PHY failed because some error occurred
  197. * - ESP_ERR_TIMEOUT: MII Management read/write operation timeout
  198. * - ESP_ERR_INVALID_STATE: PHY is in invalid state to perform requested operation
  199. */
  200. esp_err_t esp_eth_phy_802_3_basic_phy_deinit(phy_802_3_t *phy_802_3);
  201. /**
  202. * @brief Reads raw content of OUI field
  203. *
  204. * @param phy_802_3 IEEE 802.3 PHY object infostructure
  205. * @param[out] oui OUI value
  206. * @return
  207. * - ESP_OK: OUI field read successfully
  208. * - ESP_FAIL: OUI field read failed because some error occurred
  209. * - ESP_ERR_INVALID_ARG: invalid @c oui argument
  210. * - ESP_ERR_TIMEOUT: MII Management read/write operation timeout
  211. * - ESP_ERR_INVALID_STATE: PHY is in invalid state to perform requested operation
  212. */
  213. esp_err_t esp_eth_phy_802_3_read_oui(phy_802_3_t *phy_802_3, uint32_t *oui);
  214. /**
  215. * @brief Reads manufacturer’s model and revision number
  216. *
  217. * @param phy_802_3 IEEE 802.3 PHY object infostructure
  218. * @param[out] model Manufacturer’s model number (can be NULL when not required)
  219. * @param[out] rev Manufacturer’s revision number (can be NULL when not required)
  220. * @return
  221. * - ESP_OK: Manufacturer’s info read successfully
  222. * - ESP_FAIL: Manufacturer’s info read failed because some error occurred
  223. * - ESP_ERR_TIMEOUT: MII Management read/write operation timeout
  224. * - ESP_ERR_INVALID_STATE: PHY is in invalid state to perform requested operation
  225. */
  226. esp_err_t esp_eth_phy_802_3_read_manufac_info(phy_802_3_t *phy_802_3, uint8_t *model, uint8_t *rev);
  227. /**
  228. * @brief Returns address to parent IEEE 802.3 PHY object infostructure
  229. *
  230. * @param phy Ethernet PHY instance
  231. * @return phy_802_3_t*
  232. * - address to parent IEEE 802.3 PHY object infostructure
  233. */
  234. inline __attribute__((always_inline)) phy_802_3_t *esp_eth_phy_into_phy_802_3(esp_eth_phy_t *phy)
  235. {
  236. return __containerof(phy, phy_802_3_t, parent);
  237. }
  238. /**
  239. * @brief Initializes configuration of parent IEEE 802.3 PHY object infostructure
  240. *
  241. * @param phy_802_3 Address to IEEE 802.3 PHY object infostructure
  242. * @param config Configuration of the IEEE 802.3 PHY object
  243. * @return
  244. * - ESP_OK: configuration initialized successfully
  245. * - ESP_ERR_INVALID_ARG: invalid @c config argument
  246. */
  247. esp_err_t esp_eth_phy_802_3_obj_config_init(phy_802_3_t *phy_802_3, const eth_phy_config_t *config);
  248. #ifdef __cplusplus
  249. }
  250. #endif