GD32f307芯片配置Enet接口示例代码
时间: 2023-08-21 16:04:14 浏览: 223
GD32F307 DAC 输出波形
以下是基于GD32F30x标准库的Enet接口配置示例代码:
```c
#include "gd32f30x.h"
#define ETH_MAC_ADDR0 (0x00)
#define ETH_MAC_ADDR1 (0x11)
#define ETH_MAC_ADDR2 (0x22)
#define ETH_MAC_ADDR3 (0x33)
#define ETH_MAC_ADDR4 (0x44)
#define ETH_MAC_ADDR5 (0x55)
#define ETH_RX_BUF_SIZE (ETH_MAX_PACKET_SIZE)
#define ETH_TX_BUF_SIZE (ETH_MAX_PACKET_SIZE)
uint8_t eth_rx_buf[ETH_RX_BUF_SIZE];
uint8_t eth_tx_buf[ETH_TX_BUF_SIZE];
void enet_gpio_init(void);
void enet_mac_init(void);
void enet_dma_init(void);
void enet_config(void);
int main(void)
{
/* Enable the ENET clock */
rcu_periph_clock_enable(RCU_ENET);
/* Initialize the ENET GPIO */
enet_gpio_init();
/* Initialize the ENET MAC */
enet_mac_init();
/* Initialize the ENET DMA */
enet_dma_init();
/* Configure the ENET */
enet_config();
while(1);
}
void enet_gpio_init(void)
{
/* Configure PA1 as ENET_MII_RX_CLK */
gpio_af_set(GPIOA, GPIO_AF_11, GPIO_PIN_1);
/* Configure PA2 as ENET_MII_MDIO */
gpio_af_set(GPIOA, GPIO_AF_11, GPIO_PIN_2);
/* Configure PA7 as ENET_MII_RX_DV */
gpio_af_set(GPIOA, GPIO_AF_11, GPIO_PIN_7);
/* Configure PB0 as ENET_MII_RXD0 */
gpio_af_set(GPIOB, GPIO_AF_11, GPIO_PIN_0);
/* Configure PB1 as ENET_MII_RXD1 */
gpio_af_set(GPIOB, GPIO_AF_11, GPIO_PIN_1);
/* Configure PB8 as ENET_MII_TXD0 */
gpio_af_set(GPIOB, GPIO_AF_11, GPIO_PIN_8);
/* Configure PB9 as ENET_MII_TXD1 */
gpio_af_set(GPIOB, GPIO_AF_11, GPIO_PIN_9);
/* Configure PC1 as ENET_MDC */
gpio_af_set(GPIOC, GPIO_AF_11, GPIO_PIN_1);
/* Configure PC4 as ENET_MII_TX_EN */
gpio_af_set(GPIOC, GPIO_AF_11, GPIO_PIN_4);
/* Configure PC5 as ENET_MII_TX_CLK */
gpio_af_set(GPIOC, GPIO_AF_11, GPIO_PIN_5);
/* Configure PC8 as ENET_MII_COL */
gpio_af_set(GPIOC, GPIO_AF_11, GPIO_PIN_8);
/* Configure PC9 as ENET_MII_TXD2 */
gpio_af_set(GPIOC, GPIO_AF_11, GPIO_PIN_9);
/* Configure PC10 as ENET_MII_TXD3 */
gpio_af_set(GPIOC, GPIO_AF_11, GPIO_PIN_10);
}
void enet_mac_init(void)
{
/* Enable the MAC and reset it */
enet_mac_deinit();
/* Set the MAC parameters */
enet_mac_config_t mac_config;
mac_config.receive_all = ENABLE;
mac_config.broadcast_frames_reception = ENABLE;
mac_config.destination_addr_filter = DISABLE;
mac_config.pass_control_frames = ENABLE;
mac_config.source_addr_filter = DISABLE;
mac_config.source_addr_hash = DISABLE;
mac_config.jumbo_frame_size = DISABLE;
mac_config.watchdog_disable = ENABLE;
mac_config.checksum_offload = DISABLE;
mac_config.giant_frame_size = DISABLE;
/* Initialize the MAC */
enet_mac_init(&mac_config, ETH_MAC_ADDR0, ETH_MAC_ADDR1, ETH_MAC_ADDR2, ETH_MAC_ADDR3, ETH_MAC_ADDR4, ETH_MAC_ADDR5);
}
void enet_dma_init(void)
{
/* Reset the DMA */
enet_dma_deinit();
/* Set the DMA parameters */
enet_dma_config_t dma_config;
dma_config.tx_buf_size = ETH_TX_BUF_SIZE;
dma_config.rx_buf_size = ETH_RX_BUF_SIZE;
dma_config.tx_buf_count = 1;
dma_config.rx_buf_count = 1;
dma_config.tx_dma_channel = DMA_CH4;
dma_config.rx_dma_channel = DMA_CH5;
dma_config.tx_irq_enable = DISABLE;
dma_config.rx_irq_enable = DISABLE;
/* Initialize the DMA */
enet_dma_init(&dma_config, eth_tx_buf, eth_rx_buf);
}
void enet_config(void)
{
/* Set the PHY parameters */
enet_phy_config_t phy_config;
phy_config.mode = ENET_PHY_MODE_AUTO;
phy_config.speed = ENET_PHY_SPEED_AUTO;
phy_config.duplex = ENET_PHY_DUPLEX_AUTO;
phy_config.auto_negotiation = ENABLE;
/* Configure the PHY */
enet_phy_config(phy_config);
/* Start the ENET */
enet_start();
}
```
需要注意的是,以上代码仅是示例代码,实际使用时需要根据具体情况进行适当的修改。
阅读全文