esp_eth.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 "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 Command list for ioctl API
  114. *
  115. */
  116. typedef enum {
  117. ETH_CMD_G_MAC_ADDR, /*!< Get MAC address */
  118. ETH_CMD_S_MAC_ADDR, /*!< Set MAC address */
  119. ETH_CMD_G_PHY_ADDR, /*!< Get PHY address */
  120. ETH_CMD_S_PHY_ADDR, /*!< Set PHY address */
  121. ETH_CMD_G_AUTONEGO, /*!< Get PHY Auto Negotiation */
  122. ETH_CMD_S_AUTONEGO, /*!< Set PHY Auto Negotiation */
  123. ETH_CMD_G_SPEED, /*!< Get Speed */
  124. ETH_CMD_S_SPEED, /*!< Set Speed */
  125. ETH_CMD_S_PROMISCUOUS, /*!< Set promiscuous mode */
  126. ETH_CMD_S_FLOW_CTRL, /*!< Set flow control */
  127. ETH_CMD_G_DUPLEX_MODE, /*!< Get Duplex mode */
  128. ETH_CMD_S_DUPLEX_MODE, /*!< Set Duplex mode */
  129. ETH_CMD_S_PHY_LOOPBACK,/*!< Set PHY loopback */
  130. } esp_eth_io_cmd_t;
  131. /**
  132. * @brief Default configuration for Ethernet driver
  133. *
  134. */
  135. #define ETH_DEFAULT_CONFIG(emac, ephy) \
  136. { \
  137. .mac = emac, \
  138. .phy = ephy, \
  139. .check_link_period_ms = 2000, \
  140. .stack_input = NULL, \
  141. .on_lowlevel_init_done = NULL, \
  142. .on_lowlevel_deinit_done = NULL, \
  143. .read_phy_reg = NULL, \
  144. .write_phy_reg = NULL, \
  145. }
  146. /**
  147. * @brief Install Ethernet driver
  148. *
  149. * @param[in] config: configuration of the Ethernet driver
  150. * @param[out] out_hdl: handle of Ethernet driver
  151. *
  152. * @return
  153. * - ESP_OK: install esp_eth driver successfully
  154. * - ESP_ERR_INVALID_ARG: install esp_eth driver failed because of some invalid argument
  155. * - ESP_ERR_NO_MEM: install esp_eth driver failed because there's no memory for driver
  156. * - ESP_FAIL: install esp_eth driver failed because some other error occurred
  157. */
  158. esp_err_t esp_eth_driver_install(const esp_eth_config_t *config, esp_eth_handle_t *out_hdl);
  159. /**
  160. * @brief Uninstall Ethernet driver
  161. * @note It's not recommended to uninstall Ethernet driver unless it won't get used any more in application code.
  162. * To uninstall Ethernet driver, you have to make sure, all references to the driver are released.
  163. * Ethernet driver can only be uninstalled successfully when reference counter equals to one.
  164. *
  165. * @param[in] hdl: handle of Ethernet driver
  166. *
  167. * @return
  168. * - ESP_OK: uninstall esp_eth driver successfully
  169. * - ESP_ERR_INVALID_ARG: uninstall esp_eth driver failed because of some invalid argument
  170. * - ESP_ERR_INVALID_STATE: uninstall esp_eth driver failed because it has more than one reference
  171. * - ESP_FAIL: uninstall esp_eth driver failed because some other error occurred
  172. */
  173. esp_err_t esp_eth_driver_uninstall(esp_eth_handle_t hdl);
  174. /**
  175. * @brief Start Ethernet driver **ONLY** in standalone mode (i.e. without TCP/IP stack)
  176. *
  177. * @note This API will start driver state machine and internal software timer (for checking link status).
  178. *
  179. * @param[in] hdl handle of Ethernet driver
  180. *
  181. * @return
  182. * - ESP_OK: start esp_eth driver successfully
  183. * - ESP_ERR_INVALID_ARG: start esp_eth driver failed because of some invalid argument
  184. * - ESP_ERR_INVALID_STATE: start esp_eth driver failed because driver has started already
  185. * - ESP_FAIL: start esp_eth driver failed because some other error occurred
  186. */
  187. esp_err_t esp_eth_start(esp_eth_handle_t hdl);
  188. /**
  189. * @brief Stop Ethernet driver
  190. *
  191. * @note This function does the oppsite operation of `esp_eth_start`.
  192. *
  193. * @param[in] hdl handle of Ethernet driver
  194. * @return
  195. * - ESP_OK: stop esp_eth driver successfully
  196. * - ESP_ERR_INVALID_ARG: stop esp_eth driver failed because of some invalid argument
  197. * - ESP_ERR_INVALID_STATE: stop esp_eth driver failed because driver has not started yet
  198. * - ESP_FAIL: stop esp_eth driver failed because some other error occurred
  199. */
  200. esp_err_t esp_eth_stop(esp_eth_handle_t hdl);
  201. /**
  202. * @brief Update Ethernet data input path (i.e. specify where to pass the input buffer)
  203. *
  204. * @note After install driver, Ethernet still don't know where to deliver the input buffer.
  205. * In fact, this API registers a callback function which get invoked when Ethernet received new packets.
  206. *
  207. * @param[in] hdl handle of Ethernet driver
  208. * @param[in] stack_input function pointer, which does the actual process on incoming packets
  209. * @param[in] priv private resource, which gets passed to `stack_input` callback without any modification
  210. * @return
  211. * - ESP_OK: update input path successfully
  212. * - ESP_ERR_INVALID_ARG: update input path failed because of some invalid argument
  213. * - ESP_FAIL: update input path failed because some other error occurred
  214. */
  215. esp_err_t esp_eth_update_input_path(
  216. esp_eth_handle_t hdl,
  217. esp_err_t (*stack_input)(esp_eth_handle_t hdl, uint8_t *buffer, uint32_t length, void *priv),
  218. void *priv);
  219. /**
  220. * @brief General Transmit
  221. *
  222. * @param[in] hdl: handle of Ethernet driver
  223. * @param[in] buf: buffer of the packet to transfer
  224. * @param[in] length: length of the buffer to transfer
  225. *
  226. * @return
  227. * - ESP_OK: transmit frame buffer successfully
  228. * - ESP_ERR_INVALID_ARG: transmit frame buffer failed because of some invalid argument
  229. * - ESP_FAIL: transmit frame buffer failed because some other error occurred
  230. */
  231. esp_err_t esp_eth_transmit(esp_eth_handle_t hdl, void *buf, size_t length);
  232. /**
  233. * @brief General Receive is deprecated and shall not be accessed from app code,
  234. * as polling is not supported by Ethernet.
  235. *
  236. * @param[in] hdl: handle of Ethernet driver
  237. * @param[out] buf: buffer to preserve the received packet
  238. * @param[out] length: length of the received packet
  239. *
  240. * @note Before this function got invoked, the value of "length" should set by user, equals the size of buffer.
  241. * After the function returned, the value of "length" means the real length of received data.
  242. * @note This API was exposed by accident, users should not use this API in their applications.
  243. * Ethernet driver is interrupt driven, and doesn't support polling mode.
  244. * Instead, users should register input callback with ``esp_eth_update_input_path``.
  245. *
  246. * @return
  247. * - ESP_OK: receive frame buffer successfully
  248. * - ESP_ERR_INVALID_ARG: receive frame buffer failed because of some invalid argument
  249. * - ESP_ERR_INVALID_SIZE: input buffer size is not enough to hold the incoming data.
  250. * in this case, value of returned "length" indicates the real size of incoming data.
  251. * - ESP_FAIL: receive frame buffer failed because some other error occurred
  252. */
  253. esp_err_t esp_eth_receive(esp_eth_handle_t hdl, uint8_t *buf, uint32_t *length) __attribute__((deprecated("Ethernet driver is interrupt driven only, please register input callback with esp_eth_update_input_path")));
  254. /**
  255. * @brief Misc IO function of Etherent driver
  256. *
  257. * @param[in] hdl: handle of Ethernet driver
  258. * @param[in] cmd: IO control command
  259. * @param[in, out] data: address of data for `set` command or address where to store the data when used with `get` command
  260. *
  261. * @return
  262. * - ESP_OK: process io command successfully
  263. * - ESP_ERR_INVALID_ARG: process io command failed because of some invalid argument
  264. * - ESP_FAIL: process io command failed because some other error occurred
  265. * - ESP_ERR_NOT_SUPPORTED: requested feature is not supported
  266. *
  267. * The following IO control commands are supported:
  268. * @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.
  269. * @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.
  270. * @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.
  271. * @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.
  272. * @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.
  273. * Preconditions: Ethernet driver needs to be stopped.
  274. * @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.
  275. * @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.
  276. * Preconditions: Ethernet driver needs to be stopped and auto-negotiation disabled.
  277. * @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.
  278. * @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.
  279. * @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.
  280. * @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.
  281. * Preconditions: Ethernet driver needs to be stopped and auto-negotiation disabled.
  282. * @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.
  283. * @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.
  284. *
  285. */
  286. esp_err_t esp_eth_ioctl(esp_eth_handle_t hdl, esp_eth_io_cmd_t cmd, void *data);
  287. /**
  288. * @brief Increase Ethernet driver reference
  289. * @note Ethernet driver handle can be obtained by os timer, netif, etc.
  290. * It's dangerous when thread A is using Ethernet but thread B uninstall the driver.
  291. * Using reference counter can prevent such risk, but care should be taken, when you obtain Ethernet driver,
  292. * this API must be invoked so that the driver won't be uninstalled during your using time.
  293. *
  294. *
  295. * @param[in] hdl: handle of Ethernet driver
  296. * @return
  297. * - ESP_OK: increase reference successfully
  298. * - ESP_ERR_INVALID_ARG: increase reference failed because of some invalid argument
  299. */
  300. esp_err_t esp_eth_increase_reference(esp_eth_handle_t hdl);
  301. /**
  302. * @brief Decrease Ethernet driver reference
  303. *
  304. * @param[in] hdl: handle of Ethernet driver
  305. * @return
  306. * - ESP_OK: increase reference successfully
  307. * - ESP_ERR_INVALID_ARG: increase reference failed because of some invalid argument
  308. */
  309. esp_err_t esp_eth_decrease_reference(esp_eth_handle_t hdl);
  310. #ifdef __cplusplus
  311. }
  312. #endif