esp_eth.h 7.7 KB

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