32 #define TRACE_LEVEL NIC_TRACE_LEVEL
44 #if defined(__ICCARM__)
47 #pragma data_alignment = 4
50 #pragma data_alignment = 4
53 #pragma data_alignment = 4
56 #pragma data_alignment = 4
119 TRACE_INFO(
"Initializing GD32E5XX Ethernet MAC...\r\n");
122 nicDriverInterface = interface;
128 rcu_periph_clock_enable(RCU_ENET);
129 rcu_periph_clock_enable(RCU_ENETTX);
130 rcu_periph_clock_enable(RCU_ENETRX);
133 rcu_periph_reset_enable(RCU_ENETRST);
134 rcu_periph_reset_disable(RCU_ENETRST);
137 ENET_DMA_BCTL |= ENET_DMA_BCTL_SWR;
139 while((ENET_DMA_BCTL & ENET_DMA_BCTL_SWR) != 0)
144 ENET_MAC_PHY_CTL = ENET_MDC_HCLK_DIV62;
147 if(interface->phyDriver != NULL)
150 error = interface->phyDriver->init(interface);
152 else if(interface->switchDriver != NULL)
155 error = interface->switchDriver->init(interface);
170 ENET_MAC_CFG = ENET_MAC_CFG_ROD;
173 ENET_MAC_ADDR0L = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
174 ENET_MAC_ADDR0H = interface->macAddr.w[2] | ENET_MAC_ADDR0H_MO;
189 ENET_MAC_FRMF = ENET_MAC_FRMF_HPFLT | ENET_MAC_FRMF_HMF;
193 ENET_DMA_CTL = ENET_DMA_CTL_RSFD | ENET_DMA_CTL_TSFD;
196 ENET_DMA_BCTL = ENET_DMA_BCTL_AA | ENET_DMA_BCTL_UIP | ENET_RXDP_32BEAT |
197 ENET_ARBITRATION_RXTX_1_1 | ENET_PGBL_32BEAT | ENET_DMA_BCTL_DFM;
204 ENET_MSC_TINTMSK = ENET_MSC_TINTMSK_TGFIM | ENET_MSC_TINTMSK_TGFMSCIM |
205 ENET_MSC_TINTMSK_TGFSCIM;
209 ENET_MSC_RINTMSK = ENET_MSC_RINTMSK_RGUFIM | ENET_MSC_RINTMSK_RFAEIM |
210 ENET_MSC_RINTMSK_RFCEIM;
213 ENET_MAC_INTMSK = ENET_MAC_INTMSK_TMSTIM | ENET_MAC_INTMSK_WUMIM;
215 ENET_DMA_INTEN = ENET_DMA_INTEN_NIE | ENET_DMA_INTEN_RIE | ENET_DMA_INTEN_TIE;
225 ENET_MAC_CFG |= ENET_MAC_CFG_TEN | ENET_MAC_CFG_REN;
227 ENET_DMA_CTL |= ENET_DMA_CTL_STE | ENET_DMA_CTL_SRE;
245 #if defined(USE_GD32E507Z_EVAL)
247 rcu_periph_clock_enable(RCU_AF);
250 rcu_periph_clock_enable(RCU_GPIOA);
251 rcu_periph_clock_enable(RCU_GPIOB);
252 rcu_periph_clock_enable(RCU_GPIOC);
255 gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_MAX, GPIO_PIN_8);
258 rcu_pll2_config(RCU_PLL2_MUL10);
259 rcu_osci_on(RCU_PLL2_CK);
260 rcu_osci_stab_wait(RCU_PLL2_CK);
261 rcu_ckout0_config(RCU_CKOUT0SRC_CKPLL2);
264 gpio_ethernet_phy_select(GPIO_ENET_PHY_RMII);
267 gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
269 gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
271 gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
274 gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_11);
276 gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12);
278 gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13);
281 gpio_init(GPIOC, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
283 gpio_init(GPIOC, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
285 gpio_init(GPIOC, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_5);
303 txDmaDesc[i].tdes0 = ENET_TDES0_INTC | ENET_TDES0_TCHM;
367 if(interface->phyDriver != NULL)
370 interface->phyDriver->tick(interface);
372 else if(interface->switchDriver != NULL)
375 interface->switchDriver->tick(interface);
392 NVIC_EnableIRQ(ENET_IRQn);
395 if(interface->phyDriver != NULL)
398 interface->phyDriver->enableIrq(interface);
400 else if(interface->switchDriver != NULL)
403 interface->switchDriver->enableIrq(interface);
420 NVIC_DisableIRQ(ENET_IRQn);
423 if(interface->phyDriver != NULL)
426 interface->phyDriver->disableIrq(interface);
428 else if(interface->switchDriver != NULL)
431 interface->switchDriver->disableIrq(interface);
456 status = ENET_DMA_STAT;
459 if((status & ENET_DMA_STAT_TS) != 0)
462 ENET_DMA_STAT = ENET_DMA_STAT_TS;
465 if((txCurDmaDesc->
tdes0 & ENET_TDES0_DAV) == 0)
473 if((status & ENET_DMA_STAT_RS) != 0)
476 ENET_DMA_STAT = ENET_DMA_STAT_RS;
479 nicDriverInterface->nicEvent =
TRUE;
485 ENET_DMA_STAT = ENET_DMA_STAT_NI;
540 if((txCurDmaDesc->
tdes0 & ENET_TDES0_DAV) != 0)
551 txCurDmaDesc->
tdes0 |= ENET_TDES0_LSG | ENET_TDES0_FSG;
553 txCurDmaDesc->
tdes0 |= ENET_TDES0_DAV;
559 ENET_DMA_STAT = ENET_DMA_STAT_TBU;
567 if((txCurDmaDesc->
tdes0 & ENET_TDES0_DAV) == 0)
591 if((rxCurDmaDesc->
rdes0 & ENET_RDES0_DAV) == 0)
594 if((rxCurDmaDesc->
rdes0 & ENET_RDES0_FDES) != 0 &&
595 (rxCurDmaDesc->
rdes0 & ENET_RDES0_LDES) != 0)
598 if((rxCurDmaDesc->
rdes0 & ENET_RDES0_ERRS) == 0)
601 n = (rxCurDmaDesc->
rdes0 & ENET_RDES0_FRML) >> 16;
628 rxCurDmaDesc->
rdes0 = ENET_RDES0_DAV;
639 ENET_DMA_STAT = ENET_DMA_STAT_RBU;
660 uint32_t hashTable[2];
668 ENET_MAC_ADDR0L = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
669 ENET_MAC_ADDR0H = interface->macAddr.w[2] | ENET_MAC_ADDR0H_MO;
685 entry = &interface->macAddrFilter[i];
698 k = (crc >> 26) & 0x3F;
701 hashTable[k / 32] |= (1 << (k % 32));
709 unicastMacAddr[j++] = entry->
addr;
719 ENET_MAC_ADDR1L = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
720 ENET_MAC_ADDR1H = unicastMacAddr[0].w[2] | ENET_MAC_ADDR1H_AFE;
733 ENET_MAC_ADDR2L = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
734 ENET_MAC_ADDT2H = unicastMacAddr[1].w[2] | ENET_MAC_ADDR2H_AFE;
747 ENET_MAC_ADDR3L = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
748 ENET_MAC_ADDR3H = unicastMacAddr[2].w[2] | ENET_MAC_ADDR3H_AFE;
758 ENET_MAC_HLL = hashTable[0];
759 ENET_MAC_HLH = hashTable[1];
762 TRACE_DEBUG(
" ENET_MAC_HLL = %08" PRIX32
"\r\n", ENET_MAC_HLL);
763 TRACE_DEBUG(
" ENET_MAC_HLH = %08" PRIX32
"\r\n", ENET_MAC_HLH);
781 config = ENET_MAC_CFG;
786 config |= ENET_MAC_CFG_SPD;
790 config &= ~ENET_MAC_CFG_SPD;
796 config |= ENET_MAC_CFG_DPM;
800 config &= ~ENET_MAC_CFG_DPM;
804 ENET_MAC_CFG = config;
828 temp = ENET_MAC_PHY_CTL & ENET_MAC_PHY_CTL_CLR;
830 temp |= ENET_MAC_PHY_CTL_PW | ENET_MAC_PHY_CTL_PB;
832 temp |= MAC_PHY_CTL_PA(phyAddr);
834 temp |= MAC_PHY_CTL_PR(
regAddr);
837 ENET_MAC_PHY_DATA =
data & ENET_MAC_PHY_DATA_PD;
840 ENET_MAC_PHY_CTL = temp;
842 while((ENET_MAC_PHY_CTL & ENET_MAC_PHY_CTL_PB) != 0)
871 temp = ENET_MAC_PHY_CTL & ENET_MAC_PHY_CTL_CLR;
873 temp |= ENET_MAC_PHY_CTL_PB;
875 temp |= MAC_PHY_CTL_PA(phyAddr);
877 temp |= MAC_PHY_CTL_PR(
regAddr);
880 ENET_MAC_PHY_CTL = temp;
882 while((ENET_MAC_PHY_CTL & ENET_MAC_PHY_CTL_PB) != 0)
887 data = ENET_MAC_PHY_DATA & ENET_MAC_PHY_DATA_PD;
915 p = (uint8_t *)
data;
920 for(i = 0; i <
length; i++)
923 for(j = 0; j < 8; j++)
926 if((((crc >> 31) ^ (
p[i] >> j)) & 0x01) != 0)
928 crc = (crc << 1) ^ 0x04C11DB7;