esp_eth_driver.h 15 KB

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