esp_eth_phy.h 6.9 KB

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