esp_eth_phy.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. /**
  23. * @brief Ethernet PHY
  24. *
  25. */
  26. typedef struct esp_eth_phy_s esp_eth_phy_t;
  27. /**
  28. * @brief Ethernet PHY
  29. *
  30. */
  31. struct esp_eth_phy_s {
  32. /**
  33. * @brief Reference count of PHY instance
  34. *
  35. */
  36. atomic_int ref_count;
  37. /**
  38. * @brief Set mediator for PHY
  39. *
  40. * @param[in] phy: Ethernet PHY instance
  41. * @param[in] mediator: mediator of Ethernet driver
  42. *
  43. * @return
  44. * - ESP_OK: set mediator for Ethernet PHY instance successfully
  45. * - ESP_ERR_INVALID_ARG: set mediator for Ethernet PHY instance failed because of some invalid arguments
  46. *
  47. */
  48. esp_err_t (*set_mediator)(esp_eth_phy_t *phy, esp_eth_mediator_t *mediator);
  49. /**
  50. * @brief Software Reset Ethernet PHY
  51. *
  52. * @param[in] phy: Ethernet PHY instance
  53. *
  54. * @return
  55. * - ESP_OK: reset Ethernet PHY successfully
  56. * - ESP_FAIL: reset Ethernet PHY failed because some error occurred
  57. *
  58. */
  59. esp_err_t (*reset)(esp_eth_phy_t *phy);
  60. /**
  61. * @brief Hardware Reset Ethernet PHY
  62. *
  63. * @note Hardware reset is mostly done by pull down and up PHY's nRST pin
  64. *
  65. * @param[in] phy: Ethernet PHY instance
  66. *
  67. * @return
  68. * - ESP_OK: reset Ethernet PHY successfully
  69. * - ESP_FAIL: reset Ethernet PHY failed because some error occurred
  70. *
  71. */
  72. esp_err_t (*reset_hw)(esp_eth_phy_t *phy);
  73. /**
  74. * @brief Initialize Ethernet PHY
  75. *
  76. * @param[in] phy: Ethernet PHY instance
  77. *
  78. * @return
  79. * - ESP_OK: initialize Ethernet PHY successfully
  80. * - ESP_FAIL: initialize Ethernet PHY failed because some error occurred
  81. *
  82. */
  83. esp_err_t (*init)(esp_eth_phy_t *phy);
  84. /**
  85. * @brief Deinitialize Ethernet PHY
  86. *
  87. * @param[in] phyL Ethernet PHY instance
  88. *
  89. * @return
  90. * - ESP_OK: deinitialize Ethernet PHY successfully
  91. * - ESP_FAIL: deinitialize Ethernet PHY failed because some error occurred
  92. *
  93. */
  94. esp_err_t (*deinit)(esp_eth_phy_t *phy);
  95. /**
  96. * @brief Start auto negotiation
  97. *
  98. * @param[in] phy: Ethernet PHY instance
  99. *
  100. * @return
  101. * - ESP_OK: restart auto negotiation successfully
  102. * - ESP_FAIL: restart auto negotiation failed because some error occurred
  103. *
  104. */
  105. esp_err_t (*negotiate)(esp_eth_phy_t *phy);
  106. /**
  107. * @brief Get Ethernet PHY link status
  108. *
  109. * @param[in] phy: Ethernet PHY instance
  110. *
  111. * @return
  112. * - ESP_OK: get Ethernet PHY link status successfully
  113. * - ESP_FAIL: get Ethernet PHY link status failed because some error occurred
  114. *
  115. */
  116. esp_err_t (*get_link)(esp_eth_phy_t *phy);
  117. /**
  118. * @brief Power control of Ethernet PHY
  119. *
  120. * @param[in] phy: Ethernet PHY instance
  121. * @param[in] enable: set true to power on Ethernet PHY; ser false to power off Ethernet PHY
  122. *
  123. * @return
  124. * - ESP_OK: control Ethernet PHY power successfully
  125. * - ESP_FAIL: control Ethernet PHY power failed because some error occurred
  126. *
  127. */
  128. esp_err_t (*pwrctl)(esp_eth_phy_t *phy, bool enable);
  129. /**
  130. * @brief Set PHY chip address
  131. *
  132. * @param[in] phy: Ethernet PHY instance
  133. * @param[in] addr: PHY chip address
  134. *
  135. * @return
  136. * - ESP_OK: set Ethernet PHY address successfully
  137. * - ESP_FAIL: set Ethernet PHY address failed because some error occurred
  138. *
  139. */
  140. esp_err_t (*set_addr)(esp_eth_phy_t *phy, uint32_t addr);
  141. /**
  142. * @brief Get PHY chip address
  143. *
  144. * @param[in] phy: Ethernet PHY instance
  145. * @param[out] addr: PHY chip address
  146. *
  147. * @return
  148. * - ESP_OK: get Ethernet PHY address successfully
  149. * - ESP_ERR_INVALID_ARG: get Ethernet PHY address failed because of invalid argument
  150. *
  151. */
  152. esp_err_t (*get_addr)(esp_eth_phy_t *phy, uint32_t *addr);
  153. /**
  154. * @brief Free memory of Ethernet PHY instance
  155. *
  156. * @param[in] phy: Ethernet PHY instance
  157. *
  158. * @return
  159. * - ESP_OK: free PHY instance successfully
  160. * - ESP_FAIL: free PHY instance failed because some error occurred
  161. *
  162. */
  163. esp_err_t (*del)(esp_eth_phy_t *phy);
  164. };
  165. /**
  166. * @brief Ethernet PHY configuration
  167. *
  168. */
  169. typedef struct {
  170. uint32_t phy_addr; /*!< PHY address */
  171. uint32_t reset_timeout_ms; /*!< Reset timeout value (Unit: ms) */
  172. uint32_t autonego_timeout_ms; /*!< Auto-negotiation timeout value (Unit: ms) */
  173. int reset_gpio_num; /*!< Reset GPIO number, -1 means no hardware reset */
  174. } eth_phy_config_t;
  175. /**
  176. * @brief Default configuration for Ethernet PHY object
  177. *
  178. */
  179. #define ETH_PHY_DEFAULT_CONFIG() \
  180. { \
  181. .phy_addr = 1, \
  182. .reset_timeout_ms = 100, \
  183. .autonego_timeout_ms = 4000, \
  184. .reset_gpio_num = 5, \
  185. }
  186. /**
  187. * @brief Create a PHY instance of IP101
  188. *
  189. * @param[in] config: configuration of PHY
  190. *
  191. * @return
  192. * - instance: create PHY instance successfully
  193. * - NULL: create PHY instance failed because some error occurred
  194. */
  195. esp_eth_phy_t *esp_eth_phy_new_ip101(const eth_phy_config_t *config);
  196. /**
  197. * @brief Create a PHY instance of RTL8201
  198. *
  199. * @param[in] config: configuration of PHY
  200. *
  201. * @return
  202. * - instance: create PHY instance successfully
  203. * - NULL: create PHY instance failed because some error occurred
  204. */
  205. esp_eth_phy_t *esp_eth_phy_new_rtl8201(const eth_phy_config_t *config);
  206. /**
  207. * @brief Create a PHY instance of LAN8720
  208. *
  209. * @param[in] config: configuration of PHY
  210. *
  211. * @return
  212. * - instance: create PHY instance successfully
  213. * - NULL: create PHY instance failed because some error occurred
  214. */
  215. esp_eth_phy_t *esp_eth_phy_new_lan8720(const eth_phy_config_t *config);
  216. /**
  217. * @brief Create a PHY instance of DP83848
  218. *
  219. * @param[in] config: configuration of PHY
  220. *
  221. * @return
  222. * - instance: create PHY instance successfully
  223. * - NULL: create PHY instance failed because some error occurred
  224. */
  225. esp_eth_phy_t *esp_eth_phy_new_dp83848(const eth_phy_config_t *config);
  226. #if CONFIG_ETH_SPI_ETHERNET_DM9051
  227. /**
  228. * @brief Create a PHY instance of DM9051
  229. *
  230. * @param[in] config: configuration of PHY
  231. *
  232. * @return
  233. * - instance: create PHY instance successfully
  234. * - NULL: create PHY instance failed because some error occurred
  235. */
  236. esp_eth_phy_t *esp_eth_phy_new_dm9051(const eth_phy_config_t *config);
  237. #endif
  238. #ifdef __cplusplus
  239. }
  240. #endif