esp_eth_phy.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 Custom IO function of PHY driver. This function is intended to extend common options of esp_eth_ioctl to cover specifics of PHY chip.
  210. *
  211. * @note This function may not be assigned when the PHY chip supports only most common set of configuration options.
  212. *
  213. * @param[in] phy: Ethernet PHY instance
  214. * @param[in] cmd: IO control command
  215. * @param[in, out] data: address of data for `set` command or address where to store the data when used with `get` command
  216. *
  217. * @return
  218. * - ESP_OK: process io command successfully
  219. * - ESP_ERR_INVALID_ARG: process io command failed because of some invalid argument
  220. * - ESP_FAIL: process io command failed because some other error occurred
  221. * - ESP_ERR_NOT_SUPPORTED: requested feature is not supported
  222. */
  223. esp_err_t (*custom_ioctl)(esp_eth_phy_t *phy, uint32_t cmd, void *data);
  224. /**
  225. * @brief Free memory of Ethernet PHY instance
  226. *
  227. * @param[in] phy: Ethernet PHY instance
  228. *
  229. * @return
  230. * - ESP_OK: free PHY instance successfully
  231. * - ESP_FAIL: free PHY instance failed because some error occurred
  232. *
  233. */
  234. esp_err_t (*del)(esp_eth_phy_t *phy);
  235. };
  236. /**
  237. * @brief Ethernet PHY configuration
  238. *
  239. */
  240. typedef struct {
  241. int32_t phy_addr; /*!< PHY address, set -1 to enable PHY address detection at initialization stage */
  242. uint32_t reset_timeout_ms; /*!< Reset timeout value (Unit: ms) */
  243. uint32_t autonego_timeout_ms; /*!< Auto-negotiation timeout value (Unit: ms) */
  244. int reset_gpio_num; /*!< Reset GPIO number, -1 means no hardware reset */
  245. } eth_phy_config_t;
  246. /**
  247. * @brief Default configuration for Ethernet PHY object
  248. *
  249. */
  250. #define ETH_PHY_DEFAULT_CONFIG() \
  251. { \
  252. .phy_addr = ESP_ETH_PHY_ADDR_AUTO, \
  253. .reset_timeout_ms = 100, \
  254. .autonego_timeout_ms = 4000, \
  255. .reset_gpio_num = 5, \
  256. }
  257. /**
  258. * @brief Create a PHY instance of IP101
  259. *
  260. * @param[in] config: configuration of PHY
  261. *
  262. * @return
  263. * - instance: create PHY instance successfully
  264. * - NULL: create PHY instance failed because some error occurred
  265. */
  266. esp_eth_phy_t *esp_eth_phy_new_ip101(const eth_phy_config_t *config);
  267. /**
  268. * @brief Create a PHY instance of RTL8201
  269. *
  270. * @param[in] config: configuration of PHY
  271. *
  272. * @return
  273. * - instance: create PHY instance successfully
  274. * - NULL: create PHY instance failed because some error occurred
  275. */
  276. esp_eth_phy_t *esp_eth_phy_new_rtl8201(const eth_phy_config_t *config);
  277. /**
  278. * @brief Create a PHY instance of LAN87xx
  279. *
  280. * @param[in] config: configuration of PHY
  281. *
  282. * @return
  283. * - instance: create PHY instance successfully
  284. * - NULL: create PHY instance failed because some error occurred
  285. */
  286. esp_eth_phy_t *esp_eth_phy_new_lan87xx(const eth_phy_config_t *config);
  287. /**
  288. * @brief Create a PHY instance of DP83848
  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_dp83848(const eth_phy_config_t *config);
  297. /**
  298. * @brief Create a PHY instance of KSZ80xx
  299. *
  300. * The phy model from the KSZ80xx series is detected automatically. If the driver
  301. * is unable to detect a supported model, \c NULL is returned.
  302. *
  303. * Currently, the following models are supported:
  304. * KSZ8001, KSZ8021, KSZ8031, KSZ8041, KSZ8051, KSZ8061, KSZ8081, KSZ8091
  305. *
  306. * @param[in] config: configuration of PHY
  307. *
  308. * @return
  309. * - instance: create PHY instance successfully
  310. * - NULL: create PHY instance failed because some error occurred
  311. */
  312. esp_eth_phy_t *esp_eth_phy_new_ksz80xx(const eth_phy_config_t *config);
  313. #if CONFIG_ETH_SPI_ETHERNET_DM9051
  314. /**
  315. * @brief Create a PHY instance of DM9051
  316. *
  317. * @param[in] config: configuration of PHY
  318. *
  319. * @return
  320. * - instance: create PHY instance successfully
  321. * - NULL: create PHY instance failed because some error occurred
  322. */
  323. esp_eth_phy_t *esp_eth_phy_new_dm9051(const eth_phy_config_t *config);
  324. #endif
  325. #if CONFIG_ETH_SPI_ETHERNET_W5500
  326. /**
  327. * @brief Create a PHY instance of W5500
  328. *
  329. * @param[in] config: configuration of PHY
  330. *
  331. * @return
  332. * - instance: create PHY instance successfully
  333. * - NULL: create PHY instance failed because some error occurred
  334. */
  335. esp_eth_phy_t *esp_eth_phy_new_w5500(const eth_phy_config_t *config);
  336. #endif
  337. #if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL
  338. /**
  339. * @brief Create a PHY instance of KSZ8851SNL
  340. *
  341. * @param[in] config: configuration of PHY
  342. *
  343. * @return
  344. * - instance: create PHY instance successfully
  345. * - NULL: create PHY instance failed because some error occurred
  346. */
  347. esp_eth_phy_t *esp_eth_phy_new_ksz8851snl(const eth_phy_config_t *config);
  348. #endif
  349. #ifdef __cplusplus
  350. }
  351. #endif