static void low_level_init(struct netif *netif)

{

uint32_t regvalue = 0;

HAL_StatusTypeDef hal_eth_init_status;

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_RESET);

osDelay(50);

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_SET);

uint32_t sn0;

sn0=(vu32)(0x1FFF7A10);

/* Init ETH */

uint8_t MACAddr[6] ;

heth.Instance = ETH;

heth.Init.AutoNegotiation = ETH_AUTONEGOTIATION_ENABLE;

heth.Init.Speed = ETH_SPEED_100M;

heth.Init.DuplexMode = ETH_MODE_FULLDUPLEX;

heth.Init.PhyAddress = LAN8742A_PHY_ADDRESS;

MACAddr[0] = 0x02;

MACAddr[1] = 0x00;

MACAddr[2] = 0x00;

MACAddr[3] = (sn0>>16)&0XFF;

MACAddr[4] = (sn0>>8)&0XFFF;

MACAddr[5] = sn0&0XFF;

heth.Init.MACAddr = &MACAddr[0];

heth.Init.RxMode = ETH_RXINTERRUPT_MODE;

heth.Init.ChecksumMode = ETH_CHECKSUM_BY_HARDWARE;

heth.Init.MediaInterface = ETH_MEDIA_INTERFACE_RMII;

/* USER CODE BEGIN MACADDRESS */

/* USER CODE END MACADDRESS */

hal_eth_init_status = HAL_ETH_Init(&heth);

if (hal_eth_init_status == HAL_OK)

{

/* Set netif link flag /
netif->flags |= NETIF_FLAG_LINK_UP;
}
/
Initialize Tx Descriptors list: Chain Mode */

HAL_ETH_DMATxDescListInit(&heth, DMATxDscrTab, &Tx_Buff[0][0], ETH_TXBUFNB);

/* Initialize Rx Descriptors list: Chain Mode */

HAL_ETH_DMARxDescListInit(&heth, DMARxDscrTab, &Rx_Buff[0][0], ETH_RXBUFNB);

#if LWIP_ARP || LWIP_ETHERNET

/* set MAC hardware address length */

netif->hwaddr_len = ETH_HWADDR_LEN;

/* set MAC hardware address */

netif->hwaddr[0] = heth.Init.MACAddr[0];

netif->hwaddr[1] = heth.Init.MACAddr[1];

netif->hwaddr[2] = heth.Init.MACAddr[2];

netif->hwaddr[3] = heth.Init.MACAddr[3];

netif->hwaddr[4] = heth.Init.MACAddr[4];

netif->hwaddr[5] = heth.Init.MACAddr[5];

/* maximum transfer unit */

netif->mtu = 1500;

/* Accept broadcast address and ARP traffic /
/
don’t set NETIF_FLAG_ETHARP if this device is not an ethernet one /
#if LWIP_ARP
netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
#else
netif->flags |= NETIF_FLAG_BROADCAST;
#endif /
LWIP_ARP */

/* create a binary semaphore used for informing ethernetif of frame reception */

osSemaphoreDef(SEM);

s_xSemaphore = osSemaphoreCreate(osSemaphore(SEM), 1);

/* create the task that handles the ETH_MAC /
/
USER CODE BEGIN OS_THREAD_DEF_CREATE_CMSIS_RTOS_V1 /
osThreadDef(EthIf, ethernetif_input, osPriorityRealtime, 0, INTERFACE_THREAD_STACK_SIZE);
osThreadCreate (osThread(EthIf), netif);
/
USER CODE END OS_THREAD_DEF_CREATE_CMSIS_RTOS_V1 /
/
Enable MAC and DMA transmission and reception */

HAL_ETH_Start(&heth);

/* USER CODE BEGIN PHY_PRE_CONFIG */

/* USER CODE END PHY_PRE_CONFIG */

/* Read Register Configuration */

HAL_ETH_ReadPHYRegister(&heth, PHY_ISFR, &regvalue);

regvalue |= (PHY_ISFR_INT4);

/* Enable Interrupt on change of link status */

HAL_ETH_WritePHYRegister(&heth, PHY_ISFR , regvalue );

/* Read Register Configuration */

HAL_ETH_ReadPHYRegister(&heth, PHY_ISFR , &regvalue);

/* USER CODE BEGIN PHY_POST_CONFIG */

/* USER CODE END PHY_POST_CONFIG */

#endif /* LWIP_ARP || LWIP_ETHERNET */

/* USER CODE BEGIN LOW_LEVEL_INIT */

/* USER CODE END LOW_LEVEL_INIT */

}

mac 地址 的填写:

MACAddr[0] = 0x02;

MACAddr[1] = 0x00;

MACAddr[2] = 0x00;

MACAddr[3] = (sn0>>16)&0XFF;

MACAddr[4] = (sn0>>8)&0XFFF;

MACAddr[5] = sn0&0XFF;

stm32以太网_引脚

引脚 注意

stm32以太网_stm32以太网_02

stm32以太网_stm32以太网_03

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_RESET);

osDelay(50);

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_SET);