esp_eth.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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_CLOCK_GPIO0_IN = 0,
  27. ETH_CLOCK_GPIO16_OUT = 2,
  28. ETH_CLOCK_GPIO17_OUT = 3,
  29. } eth_clock_mode_t;
  30. typedef enum {
  31. ETH_SPEED_MODE_10M = 0,
  32. ETH_SPEED_MODE_100M,
  33. } eth_speed_mode_t;
  34. typedef enum {
  35. ETH_MODE_HALFDUPLEX = 0,
  36. ETH_MODE_FULLDUPLEX,
  37. } eth_duplex_mode_t;
  38. typedef enum {
  39. PHY0 = 0,
  40. PHY1,
  41. PHY2,
  42. PHY3,
  43. PHY4,
  44. PHY5,
  45. PHY6,
  46. PHY7,
  47. PHY8,
  48. PHY9,
  49. PHY10,
  50. PHY11,
  51. PHY12,
  52. PHY13,
  53. PHY14,
  54. PHY15,
  55. PHY16,
  56. PHY17,
  57. PHY18,
  58. PHY19,
  59. PHY20,
  60. PHY21,
  61. PHY22,
  62. PHY23,
  63. PHY24,
  64. PHY25,
  65. PHY26,
  66. PHY27,
  67. PHY28,
  68. PHY29,
  69. PHY30,
  70. PHY31,
  71. } eth_phy_base_t;
  72. typedef bool (*eth_phy_check_link_func)(void);
  73. typedef void (*eth_phy_check_init_func)(void);
  74. typedef eth_speed_mode_t (*eth_phy_get_speed_mode_func)(void);
  75. typedef eth_duplex_mode_t (*eth_phy_get_duplex_mode_func)(void);
  76. typedef void (*eth_phy_func)(void);
  77. typedef esp_err_t (*eth_tcpip_input_func)(void *buffer, uint16_t len, void *eb);
  78. typedef void (*eth_gpio_config_func)(void);
  79. typedef bool (*eth_phy_get_partner_pause_enable_func)(void);
  80. typedef void (*eth_phy_power_enable_func)(bool enable);
  81. /**
  82. * @brief ethernet configuration
  83. *
  84. */
  85. typedef struct {
  86. eth_phy_base_t phy_addr; /*!< phy base addr (0~31) */
  87. eth_mode_t mac_mode; /*!< mac mode only support RMII now */
  88. eth_clock_mode_t clock_mode; /*!< external/internal clock mode selecton */
  89. eth_tcpip_input_func tcpip_input; /*!< tcpip input func */
  90. eth_phy_func phy_init; /*!< phy init func */
  91. eth_phy_check_link_func phy_check_link; /*!< phy check link func */
  92. eth_phy_check_init_func phy_check_init; /*!< phy check init func */
  93. eth_phy_get_speed_mode_func phy_get_speed_mode; /*!< phy check init func */
  94. eth_phy_get_duplex_mode_func phy_get_duplex_mode; /*!< phy check init func */
  95. eth_gpio_config_func gpio_config; /*!< gpio config func */
  96. bool flow_ctrl_enable; /*!< flag of flow ctrl enable */
  97. eth_phy_get_partner_pause_enable_func phy_get_partner_pause_enable; /*!< get partner pause enable */
  98. eth_phy_power_enable_func phy_power_enable; /*!< enable or disable phy power */
  99. } eth_config_t;
  100. /**
  101. * @brief Init ethernet mac
  102. *
  103. * @note config can not be NULL,and phy chip must be suitable to phy init func.
  104. *
  105. * @param[in] config mac init data.
  106. *
  107. * @return
  108. * - ESP_OK
  109. * - ESP_FAIL
  110. */
  111. esp_err_t esp_eth_init(eth_config_t *config);
  112. /**
  113. * @brief Deinit ethernet mac
  114. *
  115. * @return
  116. * - ESP_OK
  117. * - ESP_FAIL
  118. * - ESP_ERR_INVALID_STATE
  119. */
  120. esp_err_t esp_eth_deinit(void);
  121. /**
  122. * @brief Init Ethernet mac driver only
  123. *
  124. * For the most part, you need not call this function directly. It gets called
  125. * from esp_eth_init().
  126. *
  127. * This function may be called, if you only need to initialize the Ethernet
  128. * driver without having to use the network stack on top.
  129. *
  130. * @note config can not be NULL,and phy chip must be suitable to phy init func.
  131. * @param[in] config mac init data.
  132. *
  133. * @return
  134. * - ESP_OK
  135. * - ESP_FAIL
  136. */
  137. esp_err_t esp_eth_init_internal(eth_config_t *config);
  138. /**
  139. * @brief Send packet from tcp/ip to mac
  140. *
  141. * @note buf can not be NULL,size must be less than 1580
  142. *
  143. * @param[in] buf: start address of packet data.
  144. *
  145. * @param[in] size: size (byte) of packet data.
  146. *
  147. * @return
  148. * - ESP_OK
  149. * - ESP_FAIL
  150. */
  151. esp_err_t esp_eth_tx(uint8_t *buf, uint16_t size);
  152. /**
  153. * @brief Enable ethernet interface
  154. *
  155. * @note Shout be called after esp_eth_init
  156. *
  157. * @return
  158. * - ESP_OK
  159. * - ESP_FAIL
  160. */
  161. esp_err_t esp_eth_enable(void);
  162. /**
  163. * @brief Disable ethernet interface
  164. *
  165. * @note Shout be called after esp_eth_init
  166. *
  167. * @return
  168. * - ESP_OK
  169. * - ESP_FAIL
  170. */
  171. esp_err_t esp_eth_disable(void);
  172. /**
  173. * @brief Get mac addr
  174. *
  175. * @note mac addr must be a valid unicast address
  176. *
  177. * @param[out] mac: start address of mac address.
  178. */
  179. void esp_eth_get_mac(uint8_t mac[6]);
  180. /**
  181. * @brief Read phy reg with smi interface.
  182. *
  183. * @note phy base addr must be right.
  184. *
  185. * @param[in] reg_num: phy reg num.
  186. *
  187. * @param[in] value: value which write to phy reg.
  188. */
  189. void esp_eth_smi_write(uint32_t reg_num, uint16_t value);
  190. /**
  191. * @brief Read phy reg with smi interface.
  192. *
  193. * @note phy base addr must be right.
  194. *
  195. * @param[in] reg_num: phy reg num.
  196. *
  197. * @return value what read from phy reg
  198. */
  199. uint16_t esp_eth_smi_read(uint32_t reg_num);
  200. /**
  201. * @brief Continuously read a PHY register over SMI interface, wait until the register has the desired value.
  202. *
  203. * @note PHY base address must be right.
  204. *
  205. * @param reg_num: PHY register number
  206. * @param value: Value to wait for (masked with value_mask)
  207. * @param value_mask: Mask of bits to match in the register.
  208. * @param timeout_ms: Timeout to wait for this value (milliseconds). 0 means never timeout.
  209. *
  210. * @return ESP_OK if desired value matches, ESP_ERR_TIMEOUT if timed out.
  211. */
  212. esp_err_t esp_eth_smi_wait_value(uint32_t reg_num, uint16_t value, uint16_t value_mask, int timeout_ms);
  213. /**
  214. * @brief Continuously read a PHY register over SMI interface, wait until the register has all bits in a mask set.
  215. *
  216. * @note PHY base address must be right.
  217. *
  218. * @param reg_num: PHY register number
  219. * @param value_mask: Value mask to wait for (all bits in this mask must be set)
  220. * @param timeout_ms: Timeout to wait for this value (milliseconds). 0 means never timeout.
  221. *
  222. * @return ESP_OK if desired value matches, ESP_ERR_TIMEOUT if timed out.
  223. */
  224. static inline esp_err_t esp_eth_smi_wait_set(uint32_t reg_num, uint16_t value_mask, int timeout_ms)
  225. {
  226. return esp_eth_smi_wait_value(reg_num, value_mask, value_mask, timeout_ms);
  227. }
  228. /**
  229. * @brief Free emac rx buf.
  230. *
  231. * @note buf can not be null,and it is tcpip input buf.
  232. *
  233. * @param[in] buf: start address of recevie packet data.
  234. *
  235. */
  236. void esp_eth_free_rx_buf(void *buf);
  237. /**
  238. * @brief Get mac of ethernet interface.
  239. *
  240. * @param[out] mac: store mac of the interface.
  241. *
  242. */
  243. void esp_eth_get_mac(uint8_t mac[6]);
  244. /**
  245. * @brief Set mac of ethernet interface.
  246. *
  247. * @note user can call this function after emac_init,and the new mac address will be enabled after emac_enable.
  248. *
  249. * @param[in] mac: the Mac address.
  250. *
  251. * @return
  252. * - ESP_OK: succeed
  253. * - ESP_ERR_INVALID_MAC: invalid mac address
  254. */
  255. esp_err_t esp_eth_set_mac(const uint8_t mac[6]);
  256. #ifdef __cplusplus
  257. }
  258. #endif
  259. #endif