esp_eth_phy.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 Auto-negotiation controll commands
  16. *
  17. */
  18. typedef enum {
  19. ESP_ETH_PHY_AUTONEGO_RESTART,
  20. ESP_ETH_PHY_AUTONEGO_EN,
  21. ESP_ETH_PHY_AUTONEGO_DIS,
  22. ESP_ETH_PHY_AUTONEGO_G_STAT,
  23. } eth_phy_autoneg_cmd_t;
  24. /**
  25. * @brief Ethernet PHY
  26. *
  27. */
  28. typedef struct esp_eth_phy_s esp_eth_phy_t;
  29. /**
  30. * @brief Ethernet PHY
  31. *
  32. */
  33. struct esp_eth_phy_s {
  34. /**
  35. * @brief Set mediator for PHY
  36. *
  37. * @param[in] phy: Ethernet PHY instance
  38. * @param[in] mediator: mediator of Ethernet driver
  39. *
  40. * @return
  41. * - ESP_OK: set mediator for Ethernet PHY instance successfully
  42. * - ESP_ERR_INVALID_ARG: set mediator for Ethernet PHY instance failed because of some invalid arguments
  43. *
  44. */
  45. esp_err_t (*set_mediator)(esp_eth_phy_t *phy, esp_eth_mediator_t *mediator);
  46. /**
  47. * @brief Software Reset Ethernet PHY
  48. *
  49. * @param[in] phy: Ethernet PHY instance
  50. *
  51. * @return
  52. * - ESP_OK: reset Ethernet PHY successfully
  53. * - ESP_FAIL: reset Ethernet PHY failed because some error occurred
  54. *
  55. */
  56. esp_err_t (*reset)(esp_eth_phy_t *phy);
  57. /**
  58. * @brief Hardware Reset Ethernet PHY
  59. *
  60. * @note Hardware reset is mostly done by pull down and up PHY's nRST pin
  61. *
  62. * @param[in] phy: Ethernet PHY instance
  63. *
  64. * @return
  65. * - ESP_OK: reset Ethernet PHY successfully
  66. * - ESP_FAIL: reset Ethernet PHY failed because some error occurred
  67. *
  68. */
  69. esp_err_t (*reset_hw)(esp_eth_phy_t *phy);
  70. /**
  71. * @brief Initialize Ethernet PHY
  72. *
  73. * @param[in] phy: Ethernet PHY instance
  74. *
  75. * @return
  76. * - ESP_OK: initialize Ethernet PHY successfully
  77. * - ESP_FAIL: initialize Ethernet PHY failed because some error occurred
  78. *
  79. */
  80. esp_err_t (*init)(esp_eth_phy_t *phy);
  81. /**
  82. * @brief Deinitialize Ethernet PHY
  83. *
  84. * @param[in] phy: Ethernet PHY instance
  85. *
  86. * @return
  87. * - ESP_OK: deinitialize Ethernet PHY successfully
  88. * - ESP_FAIL: deinitialize Ethernet PHY failed because some error occurred
  89. *
  90. */
  91. esp_err_t (*deinit)(esp_eth_phy_t *phy);
  92. /**
  93. * @brief Configure auto negotiation
  94. *
  95. * @param[in] phy: Ethernet PHY instance
  96. * @param[in] cmd: Configuration command, it is possible to Enable (restart), Disable or get current status
  97. * of PHY auto negotiation
  98. * @param[out] autonego_en_stat: Address where to store current status of auto negotiation configuration
  99. *
  100. * @return
  101. * - ESP_OK: restart auto negotiation successfully
  102. * - ESP_FAIL: restart auto negotiation failed because some error occurred
  103. * - ESP_ERR_INVALID_ARG: invalid command
  104. *
  105. */
  106. esp_err_t (*autonego_ctrl)(esp_eth_phy_t *phy, eth_phy_autoneg_cmd_t cmd, bool *autonego_en_stat);
  107. /**
  108. * @brief Get Ethernet PHY link status
  109. *
  110. * @param[in] phy: Ethernet PHY instance
  111. *
  112. * @return
  113. * - ESP_OK: get Ethernet PHY link status successfully
  114. * - ESP_FAIL: get Ethernet PHY link status failed because some error occurred
  115. *
  116. */
  117. esp_err_t (*get_link)(esp_eth_phy_t *phy);
  118. /**
  119. * @brief Power control of Ethernet PHY
  120. *
  121. * @param[in] phy: Ethernet PHY instance
  122. * @param[in] enable: set true to power on Ethernet PHY; ser false to power off Ethernet PHY
  123. *
  124. * @return
  125. * - ESP_OK: control Ethernet PHY power successfully
  126. * - ESP_FAIL: control Ethernet PHY power failed because some error occurred
  127. *
  128. */
  129. esp_err_t (*pwrctl)(esp_eth_phy_t *phy, bool enable);
  130. /**
  131. * @brief Set PHY chip address
  132. *
  133. * @param[in] phy: Ethernet PHY instance
  134. * @param[in] addr: PHY chip address
  135. *
  136. * @return
  137. * - ESP_OK: set Ethernet PHY address successfully
  138. * - ESP_FAIL: set Ethernet PHY address failed because some error occurred
  139. *
  140. */
  141. esp_err_t (*set_addr)(esp_eth_phy_t *phy, uint32_t addr);
  142. /**
  143. * @brief Get PHY chip address
  144. *
  145. * @param[in] phy: Ethernet PHY instance
  146. * @param[out] addr: PHY chip address
  147. *
  148. * @return
  149. * - ESP_OK: get Ethernet PHY address successfully
  150. * - ESP_ERR_INVALID_ARG: get Ethernet PHY address failed because of invalid argument
  151. *
  152. */
  153. esp_err_t (*get_addr)(esp_eth_phy_t *phy, uint32_t *addr);
  154. /**
  155. * @brief Advertise pause function supported by MAC layer
  156. *
  157. * @param[in] phy: Ethernet PHY instance
  158. * @param[out] addr: Pause ability
  159. *
  160. * @return
  161. * - ESP_OK: Advertise pause ability successfully
  162. * - ESP_ERR_INVALID_ARG: Advertise pause ability failed because of invalid argument
  163. *
  164. */
  165. esp_err_t (*advertise_pause_ability)(esp_eth_phy_t *phy, uint32_t ability);
  166. /**
  167. * @brief Sets the PHY to loopback mode
  168. *
  169. * @param[in] phy: Ethernet PHY instance
  170. * @param[in] enable: enables or disables PHY loopback
  171. *
  172. * @return
  173. * - ESP_OK: PHY instance loopback mode has been configured successfully
  174. * - ESP_FAIL: PHY instance loopback configuration failed because some error occurred
  175. *
  176. */
  177. esp_err_t (*loopback)(esp_eth_phy_t *phy, bool enable);
  178. /**
  179. * @brief Sets PHY speed mode
  180. *
  181. * @note Autonegotiation feature needs to be disabled prior to calling this function for the new
  182. * setting to be applied
  183. *
  184. * @param[in] phy: Ethernet PHY instance
  185. * @param[in] speed: Speed mode to be set
  186. *
  187. * @return
  188. * - ESP_OK: PHY instance speed mode has been configured successfully
  189. * - ESP_FAIL: PHY instance speed mode configuration failed because some error occurred
  190. *
  191. */
  192. esp_err_t (*set_speed)(esp_eth_phy_t *phy, eth_speed_t speed);
  193. /**
  194. * @brief Sets PHY duplex mode
  195. *
  196. * @note Autonegotiation feature needs to be disabled prior to calling this function for the new
  197. * setting to be applied
  198. *
  199. * @param[in] phy: Ethernet PHY instance
  200. * @param[in] duplex: Duplex mode to be set
  201. *
  202. * @return
  203. * - ESP_OK: PHY instance duplex mode has been configured successfully
  204. * - ESP_FAIL: PHY instance duplex mode configuration failed because some error occurred
  205. *
  206. */
  207. esp_err_t (*set_duplex)(esp_eth_phy_t *phy, eth_duplex_t duplex);
  208. /**
  209. * @brief Free memory of Ethernet PHY instance
  210. *
  211. * @param[in] phy: Ethernet PHY instance
  212. *
  213. * @return
  214. * - ESP_OK: free PHY instance successfully
  215. * - ESP_FAIL: free PHY instance failed because some error occurred
  216. *
  217. */
  218. esp_err_t (*del)(esp_eth_phy_t *phy);
  219. };
  220. /**
  221. * @brief Ethernet PHY configuration
  222. *
  223. */
  224. typedef struct {
  225. int32_t phy_addr; /*!< PHY address, set -1 to enable PHY address detection at initialization stage */
  226. uint32_t reset_timeout_ms; /*!< Reset timeout value (Unit: ms) */
  227. uint32_t autonego_timeout_ms; /*!< Auto-negotiation timeout value (Unit: ms) */
  228. int reset_gpio_num; /*!< Reset GPIO number, -1 means no hardware reset */
  229. } eth_phy_config_t;
  230. /**
  231. * @brief Default configuration for Ethernet PHY object
  232. *
  233. */
  234. #define ETH_PHY_DEFAULT_CONFIG() \
  235. { \
  236. .phy_addr = ESP_ETH_PHY_ADDR_AUTO, \
  237. .reset_timeout_ms = 100, \
  238. .autonego_timeout_ms = 4000, \
  239. .reset_gpio_num = 5, \
  240. }
  241. /**
  242. * @brief Create a PHY instance of IP101
  243. *
  244. * @param[in] config: configuration of PHY
  245. *
  246. * @return
  247. * - instance: create PHY instance successfully
  248. * - NULL: create PHY instance failed because some error occurred
  249. */
  250. esp_eth_phy_t *esp_eth_phy_new_ip101(const eth_phy_config_t *config);
  251. /**
  252. * @brief Create a PHY instance of RTL8201
  253. *
  254. * @param[in] config: configuration of PHY
  255. *
  256. * @return
  257. * - instance: create PHY instance successfully
  258. * - NULL: create PHY instance failed because some error occurred
  259. */
  260. esp_eth_phy_t *esp_eth_phy_new_rtl8201(const eth_phy_config_t *config);
  261. /**
  262. * @brief Create a PHY instance of LAN87xx
  263. *
  264. * @param[in] config: configuration of PHY
  265. *
  266. * @return
  267. * - instance: create PHY instance successfully
  268. * - NULL: create PHY instance failed because some error occurred
  269. */
  270. esp_eth_phy_t *esp_eth_phy_new_lan87xx(const eth_phy_config_t *config);
  271. /**
  272. * @brief Create a PHY instance of DP83848
  273. *
  274. * @param[in] config: configuration of PHY
  275. *
  276. * @return
  277. * - instance: create PHY instance successfully
  278. * - NULL: create PHY instance failed because some error occurred
  279. */
  280. esp_eth_phy_t *esp_eth_phy_new_dp83848(const eth_phy_config_t *config);
  281. /**
  282. * @brief Create a PHY instance of KSZ80xx
  283. *
  284. * The phy model from the KSZ80xx series is detected automatically. If the driver
  285. * is unable to detect a supported model, \c NULL is returned.
  286. *
  287. * Currently, the following models are supported:
  288. * KSZ8001, KSZ8021, KSZ8031, KSZ8041, KSZ8051, KSZ8061, KSZ8081, KSZ8091
  289. *
  290. * @param[in] config: configuration of PHY
  291. *
  292. * @return
  293. * - instance: create PHY instance successfully
  294. * - NULL: create PHY instance failed because some error occurred
  295. */
  296. esp_eth_phy_t *esp_eth_phy_new_ksz80xx(const eth_phy_config_t *config);
  297. #if CONFIG_ETH_SPI_ETHERNET_DM9051
  298. /**
  299. * @brief Create a PHY instance of DM9051
  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_dm9051(const eth_phy_config_t *config);
  308. #endif
  309. #if CONFIG_ETH_SPI_ETHERNET_W5500
  310. /**
  311. * @brief Create a PHY instance of W5500
  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_w5500(const eth_phy_config_t *config);
  320. #endif
  321. #if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL
  322. /**
  323. * @brief Create a PHY instance of KSZ8851SNL
  324. *
  325. * @param[in] config: configuration of PHY
  326. *
  327. * @return
  328. * - instance: create PHY instance successfully
  329. * - NULL: create PHY instance failed because some error occurred
  330. */
  331. esp_eth_phy_t *esp_eth_phy_new_ksz8851snl(const eth_phy_config_t *config);
  332. #endif
  333. #ifdef __cplusplus
  334. }
  335. #endif