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 LPC18xx Ethernet MAC...\r\n");
122 nicDriverInterface = interface;
125 LPC_CCU1->CLK_M3_ETHERNET_CFG |= CCU1_CLK_M3_ETHERNET_CFG_RUN_Msk;
127 while((LPC_CCU1->CLK_M3_ETHERNET_STAT & CCU1_CLK_M3_ETHERNET_STAT_RUN_Msk) == 0)
132 LPC_RGU->RESET_EXT_STAT19 |= RGU_RESET_EXT_STAT19_MASTER_RESET_Msk;
133 LPC_RGU->RESET_EXT_STAT19 &= ~RGU_RESET_EXT_STAT19_MASTER_RESET_Msk;
136 LPC_RGU->RESET_EXT_STAT22 |= RGU_RESET_EXT_STAT22_MASTER_RESET_Msk;
137 LPC_RGU->RESET_EXT_STAT22 &= ~RGU_RESET_EXT_STAT22_MASTER_RESET_Msk;
143 LPC_RGU->RESET_CTRL0 = RGU_RESET_CTRL0_ETHERNET_RST_Msk;
145 while((LPC_RGU->RESET_ACTIVE_STATUS0 & RGU_RESET_ACTIVE_STATUS0_ETHERNET_RST_Msk) == 0)
150 LPC_ETHERNET->DMA_BUS_MODE |= ETHERNET_DMA_BUS_MODE_SWR_Msk;
152 while((LPC_ETHERNET->DMA_BUS_MODE & ETHERNET_DMA_BUS_MODE_SWR_Msk) != 0)
160 if(interface->phyDriver != NULL)
163 error = interface->phyDriver->init(interface);
165 else if(interface->switchDriver != NULL)
168 error = interface->switchDriver->init(interface);
183 LPC_ETHERNET->MAC_CONFIG = ETHERNET_MAC_CONFIG_DO_Msk;
186 LPC_ETHERNET->MAC_ADDR0_LOW = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
187 LPC_ETHERNET->MAC_ADDR0_HIGH = interface->macAddr.w[2];
190 LPC_ETHERNET->MAC_HASHTABLE_LOW = 0;
191 LPC_ETHERNET->MAC_HASHTABLE_HIGH = 0;
194 LPC_ETHERNET->MAC_FRAME_FILTER = ETHERNET_MAC_FRAME_FILTER_HPF_Msk |
195 ETHERNET_MAC_FRAME_FILTER_HMC_Msk;
198 LPC_ETHERNET->MAC_FLOW_CTRL = 0;
203 LPC_ETHERNET->DMA_BUS_MODE = ETHERNET_DMA_BUS_MODE_AAL_Msk | ETHERNET_DMA_BUS_MODE_USP_Msk |
211 LPC_ETHERNET->MAC_INTR_MASK = ETHERNET_MAC_INTR_MASK_TSIM_Msk |
212 ETHERNET_MAC_INTR_MASK_PMTIM_Msk;
215 LPC_ETHERNET->DMA_INT_EN = ETHERNET_DMA_INT_EN_NIE_Msk |
216 ETHERNET_DMA_INT_EN_AIE_Msk | ETHERNET_DMA_INT_EN_RIE_Msk |
217 ETHERNET_DMA_INT_EN_OVE_Msk | ETHERNET_DMA_INT_EN_TIE_Msk |
218 ETHERNET_DMA_INT_EN_UNE_Msk;
228 LPC_ETHERNET->MAC_CONFIG |= ETHERNET_MAC_CONFIG_TE_Msk | ETHERNET_MAC_CONFIG_RE_Msk;
230 LPC_ETHERNET->DMA_OP_MODE |= ETHERNET_DMA_OP_MODE_ST_Msk | ETHERNET_DMA_OP_MODE_SR_Msk;
248 #if defined(USE_LPC1830_XPLORER)
250 LPC_CCU1->CLK_M3_GPIO_CFG |= CCU1_CLK_M3_GPIO_CFG_RUN_Msk;
252 while((LPC_CCU1->CLK_M3_GPIO_STAT & CCU1_CLK_M3_GPIO_STAT_RUN_Msk) == 0)
257 LPC_CREG->CREG6 &= ~CREG_CREG6_ETHMODE_Msk;
261 LPC_SCU->SFSP0_0 = SCU_SFSP0_0_EZI_Msk | SCU_SFSP0_0_EHS_Msk | (2 & SCU_SFSP0_0_MODE_Msk);
263 LPC_SCU->SFSP0_1 = SCU_SFSP0_1_EHS_Msk | (6 & SCU_SFSP0_1_MODE_Msk);
266 LPC_SCU->SFSP1_15 = SCU_SFSP1_15_EZI_Msk | SCU_SFSP1_15_EHS_Msk | (3 & SCU_SFSP1_15_MODE_Msk);
268 LPC_SCU->SFSP1_16 = SCU_SFSP1_16_EZI_Msk | SCU_SFSP1_16_EHS_Msk | (7 & SCU_SFSP1_16_MODE_Msk);
270 LPC_SCU->SFSP1_17 = SCU_SFSP1_17_EZI_Msk | (3 & SCU_SFSP1_17_MODE_Msk);
272 LPC_SCU->SFSP1_18 = SCU_SFSP1_18_EHS_Msk | (3 & SCU_SFSP1_18_MODE_Msk);
274 LPC_SCU->SFSP1_19 = SCU_SFSP1_19_EZI_Msk | SCU_SFSP1_19_EHS_Msk | (0 & SCU_SFSP1_19_MODE_Msk);
276 LPC_SCU->SFSP1_20 = SCU_SFSP1_20_EHS_Msk | (3 & SCU_SFSP1_20_MODE_Msk);
279 LPC_SCU->SFSP2_0 = (7 & SCU_SFSP2_0_MODE_Msk);
343 LPC_ETHERNET->DMA_TRANS_DES_ADDR = (uint32_t)
txDmaDesc;
345 LPC_ETHERNET->DMA_REC_DES_ADDR = (uint32_t)
rxDmaDesc;
361 if(interface->phyDriver != NULL)
364 interface->phyDriver->tick(interface);
366 else if(interface->switchDriver != NULL)
369 interface->switchDriver->tick(interface);
386 NVIC_EnableIRQ(ETHERNET_IRQn);
389 if(interface->phyDriver != NULL)
392 interface->phyDriver->enableIrq(interface);
394 else if(interface->switchDriver != NULL)
397 interface->switchDriver->enableIrq(interface);
414 NVIC_DisableIRQ(ETHERNET_IRQn);
417 if(interface->phyDriver != NULL)
420 interface->phyDriver->disableIrq(interface);
422 else if(interface->switchDriver != NULL)
425 interface->switchDriver->disableIrq(interface);
450 status = LPC_ETHERNET->DMA_STAT;
453 if((status & (ETHERNET_DMA_STAT_TI_Msk | ETHERNET_DMA_STAT_UNF_Msk)) != 0)
456 LPC_ETHERNET->DMA_STAT = ETHERNET_DMA_STAT_TI_Msk | ETHERNET_DMA_STAT_UNF_Msk;
467 if((status & (ETHERNET_DMA_STAT_RI_Msk | ETHERNET_DMA_STAT_OVF_Msk)) != 0)
470 LPC_ETHERNET->DMA_INT_EN &= ~(ETHERNET_DMA_INT_EN_RIE_Msk |
471 ETHERNET_DMA_INT_EN_OVE_Msk);
474 nicDriverInterface->nicEvent =
TRUE;
480 LPC_ETHERNET->DMA_STAT = ETHERNET_DMA_STAT_NIS_Msk | ETHERNET_DMA_STAT_AIE_Msk;
497 if((LPC_ETHERNET->DMA_STAT & (ETHERNET_DMA_STAT_RI_Msk | ETHERNET_DMA_STAT_OVF_Msk)) != 0)
500 LPC_ETHERNET->DMA_STAT = ETHERNET_DMA_STAT_RI_Msk | ETHERNET_DMA_STAT_OVF_Msk;
513 LPC_ETHERNET->DMA_INT_EN = ETHERNET_DMA_INT_EN_NIE_Msk |
514 ETHERNET_DMA_INT_EN_AIE_Msk | ETHERNET_DMA_INT_EN_RIE_Msk |
515 ETHERNET_DMA_INT_EN_OVE_Msk | ETHERNET_DMA_INT_EN_TIE_Msk |
516 ETHERNET_DMA_INT_EN_UNE_Msk;
564 LPC_ETHERNET->DMA_STAT = ETHERNET_DMA_STAT_TU_Msk;
566 LPC_ETHERNET->DMA_TRANS_POLL_DEMAND = 0;
644 LPC_ETHERNET->DMA_STAT = ETHERNET_DMA_STAT_RU_Msk;
646 LPC_ETHERNET->DMA_REC_POLL_DEMAND = 0;
664 uint32_t hashTable[2];
671 LPC_ETHERNET->MAC_ADDR0_LOW = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
672 LPC_ETHERNET->MAC_ADDR0_HIGH = interface->macAddr.w[2];
683 entry = &interface->macAddrFilter[i];
693 k = (crc >> 26) & 0x3F;
696 hashTable[k / 32] |= (1 << (k % 32));
701 LPC_ETHERNET->MAC_HASHTABLE_LOW = hashTable[0];
702 LPC_ETHERNET->MAC_HASHTABLE_HIGH = hashTable[1];
705 TRACE_DEBUG(
" MAC_HASHTABLE_LOW = %08" PRIX32
"\r\n", LPC_ETHERNET->MAC_HASHTABLE_LOW);
706 TRACE_DEBUG(
" MAC_HASHTABLE_HIGH = %08" PRIX32
"\r\n", LPC_ETHERNET->MAC_HASHTABLE_HIGH);
724 config = LPC_ETHERNET->MAC_CONFIG;
729 config |= ETHERNET_MAC_CONFIG_FES_Msk;
733 config &= ~ETHERNET_MAC_CONFIG_FES_Msk;
739 config |= ETHERNET_MAC_CONFIG_DM_Msk;
743 config &= ~ETHERNET_MAC_CONFIG_DM_Msk;
747 LPC_ETHERNET->MAC_CONFIG = config;
771 temp = LPC_ETHERNET->MAC_MII_ADDR & ETHERNET_MAC_MII_ADDR_CR_Msk;
773 temp |= ETHERNET_MAC_MII_ADDR_W_Msk | ETHERNET_MAC_MII_ADDR_GB_Msk;
775 temp |= (phyAddr << ETHERNET_MAC_MII_ADDR_PA_Pos) & ETHERNET_MAC_MII_ADDR_PA_Msk;
777 temp |= (
regAddr << ETHERNET_MAC_MII_ADDR_GR_Pos) & ETHERNET_MAC_MII_ADDR_GR_Msk;
780 LPC_ETHERNET->MAC_MII_DATA =
data & ETHERNET_MAC_MII_DATA_GD_Msk;
783 LPC_ETHERNET->MAC_MII_ADDR = temp;
785 while((LPC_ETHERNET->MAC_MII_ADDR & ETHERNET_MAC_MII_ADDR_GB_Msk) != 0)
814 temp = LPC_ETHERNET->MAC_MII_ADDR & ETHERNET_MAC_MII_ADDR_CR_Msk;
816 temp |= ETHERNET_MAC_MII_ADDR_GB_Msk;
818 temp |= (phyAddr << ETHERNET_MAC_MII_ADDR_PA_Pos) & ETHERNET_MAC_MII_ADDR_PA_Msk;
820 temp |= (
regAddr << ETHERNET_MAC_MII_ADDR_GR_Pos) & ETHERNET_MAC_MII_ADDR_GR_Msk;
823 LPC_ETHERNET->MAC_MII_ADDR = temp;
825 while((LPC_ETHERNET->MAC_MII_ADDR & ETHERNET_MAC_MII_ADDR_GB_Msk) != 0)
830 data = LPC_ETHERNET->MAC_MII_DATA & ETHERNET_MAC_MII_DATA_GD_Msk;
858 p = (uint8_t *)
data;
863 for(i = 0; i <
length; i++)
866 for(j = 0; j < 8; j++)
869 if((((crc >> 31) ^ (
p[i] >> j)) & 0x01) != 0)
871 crc = (crc << 1) ^ 0x04C11DB7;