esp_eth.h 13 KB

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