32 #define TRACE_LEVEL NIC_TRACE_LEVEL
35 #include "stm32h7xx.h"
36 #include "stm32h7xx_hal.h"
45 #if defined(__ICCARM__)
48 #pragma data_alignment = 4
49 #pragma location = STM32H7XX_ETH_RAM_SECTION
52 #pragma data_alignment = 4
53 #pragma location = STM32H7XX_ETH_RAM_SECTION
56 #pragma data_alignment = 4
57 #pragma location = STM32H7XX_ETH_RAM_SECTION
60 #pragma data_alignment = 4
61 #pragma location = STM32H7XX_ETH_RAM_SECTION
125 TRACE_INFO(
"Initializing STM32H7 Ethernet MAC...\r\n");
128 nicDriverInterface = interface;
134 __HAL_RCC_ETH1MAC_CLK_ENABLE();
135 __HAL_RCC_ETH1TX_CLK_ENABLE();
136 __HAL_RCC_ETH1RX_CLK_ENABLE();
139 __HAL_RCC_ETH1MAC_FORCE_RESET();
140 __HAL_RCC_ETH1MAC_RELEASE_RESET();
143 ETH->DMAMR |= ETH_DMAMR_SWR;
145 while((ETH->DMAMR & ETH_DMAMR_SWR) != 0)
150 ETH->MACMDIOAR = ETH_MACMDIOAR_CR_DIV124;
153 if(interface->phyDriver != NULL)
156 error = interface->phyDriver->init(interface);
158 else if(interface->switchDriver != NULL)
161 error = interface->switchDriver->init(interface);
179 temp = ETH->MACECR & ~ETH_MACECR_GPSL;
190 ETH->DMAMR = ETH_DMAMR_INTM_0 | ETH_DMAMR_PR_1_1;
192 ETH->DMASBMR |= ETH_DMASBMR_AAL;
194 ETH->DMACCR = ETH_DMACCR_DSL_0BIT;
197 ETH->DMACTCR = ETH_DMACTCR_TPBL_32PBL;
200 ETH->DMACRCR = ETH_DMACRCR_RPBL_32PBL;
204 ETH->MTLTQOMR |= ETH_MTLTQOMR_TSF;
205 ETH->MTLRQOMR |= ETH_MTLRQOMR_RSF;
223 ETH->DMACIER = ETH_DMACIER_NIE | ETH_DMACIER_RIE | ETH_DMACIER_TIE;
233 ETH->MACCR |= ETH_MACCR_TE | ETH_MACCR_RE;
236 ETH->DMACTCR |= ETH_DMACTCR_ST;
237 ETH->DMACRCR |= ETH_DMACRCR_SR;
256 #if defined(USE_NUCLEO_H723ZG) || defined(USE_NUCLEO_H743ZI) || \
257 defined(USE_NUCLEO_H743ZI2) || defined(USE_NUCLEO_H745ZI_Q)
258 GPIO_InitTypeDef GPIO_InitStructure;
261 __HAL_RCC_SYSCFG_CLK_ENABLE();
264 __HAL_RCC_GPIOA_CLK_ENABLE();
265 __HAL_RCC_GPIOB_CLK_ENABLE();
266 __HAL_RCC_GPIOC_CLK_ENABLE();
267 __HAL_RCC_GPIOG_CLK_ENABLE();
270 HAL_SYSCFG_ETHInterfaceSelect(SYSCFG_ETH_RMII);
273 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
274 GPIO_InitStructure.Pull = GPIO_NOPULL;
275 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
276 GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
279 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7;
280 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
283 GPIO_InitStructure.Pin = GPIO_PIN_13;
284 HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
287 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5;
288 HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
291 GPIO_InitStructure.Pin = GPIO_PIN_11 | GPIO_PIN_13;
292 HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
295 #elif defined(USE_STM32H735G_DK)
296 GPIO_InitTypeDef GPIO_InitStructure;
299 __HAL_RCC_SYSCFG_CLK_ENABLE();
302 __HAL_RCC_GPIOA_CLK_ENABLE();
303 __HAL_RCC_GPIOB_CLK_ENABLE();
304 __HAL_RCC_GPIOC_CLK_ENABLE();
307 HAL_SYSCFG_ETHInterfaceSelect(SYSCFG_ETH_RMII);
310 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
311 GPIO_InitStructure.Pull = GPIO_NOPULL;
312 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
313 GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
316 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7;
317 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
321 GPIO_InitStructure.Pin = GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
322 HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
325 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5;
326 HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
329 #elif defined(USE_STM32H745I_DISCO) || defined(USE_STM32H750B_DISCO)
330 GPIO_InitTypeDef GPIO_InitStructure;
333 __HAL_RCC_SYSCFG_CLK_ENABLE();
336 __HAL_RCC_GPIOA_CLK_ENABLE();
337 __HAL_RCC_GPIOB_CLK_ENABLE();
338 __HAL_RCC_GPIOC_CLK_ENABLE();
339 __HAL_RCC_GPIOE_CLK_ENABLE();
340 __HAL_RCC_GPIOG_CLK_ENABLE();
341 __HAL_RCC_GPIOH_CLK_ENABLE();
342 __HAL_RCC_GPIOI_CLK_ENABLE();
345 HAL_SYSCFG_ETHInterfaceSelect(SYSCFG_ETH_MII);
348 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
349 GPIO_InitStructure.Pull = GPIO_NOPULL;
350 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
351 GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
354 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7;
355 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
358 GPIO_InitStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2;
359 HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
363 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 |
365 HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
368 GPIO_InitStructure.Pin = GPIO_PIN_2;
369 HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);
372 GPIO_InitStructure.Pin = GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
373 HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
380 GPIO_InitStructure.Pin = GPIO_PIN_10;
381 HAL_GPIO_Init(GPIOI, &GPIO_InitStructure);
384 #elif defined(USE_STM32H743I_EVAL) || defined(USE_STM32H747I_EVAL) || \
385 defined(USE_STM32H747I_DISCO)
386 GPIO_InitTypeDef GPIO_InitStructure;
389 __HAL_RCC_SYSCFG_CLK_ENABLE();
392 __HAL_RCC_GPIOA_CLK_ENABLE();
393 __HAL_RCC_GPIOC_CLK_ENABLE();
394 __HAL_RCC_GPIOG_CLK_ENABLE();
397 HAL_SYSCFG_ETHInterfaceSelect(SYSCFG_ETH_RMII);
400 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
401 GPIO_InitStructure.Pull = GPIO_NOPULL;
402 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
403 GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
406 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7;
407 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
410 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5;
411 HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
414 GPIO_InitStructure.Pin = GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
415 HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
456 ETH->DMACTDLAR = (uint32_t) &
txDmaDesc[0];
461 ETH->DMACRDLAR = (uint32_t) &
rxDmaDesc[0];
479 if(interface->phyDriver != NULL)
482 interface->phyDriver->tick(interface);
484 else if(interface->switchDriver != NULL)
487 interface->switchDriver->tick(interface);
504 NVIC_EnableIRQ(ETH_IRQn);
507 if(interface->phyDriver != NULL)
510 interface->phyDriver->enableIrq(interface);
512 else if(interface->switchDriver != NULL)
515 interface->switchDriver->enableIrq(interface);
532 NVIC_DisableIRQ(ETH_IRQn);
535 if(interface->phyDriver != NULL)
538 interface->phyDriver->disableIrq(interface);
540 else if(interface->switchDriver != NULL)
543 interface->switchDriver->disableIrq(interface);
568 status = ETH->DMACSR;
571 if((status & ETH_DMACSR_TI) != 0)
574 ETH->DMACSR = ETH_DMACSR_TI;
585 if((status & ETH_DMACSR_RI) != 0)
588 ETH->DMACSR = ETH_DMACSR_RI;
591 nicDriverInterface->nicEvent =
TRUE;
597 ETH->DMACSR = ETH_DMACSR_NIS;
671 ETH->DMACSR = ETH_DMACSR_TBU;
718 if((SYSCFG->PMCR & SYSCFG_PMCR_EPIS_SEL) != SYSCFG_ETH_MII)
770 ETH->DMACSR = ETH_DMACSR_RBU;
791 uint32_t hashTable[2];
799 if(interface->promiscuous)
802 ETH->MACPFR = ETH_MACPFR_PR;
807 ETH->MACA0LR = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
808 ETH->MACA0HR = interface->macAddr.w[2];
824 entry = &interface->macAddrFilter[i];
837 k = (crc >> 26) & 0x3F;
840 hashTable[k / 32] |= (1 << (k % 32));
848 unicastMacAddr[j++] = entry->
addr;
858 ETH->MACA1LR = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
859 ETH->MACA1HR = unicastMacAddr[0].w[2] | ETH_MACAHR_AE;
872 ETH->MACA2LR = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
873 ETH->MACA2HR = unicastMacAddr[1].w[2] | ETH_MACAHR_AE;
886 ETH->MACA3LR = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
887 ETH->MACA3HR = unicastMacAddr[2].w[2] | ETH_MACAHR_AE;
898 if(interface->acceptAllMulticast)
901 ETH->MACPFR = ETH_MACPFR_HPF | ETH_MACPFR_PM;
906 ETH->MACPFR = ETH_MACPFR_HPF | ETH_MACPFR_HMC;
909 ETH->MACHT0R = hashTable[0];
910 ETH->MACHT1R = hashTable[1];
913 TRACE_DEBUG(
" MACHT0R = %08" PRIX32
"\r\n", ETH->MACHT0R);
914 TRACE_DEBUG(
" MACHT1R = %08" PRIX32
"\r\n", ETH->MACHT1R);
939 config |= ETH_MACCR_FES;
943 config &= ~ETH_MACCR_FES;
949 config |= ETH_MACCR_DM;
953 config &= ~ETH_MACCR_DM;
981 temp = ETH->MACMDIOAR & ETH_MACMDIOAR_CR;
983 temp |= ETH_MACMDIOAR_MOC_WR | ETH_MACMDIOAR_MB;
985 temp |= (phyAddr << 21) & ETH_MACMDIOAR_PA;
987 temp |= (
regAddr << 16) & ETH_MACMDIOAR_RDA;
990 ETH->MACMDIODR =
data & ETH_MACMDIODR_MD;
993 ETH->MACMDIOAR = temp;
995 while((ETH->MACMDIOAR & ETH_MACMDIOAR_MB) != 0)
1024 temp = ETH->MACMDIOAR & ETH_MACMDIOAR_CR;
1026 temp |= ETH_MACMDIOAR_MOC_RD | ETH_MACMDIOAR_MB;
1028 temp |= (phyAddr << 21) & ETH_MACMDIOAR_PA;
1030 temp |= (
regAddr << 16) & ETH_MACMDIOAR_RDA;
1033 ETH->MACMDIOAR = temp;
1035 while((ETH->MACMDIOAR & ETH_MACMDIOAR_MB) != 0)
1040 data = ETH->MACMDIODR & ETH_MACMDIODR_MD;
1068 p = (uint8_t *)
data;
1073 for(i = 0; i <
length; i++)
1076 for(j = 0; j < 8; j++)
1079 if((((crc >> 31) ^ (
p[i] >> j)) & 0x01) != 0)
1081 crc = (crc << 1) ^ 0x04C11DB7;