esp_eth.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_SPEED_MODE_10M = 0,
  27. ETH_SPEED_MODE_100M,
  28. } eth_speed_mode_t;
  29. typedef enum {
  30. ETH_MODE_HALFDUPLEX = 0,
  31. ETH_MODE_FULLDUPLEX,
  32. } eth_duplex_mode_t;
  33. typedef enum {
  34. PHY0 = 0,
  35. PHY1,
  36. PHY2,
  37. PHY3,
  38. PHY4,
  39. PHY5,
  40. PHY6,
  41. PHY7,
  42. PHY8,
  43. PHY9,
  44. PHY10,
  45. PHY11,
  46. PHY12,
  47. PHY13,
  48. PHY14,
  49. PHY15,
  50. PHY16,
  51. PHY17,
  52. PHY18,
  53. PHY19,
  54. PHY20,
  55. PHY21,
  56. PHY22,
  57. PHY23,
  58. PHY24,
  59. PHY25,
  60. PHY26,
  61. PHY27,
  62. PHY28,
  63. PHY29,
  64. PHY30,
  65. PHY31,
  66. } eth_phy_base_t;
  67. typedef bool (*eth_phy_check_link_func)(void);
  68. typedef void (*eth_phy_check_init_func)(void);
  69. typedef eth_speed_mode_t (*eth_phy_get_speed_mode_func)(void);
  70. typedef eth_duplex_mode_t (*eth_phy_get_duplex_mode_func)(void);
  71. typedef void (*eth_phy_func)(void);
  72. typedef esp_err_t (*eth_tcpip_input_func)(void *buffer, uint16_t len, void *eb);
  73. typedef void (*eth_gpio_config_func)(void);
  74. typedef bool (*eth_phy_get_partner_pause_enable_func)(void);
  75. typedef void (*eth_phy_power_enable_func)(bool enable);
  76. /**
  77. * @brief ethernet configuration
  78. *
  79. */
  80. typedef struct {
  81. eth_phy_base_t phy_addr; /*!< phy base addr (0~31) */
  82. eth_mode_t mac_mode; /*!< mac mode only support RMII now */
  83. eth_tcpip_input_func tcpip_input; /*!< tcpip input func */
  84. eth_phy_func phy_init; /*!< phy init func */
  85. eth_phy_check_link_func phy_check_link; /*!< phy check link func */
  86. eth_phy_check_init_func phy_check_init; /*!< phy check init func */
  87. eth_phy_get_speed_mode_func phy_get_speed_mode; /*!< phy check init func */
  88. eth_phy_get_duplex_mode_func phy_get_duplex_mode; /*!< phy check init func */
  89. eth_gpio_config_func gpio_config; /*!< gpio config func */
  90. bool flow_ctrl_enable; /*!< flag of flow ctrl enable */
  91. eth_phy_get_partner_pause_enable_func phy_get_partner_pause_enable; /*!< get partner pause enable */
  92. eth_phy_power_enable_func phy_power_enable; /*!< enable or disable phy power */
  93. } eth_config_t;
  94. /**
  95. * @brief Init ethernet mac
  96. *
  97. * @note config can not be NULL,and phy chip must be suitable to phy init func.
  98. *
  99. * @param[in] config mac init data.
  100. *
  101. * @return
  102. * - ESP_OK
  103. * - ESP_FAIL
  104. */
  105. esp_err_t esp_eth_init(eth_config_t *config);
  106. /**
  107. * @brief Send packet from tcp/ip to mac
  108. *
  109. * @note buf can not be NULL,size must be less than 1580
  110. *
  111. * @param[in] buf: start address of packet data.
  112. *
  113. * @param[in] size: size (byte) of packet data.
  114. *
  115. * @return
  116. * - ESP_OK
  117. * - ESP_FAIL
  118. */
  119. esp_err_t esp_eth_tx(uint8_t *buf, uint16_t size);
  120. /**
  121. * @brief Enable ethernet interface
  122. *
  123. * @note Shout be called after esp_eth_init
  124. *
  125. * @return
  126. * - ESP_OK
  127. * - ESP_FAIL
  128. */
  129. esp_err_t esp_eth_enable(void);
  130. /**
  131. * @brief Disable ethernet interface
  132. *
  133. * @note Shout be called after esp_eth_init
  134. *
  135. * @return
  136. * - ESP_OK
  137. * - ESP_FAIL
  138. */
  139. esp_err_t esp_eth_disable(void);
  140. /**
  141. * @brief Get mac addr
  142. *
  143. * @note mac addr must be a valid unicast address
  144. *
  145. * @param[out] mac: start address of mac address.
  146. */
  147. void esp_eth_get_mac(uint8_t mac[6]);
  148. /**
  149. * @brief Read phy reg with smi interface.
  150. *
  151. * @note phy base addr must be right.
  152. *
  153. * @param[in] reg_num: phy reg num.
  154. *
  155. * @param[in] value: value which write to phy reg.
  156. */
  157. void esp_eth_smi_write(uint32_t reg_num, uint16_t value);
  158. /**
  159. * @brief Read phy reg with smi interface.
  160. *
  161. * @note phy base addr must be right.
  162. *
  163. * @param[in] reg_num: phy reg num.
  164. *
  165. * @return value what read from phy reg
  166. */
  167. uint16_t esp_eth_smi_read(uint32_t reg_num);
  168. /**
  169. * @brief Continuously read a PHY register over SMI interface, wait until the register has the desired value.
  170. *
  171. * @note PHY base address must be right.
  172. *
  173. * @param reg_num: PHY register number
  174. * @param value: Value to wait for (masked with value_mask)
  175. * @param value_mask: Mask of bits to match in the register.
  176. * @param timeout_ms: Timeout to wait for this value (milliseconds). 0 means never timeout.
  177. *
  178. * @return ESP_OK if desired value matches, ESP_ERR_TIMEOUT if timed out.
  179. */
  180. esp_err_t esp_eth_smi_wait_value(uint32_t reg_num, uint16_t value, uint16_t value_mask, int timeout_ms);
  181. /**
  182. * @brief Continuously read a PHY register over SMI interface, wait until the register has all bits in a mask set.
  183. *
  184. * @note PHY base address must be right.
  185. *
  186. * @param reg_num: PHY register number
  187. * @param value_mask: Value mask to wait for (all bits in this mask must be set)
  188. * @param timeout_ms: Timeout to wait for this value (milliseconds). 0 means never timeout.
  189. *
  190. * @return ESP_OK if desired value matches, ESP_ERR_TIMEOUT if timed out.
  191. */
  192. static inline esp_err_t esp_eth_smi_wait_set(uint32_t reg_num, uint16_t value_mask, int timeout_ms) {
  193. return esp_eth_smi_wait_value(reg_num, value_mask, value_mask, timeout_ms);
  194. }
  195. /**
  196. * @brief Free emac rx buf.
  197. *
  198. * @note buf can not be null,and it is tcpip input buf.
  199. *
  200. * @param[in] buf: start address of recevie packet data.
  201. *
  202. */
  203. void esp_eth_free_rx_buf(void *buf);
  204. #ifdef __cplusplus
  205. }
  206. #endif
  207. #endif