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 SAM3X Ethernet MAC...\r\n");
124 nicDriverInterface = interface;
127 PMC->PMC_PCER1 = (1 << (ID_EMAC - 32));
136 EMAC->EMAC_NCFGR = EMAC_NCFGR_CLK_MCK_64;
138 EMAC->EMAC_NCR |= EMAC_NCR_MPE;
141 if(interface->phyDriver != NULL)
144 error = interface->phyDriver->init(interface);
146 else if(interface->switchDriver != NULL)
149 error = interface->switchDriver->init(interface);
164 EMAC->EMAC_SA[0].EMAC_SAxB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
165 EMAC->EMAC_SA[0].EMAC_SAxT = interface->macAddr.w[2];
168 EMAC->EMAC_SA[1].EMAC_SAxB = 0;
169 EMAC->EMAC_SA[2].EMAC_SAxB = 0;
170 EMAC->EMAC_SA[3].EMAC_SAxB = 0;
177 EMAC->EMAC_NCFGR |= EMAC_NCFGR_BIG | EMAC_NCFGR_MTI;
183 EMAC->EMAC_TSR = EMAC_TSR_UND | EMAC_TSR_COMP | EMAC_TSR_BEX |
184 EMAC_TSR_TGO | EMAC_TSR_RLES | EMAC_TSR_COL | EMAC_TSR_UBR;
187 EMAC->EMAC_RSR = EMAC_RSR_OVR | EMAC_RSR_REC | EMAC_RSR_BNA;
190 EMAC->EMAC_IDR = 0xFFFFFFFF;
193 EMAC->EMAC_IER = EMAC_IER_ROVR | EMAC_IER_TCOMP | EMAC_IER_TXERR |
194 EMAC_IER_RLE | EMAC_IER_TUND | EMAC_IER_RXUBR | EMAC_IER_RCOMP;
197 status = EMAC->EMAC_ISR;
208 EMAC->EMAC_NCR |= EMAC_NCR_TE | EMAC_NCR_RE;
226 #if defined(USE_SAM3X_EK)
228 PMC->PMC_PCER0 = (1 << ID_PIOB);
240 EMAC->EMAC_USRIO = EMAC_USRIO_CLKEN | EMAC_USRIO_RMII;
279 rxBufferDesc[i].
status = 0;
288 EMAC->EMAC_TBQP = (uint32_t) txBufferDesc;
290 EMAC->EMAC_RBQP = (uint32_t) rxBufferDesc;
306 if(interface->phyDriver != NULL)
309 interface->phyDriver->tick(interface);
311 else if(interface->switchDriver != NULL)
314 interface->switchDriver->tick(interface);
331 NVIC_EnableIRQ(EMAC_IRQn);
334 if(interface->phyDriver != NULL)
337 interface->phyDriver->enableIrq(interface);
339 else if(interface->switchDriver != NULL)
342 interface->switchDriver->enableIrq(interface);
359 NVIC_DisableIRQ(EMAC_IRQn);
362 if(interface->phyDriver != NULL)
365 interface->phyDriver->disableIrq(interface);
367 else if(interface->switchDriver != NULL)
370 interface->switchDriver->disableIrq(interface);
386 volatile uint32_t isr;
387 volatile uint32_t tsr;
388 volatile uint32_t rsr;
398 isr = EMAC->EMAC_ISR;
399 tsr = EMAC->EMAC_TSR;
400 rsr = EMAC->EMAC_RSR;
404 if((tsr & (EMAC_TSR_UND | EMAC_TSR_COMP | EMAC_TSR_BEX |
405 EMAC_TSR_TGO | EMAC_TSR_RLES | EMAC_TSR_COL | EMAC_TSR_UBR)) != 0)
408 EMAC->EMAC_TSR = tsr;
411 if((txBufferDesc[txBufferIndex].status &
EMAC_TX_USED) != 0)
419 if((rsr & (EMAC_RSR_OVR | EMAC_RSR_REC | EMAC_RSR_BNA)) != 0)
422 nicDriverInterface->nicEvent =
TRUE;
443 rsr = EMAC->EMAC_RSR;
446 if((rsr & (EMAC_RSR_OVR | EMAC_RSR_REC | EMAC_RSR_BNA)) != 0)
449 EMAC->EMAC_RSR = rsr;
491 if((txBufferDesc[txBufferIndex].status &
EMAC_TX_USED) == 0)
520 EMAC->EMAC_NCR |= EMAC_NCR_TSTART;
523 if((txBufferDesc[txBufferIndex].status &
EMAC_TX_USED) != 0)
561 j = rxBufferIndex + i;
584 if((rxBufferDesc[j].status &
EMAC_RX_EOF) != 0 && sofIndex != UINT_MAX)
598 if(eofIndex != UINT_MAX)
602 else if(sofIndex != UINT_MAX)
615 for(i = 0; i < j; i++)
618 if(eofIndex != UINT_MAX && i >= sofIndex && i <= eofIndex)
678 uint32_t hashTable[2];
686 EMAC->EMAC_SA[0].EMAC_SAxB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
687 EMAC->EMAC_SA[0].EMAC_SAxT = interface->macAddr.w[2];
703 entry = &interface->macAddrFilter[i];
715 k = (
p[0] >> 6) ^
p[0];
716 k ^= (
p[1] >> 4) ^ (
p[1] << 2);
717 k ^= (
p[2] >> 2) ^ (
p[2] << 4);
718 k ^= (
p[3] >> 6) ^
p[3];
719 k ^= (
p[4] >> 4) ^ (
p[4] << 2);
720 k ^= (
p[5] >> 2) ^ (
p[5] << 4);
726 hashTable[k / 32] |= (1 << (k % 32));
734 unicastMacAddr[j++] = entry->
addr;
744 EMAC->EMAC_SA[1].EMAC_SAxB = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
745 EMAC->EMAC_SA[1].EMAC_SAxT = unicastMacAddr[0].w[2];
750 EMAC->EMAC_SA[1].EMAC_SAxB = 0;
757 EMAC->EMAC_SA[2].EMAC_SAxB = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
758 EMAC->EMAC_SA[2].EMAC_SAxT = unicastMacAddr[1].w[2];
763 EMAC->EMAC_SA[2].EMAC_SAxB = 0;
770 EMAC->EMAC_SA[3].EMAC_SAxB = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
771 EMAC->EMAC_SA[3].EMAC_SAxT = unicastMacAddr[2].w[2];
776 EMAC->EMAC_SA[3].EMAC_SAxB = 0;
780 EMAC->EMAC_HRB = hashTable[0];
781 EMAC->EMAC_HRT = hashTable[1];
784 TRACE_DEBUG(
" HRB = %08" PRIX32
"\r\n", EMAC->EMAC_HRB);
785 TRACE_DEBUG(
" HRT = %08" PRIX32
"\r\n", EMAC->EMAC_HRT);
803 config = EMAC->EMAC_NCFGR;
808 config |= EMAC_NCFGR_SPD;
812 config &= ~EMAC_NCFGR_SPD;
818 config |= EMAC_NCFGR_FD;
822 config &= ~EMAC_NCFGR_FD;
826 EMAC->EMAC_NCFGR = config;
850 temp = EMAC_MAN_SOF(1) | EMAC_MAN_RW(1) | EMAC_MAN_CODE(2);
852 temp |= EMAC_MAN_PHYA(phyAddr);
854 temp |= EMAC_MAN_REGA(
regAddr);
856 temp |= EMAC_MAN_DATA(
data);
859 EMAC->EMAC_MAN = temp;
861 while((EMAC->EMAC_NSR & EMAC_NSR_IDLE) == 0)
890 temp = EMAC_MAN_SOF(1) | EMAC_MAN_RW(2) | EMAC_MAN_CODE(2);
892 temp |= EMAC_MAN_PHYA(phyAddr);
894 temp |= EMAC_MAN_REGA(
regAddr);
897 EMAC->EMAC_MAN = temp;
899 while((EMAC->EMAC_NSR & EMAC_NSR_IDLE) == 0)
904 data = EMAC->EMAC_MAN & EMAC_MAN_DATA_Msk;