MCU驱动RTLL8211代码
时间: 2023-08-03 13:08:33 浏览: 180
RTL8211E模块板_sch_pcb.zip
RTL8211是一款千兆以太网PHY芯片,它需要在MCU上使用MAC控制器来驱动以太网通信。以下是一个示例代码,用于在STM32 MCU上驱动RTL8211:
```c
#include "stm32f4xx.h"
#include "stm32f4xx_eth.h"
#define PHY_ADDRESS 0x01
#define PHY_BCR 0x00
#define PHY_BSR 0x01
#define PHY_RESET_DELAY ((uint32_t)0x000FFFFF)
#define ETH_MAC_ADDRESS_0 0x00
#define ETH_MAC_ADDRESS_1 0x80
#define ETH_MAC_ADDRESS_2 0xE1
#define ETH_MAC_ADDRESS_3 0x00
#define ETH_MAC_ADDRESS_4 0x00
#define ETH_MAC_ADDRESS_5 0x00
void ETH_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOs clocks */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOF | RCC_AHB1Periph_GPIOG | RCC_AHB1Periph_GPIOH | RCC_AHB1Periph_GPIOI, ENABLE);
/* Enable SYSCFG clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
/* MII/RMII Media interface selection --------------------------------------*/
#ifdef MII_MODE /* Mode MII with STM324xG-EVAL */
SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_MII);
#elif defined RMII_MODE /* Mode RMII with STM324xG-EVAL */
SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_RMII);
#endif
/* Ethernet pins configuration ************************************************/
/*
ETH_MDIO -------------------------> PA2
ETH_MDC --------------------------> PC1
ETH_RMII_REF_CLK------------------> PA1
ETH_RMII_CRS_DV ------------------> PA7
ETH_RMII_RXD0 --------------------> PC4
ETH_RMII_RXD1 --------------------> PC5
ETH_RMII_TX_EN -------------------> PB11
ETH_RMII_TXD0 --------------------> PB12
ETH_RMII_TXD1 --------------------> PB13
*/
/* Configure PA1, PA2 and PA7 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure PB11, PB12 and PB13 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure PC1, PC4 and PC5 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Configure PH2 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOH, &GPIO_InitStructure);
/* Configure PH6 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOH, &GPIO_InitStructure);
/* Connect PA1, PA2 and PA7 */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_ETH);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_ETH);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_ETH);
/* Connect PB11, PB12 and PB13 */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_ETH);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_ETH);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_ETH);
/* Connect PC1, PC4 and PC5 */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource1, GPIO_AF_ETH);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_ETH);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource5, GPIO_AF_ETH);
}
void ETH_MACDMA_Config(void)
{
ETH_InitTypeDef ETH_InitStructure;
/* Enable ETHERNET clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_ETH_MAC | RCC_AHB1Periph_ETH_MAC_Tx | RCC_AHB1Periph_ETH_MAC_Rx, ENABLE);
/* Enable SYSCFG clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
/* Reset ETHERNET on AHB Bus */
ETH_DeInit();
/* Software reset */
ETH_SoftwareReset();
/* Wait for software reset */
while (ETH_GetSoftwareResetStatus() == SET);
/* MAC Config */
ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Enable;
ETH_InitStructure.ETH_Speed = ETH_Speed_100M;
ETH_InitStructure.ETH_Mode = ETH_Mode_FullDuplex;
ETH_InitStructure.ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Enable;
ETH_InitStructure.ETH_ReceiveAll = ETH_ReceiveAll_Enable;
ETH_InitStructure.ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Enable;
ETH_InitStructure.ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
ETH_InitStructure.ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_Perfect;
ETH_InitStructure.ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
ETH_InitStructure.ETH_ChecksumOffload = ETH_ChecksumOffload_Enable;
ETH_InitStructure.ETH_VLANTagComparison = ETH_VLANTagComparison_Disable;
ETH_InitStructure.ETH_VLANTagIdentifier = 0;
ETH_InitStructure.ETH_DropTCPIPChecksumErrorFrame = ETH_DropTCPIPChecksumErrorFrame_Enable;
ETH_InitStructure.ETH_ReceiveStoreForward = ETH_ReceiveStoreForward_Enable;
ETH_InitStructure.ETH_TransmitStoreForward = ETH_TransmitStoreForward_Enable;
ETH_InitStructure.ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Disable;
ETH_InitStructure.ETH_ForwardUndersizedGoodFrames = ETH_ForwardUndersizedGoodFrames_Disable;
ETH_InitStructure.ETH_SecondFrameOperate = ETH_SecondFrameOperate_Enable;
ETH_InitStructure.ETH_AddressAlignedBeats = ETH_AddressAlignedBeats_Enable;
ETH_InitStructure.ETH_FixedBurst = ETH_FixedBurst_Enable;
ETH_InitStructure.ETH_RXDMA_BurstLength = ETH_RXDMA_BurstLength_32Beat;
ETH_InitStructure.ETH_TXDMA_BurstLength = ETH_TXDMA_BurstLength_32Beat;
ETH_InitStructure.ETH_ROKInterrupt = ETH_ROKInterrupt_Enable;
ETH_InitStructure.ETH_TOKInterrupt = ETH_TOKInterrupt_Enable;
ETH_InitStructure.ETH_DMAInterrupt = ETH_DMAInterrupt_Enable;
/* Configure Ethernet */
ETH_Init(Ð_InitStructure, PHY_ADDRESS);
/* Enable the Ethernet Rx Interrupt */
ETH_DMAITConfig(ETH_DMA_IT_NIS | ETH_DMA_IT_R, ENABLE);
/* Enable MAC and DMA transmission and reception */
ETH_Start();
}
void ETH_PHY_Config(void)
{
uint16_t reg_value = 0;
uint32_t timeout = 0;
/* Reset PHY */
ETH_WritePHYRegister(PHY_ADDRESS, PHY_BCR, PHY_Reset);
timeout = PHY_RESET_DELAY;
while ((ETH_ReadPHYRegister(PHY_ADDRESS, PHY_BCR) & PHY_Reset) && timeout) {
timeout--;
}
/* Configure PHY */
ETH_WritePHYRegister(PHY_ADDRESS, PHY_BCR, PHY_AutoNegotiation);
timeout = PHY_RESET_DELAY;
while (!(ETH_ReadPHYRegister(PHY_ADDRESS, PHY_BSR) & PHY_AutoNegoComplete) && timeout) {
timeout--;
}
/* Configure MAC */
ETH_MACAddressConfig(ETH_MAC_Address_0, ETH_MAC_Address_1, ETH_MAC_Address_2, ETH_MAC_Address_3, ETH_MAC_Address_4, ETH_MAC_Address_5);
ETH_MACAddressPerfectFilterCmd(ENABLE);
/* Enable the MAC transmission and reception */
ETH_DMACmd(ETH_DMA_Transmit | ETH_DMA_Receive, ENABLE);
/* Enable the Ethernet Rx Interrupt */
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = ETH_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x7;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
int main(void)
{
/* Configure GPIOs for Ethernet */
ETH_GPIO_Config();
/* Configure Ethernet MAC/DMA */
ETH_MACDMA_Config();
/* Configure Ethernet PHY */
ETH_PHY_Config();
while(1) {
/* Your application code here */
}
}
```
这个示例代码假设你已经正确配置了STM32 MCU的系统时钟,并且已经在代码中定义了以下常量:
- `MII_MODE`或`RMII_MODE`,指示使用哪种以太网接口模式
- `PHY_ADDRESS`,RTL8211在以太网总线上的地址
- `ETH_MAC_ADDRESS_0`到`ETH_MAC_ADDRESS_5`,你的设备的MAC地址
请注意,这只是一个示例代码,需要根据你的具体应用进行修改。例如,你可能需要在代码中定义和设置更多的寄存器值,以满足你的特定需求。
阅读全文