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 = 4
57 #pragma data_alignment = 4
79 static uint_t txBufferIndex;
81 static uint_t rxBufferIndex;
118 volatile uint32_t status;
121 TRACE_INFO(
"Initializing PIC32CX Ethernet MAC...\r\n");
124 nicDriverInterface = interface;
127 MCLK_REGS->MCLK_APBCMASK |= MCLK_APBCMASK_GMAC_Msk;
128 MCLK_REGS->MCLK_AHBMASK |= MCLK_AHBMASK_GMAC_Msk;
131 GMAC_REGS->GMAC_NCR = 0;
137 GMAC_REGS->GMAC_NCFGR = GMAC_NCFGR_CLK(5);
139 GMAC_REGS->GMAC_NCR |= GMAC_NCR_MPE_Msk;
142 if(interface->phyDriver != NULL)
145 error = interface->phyDriver->init(interface);
147 else if(interface->switchDriver != NULL)
150 error = interface->switchDriver->init(interface);
165 GMAC_REGS->SA[0].GMAC_SAB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
166 GMAC_REGS->SA[0].GMAC_SAT = interface->macAddr.w[2];
169 GMAC_REGS->SA[1].GMAC_SAB = 0;
170 GMAC_REGS->SA[2].GMAC_SAB = 0;
171 GMAC_REGS->SA[3].GMAC_SAB = 0;
174 GMAC_REGS->GMAC_HRB = 0;
175 GMAC_REGS->GMAC_HRT = 0;
178 GMAC_REGS->GMAC_NCFGR |= GMAC_NCFGR_MAXFS_Msk | GMAC_NCFGR_MTIHEN_Msk;
184 GMAC_REGS->GMAC_TSR = GMAC_TSR_HRESP_Msk | GMAC_TSR_UND_Msk |
185 GMAC_TSR_TXCOMP_Msk | GMAC_TSR_TFC_Msk | GMAC_TSR_TXGO_Msk |
186 GMAC_TSR_RLE_Msk | GMAC_TSR_COL_Msk | GMAC_TSR_UBR_Msk;
189 GMAC_REGS->GMAC_RSR = GMAC_RSR_HNO_Msk | GMAC_RSR_RXOVR_Msk |
190 GMAC_RSR_REC_Msk | GMAC_RSR_BNA_Msk;
193 GMAC_REGS->GMAC_IDR = 0xFFFFFFFF;
196 GMAC_REGS->GMAC_IER = GMAC_IER_HRESP_Msk | GMAC_IER_ROVR_Msk |
197 GMAC_IER_TCOMP_Msk | GMAC_IER_TFC_Msk | GMAC_IER_RLEX_Msk |
198 GMAC_IER_TUR_Msk | GMAC_IER_RXUBR_Msk | GMAC_IER_RCOMP_Msk;
201 status = GMAC_REGS->GMAC_ISR;
212 GMAC_REGS->GMAC_NCR |= GMAC_NCR_TXEN_Msk | GMAC_NCR_RXEN_Msk;
230 #if defined(USE_PIC32CX_SG41_CURIOSITY_ULTRA) || \
231 defined(USE_PIC32CX_SG61_CURIOSITY_ULTRA)
235 MCLK_REGS->MCLK_APBBMASK |= MCLK_APBBMASK_PORT_Msk;
238 PORT_REGS->GROUP[0].PORT_PINCFG[12] |= PORT_PINCFG_PMUXEN_Msk;
239 temp = PORT_REGS->GROUP[0].PORT_PMUX[6] & ~PORT_PMUX_PMUXE_Msk;
240 PORT_REGS->GROUP[0].PORT_PMUX[6] = temp | PORT_PMUX_PMUXE(MUX_PA12L_GMAC_GRX1);
243 PORT_REGS->GROUP[0].PORT_PINCFG[13] |= PORT_PINCFG_PMUXEN_Msk;
244 temp = PORT_REGS->GROUP[0].PORT_PMUX[6] & ~PORT_PMUX_PMUXO_Msk;
245 PORT_REGS->GROUP[0].PORT_PMUX[6] = temp | PORT_PMUX_PMUXO(MUX_PA13L_GMAC_GRX0);
248 PORT_REGS->GROUP[0].PORT_PINCFG[14] |= PORT_PINCFG_PMUXEN_Msk;
249 temp = PORT_REGS->GROUP[0].PORT_PMUX[7] & ~PORT_PMUX_PMUXE_Msk;
250 PORT_REGS->GROUP[0].PORT_PMUX[7] = temp | PORT_PMUX_PMUXE(MUX_PA14L_GMAC_GTXCK);
253 PORT_REGS->GROUP[0].PORT_PINCFG[15] |= PORT_PINCFG_PMUXEN_Msk;
254 temp = PORT_REGS->GROUP[0].PORT_PMUX[7] & ~PORT_PMUX_PMUXO_Msk;
255 PORT_REGS->GROUP[0].PORT_PMUX[7] = temp | PORT_PMUX_PMUXO(MUX_PA15L_GMAC_GRXER);
258 PORT_REGS->GROUP[0].PORT_PINCFG[17] |= PORT_PINCFG_DRVSTR_Msk;
259 PORT_REGS->GROUP[0].PORT_PINCFG[17] |= PORT_PINCFG_PMUXEN_Msk;
260 temp = PORT_REGS->GROUP[0].PORT_PMUX[8] & ~PORT_PMUX_PMUXO_Msk;
261 PORT_REGS->GROUP[0].PORT_PMUX[8] = temp | PORT_PMUX_PMUXO(MUX_PA17L_GMAC_GTXEN);
264 PORT_REGS->GROUP[0].PORT_PINCFG[18] |= PORT_PINCFG_DRVSTR_Msk;
265 PORT_REGS->GROUP[0].PORT_PINCFG[18] |= PORT_PINCFG_PMUXEN_Msk;
266 temp = PORT_REGS->GROUP[0].PORT_PMUX[9] & ~PORT_PMUX_PMUXE_Msk;
267 PORT_REGS->GROUP[0].PORT_PMUX[9] = temp | PORT_PMUX_PMUXE(MUX_PA18L_GMAC_GTX0);
270 PORT_REGS->GROUP[0].PORT_PINCFG[19] |= PORT_PINCFG_DRVSTR_Msk;
271 PORT_REGS->GROUP[0].PORT_PINCFG[19] |= PORT_PINCFG_PMUXEN_Msk;
272 temp = PORT_REGS->GROUP[0].PORT_PMUX[9] & ~PORT_PMUX_PMUXO_Msk;
273 PORT_REGS->GROUP[0].PORT_PMUX[9] = temp | PORT_PMUX_PMUXO(MUX_PA19L_GMAC_GTX1);
276 PORT_REGS->GROUP[2].PORT_PINCFG[20] |= PORT_PINCFG_PMUXEN_Msk;
277 temp = PORT_REGS->GROUP[2].PORT_PMUX[10] & ~PORT_PMUX_PMUXE_Msk;
278 PORT_REGS->GROUP[2].PORT_PMUX[10] = temp | PORT_PMUX_PMUXE(MUX_PC20L_GMAC_GRXDV);
281 PORT_REGS->GROUP[2].PORT_PINCFG[22] |= PORT_PINCFG_PMUXEN_Msk;
282 temp = PORT_REGS->GROUP[2].PORT_PMUX[11] & ~PORT_PMUX_PMUXE_Msk;
283 PORT_REGS->GROUP[2].PORT_PMUX[11] = temp | PORT_PMUX_PMUXE(MUX_PC22L_GMAC_GMDC);
286 PORT_REGS->GROUP[2].PORT_PINCFG[23] |= PORT_PINCFG_PMUXEN_Msk;
287 temp = PORT_REGS->GROUP[2].PORT_PMUX[11] & ~PORT_PMUX_PMUXO_Msk;
288 PORT_REGS->GROUP[2].PORT_PMUX[11] = temp | PORT_PMUX_PMUXO(MUX_PC23L_GMAC_GMDIO);
291 GMAC_REGS->GMAC_UR &= ~GMAC_UR_MII_Msk;
294 PORT_REGS->GROUP[2].PORT_DIRSET = PORT_PC18;
297 PORT_REGS->GROUP[2].PORT_OUTCLR = PORT_PC18;
299 PORT_REGS->GROUP[2].PORT_OUTSET = PORT_PC18;
339 rxBufferDesc[i].
status = 0;
348 GMAC_REGS->GMAC_TBQB = (uint32_t) txBufferDesc;
350 GMAC_REGS->GMAC_RBQB = (uint32_t) rxBufferDesc;
366 if(interface->phyDriver != NULL)
369 interface->phyDriver->tick(interface);
371 else if(interface->switchDriver != NULL)
374 interface->switchDriver->tick(interface);
391 NVIC_EnableIRQ(GMAC_IRQn);
394 if(interface->phyDriver != NULL)
397 interface->phyDriver->enableIrq(interface);
399 else if(interface->switchDriver != NULL)
402 interface->switchDriver->enableIrq(interface);
419 NVIC_DisableIRQ(GMAC_IRQn);
422 if(interface->phyDriver != NULL)
425 interface->phyDriver->disableIrq(interface);
427 else if(interface->switchDriver != NULL)
430 interface->switchDriver->disableIrq(interface);
446 volatile uint32_t isr;
447 volatile uint32_t tsr;
448 volatile uint32_t rsr;
458 isr = GMAC_REGS->GMAC_ISR;
459 tsr = GMAC_REGS->GMAC_TSR;
460 rsr = GMAC_REGS->GMAC_RSR;
464 if((tsr & (GMAC_TSR_HRESP_Msk | GMAC_TSR_UND_Msk |
465 GMAC_TSR_TXCOMP_Msk | GMAC_TSR_TFC_Msk | GMAC_TSR_TXGO_Msk |
466 GMAC_TSR_RLE_Msk | GMAC_TSR_COL_Msk | GMAC_TSR_UBR_Msk)) != 0)
469 GMAC_REGS->GMAC_TSR = tsr;
472 if((txBufferDesc[txBufferIndex].status &
GMAC_TX_USED) != 0)
480 if((rsr & (GMAC_RSR_HNO_Msk | GMAC_RSR_RXOVR_Msk | GMAC_RSR_REC_Msk |
481 GMAC_RSR_BNA_Msk)) != 0)
484 nicDriverInterface->nicEvent =
TRUE;
505 rsr = GMAC_REGS->GMAC_RSR;
508 if((rsr & (GMAC_RSR_HNO_Msk | GMAC_RSR_RXOVR_Msk | GMAC_RSR_REC_Msk |
509 GMAC_RSR_BNA_Msk)) != 0)
512 GMAC_REGS->GMAC_RSR = rsr;
554 if((txBufferDesc[txBufferIndex].status &
GMAC_TX_USED) == 0)
586 GMAC_REGS->GMAC_NCR |= GMAC_NCR_TSTART_Msk;
589 if((txBufferDesc[txBufferIndex].status &
GMAC_TX_USED) != 0)
627 j = rxBufferIndex + i;
650 if((rxBufferDesc[j].status &
GMAC_RX_EOF) != 0 && sofIndex != UINT_MAX)
664 if(eofIndex != UINT_MAX)
668 else if(sofIndex != UINT_MAX)
681 for(i = 0; i < j; i++)
684 if(eofIndex != UINT_MAX && i >= sofIndex && i <= eofIndex)
744 uint32_t hashTable[2];
752 GMAC_REGS->SA[0].GMAC_SAB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
753 GMAC_REGS->SA[0].GMAC_SAT = interface->macAddr.w[2];
769 entry = &interface->macAddrFilter[i];
781 k = (
p[0] >> 6) ^
p[0];
782 k ^= (
p[1] >> 4) ^ (
p[1] << 2);
783 k ^= (
p[2] >> 2) ^ (
p[2] << 4);
784 k ^= (
p[3] >> 6) ^
p[3];
785 k ^= (
p[4] >> 4) ^ (
p[4] << 2);
786 k ^= (
p[5] >> 2) ^ (
p[5] << 4);
792 hashTable[k / 32] |= (1 << (k % 32));
800 unicastMacAddr[j++] = entry->
addr;
810 GMAC_REGS->SA[1].GMAC_SAB = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
811 GMAC_REGS->SA[1].GMAC_SAT = unicastMacAddr[0].w[2];
816 GMAC_REGS->SA[1].GMAC_SAB = 0;
823 GMAC_REGS->SA[2].GMAC_SAB = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
824 GMAC_REGS->SA[2].GMAC_SAT = unicastMacAddr[1].w[2];
829 GMAC_REGS->SA[2].GMAC_SAB = 0;
836 GMAC_REGS->SA[3].GMAC_SAB = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
837 GMAC_REGS->SA[3].GMAC_SAT = unicastMacAddr[2].w[2];
842 GMAC_REGS->SA[3].GMAC_SAB = 0;
846 GMAC_REGS->GMAC_HRB = hashTable[0];
847 GMAC_REGS->GMAC_HRT = hashTable[1];
850 TRACE_DEBUG(
" HRB = %08" PRIX32
"\r\n", GMAC_REGS->GMAC_HRB);
851 TRACE_DEBUG(
" HRT = %08" PRIX32
"\r\n", GMAC_REGS->GMAC_HRT);
869 config = GMAC_REGS->GMAC_NCFGR;
874 config |= GMAC_NCFGR_SPD_Msk;
878 config &= ~GMAC_NCFGR_SPD_Msk;
884 config |= GMAC_NCFGR_FD_Msk;
888 config &= ~GMAC_NCFGR_FD_Msk;
892 GMAC_REGS->GMAC_NCFGR = config;
916 temp = GMAC_MAN_CLTTO_Msk | GMAC_MAN_OP(1) | GMAC_MAN_WTN(2);
918 temp |= GMAC_MAN_PHYA(phyAddr);
920 temp |= GMAC_MAN_REGA(
regAddr);
922 temp |= GMAC_MAN_DATA(
data);
925 GMAC_REGS->GMAC_MAN = temp;
927 while((GMAC_REGS->GMAC_NSR & GMAC_NSR_IDLE_Msk) == 0)
956 temp = GMAC_MAN_CLTTO_Msk | GMAC_MAN_OP(2) | GMAC_MAN_WTN(2);
958 temp |= GMAC_MAN_PHYA(phyAddr);
960 temp |= GMAC_MAN_REGA(
regAddr);
963 GMAC_REGS->GMAC_MAN = temp;
965 while((GMAC_REGS->GMAC_NSR & GMAC_NSR_IDLE_Msk) == 0)
970 data = GMAC_REGS->GMAC_MAN & GMAC_MAN_DATA_Msk;