esp_eth_phy.h 8.1 KB

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