drv_enet.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. * File : eth_driver.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2011-11-30 aozima the first version.
  13. * 2011-12-10 aozima support dual ethernet.
  14. * 2011-12-21 aozima cleanup code.
  15. * 2012-07-13 aozima mask all GMAC MMC Interrupt.
  16. * 2012-07-20 aozima fixed mask all GMAC MMC Interrupt,and read clear.
  17. * 2012-07-20 aozima use memcpy replace byte copy.
  18. */
  19. #include <rtthread.h>
  20. #include <rthw.h>
  21. #include "lwipopts.h"
  22. #include <netif/ethernetif.h>
  23. #include <netif/etharp.h>
  24. #include <lwip/icmp.h>
  25. #include "gd32f4xx.h"
  26. #include "synopsys_emac.h"
  27. #define ETHERNET_MAC0 ((struct rt_synopsys_eth *)(0x40020000U + 0x00008000U))
  28. //#define EMAC_DEBUG
  29. //#define EMAC_RX_DUMP
  30. //#define EMAC_TX_DUMP
  31. #ifdef EMAC_DEBUG
  32. #define EMAC_TRACE rt_kprintf
  33. #else
  34. #define EMAC_TRACE(...)
  35. #endif
  36. #define EMAC_RXBUFNB 4
  37. #define EMAC_TXBUFNB 2
  38. #define EMAC_PHY_AUTO 0
  39. #define EMAC_PHY_10MBIT 1
  40. #define EMAC_PHY_100MBIT 2
  41. #define MAX_ADDR_LEN 6
  42. struct gd32_emac
  43. {
  44. /* inherit from Ethernet device */
  45. struct eth_device parent;
  46. rt_uint8_t phy_mode;
  47. /* interface address info. */
  48. rt_uint8_t dev_addr[MAX_ADDR_LEN]; /* hw address */
  49. struct rt_synopsys_eth * ETHERNET_MAC;
  50. IRQn_Type ETHER_MAC_IRQ;
  51. EMAC_DMADESCTypeDef *DMATxDescToSet;
  52. EMAC_DMADESCTypeDef *DMARxDescToGet;
  53. #pragma pack(4)
  54. EMAC_DMADESCTypeDef DMARxDscrTab[EMAC_RXBUFNB];
  55. #pragma pack(4)
  56. EMAC_DMADESCTypeDef DMATxDscrTab[EMAC_TXBUFNB];
  57. #pragma pack(4)
  58. rt_uint8_t Rx_Buff[EMAC_RXBUFNB][EMAC_MAX_PACKET_SIZE];
  59. #pragma pack(4)
  60. rt_uint8_t Tx_Buff[EMAC_TXBUFNB][EMAC_MAX_PACKET_SIZE];
  61. struct rt_semaphore tx_buf_free;
  62. };
  63. static struct gd32_emac gd32_emac_device0;
  64. /**
  65. * Initializes the DMA Tx descriptors in chain mode.
  66. */
  67. static void EMAC_DMA_tx_desc_init(EMAC_DMADESCTypeDef *DMATxDescTab, uint8_t* TxBuff, uint32_t TxBuffCount)
  68. {
  69. uint32_t i = 0;
  70. EMAC_DMADESCTypeDef *DMATxDesc;
  71. /* Fill each DMATxDesc descriptor with the right values */
  72. for(i=0; i < TxBuffCount; i++)
  73. {
  74. /* Get the pointer on the ith member of the Tx Desc list */
  75. DMATxDesc = DMATxDescTab + i;
  76. /* Set Second Address Chained bit */
  77. DMATxDesc->Status = EMAC_DMATxDesc_TCH;
  78. /* Set Buffer1 address pointer */
  79. DMATxDesc->Buffer1Addr = (uint32_t)(&TxBuff[i*EMAC_MAX_PACKET_SIZE]);
  80. /* Initialize the next descriptor with the Next Descriptor Polling Enable */
  81. if(i < (TxBuffCount-1))
  82. {
  83. /* Set next descriptor address register with next descriptor base address */
  84. DMATxDesc->Buffer2NextDescAddr = (uint32_t)(DMATxDescTab+i+1);
  85. }
  86. else
  87. {
  88. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  89. DMATxDesc->Buffer2NextDescAddr = (uint32_t) DMATxDescTab;
  90. }
  91. }
  92. }
  93. /**
  94. * Initializes the DMA Rx descriptors in chain mode.
  95. */
  96. static void EMAC_DMA_rx_desc_init(EMAC_DMADESCTypeDef *DMARxDescTab, uint8_t *RxBuff, uint32_t RxBuffCount)
  97. {
  98. uint32_t i = 0;
  99. EMAC_DMADESCTypeDef *DMARxDesc;
  100. /* Fill each DMARxDesc descriptor with the right values */
  101. for(i=0; i < RxBuffCount; i++)
  102. {
  103. /* Get the pointer on the ith member of the Rx Desc list */
  104. DMARxDesc = DMARxDescTab+i;
  105. /* Set Own bit of the Rx descriptor Status */
  106. DMARxDesc->Status = EMAC_DMARxDesc_OWN;
  107. /* Set Buffer1 size and Second Address Chained bit */
  108. DMARxDesc->ControlBufferSize = EMAC_DMARxDesc_RCH | (uint32_t)EMAC_MAX_PACKET_SIZE;
  109. /* Set Buffer1 address pointer */
  110. DMARxDesc->Buffer1Addr = (uint32_t)(&RxBuff[i*EMAC_MAX_PACKET_SIZE]);
  111. /* Initialize the next descriptor with the Next Descriptor Polling Enable */
  112. if(i < (RxBuffCount-1))
  113. {
  114. /* Set next descriptor address register with next descriptor base address */
  115. DMARxDesc->Buffer2NextDescAddr = (uint32_t)(DMARxDescTab+i+1);
  116. }
  117. else
  118. {
  119. /* For last descriptor, set next descriptor address register equal to the first descriptor base address */
  120. DMARxDesc->Buffer2NextDescAddr = (uint32_t)(DMARxDescTab);
  121. }
  122. }
  123. }
  124. static rt_err_t gd32_emac_init(rt_device_t dev)
  125. {
  126. struct gd32_emac * gd32_emac_device;
  127. struct rt_synopsys_eth * ETHERNET_MAC;
  128. gd32_emac_device = (struct gd32_emac *)dev;
  129. ETHERNET_MAC = gd32_emac_device->ETHERNET_MAC;
  130. /* Software reset */
  131. ETHERNET_MAC->BMR |= (1<<0); /* [bit0]SWR (Software Reset) */
  132. /* Wait for software reset */
  133. while(ETHERNET_MAC->BMR & (1<<0));
  134. /* Configure ETHERNET */
  135. EMAC_init(ETHERNET_MAC, SystemCoreClock);
  136. /* mask all GMAC MMC Interrupt.*/
  137. ETHERNET_MAC->mmc_cntl = (1<<3) | (1<<0); /* MMC Counter Freeze and reset. */
  138. ETHERNET_MAC->mmc_intr_mask_rx = 0xFFFFFFFF;
  139. ETHERNET_MAC->mmc_intr_mask_tx = 0xFFFFFFFF;
  140. ETHERNET_MAC->mmc_ipc_intr_mask_rx = 0xFFFFFFFF;
  141. /* Enable DMA Receive interrupt (need to enable in this case Normal interrupt) */
  142. EMAC_INT_config(ETHERNET_MAC, EMAC_DMA_INT_NIS | EMAC_DMA_INT_R | EMAC_DMA_INT_T , ENABLE);
  143. /* Initialize Tx Descriptors list: Chain Mode */
  144. EMAC_DMA_tx_desc_init(gd32_emac_device->DMATxDscrTab, &gd32_emac_device->Tx_Buff[0][0], EMAC_TXBUFNB);
  145. gd32_emac_device->DMATxDescToSet = gd32_emac_device->DMATxDscrTab;
  146. /* Set Transmit Descriptor List Address Register */
  147. ETHERNET_MAC->TDLAR = (uint32_t) gd32_emac_device->DMATxDescToSet;
  148. /* Initialize Rx Descriptors list: Chain Mode */
  149. EMAC_DMA_rx_desc_init(gd32_emac_device->DMARxDscrTab, &gd32_emac_device->Rx_Buff[0][0], EMAC_RXBUFNB);
  150. gd32_emac_device->DMARxDescToGet = gd32_emac_device->DMARxDscrTab;
  151. /* Set Receive Descriptor List Address Register */
  152. ETHERNET_MAC->RDLAR = (uint32_t) gd32_emac_device->DMARxDescToGet;
  153. /* MAC address configuration */
  154. EMAC_MAC_Addr_config(ETHERNET_MAC, EMAC_MAC_Address0, (uint8_t*)&gd32_emac_device->dev_addr[0]);
  155. NVIC_EnableIRQ( gd32_emac_device->ETHER_MAC_IRQ );
  156. /* Enable MAC and DMA transmission and reception */
  157. EMAC_start(ETHERNET_MAC);
  158. return RT_EOK;
  159. }
  160. static rt_err_t gd32_emac_open(rt_device_t dev, rt_uint16_t oflag)
  161. {
  162. return RT_EOK;
  163. }
  164. static rt_err_t gd32_emac_close(rt_device_t dev)
  165. {
  166. return RT_EOK;
  167. }
  168. static rt_size_t gd32_emac_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  169. {
  170. rt_set_errno(-RT_ENOSYS);
  171. return 0;
  172. }
  173. static rt_size_t gd32_emac_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  174. {
  175. rt_set_errno(-RT_ENOSYS);
  176. return 0;
  177. }
  178. static rt_err_t gd32_emac_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  179. {
  180. struct gd32_emac * gd32_emac_device = (struct gd32_emac *)dev;
  181. switch (cmd)
  182. {
  183. case NIOCTL_GADDR:
  184. /* get mac address */
  185. if (args) memcpy(args, &gd32_emac_device->dev_addr[0], MAX_ADDR_LEN);
  186. else return -RT_ERROR;
  187. break;
  188. default :
  189. break;
  190. }
  191. return RT_EOK;
  192. }
  193. static void EMAC_IRQHandler(struct gd32_emac * gd32_emac_device)
  194. {
  195. rt_uint32_t status, ier;
  196. struct rt_synopsys_eth * ETHERNET_MAC;
  197. ETHERNET_MAC = gd32_emac_device->ETHERNET_MAC;
  198. /* get DMA IT status */
  199. status = ETHERNET_MAC->SR;
  200. ier = ETHERNET_MAC->IER;
  201. /* GMAC MMC Interrupt. */
  202. if(status & EMAC_DMA_INT_GMI)
  203. {
  204. volatile rt_uint32_t dummy;
  205. volatile rt_uint32_t * reg;
  206. EMAC_TRACE("EMAC_DMA_INT_GMI\r\n");
  207. /* read clear all MMC interrupt. */
  208. reg = &ETHERNET_MAC->mmc_cntl;
  209. while((uint32_t)reg < (uint32_t)&ETHERNET_MAC->rxicmp_err_octets)
  210. {
  211. dummy = *reg++;
  212. }
  213. }
  214. /* Normal interrupt summary. */
  215. if(status & EMAC_DMA_INT_NIS)
  216. {
  217. rt_uint32_t nis_clear = EMAC_DMA_INT_NIS;
  218. /* [0]:Transmit Interrupt. */
  219. if((status & ier) & EMAC_DMA_INT_T) /* packet transmission */
  220. {
  221. rt_sem_release(&gd32_emac_device->tx_buf_free);
  222. nis_clear |= EMAC_DMA_INT_T;
  223. }
  224. /* [2]:Transmit Buffer Unavailable. */
  225. /* [6]:Receive Interrupt. */
  226. if((status & ier) & EMAC_DMA_INT_R) /* packet reception */
  227. {
  228. /* a frame has been received */
  229. eth_device_ready(&(gd32_emac_device->parent));
  230. nis_clear |= EMAC_DMA_INT_R;
  231. }
  232. /* [14]:Early Receive Interrupt. */
  233. EMAC_clear_pending(ETHERNET_MAC, nis_clear);
  234. }
  235. /* Abnormal interrupt summary. */
  236. if( status & EMAC_DMA_INT_AIS)
  237. {
  238. rt_uint32_t ais_clear = EMAC_DMA_INT_AIS;
  239. /* [1]:Transmit Process Stopped. */
  240. /* [3]:Transmit Jabber Timeout. */
  241. /* [4]: Receive FIFO Overflow. */
  242. /* [5]: Transmit Underflow. */
  243. /* [7]: Receive Buffer Unavailable. */
  244. /* [8]: Receive Process Stopped. */
  245. /* [9]: Receive Watchdog Timeout. */
  246. /* [10]: Early Transmit Interrupt. */
  247. /* [13]: Fatal Bus Error. */
  248. EMAC_clear_pending(ETHERNET_MAC, ais_clear);
  249. }
  250. }
  251. void ENET_IRQHandler(void)
  252. {
  253. /* enter interrupt */
  254. rt_interrupt_enter();
  255. EMAC_IRQHandler(&gd32_emac_device0);
  256. /* leave interrupt */
  257. rt_interrupt_leave();
  258. }
  259. /* EtherNet Device Interface */
  260. rt_err_t gd32_emac_tx( rt_device_t dev, struct pbuf* p)
  261. {
  262. struct pbuf* q;
  263. char * to;
  264. struct gd32_emac * gd32_emac_device;
  265. struct rt_synopsys_eth * ETHERNET_MAC;
  266. gd32_emac_device = (struct gd32_emac *)dev;
  267. ETHERNET_MAC = gd32_emac_device->ETHERNET_MAC;
  268. /* get free tx buffer */
  269. {
  270. rt_err_t result;
  271. result = rt_sem_take(&gd32_emac_device->tx_buf_free, RT_TICK_PER_SECOND/10);
  272. if (result != RT_EOK) return -RT_ERROR;
  273. }
  274. to = (char *)gd32_emac_device->DMATxDescToSet->Buffer1Addr;
  275. for (q = p; q != NULL; q = q->next)
  276. {
  277. /* Copy the frame to be sent into memory pointed by the current ETHERNET DMA Tx descriptor */
  278. memcpy(to, q->payload, q->len);
  279. to += q->len;
  280. }
  281. #ifdef EMAC_TX_DUMP
  282. {
  283. rt_uint32_t i;
  284. rt_uint8_t *ptr = (rt_uint8_t*)(gd32_emac_device->DMATxDescToSet->Buffer1Addr);
  285. EMAC_TRACE("\r\n%c%c tx_dump:", gd32_emac_device->parent.netif->name[0], gd32_emac_device->parent.netif->name[1]);
  286. for(i=0; i<p->tot_len; i++)
  287. {
  288. if( (i%8) == 0 )
  289. {
  290. EMAC_TRACE(" ");
  291. }
  292. if( (i%16) == 0 )
  293. {
  294. EMAC_TRACE("\r\n");
  295. }
  296. EMAC_TRACE("%02x ",*ptr);
  297. ptr++;
  298. }
  299. EMAC_TRACE("\r\ndump done!\r\n");
  300. }
  301. #endif
  302. /* Setting the Frame Length: bits[12:0] */
  303. gd32_emac_device->DMATxDescToSet->ControlBufferSize = (p->tot_len & EMAC_DMATxDesc_TBS1);
  304. /* Setting the last segment and first segment bits (in this case a frame is transmitted in one descriptor) */
  305. gd32_emac_device->DMATxDescToSet->Status |= EMAC_DMATxDesc_LS | EMAC_DMATxDesc_FS;
  306. /* Enable TX Completion Interrupt */
  307. gd32_emac_device->DMATxDescToSet->Status |= EMAC_DMATxDesc_IC;
  308. #ifdef CHECKSUM_BY_HARDWARE
  309. gd32_emac_device->DMATxDescToSet->Status |= EMAC_DMATxDesc_ChecksumTCPUDPICMPFull;
  310. /* clean ICMP checksum */
  311. {
  312. struct eth_hdr *ethhdr = (struct eth_hdr *)(gd32_emac_device->DMATxDescToSet->Buffer1Addr);
  313. /* is IP ? */
  314. if( ethhdr->type == htons(ETHTYPE_IP) )
  315. {
  316. struct ip_hdr *iphdr = (struct ip_hdr *)(gd32_emac_device->DMATxDescToSet->Buffer1Addr + SIZEOF_ETH_HDR);
  317. /* is ICMP ? */
  318. if( IPH_PROTO(iphdr) == IP_PROTO_ICMP )
  319. {
  320. struct icmp_echo_hdr *iecho = (struct icmp_echo_hdr *)(gd32_emac_device->DMATxDescToSet->Buffer1Addr + SIZEOF_ETH_HDR + sizeof(struct ip_hdr) );
  321. iecho->chksum = 0;
  322. }
  323. }
  324. }
  325. #endif
  326. /* Set Own bit of the Tx descriptor Status: gives the buffer back to ETHERNET DMA */
  327. gd32_emac_device->DMATxDescToSet->Status |= EMAC_DMATxDesc_OWN;
  328. /* When Tx Buffer unavailable flag is set: clear it and resume transmission */
  329. if ((ETHERNET_MAC->SR & EMAC_DMASR_TBUS) != (uint32_t)RESET)
  330. {
  331. /* Clear TBUS ETHERNET DMA flag */
  332. ETHERNET_MAC->SR = EMAC_DMASR_TBUS;
  333. /* Transmit Poll Demand to resume DMA transmission*/
  334. ETHERNET_MAC->TPDR = 0;
  335. }
  336. /* Update the ETHERNET DMA global Tx descriptor with next Tx decriptor */
  337. /* Chained Mode */
  338. /* Selects the next DMA Tx descriptor list for next buffer to send */
  339. gd32_emac_device->DMATxDescToSet = (EMAC_DMADESCTypeDef*) (gd32_emac_device->DMATxDescToSet->Buffer2NextDescAddr);
  340. /* Return SUCCESS */
  341. return RT_EOK;
  342. }
  343. /* reception a Ethernet packet. */
  344. struct pbuf * gd32_emac_rx(rt_device_t dev)
  345. {
  346. struct pbuf* p;
  347. rt_uint32_t framelength = 0;
  348. struct gd32_emac * gd32_emac_device;
  349. struct rt_synopsys_eth * ETHERNET_MAC;
  350. gd32_emac_device = (struct gd32_emac *)dev;
  351. ETHERNET_MAC = gd32_emac_device->ETHERNET_MAC;
  352. /* init p pointer */
  353. p = RT_NULL;
  354. /* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
  355. if(((gd32_emac_device->DMARxDescToGet->Status & EMAC_DMARxDesc_OWN) != (uint32_t)RESET))
  356. {
  357. return p;
  358. }
  359. if (((gd32_emac_device->DMARxDescToGet->Status & EMAC_DMARxDesc_ES) == (uint32_t)RESET) &&
  360. ((gd32_emac_device->DMARxDescToGet->Status & EMAC_DMARxDesc_LS) != (uint32_t)RESET) &&
  361. ((gd32_emac_device->DMARxDescToGet->Status & EMAC_DMARxDesc_FS) != (uint32_t)RESET))
  362. {
  363. /* Get the Frame Length of the received packet: substruct 4 bytes of the CRC */
  364. framelength = ((gd32_emac_device->DMARxDescToGet->Status & EMAC_DMARxDesc_FL)
  365. >> EMAC_DMARXDESC_FRAME_LENGTHSHIFT) - 4;
  366. /* allocate buffer */
  367. p = pbuf_alloc(PBUF_LINK, framelength, PBUF_RAM);
  368. if (p != RT_NULL)
  369. {
  370. const char * from;
  371. struct pbuf* q;
  372. from = (const char *)gd32_emac_device->DMARxDescToGet->Buffer1Addr;
  373. for (q = p; q != RT_NULL; q= q->next)
  374. {
  375. /* Copy the received frame into buffer from memory pointed by the current ETHERNET DMA Rx descriptor */
  376. memcpy(q->payload, from, q->len);
  377. from += q->len;
  378. }
  379. #ifdef EMAC_RX_DUMP
  380. {
  381. rt_uint32_t i;
  382. rt_uint8_t *ptr = (rt_uint8_t*)(gd32_emac_device->DMARxDescToGet->Buffer1Addr);
  383. EMAC_TRACE("\r\n%c%c rx_dump:", gd32_emac_device->parent.netif->name[0], gd32_emac_device->parent.netif->name[1]);
  384. for(i=0; i<p->tot_len; i++)
  385. {
  386. if( (i%8) == 0 )
  387. {
  388. EMAC_TRACE(" ");
  389. }
  390. if( (i%16) == 0 )
  391. {
  392. EMAC_TRACE("\r\n");
  393. }
  394. EMAC_TRACE("%02x ",*ptr);
  395. ptr++;
  396. }
  397. EMAC_TRACE("\r\ndump done!\r\n");
  398. }
  399. #endif
  400. }
  401. }
  402. /* Set Own bit of the Rx descriptor Status: gives the buffer back to ETHERNET DMA */
  403. gd32_emac_device->DMARxDescToGet->Status = EMAC_DMARxDesc_OWN;
  404. /* When Rx Buffer unavailable flag is set: clear it and resume reception */
  405. if ((ETHERNET_MAC->SR & EMAC_DMASR_RBUS) != (uint32_t)RESET)
  406. {
  407. /* Clear RBUS ETHERNET DMA flag */
  408. ETHERNET_MAC->SR = EMAC_DMASR_RBUS;
  409. /* Resume DMA reception */
  410. ETHERNET_MAC->RPDR = 0;
  411. }
  412. /* Update the ETHERNET DMA global Rx descriptor with next Rx decriptor */
  413. /* Chained Mode */
  414. if((gd32_emac_device->DMARxDescToGet->ControlBufferSize & EMAC_DMARxDesc_RCH) != (uint32_t)RESET)
  415. {
  416. /* Selects the next DMA Rx descriptor list for next buffer to read */
  417. gd32_emac_device->DMARxDescToGet = (EMAC_DMADESCTypeDef*) (gd32_emac_device->DMARxDescToGet->Buffer2NextDescAddr);
  418. }
  419. else /* Ring Mode */
  420. {
  421. if((gd32_emac_device->DMARxDescToGet->ControlBufferSize & EMAC_DMARxDesc_RER) != (uint32_t)RESET)
  422. {
  423. /* Selects the first DMA Rx descriptor for next buffer to read: last Rx descriptor was used */
  424. gd32_emac_device->DMARxDescToGet = (EMAC_DMADESCTypeDef*) (ETHERNET_MAC->RDLAR);
  425. }
  426. else
  427. {
  428. /* Selects the next DMA Rx descriptor list for next buffer to read */
  429. gd32_emac_device->DMARxDescToGet = (EMAC_DMADESCTypeDef*) ((uint32_t)gd32_emac_device->DMARxDescToGet + 0x10 + ((ETHERNET_MAC->BMR & EMAC_DMABMR_DSL) >> 2));
  430. }
  431. }
  432. return p;
  433. }
  434. /*!
  435. \brief configures the nested vectored interrupt controller
  436. \param[in] none
  437. \param[out] none
  438. \retval none
  439. */
  440. static void nvic_configuration(void)
  441. {
  442. nvic_vector_table_set(NVIC_VECTTAB_FLASH, 0x0);
  443. nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
  444. nvic_irq_enable(ENET_IRQn, 0, 0);
  445. }
  446. /*!
  447. \brief configures the different GPIO ports
  448. \param[in] none
  449. \param[out] none
  450. \retval none
  451. */
  452. static void enet_gpio_config(void)
  453. {
  454. rcu_periph_clock_enable(RCU_GPIOA);
  455. rcu_periph_clock_enable(RCU_GPIOB);
  456. rcu_periph_clock_enable(RCU_GPIOC);
  457. rcu_periph_clock_enable(RCU_GPIOD);
  458. rcu_periph_clock_enable(RCU_GPIOG);
  459. rcu_periph_clock_enable(RCU_GPIOH);
  460. rcu_periph_clock_enable(RCU_GPIOI);
  461. gpio_af_set(GPIOA, GPIO_AF_0, GPIO_PIN_8);
  462. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_8);
  463. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_200MHZ,GPIO_PIN_8);
  464. /* enable SYSCFG clock */
  465. rcu_periph_clock_enable(RCU_SYSCFG);
  466. /* choose DIV2 to get 50MHz from 200MHz on CKOUT0 pin (PA8) to clock the PHY */
  467. rcu_ckout0_config(RCU_CKOUT0SRC_PLLP, RCU_CKOUT0_DIV4);
  468. syscfg_enet_phy_interface_config(SYSCFG_ENET_PHY_RMII);
  469. /* PA1: ETH_RMII_REF_CLK */
  470. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_1);
  471. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_200MHZ,GPIO_PIN_1);
  472. /* PA2: ETH_MDIO */
  473. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_2);
  474. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_200MHZ,GPIO_PIN_2);
  475. /* PA7: ETH_RMII_CRS_DV */
  476. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_7);
  477. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_200MHZ,GPIO_PIN_7);
  478. gpio_af_set(GPIOA, GPIO_AF_11, GPIO_PIN_1);
  479. gpio_af_set(GPIOA, GPIO_AF_11, GPIO_PIN_2);
  480. gpio_af_set(GPIOA, GPIO_AF_11, GPIO_PIN_7);
  481. /* PB11: ETH_RMII_TX_EN */
  482. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_11);
  483. gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_200MHZ,GPIO_PIN_11);
  484. /* PB12: ETH_RMII_TXD0 */
  485. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_12);
  486. gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_200MHZ,GPIO_PIN_12);
  487. /* PB13: ETH_RMII_TXD1 */
  488. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_13);
  489. gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_200MHZ,GPIO_PIN_13);
  490. gpio_af_set(GPIOB, GPIO_AF_11, GPIO_PIN_11);
  491. gpio_af_set(GPIOB, GPIO_AF_11, GPIO_PIN_12);
  492. gpio_af_set(GPIOB, GPIO_AF_11, GPIO_PIN_13);
  493. /* PC1: ETH_MDC */
  494. gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_1);
  495. gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_200MHZ,GPIO_PIN_1);
  496. /* PC4: ETH_RMII_RXD0 */
  497. gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_4);
  498. gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_200MHZ,GPIO_PIN_4);
  499. /* PC5: ETH_RMII_RXD1 */
  500. gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_5);
  501. gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_200MHZ,GPIO_PIN_5);
  502. gpio_af_set(GPIOC, GPIO_AF_11, GPIO_PIN_1);
  503. gpio_af_set(GPIOC, GPIO_AF_11, GPIO_PIN_4);
  504. gpio_af_set(GPIOC, GPIO_AF_11, GPIO_PIN_5);
  505. }
  506. int rt_hw_gd32_eth_init(void)
  507. {
  508. rt_kprintf("rt_gd32_eth_init...\n");
  509. /* enable ethernet clock */
  510. rcu_periph_clock_enable(RCU_ENET);
  511. rcu_periph_clock_enable(RCU_ENETTX);
  512. rcu_periph_clock_enable(RCU_ENETRX);
  513. nvic_configuration();
  514. /* configure the GPIO ports for ethernet pins */
  515. enet_gpio_config();
  516. /* set autonegotiation mode */
  517. gd32_emac_device0.phy_mode = EMAC_PHY_AUTO;
  518. gd32_emac_device0.ETHERNET_MAC = ETHERNET_MAC0;
  519. gd32_emac_device0.ETHER_MAC_IRQ = ENET_IRQn;
  520. // OUI 00-00-0E FUJITSU LIMITED
  521. gd32_emac_device0.dev_addr[0] = 0x00;
  522. gd32_emac_device0.dev_addr[1] = 0x00;
  523. gd32_emac_device0.dev_addr[2] = 0x0E;
  524. /* set mac address: (only for test) */
  525. gd32_emac_device0.dev_addr[3] = 0x12;
  526. gd32_emac_device0.dev_addr[4] = 0x34;
  527. gd32_emac_device0.dev_addr[5] = 0x56;
  528. gd32_emac_device0.parent.parent.init = gd32_emac_init;
  529. gd32_emac_device0.parent.parent.open = gd32_emac_open;
  530. gd32_emac_device0.parent.parent.close = gd32_emac_close;
  531. gd32_emac_device0.parent.parent.read = gd32_emac_read;
  532. gd32_emac_device0.parent.parent.write = gd32_emac_write;
  533. gd32_emac_device0.parent.parent.control = gd32_emac_control;
  534. gd32_emac_device0.parent.parent.user_data = RT_NULL;
  535. gd32_emac_device0.parent.eth_rx = gd32_emac_rx;
  536. gd32_emac_device0.parent.eth_tx = gd32_emac_tx;
  537. /* init tx buffer free semaphore */
  538. rt_sem_init(&gd32_emac_device0.tx_buf_free, "tx_buf0", EMAC_TXBUFNB, RT_IPC_FLAG_FIFO);
  539. eth_device_init(&(gd32_emac_device0.parent), "e0");
  540. return 0;
  541. }
  542. INIT_DEVICE_EXPORT(rt_hw_gd32_eth_init);