esp_eth_phy.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2021 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_com.h"
  9. #include "sdkconfig.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. #define ESP_ETH_PHY_ADDR_AUTO (-1)
  14. /**
  15. * @brief Ethernet PHY
  16. *
  17. */
  18. typedef struct esp_eth_phy_s esp_eth_phy_t;
  19. /**
  20. * @brief Ethernet PHY
  21. *
  22. */
  23. struct esp_eth_phy_s {
  24. /**
  25. * @brief Set mediator for PHY
  26. *
  27. * @param[in] phy: Ethernet PHY instance
  28. * @param[in] mediator: mediator of Ethernet driver
  29. *
  30. * @return
  31. * - ESP_OK: set mediator for Ethernet PHY instance successfully
  32. * - ESP_ERR_INVALID_ARG: set mediator for Ethernet PHY instance failed because of some invalid arguments
  33. *
  34. */
  35. esp_err_t (*set_mediator)(esp_eth_phy_t *phy, esp_eth_mediator_t *mediator);
  36. /**
  37. * @brief Software Reset Ethernet PHY
  38. *
  39. * @param[in] phy: Ethernet PHY instance
  40. *
  41. * @return
  42. * - ESP_OK: reset Ethernet PHY successfully
  43. * - ESP_FAIL: reset Ethernet PHY failed because some error occurred
  44. *
  45. */
  46. esp_err_t (*reset)(esp_eth_phy_t *phy);
  47. /**
  48. * @brief Hardware Reset Ethernet PHY
  49. *
  50. * @note Hardware reset is mostly done by pull down and up PHY's nRST pin
  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_hw)(esp_eth_phy_t *phy);
  60. /**
  61. * @brief Initialize Ethernet PHY
  62. *
  63. * @param[in] phy: Ethernet PHY instance
  64. *
  65. * @return
  66. * - ESP_OK: initialize Ethernet PHY successfully
  67. * - ESP_FAIL: initialize Ethernet PHY failed because some error occurred
  68. *
  69. */
  70. esp_err_t (*init)(esp_eth_phy_t *phy);
  71. /**
  72. * @brief Deinitialize Ethernet PHY
  73. *
  74. * @param[in] phyL Ethernet PHY instance
  75. *
  76. * @return
  77. * - ESP_OK: deinitialize Ethernet PHY successfully
  78. * - ESP_FAIL: deinitialize Ethernet PHY failed because some error occurred
  79. *
  80. */
  81. esp_err_t (*deinit)(esp_eth_phy_t *phy);
  82. /**
  83. * @brief Start auto negotiation
  84. *
  85. * @param[in] phy: Ethernet PHY instance
  86. *
  87. * @return
  88. * - ESP_OK: restart auto negotiation successfully
  89. * - ESP_FAIL: restart auto negotiation failed because some error occurred
  90. *
  91. */
  92. esp_err_t (*negotiate)(esp_eth_phy_t *phy);
  93. /**
  94. * @brief Get Ethernet PHY link status
  95. *
  96. * @param[in] phy: Ethernet PHY instance
  97. *
  98. * @return
  99. * - ESP_OK: get Ethernet PHY link status successfully
  100. * - ESP_FAIL: get Ethernet PHY link status failed because some error occurred
  101. *
  102. */
  103. esp_err_t (*get_link)(esp_eth_phy_t *phy);
  104. /**
  105. * @brief Power control of Ethernet PHY
  106. *
  107. * @param[in] phy: Ethernet PHY instance
  108. * @param[in] enable: set true to power on Ethernet PHY; ser false to power off Ethernet PHY
  109. *
  110. * @return
  111. * - ESP_OK: control Ethernet PHY power successfully
  112. * - ESP_FAIL: control Ethernet PHY power failed because some error occurred
  113. *
  114. */
  115. esp_err_t (*pwrctl)(esp_eth_phy_t *phy, bool enable);
  116. /**
  117. * @brief Set PHY chip address
  118. *
  119. * @param[in] phy: Ethernet PHY instance
  120. * @param[in] addr: PHY chip address
  121. *
  122. * @return
  123. * - ESP_OK: set Ethernet PHY address successfully
  124. * - ESP_FAIL: set Ethernet PHY address failed because some error occurred
  125. *
  126. */
  127. esp_err_t (*set_addr)(esp_eth_phy_t *phy, uint32_t addr);
  128. /**
  129. * @brief Get PHY chip address
  130. *
  131. * @param[in] phy: Ethernet PHY instance
  132. * @param[out] addr: PHY chip address
  133. *
  134. * @return
  135. * - ESP_OK: get Ethernet PHY address successfully
  136. * - ESP_ERR_INVALID_ARG: get Ethernet PHY address failed because of invalid argument
  137. *
  138. */
  139. esp_err_t (*get_addr)(esp_eth_phy_t *phy, uint32_t *addr);
  140. /**
  141. * @brief Advertise pause function supported by MAC layer
  142. *
  143. * @param[in] phy: Ethernet PHY instance
  144. * @param[out] addr: Pause ability
  145. *
  146. * @return
  147. * - ESP_OK: Advertise pause ability successfully
  148. * - ESP_ERR_INVALID_ARG: Advertise pause ability failed because of invalid argument
  149. *
  150. */
  151. esp_err_t (*advertise_pause_ability)(esp_eth_phy_t *phy, uint32_t ability);
  152. /**
  153. * @brief
  154. *
  155. * @param[in] phy: Ethernet PHY instance
  156. * @param[in] enable: enables or disables PHY loopback
  157. *
  158. * @return
  159. * - ESP_OK: configures PHY instance loopback function successfully
  160. * - ESP_FAIL: PHY instance loopback configuration failed because some error occurred
  161. *
  162. */
  163. esp_err_t (*loopback)(esp_eth_phy_t *phy, bool enable);
  164. /**
  165. * @brief Free memory of Ethernet PHY instance
  166. *
  167. * @param[in] phy: Ethernet PHY instance
  168. *
  169. * @return
  170. * - ESP_OK: free PHY instance successfully
  171. * - ESP_FAIL: free PHY instance failed because some error occurred
  172. *
  173. */
  174. esp_err_t (*del)(esp_eth_phy_t *phy);
  175. };
  176. /**
  177. * @brief Ethernet PHY configuration
  178. *
  179. */
  180. typedef struct {
  181. int32_t phy_addr; /*!< PHY address, set -1 to enable PHY address detection at initialization stage */
  182. uint32_t reset_timeout_ms; /*!< Reset timeout value (Unit: ms) */
  183. uint32_t autonego_timeout_ms; /*!< Auto-negotiation timeout value (Unit: ms) */
  184. int reset_gpio_num; /*!< Reset GPIO number, -1 means no hardware reset */
  185. } eth_phy_config_t;
  186. /**
  187. * @brief Default configuration for Ethernet PHY object
  188. *
  189. */
  190. #define ETH_PHY_DEFAULT_CONFIG() \
  191. { \
  192. .phy_addr = ESP_ETH_PHY_ADDR_AUTO, \
  193. .reset_timeout_ms = 100, \
  194. .autonego_timeout_ms = 4000, \
  195. .reset_gpio_num = 5, \
  196. }
  197. /**
  198. * @brief Create a PHY instance of IP101
  199. *
  200. * @param[in] config: configuration of PHY
  201. *
  202. * @return
  203. * - instance: create PHY instance successfully
  204. * - NULL: create PHY instance failed because some error occurred
  205. */
  206. esp_eth_phy_t *esp_eth_phy_new_ip101(const eth_phy_config_t *config);
  207. /**
  208. * @brief Create a PHY instance of RTL8201
  209. *
  210. * @param[in] config: configuration of PHY
  211. *
  212. * @return
  213. * - instance: create PHY instance successfully
  214. * - NULL: create PHY instance failed because some error occurred
  215. */
  216. esp_eth_phy_t *esp_eth_phy_new_rtl8201(const eth_phy_config_t *config);
  217. /**
  218. * @brief Create a PHY instance of LAN87xx
  219. *
  220. * @param[in] config: configuration of PHY
  221. *
  222. * @return
  223. * - instance: create PHY instance successfully
  224. * - NULL: create PHY instance failed because some error occurred
  225. */
  226. esp_eth_phy_t *esp_eth_phy_new_lan87xx(const eth_phy_config_t *config);
  227. /**
  228. * @brief Create a PHY instance of LAN8720
  229. *
  230. * @note For ESP-IDF backwards compatibility reasons. In all other cases, use esp_eth_phy_new_lan87xx instead.
  231. *
  232. * @param[in] config: configuration of PHY
  233. *
  234. * @return
  235. * - instance: create PHY instance successfully
  236. * - NULL: create PHY instance failed because some error occurred
  237. */
  238. static inline esp_eth_phy_t *esp_eth_phy_new_lan8720(const eth_phy_config_t *config)
  239. {
  240. return esp_eth_phy_new_lan87xx(config);
  241. }
  242. /**
  243. * @brief Create a PHY instance of DP83848
  244. *
  245. * @param[in] config: configuration of PHY
  246. *
  247. * @return
  248. * - instance: create PHY instance successfully
  249. * - NULL: create PHY instance failed because some error occurred
  250. */
  251. esp_eth_phy_t *esp_eth_phy_new_dp83848(const eth_phy_config_t *config);
  252. /**
  253. * @brief Create a PHY instance of KSZ8041
  254. *
  255. * @param[in] config: configuration of PHY
  256. *
  257. * @return
  258. * - instance: create PHY instance successfully
  259. * - NULL: create PHY instance failed because some error occurred
  260. */
  261. esp_eth_phy_t *esp_eth_phy_new_ksz8041(const eth_phy_config_t *config);
  262. /**
  263. * @brief Create a PHY instance of KSZ8081
  264. *
  265. * @param[in] config: configuration of PHY
  266. *
  267. * @return
  268. * - instance: create PHY instance successfully
  269. * - NULL: create PHY instance failed because some error occurred
  270. */
  271. esp_eth_phy_t *esp_eth_phy_new_ksz8081(const eth_phy_config_t *config);
  272. /**
  273. * @brief Create a PHY instance of KSZ8091
  274. *
  275. * @param[in] config: configuration of PHY
  276. *
  277. * @return
  278. * - instance: create PHY instance successfully
  279. * - NULL: create PHY instance failed because some error occurred
  280. */
  281. static inline esp_eth_phy_t *esp_eth_phy_new_ksz8091(const eth_phy_config_t *config)
  282. {
  283. return esp_eth_phy_new_ksz8081(config);
  284. }
  285. #if CONFIG_ETH_SPI_ETHERNET_DM9051
  286. /**
  287. * @brief Create a PHY instance of DM9051
  288. *
  289. * @param[in] config: configuration of PHY
  290. *
  291. * @return
  292. * - instance: create PHY instance successfully
  293. * - NULL: create PHY instance failed because some error occurred
  294. */
  295. esp_eth_phy_t *esp_eth_phy_new_dm9051(const eth_phy_config_t *config);
  296. #endif
  297. #if CONFIG_ETH_SPI_ETHERNET_W5500
  298. /**
  299. * @brief Create a PHY instance of W5500
  300. *
  301. * @param[in] config: configuration of PHY
  302. *
  303. * @return
  304. * - instance: create PHY instance successfully
  305. * - NULL: create PHY instance failed because some error occurred
  306. */
  307. esp_eth_phy_t *esp_eth_phy_new_w5500(const eth_phy_config_t *config);
  308. #endif
  309. #if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL
  310. /**
  311. * @brief Create a PHY instance of KSZ8851SNL
  312. *
  313. * @param[in] config: configuration of PHY
  314. *
  315. * @return
  316. * - instance: create PHY instance successfully
  317. * - NULL: create PHY instance failed because some error occurred
  318. */
  319. esp_eth_phy_t *esp_eth_phy_new_ksz8851snl(const eth_phy_config_t *config);
  320. #endif
  321. #ifdef __cplusplus
  322. }
  323. #endif