32 #define TRACE_LEVEL NIC_TRACE_LEVEL
99 TRACE_INFO(
"Initializing PIC32MZ Ethernet MAC...\r\n");
102 nicDriverInterface = interface;
108 IEC4CLR = _IEC4_ETHIE_MASK;
110 ETHCON1CLR = _ETHCON1_ON_MASK | _ETHCON1_TXRTS_POSITION | _ETHCON1_RXEN_MASK;
113 while((ETHSTAT & _ETHSTAT_ETHBUSY_MASK) != 0)
118 ETHCON1SET = _ETHCON1_ON_MASK;
121 IFS4CLR = _IFS4_ETHIF_MASK;
130 EMAC1CFG1SET = _EMAC1CFG1_SOFTRESET_MASK;
131 EMAC1CFG1CLR = _EMAC1CFG1_SOFTRESET_MASK;
134 EMAC1SUPPSET = _EMAC1SUPP_RESETRMII_MASK;
135 EMAC1SUPPCLR = _EMAC1SUPP_RESETRMII_MASK;
138 EMAC1MCFGSET = _EMAC1MCFG_RESETMGMT_MASK;
139 EMAC1MCFGCLR = _EMAC1MCFG_RESETMGMT_MASK;
145 if(interface->phyDriver != NULL)
148 error = interface->phyDriver->init(interface);
150 else if(interface->switchDriver != NULL)
153 error = interface->switchDriver->init(interface);
171 interface->macAddr.w[0] = EMAC1SA2;
172 interface->macAddr.w[1] = EMAC1SA1;
173 interface->macAddr.w[2] = EMAC1SA0;
181 EMAC1SA0 = interface->macAddr.w[2];
182 EMAC1SA1 = interface->macAddr.w[1];
183 EMAC1SA2 = interface->macAddr.w[0];
191 ETHRXFC = _ETHRXFC_HTEN_MASK | _ETHRXFC_CRCOKEN_MASK |
192 _ETHRXFC_RUNTEN_MASK | _ETHRXFC_UCEN_MASK | _ETHRXFC_BCEN_MASK;
195 EMAC1CFG1 = _EMAC1CFG1_RXENABLE_MASK;
197 EMAC1CFG2 = _EMAC1CFG2_PADENABLE_MASK | _EMAC1CFG2_CRCENABLE_MASK;
205 ETHIENSET = _ETHIEN_PKTPENDIE_MASK | _ETHIEN_TXDONEIE_MASK;
208 IPC38CLR = _IPC38_ETHIP_MASK;
211 IPC38CLR = _IPC38_ETHIS_MASK;
215 ETHCON1SET = _ETHCON1_RXEN_MASK;
233 #if defined(USE_PIC32MZ_EC_STARTER_KIT)
235 ANSELJCLR = _ANSELJ_ANSJ8_MASK;
237 ANSELJCLR = _ANSELJ_ANSJ9_MASK;
239 ANSELJCLR = _ANSELJ_ANSJ11_MASK;
241 ANSELHCLR = _ANSELH_ANSH4_MASK;
243 ANSELHCLR = _ANSELH_ANSH5_MASK;
246 #elif defined(USE_PIC32MZ_EF_STARTER_KIT)
248 ANSELJCLR = _ANSELJ_ANSJ8_MASK;
250 ANSELJCLR = _ANSELJ_ANSJ9_MASK;
252 ANSELJCLR = _ANSELJ_ANSJ11_MASK;
254 ANSELHCLR = _ANSELH_ANSH4_MASK;
256 ANSELHCLR = _ANSELH_ANSH5_MASK;
259 TRISHCLR = _TRISH_TRISH11_MASK;
262 LATHCLR = _LATH_LATH11_MASK;
264 LATHSET = _LATH_LATH11_MASK;
268 #elif defined(USE_PIC32MZ_DA_STARTER_KIT)
270 TRISJCLR = _TRISJ_TRISJ15_MASK;
273 LATJCLR = _LATJ_LATJ15_MASK;
275 LATJSET = _LATJ_LATJ15_MASK;
279 #elif defined(USE_PIC32MZ_EF_CURIOSITY)
281 ANSELBCLR = _ANSELB_ANSB11_MASK;
283 ANSELBCLR = _ANSELB_ANSB12_MASK;
285 ANSELBCLR = _ANSELB_ANSB13_MASK;
287 ANSELGCLR = _ANSELG_ANSG8_MASK;
289 ANSELGCLR = _ANSELG_ANSG9_MASK;
292 TRISGCLR = _TRISG_TRISG15_MASK;
294 ANSELGCLR = _ANSELG_ANSG15_MASK;
297 LATGCLR = _LATG_LATG15_MASK;
299 LATGSET = _LATG_LATG15_MASK;
303 #elif defined(USE_PIC32MZ_EF_CURIOSITY_2)
305 ANSELJCLR = _ANSELJ_ANSJ8_MASK;
307 ANSELJCLR = _ANSELJ_ANSJ9_MASK;
309 ANSELJCLR = _ANSELJ_ANSJ11_MASK;
311 ANSELHCLR = _ANSELH_ANSH4_MASK;
313 ANSELHCLR = _ANSELH_ANSH5_MASK;
316 #elif defined(USE_PIC32MZ_DA_CURIOSITY)
318 TRISECLR = _TRISE_TRISE1_MASK;
320 ANSELECLR = _ANSELE_ANSE1_MASK;
323 LATECLR = _LATE_LATE1_MASK;
325 LATESET = _LATE_LATE1_MASK;
329 #elif defined(USE_IOT_ETHERNET_KIT)
331 ANSELECLR = _ANSELE_ANSE4_MASK;
333 ANSELECLR = _ANSELE_ANSE5_MASK;
335 ANSELECLR = _ANSELE_ANSE6_MASK;
337 ANSELECLR = _ANSELE_ANSE7_MASK;
340 TRISBCLR = _TRISB_TRISB14_MASK;
342 ANSELBCLR = _ANSELB_ANSB14_MASK;
345 LATBCLR = _LATB_LATB14_MASK;
347 LATBSET = _LATB_LATB14_MASK;
366 txCurBufferDesc = &txBufferDesc[i];
376 txCurBufferDesc->
next = KVA_TO_PA(&txBufferDesc[i + 1]);
380 txCurBufferDesc->
next = KVA_TO_PA(&txBufferDesc[0]);
382 txCurBufferDesc = &txBufferDesc[0];
388 rxCurBufferDesc = &rxBufferDesc[i];
398 rxCurBufferDesc->
next = KVA_TO_PA(&rxBufferDesc[i + 1]);
402 rxCurBufferDesc->
next = KVA_TO_PA(&rxBufferDesc[0]);
404 rxCurBufferDesc = &rxBufferDesc[0];
407 ETHTXST = KVA_TO_PA(&txBufferDesc[0]);
409 ETHRXST = KVA_TO_PA(&rxBufferDesc[0]);
427 if(interface->phyDriver != NULL)
430 interface->phyDriver->tick(interface);
432 else if(interface->switchDriver != NULL)
435 interface->switchDriver->tick(interface);
452 IEC4SET = _IEC4_ETHIE_MASK;
455 if(interface->phyDriver != NULL)
458 interface->phyDriver->enableIrq(interface);
460 else if(interface->switchDriver != NULL)
463 interface->switchDriver->enableIrq(interface);
480 IEC4CLR = _IEC4_ETHIE_MASK;
483 if(interface->phyDriver != NULL)
486 interface->phyDriver->disableIrq(interface);
488 else if(interface->switchDriver != NULL)
491 interface->switchDriver->disableIrq(interface);
519 if((status & _ETHIRQ_TXDONE_MASK) != 0)
522 ETHIRQCLR = _ETHIRQ_TXDONE_MASK;
533 if((status & _ETHIRQ_PKTPEND_MASK) != 0)
536 ETHIENCLR = _ETHIEN_PKTPENDIE_MASK;
539 nicDriverInterface->nicEvent =
TRUE;
545 IFS4CLR = _IFS4_ETHIF_MASK;
562 if((ETHIRQ & _ETHIRQ_PKTPEND_MASK) != 0)
575 ETHIENSET = _ETHIEN_PKTPENDIE_MASK;
624 ETHCON1SET = _ETHCON1_TXRTS_MASK;
627 txCurBufferDesc = PA_TO_KVA1(txCurBufferDesc->
next);
697 rxCurBufferDesc = PA_TO_KVA1(rxCurBufferDesc->
next);
701 ETHCON1SET = _ETHCON1_BUFCDEC_MASK;
725 uint32_t hashTable[2];
740 entry = &interface->macAddrFilter[i];
748 k = (crc >> 23) & 0x3F;
750 hashTable[k / 32] |= (1 << (k % 32));
755 ETHHT0 = hashTable[0];
756 ETHHT1 = hashTable[1];
759 TRACE_DEBUG(
" ETHHT0 = %08" PRIX32
"\r\n", ETHHT0);
760 TRACE_DEBUG(
" ETHHT1 = %08" PRIX32
"\r\n", ETHHT1);
779 EMAC1SUPPSET = _EMAC1SUPP_SPEEDRMII_MASK;
784 EMAC1SUPPCLR = _EMAC1SUPP_SPEEDRMII_MASK;
791 EMAC1CFG2SET = _EMAC1CFG2_FULLDPLX_MASK;
798 EMAC1CFG2CLR = _EMAC1CFG2_FULLDPLX_MASK;
825 EMAC1MADR = (phyAddr << _EMAC1MADR_PHYADDR_POSITION) |
regAddr;
827 EMAC1MWTD =
data & _EMAC1MWTD_MWTD_MASK;
830 for(i = 0; i < 16; i++)
832 __asm__ __volatile__ (
"nop;");
836 while((EMAC1MIND & _EMAC1MIND_MIIMBUSY_MASK) != 0)
865 EMAC1MADR = (phyAddr << _EMAC1MADR_PHYADDR_POSITION) |
regAddr;
867 EMAC1MCMD = _EMAC1MCMD_READ_MASK;
870 for(i = 0; i < 16; i++)
872 __asm__ __volatile__ (
"nop;");
876 while((EMAC1MIND & _EMAC1MIND_MIIMBUSY_MASK) != 0)
883 data = EMAC1MRDD & _EMAC1MRDD_MRDD_MASK;
911 p = (uint8_t *)
data;
916 for(i = 0; i <
length; i++)
919 for(j = 0; j < 8; j++)
922 if((((crc >> 31) ^ (
p[i] >> j)) & 0x01) != 0)
924 crc = (crc << 1) ^ 0x04C11DB7;