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 = MIMXRT1050_ETH_RAM_SECTION
53 #pragma data_alignment = 64
54 #pragma location = MIMXRT1050_ETH_RAM_SECTION
57 #pragma data_alignment = 64
58 #pragma location = MIMXRT1050_ETH_RAM_SECTION
61 #pragma data_alignment = 64
62 #pragma location = MIMXRT1050_ETH_RAM_SECTION
84 static uint_t txBufferIndex;
86 static uint_t rxBufferIndex;
126 TRACE_INFO(
"Initializing i.MX RT1050 Ethernet MAC...\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_MIMXRT1050_EVK) || defined(USE_MIMXRT1050_EVKB)
244 gpio_pin_config_t pinConfig;
245 clock_enet_pll_config_t pllConfig;
247 #if (defined(CPU_MIMXRT1052CVL5A) || defined(CPU_MIMXRT1052DVL6A))
249 pllConfig.enableClkOutput0 =
true;
250 pllConfig.enableClkOutput1 =
false;
251 pllConfig.enableClkOutput2 =
false;
252 pllConfig.loopDivider0 = 1;
253 pllConfig.loopDivider1 = 0;
254 CLOCK_InitEnetPll(&pllConfig);
257 pllConfig.enableClkOutput =
true;
258 pllConfig.enableClkOutput25M =
false;
259 pllConfig.loopDivider = 1;
261 CLOCK_InitEnetPll(&pllConfig);
265 IOMUXC_EnableMode(IOMUXC_GPR, kIOMUXC_GPR_ENET1TxClkOutputDir,
true);
268 CLOCK_EnableClock(kCLOCK_Iomuxc);
271 IOMUXC_SetPinMux(IOMUXC_GPIO_B1_04_ENET_RX_DATA00, 0);
274 IOMUXC_SetPinConfig(IOMUXC_GPIO_B1_04_ENET_RX_DATA00,
275 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
276 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
277 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
278 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
279 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
280 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
281 IOMUXC_SW_PAD_CTL_PAD_DSE(0) |
282 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
285 IOMUXC_SetPinMux(IOMUXC_GPIO_B1_05_ENET_RX_DATA01, 0);
288 IOMUXC_SetPinConfig(IOMUXC_GPIO_B1_05_ENET_RX_DATA01,
289 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
290 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
291 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
292 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
293 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
294 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
295 IOMUXC_SW_PAD_CTL_PAD_DSE(0) |
296 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
299 IOMUXC_SetPinMux(IOMUXC_GPIO_B1_06_ENET_RX_EN, 0);
302 IOMUXC_SetPinConfig(IOMUXC_GPIO_B1_06_ENET_RX_EN,
303 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
304 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
305 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
306 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
307 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
308 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
309 IOMUXC_SW_PAD_CTL_PAD_DSE(0) |
310 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
313 IOMUXC_SetPinMux(IOMUXC_GPIO_B1_07_ENET_TX_DATA00, 0);
316 IOMUXC_SetPinConfig(IOMUXC_GPIO_B1_07_ENET_TX_DATA00,
317 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
318 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
319 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
320 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
321 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
322 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
323 IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
324 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
327 IOMUXC_SetPinMux(IOMUXC_GPIO_B1_08_ENET_TX_DATA01, 0);
330 IOMUXC_SetPinConfig(IOMUXC_GPIO_B1_08_ENET_TX_DATA01,
331 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
332 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
333 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
334 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
335 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
336 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
337 IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
338 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
341 IOMUXC_SetPinMux(IOMUXC_GPIO_B1_09_ENET_TX_EN, 0);
344 IOMUXC_SetPinConfig(IOMUXC_GPIO_B1_09_ENET_TX_EN,
345 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
346 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
347 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
348 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
349 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
350 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
351 IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
352 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
355 IOMUXC_SetPinMux(IOMUXC_GPIO_B1_10_ENET_REF_CLK, 1);
358 IOMUXC_SetPinConfig(IOMUXC_GPIO_B1_10_ENET_REF_CLK,
359 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
360 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
361 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
362 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
363 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
364 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
365 IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
366 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
369 IOMUXC_SetPinMux(IOMUXC_GPIO_B1_11_ENET_RX_ER, 0);
372 IOMUXC_SetPinConfig(IOMUXC_GPIO_B1_11_ENET_RX_ER,
373 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
374 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
375 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
376 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
377 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
378 IOMUXC_SW_PAD_CTL_PAD_SPEED(3) |
379 IOMUXC_SW_PAD_CTL_PAD_DSE(0) |
380 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
383 IOMUXC_SetPinMux(IOMUXC_GPIO_EMC_40_ENET_MDC, 0);
386 IOMUXC_SetPinConfig(IOMUXC_GPIO_EMC_40_ENET_MDC,
387 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
388 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
389 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
390 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
391 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
392 IOMUXC_SW_PAD_CTL_PAD_SPEED(0) |
393 IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
394 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
397 IOMUXC_SetPinMux(IOMUXC_GPIO_EMC_41_ENET_MDIO, 0);
400 IOMUXC_SetPinConfig(IOMUXC_GPIO_EMC_41_ENET_MDIO,
401 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
402 IOMUXC_SW_PAD_CTL_PAD_PUS(2) |
403 IOMUXC_SW_PAD_CTL_PAD_PUE(1) |
404 IOMUXC_SW_PAD_CTL_PAD_PKE(1) |
405 IOMUXC_SW_PAD_CTL_PAD_ODE(1) |
406 IOMUXC_SW_PAD_CTL_PAD_SPEED(0) |
407 IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
408 IOMUXC_SW_PAD_CTL_PAD_SRE(1));
411 IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B0_09_GPIO1_IO09, 0);
414 IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B0_09_GPIO1_IO09,
415 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
416 IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
417 IOMUXC_SW_PAD_CTL_PAD_PUE(0) |
418 IOMUXC_SW_PAD_CTL_PAD_PKE(0) |
419 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
420 IOMUXC_SW_PAD_CTL_PAD_SPEED(0) |
421 IOMUXC_SW_PAD_CTL_PAD_DSE(5) |
422 IOMUXC_SW_PAD_CTL_PAD_SRE(0));
425 IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B0_10_GPIO1_IO10, 0);
428 IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B0_10_GPIO1_IO10,
429 IOMUXC_SW_PAD_CTL_PAD_HYS(0) |
430 IOMUXC_SW_PAD_CTL_PAD_PUS(2) |
431 IOMUXC_SW_PAD_CTL_PAD_PUE(1) |
432 IOMUXC_SW_PAD_CTL_PAD_PKE(1) |
433 IOMUXC_SW_PAD_CTL_PAD_ODE(0) |
434 IOMUXC_SW_PAD_CTL_PAD_SPEED(0) |
435 IOMUXC_SW_PAD_CTL_PAD_DSE(0) |
436 IOMUXC_SW_PAD_CTL_PAD_SRE(0));
439 pinConfig.direction = kGPIO_DigitalOutput;
440 pinConfig.outputLogic = 0;
441 pinConfig.interruptMode = kGPIO_NoIntmode;
442 GPIO_PinInit(GPIO1, 9, &pinConfig);
445 pinConfig.direction = kGPIO_DigitalInput;
446 pinConfig.outputLogic = 0;
447 pinConfig.interruptMode = kGPIO_NoIntmode;
448 GPIO_PinInit(GPIO1, 10, &pinConfig);
451 GPIO_PinWrite(GPIO1, 9, 0);
453 GPIO_PinWrite(GPIO1, 9, 1);
470 osMemset(txBufferDesc, 0,
sizeof(txBufferDesc));
471 osMemset(rxBufferDesc, 0,
sizeof(rxBufferDesc));
508 ENET->TDSR = (uint32_t) txBufferDesc;
510 ENET->RDSR = (uint32_t) rxBufferDesc;
528 if(interface->phyDriver != NULL)
531 interface->phyDriver->tick(interface);
533 else if(interface->switchDriver != NULL)
536 interface->switchDriver->tick(interface);
553 NVIC_EnableIRQ(ENET_IRQn);
556 if(interface->phyDriver != NULL)
559 interface->phyDriver->enableIrq(interface);
561 else if(interface->switchDriver != NULL)
564 interface->switchDriver->enableIrq(interface);
581 NVIC_DisableIRQ(ENET_IRQn);
584 if(interface->phyDriver != NULL)
587 interface->phyDriver->disableIrq(interface);
589 else if(interface->switchDriver != NULL)
592 interface->switchDriver->disableIrq(interface);
619 if((events & ENET_EIR_TXF_MASK) != 0)
622 ENET->EIR = ENET_EIR_TXF_MASK;
625 if((txBufferDesc[txBufferIndex][0] &
ENET_TBD0_R) == 0)
632 ENET->TDAR = ENET_TDAR_TDAR_MASK;
636 if((events & ENET_EIR_RXF_MASK) != 0)
639 ENET->EIMR &= ~ENET_EIMR_RXF_MASK;
642 nicDriverInterface->nicEvent =
TRUE;
648 if((events & ENET_EIR_EBERR_MASK) != 0)
651 ENET->EIMR &= ~ENET_EIMR_EBERR_MASK;
654 nicDriverInterface->nicEvent =
TRUE;
678 if((status & ENET_EIR_RXF_MASK) != 0)
681 ENET->EIR = ENET_EIR_RXF_MASK;
694 if((status & ENET_EIR_EBERR_MASK) != 0)
697 ENET->EIR = ENET_EIR_EBERR_MASK;
700 ENET->ECR &= ~ENET_ECR_ETHEREN_MASK;
704 ENET->ECR |= ENET_ECR_ETHEREN_MASK;
706 ENET->RDAR = ENET_RDAR_RDAR_MASK;
710 ENET->EIMR = ENET_EIMR_TXF_MASK | ENET_EIMR_RXF_MASK | ENET_EIMR_EBERR_MASK;
742 if((txBufferDesc[txBufferIndex][0] &
ENET_TBD0_R) != 0)
751 txBufferDesc[txBufferIndex][4] = 0;
777 ENET->TDAR = ENET_TDAR_TDAR_MASK;
780 if((txBufferDesc[txBufferIndex][0] &
ENET_TBD0_R) == 0)
804 if((rxBufferDesc[rxBufferIndex][0] &
ENET_RBD0_E) == 0)
807 if((rxBufferDesc[rxBufferIndex][0] &
ENET_RBD0_L) != 0)
840 rxBufferDesc[rxBufferIndex][4] = 0;
859 ENET->RDAR = ENET_RDAR_RDAR_MASK;
884 uint32_t unicastHashTable[2];
885 uint32_t multicastHashTable[2];
892 value = interface->macAddr.b[5];
893 value |= (interface->macAddr.b[4] << 8);
894 ENET->PAUR = ENET_PAUR_PADDR2(
value) | ENET_PAUR_TYPE(0x8808);
897 value = interface->macAddr.b[3];
898 value |= (interface->macAddr.b[2] << 8);
899 value |= (interface->macAddr.b[1] << 16);
900 value |= (interface->macAddr.b[0] << 24);
901 ENET->PALR = ENET_PALR_PADDR1(
value);
904 unicastHashTable[0] = 0;
905 unicastHashTable[1] = 0;
908 multicastHashTable[0] = 0;
909 multicastHashTable[1] = 0;
916 entry = &interface->macAddrFilter[i];
926 k = (crc >> 26) & 0x3F;
932 multicastHashTable[k / 32] |= (1 << (k % 32));
937 unicastHashTable[k / 32] |= (1 << (k % 32));
943 ENET->IALR = unicastHashTable[0];
944 ENET->IAUR = unicastHashTable[1];
947 ENET->GALR = multicastHashTable[0];
948 ENET->GAUR = multicastHashTable[1];
951 TRACE_DEBUG(
" IALR = %08" PRIX32
"\r\n", ENET->IALR);
952 TRACE_DEBUG(
" IAUR = %08" PRIX32
"\r\n", ENET->IAUR);
953 TRACE_DEBUG(
" GALR = %08" PRIX32
"\r\n", ENET->GALR);
954 TRACE_DEBUG(
" GAUR = %08" PRIX32
"\r\n", ENET->GAUR);
970 ENET->ECR &= ~ENET_ECR_ETHEREN_MASK;
976 ENET->RCR &= ~ENET_RCR_RMII_10T_MASK;
981 ENET->RCR |= ENET_RCR_RMII_10T_MASK;
988 ENET->TCR |= ENET_TCR_FDEN_MASK;
990 ENET->RCR &= ~ENET_RCR_DRT_MASK;
995 ENET->TCR &= ~ENET_TCR_FDEN_MASK;
997 ENET->RCR |= ENET_RCR_DRT_MASK;
1004 ENET->ECR |= ENET_ECR_ETHEREN_MASK;
1006 ENET->RDAR = ENET_RDAR_RDAR_MASK;
1030 temp = ENET_MMFR_ST(1) | ENET_MMFR_OP(1) | ENET_MMFR_TA(2);
1032 temp |= ENET_MMFR_PA(phyAddr);
1034 temp |= ENET_MMFR_RA(
regAddr);
1036 temp |= ENET_MMFR_DATA(
data);
1039 ENET->EIR = ENET_EIR_MII_MASK;
1044 while((ENET->EIR & ENET_EIR_MII_MASK) == 0)
1073 temp = ENET_MMFR_ST(1) | ENET_MMFR_OP(2) | ENET_MMFR_TA(2);
1075 temp |= ENET_MMFR_PA(phyAddr);
1077 temp |= ENET_MMFR_RA(
regAddr);
1080 ENET->EIR = ENET_EIR_MII_MASK;
1085 while((ENET->EIR & ENET_EIR_MII_MASK) == 0)
1090 data = ENET->MMFR & ENET_MMFR_DATA_MASK;
1118 p = (uint8_t *)
data;
1123 for(i = 0; i <
length; i++)
1129 for(j = 0; j < 8; j++)
1131 if((crc & 0x01) != 0)
1133 crc = (crc >> 1) ^ 0xEDB88320;