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 GD32F2XX 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;
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_GD32207C_EVAL) || defined(USE_GD32207I_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_50MHZ, 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, RCU_CKOUT0_DIV1);
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_2MHZ, 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_2MHZ, 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;
354 if(interface->phyDriver != NULL)
357 interface->phyDriver->tick(interface);
359 else if(interface->switchDriver != NULL)
362 interface->switchDriver->tick(interface);
379 NVIC_EnableIRQ(ENET_IRQn);
382 if(interface->phyDriver != NULL)
385 interface->phyDriver->enableIrq(interface);
387 else if(interface->switchDriver != NULL)
390 interface->switchDriver->enableIrq(interface);
407 NVIC_DisableIRQ(ENET_IRQn);
410 if(interface->phyDriver != NULL)
413 interface->phyDriver->disableIrq(interface);
415 else if(interface->switchDriver != NULL)
418 interface->switchDriver->disableIrq(interface);
443 status = ENET_DMA_STAT;
446 if((status & ENET_DMA_STAT_TS) != 0)
449 ENET_DMA_STAT = ENET_DMA_STAT_TS;
452 if((txCurDmaDesc->
tdes0 & ENET_TDES0_DAV) == 0)
460 if((status & ENET_DMA_STAT_RS) != 0)
463 ENET_DMA_STAT = ENET_DMA_STAT_RS;
466 nicDriverInterface->nicEvent =
TRUE;
472 ENET_DMA_STAT = ENET_DMA_STAT_NI;
527 if((txCurDmaDesc->
tdes0 & ENET_TDES0_DAV) != 0)
538 txCurDmaDesc->
tdes0 |= ENET_TDES0_LSG | ENET_TDES0_FSG;
540 txCurDmaDesc->
tdes0 |= ENET_TDES0_DAV;
543 ENET_DMA_STAT = ENET_DMA_STAT_TBU;
551 if((txCurDmaDesc->
tdes0 & ENET_TDES0_DAV) == 0)
575 if((rxCurDmaDesc->
rdes0 & ENET_RDES0_DAV) == 0)
578 if((rxCurDmaDesc->
rdes0 & ENET_RDES0_FDES) != 0 &&
579 (rxCurDmaDesc->
rdes0 & ENET_RDES0_LDES) != 0)
582 if((rxCurDmaDesc->
rdes0 & ENET_RDES0_ERRS) == 0)
585 n = (rxCurDmaDesc->
rdes0 & ENET_RDES0_FRML) >> 16;
612 rxCurDmaDesc->
rdes0 = ENET_RDES0_DAV;
623 ENET_DMA_STAT = ENET_DMA_STAT_RBU;
644 uint32_t hashTable[2];
652 ENET_MAC_ADDR0L = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
653 ENET_MAC_ADDR0H = interface->macAddr.w[2] | ENET_MAC_ADDR0H_MO;
669 entry = &interface->macAddrFilter[i];
682 k = (crc >> 26) & 0x3F;
685 hashTable[k / 32] |= (1 << (k % 32));
693 unicastMacAddr[j++] = entry->
addr;
703 ENET_MAC_ADDR1L = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
704 ENET_MAC_ADDR1H = unicastMacAddr[0].w[2] | ENET_MAC_ADDR1H_AFE;
717 ENET_MAC_ADDR2L = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
718 ENET_MAC_ADDT2H = unicastMacAddr[1].w[2] | ENET_MAC_ADDR2H_AFE;
731 ENET_MAC_ADDR3L = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
732 ENET_MAC_ADDR3H = unicastMacAddr[2].w[2] | ENET_MAC_ADDR3H_AFE;
742 ENET_MAC_HLL = hashTable[0];
743 ENET_MAC_HLH = hashTable[1];
746 TRACE_DEBUG(
" ENET_MAC_HLL = %08" PRIX32
"\r\n", ENET_MAC_HLL);
747 TRACE_DEBUG(
" ENET_MAC_HLH = %08" PRIX32
"\r\n", ENET_MAC_HLH);
765 config = ENET_MAC_CFG;
770 config |= ENET_MAC_CFG_SPD;
774 config &= ~ENET_MAC_CFG_SPD;
780 config |= ENET_MAC_CFG_DPM;
784 config &= ~ENET_MAC_CFG_DPM;
788 ENET_MAC_CFG = config;
812 temp = ENET_MAC_PHY_CTL & ENET_MAC_PHY_CTL_CLR;
814 temp |= ENET_MAC_PHY_CTL_PW | ENET_MAC_PHY_CTL_PB;
816 temp |= MAC_PHY_CTL_PA(phyAddr);
818 temp |= MAC_PHY_CTL_PR(
regAddr);
821 ENET_MAC_PHY_DATA =
data & ENET_MAC_PHY_DATA_PD;
824 ENET_MAC_PHY_CTL = temp;
826 while((ENET_MAC_PHY_CTL & ENET_MAC_PHY_CTL_PB) != 0)
855 temp = ENET_MAC_PHY_CTL & ENET_MAC_PHY_CTL_CLR;
857 temp |= ENET_MAC_PHY_CTL_PB;
859 temp |= MAC_PHY_CTL_PA(phyAddr);
861 temp |= MAC_PHY_CTL_PR(
regAddr);
864 ENET_MAC_PHY_CTL = temp;
866 while((ENET_MAC_PHY_CTL & ENET_MAC_PHY_CTL_PB) != 0)
871 data = ENET_MAC_PHY_DATA & ENET_MAC_PHY_DATA_PD;
899 p = (uint8_t *)
data;
904 for(i = 0; i <
length; i++)
907 for(j = 0; j < 8; j++)
910 if((((crc >> 31) ^ (
p[i] >> j)) & 0x01) != 0)
912 crc = (crc << 1) ^ 0x04C11DB7;