32 #define TRACE_LEVEL NIC_TRACE_LEVEL
35 #include "stm32f2xx.h"
37 #ifndef USE_STDPERIPH_DRIVER
38 #include "stm32f2xx_hal.h"
49 #if defined(__ICCARM__)
52 #pragma data_alignment = 4
55 #pragma data_alignment = 4
58 #pragma data_alignment = 4
61 #pragma data_alignment = 4
124 TRACE_INFO(
"Initializing STM32F2 Ethernet MAC...\r\n");
127 nicDriverInterface = interface;
132 #ifdef USE_STDPERIPH_DRIVER
134 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_ETH_MAC |
135 RCC_AHB1Periph_ETH_MAC_Tx | RCC_AHB1Periph_ETH_MAC_Rx, ENABLE);
138 RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_ETH_MAC, ENABLE);
139 RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_ETH_MAC, DISABLE);
142 __HAL_RCC_ETHMAC_CLK_ENABLE();
143 __HAL_RCC_ETHMACTX_CLK_ENABLE();
144 __HAL_RCC_ETHMACRX_CLK_ENABLE();
147 __HAL_RCC_ETHMAC_FORCE_RESET();
148 __HAL_RCC_ETHMAC_RELEASE_RESET();
152 ETH->DMABMR |= ETH_DMABMR_SR;
154 while((ETH->DMABMR & ETH_DMABMR_SR) != 0)
159 ETH->MACMIIAR = ETH_MACMIIAR_CR_Div62;
162 if(interface->phyDriver != NULL)
165 error = interface->phyDriver->init(interface);
167 else if(interface->switchDriver != NULL)
170 error = interface->switchDriver->init(interface);
193 ETH->DMAOMR = ETH_DMAOMR_RSF | ETH_DMAOMR_TSF;
196 ETH->DMABMR = ETH_DMABMR_AAB | ETH_DMABMR_USP | ETH_DMABMR_RDP_32Beat |
197 ETH_DMABMR_RTPR_1_1 | ETH_DMABMR_PBL_32Beat | ETH_DMABMR_EDE;
204 ETH->MMCTIMR = ETH_MMCTIMR_TGFM | ETH_MMCTIMR_TGFMSCM | ETH_MMCTIMR_TGFSCM;
208 ETH->MMCRIMR = ETH_MMCRIMR_RGUFM | ETH_MMCRIMR_RFAEM | ETH_MMCRIMR_RFCEM;
211 ETH->MACIMR = ETH_MACIMR_TSTIM | ETH_MACIMR_PMTIM;
213 ETH->DMAIER = ETH_DMAIER_NISE | ETH_DMAIER_RIE | ETH_DMAIER_TIE;
223 ETH->MACCR |= ETH_MACCR_TE | ETH_MACCR_RE;
225 ETH->DMAOMR |= ETH_DMAOMR_ST | ETH_DMAOMR_SR;
243 #if defined(USE_STM32F2XX_NUCLEO_144)
244 GPIO_InitTypeDef GPIO_InitStructure;
247 __HAL_RCC_SYSCFG_CLK_ENABLE();
250 __HAL_RCC_GPIOA_CLK_ENABLE();
251 __HAL_RCC_GPIOB_CLK_ENABLE();
252 __HAL_RCC_GPIOC_CLK_ENABLE();
253 __HAL_RCC_GPIOG_CLK_ENABLE();
256 SYSCFG->PMC |= SYSCFG_PMC_MII_RMII_SEL;
259 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
260 GPIO_InitStructure.Pull = GPIO_NOPULL;
261 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
262 GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
265 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7;
266 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
269 GPIO_InitStructure.Pin = GPIO_PIN_13;
270 HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
273 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5;
274 HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
277 GPIO_InitStructure.Pin = GPIO_PIN_11 | GPIO_PIN_13;
278 HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
281 #elif defined(USE_STM322xG_EVAL)
282 GPIO_InitTypeDef GPIO_InitStructure;
285 __HAL_RCC_SYSCFG_CLK_ENABLE();
288 __HAL_RCC_GPIOA_CLK_ENABLE();
289 __HAL_RCC_GPIOB_CLK_ENABLE();
290 __HAL_RCC_GPIOC_CLK_ENABLE();
291 __HAL_RCC_GPIOG_CLK_ENABLE();
292 __HAL_RCC_GPIOH_CLK_ENABLE();
293 __HAL_RCC_GPIOI_CLK_ENABLE();
296 GPIO_InitStructure.Pin = GPIO_PIN_8;
297 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
298 GPIO_InitStructure.Pull = GPIO_NOPULL;
299 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
300 GPIO_InitStructure.Alternate = GPIO_AF0_MCO;
301 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
304 HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_1);
307 SYSCFG->PMC &= ~SYSCFG_PMC_MII_RMII_SEL;
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);
320 GPIO_InitStructure.Pin = GPIO_PIN_8;
321 HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
325 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5;
326 HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
329 GPIO_InitStructure.Pin = GPIO_PIN_11 | GPIO_PIN_13 | GPIO_PIN_14;
330 HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
333 GPIO_InitStructure.Pin = GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_6 | GPIO_PIN_7;
334 HAL_GPIO_Init(GPIOH, &GPIO_InitStructure);
337 GPIO_InitStructure.Pin = GPIO_PIN_10;
338 HAL_GPIO_Init(GPIOI, &GPIO_InitStructure);
341 #elif defined(USE_MCBSTM32F200)
342 GPIO_InitTypeDef GPIO_InitStructure;
345 __HAL_RCC_SYSCFG_CLK_ENABLE();
348 __HAL_RCC_GPIOA_CLK_ENABLE();
349 __HAL_RCC_GPIOC_CLK_ENABLE();
350 __HAL_RCC_GPIOG_CLK_ENABLE();
353 SYSCFG->PMC |= SYSCFG_PMC_MII_RMII_SEL;
356 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
357 GPIO_InitStructure.Pull = GPIO_NOPULL;
358 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
359 GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
362 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7;
363 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
366 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5;
367 HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
370 GPIO_InitStructure.Pin = GPIO_PIN_11 | GPIO_PIN_13 | GPIO_PIN_14;
371 HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
453 if(interface->phyDriver != NULL)
456 interface->phyDriver->tick(interface);
458 else if(interface->switchDriver != NULL)
461 interface->switchDriver->tick(interface);
478 NVIC_EnableIRQ(ETH_IRQn);
481 if(interface->phyDriver != NULL)
484 interface->phyDriver->enableIrq(interface);
486 else if(interface->switchDriver != NULL)
489 interface->switchDriver->enableIrq(interface);
506 NVIC_DisableIRQ(ETH_IRQn);
509 if(interface->phyDriver != NULL)
512 interface->phyDriver->disableIrq(interface);
514 else if(interface->switchDriver != NULL)
517 interface->switchDriver->disableIrq(interface);
545 if((status & ETH_DMASR_TS) != 0)
548 ETH->DMASR = ETH_DMASR_TS;
559 if((status & ETH_DMASR_RS) != 0)
562 ETH->DMASR = ETH_DMASR_RS;
565 nicDriverInterface->nicEvent =
TRUE;
571 ETH->DMASR = ETH_DMASR_NIS;
642 ETH->DMASR = ETH_DMASR_TBUS;
722 ETH->DMASR = ETH_DMASR_RBUS;
743 uint32_t hashTable[2];
751 if(interface->promiscuous)
754 ETH->MACFFR = ETH_MACFFR_PM;
759 ETH->MACA0LR = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
760 ETH->MACA0HR = interface->macAddr.w[2];
776 entry = &interface->macAddrFilter[i];
789 k = (crc >> 26) & 0x3F;
792 hashTable[k / 32] |= (1 << (k % 32));
800 unicastMacAddr[j++] = entry->
addr;
810 ETH->MACA1LR = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
811 ETH->MACA1HR = unicastMacAddr[0].w[2] | ETH_MACA1HR_AE;
824 ETH->MACA2LR = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
825 ETH->MACA2HR = unicastMacAddr[1].w[2] | ETH_MACA2HR_AE;
838 ETH->MACA3LR = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
839 ETH->MACA3HR = unicastMacAddr[2].w[2] | ETH_MACA3HR_AE;
850 if(interface->acceptAllMulticast)
853 ETH->MACFFR = ETH_MACFFR_HPF | ETH_MACFFR_PAM;
858 ETH->MACFFR = ETH_MACFFR_HPF | ETH_MACFFR_HM;
861 ETH->MACHTLR = hashTable[0];
862 ETH->MACHTHR = hashTable[1];
865 TRACE_DEBUG(
" MACHTLR = %08" PRIX32
"\r\n", ETH->MACHTLR);
866 TRACE_DEBUG(
" MACHTHR = %08" PRIX32
"\r\n", ETH->MACHTHR);
891 config |= ETH_MACCR_FES;
895 config &= ~ETH_MACCR_FES;
901 config |= ETH_MACCR_DM;
905 config &= ~ETH_MACCR_DM;
933 temp = ETH->MACMIIAR & ETH_MACMIIAR_CR;
935 temp |= ETH_MACMIIAR_MW | ETH_MACMIIAR_MB;
937 temp |= (phyAddr << 11) & ETH_MACMIIAR_PA;
939 temp |= (
regAddr << 6) & ETH_MACMIIAR_MR;
942 ETH->MACMIIDR =
data & ETH_MACMIIDR_MD;
945 ETH->MACMIIAR = temp;
947 while((ETH->MACMIIAR & ETH_MACMIIAR_MB) != 0)
976 temp = ETH->MACMIIAR & ETH_MACMIIAR_CR;
978 temp |= ETH_MACMIIAR_MB;
980 temp |= (phyAddr << 11) & ETH_MACMIIAR_PA;
982 temp |= (
regAddr << 6) & ETH_MACMIIAR_MR;
985 ETH->MACMIIAR = temp;
987 while((ETH->MACMIIAR & ETH_MACMIIAR_MB) != 0)
992 data = ETH->MACMIIDR & ETH_MACMIIDR_MD;
1020 p = (uint8_t *)
data;
1025 for(i = 0; i <
length; i++)
1028 for(j = 0; j < 8; j++)
1031 if((((crc >> 31) ^ (
p[i] >> j)) & 0x01) != 0)
1033 crc = (crc << 1) ^ 0x04C11DB7;