esp_eth_mac.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // Copyright 2019 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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <stdbool.h>
  16. #include "esp_eth_com.h"
  17. #include "sdkconfig.h"
  18. #if CONFIG_ETH_USE_SPI_ETHERNET
  19. #include "driver/spi_master.h"
  20. #endif
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /**
  25. * @brief Ethernet MAC
  26. *
  27. */
  28. typedef struct esp_eth_mac_s esp_eth_mac_t;
  29. /**
  30. * @brief Ethernet MAC
  31. *
  32. */
  33. struct esp_eth_mac_s {
  34. /**
  35. * @brief Set mediator for Ethernet MAC
  36. *
  37. * @param[in] mac: Ethernet MAC instance
  38. * @param[in] eth: Ethernet mediator
  39. *
  40. * @return
  41. * - ESP_OK: set mediator for Ethernet MAC successfully
  42. * - ESP_ERR_INVALID_ARG: set mediator for Ethernet MAC failed because of invalid argument
  43. *
  44. */
  45. esp_err_t (*set_mediator)(esp_eth_mac_t *mac, esp_eth_mediator_t *eth);
  46. /**
  47. * @brief Initialize Ethernet MAC
  48. *
  49. * @param[in] mac: Ethernet MAC instance
  50. *
  51. * @return
  52. * - ESP_OK: initialize Ethernet MAC successfully
  53. * - ESP_ERR_TIMEOUT: initialize Ethernet MAC failed because of timeout
  54. * - ESP_FAIL: initialize Ethernet MAC failed because some other error occurred
  55. *
  56. */
  57. esp_err_t (*init)(esp_eth_mac_t *mac);
  58. /**
  59. * @brief Deinitialize Ethernet MAC
  60. *
  61. * @param[in] mac: Ethernet MAC instance
  62. *
  63. * @return
  64. * - ESP_OK: deinitialize Ethernet MAC successfully
  65. * - ESP_FAIL: deinitialize Ethernet MAC failed because some error occurred
  66. *
  67. */
  68. esp_err_t (*deinit)(esp_eth_mac_t *mac);
  69. /**
  70. * @brief Start Ethernet MAC
  71. *
  72. * @param[in] mac: Ethernet MAC instance
  73. *
  74. * @return
  75. * - ESP_OK: start Ethernet MAC successfully
  76. * - ESP_FAIL: start Ethernet MAC failed because some other error occurred
  77. *
  78. */
  79. esp_err_t (*start)(esp_eth_mac_t *mac);
  80. /**
  81. * @brief Stop Ethernet MAC
  82. *
  83. * @param[in] mac: Ethernet MAC instance
  84. *
  85. * @return
  86. * - ESP_OK: stop Ethernet MAC successfully
  87. * - ESP_FAIL: stop Ethernet MAC failed because some error occurred
  88. *
  89. */
  90. esp_err_t (*stop)(esp_eth_mac_t *mac);
  91. /**
  92. * @brief Transmit packet from Ethernet MAC
  93. *
  94. * @param[in] mac: Ethernet MAC instance
  95. * @param[in] buf: packet buffer to transmit
  96. * @param[in] length: length of packet
  97. *
  98. * @return
  99. * - ESP_OK: transmit packet successfully
  100. * - ESP_ERR_INVALID_ARG: transmit packet failed because of invalid argument
  101. * - ESP_ERR_INVALID_STATE: transmit packet failed because of wrong state of MAC
  102. * - ESP_FAIL: transmit packet failed because some other error occurred
  103. *
  104. */
  105. esp_err_t (*transmit)(esp_eth_mac_t *mac, uint8_t *buf, uint32_t length);
  106. /**
  107. * @brief Receive packet from Ethernet MAC
  108. *
  109. * @param[in] mac: Ethernet MAC instance
  110. * @param[out] buf: packet buffer which will preserve the received frame
  111. * @param[out] length: length of the received packet
  112. *
  113. * @note Memory of buf is allocated in the Layer2, make sure it get free after process.
  114. *
  115. * @return
  116. * - ESP_OK: receive packet successfully
  117. * - ESP_ERR_INVALID_ARG: receive packet failed because of invalid argument
  118. * - ESP_FAIL: receive packet failed because some other error occurred
  119. *
  120. */
  121. esp_err_t (*receive)(esp_eth_mac_t *mac, uint8_t *buf, uint32_t *length);
  122. /**
  123. * @brief Read PHY register
  124. *
  125. * @param[in] mac: Ethernet MAC instance
  126. * @param[in] phy_addr: PHY chip address (0~31)
  127. * @param[in] phy_reg: PHY register index code
  128. * @param[out] reg_value: PHY register value
  129. *
  130. * @return
  131. * - ESP_OK: read PHY register successfully
  132. * - ESP_ERR_INVALID_ARG: read PHY register failed because of invalid argument
  133. * - ESP_ERR_INVALID_STATE: read PHY register failed because of wrong state of MAC
  134. * - ESP_ERR_TIMEOUT: read PHY register failed because of timeout
  135. * - ESP_FAIL: read PHY register failed because some other error occurred
  136. *
  137. */
  138. esp_err_t (*read_phy_reg)(esp_eth_mac_t *mac, uint32_t phy_addr, uint32_t phy_reg, uint32_t *reg_value);
  139. /**
  140. * @brief Write PHY register
  141. *
  142. * @param[in] mac: Ethernet MAC instance
  143. * @param[in] phy_addr: PHY chip address (0~31)
  144. * @param[in] phy_reg: PHY register index code
  145. * @param[in] reg_value: PHY register value
  146. *
  147. * @return
  148. * - ESP_OK: write PHY register successfully
  149. * - ESP_ERR_INVALID_STATE: write PHY register failed because of wrong state of MAC
  150. * - ESP_ERR_TIMEOUT: write PHY register failed because of timeout
  151. * - ESP_FAIL: write PHY register failed because some other error occurred
  152. *
  153. */
  154. esp_err_t (*write_phy_reg)(esp_eth_mac_t *mac, uint32_t phy_addr, uint32_t phy_reg, uint32_t reg_value);
  155. /**
  156. * @brief Set MAC address
  157. *
  158. * @param[in] mac: Ethernet MAC instance
  159. * @param[in] addr: MAC address
  160. *
  161. * @return
  162. * - ESP_OK: set MAC address successfully
  163. * - ESP_ERR_INVALID_ARG: set MAC address failed because of invalid argument
  164. * - ESP_FAIL: set MAC address failed because some other error occurred
  165. *
  166. */
  167. esp_err_t (*set_addr)(esp_eth_mac_t *mac, uint8_t *addr);
  168. /**
  169. * @brief Get MAC address
  170. *
  171. * @param[in] mac: Ethernet MAC instance
  172. * @param[out] addr: MAC address
  173. *
  174. * @return
  175. * - ESP_OK: get MAC address successfully
  176. * - ESP_ERR_INVALID_ARG: get MAC address failed because of invalid argument
  177. * - ESP_FAIL: get MAC address failed because some other error occurred
  178. *
  179. */
  180. esp_err_t (*get_addr)(esp_eth_mac_t *mac, uint8_t *addr);
  181. /**
  182. * @brief Set speed of MAC
  183. *
  184. * @param[in] ma:c Ethernet MAC instance
  185. * @param[in] speed: MAC speed
  186. *
  187. * @return
  188. * - ESP_OK: set MAC speed successfully
  189. * - ESP_ERR_INVALID_ARG: set MAC speed failed because of invalid argument
  190. * - ESP_FAIL: set MAC speed failed because some other error occurred
  191. *
  192. */
  193. esp_err_t (*set_speed)(esp_eth_mac_t *mac, eth_speed_t speed);
  194. /**
  195. * @brief Set duplex mode of MAC
  196. *
  197. * @param[in] mac: Ethernet MAC instance
  198. * @param[in] duplex: MAC duplex
  199. *
  200. * @return
  201. * - ESP_OK: set MAC duplex mode successfully
  202. * - ESP_ERR_INVALID_ARG: set MAC duplex failed because of invalid argument
  203. * - ESP_FAIL: set MAC duplex failed because some other error occurred
  204. *
  205. */
  206. esp_err_t (*set_duplex)(esp_eth_mac_t *mac, eth_duplex_t duplex);
  207. /**
  208. * @brief Set link status of MAC
  209. *
  210. * @param[in] mac: Ethernet MAC instance
  211. * @param[in] link: Link status
  212. *
  213. * @return
  214. * - ESP_OK: set link status successfully
  215. * - ESP_ERR_INVALID_ARG: set link status failed because of invalid argument
  216. * - ESP_FAIL: set link status failed because some other error occurred
  217. *
  218. */
  219. esp_err_t (*set_link)(esp_eth_mac_t *mac, eth_link_t link);
  220. /**
  221. * @brief Set promiscuous of MAC
  222. *
  223. * @param[in] mac: Ethernet MAC instance
  224. * @param[in] enable: set true to enable promiscuous mode; set false to disable promiscuous mode
  225. *
  226. * @return
  227. * - ESP_OK: set promiscuous mode successfully
  228. * - ESP_FAIL: set promiscuous mode failed because some error occurred
  229. *
  230. */
  231. esp_err_t (*set_promiscuous)(esp_eth_mac_t *mac, bool enable);
  232. /**
  233. * @brief Free memory of Ethernet MAC
  234. *
  235. * @param[in] mac: Ethernet MAC instance
  236. *
  237. * @return
  238. * - ESP_OK: free Ethernet MAC instance successfully
  239. * - ESP_FAIL: free Ethernet MAC instance failed because some error occurred
  240. *
  241. */
  242. esp_err_t (*del)(esp_eth_mac_t *mac);
  243. };
  244. /**
  245. * @brief Configuration of Ethernet MAC object
  246. *
  247. */
  248. typedef struct {
  249. uint32_t sw_reset_timeout_ms; /*!< Software reset timeout value (Unit: ms) */
  250. uint32_t rx_task_stack_size; /*!< Stack size of the receive task */
  251. uint32_t rx_task_prio; /*!< Priority of the receive task */
  252. int smi_mdc_gpio_num; /*!< SMI MDC GPIO number */
  253. int smi_mdio_gpio_num; /*!< SMI MDIO GPIO number */
  254. uint32_t flags; /*!< Flags that specify extra capability for mac driver */
  255. } eth_mac_config_t;
  256. #define ETH_MAC_FLAG_WORK_WITH_CACHE_DISABLE (1 << 0) /*!< MAC driver can work when cache is disabled */
  257. #define ETH_MAC_FLAG_PIN_TO_CORE (1 << 1) /*!< Pin MAC task to the CPU core where driver installation happened */
  258. /**
  259. * @brief Default configuration for Ethernet MAC object
  260. *
  261. */
  262. #define ETH_MAC_DEFAULT_CONFIG() \
  263. { \
  264. .sw_reset_timeout_ms = 100, \
  265. .rx_task_stack_size = 4096, \
  266. .rx_task_prio = 15, \
  267. .smi_mdc_gpio_num = 23, \
  268. .smi_mdio_gpio_num = 18, \
  269. .flags = 0, \
  270. }
  271. #if CONFIG_ETH_USE_ESP32_EMAC
  272. /**
  273. * @brief Create ESP32 Ethernet MAC instance
  274. *
  275. * @param config: Ethernet MAC configuration
  276. *
  277. * @return
  278. * - instance: create MAC instance successfully
  279. * - NULL: create MAC instance failed because some error occurred
  280. */
  281. esp_eth_mac_t *esp_eth_mac_new_esp32(const eth_mac_config_t *config);
  282. #endif
  283. #if CONFIG_ETH_SPI_ETHERNET_DM9051
  284. /**
  285. * @brief DM9051 specific configuration
  286. *
  287. */
  288. typedef struct {
  289. spi_device_handle_t spi_hdl; /*!< Handle of SPI device driver */
  290. int int_gpio_num; /*!< Interrupt GPIO number */
  291. } eth_dm9051_config_t;
  292. /**
  293. * @brief Default DM9051 specific configuration
  294. *
  295. */
  296. #define ETH_DM9051_DEFAULT_CONFIG(spi_device) \
  297. { \
  298. .spi_hdl = spi_device, \
  299. .int_gpio_num = 4, \
  300. }
  301. /**
  302. * @brief Create DM9051 Ethernet MAC instance
  303. *
  304. * @param dm9051_config: DM9051 specific configuration
  305. * @param mac_config: Ethernet MAC configuration
  306. *
  307. * @return
  308. * - instance: create MAC instance successfully
  309. * - NULL: create MAC instance failed because some error occurred
  310. */
  311. esp_eth_mac_t *esp_eth_mac_new_dm9051(const eth_dm9051_config_t *dm9051_config, const eth_mac_config_t *mac_config);
  312. #endif
  313. #ifdef __cplusplus
  314. }
  315. #endif