我需要GD32F450的USB初始化代码
时间: 2023-09-30 15:02:18 浏览: 85
初始化usb串口
你好!以下是GD32F450的USB初始化代码:
//定义USB设备状态
typedef enum {
USB_DISCONNECTED = 0,
USB_CONNECTED,
USB_CONFIGURED
} USB_State;
//定义USB端口号
#define USB_PORT GPIOA
#define USB_DN_PIN GPIO_PIN_11
#define USB_DP_PIN GPIO_PIN_12
//定义USB状态
USB_State usbState = USB_DISCONNECTED;
//初始化USB
void USB_Init(void) {
//开启USB时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
//USB引脚配置
GPIO_InitTypeDef gpioInitStruct;
gpioInitStruct.GPIO_Pin = USB_DP_PIN | USB_DN_PIN;
gpioInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
gpioInitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(USB_PORT, &gpioInitStruct);
//USB中断配置
NVIC_InitTypeDef nvicInitStruct;
nvicInitStruct.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
nvicInitStruct.NVIC_IRQChannelPreemptionPriority = 0;
nvicInitStruct.NVIC_IRQChannelSubPriority = 0;
nvicInitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvicInitStruct);
//USB配置
USB_InitTypeDef usbInitStruct;
usbInitStruct.dev_endpoints = 7;
usbInitStruct.speed = USB_SPEED_FULL;
usbInitStruct.ep0_mps = 0x40;
usbInitStruct.phy_itface = USB_PHY_EMBEDDED;
usbInitStruct.low_power = USB_LOW_POWER_DISABLED;
usbInitStruct.sof_output = USB_SOF_OUTPUT_DISABLED;
USB_Init(&usbInitStruct);
//USB连接检测
if (USB_GetConnectState() == USB_CONNECT) {
usbState = USB_CONNECTED;
}
}
//USB中断处理函数
void USB_LP_CAN1_RX0_IRQHandler(void) {
//USB连接检测
if (USB_GetConnectState() == USB_DISCONNECT) {
usbState = USB_DISCONNECTED;
} else if (usbState == USB_DISCONNECTED) {
usbState = USB_CONNECTED;
}
//USB数据处理
if (usbState == USB_CONFIGURED) {
//TODO: 处理USB数据
}
//清除中断标志
USB_ClearITPendingBit(USB_IT_ALL);
}
希望能对你有所帮助!
阅读全文