高级会员
- 积分
- 542
- 金钱
- 542
- 注册时间
- 2017-10-17
- 在线时间
- 118 小时
|
修改的内容1:
#include "hw_config.h"
#include "delay.h"
#include "usb_lib.h"
#include "sys.h"
#include "usb_desc.h"
#include "usb_pwr.h"
#include "usb_endp.h"
//USB唤醒中断服务函数
void USBWakeUp_IRQHandler(void)
{
EXTI_ClearITPendingBit(EXTI_Line18);//清除USB唤醒中断挂起位
}
//USB中断处理函数
void USB_LP_IRQHandler(void)
{
USB_Istr();
}
//LED初始化配置
void USB_GpioInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //使能GPIOB时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
// GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_USB);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_USB);
// //GPIOB6,B7初始化设置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化
delay_ms(50);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化
}
//设置USB 连接/断线
//enable:0,断开
//1,允许连接
void UsbSetPort(u8 enable)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //使能PORTA时钟
if(enable)_SetCNTR(_GetCNTR() & (~(1 << 1))); //退出断电模式 cntr第二位表示断电设置
else
{
_SetCNTR(_GetCNTR() | (1 << 1)); // 断电模式
GPIOA->MODER &= 0XFFF00FFF; //设置为输出
GPIOA->MODER |= 0X00033000;
PAout(12) = 0; //D+持续输出0
}
}
//USB中断配置
void UsbInterruptsConfig(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
/* Configure the EXTI line 18 connected internally to the USB IP 外部中断连接EXTI18*/
EXTI_ClearITPendingBit(EXTI_Line18);
// 开启线18上的中断
EXTI_InitStructure.EXTI_Line = EXTI_Line18; // USB resume from suspend mode
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //line 18上事件上升降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
// /* Enable the USB interrupt 低级USB中断 控制传输,中断传输与批量传输*/
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the USB Wake-up interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USB_FS_WKUP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
}
/*********************************************************************
* 函 数 名: SetUsbClock
* 函数入参: void
* 函数出参: 无
* 返 回 值: void
* 功能描述: 配置USB时钟,USBclk=48Mhz
**********************************************************************/
void SetUsbClock(void)
{
// RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); //USBclk=PLLclk/1.5=48Mhz
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); //USB时钟使能
}
/*********************************************************************
* 函 数 名: USB_Set_Init
* 函数入参: void
* 函数出参: 无
* 返 回 值: void
* 功能描述: USB设置初始化
**********************************************************************/
void USB_Set_Init(void)
{
//USB引脚出视化
USB_GpioInit();
delay_ms(200);
//配置USB时钟
SetUsbClock();
//USB中断配置
UsbInterruptsConfig();
//延时100毫秒
//USB初始化
USB_Init();
}
void USB_Set_Init_222(void)
{
USB_GpioInit();
delay_ms(50);
// //USB初始化
// USB_Init();
//USB引脚出视化
// UsbSetPort(0);
//延时100毫秒
delay_ms(150);
//USB初始化
USB_Init();
//允许连接
// UsbSetPort(1);
}
/*******************************************************************************
* Function Name : Get_SerialNum.获得设备唯一序列号,获取描述符
* Description : Create the serial number string descriptor.
* Input : None.
* Output : None.
* Return : None.
*******************************************************************************/
void Get_SerialNum(void)
{
u32 Device_Serial0, Device_Serial1, Device_Serial2;
Device_Serial0 = *(vu32*)(0x1FF80050);
Device_Serial1 = *(vu32*)(0x1FF80054);
Device_Serial2 = *(vu32*)(0x1FF80064);
if(Device_Serial0 != 0)
{
DinkUsbStringSerialUniqueId[2] = (u8)(Device_Serial0 & 0x000000FF);
DinkUsbStringSerialUniqueId[4] = (u8)((Device_Serial0 & 0x0000FF00) >> 8);
DinkUsbStringSerialUniqueId[6] = (u8)((Device_Serial0 & 0x00FF0000) >> 16);
DinkUsbStringSerialUniqueId[8] = (u8)((Device_Serial0 & 0xFF000000) >> 24);
DinkUsbStringSerialUniqueId[10] = (u8)(Device_Serial1 & 0x000000FF);
DinkUsbStringSerialUniqueId[12] = (u8)((Device_Serial1 & 0x0000FF00) >> 8);
DinkUsbStringSerialUniqueId[14] = (u8)((Device_Serial1 & 0x00FF0000) >> 16);
DinkUsbStringSerialUniqueId[16] = (u8)((Device_Serial1 & 0xFF000000) >> 24);
DinkUsbStringSerialUniqueId[18] = (u8)(Device_Serial2 & 0x000000FF);
DinkUsbStringSerialUniqueId[20] = (u8)((Device_Serial2 & 0x0000FF00) >> 8);
DinkUsbStringSerialUniqueId[22] = (u8)((Device_Serial2 & 0x00FF0000) >> 16);
DinkUsbStringSerialUniqueId[24] = (u8)((Device_Serial2 & 0xFF000000) >> 24);
}
}
/*******************************************************************************
* Function Name : USB_Cable_Config表示USB开始连接的标志,空函数,可以在这里面添加
* Description : Software Connection/Disconnection of USB Cable.
* Input : None.
* Return : Status
*******************************************************************************/
void USB_Cable_Config(FunctionalState NewState)
{
if(NewState != DISABLE)
{
//add something to do
}
else
{
//add something to do
}
}
void Enter_LowPowerMode(void)
{
bDeviceState = SUSPENDED;
}
void Leave_LowPowerMode(void)
{
DEVICE_INFO* pInfo = &Device_Info;
if(pInfo->Current_Configuration != 0)
{
bDeviceState = CONFIGURED;
}
else
{
bDeviceState = ATTACHED;
}
}
|
|