32 #define TRACE_LEVEL NIC_TRACE_LEVEL
44 #if defined(__ICCARM__)
47 #pragma data_alignment = 16
50 #pragma data_alignment = 16
53 #pragma data_alignment = 16
56 #pragma data_alignment = 16
78 static uint_t txBufferIndex;
80 static uint_t rxBufferIndex;
120 TRACE_INFO(
"Initializing Kinetis K7x Ethernet MAC...\r\n");
123 nicDriverInterface = interface;
129 OSC0->CR |= OSC_CR_ERCLKEN_MASK;
131 SIM->SCGC2 |= SIM_SCGC2_ENET_MASK;
137 ENET->ECR = ENET_ECR_RESET_MASK;
139 while((ENET->ECR & ENET_ECR_RESET_MASK) != 0)
145 ENET_RCR_RMII_MODE_MASK | ENET_RCR_MII_MODE_MASK;
150 ENET->MSCR = ENET_MSCR_MII_SPEED(59);
153 if(interface->phyDriver != NULL)
156 error = interface->phyDriver->init(interface);
158 else if(interface->switchDriver != NULL)
161 error = interface->switchDriver->init(interface);
176 value = interface->macAddr.b[5];
177 value |= (interface->macAddr.b[4] << 8);
178 ENET->PAUR = ENET_PAUR_PADDR2(
value) | ENET_PAUR_TYPE(0x8808);
181 value = interface->macAddr.b[3];
182 value |= (interface->macAddr.b[2] << 8);
183 value |= (interface->macAddr.b[1] << 16);
184 value |= (interface->macAddr.b[0] << 24);
185 ENET->PALR = ENET_PALR_PADDR1(
value);
200 ENET->ECR = ENET_ECR_DBSWP_MASK | ENET_ECR_EN1588_MASK;
203 ENET->MIBC = ENET_MIBC_MIB_CLEAR_MASK;
210 ENET->EIR = 0xFFFFFFFF;
212 ENET->EIMR = ENET_EIMR_TXF_MASK | ENET_EIMR_RXF_MASK | ENET_EIMR_EBERR_MASK;
230 ENET->ECR |= ENET_ECR_ETHEREN_MASK;
232 ENET->RDAR = ENET_RDAR_RDAR_MASK;
250 #if defined(USE_TWR_K70F120M)
252 SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK;
255 PORTA->PCR[5] = PORT_PCR_MUX(4) | PORT_PCR_PE_MASK;
257 PORTA->PCR[12] = PORT_PCR_MUX(4);
259 PORTA->PCR[13] = PORT_PCR_MUX(4);
261 PORTA->PCR[14] = PORT_PCR_MUX(4);
263 PORTA->PCR[15] = PORT_PCR_MUX(4);
265 PORTA->PCR[16] = PORT_PCR_MUX(4);
267 PORTA->PCR[17] = PORT_PCR_MUX(4);
270 PORTB->PCR[0] = PORT_PCR_MUX(4) | PORT_PCR_PE_MASK | PORT_PCR_PS_MASK;
272 PORTB->PCR[1] = PORT_PCR_MUX(4);
288 osMemset(txBufferDesc, 0,
sizeof(txBufferDesc));
289 osMemset(rxBufferDesc, 0,
sizeof(rxBufferDesc));
326 ENET->TDSR = (uint32_t) txBufferDesc;
328 ENET->RDSR = (uint32_t) rxBufferDesc;
346 if(interface->phyDriver != NULL)
349 interface->phyDriver->tick(interface);
351 else if(interface->switchDriver != NULL)
354 interface->switchDriver->tick(interface);
371 NVIC_EnableIRQ(ENET_Transmit_IRQn);
372 NVIC_EnableIRQ(ENET_Receive_IRQn);
373 NVIC_EnableIRQ(ENET_Error_IRQn);
376 if(interface->phyDriver != NULL)
379 interface->phyDriver->enableIrq(interface);
381 else if(interface->switchDriver != NULL)
384 interface->switchDriver->enableIrq(interface);
401 NVIC_DisableIRQ(ENET_Transmit_IRQn);
402 NVIC_DisableIRQ(ENET_Receive_IRQn);
403 NVIC_DisableIRQ(ENET_Error_IRQn);
406 if(interface->phyDriver != NULL)
409 interface->phyDriver->disableIrq(interface);
411 else if(interface->switchDriver != NULL)
414 interface->switchDriver->disableIrq(interface);
438 if((ENET->EIR & ENET_EIR_TXF_MASK) != 0)
441 ENET->EIR = ENET_EIR_TXF_MASK;
444 if((txBufferDesc[txBufferIndex][0] &
ENET_TBD0_R) == 0)
451 ENET->TDAR = ENET_TDAR_TDAR_MASK;
474 if((ENET->EIR & ENET_EIR_RXF_MASK) != 0)
477 ENET->EIMR &= ~ENET_EIMR_RXF_MASK;
480 nicDriverInterface->nicEvent =
TRUE;
505 if((ENET->EIR & ENET_EIR_EBERR_MASK) != 0)
508 ENET->EIMR &= ~ENET_EIMR_EBERR_MASK;
511 nicDriverInterface->nicEvent =
TRUE;
535 if((status & ENET_EIR_RXF_MASK) != 0)
538 ENET->EIR = ENET_EIR_RXF_MASK;
551 if((status & ENET_EIR_EBERR_MASK) != 0)
554 ENET->EIR = ENET_EIR_EBERR_MASK;
557 ENET->ECR &= ~ENET_ECR_ETHEREN_MASK;
561 ENET->ECR |= ENET_ECR_ETHEREN_MASK;
563 ENET->RDAR = ENET_RDAR_RDAR_MASK;
567 ENET->EIMR = ENET_EIMR_TXF_MASK | ENET_EIMR_RXF_MASK | ENET_EIMR_EBERR_MASK;
599 if((txBufferDesc[txBufferIndex][0] &
ENET_TBD0_R) != 0)
608 txBufferDesc[txBufferIndex][4] = 0;
631 ENET->TDAR = ENET_TDAR_TDAR_MASK;
634 if((txBufferDesc[txBufferIndex][0] &
ENET_TBD0_R) == 0)
658 if((rxBufferDesc[rxBufferIndex][0] &
ENET_RBD0_E) == 0)
661 if((rxBufferDesc[rxBufferIndex][0] &
ENET_RBD0_L) != 0)
694 rxBufferDesc[rxBufferIndex][4] = 0;
713 ENET->RDAR = ENET_RDAR_RDAR_MASK;
738 uint32_t unicastHashTable[2];
739 uint32_t multicastHashTable[2];
746 value = interface->macAddr.b[5];
747 value |= (interface->macAddr.b[4] << 8);
748 ENET->PAUR = ENET_PAUR_PADDR2(
value) | ENET_PAUR_TYPE(0x8808);
751 value = interface->macAddr.b[3];
752 value |= (interface->macAddr.b[2] << 8);
753 value |= (interface->macAddr.b[1] << 16);
754 value |= (interface->macAddr.b[0] << 24);
755 ENET->PALR = ENET_PALR_PADDR1(
value);
758 unicastHashTable[0] = 0;
759 unicastHashTable[1] = 0;
762 multicastHashTable[0] = 0;
763 multicastHashTable[1] = 0;
770 entry = &interface->macAddrFilter[i];
780 k = (crc >> 26) & 0x3F;
786 multicastHashTable[k / 32] |= (1 << (k % 32));
791 unicastHashTable[k / 32] |= (1 << (k % 32));
797 ENET->IALR = unicastHashTable[0];
798 ENET->IAUR = unicastHashTable[1];
801 ENET->GALR = multicastHashTable[0];
802 ENET->GAUR = multicastHashTable[1];
805 TRACE_DEBUG(
" IALR = %08" PRIX32
"\r\n", ENET->IALR);
806 TRACE_DEBUG(
" IAUR = %08" PRIX32
"\r\n", ENET->IAUR);
807 TRACE_DEBUG(
" GALR = %08" PRIX32
"\r\n", ENET->GALR);
808 TRACE_DEBUG(
" GAUR = %08" PRIX32
"\r\n", ENET->GAUR);
824 ENET->ECR &= ~ENET_ECR_ETHEREN_MASK;
830 ENET->RCR &= ~ENET_RCR_RMII_10T_MASK;
835 ENET->RCR |= ENET_RCR_RMII_10T_MASK;
842 ENET->TCR |= ENET_TCR_FDEN_MASK;
844 ENET->RCR &= ~ENET_RCR_DRT_MASK;
849 ENET->TCR &= ~ENET_TCR_FDEN_MASK;
851 ENET->RCR |= ENET_RCR_DRT_MASK;
858 ENET->ECR |= ENET_ECR_ETHEREN_MASK;
860 ENET->RDAR = ENET_RDAR_RDAR_MASK;
884 temp = ENET_MMFR_ST(1) | ENET_MMFR_OP(1) | ENET_MMFR_TA(2);
886 temp |= ENET_MMFR_PA(phyAddr);
890 temp |= ENET_MMFR_DATA(
data);
893 ENET->EIR = ENET_EIR_MII_MASK;
898 while((ENET->EIR & ENET_EIR_MII_MASK) == 0)
927 temp = ENET_MMFR_ST(1) | ENET_MMFR_OP(2) | ENET_MMFR_TA(2);
929 temp |= ENET_MMFR_PA(phyAddr);
934 ENET->EIR = ENET_EIR_MII_MASK;
939 while((ENET->EIR & ENET_EIR_MII_MASK) == 0)
944 data = ENET->MMFR & ENET_MMFR_DATA_MASK;
972 p = (uint8_t *)
data;
977 for(i = 0; i <
length; i++)
983 for(j = 0; j < 8; j++)
985 if((crc & 0x01) != 0)
987 crc = (crc >> 1) ^ 0xEDB88320;