esp_eth.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 Configuration status of PHY autonegotiation feature
  40. *
  41. */
  42. bool auto_nego_en;
  43. /**
  44. * @brief Input frame buffer to user's stack
  45. *
  46. * @param[in] eth_handle: handle of Ethernet driver
  47. * @param[in] buffer: frame buffer that will get input to upper stack
  48. * @param[in] length: length of the frame buffer
  49. *
  50. * @return
  51. * - ESP_OK: input frame buffer to upper stack successfully
  52. * - ESP_FAIL: error occurred when inputting buffer to upper stack
  53. *
  54. */
  55. esp_err_t (*stack_input)(esp_eth_handle_t eth_handle, uint8_t *buffer, uint32_t length, void *priv);
  56. /**
  57. * @brief Callback function invoked when lowlevel initialization is finished
  58. *
  59. * @param[in] eth_handle: handle of Ethernet driver
  60. *
  61. * @return
  62. * - ESP_OK: process extra lowlevel initialization successfully
  63. * - ESP_FAIL: error occurred when processing extra lowlevel initialization
  64. */
  65. esp_err_t (*on_lowlevel_init_done)(esp_eth_handle_t eth_handle);
  66. /**
  67. * @brief Callback function invoked when lowlevel deinitialization is finished
  68. *
  69. * @param[in] eth_handle: handle of Ethernet driver
  70. *
  71. * @return
  72. * - ESP_OK: process extra lowlevel deinitialization successfully
  73. * - ESP_FAIL: error occurred when processing extra lowlevel deinitialization
  74. */
  75. esp_err_t (*on_lowlevel_deinit_done)(esp_eth_handle_t eth_handle);
  76. /**
  77. * @brief Read PHY register
  78. *
  79. * @note Usually the PHY register read/write function is provided by MAC (SMI interface),
  80. * but if the PHY device is managed by other interface (e.g. I2C), then user needs to
  81. * implement the corresponding read/write.
  82. * Setting this to NULL means your PHY device is managed by MAC's SMI interface.
  83. *
  84. * @param[in] eth_handle: handle of Ethernet driver
  85. * @param[in] phy_addr: PHY chip address (0~31)
  86. * @param[in] phy_reg: PHY register index code
  87. * @param[out] reg_value: PHY register value
  88. *
  89. * @return
  90. * - ESP_OK: read PHY register successfully
  91. * - ESP_ERR_INVALID_ARG: read PHY register failed because of invalid argument
  92. * - ESP_ERR_TIMEOUT: read PHY register failed because of timeout
  93. * - ESP_FAIL: read PHY register failed because some other error occurred
  94. */
  95. esp_err_t (*read_phy_reg)(esp_eth_handle_t eth_handle, uint32_t phy_addr, uint32_t phy_reg, uint32_t *reg_value);
  96. /**
  97. * @brief Write PHY register
  98. *
  99. * @note Usually the PHY register read/write function is provided by MAC (SMI interface),
  100. * but if the PHY device is managed by other interface (e.g. I2C), then user needs to
  101. * implement the corresponding read/write.
  102. * Setting this to NULL means your PHY device is managed by MAC's SMI interface.
  103. *
  104. * @param[in] eth_handle: handle of Ethernet driver
  105. * @param[in] phy_addr: PHY chip address (0~31)
  106. * @param[in] phy_reg: PHY register index code
  107. * @param[in] reg_value: PHY register value
  108. *
  109. * @return
  110. * - ESP_OK: write PHY register successfully
  111. * - ESP_ERR_INVALID_ARG: read PHY register failed because of invalid argument
  112. * - ESP_ERR_TIMEOUT: write PHY register failed because of timeout
  113. * - ESP_FAIL: write PHY register failed because some other error occurred
  114. */
  115. esp_err_t (*write_phy_reg)(esp_eth_handle_t eth_handle, uint32_t phy_addr, uint32_t phy_reg, uint32_t reg_value);
  116. } esp_eth_config_t;
  117. /**
  118. * @brief Default configuration for Ethernet driver
  119. *
  120. */
  121. #define ETH_DEFAULT_CONFIG(emac, ephy) \
  122. { \
  123. .mac = emac, \
  124. .phy = ephy, \
  125. .check_link_period_ms = 2000, \
  126. .stack_input = NULL, \
  127. .on_lowlevel_init_done = NULL, \
  128. .on_lowlevel_deinit_done = NULL, \
  129. .read_phy_reg = NULL, \
  130. .write_phy_reg = NULL, \
  131. }
  132. /**
  133. * @brief Install Ethernet driver
  134. *
  135. * @param[in] config: configuration of the Ethernet driver
  136. * @param[out] out_hdl: handle of Ethernet driver
  137. *
  138. * @return
  139. * - ESP_OK: install esp_eth driver successfully
  140. * - ESP_ERR_INVALID_ARG: install esp_eth driver failed because of some invalid argument
  141. * - ESP_ERR_NO_MEM: install esp_eth driver failed because there's no memory for driver
  142. * - ESP_FAIL: install esp_eth driver failed because some other error occurred
  143. */
  144. esp_err_t esp_eth_driver_install(const esp_eth_config_t *config, esp_eth_handle_t *out_hdl);
  145. /**
  146. * @brief Uninstall Ethernet driver
  147. * @note It's not recommended to uninstall Ethernet driver unless it won't get used any more in application code.
  148. * To uninstall Ethernet driver, you have to make sure, all references to the driver are released.
  149. * Ethernet driver can only be uninstalled successfully when reference counter equals to one.
  150. *
  151. * @param[in] hdl: handle of Ethernet driver
  152. *
  153. * @return
  154. * - ESP_OK: uninstall esp_eth driver successfully
  155. * - ESP_ERR_INVALID_ARG: uninstall esp_eth driver failed because of some invalid argument
  156. * - ESP_ERR_INVALID_STATE: uninstall esp_eth driver failed because it has more than one reference
  157. * - ESP_FAIL: uninstall esp_eth driver failed because some other error occurred
  158. */
  159. esp_err_t esp_eth_driver_uninstall(esp_eth_handle_t hdl);
  160. /**
  161. * @brief Start Ethernet driver **ONLY** in standalone mode (i.e. without TCP/IP stack)
  162. *
  163. * @note This API will start driver state machine and internal software timer (for checking link status).
  164. *
  165. * @param[in] hdl handle of Ethernet driver
  166. *
  167. * @return
  168. * - ESP_OK: start esp_eth driver successfully
  169. * - ESP_ERR_INVALID_ARG: start esp_eth driver failed because of some invalid argument
  170. * - ESP_ERR_INVALID_STATE: start esp_eth driver failed because driver has started already
  171. * - ESP_FAIL: start esp_eth driver failed because some other error occurred
  172. */
  173. esp_err_t esp_eth_start(esp_eth_handle_t hdl);
  174. /**
  175. * @brief Stop Ethernet driver
  176. *
  177. * @note This function does the oppsite operation of `esp_eth_start`.
  178. *
  179. * @param[in] hdl handle of Ethernet driver
  180. * @return
  181. * - ESP_OK: stop esp_eth driver successfully
  182. * - ESP_ERR_INVALID_ARG: stop esp_eth driver failed because of some invalid argument
  183. * - ESP_ERR_INVALID_STATE: stop esp_eth driver failed because driver has not started yet
  184. * - ESP_FAIL: stop esp_eth driver failed because some other error occurred
  185. */
  186. esp_err_t esp_eth_stop(esp_eth_handle_t hdl);
  187. /**
  188. * @brief Update Ethernet data input path (i.e. specify where to pass the input buffer)
  189. *
  190. * @note After install driver, Ethernet still don't know where to deliver the input buffer.
  191. * In fact, this API registers a callback function which get invoked when Ethernet received new packets.
  192. *
  193. * @param[in] hdl handle of Ethernet driver
  194. * @param[in] stack_input function pointer, which does the actual process on incoming packets
  195. * @param[in] priv private resource, which gets passed to `stack_input` callback without any modification
  196. * @return
  197. * - ESP_OK: update input path successfully
  198. * - ESP_ERR_INVALID_ARG: update input path failed because of some invalid argument
  199. * - ESP_FAIL: update input path failed because some other error occurred
  200. */
  201. esp_err_t esp_eth_update_input_path(
  202. esp_eth_handle_t hdl,
  203. esp_err_t (*stack_input)(esp_eth_handle_t hdl, uint8_t *buffer, uint32_t length, void *priv),
  204. void *priv);
  205. /**
  206. * @brief General Transmit
  207. *
  208. * @param[in] hdl: handle of Ethernet driver
  209. * @param[in] buf: buffer of the packet to transfer
  210. * @param[in] length: length of the buffer to transfer
  211. *
  212. * @return
  213. * - ESP_OK: transmit frame buffer successfully
  214. * - ESP_ERR_INVALID_ARG: transmit frame buffer failed because of some invalid argument
  215. * - ESP_FAIL: transmit frame buffer failed because some other error occurred
  216. */
  217. esp_err_t esp_eth_transmit(esp_eth_handle_t hdl, void *buf, size_t length);
  218. /**
  219. * @brief General Receive is deprecated and shall not be accessed from app code,
  220. * as polling is not supported by Ethernet.
  221. *
  222. * @param[in] hdl: handle of Ethernet driver
  223. * @param[out] buf: buffer to preserve the received packet
  224. * @param[out] length: length of the received packet
  225. *
  226. * @note Before this function got invoked, the value of "length" should set by user, equals the size of buffer.
  227. * After the function returned, the value of "length" means the real length of received data.
  228. * @note This API was exposed by accident, users should not use this API in their applications.
  229. * Ethernet driver is interrupt driven, and doesn't support polling mode.
  230. * Instead, users should register input callback with ``esp_eth_update_input_path``.
  231. *
  232. * @return
  233. * - ESP_OK: receive frame buffer successfully
  234. * - ESP_ERR_INVALID_ARG: receive frame buffer failed because of some invalid argument
  235. * - ESP_ERR_INVALID_SIZE: input buffer size is not enough to hold the incoming data.
  236. * in this case, value of returned "length" indicates the real size of incoming data.
  237. * - ESP_FAIL: receive frame buffer failed because some other error occurred
  238. */
  239. 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")));
  240. /**
  241. * @brief Misc IO function of Etherent driver
  242. *
  243. * @param[in] hdl: handle of Ethernet driver
  244. * @param[in] cmd: IO control command
  245. * @param[in, out] data: address of data for `set` command or address where to store the data when used with `get` command
  246. *
  247. * @return
  248. * - ESP_OK: process io command successfully
  249. * - ESP_ERR_INVALID_ARG: process io command failed because of some invalid argument
  250. * - ESP_FAIL: process io command failed because some other error occurred
  251. * - ESP_ERR_NOT_SUPPORTED: requested feature is not supported
  252. *
  253. * The following IO control commands are supported:
  254. * @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.
  255. * @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.
  256. * @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.
  257. * @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.
  258. * @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.
  259. * Preconditions: Ethernet driver needs to be stopped.
  260. * @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.
  261. * @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.
  262. * Preconditions: Ethernet driver needs to be stopped and auto-negotiation disabled.
  263. * @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.
  264. * @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.
  265. * @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.
  266. * @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.
  267. * Preconditions: Ethernet driver needs to be stopped and auto-negotiation disabled.
  268. * @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.
  269. * @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.
  270. *
  271. */
  272. esp_err_t esp_eth_ioctl(esp_eth_handle_t hdl, esp_eth_io_cmd_t cmd, void *data);
  273. /**
  274. * @brief Increase Ethernet driver reference
  275. * @note Ethernet driver handle can be obtained by os timer, netif, etc.
  276. * It's dangerous when thread A is using Ethernet but thread B uninstall the driver.
  277. * Using reference counter can prevent such risk, but care should be taken, when you obtain Ethernet driver,
  278. * this API must be invoked so that the driver won't be uninstalled during your using time.
  279. *
  280. *
  281. * @param[in] hdl: handle of Ethernet driver
  282. * @return
  283. * - ESP_OK: increase reference successfully
  284. * - ESP_ERR_INVALID_ARG: increase reference failed because of some invalid argument
  285. */
  286. esp_err_t esp_eth_increase_reference(esp_eth_handle_t hdl);
  287. /**
  288. * @brief Decrease Ethernet driver reference
  289. *
  290. * @param[in] hdl: handle of Ethernet driver
  291. * @return
  292. * - ESP_OK: increase reference successfully
  293. * - ESP_ERR_INVALID_ARG: increase reference failed because of some invalid argument
  294. */
  295. esp_err_t esp_eth_decrease_reference(esp_eth_handle_t hdl);
  296. #ifdef __cplusplus
  297. }
  298. #endif