32 #define TRACE_LEVEL NIC_TRACE_LEVEL
35 #include "stm32h7rsxx.h"
36 #include "stm32h7rsxx_hal.h"
45 #if defined(__ICCARM__)
48 #pragma data_alignment = 4
49 #pragma location = STM32H7RSXX_ETH_RAM_SECTION
52 #pragma data_alignment = 4
53 #pragma location = STM32H7RSXX_ETH_RAM_SECTION
56 #pragma data_alignment = 4
57 #pragma location = STM32H7RSXX_ETH_RAM_SECTION
60 #pragma data_alignment = 4
61 #pragma location = STM32H7RSXX_ETH_RAM_SECTION
125 TRACE_INFO(
"Initializing STM32H7Rx/Sx 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_ETH1_FORCE_RESET();
140 __HAL_RCC_ETH1_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;
255 #if defined(USE_STM32H7RSxx_NUCLEO)
256 GPIO_InitTypeDef GPIO_InitStructure;
259 __HAL_RCC_SBS_CLK_ENABLE();
262 __HAL_RCC_GPIOA_CLK_ENABLE();
263 __HAL_RCC_GPIOB_CLK_ENABLE();
264 __HAL_RCC_GPIOG_CLK_ENABLE();
267 HAL_SBS_ConfigEthernetPHY(SBS_ETHERNET_PHY_RMII);
270 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
271 GPIO_InitStructure.Pull = GPIO_NOPULL;
272 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
273 GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
276 GPIO_InitStructure.Pin = GPIO_PIN_2 | GPIO_PIN_7;
277 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
280 GPIO_InitStructure.Pin = GPIO_PIN_6;
281 HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
285 GPIO_InitStructure.Pin = GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 |
286 GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
287 HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
290 #elif defined(USE_STM32H7S78_DK)
291 GPIO_InitTypeDef GPIO_InitStructure;
294 __HAL_RCC_SBS_CLK_ENABLE();
297 __HAL_RCC_GPIOA_CLK_ENABLE();
298 __HAL_RCC_GPIOB_CLK_ENABLE();
299 __HAL_RCC_GPIOC_CLK_ENABLE();
300 __HAL_RCC_GPIOD_CLK_ENABLE();
301 __HAL_RCC_GPIOG_CLK_ENABLE();
304 HAL_SBS_ConfigEthernetPHY(SBS_ETHERNET_PHY_RMII);
307 if(interface->smiDriver == NULL)
310 GPIO_InitStructure.Pin = GPIO_PIN_2;
311 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
312 GPIO_InitStructure.Pull = GPIO_PULLUP;
313 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_MEDIUM;
314 GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
315 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
318 GPIO_InitStructure.Pin = GPIO_PIN_1;
319 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
320 GPIO_InitStructure.Pull = GPIO_NOPULL;
321 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_MEDIUM;
322 GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
323 HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
327 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
328 GPIO_InitStructure.Pull = GPIO_NOPULL;
329 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
330 GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
333 GPIO_InitStructure.Pin = GPIO_PIN_7;
334 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
337 GPIO_InitStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_10;
338 HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
341 GPIO_InitStructure.Pin = GPIO_PIN_4 | GPIO_PIN_5;
342 HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
345 GPIO_InitStructure.Pin = GPIO_PIN_11;
346 HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
349 GPIO_InitStructure.Pin = GPIO_PIN_7;
350 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
351 GPIO_InitStructure.Pull = GPIO_NOPULL;
352 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
353 GPIO_InitStructure.Alternate = GPIO_AF4_ETH;
354 HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
395 ETH->DMACTDLAR = (uint32_t) &
txDmaDesc[0];
400 ETH->DMACRDLAR = (uint32_t) &
rxDmaDesc[0];
418 if(interface->phyDriver != NULL)
421 interface->phyDriver->tick(interface);
423 else if(interface->switchDriver != NULL)
426 interface->switchDriver->tick(interface);
443 NVIC_EnableIRQ(ETH_IRQn);
446 if(interface->phyDriver != NULL)
449 interface->phyDriver->enableIrq(interface);
451 else if(interface->switchDriver != NULL)
454 interface->switchDriver->enableIrq(interface);
471 NVIC_DisableIRQ(ETH_IRQn);
474 if(interface->phyDriver != NULL)
477 interface->phyDriver->disableIrq(interface);
479 else if(interface->switchDriver != NULL)
482 interface->switchDriver->disableIrq(interface);
507 status = ETH->DMACSR;
510 if((status & ETH_DMACSR_TI) != 0)
513 ETH->DMACSR = ETH_DMACSR_TI;
524 if((status & ETH_DMACSR_RI) != 0)
527 ETH->DMACSR = ETH_DMACSR_RI;
530 nicDriverInterface->nicEvent =
TRUE;
536 ETH->DMACSR = ETH_DMACSR_NIS;
610 ETH->DMACSR = ETH_DMACSR_TBU;
657 if((SBS->PMCR & SBS_PMCR_ETH_PHYSEL) != SBS_ETHERNET_PHY_GMII_OR_MII)
709 ETH->DMACSR = ETH_DMACSR_RBU;
730 uint32_t hashTable[2];
738 if(interface->promiscuous)
741 ETH->MACPFR = ETH_MACPFR_PR;
746 ETH->MACA0LR = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
747 ETH->MACA0HR = interface->macAddr.w[2];
763 entry = &interface->macAddrFilter[i];
776 k = (crc >> 26) & 0x3F;
779 hashTable[k / 32] |= (1 << (k % 32));
787 unicastMacAddr[j++] = entry->
addr;
797 ETH->MACA1LR = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
798 ETH->MACA1HR = unicastMacAddr[0].w[2] | ETH_MACAHR_AE;
811 ETH->MACA2LR = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
812 ETH->MACA2HR = unicastMacAddr[1].w[2] | ETH_MACAHR_AE;
825 ETH->MACA3LR = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
826 ETH->MACA3HR = unicastMacAddr[2].w[2] | ETH_MACAHR_AE;
837 if(interface->acceptAllMulticast)
840 ETH->MACPFR = ETH_MACPFR_HPF | ETH_MACPFR_PM;
845 ETH->MACPFR = ETH_MACPFR_HPF | ETH_MACPFR_HMC;
848 ETH->MACHT0R = hashTable[0];
849 ETH->MACHT1R = hashTable[1];
852 TRACE_DEBUG(
" MACHT0R = %08" PRIX32
"\r\n", ETH->MACHT0R);
853 TRACE_DEBUG(
" MACHT1R = %08" PRIX32
"\r\n", ETH->MACHT1R);
878 config |= ETH_MACCR_FES;
882 config &= ~ETH_MACCR_FES;
888 config |= ETH_MACCR_DM;
892 config &= ~ETH_MACCR_DM;
920 temp = ETH->MACMDIOAR & ETH_MACMDIOAR_CR;
922 temp |= ETH_MACMDIOAR_MOC_WR | ETH_MACMDIOAR_MB;
924 temp |= (phyAddr << 21) & ETH_MACMDIOAR_PA;
926 temp |= (
regAddr << 16) & ETH_MACMDIOAR_RDA;
929 ETH->MACMDIODR =
data & ETH_MACMDIODR_MD;
932 ETH->MACMDIOAR = temp;
934 while((ETH->MACMDIOAR & ETH_MACMDIOAR_MB) != 0)
963 temp = ETH->MACMDIOAR & ETH_MACMDIOAR_CR;
965 temp |= ETH_MACMDIOAR_MOC_RD | ETH_MACMDIOAR_MB;
967 temp |= (phyAddr << 21) & ETH_MACMDIOAR_PA;
969 temp |= (
regAddr << 16) & ETH_MACMDIOAR_RDA;
972 ETH->MACMDIOAR = temp;
974 while((ETH->MACMDIOAR & ETH_MACMDIOAR_MB) != 0)
979 data = ETH->MACMDIODR & ETH_MACMDIODR_MD;
1007 p = (uint8_t *)
data;
1012 for(i = 0; i <
length; i++)
1015 for(j = 0; j < 8; j++)
1018 if((((crc >> 31) ^ (
p[i] >> j)) & 0x01) != 0)
1020 crc = (crc << 1) ^ 0x04C11DB7;