32 #define TRACE_LEVEL NIC_TRACE_LEVEL
35 #include "stm32f7xx.h"
36 #include "stm32f7xx_hal.h"
45 #if defined(__ICCARM__)
48 #pragma data_alignment = 4
49 #pragma location = STM32F7XX_ETH_RAM_SECTION
52 #pragma data_alignment = 4
53 #pragma location = STM32F7XX_ETH_RAM_SECTION
56 #pragma data_alignment = 4
57 #pragma location = STM32F7XX_ETH_RAM_SECTION
60 #pragma data_alignment = 4
61 #pragma location = STM32F7XX_ETH_RAM_SECTION
124 TRACE_INFO(
"Initializing STM32F7 Ethernet MAC...\r\n");
127 nicDriverInterface = interface;
133 __HAL_RCC_ETHMAC_CLK_ENABLE();
134 __HAL_RCC_ETHMACTX_CLK_ENABLE();
135 __HAL_RCC_ETHMACRX_CLK_ENABLE();
138 __HAL_RCC_ETHMAC_FORCE_RESET();
139 __HAL_RCC_ETHMAC_RELEASE_RESET();
142 ETH->DMABMR |= ETH_DMABMR_SR;
144 while((ETH->DMABMR & ETH_DMABMR_SR) != 0)
149 ETH->MACMIIAR = ETH_MACMIIAR_CR_Div102;
152 if(interface->phyDriver != NULL)
155 error = interface->phyDriver->init(interface);
157 else if(interface->switchDriver != NULL)
160 error = interface->switchDriver->init(interface);
183 ETH->DMAOMR = ETH_DMAOMR_RSF | ETH_DMAOMR_TSF;
186 ETH->DMABMR = ETH_DMABMR_AAB | ETH_DMABMR_USP | ETH_DMABMR_RDP_32Beat |
187 ETH_DMABMR_RTPR_1_1 | ETH_DMABMR_PBL_32Beat | ETH_DMABMR_EDE;
194 ETH->MMCTIMR = ETH_MMCTIMR_TGFM | ETH_MMCTIMR_TGFMSCM | ETH_MMCTIMR_TGFSCM;
198 ETH->MMCRIMR = ETH_MMCRIMR_RGUFM | ETH_MMCRIMR_RFAEM | ETH_MMCRIMR_RFCEM;
201 ETH->MACIMR = ETH_MACIMR_TSTIM | ETH_MACIMR_PMTIM;
203 ETH->DMAIER = ETH_DMAIER_NISE | ETH_DMAIER_RIE | ETH_DMAIER_TIE;
213 ETH->MACCR |= ETH_MACCR_TE | ETH_MACCR_RE;
215 ETH->DMAOMR |= ETH_DMAOMR_ST | ETH_DMAOMR_SR;
233 #if defined(USE_STM32F7XX_NUCLEO_144)
234 GPIO_InitTypeDef GPIO_InitStructure;
237 __HAL_RCC_SYSCFG_CLK_ENABLE();
240 __HAL_RCC_GPIOA_CLK_ENABLE();
241 __HAL_RCC_GPIOB_CLK_ENABLE();
242 __HAL_RCC_GPIOC_CLK_ENABLE();
243 __HAL_RCC_GPIOG_CLK_ENABLE();
246 SYSCFG->PMC |= SYSCFG_PMC_MII_RMII_SEL;
249 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
250 GPIO_InitStructure.Pull = GPIO_NOPULL;
251 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
252 GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
255 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7;
256 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
259 GPIO_InitStructure.Pin = GPIO_PIN_13;
260 HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
263 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5;
264 HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
267 GPIO_InitStructure.Pin = GPIO_PIN_11 | GPIO_PIN_13;
268 HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
271 #elif defined(USE_STM32746G_DISCO) || defined(USE_STM32F7508_DISCO)
272 GPIO_InitTypeDef GPIO_InitStructure;
275 __HAL_RCC_SYSCFG_CLK_ENABLE();
278 __HAL_RCC_GPIOA_CLK_ENABLE();
279 __HAL_RCC_GPIOC_CLK_ENABLE();
280 __HAL_RCC_GPIOG_CLK_ENABLE();
283 SYSCFG->PMC |= SYSCFG_PMC_MII_RMII_SEL;
286 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
287 GPIO_InitStructure.Pull = GPIO_NOPULL;
288 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
289 GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
292 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7;
293 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
296 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5;
297 HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
301 GPIO_InitStructure.Pin = GPIO_PIN_2 | GPIO_PIN_11 | GPIO_PIN_13 | GPIO_PIN_14;
302 HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
305 #elif defined(USE_STM32F769I_DISCO)
306 GPIO_InitTypeDef GPIO_InitStructure;
309 __HAL_RCC_SYSCFG_CLK_ENABLE();
312 __HAL_RCC_GPIOA_CLK_ENABLE();
313 __HAL_RCC_GPIOC_CLK_ENABLE();
314 __HAL_RCC_GPIOD_CLK_ENABLE();
315 __HAL_RCC_GPIOG_CLK_ENABLE();
318 SYSCFG->PMC |= SYSCFG_PMC_MII_RMII_SEL;
321 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
322 GPIO_InitStructure.Pull = GPIO_NOPULL;
323 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
324 GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
327 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7;
328 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
331 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5;
332 HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
335 GPIO_InitStructure.Pin = GPIO_PIN_5;
336 HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
339 GPIO_InitStructure.Pin = GPIO_PIN_11 | GPIO_PIN_13 | GPIO_PIN_14;
340 HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
343 #elif defined(USE_STM32756G_EVAL) || defined(USE_STM32F769I_EVAL)
344 GPIO_InitTypeDef GPIO_InitStructure;
347 __HAL_RCC_SYSCFG_CLK_ENABLE();
350 __HAL_RCC_GPIOA_CLK_ENABLE();
351 __HAL_RCC_GPIOC_CLK_ENABLE();
352 __HAL_RCC_GPIOE_CLK_ENABLE();
353 __HAL_RCC_GPIOG_CLK_ENABLE();
354 __HAL_RCC_GPIOH_CLK_ENABLE();
355 __HAL_RCC_GPIOI_CLK_ENABLE();
358 GPIO_InitStructure.Pin = GPIO_PIN_8;
359 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
360 GPIO_InitStructure.Pull = GPIO_NOPULL;
361 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
362 GPIO_InitStructure.Alternate = GPIO_AF0_MCO;
363 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
366 HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_1);
369 SYSCFG->PMC &= ~SYSCFG_PMC_MII_RMII_SEL;
372 if(interface->smiDriver == NULL)
375 GPIO_InitStructure.Pin = GPIO_PIN_2;
376 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
377 GPIO_InitStructure.Pull = GPIO_PULLUP;
378 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_MEDIUM;
379 GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
380 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
383 GPIO_InitStructure.Pin = GPIO_PIN_1;
384 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
385 GPIO_InitStructure.Pull = GPIO_NOPULL;
386 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_MEDIUM;
387 GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
388 HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
392 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
393 GPIO_InitStructure.Pull = GPIO_NOPULL;
394 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
395 GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
402 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_7;
403 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
407 GPIO_InitStructure.Pin = GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5;
408 HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
411 GPIO_InitStructure.Pin = GPIO_PIN_2;
412 HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);
415 GPIO_InitStructure.Pin = GPIO_PIN_11 | GPIO_PIN_13 | GPIO_PIN_14;
416 HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
423 GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7;
424 HAL_GPIO_Init(GPIOH, &GPIO_InitStructure);
510 if(interface->phyDriver != NULL)
513 interface->phyDriver->tick(interface);
515 else if(interface->switchDriver != NULL)
518 interface->switchDriver->tick(interface);
535 NVIC_EnableIRQ(ETH_IRQn);
538 if(interface->phyDriver != NULL)
541 interface->phyDriver->enableIrq(interface);
543 else if(interface->switchDriver != NULL)
546 interface->switchDriver->enableIrq(interface);
563 NVIC_DisableIRQ(ETH_IRQn);
566 if(interface->phyDriver != NULL)
569 interface->phyDriver->disableIrq(interface);
571 else if(interface->switchDriver != NULL)
574 interface->switchDriver->disableIrq(interface);
602 if((status & ETH_DMASR_TS) != 0)
605 ETH->DMASR = ETH_DMASR_TS;
616 if((status & ETH_DMASR_RS) != 0)
619 ETH->DMASR = ETH_DMASR_RS;
622 nicDriverInterface->nicEvent =
TRUE;
628 ETH->DMASR = ETH_DMASR_NIS;
702 ETH->DMASR = ETH_DMASR_TBUS;
782 ETH->DMASR = ETH_DMASR_RBUS;
803 uint32_t hashTable[2];
811 if(interface->promiscuous)
814 ETH->MACFFR = ETH_MACFFR_PM;
819 ETH->MACA0LR = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
820 ETH->MACA0HR = interface->macAddr.w[2];
836 entry = &interface->macAddrFilter[i];
849 k = (crc >> 26) & 0x3F;
852 hashTable[k / 32] |= (1 << (k % 32));
860 unicastMacAddr[j++] = entry->
addr;
870 ETH->MACA1LR = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
871 ETH->MACA1HR = unicastMacAddr[0].w[2] | ETH_MACA1HR_AE;
884 ETH->MACA2LR = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
885 ETH->MACA2HR = unicastMacAddr[1].w[2] | ETH_MACA2HR_AE;
898 ETH->MACA3LR = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
899 ETH->MACA3HR = unicastMacAddr[2].w[2] | ETH_MACA3HR_AE;
910 if(interface->acceptAllMulticast)
913 ETH->MACFFR = ETH_MACFFR_HPF | ETH_MACFFR_PAM;
918 ETH->MACFFR = ETH_MACFFR_HPF | ETH_MACFFR_HM;
921 ETH->MACHTLR = hashTable[0];
922 ETH->MACHTHR = hashTable[1];
925 TRACE_DEBUG(
" MACHTLR = %08" PRIX32
"\r\n", ETH->MACHTLR);
926 TRACE_DEBUG(
" MACHTHR = %08" PRIX32
"\r\n", ETH->MACHTHR);
951 config |= ETH_MACCR_FES;
955 config &= ~ETH_MACCR_FES;
961 config |= ETH_MACCR_DM;
965 config &= ~ETH_MACCR_DM;
993 temp = ETH->MACMIIAR & ETH_MACMIIAR_CR;
995 temp |= ETH_MACMIIAR_MW | ETH_MACMIIAR_MB;
997 temp |= (phyAddr << 11) & ETH_MACMIIAR_PA;
999 temp |= (
regAddr << 6) & ETH_MACMIIAR_MR;
1002 ETH->MACMIIDR =
data & ETH_MACMIIDR_MD;
1005 ETH->MACMIIAR = temp;
1007 while((ETH->MACMIIAR & ETH_MACMIIAR_MB) != 0)
1036 temp = ETH->MACMIIAR & ETH_MACMIIAR_CR;
1038 temp |= ETH_MACMIIAR_MB;
1040 temp |= (phyAddr << 11) & ETH_MACMIIAR_PA;
1042 temp |= (
regAddr << 6) & ETH_MACMIIAR_MR;
1045 ETH->MACMIIAR = temp;
1047 while((ETH->MACMIIAR & ETH_MACMIIAR_MB) != 0)
1052 data = ETH->MACMIIDR & ETH_MACMIIDR_MD;
1080 p = (uint8_t *)
data;
1085 for(i = 0; i <
length; i++)
1088 for(j = 0; j < 8; j++)
1091 if((((crc >> 31) ^ (
p[i] >> j)) & 0x01) != 0)
1093 crc = (crc << 1) ^ 0x04C11DB7;