32 #define TRACE_LEVEL NIC_TRACE_LEVEL
35 #ifdef USE_STDPERIPH_DRIVER
36 #include "stm32f10x.h"
38 #include "stm32f1xx.h"
39 #include "stm32f1xx_hal.h"
50 #if defined(__ICCARM__)
53 #pragma data_alignment = 4
56 #pragma data_alignment = 4
59 #pragma data_alignment = 4
62 #pragma data_alignment = 4
125 TRACE_INFO(
"Initializing STM32F1 Ethernet MAC...\r\n");
128 nicDriverInterface = interface;
133 #ifdef USE_STDPERIPH_DRIVER
135 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ETH_MAC |
136 RCC_AHBPeriph_ETH_MAC_Tx | RCC_AHBPeriph_ETH_MAC_Rx, ENABLE);
139 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_ETH_MAC, ENABLE);
140 RCC_AHBPeriphResetCmd(RCC_AHBPeriph_ETH_MAC, DISABLE);
143 __HAL_RCC_ETHMAC_CLK_ENABLE();
144 __HAL_RCC_ETHMACTX_CLK_ENABLE();
145 __HAL_RCC_ETHMACRX_CLK_ENABLE();
148 __HAL_RCC_ETHMAC_FORCE_RESET();
149 __HAL_RCC_ETHMAC_RELEASE_RESET();
153 ETH->DMABMR |= ETH_DMABMR_SR;
155 while((ETH->DMABMR & ETH_DMABMR_SR) != 0)
160 ETH->MACMIIAR = ETH_MACMIIAR_CR_DIV42;
163 if(interface->phyDriver != NULL)
166 error = interface->phyDriver->init(interface);
168 else if(interface->switchDriver != NULL)
171 error = interface->switchDriver->init(interface);
194 ETH->DMAOMR = ETH_DMAOMR_RSF | ETH_DMAOMR_TSF;
197 ETH->DMABMR = ETH_DMABMR_AAB | ETH_DMABMR_USP | ETH_DMABMR_RDP_32Beat |
198 ETH_DMABMR_RTPR_1_1 | ETH_DMABMR_PBL_32Beat;
205 ETH->MMCTIMR = ETH_MMCTIMR_TGFM | ETH_MMCTIMR_TGFMSCM | ETH_MMCTIMR_TGFSCM;
209 ETH->MMCRIMR = ETH_MMCRIMR_RGUFM | ETH_MMCRIMR_RFAEM | ETH_MMCRIMR_RFCEM;
212 ETH->MACIMR = ETH_MACIMR_TSTIM | ETH_MACIMR_PMTIM;
214 ETH->DMAIER = ETH_DMAIER_NISE | ETH_DMAIER_RIE | ETH_DMAIER_TIE;
224 ETH->MACCR |= ETH_MACCR_TE | ETH_MACCR_RE;
226 ETH->DMAOMR |= ETH_DMAOMR_ST | ETH_DMAOMR_SR;
244 #if defined(USE_STM3210C_EVAL)
245 GPIO_InitTypeDef GPIO_InitStructure;
248 __HAL_RCC_AFIO_CLK_ENABLE();
251 __HAL_RCC_GPIOA_CLK_ENABLE();
252 __HAL_RCC_GPIOB_CLK_ENABLE();
253 __HAL_RCC_GPIOC_CLK_ENABLE();
254 __HAL_RCC_GPIOD_CLK_ENABLE();
257 GPIO_InitStructure.Pin = GPIO_PIN_8;
258 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
259 GPIO_InitStructure.Pull = GPIO_NOPULL;
260 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
261 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
264 HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, 1);
267 __HAL_AFIO_ETH_MII();
270 GPIO_InitStructure.Pin = GPIO_PIN_2;
271 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
272 GPIO_InitStructure.Pull = GPIO_NOPULL;
273 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
274 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
278 GPIO_InitStructure.Pin = GPIO_PIN_8 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
279 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
280 GPIO_InitStructure.Pull = GPIO_NOPULL;
281 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
282 HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
285 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2;
286 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
287 GPIO_InitStructure.Pull = GPIO_NOPULL;
288 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
289 HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
292 GPIO_InitStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_3;
293 GPIO_InitStructure.Mode = GPIO_MODE_AF_INPUT;
294 GPIO_InitStructure.Pull = GPIO_NOPULL;
295 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
296 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
299 GPIO_InitStructure.Pin = GPIO_PIN_10;
300 GPIO_InitStructure.Mode = GPIO_MODE_AF_INPUT;
301 GPIO_InitStructure.Pull = GPIO_NOPULL;
302 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
303 HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
306 GPIO_InitStructure.Pin = GPIO_PIN_3;
307 GPIO_InitStructure.Mode = GPIO_MODE_AF_INPUT;
308 GPIO_InitStructure.Pull = GPIO_NOPULL;
309 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
310 HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
314 GPIO_InitStructure.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
315 GPIO_InitStructure.Mode = GPIO_MODE_AF_INPUT;
316 GPIO_InitStructure.Pull = GPIO_NOPULL;
317 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
318 HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
321 __HAL_AFIO_REMAP_ETH_ENABLE();
324 #elif defined(USE_STM32_P107)
326 GPIO_InitTypeDef GPIO_InitStructure;
329 __HAL_RCC_AFIO_CLK_ENABLE();
332 __HAL_RCC_GPIOA_CLK_ENABLE();
333 __HAL_RCC_GPIOB_CLK_ENABLE();
334 __HAL_RCC_GPIOC_CLK_ENABLE();
335 __HAL_RCC_GPIOD_CLK_ENABLE();
338 GPIO_InitStructure.Pin = GPIO_PIN_8;
339 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
340 GPIO_InitStructure.Pull = GPIO_NOPULL;
341 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
342 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
345 temp = RCC->CFGR2 & ~RCC_CFGR2_PLL3MUL;
346 RCC->CFGR2 = temp | RCC_CFGR2_PLL3MUL10;
349 RCC->CR |= RCC_CR_PLL3ON;
352 while((RCC->CR & RCC_CR_PLL3RDY) == 0)
357 HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_PLL3CLK, 1);
360 __HAL_AFIO_ETH_RMII();
363 GPIO_InitStructure.Pin = GPIO_PIN_2;
364 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
365 GPIO_InitStructure.Pull = GPIO_NOPULL;
366 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
367 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
370 GPIO_InitStructure.Pin = GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
371 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
372 GPIO_InitStructure.Pull = GPIO_NOPULL;
373 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
374 HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
377 GPIO_InitStructure.Pin = GPIO_PIN_1;
378 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
379 GPIO_InitStructure.Pull = GPIO_NOPULL;
380 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
381 HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
384 GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_7;
385 GPIO_InitStructure.Mode = GPIO_MODE_AF_INPUT;
386 GPIO_InitStructure.Pull = GPIO_NOPULL;
387 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
388 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
391 GPIO_InitStructure.Pin = GPIO_PIN_4 | GPIO_PIN_5;
392 GPIO_InitStructure.Mode = GPIO_MODE_AF_INPUT;
393 GPIO_InitStructure.Pull = GPIO_NOPULL;
394 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
395 HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
398 __HAL_AFIO_REMAP_ETH_DISABLE();
467 if(interface->phyDriver != NULL)
470 interface->phyDriver->tick(interface);
472 else if(interface->switchDriver != NULL)
475 interface->switchDriver->tick(interface);
492 NVIC_EnableIRQ(ETH_IRQn);
495 if(interface->phyDriver != NULL)
498 interface->phyDriver->enableIrq(interface);
500 else if(interface->switchDriver != NULL)
503 interface->switchDriver->enableIrq(interface);
520 NVIC_DisableIRQ(ETH_IRQn);
523 if(interface->phyDriver != NULL)
526 interface->phyDriver->disableIrq(interface);
528 else if(interface->switchDriver != NULL)
531 interface->switchDriver->disableIrq(interface);
559 if((status & ETH_DMASR_TS) != 0)
562 ETH->DMASR = ETH_DMASR_TS;
573 if((status & ETH_DMASR_RS) != 0)
576 ETH->DMASR = ETH_DMASR_RS;
579 nicDriverInterface->nicEvent =
TRUE;
585 ETH->DMASR = ETH_DMASR_NIS;
656 ETH->DMASR = ETH_DMASR_TBUS;
736 ETH->DMASR = ETH_DMASR_RBUS;
757 uint32_t hashTable[2];
765 if(interface->promiscuous)
768 ETH->MACFFR = ETH_MACFFR_PM;
773 ETH->MACA0LR = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
774 ETH->MACA0HR = interface->macAddr.w[2];
790 entry = &interface->macAddrFilter[i];
803 k = (crc >> 26) & 0x3F;
806 hashTable[k / 32] |= (1 << (k % 32));
814 unicastMacAddr[j++] = entry->
addr;
824 ETH->MACA1LR = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
825 ETH->MACA1HR = unicastMacAddr[0].w[2] | ETH_MACA1HR_AE;
838 ETH->MACA2LR = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
839 ETH->MACA2HR = unicastMacAddr[1].w[2] | ETH_MACA2HR_AE;
852 ETH->MACA3LR = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
853 ETH->MACA3HR = unicastMacAddr[2].w[2] | ETH_MACA3HR_AE;
864 if(interface->acceptAllMulticast)
867 ETH->MACFFR = ETH_MACFFR_HPF | ETH_MACFFR_PAM;
872 ETH->MACFFR = ETH_MACFFR_HPF | ETH_MACFFR_HM;
875 ETH->MACHTLR = hashTable[0];
876 ETH->MACHTHR = hashTable[1];
879 TRACE_DEBUG(
" MACHTLR = %08" PRIX32
"\r\n", ETH->MACHTLR);
880 TRACE_DEBUG(
" MACHTHR = %08" PRIX32
"\r\n", ETH->MACHTHR);
905 config |= ETH_MACCR_FES;
909 config &= ~ETH_MACCR_FES;
915 config |= ETH_MACCR_DM;
919 config &= ~ETH_MACCR_DM;
947 temp = ETH->MACMIIAR & ETH_MACMIIAR_CR;
949 temp |= ETH_MACMIIAR_MW | ETH_MACMIIAR_MB;
951 temp |= (phyAddr << 11) & ETH_MACMIIAR_PA;
953 temp |= (
regAddr << 6) & ETH_MACMIIAR_MR;
956 ETH->MACMIIDR =
data & ETH_MACMIIDR_MD;
959 ETH->MACMIIAR = temp;
961 while((ETH->MACMIIAR & ETH_MACMIIAR_MB) != 0)
990 temp = ETH->MACMIIAR & ETH_MACMIIAR_CR;
992 temp |= ETH_MACMIIAR_MB;
994 temp |= (phyAddr << 11) & ETH_MACMIIAR_PA;
996 temp |= (
regAddr << 6) & ETH_MACMIIAR_MR;
999 ETH->MACMIIAR = temp;
1001 while((ETH->MACMIIAR & ETH_MACMIIAR_MB) != 0)
1006 data = ETH->MACMIIDR & ETH_MACMIIDR_MD;
1034 p = (uint8_t *)
data;
1039 for(i = 0; i <
length; i++)
1042 for(j = 0; j < 8; j++)
1045 if((((crc >> 31) ^ (
p[i] >> j)) & 0x01) != 0)
1047 crc = (crc << 1) ^ 0x04C11DB7;