esp_eth.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Copyright 2015-2016 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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef __ESP_ETH_H__
  14. #define __ESP_ETH_H__
  15. #include <stdbool.h>
  16. #include <stdint.h>
  17. #include "esp_err.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. typedef enum {
  22. ETH_MODE_RMII = 0,
  23. ETH_MODE_MII,
  24. } eth_mode_t;
  25. typedef enum {
  26. ETH_SPEED_MODE_10M = 0,
  27. ETH_SPEED_MODE_100M,
  28. } eth_speed_mode_t;
  29. typedef enum {
  30. ETH_MODE_HALFDUPLEX = 0,
  31. ETH_MODE_FULLDUPLEX,
  32. } eth_duplex_mode_t;
  33. typedef enum {
  34. PHY0 = 0,
  35. PHY1,
  36. PHY2,
  37. PHY3,
  38. PHY4,
  39. PHY5,
  40. PHY6,
  41. PHY7,
  42. PHY8,
  43. PHY9,
  44. PHY10,
  45. PHY11,
  46. PHY12,
  47. PHY13,
  48. PHY14,
  49. PHY15,
  50. PHY16,
  51. PHY17,
  52. PHY18,
  53. PHY19,
  54. PHY20,
  55. PHY21,
  56. PHY22,
  57. PHY23,
  58. PHY24,
  59. PHY25,
  60. PHY26,
  61. PHY27,
  62. PHY28,
  63. PHY29,
  64. PHY30,
  65. PHY31,
  66. } eth_phy_base_t;
  67. typedef bool (*eth_phy_check_link_func)(void);
  68. typedef void (*eth_phy_check_init_func)(void);
  69. typedef eth_speed_mode_t (*eth_phy_get_speed_mode_func)(void);
  70. typedef eth_duplex_mode_t (*eth_phy_get_duplex_mode_func)(void);
  71. typedef void (*eth_phy_func)(void);
  72. typedef esp_err_t (*eth_tcpip_input_func)(void *buffer, uint16_t len, void *eb);
  73. typedef void (*eth_gpio_config_func)(void);
  74. typedef bool (*eth_phy_get_partner_pause_enable_func)(void);
  75. typedef void (*eth_phy_power_enable_func)(bool enable);
  76. /**
  77. * @brief ethernet configuration
  78. *
  79. */
  80. typedef struct {
  81. eth_phy_base_t phy_addr; /*!< phy base addr (0~31) */
  82. eth_mode_t mac_mode; /*!< mac mode only support RMII now */
  83. eth_tcpip_input_func tcpip_input; /*!< tcpip input func */
  84. eth_phy_func phy_init; /*!< phy init func */
  85. eth_phy_check_link_func phy_check_link; /*!< phy check link func */
  86. eth_phy_check_init_func phy_check_init; /*!< phy check init func */
  87. eth_phy_get_speed_mode_func phy_get_speed_mode; /*!< phy check init func */
  88. eth_phy_get_duplex_mode_func phy_get_duplex_mode; /*!< phy check init func */
  89. eth_gpio_config_func gpio_config; /*!< gpio config func */
  90. bool flow_ctrl_enable; /*!< flag of flow ctrl enable */
  91. eth_phy_get_partner_pause_enable_func phy_get_partner_pause_enable; /*!< get partner pause enable */
  92. eth_phy_power_enable_func phy_power_enable; /*!< enable or disable phy power */
  93. } eth_config_t;
  94. /**
  95. * @brief Init ethernet mac
  96. *
  97. * @note config can not be NULL,and phy chip must be suitable to phy init func.
  98. *
  99. * @param[in] config mac init data.
  100. *
  101. * @return
  102. * - ESP_OK
  103. * - ESP_FAIL
  104. */
  105. esp_err_t esp_eth_init(eth_config_t *config);
  106. /**
  107. * @brief Init Ethernet mac driver only
  108. *
  109. * For the most part, you need not call this function directly. It gets called
  110. * from esp_eth_init().
  111. *
  112. * This function may be called, if you only need to initialize the Ethernet
  113. * driver without having to use the network stack on top.
  114. *
  115. * @note config can not be NULL,and phy chip must be suitable to phy init func.
  116. * @param[in] config mac init data.
  117. *
  118. * @return
  119. * - ESP_OK
  120. * - ESP_FAIL
  121. */
  122. esp_err_t esp_eth_init_internal(eth_config_t *config);
  123. /**
  124. * @brief Send packet from tcp/ip to mac
  125. *
  126. * @note buf can not be NULL,size must be less than 1580
  127. *
  128. * @param[in] buf: start address of packet data.
  129. *
  130. * @param[in] size: size (byte) of packet data.
  131. *
  132. * @return
  133. * - ESP_OK
  134. * - ESP_FAIL
  135. */
  136. esp_err_t esp_eth_tx(uint8_t *buf, uint16_t size);
  137. /**
  138. * @brief Enable ethernet interface
  139. *
  140. * @note Shout be called after esp_eth_init
  141. *
  142. * @return
  143. * - ESP_OK
  144. * - ESP_FAIL
  145. */
  146. esp_err_t esp_eth_enable(void);
  147. /**
  148. * @brief Disable ethernet interface
  149. *
  150. * @note Shout be called after esp_eth_init
  151. *
  152. * @return
  153. * - ESP_OK
  154. * - ESP_FAIL
  155. */
  156. esp_err_t esp_eth_disable(void);
  157. /**
  158. * @brief Get mac addr
  159. *
  160. * @note mac addr must be a valid unicast address
  161. *
  162. * @param[out] mac: start address of mac address.
  163. */
  164. void esp_eth_get_mac(uint8_t mac[6]);
  165. /**
  166. * @brief Read phy reg with smi interface.
  167. *
  168. * @note phy base addr must be right.
  169. *
  170. * @param[in] reg_num: phy reg num.
  171. *
  172. * @param[in] value: value which write to phy reg.
  173. */
  174. void esp_eth_smi_write(uint32_t reg_num, uint16_t value);
  175. /**
  176. * @brief Read phy reg with smi interface.
  177. *
  178. * @note phy base addr must be right.
  179. *
  180. * @param[in] reg_num: phy reg num.
  181. *
  182. * @return value what read from phy reg
  183. */
  184. uint16_t esp_eth_smi_read(uint32_t reg_num);
  185. /**
  186. * @brief Continuously read a PHY register over SMI interface, wait until the register has the desired value.
  187. *
  188. * @note PHY base address must be right.
  189. *
  190. * @param reg_num: PHY register number
  191. * @param value: Value to wait for (masked with value_mask)
  192. * @param value_mask: Mask of bits to match in the register.
  193. * @param timeout_ms: Timeout to wait for this value (milliseconds). 0 means never timeout.
  194. *
  195. * @return ESP_OK if desired value matches, ESP_ERR_TIMEOUT if timed out.
  196. */
  197. esp_err_t esp_eth_smi_wait_value(uint32_t reg_num, uint16_t value, uint16_t value_mask, int timeout_ms);
  198. /**
  199. * @brief Continuously read a PHY register over SMI interface, wait until the register has all bits in a mask set.
  200. *
  201. * @note PHY base address must be right.
  202. *
  203. * @param reg_num: PHY register number
  204. * @param value_mask: Value mask to wait for (all bits in this mask must be set)
  205. * @param timeout_ms: Timeout to wait for this value (milliseconds). 0 means never timeout.
  206. *
  207. * @return ESP_OK if desired value matches, ESP_ERR_TIMEOUT if timed out.
  208. */
  209. static inline esp_err_t esp_eth_smi_wait_set(uint32_t reg_num, uint16_t value_mask, int timeout_ms) {
  210. return esp_eth_smi_wait_value(reg_num, value_mask, value_mask, timeout_ms);
  211. }
  212. /**
  213. * @brief Free emac rx buf.
  214. *
  215. * @note buf can not be null,and it is tcpip input buf.
  216. *
  217. * @param[in] buf: start address of recevie packet data.
  218. *
  219. */
  220. void esp_eth_free_rx_buf(void *buf);
  221. #ifdef __cplusplus
  222. }
  223. #endif
  224. #endif