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 GD32F3XX 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_GD32307C_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_MAX, GPIO_PIN_1);
269 gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_2MHZ, GPIO_PIN_2);
271 gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_MAX, GPIO_PIN_7);
274 gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_MAX, GPIO_PIN_11);
276 gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_MAX, GPIO_PIN_12);
278 gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_MAX, GPIO_PIN_13);
281 gpio_init(GPIOC, GPIO_MODE_AF_PP, GPIO_OSPEED_2MHZ, GPIO_PIN_1);
283 gpio_init(GPIOC, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_MAX, GPIO_PIN_4);
285 gpio_init(GPIOC, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_MAX, 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;
556 ENET_DMA_STAT = ENET_DMA_STAT_TBU;
564 if((txCurDmaDesc->
tdes0 & ENET_TDES0_DAV) == 0)
588 if((rxCurDmaDesc->
rdes0 & ENET_RDES0_DAV) == 0)
591 if((rxCurDmaDesc->
rdes0 & ENET_RDES0_FDES) != 0 &&
592 (rxCurDmaDesc->
rdes0 & ENET_RDES0_LDES) != 0)
595 if((rxCurDmaDesc->
rdes0 & ENET_RDES0_ERRS) == 0)
598 n = (rxCurDmaDesc->
rdes0 & ENET_RDES0_FRML) >> 16;
625 rxCurDmaDesc->
rdes0 = ENET_RDES0_DAV;
636 ENET_DMA_STAT = ENET_DMA_STAT_RBU;
657 uint32_t hashTable[2];
665 ENET_MAC_ADDR0L = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
666 ENET_MAC_ADDR0H = interface->macAddr.w[2] | ENET_MAC_ADDR0H_MO;
682 entry = &interface->macAddrFilter[i];
695 k = (crc >> 26) & 0x3F;
698 hashTable[k / 32] |= (1 << (k % 32));
706 unicastMacAddr[j++] = entry->
addr;
716 ENET_MAC_ADDR1L = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
717 ENET_MAC_ADDR1H = unicastMacAddr[0].w[2] | ENET_MAC_ADDR1H_AFE;
730 ENET_MAC_ADDR2L = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
731 ENET_MAC_ADDT2H = unicastMacAddr[1].w[2] | ENET_MAC_ADDR2H_AFE;
744 ENET_MAC_ADDR3L = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
745 ENET_MAC_ADDR3H = unicastMacAddr[2].w[2] | ENET_MAC_ADDR3H_AFE;
755 ENET_MAC_HLL = hashTable[0];
756 ENET_MAC_HLH = hashTable[1];
759 TRACE_DEBUG(
" ENET_MAC_HLL = %08" PRIX32
"\r\n", ENET_MAC_HLL);
760 TRACE_DEBUG(
" ENET_MAC_HLH = %08" PRIX32
"\r\n", ENET_MAC_HLH);
778 config = ENET_MAC_CFG;
783 config |= ENET_MAC_CFG_SPD;
787 config &= ~ENET_MAC_CFG_SPD;
793 config |= ENET_MAC_CFG_DPM;
797 config &= ~ENET_MAC_CFG_DPM;
801 ENET_MAC_CFG = config;
825 temp = ENET_MAC_PHY_CTL & ENET_MAC_PHY_CTL_CLR;
827 temp |= ENET_MAC_PHY_CTL_PW | ENET_MAC_PHY_CTL_PB;
829 temp |= MAC_PHY_CTL_PA(phyAddr);
831 temp |= MAC_PHY_CTL_PR(
regAddr);
834 ENET_MAC_PHY_DATA =
data & ENET_MAC_PHY_DATA_PD;
837 ENET_MAC_PHY_CTL = temp;
839 while((ENET_MAC_PHY_CTL & ENET_MAC_PHY_CTL_PB) != 0)
868 temp = ENET_MAC_PHY_CTL & ENET_MAC_PHY_CTL_CLR;
870 temp |= ENET_MAC_PHY_CTL_PB;
872 temp |= MAC_PHY_CTL_PA(phyAddr);
874 temp |= MAC_PHY_CTL_PR(
regAddr);
877 ENET_MAC_PHY_CTL = temp;
879 while((ENET_MAC_PHY_CTL & ENET_MAC_PHY_CTL_PB) != 0)
884 data = ENET_MAC_PHY_DATA & ENET_MAC_PHY_DATA_PD;
912 p = (uint8_t *)
data;
917 for(i = 0; i <
length; i++)
920 for(j = 0; j < 8; j++)
923 if((((crc >> 31) ^ (
p[i] >> j)) & 0x01) != 0)
925 crc = (crc << 1) ^ 0x04C11DB7;