esp_eth.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. } eth_config_t;
  121. /**
  122. * @brief Init ethernet mac
  123. *
  124. * @note config can not be NULL, and phy chip must be suitable to phy init func.
  125. *
  126. * @param[in] config mac init data.
  127. *
  128. * @return
  129. * - ESP_OK
  130. * - ESP_FAIL
  131. */
  132. esp_err_t esp_eth_init(eth_config_t *config);
  133. /**
  134. * @brief Deinit ethernet mac
  135. *
  136. * @return
  137. * - ESP_OK
  138. * - ESP_FAIL
  139. * - ESP_ERR_INVALID_STATE
  140. */
  141. esp_err_t esp_eth_deinit(void);
  142. /**
  143. * @brief Init Ethernet mac driver only
  144. *
  145. * For the most part, you need not call this function directly. It gets called
  146. * from esp_eth_init().
  147. *
  148. * This function may be called, if you only need to initialize the Ethernet
  149. * driver without having to use the network stack on top.
  150. *
  151. * @note config can not be NULL, and phy chip must be suitable to phy init func.
  152. * @param[in] config mac init data.
  153. *
  154. * @return
  155. * - ESP_OK
  156. * - ESP_FAIL
  157. */
  158. esp_err_t esp_eth_init_internal(eth_config_t *config);
  159. /**
  160. * @brief Send packet from tcp/ip to mac
  161. *
  162. * @note buf can not be NULL, size must be less than 1580
  163. *
  164. * @param[in] buf: start address of packet data.
  165. *
  166. * @param[in] size: size (byte) of packet data.
  167. *
  168. * @return
  169. * - ESP_OK
  170. * - ESP_FAIL
  171. */
  172. esp_err_t esp_eth_tx(uint8_t *buf, uint16_t size);
  173. /**
  174. * @brief Enable ethernet interface
  175. *
  176. * @note Should be called after esp_eth_init
  177. *
  178. * @return
  179. * - ESP_OK
  180. * - ESP_FAIL
  181. */
  182. esp_err_t esp_eth_enable(void);
  183. /**
  184. * @brief Disable ethernet interface
  185. *
  186. * @note Should be called after esp_eth_init
  187. *
  188. * @return
  189. * - ESP_OK
  190. * - ESP_FAIL
  191. */
  192. esp_err_t esp_eth_disable(void);
  193. /**
  194. * @brief Get mac addr
  195. *
  196. * @note mac addr must be a valid unicast address
  197. *
  198. * @param[out] mac: start address of mac address.
  199. */
  200. void esp_eth_get_mac(uint8_t mac[6]);
  201. /**
  202. * @brief Write PHY reg with SMI interface.
  203. *
  204. * @note PHY base addr must be right.
  205. *
  206. * @param[in] reg_num: PHY reg num.
  207. *
  208. * @param[in] value: value which is written to PHY reg.
  209. */
  210. void esp_eth_smi_write(uint32_t reg_num, uint16_t value);
  211. /**
  212. * @brief Read PHY reg with SMI interface.
  213. *
  214. * @note PHY base addr must be right.
  215. *
  216. * @param[in] reg_num: PHY reg num.
  217. *
  218. * @return value that is read from PHY reg
  219. */
  220. uint16_t esp_eth_smi_read(uint32_t reg_num);
  221. /**
  222. * @brief Continuously read a PHY register over SMI interface, wait until the register has the desired value.
  223. *
  224. * @note PHY base address must be right.
  225. *
  226. * @param reg_num: PHY register number
  227. * @param value: Value to wait for (masked with value_mask)
  228. * @param value_mask: Mask of bits to match in the register.
  229. * @param timeout_ms: Timeout to wait for this value (milliseconds). 0 means never timeout.
  230. *
  231. * @return ESP_OK if desired value matches, ESP_ERR_TIMEOUT if timed out.
  232. */
  233. esp_err_t esp_eth_smi_wait_value(uint32_t reg_num, uint16_t value, uint16_t value_mask, int timeout_ms);
  234. /**
  235. * @brief Continuously read a PHY register over SMI interface, wait until the register has all bits in a mask set.
  236. *
  237. * @note PHY base address must be right.
  238. *
  239. * @param reg_num: PHY register number
  240. * @param value_mask: Value mask to wait for (all bits in this mask must be set)
  241. * @param timeout_ms: Timeout to wait for this value (milliseconds). 0 means never timeout.
  242. *
  243. * @return ESP_OK if desired value matches, ESP_ERR_TIMEOUT if timed out.
  244. */
  245. static inline esp_err_t esp_eth_smi_wait_set(uint32_t reg_num, uint16_t value_mask, int timeout_ms)
  246. {
  247. return esp_eth_smi_wait_value(reg_num, value_mask, value_mask, timeout_ms);
  248. }
  249. /**
  250. * @brief Free emac rx buf.
  251. *
  252. * @note buf can not be null, and it is tcpip input buf.
  253. *
  254. * @param[in] buf: start address of received packet data.
  255. *
  256. */
  257. void esp_eth_free_rx_buf(void *buf);
  258. /**
  259. * @brief Set mac of ethernet interface.
  260. *
  261. * @note user can call this function after emac_init, and the new mac address will be enabled after emac_enable.
  262. *
  263. * @param[in] mac: the Mac address.
  264. *
  265. * @return
  266. * - ESP_OK: succeed
  267. * - ESP_ERR_INVALID_MAC: invalid mac address
  268. */
  269. esp_err_t esp_eth_set_mac(const uint8_t mac[6]);
  270. /**
  271. * @brief Get Ethernet link speed
  272. *
  273. * @return eth_speed_mode_t ETH_SPEED_MODE_10M when link speed is 10Mbps
  274. * ETH_SPEED_MODE_100M when link speed is 100Mbps
  275. */
  276. eth_speed_mode_t esp_eth_get_speed(void);
  277. #ifdef __cplusplus
  278. }
  279. #endif
  280. #endif