📄 main.c
字号:
#include "usb_task.h"
#include "usb_kbd_enum.h"
#include "ext_5131.h"
#include "reg_5131.h"
//////////////////////////////
code struct usb_st_device_descriptor usb_device_descriptor =
{
sizeof(usb_device_descriptor), 0x01, 0x1001, 0,
0, 0, 32, 0xeb03, 0x0320,
0x0001, 0x01, 0x02, 0x03, 1
};
/*STRING描述符*/
code struct usb_st_manufacturer usb_manufacturer =
{ sizeof(usb_manufacturer), 0x03, {'X'<<8, 'I'<<8, 'I'<<8, 'Z'<<8, 'X'<<8} };
code struct usb_st_product usb_product =
{ sizeof(usb_product), 0x03, {'A'<<8, 'T'<<8, '8'<<8, '9'<<8, 'C'<<8, '5'<<8, '1'<<8, \
'3'<<8, '1'<<8, ' '<<8, \
'D'<<8, 'D'<<8, 'S'<<8, ' '<<8, \
} };
code struct usb_st_serial_number usb_serial_number =
{ sizeof(usb_serial_number), 0x03, {'1'<<8, '.'<<8, '2'<<8, '.'<<8, '3'<<8} };
code struct usb_st_language_descriptor usb_language =
{ sizeof(usb_language), STRING, 0x0904 };
/*配置描述符集合*/
code struct
{ struct usb_st_configuration_descriptor cfg;
struct usb_st_interface_descriptor ifc;
struct usb_st_hid_descriptor hid ;
struct usb_st_endpoint_descriptor ep1 ;
unsigned char rep[0x3b] ;
}
usb_configuration =
{
{ 9, 0x02, 0x2200, 1, 1,0, 0x80, 50},
{ 9, 0x04, 1, 0, 1, 0x03, 0, 0, 0 },
{ 9, 0x21, 0x1101, 8, 1, 0x22, 0x3B00 },
{ 7, 0x05, 0x01, 0x03, 32, 0x20 },
};
unsigned char Bufin[64];
static unsigned char endpoint_status[2];
static unsigned char *pbuffer;
static unsigned char bmRequestType;
unsigned char usb_configuration_nb;
extern bit usb_connected;
bit usb_connected_stored;
bit usb_configured_stored;
/////////////////////////////
void main (void)
{
EA = 1; //使能中断
init_timer0(); //定时器初始化
delay(10);
usb_task_init(); //usb控制器初始化
while(1) //任务循环
{
usb_task(); //usb任务处理子程序
}
}
void init_timer0()
{
TMOD=0x05;
TH0=0;
TL0=0;
}
void delay(int n)
{
TH0=n;
TL0=0;
TR0=1;
if(TH0>1);
return;
}
void usb_task_init(void)
{
USBCON |= 0x80; //使能USB控制器
USBCON |= 0x10; /*USB软件插拔*/
delay(100);
USBCON &= ~0x10;
PLLDIV = 32; //配置控制器时钟
PLLCON |= 0x02;//使能PLL
//usb_configure_endpoint(EP_CONTROL, CONTROL);//配置控制端口
UEPNUM = 0;//复位控制端口
UEPCONX = 0;
/*初始化状态变量*/
usb_connected = 0;
usb_connected_stored = 1;
usb_configured_stored = 0;
endpoint_status[0] = 0x00;
endpoint_status[1] = 0x00;
usb_connected = 0;
usb_configuration_nb = 0x00;
}
/////////////////////////////////
void usb_task(void)
{
if (UEPINT & 0) usb_enumeration_process();
if (UEPINT & 1)
{
unsigned char data i;
i = ReadEp(2,Bufin);
switch(Bufin[0])
{
case 1:
singletone_set();
break;
case 2:
unrampedfsk_set();
break;
case 3:
rampedfsk_set();
break;
case 4:
chirp();
break;
case 5:
bpsk();
break;
}
}
}
/*usb_enumeration_process ()*/
void usb_enumeration_process (void)//USB设备枚举过程
{
UEPNUM = 0;//选择控制端口
bmRequestType = UEPDATX; /* 得到 bmRequestType */
switch (UEPDATX) /* 判断 bRequest 的值 */
{
case GET_DESCRIPTOR: /*获得描述符请求*/
usb_get_descriptor();
break;
case GET_CONFIGURATION: /*获得配置请求*/
usb_get_configuration();
break;
case SET_ADDRESS: /*设置地址请求*/
usb_set_address();
break;
case SET_CONFIGURATION: /*设置配置请求或设置HID报表请求*/
if (bmRequestType == 0) { usb_set_configuration(); }
else { usb_hid_set_report(); }
break;
case CLEAR_FEATURE: /*清除特性请求*/
usb_clear_feature();
break;
case SET_FEATURE: /*设置特性请求*/
usb_set_feature();
break;
case GET_STATUS: /*获得状态请求*/
usb_get_status();
break;
case GET_INTERFACE: /*获得接口信息请求或设置响应间隔请求*/
if (bmRequestType == 0x81) { usb_get_interface(); }
else { usb_hid_set_idle(); }
break;
case HID_GET_IDLE: /*获得响应间隔请求*/
usb_hid_get_idle();
break;
case SET_DESCRIPTOR:
case SET_INTERFACE:
case SYNCH_FRAME:
default:
UEPSTAX =UEPSTAX & ~0x04; /*清除SETUP标志*/
UEPSTAX =UEPSTAX | 0x20; /*中止请求设置*/
while (!UEPSTAX & 0x08);
UEPSTAX =UEPSTAX &~ 0x20;
UEPSTAX =UEPSTAX &~ 0x08;
break;
}
UEPSTAX = UEPSTAX &~0x80;
}
////////////*get_descriptor*///////////////
void usb_get_descriptor (void)
{
unsigned char data_to_transfer;
unsigned int wLength;
unsigned char descriptor_type;
unsigned char string_type;
string_type = UEPDATX; /* 读取 wValue的低位 */
descriptor_type = UEPDATX; /* 读取 wValue的高位 */
switch (descriptor_type) /*判断描述符类型*/
{
case DEVICE: /*设备描述符
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -