32 #define TRACE_LEVEL NIC_TRACE_LEVEL
45 #if defined(__ICCARM__)
48 #pragma data_alignment = 8
51 #pragma data_alignment = 8
54 #pragma data_alignment = 8
57 #pragma data_alignment = 8
79 static uint_t txBufferIndex;
81 static uint_t rxBufferIndex;
118 volatile uint32_t status;
121 TRACE_INFO(
"Initializing PIC32CK Ethernet MAC...\r\n");
124 nicDriverInterface = interface;
127 GCLK_REGS->GCLK_PCHCTRL[ETH_GCLK_ID_TX] = GCLK_PCHCTRL_GEN_GCLK0 |
128 GCLK_PCHCTRL_CHEN_Msk;
131 while((GCLK_REGS->GCLK_PCHCTRL[ETH_GCLK_ID_TX] & GCLK_PCHCTRL_CHEN_Msk) == 0)
136 GCLK_REGS->GCLK_PCHCTRL[ETH_GCLK_ID_TSU] = GCLK_PCHCTRL_GEN_GCLK0 |
137 GCLK_PCHCTRL_CHEN_Msk;
140 while((GCLK_REGS->GCLK_PCHCTRL[ETH_GCLK_ID_TSU] & GCLK_PCHCTRL_CHEN_Msk) == 0)
145 MCLK_REGS->MCLK_CLKMSK[ETH_MCLK_ID_APB / 32] |= (1U << (ETH_MCLK_ID_APB % 32));
146 MCLK_REGS->MCLK_CLKMSK[ETH_MCLK_ID_AHB / 32] |= (1U << (ETH_MCLK_ID_AHB % 32));
149 ETH_REGS->ETH_CTRLA = ETH_CTRLA_ENABLE_Msk;
152 while(ETH_REGS->ETH_SYNCB != 0)
157 ETH_REGS->ETH_NCR = 0;
163 ETH_REGS->ETH_NCFGR = ETH_NCFGR_DBW(1) | ETH_NCFGR_CLK(5);
165 ETH_REGS->ETH_NCR |= ETH_NCR_MPE_Msk;
168 if(interface->phyDriver != NULL)
171 error = interface->phyDriver->init(interface);
173 else if(interface->switchDriver != NULL)
176 error = interface->switchDriver->init(interface);
191 ETH_REGS->SA[0].ETH_SAB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
192 ETH_REGS->SA[0].ETH_SAT = interface->macAddr.w[2];
195 ETH_REGS->SA[1].ETH_SAB = 0;
196 ETH_REGS->SA[2].ETH_SAB = 0;
197 ETH_REGS->SA[3].ETH_SAB = 0;
200 ETH_REGS->ETH_HRB = 0;
201 ETH_REGS->ETH_HRT = 0;
204 ETH_REGS->ETH_NCFGR |= ETH_NCFGR_MAXFS_Msk | ETH_NCFGR_MTIHEN_Msk;
210 ETH_REGS->ETH_TSR = ETH_TSR_HRESP_Msk | ETH_TSR_UND_Msk |
211 ETH_TSR_TXCOMP_Msk | ETH_TSR_TFC_Msk | ETH_TSR_TXGO_Msk |
212 ETH_TSR_RLE_Msk | ETH_TSR_COL_Msk | ETH_TSR_UBR_Msk;
215 ETH_REGS->ETH_RSR = ETH_RSR_HNO_Msk | ETH_RSR_RXOVR_Msk |
216 ETH_RSR_REC_Msk | ETH_RSR_BNA_Msk;
219 ETH_REGS->ETH_IDR = 0xFFFFFFFF;
222 ETH_REGS->ETH_IER = ETH_IER_HRESP_Msk | ETH_IER_ROVR_Msk |
223 ETH_IER_TCOMP_Msk | ETH_IER_TFC_Msk | ETH_IER_RLEX_Msk |
224 ETH_IER_TUR_Msk | ETH_IER_RXUBR_Msk | ETH_IER_RCOMP_Msk;
227 status = ETH_REGS->ETH_ISR;
238 ETH_REGS->ETH_NCR |= ETH_NCR_TXEN_Msk | ETH_NCR_RXEN_Msk;
256 #if defined(USE_PIC32CK_GC01_CURIOSITY_ULTRA) || \
257 defined(USE_PIC32CK_SG01_CURIOSITY_ULTRA)
261 MCLK_REGS->MCLK_CLKMSK[PORT_MCLK_ID_APB / 32] |= (1U << (PORT_MCLK_ID_APB % 32));
264 PORT_REGS->GROUP[2].PORT_PINCFG[0] |= PORT_PINCFG_PMUXEN_Msk;
265 temp = PORT_REGS->GROUP[2].PORT_PMUX[0] & ~PORT_PMUX_PMUXE_Msk;
266 PORT_REGS->GROUP[2].PORT_PMUX[0] = temp | PORT_PMUX_PMUXE(MUX_PC00L_ETH_REF_CLK);
269 PORT_REGS->GROUP[2].PORT_PINCFG[3] |= PORT_PINCFG_PMUXEN_Msk;
270 temp = PORT_REGS->GROUP[2].PORT_PMUX[1] & ~PORT_PMUX_PMUXO_Msk;
271 PORT_REGS->GROUP[2].PORT_PMUX[1] = temp | PORT_PMUX_PMUXO(MUX_PC03L_ETH_MDC);
274 PORT_REGS->GROUP[2].PORT_PINCFG[4] |= PORT_PINCFG_PMUXEN_Msk;
275 temp = PORT_REGS->GROUP[2].PORT_PMUX[2] & ~PORT_PMUX_PMUXE_Msk;
276 PORT_REGS->GROUP[2].PORT_PMUX[2] = temp | PORT_PMUX_PMUXE(MUX_PC04L_ETH_MDIO);
279 PORT_REGS->GROUP[2].PORT_PINCFG[6] |= PORT_PINCFG_PMUXEN_Msk;
280 temp = PORT_REGS->GROUP[2].PORT_PMUX[3] & ~PORT_PMUX_PMUXE_Msk;
281 PORT_REGS->GROUP[2].PORT_PMUX[3] = temp | PORT_PMUX_PMUXE(MUX_PC06L_ETH_RXD1);
284 PORT_REGS->GROUP[2].PORT_PINCFG[7] |= PORT_PINCFG_PMUXEN_Msk;
285 temp = PORT_REGS->GROUP[2].PORT_PMUX[3] & ~PORT_PMUX_PMUXO_Msk;
286 PORT_REGS->GROUP[2].PORT_PMUX[3] = temp | PORT_PMUX_PMUXO(MUX_PC07L_ETH_RXD0);
289 PORT_REGS->GROUP[2].PORT_PINCFG[9] |= PORT_PINCFG_PMUXEN_Msk;
290 temp = PORT_REGS->GROUP[2].PORT_PMUX[4] & ~PORT_PMUX_PMUXO_Msk;
291 PORT_REGS->GROUP[2].PORT_PMUX[4] = temp | PORT_PMUX_PMUXO(MUX_PC09L_ETH_RXER);
294 PORT_REGS->GROUP[2].PORT_PINCFG[10] |= PORT_PINCFG_PMUXEN_Msk;
295 temp = PORT_REGS->GROUP[2].PORT_PMUX[5] & ~PORT_PMUX_PMUXE_Msk;
296 PORT_REGS->GROUP[2].PORT_PMUX[5] = temp | PORT_PMUX_PMUXE(MUX_PC10L_ETH_RXDV);
299 PORT_REGS->GROUP[2].PORT_PINCFG[11] |= PORT_PINCFG_PMUXEN_Msk;
300 temp = PORT_REGS->GROUP[2].PORT_PMUX[5] & ~PORT_PMUX_PMUXO_Msk;
301 PORT_REGS->GROUP[2].PORT_PMUX[5] = temp | PORT_PMUX_PMUXO(MUX_PC11L_ETH_TXEN);
304 PORT_REGS->GROUP[2].PORT_PINCFG[12] |= PORT_PINCFG_PMUXEN_Msk;
305 temp = PORT_REGS->GROUP[2].PORT_PMUX[6] & ~PORT_PMUX_PMUXE_Msk;
306 PORT_REGS->GROUP[2].PORT_PMUX[6] = temp | PORT_PMUX_PMUXE(MUX_PC12L_ETH_TXD0);
309 PORT_REGS->GROUP[2].PORT_PINCFG[13] |= PORT_PINCFG_PMUXEN_Msk;
310 temp = PORT_REGS->GROUP[2].PORT_PMUX[6] & ~PORT_PMUX_PMUXO_Msk;
311 PORT_REGS->GROUP[2].PORT_PMUX[6] = temp | PORT_PMUX_PMUXO(MUX_PC13L_ETH_TXD1);
317 PORT_REGS->GROUP[3].PORT_DIRSET = PORT_PD21;
320 PORT_REGS->GROUP[3].PORT_OUTCLR = PORT_PD21;
322 PORT_REGS->GROUP[3].PORT_OUTSET = PORT_PD21;
362 rxBufferDesc[i].
status = 0;
371 ETH_REGS->ETH_TBQB = (uint32_t) txBufferDesc;
373 ETH_REGS->ETH_RBQB = (uint32_t) rxBufferDesc;
389 if(interface->phyDriver != NULL)
392 interface->phyDriver->tick(interface);
394 else if(interface->switchDriver != NULL)
397 interface->switchDriver->tick(interface);
414 NVIC_EnableIRQ(ETH_IRQn);
417 if(interface->phyDriver != NULL)
420 interface->phyDriver->enableIrq(interface);
422 else if(interface->switchDriver != NULL)
425 interface->switchDriver->enableIrq(interface);
442 NVIC_DisableIRQ(ETH_IRQn);
445 if(interface->phyDriver != NULL)
448 interface->phyDriver->disableIrq(interface);
450 else if(interface->switchDriver != NULL)
453 interface->switchDriver->disableIrq(interface);
469 volatile uint32_t isr;
470 volatile uint32_t tsr;
471 volatile uint32_t rsr;
481 isr = ETH_REGS->ETH_ISR;
482 tsr = ETH_REGS->ETH_TSR;
483 rsr = ETH_REGS->ETH_RSR;
486 ETH_REGS->ETH_ISR = isr;
489 if((tsr & (ETH_TSR_HRESP_Msk | ETH_TSR_UND_Msk |
490 ETH_TSR_TXCOMP_Msk | ETH_TSR_TFC_Msk | ETH_TSR_TXGO_Msk |
491 ETH_TSR_RLE_Msk | ETH_TSR_COL_Msk | ETH_TSR_UBR_Msk)) != 0)
494 ETH_REGS->ETH_TSR = tsr;
497 if((txBufferDesc[txBufferIndex].status &
ETH_TX_USED) != 0)
505 if((rsr & (ETH_RSR_HNO_Msk | ETH_RSR_RXOVR_Msk | ETH_RSR_REC_Msk |
506 ETH_RSR_BNA_Msk)) != 0)
509 nicDriverInterface->nicEvent =
TRUE;
530 rsr = ETH_REGS->ETH_RSR;
533 if((rsr & (ETH_RSR_HNO_Msk | ETH_RSR_RXOVR_Msk | ETH_RSR_REC_Msk |
534 ETH_RSR_BNA_Msk)) != 0)
537 ETH_REGS->ETH_RSR = rsr;
579 if((txBufferDesc[txBufferIndex].status &
ETH_TX_USED) == 0)
611 ETH_REGS->ETH_NCR |= ETH_NCR_TSTART_Msk;
614 if((txBufferDesc[txBufferIndex].status &
ETH_TX_USED) != 0)
652 j = rxBufferIndex + i;
668 if((rxBufferDesc[j].status &
ETH_RX_SOF) != 0)
675 if((rxBufferDesc[j].status &
ETH_RX_EOF) != 0 && sofIndex != UINT_MAX)
689 if(eofIndex != UINT_MAX)
693 else if(sofIndex != UINT_MAX)
706 for(i = 0; i < j; i++)
709 if(eofIndex != UINT_MAX && i >= sofIndex && i <= eofIndex)
769 uint32_t hashTable[2];
777 ETH_REGS->SA[0].ETH_SAB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
778 ETH_REGS->SA[0].ETH_SAT = interface->macAddr.w[2];
794 entry = &interface->macAddrFilter[i];
806 k = (
p[0] >> 6) ^
p[0];
807 k ^= (
p[1] >> 4) ^ (
p[1] << 2);
808 k ^= (
p[2] >> 2) ^ (
p[2] << 4);
809 k ^= (
p[3] >> 6) ^
p[3];
810 k ^= (
p[4] >> 4) ^ (
p[4] << 2);
811 k ^= (
p[5] >> 2) ^ (
p[5] << 4);
817 hashTable[k / 32] |= (1 << (k % 32));
825 unicastMacAddr[j++] = entry->
addr;
835 ETH_REGS->SA[1].ETH_SAB = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
836 ETH_REGS->SA[1].ETH_SAT = unicastMacAddr[0].w[2];
841 ETH_REGS->SA[1].ETH_SAB = 0;
848 ETH_REGS->SA[2].ETH_SAB = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
849 ETH_REGS->SA[2].ETH_SAT = unicastMacAddr[1].w[2];
854 ETH_REGS->SA[2].ETH_SAB = 0;
861 ETH_REGS->SA[3].ETH_SAB = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
862 ETH_REGS->SA[3].ETH_SAT = unicastMacAddr[2].w[2];
867 ETH_REGS->SA[3].ETH_SAB = 0;
871 ETH_REGS->ETH_HRB = hashTable[0];
872 ETH_REGS->ETH_HRT = hashTable[1];
875 TRACE_DEBUG(
" HRB = %08" PRIX32
"\r\n", ETH_REGS->ETH_HRB);
876 TRACE_DEBUG(
" HRT = %08" PRIX32
"\r\n", ETH_REGS->ETH_HRT);
894 config = ETH_REGS->ETH_NCFGR;
899 config |= ETH_NCFGR_SPD_Msk;
903 config &= ~ETH_NCFGR_SPD_Msk;
909 config |= ETH_NCFGR_FD_Msk;
913 config &= ~ETH_NCFGR_FD_Msk;
917 ETH_REGS->ETH_NCFGR = config;
941 temp = ETH_MAN_CLTTO_Msk | ETH_MAN_OP(1) | ETH_MAN_WTN(2);
943 temp |= ETH_MAN_PHYA(phyAddr);
947 temp |= ETH_MAN_DATA(
data);
950 ETH_REGS->ETH_MAN = temp;
952 while((ETH_REGS->ETH_NSR & ETH_NSR_IDLE_Msk) == 0)
981 temp = ETH_MAN_CLTTO_Msk | ETH_MAN_OP(2) | ETH_MAN_WTN(2);
983 temp |= ETH_MAN_PHYA(phyAddr);
988 ETH_REGS->ETH_MAN = temp;
990 while((ETH_REGS->ETH_NSR & ETH_NSR_IDLE_Msk) == 0)
995 data = ETH_REGS->ETH_MAN & ETH_MAN_DATA_Msk;