esp_eth.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include "esp_eth_com.h"
  19. #include "esp_eth_mac.h"
  20. #include "esp_eth_phy.h"
  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);
  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 Start ethernet driver **ONLY** in standalone mode, i.e. without TCP/IP stack
  108. *
  109. * Note that ethernet driver is typically started as soon as it is attached to esp-netif.
  110. * This API should only be called if ethernet is used separately without esp-netif, for example
  111. * when esp_eth_config_t.stack_input is not NULL.
  112. *
  113. * @param[in] eth_handle handle of Ethernet driver
  114. *
  115. * @return
  116. * - ESP_OK: starts ethernet driver
  117. * - ESP_ERR_INVALID_STATE: if event loop hasn't been initialized
  118. */
  119. esp_err_t esp_eth_driver_start(esp_eth_handle_t eth_handle);
  120. /**
  121. * @brief Uninstall Ethernet driver
  122. *
  123. * @param[in] hdl: handle of Ethernet driver
  124. *
  125. * @return
  126. * - ESP_OK: uninstall esp_eth driver successfully
  127. * - ESP_ERR_INVALID_ARG: uninstall esp_eth driver failed because of some invalid argument
  128. * - ESP_FAIL: uninstall esp_eth driver failed because some other error occurred
  129. */
  130. esp_err_t esp_eth_driver_uninstall(esp_eth_handle_t hdl);
  131. /**
  132. * @brief General Transmit
  133. *
  134. * @param[in] hdl: handle of Ethernet driver
  135. * @param[in] buf: buffer of the packet to transfer
  136. * @param[in] length: length of the buffer to transfer
  137. *
  138. * @return
  139. * - ESP_OK: transmit frame buffer successfully
  140. * - ESP_ERR_INVALID_ARG: transmit frame buffer failed because of some invalid argument
  141. * - ESP_FAIL: transmit frame buffer failed because some other error occurred
  142. */
  143. esp_err_t esp_eth_transmit(void* hdl, void *buf, uint32_t length);
  144. /**
  145. * @brief General Receive
  146. *
  147. * @param[in] hdl: handle of Ethernet driver
  148. * @param[out] buf: buffer to preserve the received packet
  149. * @param[out] length: length of the received packet
  150. *
  151. * @note Before this function got invoked, the value of "length" should set by user, equals the size of buffer.
  152. * After the function returned, the value of "length" means the real length of received data.
  153. *
  154. * @return
  155. * - ESP_OK: receive frame buffer successfully
  156. * - ESP_ERR_INVALID_ARG: receive frame buffer failed because of some invalid argument
  157. * - ESP_ERR_INVALID_SIZE: input buffer size is not enough to hold the incoming data.
  158. * in this case, value of returned "length" indicates the real size of incoming data.
  159. * - ESP_FAIL: receive frame buffer failed because some other error occurred
  160. */
  161. esp_err_t esp_eth_receive(esp_eth_handle_t hdl, uint8_t *buf, uint32_t *length);
  162. /**
  163. * @brief Misc IO function of Etherent driver
  164. *
  165. * @param[in] hdl: handle of Ethernet driver
  166. * @param[in] cmd: IO control command
  167. * @param[in] data: specificed data for command
  168. *
  169. * @return
  170. * - ESP_OK: process io command successfully
  171. * - ESP_ERR_INVALID_ARG: process io command failed because of some invalid argument
  172. * - ESP_FAIL: process io command failed because some other error occurred
  173. */
  174. esp_err_t esp_eth_ioctl(esp_eth_handle_t hdl, esp_eth_io_cmd_t cmd, void *data);
  175. /**
  176. * @brief Register default ethernet handlers
  177. *
  178. * @param[in] esp_netif esp network interface handle created for this driver
  179. * (note: appropriate ethernet handle not yet properly initialized when setting up
  180. * default handlers)
  181. */
  182. esp_err_t esp_eth_set_default_handlers(void* esp_netif);
  183. /**
  184. * @brief Unregister default ethernet handlers
  185. *
  186. * @param[in] esp_netif esp network interface handle created for this driver
  187. */
  188. esp_err_t esp_eth_clear_default_handlers(void* esp_netif);
  189. #ifdef __cplusplus
  190. }
  191. #endif