GD32芯片配置Enet接口示例代码
时间: 2023-08-21 21:04:14 浏览: 463
GD32MCU_Example
以下是基于GD32F450芯片的Enet接口配置示例代码,供参考:
```c
#include "gd32f4xx.h"
#include <stdio.h>
#include <string.h>
#define ENET_BUF_SIZE 1536
#define ENET_PHY_ADDRESS 0x01
#define ENET_PHY_STATUS 0x01
#define ENET_RX_DMA_CHANNEL DMA_CH1
#define ENET_TX_DMA_CHANNEL DMA_CH2
uint8_t rx_buffer[ENET_BUF_SIZE];
uint8_t tx_buffer[ENET_BUF_SIZE];
/* ENET MAC address */
uint8_t macaddr[6] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
/* ENET IP address */
uint8_t ipaddr[4] = {192, 168, 0, 100};
/* ENET netmask */
uint8_t netmask[4] = {255, 255, 255, 0};
/* ENET gateway address */
uint8_t gateway[4] = {192, 168, 0, 1};
/* ENET RX packet struct */
struct enet_rx_packet {
uint32_t status_info;
uint32_t length;
uint8_t data[ENET_BUF_SIZE];
};
/* ENET TX packet struct */
struct enet_tx_packet {
uint32_t length;
uint8_t data[ENET_BUF_SIZE];
};
/* ENET init */
void enet_init(void) {
/* Enable GPIO clock */
rcu_periph_clock_enable(RCU_GPIOA);
/* Configure PA1 as ENET_RXD0 */
gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
/* Configure PA2 as ENET_MDIO */
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
/* Configure PA7 as ENET_RXD1 */
gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
/* Configure PB0 as ENET_MDC */
gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
/* Configure PB10 as ENET_TX_EN */
gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
/* Configure PB11 as ENET_TXD0 */
gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_11);
/* Configure PB12 as ENET_TXD1 */
gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12);
/* Enable ENET clock */
rcu_periph_clock_enable(RCU_ENET);
/* Reset ENET */
enet_deinit();
/* Enable ENET MAC */
enet_mac_enable();
/* Set ENET MAC address */
enet_mac_address_set(macaddr);
/* Enable ENET RX */
enet_rx_enable();
/* Enable ENET TX */
enet_tx_enable();
/* Set ENET IP address */
enet_ip_address_set(ipaddr);
/* Set ENET netmask */
enet_netmask_set(netmask);
/* Set ENET gateway address */
enet_gateway_address_set(gateway);
/* Enable ENET interrupt */
enet_interrupt_enable(ENET_INT_TIE | ENET_INT_RIE | ENET_INT_RBUIE);
/* Enable DMA clock */
rcu_periph_clock_enable(RCU_DMA);
/* Enable ENET RX DMA */
enet_dma_rx_enable();
/* Enable ENET TX DMA */
enet_dma_tx_enable();
}
/* ENET RX DMA interrupt handler */
void enet_rx_dma_irq_handler(void) {
/* Get ENET RX DMA channel interrupt flag */
if (dma_interrupt_flag_get(DMA1, ENET_RX_DMA_CHANNEL, DMA_INT_FLAG_FTF)) {
/* Clear ENET RX DMA channel interrupt flag */
dma_interrupt_flag_clear(DMA1, ENET_RX_DMA_CHANNEL, DMA_INT_FLAG_FTF);
/* Get ENET RX packet struct */
struct enet_rx_packet *rx_packet = (struct enet_rx_packet *)rx_buffer;
/* Process ENET RX packet */
/* ... */
/* Allocate new buffer for ENET RX DMA */
dma_memory_to_memory_disable(DMA1, ENET_RX_DMA_CHANNEL);
dma_transfer_size_config(DMA1, ENET_RX_DMA_CHANNEL, ENET_BUF_SIZE);
dma_memory_address_config(DMA1, ENET_RX_DMA_CHANNEL, (uint32_t)rx_buffer);
dma_periph_address_config(DMA1, ENET_RX_DMA_CHANNEL, (uint32_t)&ENET->DR);
dma_memory_to_memory_enable(DMA1, ENET_RX_DMA_CHANNEL);
}
}
/* ENET TX DMA interrupt handler */
void enet_tx_dma_irq_handler(void) {
/* Get ENET TX DMA channel interrupt flag */
if (dma_interrupt_flag_get(DMA1, ENET_TX_DMA_CHANNEL, DMA_INT_FLAG_FTF)) {
/* Clear ENET TX DMA channel interrupt flag */
dma_interrupt_flag_clear(DMA1, ENET_TX_DMA_CHANNEL, DMA_INT_FLAG_FTF);
/* Process ENET TX packet */
/* ... */
}
}
int main(void) {
/* Initialize ENET */
enet_init();
/* Main loop */
while (1) {
/* ... */
}
return 0;
}
```
上述代码中使用了DMA来进行ENET数据包的传输,同时也实现了ENET的中断处理函数。需要注意的是,ENET的配置需要根据具体芯片和网络环境进行调整,上述代码仅供参考。
阅读全文