esp_eth_driver.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include "esp_eth_com.h"
  8. #include "esp_eth_mac.h"
  9. #include "esp_eth_phy.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /**
  14. * @brief Handle of Ethernet driver
  15. *
  16. */
  17. typedef void *esp_eth_handle_t;
  18. /**
  19. * @brief Configuration of Ethernet driver
  20. *
  21. */
  22. typedef struct {
  23. /**
  24. * @brief Ethernet MAC object
  25. *
  26. */
  27. esp_eth_mac_t *mac;
  28. /**
  29. * @brief Ethernet PHY object
  30. *
  31. */
  32. esp_eth_phy_t *phy;
  33. /**
  34. * @brief Period time of checking Ethernet link status
  35. *
  36. */
  37. uint32_t check_link_period_ms;
  38. /**
  39. * @brief Input frame buffer to user's stack
  40. *
  41. * @param[in] eth_handle: handle of Ethernet driver
  42. * @param[in] buffer: frame buffer that will get input to upper stack
  43. * @param[in] length: length of the frame buffer
  44. *
  45. * @return
  46. * - ESP_OK: input frame buffer to upper stack successfully
  47. * - ESP_FAIL: error occurred when inputting buffer to upper stack
  48. *
  49. */
  50. esp_err_t (*stack_input)(esp_eth_handle_t eth_handle, uint8_t *buffer, uint32_t length, void *priv);
  51. /**
  52. * @brief Callback function invoked when lowlevel initialization is finished
  53. *
  54. * @param[in] eth_handle: handle of Ethernet driver
  55. *
  56. * @return
  57. * - ESP_OK: process extra lowlevel initialization successfully
  58. * - ESP_FAIL: error occurred when processing extra lowlevel initialization
  59. */
  60. esp_err_t (*on_lowlevel_init_done)(esp_eth_handle_t eth_handle);
  61. /**
  62. * @brief Callback function invoked when lowlevel deinitialization is finished
  63. *
  64. * @param[in] eth_handle: handle of Ethernet driver
  65. *
  66. * @return
  67. * - ESP_OK: process extra lowlevel deinitialization successfully
  68. * - ESP_FAIL: error occurred when processing extra lowlevel deinitialization
  69. */
  70. esp_err_t (*on_lowlevel_deinit_done)(esp_eth_handle_t eth_handle);
  71. /**
  72. * @brief Read PHY register
  73. *
  74. * @note Usually the PHY register read/write function is provided by MAC (SMI interface),
  75. * but if the PHY device is managed by other interface (e.g. I2C), then user needs to
  76. * implement the corresponding read/write.
  77. * Setting this to NULL means your PHY device is managed by MAC's SMI interface.
  78. *
  79. * @param[in] eth_handle: handle of Ethernet driver
  80. * @param[in] phy_addr: PHY chip address (0~31)
  81. * @param[in] phy_reg: PHY register index code
  82. * @param[out] reg_value: PHY register value
  83. *
  84. * @return
  85. * - ESP_OK: read PHY register successfully
  86. * - ESP_ERR_INVALID_ARG: read PHY register failed because of invalid argument
  87. * - ESP_ERR_TIMEOUT: read PHY register failed because of timeout
  88. * - ESP_FAIL: read PHY register failed because some other error occurred
  89. */
  90. esp_err_t (*read_phy_reg)(esp_eth_handle_t eth_handle, uint32_t phy_addr, uint32_t phy_reg, uint32_t *reg_value);
  91. /**
  92. * @brief Write PHY register
  93. *
  94. * @note Usually the PHY register read/write function is provided by MAC (SMI interface),
  95. * but if the PHY device is managed by other interface (e.g. I2C), then user needs to
  96. * implement the corresponding read/write.
  97. * Setting this to NULL means your PHY device is managed by MAC's SMI interface.
  98. *
  99. * @param[in] eth_handle: handle of Ethernet driver
  100. * @param[in] phy_addr: PHY chip address (0~31)
  101. * @param[in] phy_reg: PHY register index code
  102. * @param[in] reg_value: PHY register value
  103. *
  104. * @return
  105. * - ESP_OK: write PHY register successfully
  106. * - ESP_ERR_INVALID_ARG: read PHY register failed because of invalid argument
  107. * - ESP_ERR_TIMEOUT: write PHY register failed because of timeout
  108. * - ESP_FAIL: write PHY register failed because some other error occurred
  109. */
  110. esp_err_t (*write_phy_reg)(esp_eth_handle_t eth_handle, uint32_t phy_addr, uint32_t phy_reg, uint32_t reg_value);
  111. } esp_eth_config_t;
  112. /**
  113. * @brief Data structure to Read/Write PHY register via ioctl API
  114. *
  115. */
  116. typedef struct {
  117. uint32_t reg_addr; /*!< PHY register address */
  118. uint32_t *reg_value_p; /*!< Pointer to a memory where the register value is read/written */
  119. } esp_eth_phy_reg_rw_data_t;
  120. /**
  121. * @brief Command list for ioctl API
  122. *
  123. */
  124. typedef enum {
  125. ETH_CMD_G_MAC_ADDR, /*!< Get MAC address */
  126. ETH_CMD_S_MAC_ADDR, /*!< Set MAC address */
  127. ETH_CMD_G_PHY_ADDR, /*!< Get PHY address */
  128. ETH_CMD_S_PHY_ADDR, /*!< Set PHY address */
  129. ETH_CMD_G_AUTONEGO, /*!< Get PHY Auto Negotiation */
  130. ETH_CMD_S_AUTONEGO, /*!< Set PHY Auto Negotiation */
  131. ETH_CMD_G_SPEED, /*!< Get Speed */
  132. ETH_CMD_S_SPEED, /*!< Set Speed */
  133. ETH_CMD_S_PROMISCUOUS, /*!< Set promiscuous mode */
  134. ETH_CMD_S_FLOW_CTRL, /*!< Set flow control */
  135. ETH_CMD_G_DUPLEX_MODE, /*!< Get Duplex mode */
  136. ETH_CMD_S_DUPLEX_MODE, /*!< Set Duplex mode */
  137. ETH_CMD_S_PHY_LOOPBACK, /*!< Set PHY loopback */
  138. ETH_CMD_READ_PHY_REG, /*!< Read PHY register */
  139. ETH_CMD_WRITE_PHY_REG, /*!< Write PHY register */
  140. ETH_CMD_CUSTOM_MAC_CMDS = 0x0FFF, // Offset for start of MAC custom commands
  141. ETH_CMD_CUSTOM_PHY_CMDS = 0x1FFF, // Offset for start of PHY custom commands
  142. } esp_eth_io_cmd_t;
  143. /**
  144. * @brief Default configuration for Ethernet driver
  145. *
  146. */
  147. #define ETH_DEFAULT_CONFIG(emac, ephy) \
  148. { \
  149. .mac = emac, \
  150. .phy = ephy, \
  151. .check_link_period_ms = 2000, \
  152. .stack_input = NULL, \
  153. .on_lowlevel_init_done = NULL, \
  154. .on_lowlevel_deinit_done = NULL, \
  155. .read_phy_reg = NULL, \
  156. .write_phy_reg = NULL, \
  157. }
  158. /**
  159. * @brief Install Ethernet driver
  160. *
  161. * @param[in] config: configuration of the Ethernet driver
  162. * @param[out] out_hdl: handle of Ethernet driver
  163. *
  164. * @return
  165. * - ESP_OK: install esp_eth driver successfully
  166. * - ESP_ERR_INVALID_ARG: install esp_eth driver failed because of some invalid argument
  167. * - ESP_ERR_NO_MEM: install esp_eth driver failed because there's no memory for driver
  168. * - ESP_FAIL: install esp_eth driver failed because some other error occurred
  169. */
  170. esp_err_t esp_eth_driver_install(const esp_eth_config_t *config, esp_eth_handle_t *out_hdl);
  171. /**
  172. * @brief Uninstall Ethernet driver
  173. * @note It's not recommended to uninstall Ethernet driver unless it won't get used any more in application code.
  174. * To uninstall Ethernet driver, you have to make sure, all references to the driver are released.
  175. * Ethernet driver can only be uninstalled successfully when reference counter equals to one.
  176. *
  177. * @param[in] hdl: handle of Ethernet driver
  178. *
  179. * @return
  180. * - ESP_OK: uninstall esp_eth driver successfully
  181. * - ESP_ERR_INVALID_ARG: uninstall esp_eth driver failed because of some invalid argument
  182. * - ESP_ERR_INVALID_STATE: uninstall esp_eth driver failed because it has more than one reference
  183. * - ESP_FAIL: uninstall esp_eth driver failed because some other error occurred
  184. */
  185. esp_err_t esp_eth_driver_uninstall(esp_eth_handle_t hdl);
  186. /**
  187. * @brief Start Ethernet driver **ONLY** in standalone mode (i.e. without TCP/IP stack)
  188. *
  189. * @note This API will start driver state machine and internal software timer (for checking link status).
  190. *
  191. * @param[in] hdl handle of Ethernet driver
  192. *
  193. * @return
  194. * - ESP_OK: start esp_eth driver successfully
  195. * - ESP_ERR_INVALID_ARG: start esp_eth driver failed because of some invalid argument
  196. * - ESP_ERR_INVALID_STATE: start esp_eth driver failed because driver has started already
  197. * - ESP_FAIL: start esp_eth driver failed because some other error occurred
  198. */
  199. esp_err_t esp_eth_start(esp_eth_handle_t hdl);
  200. /**
  201. * @brief Stop Ethernet driver
  202. *
  203. * @note This function does the oppsite operation of `esp_eth_start`.
  204. *
  205. * @param[in] hdl handle of Ethernet driver
  206. * @return
  207. * - ESP_OK: stop esp_eth driver successfully
  208. * - ESP_ERR_INVALID_ARG: stop esp_eth driver failed because of some invalid argument
  209. * - ESP_ERR_INVALID_STATE: stop esp_eth driver failed because driver has not started yet
  210. * - ESP_FAIL: stop esp_eth driver failed because some other error occurred
  211. */
  212. esp_err_t esp_eth_stop(esp_eth_handle_t hdl);
  213. /**
  214. * @brief Update Ethernet data input path (i.e. specify where to pass the input buffer)
  215. *
  216. * @note After install driver, Ethernet still don't know where to deliver the input buffer.
  217. * In fact, this API registers a callback function which get invoked when Ethernet received new packets.
  218. *
  219. * @param[in] hdl handle of Ethernet driver
  220. * @param[in] stack_input function pointer, which does the actual process on incoming packets
  221. * @param[in] priv private resource, which gets passed to `stack_input` callback without any modification
  222. * @return
  223. * - ESP_OK: update input path successfully
  224. * - ESP_ERR_INVALID_ARG: update input path failed because of some invalid argument
  225. * - ESP_FAIL: update input path failed because some other error occurred
  226. */
  227. esp_err_t esp_eth_update_input_path(
  228. esp_eth_handle_t hdl,
  229. esp_err_t (*stack_input)(esp_eth_handle_t hdl, uint8_t *buffer, uint32_t length, void *priv),
  230. void *priv);
  231. /**
  232. * @brief General Transmit
  233. *
  234. * @param[in] hdl: handle of Ethernet driver
  235. * @param[in] buf: buffer of the packet to transfer
  236. * @param[in] length: length of the buffer to transfer
  237. *
  238. * @return
  239. * - ESP_OK: transmit frame buffer successfully
  240. * - ESP_ERR_INVALID_ARG: transmit frame buffer failed because of some invalid argument
  241. * - ESP_ERR_INVALID_STATE: invalid driver state (e.i. driver is not started)
  242. * - ESP_ERR_TIMEOUT: transmit frame buffer failed because HW was not get available in predefined period
  243. * - ESP_FAIL: transmit frame buffer failed because some other error occurred
  244. */
  245. esp_err_t esp_eth_transmit(esp_eth_handle_t hdl, void *buf, size_t length);
  246. /**
  247. * @brief Special Transmit with variable number of arguments
  248. *
  249. * @param[in] hdl handle of Ethernet driver
  250. * @param[in] argc number variable arguments
  251. * @param ... variable arguments
  252. * @return
  253. * - ESP_OK: transmit successfull
  254. * - ESP_ERR_INVALID_STATE: invalid driver state (e.i. driver is not started)
  255. * - ESP_ERR_TIMEOUT: transmit frame buffer failed because HW was not get available in predefined period
  256. * - ESP_FAIL: transmit frame buffer failed because some other error occurred
  257. */
  258. esp_err_t esp_eth_transmit_vargs(esp_eth_handle_t hdl, uint32_t argc, ...);
  259. /**
  260. * @brief Misc IO function of Etherent driver
  261. *
  262. * @param[in] hdl: handle of Ethernet driver
  263. * @param[in] cmd: IO control command
  264. * @param[in, out] data: address of data for `set` command or address where to store the data when used with `get` command
  265. *
  266. * @return
  267. * - ESP_OK: process io command successfully
  268. * - ESP_ERR_INVALID_ARG: process io command failed because of some invalid argument
  269. * - ESP_FAIL: process io command failed because some other error occurred
  270. * - ESP_ERR_NOT_SUPPORTED: requested feature is not supported
  271. *
  272. * The following common IO control commands are supported:
  273. * @li @c ETH_CMD_S_MAC_ADDR sets Ethernet interface MAC address. @c data argument is pointer to MAC address buffer with expected size of 6 bytes.
  274. * @li @c ETH_CMD_G_MAC_ADDR gets Ethernet interface MAC address. @c data argument is pointer to a buffer to which MAC address is to be copied. The buffer size must be at least 6 bytes.
  275. * @li @c ETH_CMD_S_PHY_ADDR sets PHY address in range of <0-31>. @c data argument is pointer to memory of uint32_t datatype from where the configuration option is read.
  276. * @li @c ETH_CMD_G_PHY_ADDR gets PHY address. @c data argument is pointer to memory of uint32_t datatype to which the PHY address is to be stored.
  277. * @li @c ETH_CMD_S_AUTONEGO enables or disables Ethernet link speed and duplex mode autonegotiation. @c data argument is pointer to memory of bool datatype from which the configuration option is read.
  278. * Preconditions: Ethernet driver needs to be stopped.
  279. * @li @c ETH_CMD_G_AUTONEGO gets current configuration of the Ethernet link speed and duplex mode autonegotiation. @c data argument is pointer to memory of bool datatype to which the current configuration is to be stored.
  280. * @li @c ETH_CMD_S_SPEED sets the Ethernet link speed. @c data argument is pointer to memory of eth_speed_t datatype from which the configuration option is read.
  281. * Preconditions: Ethernet driver needs to be stopped and auto-negotiation disabled.
  282. * @li @c ETH_CMD_G_SPEED gets current Ethernet link speed. @c data argument is pointer to memory of eth_speed_t datatype to which the speed is to be stored.
  283. * @li @c ETH_CMD_S_PROMISCUOUS sets/resets Ethernet interface promiscuous mode. @c data argument is pointer to memory of bool datatype from which the configuration option is read.
  284. * @li @c ETH_CMD_S_FLOW_CTRL sets/resets Ethernet interface flow control. @c data argument is pointer to memory of bool datatype from which the configuration option is read.
  285. * @li @c ETH_CMD_S_DUPLEX_MODE sets the Ethernet duplex mode. @c data argument is pointer to memory of eth_duplex_t datatype from which the configuration option is read.
  286. * Preconditions: Ethernet driver needs to be stopped and auto-negotiation disabled.
  287. * @li @c ETH_CMD_G_DUPLEX_MODE gets current Ethernet link duplex mode. @c data argument is pointer to memory of eth_duplex_t datatype to which the duplex mode is to be stored.
  288. * @li @c ETH_CMD_S_PHY_LOOPBACK sets/resets PHY to/from loopback mode. @c data argument is pointer to memory of bool datatype from which the configuration option is read.
  289. *
  290. * @li Note that additional control commands may be available for specific MAC or PHY chips. Please consult specific MAC or PHY documentation or driver code.
  291. */
  292. esp_err_t esp_eth_ioctl(esp_eth_handle_t hdl, esp_eth_io_cmd_t cmd, void *data);
  293. /**
  294. * @brief Increase Ethernet driver reference
  295. * @note Ethernet driver handle can be obtained by os timer, netif, etc.
  296. * It's dangerous when thread A is using Ethernet but thread B uninstall the driver.
  297. * Using reference counter can prevent such risk, but care should be taken, when you obtain Ethernet driver,
  298. * this API must be invoked so that the driver won't be uninstalled during your using time.
  299. *
  300. *
  301. * @param[in] hdl: handle of Ethernet driver
  302. * @return
  303. * - ESP_OK: increase reference successfully
  304. * - ESP_ERR_INVALID_ARG: increase reference failed because of some invalid argument
  305. */
  306. esp_err_t esp_eth_increase_reference(esp_eth_handle_t hdl);
  307. /**
  308. * @brief Decrease Ethernet driver reference
  309. *
  310. * @param[in] hdl: handle of Ethernet driver
  311. * @return
  312. * - ESP_OK: increase reference successfully
  313. * - ESP_ERR_INVALID_ARG: increase reference failed because of some invalid argument
  314. */
  315. esp_err_t esp_eth_decrease_reference(esp_eth_handle_t hdl);
  316. #ifdef __cplusplus
  317. }
  318. #endif