esp_eth.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. } esp_eth_config_t;
  80. /**
  81. * @brief Default configuration for Ethernet driver
  82. *
  83. */
  84. #define ETH_DEFAULT_CONFIG(emac, ephy) \
  85. { \
  86. .mac = emac, \
  87. .phy = ephy, \
  88. .check_link_period_ms = 2000, \
  89. .stack_input = NULL, \
  90. .on_lowlevel_init_done = NULL, \
  91. .on_lowlevel_deinit_done = NULL, \
  92. }
  93. /**
  94. * @brief Install Ethernet driver
  95. *
  96. * @param[in] config: configuration of the Ethernet driver
  97. * @param[out] out_hdl: handle of Ethernet driver
  98. *
  99. * @return
  100. * - ESP_OK: install esp_eth driver successfully
  101. * - ESP_ERR_INVALID_ARG: install esp_eth driver failed because of some invalid argument
  102. * - ESP_ERR_NO_MEM: install esp_eth driver failed because there's no memory for driver
  103. * - ESP_FAIL: install esp_eth driver failed because some other error occurred
  104. */
  105. esp_err_t esp_eth_driver_install(const esp_eth_config_t *config, esp_eth_handle_t *out_hdl);
  106. /**
  107. * @brief Uninstall Ethernet driver
  108. * @note It's not recommended to uninstall Ethernet driver unless it won't get used any more in application code.
  109. * To uninstall Ethernet driver, you have to make sure, all references to the driver are released.
  110. * Ethernet driver can only be uninstalled successfully when reference counter equals to one.
  111. *
  112. * @param[in] hdl: handle of Ethernet driver
  113. *
  114. * @return
  115. * - ESP_OK: uninstall esp_eth driver successfully
  116. * - ESP_ERR_INVALID_ARG: uninstall esp_eth driver failed because of some invalid argument
  117. * - ESP_ERR_INVALID_STATE: uninstall esp_eth driver failed because it has more than one reference
  118. * - ESP_FAIL: uninstall esp_eth driver failed because some other error occurred
  119. */
  120. esp_err_t esp_eth_driver_uninstall(esp_eth_handle_t hdl);
  121. /**
  122. * @brief Start Ethernet driver **ONLY** in standalone mode (i.e. without TCP/IP stack)
  123. *
  124. * @note This API will start driver state machine and internal software timer (for checking link status).
  125. *
  126. * @param[in] hdl handle of Ethernet driver
  127. *
  128. * @return
  129. * - ESP_OK: start esp_eth driver successfully
  130. * - ESP_ERR_INVALID_ARG: start esp_eth driver failed because of some invalid argument
  131. * - ESP_ERR_INVALID_STATE: start esp_eth driver failed because driver has started already
  132. * - ESP_FAIL: start esp_eth driver failed because some other error occurred
  133. */
  134. esp_err_t esp_eth_start(esp_eth_handle_t hdl);
  135. /**
  136. * @brief Stop Ethernet driver
  137. *
  138. * @note This function does the oppsite operation of `esp_eth_start`.
  139. *
  140. * @param[in] hdl handle of Ethernet driver
  141. * @return
  142. * - ESP_OK: stop esp_eth driver successfully
  143. * - ESP_ERR_INVALID_ARG: stop esp_eth driver failed because of some invalid argument
  144. * - ESP_ERR_INVALID_STATE: stop esp_eth driver failed because driver has not started yet
  145. * - ESP_FAIL: stop esp_eth driver failed because some other error occurred
  146. */
  147. esp_err_t esp_eth_stop(esp_eth_handle_t hdl);
  148. /**
  149. * @brief Update Ethernet data input path (i.e. specify where to pass the input buffer)
  150. *
  151. * @note After install driver, Ethernet still don't know where to deliver the input buffer.
  152. * In fact, this API registers a callback function which get invoked when Ethernet received new packets.
  153. *
  154. * @param[in] hdl handle of Ethernet driver
  155. * @param[in] stack_input function pointer, which does the actual process on incoming packets
  156. * @param[in] priv private resource, which gets passed to `stack_input` callback without any modification
  157. * @return
  158. * - ESP_OK: update input path successfully
  159. * - ESP_ERR_INVALID_ARG: update input path failed because of some invalid argument
  160. * - ESP_FAIL: update input path failed because some other error occurred
  161. */
  162. esp_err_t esp_eth_update_input_path(
  163. esp_eth_handle_t hdl,
  164. esp_err_t (*stack_input)(esp_eth_handle_t hdl, uint8_t *buffer, uint32_t length, void *priv),
  165. void *priv);
  166. /**
  167. * @brief General Transmit
  168. *
  169. * @param[in] hdl: handle of Ethernet driver
  170. * @param[in] buf: buffer of the packet to transfer
  171. * @param[in] length: length of the buffer to transfer
  172. *
  173. * @return
  174. * - ESP_OK: transmit frame buffer successfully
  175. * - ESP_ERR_INVALID_ARG: transmit frame buffer failed because of some invalid argument
  176. * - ESP_FAIL: transmit frame buffer failed because some other error occurred
  177. */
  178. esp_err_t esp_eth_transmit(esp_eth_handle_t hdl, void *buf, uint32_t length);
  179. /**
  180. * @brief General Receive
  181. *
  182. * @param[in] hdl: handle of Ethernet driver
  183. * @param[out] buf: buffer to preserve the received packet
  184. * @param[out] length: length of the received packet
  185. *
  186. * @note Before this function got invoked, the value of "length" should set by user, equals the size of buffer.
  187. * After the function returned, the value of "length" means the real length of received data.
  188. *
  189. * @return
  190. * - ESP_OK: receive frame buffer successfully
  191. * - ESP_ERR_INVALID_ARG: receive frame buffer failed because of some invalid argument
  192. * - ESP_ERR_INVALID_SIZE: input buffer size is not enough to hold the incoming data.
  193. * in this case, value of returned "length" indicates the real size of incoming data.
  194. * - ESP_FAIL: receive frame buffer failed because some other error occurred
  195. */
  196. esp_err_t esp_eth_receive(esp_eth_handle_t hdl, uint8_t *buf, uint32_t *length);
  197. /**
  198. * @brief Misc IO function of Etherent driver
  199. *
  200. * @param[in] hdl: handle of Ethernet driver
  201. * @param[in] cmd: IO control command
  202. * @param[in] data: specificed data for command
  203. *
  204. * @return
  205. * - ESP_OK: process io command successfully
  206. * - ESP_ERR_INVALID_ARG: process io command failed because of some invalid argument
  207. * - ESP_FAIL: process io command failed because some other error occurred
  208. */
  209. esp_err_t esp_eth_ioctl(esp_eth_handle_t hdl, esp_eth_io_cmd_t cmd, void *data);
  210. /**
  211. * @brief Increase Ethernet driver reference
  212. * @note Ethernet driver handle can be obtained by os timer, netif, etc.
  213. * It's dangerous when thread A is using Ethernet but thread B uninstall the driver.
  214. * Using reference counter can prevent such risk, but care should be taken, when you obtain Ethernet driver,
  215. * this API must be invoked so that the driver won't be uninstalled during your using time.
  216. *
  217. *
  218. * @param[in] hdl: handle of Ethernet driver
  219. * @return
  220. * - ESP_OK: increase reference successfully
  221. * - ESP_ERR_INVALID_ARG: increase reference failed because of some invalid argument
  222. */
  223. esp_err_t esp_eth_increase_reference(esp_eth_handle_t hdl);
  224. /**
  225. * @brief Decrease Ethernet driver reference
  226. *
  227. * @param[in] hdl: handle of Ethernet driver
  228. * @return
  229. * - ESP_OK: increase reference successfully
  230. * - ESP_ERR_INVALID_ARG: increase reference failed because of some invalid argument
  231. */
  232. esp_err_t esp_eth_decrease_reference(esp_eth_handle_t hdl);
  233. #ifdef __cplusplus
  234. }
  235. #endif