emac_hal.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include "sdkconfig.h"
  8. #include "esp_attr.h"
  9. #include "hal/emac_hal.h"
  10. #include "hal/emac_ll.h"
  11. #include "hal/gpio_ll.h"
  12. #define ETH_CRC_LENGTH (4)
  13. void emac_hal_iomux_init_mii(void)
  14. {
  15. /* TX_CLK to GPIO0 */
  16. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_EMAC_TX_CLK);
  17. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[0]);
  18. /* TX_EN to GPIO21 */
  19. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_GPIO21_U, FUNC_GPIO21_EMAC_TX_EN);
  20. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[21]);
  21. /* TXD0 to GPIO19 */
  22. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_GPIO19_U, FUNC_GPIO19_EMAC_TXD0);
  23. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[19]);
  24. /* TXD1 to GPIO22 */
  25. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_GPIO22_U, FUNC_GPIO22_EMAC_TXD1);
  26. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[22]);
  27. /* TXD2 to MTMS */
  28. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_MTMS_U, FUNC_MTMS_EMAC_TXD2);
  29. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[14]);
  30. /* TXD3 to MTDI */
  31. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_MTDI_U, FUNC_MTDI_EMAC_TXD3);
  32. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[12]);
  33. /* RX_CLK to GPIO5 */
  34. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_GPIO5_U, FUNC_GPIO5_EMAC_RX_CLK);
  35. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[5]);
  36. /* RX_DV to GPIO27 */
  37. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_GPIO27_U, FUNC_GPIO27_EMAC_RX_DV);
  38. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[27]);
  39. /* RXD0 to GPIO25 */
  40. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_GPIO25_U, FUNC_GPIO25_EMAC_RXD0);
  41. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[25]);
  42. /* RXD1 to GPIO26 */
  43. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_GPIO26_U, FUNC_GPIO26_EMAC_RXD1);
  44. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[26]);
  45. /* RXD2 to U0TXD */
  46. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD_EMAC_RXD2);
  47. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[1]);
  48. /* RXD3 to MTDO */
  49. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_MTDO_U, FUNC_MTDO_EMAC_RXD3);
  50. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[15]);
  51. }
  52. void emac_hal_iomux_rmii_clk_input(void)
  53. {
  54. /* REF_CLK(RMII mode) to GPIO0 */
  55. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_EMAC_TX_CLK);
  56. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[0]);
  57. }
  58. void emac_hal_iomux_rmii_clk_ouput(int num)
  59. {
  60. switch (num) {
  61. case 0:
  62. /* APLL clock output to GPIO0 (must be configured to 50MHz!) */
  63. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1);
  64. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[0]);
  65. break;
  66. case 16:
  67. /* RMII CLK (50MHz) output to GPIO16 */
  68. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_GPIO16_U, FUNC_GPIO16_EMAC_CLK_OUT);
  69. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[16]);
  70. break;
  71. case 17:
  72. /* RMII CLK (50MHz) output to GPIO17 */
  73. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_GPIO17_U, FUNC_GPIO17_EMAC_CLK_OUT_180);
  74. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[17]);
  75. break;
  76. default:
  77. break;
  78. }
  79. }
  80. void emac_hal_iomux_init_rmii(void)
  81. {
  82. /* TX_EN to GPIO21 */
  83. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_GPIO21_U, FUNC_GPIO21_EMAC_TX_EN);
  84. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[21]);
  85. /* TXD0 to GPIO19 */
  86. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_GPIO19_U, FUNC_GPIO19_EMAC_TXD0);
  87. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[19]);
  88. /* TXD1 to GPIO22 */
  89. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_GPIO22_U, FUNC_GPIO22_EMAC_TXD1);
  90. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[22]);
  91. /* CRS_DV to GPIO27 */
  92. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_GPIO27_U, FUNC_GPIO27_EMAC_RX_DV);
  93. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[27]);
  94. /* RXD0 to GPIO25 */
  95. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_GPIO25_U, FUNC_GPIO25_EMAC_RXD0);
  96. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[25]);
  97. /* RXD1 to GPIO26 */
  98. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_GPIO26_U, FUNC_GPIO26_EMAC_RXD1);
  99. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[26]);
  100. }
  101. void emac_hal_iomux_init_tx_er(void)
  102. {
  103. /* TX_ER to GPIO4 */
  104. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_GPIO4_U, FUNC_GPIO4_EMAC_TX_ER);
  105. PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[4]);
  106. }
  107. void emac_hal_iomux_init_rx_er(void)
  108. {
  109. /* RX_ER to MTCK */
  110. gpio_ll_iomux_func_sel(PERIPHS_IO_MUX_MTCK_U, FUNC_MTCK_EMAC_RX_ER);
  111. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[13]);
  112. }
  113. void emac_hal_init(emac_hal_context_t *hal, void *descriptors,
  114. uint8_t **rx_buf, uint8_t **tx_buf)
  115. {
  116. hal->dma_regs = &EMAC_DMA;
  117. hal->mac_regs = &EMAC_MAC;
  118. hal->ext_regs = &EMAC_EXT;
  119. hal->descriptors = descriptors;
  120. hal->rx_buf = rx_buf;
  121. hal->tx_buf = tx_buf;
  122. }
  123. void emac_hal_set_csr_clock_range(emac_hal_context_t *hal, int freq)
  124. {
  125. /* Tell MAC system clock Frequency in MHz, which will determine the frequency range of MDC(1MHz~2.5MHz) */
  126. if (freq >= 20000000 && freq < 35000000) {
  127. emac_ll_set_csr_clock_division(hal->mac_regs, 2); // CSR clock/16
  128. } else if (freq >= 35000000 && freq < 60000000) {
  129. emac_ll_set_csr_clock_division(hal->mac_regs, 3); // CSR clock/26
  130. } else if (freq >= 60000000 && freq < 100000000) {
  131. emac_ll_set_csr_clock_division(hal->mac_regs, 0); // CSR clock/42
  132. } else if (freq >= 100000000 && freq < 150000000) {
  133. emac_ll_set_csr_clock_division(hal->mac_regs, 1); // CSR clock/62
  134. } else if (freq >= 150000000 && freq < 250000000) {
  135. emac_ll_set_csr_clock_division(hal->mac_regs, 4); // CSR clock/102
  136. } else {
  137. emac_ll_set_csr_clock_division(hal->mac_regs, 5); // CSR clock/124
  138. }
  139. }
  140. void emac_hal_reset_desc_chain(emac_hal_context_t *hal)
  141. {
  142. /* reset DMA descriptors */
  143. hal->rx_desc = (eth_dma_rx_descriptor_t *)(hal->descriptors);
  144. hal->tx_desc = (eth_dma_tx_descriptor_t *)(hal->descriptors +
  145. sizeof(eth_dma_rx_descriptor_t) * CONFIG_ETH_DMA_RX_BUFFER_NUM);
  146. /* init rx chain */
  147. for (int i = 0; i < CONFIG_ETH_DMA_RX_BUFFER_NUM; i++) {
  148. /* Set Own bit of the Rx descriptor Status: DMA */
  149. hal->rx_desc[i].RDES0.Own = EMAC_LL_DMADESC_OWNER_DMA;
  150. /* Set Buffer1 size and Second Address Chained bit */
  151. hal->rx_desc[i].RDES1.SecondAddressChained = 1;
  152. hal->rx_desc[i].RDES1.ReceiveBuffer1Size = CONFIG_ETH_DMA_BUFFER_SIZE;
  153. /* Enable Ethernet DMA Rx Descriptor interrupt */
  154. hal->rx_desc[i].RDES1.DisableInterruptOnComplete = 0;
  155. /* point to the buffer */
  156. hal->rx_desc[i].Buffer1Addr = (uint32_t)(hal->rx_buf[i]);
  157. /* point to next descriptor */
  158. hal->rx_desc[i].Buffer2NextDescAddr = (uint32_t)(hal->rx_desc + i + 1);
  159. }
  160. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  161. hal->rx_desc[CONFIG_ETH_DMA_RX_BUFFER_NUM - 1].Buffer2NextDescAddr = (uint32_t)(hal->rx_desc);
  162. /* init tx chain */
  163. for (int i = 0; i < CONFIG_ETH_DMA_TX_BUFFER_NUM; i++) {
  164. /* Set Own bit of the Tx descriptor Status: CPU */
  165. hal->tx_desc[i].TDES0.Own = EMAC_LL_DMADESC_OWNER_CPU;
  166. hal->tx_desc[i].TDES0.SecondAddressChained = 1;
  167. hal->tx_desc[i].TDES1.TransmitBuffer1Size = CONFIG_ETH_DMA_BUFFER_SIZE;
  168. /* Enable Ethernet DMA Tx Descriptor interrupt */
  169. hal->tx_desc[1].TDES0.InterruptOnComplete = 1;
  170. /* Enable Transmit Timestamp */
  171. hal->tx_desc[i].TDES0.TransmitTimestampEnable = 1;
  172. /* point to the buffer */
  173. hal->tx_desc[i].Buffer1Addr = (uint32_t)(hal->tx_buf[i]);
  174. /* point to next descriptor */
  175. hal->tx_desc[i].Buffer2NextDescAddr = (uint32_t)(hal->tx_desc + i + 1);
  176. }
  177. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  178. hal->tx_desc[CONFIG_ETH_DMA_TX_BUFFER_NUM - 1].Buffer2NextDescAddr = (uint32_t)(hal->tx_desc);
  179. /* set base address of the first descriptor */
  180. emac_ll_set_rx_desc_addr(hal->dma_regs, (uint32_t)hal->rx_desc);
  181. emac_ll_set_tx_desc_addr(hal->dma_regs, (uint32_t)hal->tx_desc);
  182. }
  183. void emac_hal_init_mac_default(emac_hal_context_t *hal)
  184. {
  185. /* MACCR Configuration */
  186. /* Enable the watchdog on the receiver, frame longer than 2048 Bytes is not allowed */
  187. emac_ll_watchdog_enable(hal->mac_regs, true);
  188. /* Enable the jabber timer on the transmitter, frame longer than 2048 Bytes is not allowed */
  189. emac_ll_jabber_enable(hal->mac_regs, true);
  190. /* minimum IFG between frames during transmission is 96 bit times */
  191. emac_ll_set_inter_frame_gap(hal->mac_regs, EMAC_LL_INTERFRAME_GAP_96BIT);
  192. /* Enable Carrier Sense During Transmission */
  193. emac_ll_carrier_sense_enable(hal->mac_regs, true);
  194. /* Select speed: port: 10/100 Mbps, here set default 100M, afterwards, will reset by auto-negotiation */
  195. emac_ll_set_port_speed(hal->mac_regs, ETH_SPEED_100M);;
  196. /* Allow the reception of frames when the TX_EN signal is asserted in Half-Duplex mode */
  197. emac_ll_recv_own_enable(hal->mac_regs, true);
  198. /* Disable internal loopback mode */
  199. emac_ll_loopback_enable(hal->mac_regs, false);
  200. /* Select duplex mode: here set default full duplex, afterwards, will reset by auto-negotiation */
  201. emac_ll_set_duplex(hal->mac_regs, ETH_DUPLEX_FULL);
  202. /* Select the checksum mode for received frame payload's TCP/UDP/ICMP headers */
  203. emac_ll_checksum_offload_mode(hal->mac_regs, ETH_CHECKSUM_HW);
  204. /* Enable MAC retry transmission when a colision occurs in half duplex mode */
  205. emac_ll_retry_enable(hal->mac_regs, true);
  206. /* MAC passes all incoming frames to host, without modifying them */
  207. emac_ll_auto_pad_crc_strip_enable(hal->mac_regs, false);
  208. /* Set Back-Off limit time before retry a transmittion after a collision */
  209. emac_ll_set_back_off_limit(hal->mac_regs, EMAC_LL_BACKOFF_LIMIT_10);
  210. /* Disable deferral check, MAC defers until the CRS signal goes inactive */
  211. emac_ll_deferral_check_enable(hal->mac_regs, false);
  212. /* Set preamble length 7 Bytes */
  213. emac_ll_set_preamble_length(hal->mac_regs, EMAC_LL_PREAMBLE_LENGTH_7);
  214. /* MACFFR Configuration */
  215. /* Receiver module passes only those frames to the Application that pass the SA or DA address filter */
  216. emac_ll_receive_all_enable(hal->mac_regs, false);
  217. /* Disable source address filter */
  218. emac_ll_set_src_addr_filter(hal->mac_regs, EMAC_LL_SOURCE_ADDR_FILTER_DISABLE);
  219. emac_ll_sa_inverse_filter_enable(hal->mac_regs, false);
  220. /* MAC blocks all control frames */
  221. emac_ll_set_pass_ctrl_frame_mode(hal->mac_regs, EMAC_LL_CONTROL_FRAME_BLOCKALL);
  222. /* AFM module passes all received broadcast frames and multicast frames */
  223. emac_ll_broadcast_frame_enable(hal->mac_regs, true);
  224. emac_ll_pass_all_multicast_enable(hal->mac_regs, true);
  225. /* Address Check block operates in normal filtering mode for the DA address */
  226. emac_ll_da_inverse_filter_enable(hal->mac_regs, false);
  227. /* Disable Promiscuous Mode */
  228. emac_ll_promiscuous_mode_enable(hal->mac_regs, false);
  229. }
  230. void emac_hal_enable_flow_ctrl(emac_hal_context_t *hal, bool enable)
  231. {
  232. /* MACFCR Configuration */
  233. if (enable) {
  234. /* Pause time */
  235. emac_ll_set_pause_time(hal->mac_regs, EMAC_LL_PAUSE_TIME);
  236. /* Enable generation of Zero-Quanta Pause Control frames */
  237. emac_ll_zero_quanta_pause_enable(hal->mac_regs, true);
  238. /* Threshold of the PAUSE to be checked for automatic retransmission of PAUSE Frame */
  239. emac_ll_set_pause_low_threshold(hal->mac_regs, EMAC_LL_PAUSE_LOW_THRESHOLD_MINUS_28);
  240. /* Don't allow MAC detect Pause frames with MAC address0 unicast address and unique multicast address */
  241. emac_ll_unicast_pause_frame_detect_enable(hal->mac_regs, false);
  242. /* Enable MAC to decode the received Pause frame and disable its transmitter for a specific time */
  243. emac_ll_receive_flow_ctrl_enable(hal->mac_regs, true);
  244. /* Enable MAC to transmit Pause frames in full duplex mode or the MAC back-pressure operation in half duplex mode */
  245. emac_ll_transmit_flow_ctrl_enable(hal->mac_regs, true);
  246. } else {
  247. emac_ll_clear(hal->mac_regs);
  248. }
  249. }
  250. void emac_hal_init_dma_default(emac_hal_context_t *hal, emac_hal_dma_config_t *hal_config)
  251. {
  252. /* DMAOMR Configuration */
  253. /* Enable Dropping of TCP/IP Checksum Error Frames */
  254. emac_ll_drop_tcp_err_frame_enable(hal->dma_regs, true);
  255. /* Enable Receive Store Forward */
  256. emac_ll_recv_store_forward_enable(hal->dma_regs, true);
  257. /* Enable Flushing of Received Frames because of the unavailability of receive descriptors or buffers */
  258. emac_ll_flush_recv_frame_enable(hal->dma_regs, true);
  259. /* Disable Transmit Store Forward */
  260. emac_ll_trans_store_forward_enable(hal->dma_regs, false);
  261. /* Flush Transmit FIFO */
  262. emac_ll_flush_trans_fifo_enable(hal->dma_regs, true);
  263. /* Transmit Threshold Control */
  264. emac_ll_set_transmit_threshold(hal->dma_regs, EMAC_LL_TRANSMIT_THRESHOLD_CONTROL_64);
  265. /* Disable Forward Error Frame */
  266. emac_ll_forward_err_frame_enable(hal->dma_regs, false);
  267. /* Disable forward undersized good frame */
  268. emac_ll_forward_undersized_good_frame_enable(hal->dma_regs, false);
  269. /* Receive Threshold Control */
  270. emac_ll_set_recv_threshold(hal->dma_regs, EMAC_LL_RECEIVE_THRESHOLD_CONTROL_64);
  271. /* Allow the DMA to process a second frame of Transmit data even before obtaining the status for the first frame */
  272. emac_ll_opt_second_frame_enable(hal->dma_regs, true);;
  273. /* DMABMR Configuration */
  274. /* Enable Mixed Burst */
  275. emac_ll_mixed_burst_enable(hal->dma_regs, true);
  276. /* Enable Address Aligned Beates */
  277. emac_ll_addr_align_enable(hal->dma_regs, true);
  278. /* Don't use Separate PBL */
  279. emac_ll_use_separate_pbl_enable(hal->dma_regs, false);
  280. /* Set Rx/Tx DMA Burst Length */
  281. emac_ll_set_prog_burst_len(hal->dma_regs, hal_config->dma_burst_len);
  282. /* Enable Enhanced Descriptor,8 Words(32 Bytes) */
  283. emac_ll_enhance_desc_enable(hal->dma_regs, true);
  284. /* Specifies the number of word to skip between two unchained descriptors (Ring mode) */
  285. emac_ll_set_desc_skip_len(hal->dma_regs, 0);
  286. /* DMA Arbitration Scheme */
  287. emac_ll_fixed_arbitration_enable(hal->dma_regs, false);
  288. /* Set priority ratio in the weighted round-robin arbitration between Rx DMA and Tx DMA */
  289. emac_ll_set_priority_ratio(hal->dma_regs, EMAC_LL_DMA_ARBITRATION_ROUNDROBIN_RXTX_1_1);
  290. }
  291. void emac_hal_set_phy_cmd(emac_hal_context_t *hal, uint32_t phy_addr, uint32_t phy_reg, bool write)
  292. {
  293. /* Write the result value into the MII Address register */
  294. emac_ll_set_phy_addr(hal->mac_regs, phy_addr);
  295. /* Set the PHY register address */
  296. emac_ll_set_phy_reg(hal->mac_regs, phy_reg);
  297. /* Set as write mode */
  298. emac_ll_write_enable(hal->mac_regs, write);
  299. /* Set MII busy bit */
  300. emac_ll_set_busy(hal->mac_regs, true);
  301. }
  302. void emac_hal_set_address(emac_hal_context_t *hal, uint8_t *mac_addr)
  303. {
  304. /* Make sure mac address is unicast type */
  305. if (!(mac_addr[0] & 0x01)) {
  306. emac_ll_set_addr(hal->mac_regs, mac_addr);
  307. }
  308. }
  309. void emac_hal_start(emac_hal_context_t *hal)
  310. {
  311. /* Enable Ethernet MAC and DMA Interrupt */
  312. emac_ll_enable_corresponding_intr(hal->dma_regs, EMAC_LL_CONFIG_ENABLE_INTR_MASK);
  313. /* Flush Transmit FIFO */
  314. emac_ll_flush_trans_fifo_enable(hal->dma_regs, true);
  315. /* Start DMA transmission */
  316. emac_ll_start_stop_dma_transmit(hal->dma_regs, true);
  317. /* Start DMA reception */
  318. emac_ll_start_stop_dma_receive(hal->dma_regs, true);
  319. /* Enable transmit state machine of the MAC for transmission on the MII */
  320. emac_ll_transmit_enable(hal->mac_regs, true);
  321. /* Enable receive state machine of the MAC for reception from the MII */
  322. emac_ll_receive_enable(hal->mac_regs, true);
  323. /* Clear all pending interrupts */
  324. emac_ll_clear_all_pending_intr(hal->dma_regs);
  325. }
  326. esp_err_t emac_hal_stop(emac_hal_context_t *hal)
  327. {
  328. /* Stop DMA transmission */
  329. emac_ll_start_stop_dma_transmit(hal->dma_regs, false);
  330. if (emac_ll_transmit_frame_ctrl_status(hal->mac_regs) != 0x0) {
  331. /* Previous transmit in progress */
  332. return ESP_ERR_INVALID_STATE;
  333. }
  334. /* Disable transmit state machine of the MAC for transmission on the MII */
  335. emac_ll_receive_enable(hal->mac_regs, false);
  336. /* Disable receive state machine of the MAC for reception from the MII */
  337. emac_ll_transmit_enable(hal->mac_regs, false);
  338. if (emac_ll_receive_read_ctrl_state(hal->mac_regs) != 0x0) {
  339. /* Previous receive copy in progress */
  340. return ESP_ERR_INVALID_STATE;
  341. }
  342. /* Stop DMA reception */
  343. emac_ll_start_stop_dma_receive(hal->dma_regs, false);
  344. /* Disable Ethernet MAC and DMA Interrupt */
  345. emac_ll_disable_all_intr(hal->dma_regs);
  346. return ESP_OK;
  347. }
  348. uint32_t emac_hal_get_tx_desc_owner(emac_hal_context_t *hal)
  349. {
  350. return hal->tx_desc->TDES0.Own;
  351. }
  352. uint32_t emac_hal_transmit_frame(emac_hal_context_t *hal, uint8_t *buf, uint32_t length)
  353. {
  354. /* Get the number of Tx buffers to use for the frame */
  355. uint32_t bufcount = 0;
  356. uint32_t lastlen = length;
  357. uint32_t sentout = 0;
  358. while (lastlen > CONFIG_ETH_DMA_BUFFER_SIZE) {
  359. lastlen -= CONFIG_ETH_DMA_BUFFER_SIZE;
  360. bufcount++;
  361. }
  362. if (lastlen) {
  363. bufcount++;
  364. }
  365. if (bufcount > CONFIG_ETH_DMA_TX_BUFFER_NUM) {
  366. goto err;
  367. }
  368. eth_dma_tx_descriptor_t *desc_iter = hal->tx_desc;
  369. /* A frame is transmitted in multiple descriptor */
  370. for (size_t i = 0; i < bufcount; i++) {
  371. /* Check if the descriptor is owned by the Ethernet DMA (when 1) or CPU (when 0) */
  372. if (desc_iter->TDES0.Own != EMAC_LL_DMADESC_OWNER_CPU) {
  373. goto err;
  374. }
  375. /* Clear FIRST and LAST segment bits */
  376. desc_iter->TDES0.FirstSegment = 0;
  377. desc_iter->TDES0.LastSegment = 0;
  378. desc_iter->TDES0.InterruptOnComplete = 0;
  379. if (i == 0) {
  380. /* Setting the first segment bit */
  381. desc_iter->TDES0.FirstSegment = 1;
  382. }
  383. if (i == (bufcount - 1)) {
  384. /* Setting the last segment bit */
  385. desc_iter->TDES0.LastSegment = 1;
  386. /* Enable transmit interrupt */
  387. desc_iter->TDES0.InterruptOnComplete = 1;
  388. /* Program size */
  389. desc_iter->TDES1.TransmitBuffer1Size = lastlen;
  390. /* copy data from uplayer stack buffer */
  391. memcpy((void *)(desc_iter->Buffer1Addr), buf + i * CONFIG_ETH_DMA_BUFFER_SIZE, lastlen);
  392. sentout += lastlen;
  393. } else {
  394. /* Program size */
  395. desc_iter->TDES1.TransmitBuffer1Size = CONFIG_ETH_DMA_BUFFER_SIZE;
  396. /* copy data from uplayer stack buffer */
  397. memcpy((void *)(desc_iter->Buffer1Addr), buf + i * CONFIG_ETH_DMA_BUFFER_SIZE, CONFIG_ETH_DMA_BUFFER_SIZE);
  398. sentout += CONFIG_ETH_DMA_BUFFER_SIZE;
  399. }
  400. /* Point to next descriptor */
  401. desc_iter = (eth_dma_tx_descriptor_t *)(desc_iter->Buffer2NextDescAddr);
  402. }
  403. /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
  404. for (size_t i = 0; i < bufcount; i++) {
  405. hal->tx_desc->TDES0.Own = EMAC_LL_DMADESC_OWNER_DMA;
  406. hal->tx_desc = (eth_dma_tx_descriptor_t *)(hal->tx_desc->Buffer2NextDescAddr);
  407. }
  408. emac_ll_transmit_poll_demand(hal->dma_regs, 0);
  409. return sentout;
  410. err:
  411. return 0;
  412. }
  413. uint32_t emac_hal_transmit_multiple_buf_frame(emac_hal_context_t *hal, uint8_t **buffs, uint32_t *lengths, uint32_t buffs_cnt)
  414. {
  415. /* Get the number of Tx buffers to use for the frame */
  416. uint32_t dma_bufcount = 0;
  417. uint32_t sentout = 0;
  418. uint8_t *ptr = buffs[0];
  419. uint32_t lastlen = lengths[0];
  420. uint32_t avail_len = CONFIG_ETH_DMA_BUFFER_SIZE;
  421. eth_dma_tx_descriptor_t *desc_iter = hal->tx_desc;
  422. /* A frame is transmitted in multiple descriptor */
  423. while (dma_bufcount < CONFIG_ETH_DMA_TX_BUFFER_NUM) {
  424. /* Check if the descriptor is owned by the Ethernet DMA (when 1) or CPU (when 0) */
  425. if (desc_iter->TDES0.Own != EMAC_LL_DMADESC_OWNER_CPU) {
  426. goto err;
  427. }
  428. /* Clear FIRST and LAST segment bits */
  429. desc_iter->TDES0.FirstSegment = 0;
  430. desc_iter->TDES0.LastSegment = 0;
  431. desc_iter->TDES0.InterruptOnComplete = 0;
  432. desc_iter->TDES1.TransmitBuffer1Size = 0;
  433. if (dma_bufcount == 0) {
  434. /* Setting the first segment bit */
  435. desc_iter->TDES0.FirstSegment = 1;
  436. }
  437. while (buffs_cnt > 0) {
  438. /* Check if input buff data fits to currently available space in the descriptor */
  439. if (lastlen < avail_len) {
  440. /* copy data from uplayer stack buffer */
  441. memcpy((void *)(desc_iter->Buffer1Addr + (CONFIG_ETH_DMA_BUFFER_SIZE - avail_len)), ptr, lastlen);
  442. sentout += lastlen;
  443. avail_len -= lastlen;
  444. desc_iter->TDES1.TransmitBuffer1Size += lastlen;
  445. /* Update processed input buffers info */
  446. buffs_cnt--;
  447. ptr = *(++buffs);
  448. lastlen = *(++lengths);
  449. /* There is only limited available space in the current descriptor, use it all */
  450. } else {
  451. /* copy data from uplayer stack buffer */
  452. memcpy((void *)(desc_iter->Buffer1Addr + (CONFIG_ETH_DMA_BUFFER_SIZE - avail_len)), ptr, avail_len);
  453. sentout += avail_len;
  454. lastlen -= avail_len;
  455. /* If lastlen is not zero, input buff will be fragmented over multiple descriptors */
  456. if (lastlen > 0) {
  457. ptr += avail_len;
  458. /* Input buff fully fits the descriptor, move to the next input buff */
  459. } else {
  460. /* Update processed input buffers info */
  461. buffs_cnt--;
  462. ptr = *(++buffs);
  463. lastlen = *(++lengths);
  464. }
  465. avail_len = CONFIG_ETH_DMA_BUFFER_SIZE;
  466. desc_iter->TDES1.TransmitBuffer1Size = CONFIG_ETH_DMA_BUFFER_SIZE;
  467. /* The descriptor is full here so exit and use the next descriptor */
  468. break;
  469. }
  470. }
  471. /* Increase counter of utilized DMA buffers */
  472. dma_bufcount++;
  473. /* If all input buffers processed, mark as LAST segment and finish the coping */
  474. if (buffs_cnt == 0) {
  475. /* Setting the last segment bit */
  476. desc_iter->TDES0.LastSegment = 1;
  477. /* Enable transmit interrupt */
  478. desc_iter->TDES0.InterruptOnComplete = 1;
  479. break;
  480. }
  481. /* Point to next descriptor */
  482. desc_iter = (eth_dma_tx_descriptor_t *)(desc_iter->Buffer2NextDescAddr);
  483. }
  484. /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
  485. for (size_t i = 0; i < dma_bufcount; i++) {
  486. hal->tx_desc->TDES0.Own = EMAC_LL_DMADESC_OWNER_DMA;
  487. hal->tx_desc = (eth_dma_tx_descriptor_t *)(hal->tx_desc->Buffer2NextDescAddr);
  488. }
  489. emac_ll_transmit_poll_demand(hal->dma_regs, 0);
  490. return sentout;
  491. err:
  492. return 0;
  493. }
  494. uint32_t emac_hal_receive_frame(emac_hal_context_t *hal, uint8_t *buf, uint32_t size, uint32_t *frames_remain, uint32_t *free_desc)
  495. {
  496. eth_dma_rx_descriptor_t *desc_iter = NULL;
  497. eth_dma_rx_descriptor_t *first_desc = NULL;
  498. uint32_t used_descs = 0;
  499. uint32_t seg_count = 0;
  500. uint32_t ret_len = 0;
  501. uint32_t copy_len = 0;
  502. uint32_t write_len = 0;
  503. uint32_t frame_count = 0;
  504. first_desc = hal->rx_desc;
  505. desc_iter = hal->rx_desc;
  506. /* Traverse descriptors owned by CPU */
  507. while ((desc_iter->RDES0.Own != EMAC_LL_DMADESC_OWNER_DMA) && (used_descs < CONFIG_ETH_DMA_RX_BUFFER_NUM) && !frame_count) {
  508. used_descs++;
  509. seg_count++;
  510. /* Last segment in frame */
  511. if (desc_iter->RDES0.LastDescriptor) {
  512. /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
  513. ret_len = desc_iter->RDES0.FrameLength - ETH_CRC_LENGTH;
  514. /* packets larger than expected will be truncated */
  515. copy_len = ret_len > size ? size : ret_len;
  516. /* update unhandled frame count */
  517. frame_count++;
  518. }
  519. /* First segment in frame */
  520. if (desc_iter->RDES0.FirstDescriptor) {
  521. first_desc = desc_iter;
  522. }
  523. /* point to next descriptor */
  524. desc_iter = (eth_dma_rx_descriptor_t *)(desc_iter->Buffer2NextDescAddr);
  525. }
  526. /* there's at least one frame to process */
  527. if (frame_count) {
  528. /* check how many frames left to handle */
  529. while ((desc_iter->RDES0.Own != EMAC_LL_DMADESC_OWNER_DMA) && (used_descs < CONFIG_ETH_DMA_RX_BUFFER_NUM)) {
  530. used_descs++;
  531. if (desc_iter->RDES0.LastDescriptor) {
  532. frame_count++;
  533. }
  534. /* point to next descriptor */
  535. desc_iter = (eth_dma_rx_descriptor_t *)(desc_iter->Buffer2NextDescAddr);
  536. }
  537. desc_iter = first_desc;
  538. for (size_t i = 0; i < seg_count - 1; i++) {
  539. used_descs--;
  540. write_len = copy_len < CONFIG_ETH_DMA_BUFFER_SIZE ? copy_len : CONFIG_ETH_DMA_BUFFER_SIZE;
  541. /* copy data to buffer */
  542. memcpy(buf, (void *)(desc_iter->Buffer1Addr), write_len);
  543. buf += write_len;
  544. copy_len -= write_len;
  545. /* Set Own bit in Rx descriptors: gives the buffers back to DMA */
  546. desc_iter->RDES0.Own = EMAC_LL_DMADESC_OWNER_DMA;
  547. desc_iter = (eth_dma_rx_descriptor_t *)(desc_iter->Buffer2NextDescAddr);
  548. }
  549. memcpy(buf, (void *)(desc_iter->Buffer1Addr), copy_len);
  550. desc_iter->RDES0.Own = EMAC_LL_DMADESC_OWNER_DMA;
  551. /* update rxdesc */
  552. hal->rx_desc = (eth_dma_rx_descriptor_t *)(desc_iter->Buffer2NextDescAddr);
  553. /* poll rx demand */
  554. emac_ll_receive_poll_demand(hal->dma_regs, 0);
  555. frame_count--;
  556. used_descs--;
  557. }
  558. *frames_remain = frame_count;
  559. *free_desc = CONFIG_ETH_DMA_RX_BUFFER_NUM - used_descs;
  560. return ret_len;
  561. }