esp_eth.h 7.1 KB

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