esp_eth.h 9.5 KB

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