esp_eth_phy.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // Copyright 2019-2021 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
  162. *
  163. * @param[in] phy: Ethernet PHY instance
  164. * @param[in] enable: enables or disables PHY loopback
  165. *
  166. * @return
  167. * - ESP_OK: configures PHY instance loopback function successfully
  168. * - ESP_FAIL: PHY instance loopback configuration failed because some error occurred
  169. *
  170. */
  171. esp_err_t (*loopback)(esp_eth_phy_t *phy, bool enable);
  172. /**
  173. * @brief Free memory of Ethernet PHY instance
  174. *
  175. * @param[in] phy: Ethernet PHY instance
  176. *
  177. * @return
  178. * - ESP_OK: free PHY instance successfully
  179. * - ESP_FAIL: free PHY instance failed because some error occurred
  180. *
  181. */
  182. esp_err_t (*del)(esp_eth_phy_t *phy);
  183. };
  184. /**
  185. * @brief Ethernet PHY configuration
  186. *
  187. */
  188. typedef struct {
  189. int32_t phy_addr; /*!< PHY address, set -1 to enable PHY address detection at initialization stage */
  190. uint32_t reset_timeout_ms; /*!< Reset timeout value (Unit: ms) */
  191. uint32_t autonego_timeout_ms; /*!< Auto-negotiation timeout value (Unit: ms) */
  192. int reset_gpio_num; /*!< Reset GPIO number, -1 means no hardware reset */
  193. } eth_phy_config_t;
  194. /**
  195. * @brief Default configuration for Ethernet PHY object
  196. *
  197. */
  198. #define ETH_PHY_DEFAULT_CONFIG() \
  199. { \
  200. .phy_addr = ESP_ETH_PHY_ADDR_AUTO, \
  201. .reset_timeout_ms = 100, \
  202. .autonego_timeout_ms = 4000, \
  203. .reset_gpio_num = 5, \
  204. }
  205. /**
  206. * @brief Create a PHY instance of IP101
  207. *
  208. * @param[in] config: configuration of PHY
  209. *
  210. * @return
  211. * - instance: create PHY instance successfully
  212. * - NULL: create PHY instance failed because some error occurred
  213. */
  214. esp_eth_phy_t *esp_eth_phy_new_ip101(const eth_phy_config_t *config);
  215. /**
  216. * @brief Create a PHY instance of RTL8201
  217. *
  218. * @param[in] config: configuration of PHY
  219. *
  220. * @return
  221. * - instance: create PHY instance successfully
  222. * - NULL: create PHY instance failed because some error occurred
  223. */
  224. esp_eth_phy_t *esp_eth_phy_new_rtl8201(const eth_phy_config_t *config);
  225. /**
  226. * @brief Create a PHY instance of LAN87xx
  227. *
  228. * @param[in] config: configuration of PHY
  229. *
  230. * @return
  231. * - instance: create PHY instance successfully
  232. * - NULL: create PHY instance failed because some error occurred
  233. */
  234. esp_eth_phy_t *esp_eth_phy_new_lan87xx(const eth_phy_config_t *config);
  235. /**
  236. * @brief Create a PHY instance of LAN8720
  237. *
  238. * @note For ESP-IDF backwards compatibility reasons. In all other cases, use esp_eth_phy_new_lan87xx instead.
  239. *
  240. * @param[in] config: configuration of PHY
  241. *
  242. * @return
  243. * - instance: create PHY instance successfully
  244. * - NULL: create PHY instance failed because some error occurred
  245. */
  246. static inline esp_eth_phy_t *esp_eth_phy_new_lan8720(const eth_phy_config_t *config)
  247. {
  248. return esp_eth_phy_new_lan87xx(config);
  249. }
  250. /**
  251. * @brief Create a PHY instance of DP83848
  252. *
  253. * @param[in] config: configuration of PHY
  254. *
  255. * @return
  256. * - instance: create PHY instance successfully
  257. * - NULL: create PHY instance failed because some error occurred
  258. */
  259. esp_eth_phy_t *esp_eth_phy_new_dp83848(const eth_phy_config_t *config);
  260. /**
  261. * @brief Create a PHY instance of KSZ8041
  262. *
  263. * @param[in] config: configuration of PHY
  264. *
  265. * @return
  266. * - instance: create PHY instance successfully
  267. * - NULL: create PHY instance failed because some error occurred
  268. */
  269. esp_eth_phy_t *esp_eth_phy_new_ksz8041(const eth_phy_config_t *config);
  270. /**
  271. * @brief Create a PHY instance of KSZ8081
  272. *
  273. * @param[in] config: configuration of PHY
  274. *
  275. * @return
  276. * - instance: create PHY instance successfully
  277. * - NULL: create PHY instance failed because some error occurred
  278. */
  279. esp_eth_phy_t *esp_eth_phy_new_ksz8081(const eth_phy_config_t *config);
  280. #if CONFIG_ETH_SPI_ETHERNET_DM9051
  281. /**
  282. * @brief Create a PHY instance of DM9051
  283. *
  284. * @param[in] config: configuration of PHY
  285. *
  286. * @return
  287. * - instance: create PHY instance successfully
  288. * - NULL: create PHY instance failed because some error occurred
  289. */
  290. esp_eth_phy_t *esp_eth_phy_new_dm9051(const eth_phy_config_t *config);
  291. #endif
  292. #if CONFIG_ETH_SPI_ETHERNET_W5500
  293. /**
  294. * @brief Create a PHY instance of W5500
  295. *
  296. * @param[in] config: configuration of PHY
  297. *
  298. * @return
  299. * - instance: create PHY instance successfully
  300. * - NULL: create PHY instance failed because some error occurred
  301. */
  302. esp_eth_phy_t *esp_eth_phy_new_w5500(const eth_phy_config_t *config);
  303. #endif
  304. #if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL
  305. /**
  306. * @brief Create a PHY instance of KSZ8851SNL
  307. *
  308. * @param[in] config: configuration of PHY
  309. *
  310. * @return
  311. * - instance: create PHY instance successfully
  312. * - NULL: create PHY instance failed because some error occurred
  313. */
  314. esp_eth_phy_t *esp_eth_phy_new_ksz8851snl(const eth_phy_config_t *config);
  315. #endif
  316. #ifdef __cplusplus
  317. }
  318. #endif