32 #define TRACE_LEVEL NIC_TRACE_LEVEL
35 #include "fsl_device_registers.h"
37 #include "fsl_iomuxc.h"
46 #if defined(__ICCARM__)
49 #pragma data_alignment = 64
50 #pragma location = MIMXRT1060_ETH1_RAM_SECTION
53 #pragma data_alignment = 64
54 #pragma location = MIMXRT1060_ETH1_RAM_SECTION
57 #pragma data_alignment = 64
58 #pragma location = MIMXRT1060_ETH1_RAM_SECTION
61 #pragma data_alignment = 64
62 #pragma location = MIMXRT1060_ETH1_RAM_SECTION
84 static uint_t txBufferIndex;
86 static uint_t rxBufferIndex;
126 TRACE_INFO(
"Initializing i.MX RT1060 Ethernet MAC (ENET)...\r\n");
129 nicDriverInterface = interface;
132 CLOCK_EnableClock(kCLOCK_Enet);
138 ENET->ECR = ENET_ECR_RESET_MASK;
140 while((ENET->ECR & ENET_ECR_RESET_MASK) != 0)
146 ENET_RCR_RMII_MODE_MASK | ENET_RCR_MII_MODE_MASK;
151 ENET->MSCR = ENET_MSCR_HOLDTIME(10) | ENET_MSCR_MII_SPEED(120);
154 if(interface->phyDriver != NULL)
157 error = interface->phyDriver->init(interface);
159 else if(interface->switchDriver != NULL)
162 error = interface->switchDriver->init(interface);
177 value = interface->macAddr.b[5];
178 value |= (interface->macAddr.b[4] << 8);
179 ENET->PAUR = ENET_PAUR_PADDR2(
value) | ENET_PAUR_TYPE(0x8808);
182 value = interface->macAddr.b[3];
183 value |= (interface->macAddr.b[2] << 8);
184 value |= (interface->macAddr.b[1] << 16);
185 value |= (interface->macAddr.b[0] << 24);
186 ENET->PALR = ENET_PALR_PADDR1(
value);
201 ENET->ECR = ENET_ECR_DBSWP_MASK | ENET_ECR_EN1588_MASK;
204 ENET->MIBC = ENET_MIBC_MIB_CLEAR_MASK;
211 ENET->EIR = 0xFFFFFFFF;
213 ENET->EIMR = ENET_EIMR_TXF_MASK | ENET_EIMR_RXF_MASK | ENET_EIMR_EBERR_MASK;
223 ENET->ECR |= ENET_ECR_ETHEREN_MASK;
225 ENET->RDAR = ENET_RDAR_RDAR_MASK;
243 #if defined(USE_MIMXRT1060_EVK) || defined(USE_MIMXRT1064_EVK)
244 gpio_pin_config_t pinConfig;
245 clock_enet_pll_config_t pllConfig;
248 pllConfig.enableClkOutput =
true;
249 pllConfig.enableClkOutput25M =
false;
250 pllConfig.loopDivider = 1;
252 pllConfig.enableClkOutput1 =
true;
253 pllConfig.loopDivider1 = 1;
254 CLOCK_InitEnetPll(&pllConfig);
257 IOMUXC_EnableMode(IOMUXC_GPR, kIOMUXC_GPR_ENET1TxClkOutputDir,
true);
260 CLOCK_EnableClock(kCLOCK_Iomuxc);
263 IOMUXC_SetPinMux(IOMUXC_GPIO_B1_04_ENET_RX_DATA00, 0);
266 IOMUXC_SetPinConfig(IOMUXC_GPIO_B1_04_ENET_RX_DATA00,
267 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
268 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
269 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
270 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
271 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
272 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
273 IOMUXC_SW_PAD_CTL_PAD_DSE(0) |
274 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
277 IOMUXC_SetPinMux(IOMUXC_GPIO_B1_05_ENET_RX_DATA01, 0);
280 IOMUXC_SetPinConfig(IOMUXC_GPIO_B1_05_ENET_RX_DATA01,
281 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
282 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
283 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
284 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
285 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
286 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
287 IOMUXC_SW_PAD_CTL_PAD_DSE(0) |
288 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
291 IOMUXC_SetPinMux(IOMUXC_GPIO_B1_06_ENET_RX_EN, 0);
294 IOMUXC_SetPinConfig(IOMUXC_GPIO_B1_06_ENET_RX_EN,
295 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
296 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
297 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
298 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
299 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
300 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
301 IOMUXC_SW_PAD_CTL_PAD_DSE(0) |
302 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
305 IOMUXC_SetPinMux(IOMUXC_GPIO_B1_07_ENET_TX_DATA00, 0);
308 IOMUXC_SetPinConfig(IOMUXC_GPIO_B1_07_ENET_TX_DATA00,
309 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
310 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
311 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
312 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
313 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
314 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
315 IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
316 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
319 IOMUXC_SetPinMux(IOMUXC_GPIO_B1_08_ENET_TX_DATA01, 0);
322 IOMUXC_SetPinConfig(IOMUXC_GPIO_B1_08_ENET_TX_DATA01,
323 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
324 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
325 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
326 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
327 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
328 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
329 IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
330 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
333 IOMUXC_SetPinMux(IOMUXC_GPIO_B1_09_ENET_TX_EN, 0);
336 IOMUXC_SetPinConfig(IOMUXC_GPIO_B1_09_ENET_TX_EN,
337 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
338 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
339 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
340 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
341 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
342 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
343 IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
344 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
347 IOMUXC_SetPinMux(IOMUXC_GPIO_B1_10_ENET_REF_CLK, 1);
350 IOMUXC_SetPinConfig(IOMUXC_GPIO_B1_10_ENET_REF_CLK,
351 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
352 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
353 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
354 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
355 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
356 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
357 IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
358 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
361 IOMUXC_SetPinMux(IOMUXC_GPIO_B1_11_ENET_RX_ER, 0);
364 IOMUXC_SetPinConfig(IOMUXC_GPIO_B1_11_ENET_RX_ER,
365 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
366 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
367 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
368 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
369 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
370 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
371 IOMUXC_SW_PAD_CTL_PAD_DSE(0) |
372 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
375 IOMUXC_SetPinMux(IOMUXC_GPIO_EMC_40_ENET_MDC, 0);
378 IOMUXC_SetPinConfig(IOMUXC_GPIO_EMC_40_ENET_MDC,
379 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
380 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
381 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
382 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
383 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
384 IOMUXC_SW_PAD_CTL_PAD_SPEED(0) |
385 IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
386 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
389 IOMUXC_SetPinMux(IOMUXC_GPIO_EMC_41_ENET_MDIO, 0);
392 IOMUXC_SetPinConfig(IOMUXC_GPIO_EMC_41_ENET_MDIO,
393 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
394 IOMUXC_SW_PAD_CTL_PAD_PUS(2) |
395 IOMUXC_SW_PAD_CTL_PAD_PUE(1) |
396 IOMUXC_SW_PAD_CTL_PAD_PKE(1) |
397 IOMUXC_SW_PAD_CTL_PAD_ODE(1) |
398 IOMUXC_SW_PAD_CTL_PAD_SPEED(0) |
399 IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
400 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
403 IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B0_09_GPIO1_IO09, 0);
406 IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B0_09_GPIO1_IO09,
407 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
408 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
409 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
410 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
411 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
412 IOMUXC_SW_PAD_CTL_PAD_SPEED(0) |
413 IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
414 IOMUXC_SW_PAD_CTL_PAD_SRE(0));
417 IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B0_10_GPIO1_IO10, 0);
420 IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B0_10_GPIO1_IO10,
421 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
422 IOMUXC_SW_PAD_CTL_PAD_PUS(2) |
423 IOMUXC_SW_PAD_CTL_PAD_PUE(1) |
424 IOMUXC_SW_PAD_CTL_PAD_PKE(1) |
425 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
426 IOMUXC_SW_PAD_CTL_PAD_SPEED(0) |
427 IOMUXC_SW_PAD_CTL_PAD_DSE(0) |
428 IOMUXC_SW_PAD_CTL_PAD_SRE(0));
431 pinConfig.direction = kGPIO_DigitalOutput;
432 pinConfig.outputLogic = 0;
433 pinConfig.interruptMode = kGPIO_NoIntmode;
434 GPIO_PinInit(GPIO1, 9, &pinConfig);
437 pinConfig.direction = kGPIO_DigitalInput;
438 pinConfig.outputLogic = 0;
439 pinConfig.interruptMode = kGPIO_NoIntmode;
440 GPIO_PinInit(GPIO1, 10, &pinConfig);
443 GPIO_PinWrite(GPIO1, 9, 0);
445 GPIO_PinWrite(GPIO1, 9, 1);
462 osMemset(txBufferDesc, 0,
sizeof(txBufferDesc));
463 osMemset(rxBufferDesc, 0,
sizeof(rxBufferDesc));
500 ENET->TDSR = (uint32_t) txBufferDesc;
502 ENET->RDSR = (uint32_t) rxBufferDesc;
520 if(interface->phyDriver != NULL)
523 interface->phyDriver->tick(interface);
525 else if(interface->switchDriver != NULL)
528 interface->switchDriver->tick(interface);
545 NVIC_EnableIRQ(ENET_IRQn);
548 if(interface->phyDriver != NULL)
551 interface->phyDriver->enableIrq(interface);
553 else if(interface->switchDriver != NULL)
556 interface->switchDriver->enableIrq(interface);
573 NVIC_DisableIRQ(ENET_IRQn);
576 if(interface->phyDriver != NULL)
579 interface->phyDriver->disableIrq(interface);
581 else if(interface->switchDriver != NULL)
584 interface->switchDriver->disableIrq(interface);
611 if((events & ENET_EIR_TXF_MASK) != 0)
614 ENET->EIR = ENET_EIR_TXF_MASK;
617 if((txBufferDesc[txBufferIndex][0] &
ENET_TBD0_R) == 0)
624 ENET->TDAR = ENET_TDAR_TDAR_MASK;
628 if((events & ENET_EIR_RXF_MASK) != 0)
631 ENET->EIMR &= ~ENET_EIMR_RXF_MASK;
634 nicDriverInterface->nicEvent =
TRUE;
640 if((events & ENET_EIR_EBERR_MASK) != 0)
643 ENET->EIMR &= ~ENET_EIMR_EBERR_MASK;
646 nicDriverInterface->nicEvent =
TRUE;
670 if((status & ENET_EIR_RXF_MASK) != 0)
673 ENET->EIR = ENET_EIR_RXF_MASK;
686 if((status & ENET_EIR_EBERR_MASK) != 0)
689 ENET->EIR = ENET_EIR_EBERR_MASK;
692 ENET->ECR &= ~ENET_ECR_ETHEREN_MASK;
696 ENET->ECR |= ENET_ECR_ETHEREN_MASK;
698 ENET->RDAR = ENET_RDAR_RDAR_MASK;
702 ENET->EIMR = ENET_EIMR_TXF_MASK | ENET_EIMR_RXF_MASK | ENET_EIMR_EBERR_MASK;
734 if((txBufferDesc[txBufferIndex][0] &
ENET_TBD0_R) != 0)
743 txBufferDesc[txBufferIndex][4] = 0;
769 ENET->TDAR = ENET_TDAR_TDAR_MASK;
772 if((txBufferDesc[txBufferIndex][0] &
ENET_TBD0_R) == 0)
796 if((rxBufferDesc[rxBufferIndex][0] &
ENET_RBD0_E) == 0)
799 if((rxBufferDesc[rxBufferIndex][0] &
ENET_RBD0_L) != 0)
832 rxBufferDesc[rxBufferIndex][4] = 0;
851 ENET->RDAR = ENET_RDAR_RDAR_MASK;
876 uint32_t unicastHashTable[2];
877 uint32_t multicastHashTable[2];
884 value = interface->macAddr.b[5];
885 value |= (interface->macAddr.b[4] << 8);
886 ENET->PAUR = ENET_PAUR_PADDR2(
value) | ENET_PAUR_TYPE(0x8808);
889 value = interface->macAddr.b[3];
890 value |= (interface->macAddr.b[2] << 8);
891 value |= (interface->macAddr.b[1] << 16);
892 value |= (interface->macAddr.b[0] << 24);
893 ENET->PALR = ENET_PALR_PADDR1(
value);
896 unicastHashTable[0] = 0;
897 unicastHashTable[1] = 0;
900 multicastHashTable[0] = 0;
901 multicastHashTable[1] = 0;
908 entry = &interface->macAddrFilter[i];
918 k = (crc >> 26) & 0x3F;
924 multicastHashTable[k / 32] |= (1 << (k % 32));
929 unicastHashTable[k / 32] |= (1 << (k % 32));
935 ENET->IALR = unicastHashTable[0];
936 ENET->IAUR = unicastHashTable[1];
939 ENET->GALR = multicastHashTable[0];
940 ENET->GAUR = multicastHashTable[1];
943 TRACE_DEBUG(
" IALR = %08" PRIX32
"\r\n", ENET->IALR);
944 TRACE_DEBUG(
" IAUR = %08" PRIX32
"\r\n", ENET->IAUR);
945 TRACE_DEBUG(
" GALR = %08" PRIX32
"\r\n", ENET->GALR);
946 TRACE_DEBUG(
" GAUR = %08" PRIX32
"\r\n", ENET->GAUR);
962 ENET->ECR &= ~ENET_ECR_ETHEREN_MASK;
968 ENET->RCR &= ~ENET_RCR_RMII_10T_MASK;
973 ENET->RCR |= ENET_RCR_RMII_10T_MASK;
980 ENET->TCR |= ENET_TCR_FDEN_MASK;
982 ENET->RCR &= ~ENET_RCR_DRT_MASK;
987 ENET->TCR &= ~ENET_TCR_FDEN_MASK;
989 ENET->RCR |= ENET_RCR_DRT_MASK;
996 ENET->ECR |= ENET_ECR_ETHEREN_MASK;
998 ENET->RDAR = ENET_RDAR_RDAR_MASK;
1022 temp = ENET_MMFR_ST(1) | ENET_MMFR_OP(1) | ENET_MMFR_TA(2);
1024 temp |= ENET_MMFR_PA(phyAddr);
1026 temp |= ENET_MMFR_RA(
regAddr);
1028 temp |= ENET_MMFR_DATA(
data);
1031 ENET->EIR = ENET_EIR_MII_MASK;
1036 while((ENET->EIR & ENET_EIR_MII_MASK) == 0)
1065 temp = ENET_MMFR_ST(1) | ENET_MMFR_OP(2) | ENET_MMFR_TA(2);
1067 temp |= ENET_MMFR_PA(phyAddr);
1069 temp |= ENET_MMFR_RA(
regAddr);
1072 ENET->EIR = ENET_EIR_MII_MASK;
1077 while((ENET->EIR & ENET_EIR_MII_MASK) == 0)
1082 data = ENET->MMFR & ENET_MMFR_DATA_MASK;
1110 p = (uint8_t *)
data;
1115 for(i = 0; i <
length; i++)
1121 for(j = 0; j < 8; j++)
1123 if((crc & 0x01) != 0)
1125 crc = (crc >> 1) ^ 0xEDB88320;