esp_eth.h 9.1 KB

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