📄 usbpro.c
字号:
#include "usb.h"
const unsigned char c_deviceDescriptor[DEVICE_DESC_SIZE] =
{
DEVICE_DESC_SIZE, /*0 Device Descriptor Length : 18*/
0x01, /*1 type = DEVICE*/
0x10, /*2 USB Version LSB*/
0x01, /*3 => 1.00 => 1.10 MSB*/
0x00, /*4 class*/
0x00, /*5 subclass*/
0x00, /*6 protocol*/
0x10, /*7 Max pack caohong size for EP0 <= EP0_SIZE*/
/* vendor ID*/
0x74, /*8 vendor ID LSB (CPAD)*/
0x19, /*9 vendor ID MSB (0x0419 => 04e8)*/
/* product ID*/
0x06, /*10 product ID LSB of CPAD !!!*/
0x00, /*11 product ID MSB (0055->1003)*/
/* release number*/
0x01, /*12 device release number. LSB*/
0x00, /*13 device release number. MSB*/
0x00, /*14 Index fo string descriptor describing manufacture.*/
0x00, /*15 Index fo string descriptor describing this product.*/
0x00, /*16 Index fo string descriptor describing the device's
serial number. Keep the 0x00 data.*/
0x01 /*17 num of possible configurations.*/
};
const unsigned char c_configurationDescriptors[32] =
{
0x09, /*1 Configuration Descriptor Length*/
0x02, /*2 type = CONFIGURATION*/
CONFIG_DESC_TOTAL_SIZE,
0x00, /*4 total length of data returned MSB*/
0x01, /*5 number of interfaces*/
0x01, /*6 value to select config (1 for now)*/
0x00, /*7 index of string desc ( 0 for now)*/
0xc0,
0, /*9 max power, 60mA for now !!!*/
/* Interface Decriptor*/
0x09, /*10 Interface descriptor Length*/
0x04, /*11 INTERFACE descriptor type*/
0x00, /*12 interface index.*/
0x00,
0x02, /*14 Number of endpoints*/
0x08, /*15 Base class:? <pinter=7>*/
0x06, /*16 Subclass:? <printer=1>*/
0x50, /*17 Interface Protocol:*/
/* Bi-direction(02) => Vedor Specific(0xff)*/
0x0, /*18 string index,*/
/*25 polling interval, No meaning in Bluk/Control*/
/* Endpoint descriptor (EP 5 Bulk In)*/
0x07, /*26 Endpoint descriptor Length*/
0x05, /*27 ENDPOINT descriptor type*/
0x82, /*28 ENDPOINT_TX, IN EP, (0x80 | ENDPOINT_TX)*/
0x02, /*29 EP5 attributes: Bulk IN*/
0x40, /*EP5_SIZE, max packet size LSB*/
0x00, /*EP5_SIZE, max packet size MSB*/
0x00, /*321polling interval, No meaning in Bluk/Control*/
/* Endpoint descriptor (EP 4 Bulk Out)*/
0x07, /*19 Endpoint descriptor Length*/
0x05, /*20 ENDPOINT descriptor type*/
0x02, /*21 ENDPOINT_RX, OUT EP, endpoint direction (bit 7) and number (3-0)*/
0x02, /*22 EP4 attributes: Bulk OUT endpoint*/
0x40, /*23 EP4_SIZE, max packet size LSB*/
0x00, /*24 EP4_SIZE, max packet size MSB*/
0x00, /*25 polling interval, No meaning in Bluk/Control*/
};
const unsigned char c_stringDescriptor0[] =
{
0x04, 0x03, 0x09, 0x04
};
const unsigned char c_stringDescriptor1[18] =
{
0x12, 0x03,
'0', 0, '1', 0, '.', 0, '0', 0, '0', 0, '.', 0, '0', 0, '0', 0
};
const unsigned char c_stringDescriptor2[18] =
{
0x12, 0x03,
'0', 0, '2', 0, '.', 0, '0', 0, '0', 0, '.', 0, '0', 0, '0', 0
};
const unsigned char c_stringDescriptor3[18] =
{
0x12, 0x03,
'0', 0, '3', 0, '.', 0, '0', 0, '0', 0, '.', 0, '0', 0, '0', 0
};
const unsigned char c_maxLUN[1] =
{
0
};
/*[ USB Set-up Data ]*/
#define BMREQUESTTYPE 0
#define BREQUEST 1
#define WVALUEL 2
#define WVALUEH 3
#define WINDEXL 4
#define WINDEXH 5
#define WLENGTHL 6
#define WLENGTHH 7
/* [ bmRequestType ]请求类型定义*/
/*Data Trasfer Direction*/
#define DataTrsDir (1<<7)
#define HostToDevice 0x00
#define DeviceToHost 0x80
/*Data Trasfer Type*/
#define DataTrsType (3<<5)
#define StandardType 0x00
#define ClassType 0x20
#define VendorType 0x40
#define ResevType 0x60
/* Recipient Type*/
#define RecipiType (0x1f)
#define DeviceRecipi 0x00 /* Standard ?*/
#define InterfaceRecipi 0x01 /* HID ?*/
#define EndpointRecipi 0x02
#define OtherRecipi 0x03
/* [ bRequest ]*/
/* Specfic Request Code*/
/* StandardType*/
#define GET_STATUS 0x00
#define CLEAR_FEATURE 0x01
#define SET_FEATURE 0x03
#define SET_ADDRESS 0x05
#define GET_DESCRIPTOR 0x06
#define SET_DESCRIPTOR 0x07
#define GET_CONFIGURATION 0x08
#define SET_CONFIGURATION 0x09
#define GET_INTERFACE 0x0A
#define SET_INTERFACE 0x0B
#define SYNCH_FRAME 0x0C
/*#define GET_STATUS 1
#define CLEAR_FEATURE 2
#define SET_FEATURE 3
#define SET_ADDRESS 4
#define GET_DESCRIPTOR 5
#define SET_DESCRIPTOR 6
#define GET_CONFIGURATION 7
#define SET_CONFIGURATION 8
#define GET_INTERFACE 9
#define SET_INTERFACE 10
#define SYNCH_FRAME 11*/
/* DECRIPTOR Type (wValueH)*/
#define DecrType (3<<5)
#define StandardDecr 0x00
#define DEVICE 0x01
#define CONFIGURATION 0x02
#define STRING 0x03
#define INTERFACE 0x04
#define ENDPOINT 0x05
/* CLEAR_FEATURE Type (wValue)*/
#define DEVICE_R_WAKEUP 0x01
#define ENDPOINT_HALTN 0x00
void Default_Proc(void)
{
unsigned char * rep;
rep = (unsigned char *)&g_usbrequest;
if ( (rep[BMREQUESTTYPE] & DataTrsDir) == DeviceToHost )
{
if ( GET_STATUS == rep[BREQUEST] )
{
g_epctl.ep0sendlen = 0;
}
else if ( (rep[BMREQUESTTYPE] & DataTrsType) == StandardType )
{
g_epctl.ep0sendlen = 0;
}
else if( (rep[BMREQUESTTYPE] & DataTrsType) == ClassType )
{
g_epctl.ep0sendlen = 0;
}
else if( (rep[BMREQUESTTYPE] & DataTrsType) == VendorType )
{
g_epctl.ep0sendlen = 0;
}
}
else
{
if( (rep[BMREQUESTTYPE] & DataTrsType) == StandardType)
{
g_epctl.ep0sendlen = 0;
}
else if( (rep[BMREQUESTTYPE] & DataTrsType) == ClassType )
{
g_epctl.ep0sendlen = 0;
}
else if( (rep[BMREQUESTTYPE] & DataTrsType) == VendorType )
{
g_epctl.ep0sendlen = 0;
}
}
}
void USB_Submit(void)
{
unsigned short i = g_epctl.ep0sendlen;
if( i >= EP0_PACKET_SIZE)
{
UsbWriteEndpoint(1, g_epctl.ep0pData, EP0_PACKET_SIZE);
g_epctl.ep0pData +=EP0_PACKET_SIZE;
g_epctl.ep0sendlen = i - EP0_PACKET_SIZE;
g_epctl.state= USB_TRANSMIT;
}
else if( i != 0)
{
UsbWriteEndpoint(1, g_epctl.ep0pData, i);
g_epctl.ep0sendlen = 0;
g_epctl.state = USB_TRANSMIT;
}
else if (i == 0)
{
UsbWriteEndpoint(1, 0, 0); /* Send zero packet at the end */
g_epctl.ep0sendlen = 0;
g_epctl.state = USB_IDLE;
}
}
void Get_Status(void)
{
unsigned char ep;
unsigned char *rep;
rep = (unsigned char *)&g_usbrequest;
switch (g_usbrequest.bmRequestType)
{
case 0x80: /*Device to Host, StandardType, Device Recipient*/
g_epctl.ep0wantlen = 2;
g_epctl.ep0sendlen = 2;
g_epctl.dataSendBuf[0]=0;
g_epctl.dataSendBuf[1]=0;
g_epctl.ep0pData=g_epctl.dataSendBuf;
break;
case 0x81: /*Device to Host, StandardType, Interface Recipient*/
g_epctl.ep0wantlen = 2;
g_epctl.ep0sendlen = 2;
g_epctl.dataSendBuf[0]=0;
g_epctl.dataSendBuf[1]=0;
g_epctl.ep0pData=g_epctl.dataSendBuf;
break;
case 0x82: /*Device to Host, StandardType, Endpoint Recipient*/
/*取端点*/
ep = rep[WINDEXL] & 0x0f;
if (ep == rep[WINDEXL] & 0x80)
//if (ep == rep[WINDEXL] & 0x80)
{
ep =ep*2;
}
else
{
ep =ep*2+1;
}
/*取端点的状态*/
ep= UsbReadEndpointStatus(ep);
if (ep& 0x80)
g_epctl.dataSendBuf[0] = 0x01;
else
g_epctl.dataSendBuf[0] = 0x0;
g_epctl.dataSendBuf[1]=0x0;
g_epctl.ep0pData=g_epctl.dataSendBuf;
g_epctl.ep0wantlen = 2;
g_epctl.ep0sendlen = 2;
break;
default:
Default_Proc();
break;
}
}
void Get_Configuration(void)
{
/* Device to Host, StandardType, Device Recipient*/
if (g_usbrequest.bmRequestType != 0x80)
{
Default_Proc();
return;
}
g_epctl.dataSendBuf[0]=0x0;
g_epctl.ep0pData=g_epctl.dataSendBuf;
g_epctl.ep0wantlen = 1;
g_epctl.ep0sendlen = 1;
}
void Get_InterFace(void)
{
/* Device to Host, StandardType, Interface Recipient*/
if (g_usbrequest.bmRequestType != 0x81)
{
Default_Proc();
return;
}
g_epctl.dataSendBuf[0]=0x0;
g_epctl.ep0pData=g_epctl.dataSendBuf;
g_epctl.ep0wantlen = 1;
g_epctl.ep0sendlen = 1;
}
void Get_DesCriptor(void)
{
unsigned char * rep;
/* 6, Device to Host, StandardType, Device Recipient*/
if (g_usbrequest.bmRequestType != 0x80)
{
Default_Proc();
return;
}
rep = (unsigned char *)&g_usbrequest;
switch(rep[WVALUEH]) /* MSB of wValue is the descr type */
{
case DEVICE:
g_epctl.ep0pData = (unsigned char *)c_deviceDescriptor;
g_epctl.ep0wantlen= rep[WLENGTHL];
if ( g_epctl.ep0wantlen > sizeof(c_deviceDescriptor) )
g_epctl.ep0sendlen = sizeof(c_deviceDescriptor);
USB_Submit();
break;
case CONFIGURATION:
g_epctl.ep0pData = (unsigned char *)c_configurationDescriptors;
g_epctl.ep0wantlen= rep[WLENGTHL];
if ( g_epctl.ep0wantlen > CONFIG_DESC_TOTAL_SIZE )
g_epctl.ep0sendlen = CONFIG_DESC_TOTAL_SIZE;
USB_Submit();
break;
case STRING:
/*
switch(rep[WVALUEL])
{
case 0:
g_epctl.ep0pData = (unsigned char *)c_stringDescriptor0;
g_epctl.ep0wantlen= rep[WLENGTHL];
if ( g_epctl.ep0wantlen > sizeof(c_stringDescriptor0) )
g_epctl.ep0sendlen = sizeof(c_stringDescriptor0);
break;
case 1:
g_epctl.ep0pData = (unsigned char *)c_stringDescriptor1;
g_epctl.ep0wantlen= rep[WLENGTHL];
if ( g_epctl.ep0wantlen > sizeof(c_stringDescriptor1) )
g_epctl.ep0sendlen = sizeof(c_stringDescriptor1);
break;
case 2:
g_epctl.ep0pData = (unsigned char *)c_stringDescriptor2;
g_epctl.ep0wantlen= rep[WLENGTHL];
if ( g_epctl.ep0wantlen > sizeof(c_stringDescriptor2) )
g_epctl.ep0sendlen = sizeof(c_stringDescriptor2);
case 3:
g_epctl.ep0pData = (unsigned char *)c_stringDescriptor3;
g_epctl.ep0wantlen= rep[WLENGTHL];
if ( g_epctl.ep0wantlen > sizeof(c_stringDescriptor3) )
g_epctl.ep0sendlen = sizeof(c_stringDescriptor3);
break;
}
*/
UsbStallEp0();
g_epctl.ep0wantlen =0;
g_epctl.ep0sendlen =0;
break;
default:
UsbSetEndpointStatus(0, 1);
UsbSetEndpointStatus(1, 1);
g_epctl.ep0wantlen =0;
g_epctl.ep0sendlen =0;
break;
}
}
void Synch_Frame(void)
{
/*Device to Host, StandardType, Endpoint Recipient*/
if (g_usbrequest.bmRequestType != 0x82)
{
Default_Proc();
return;
}
g_epctl.ep0wantlen = 0;
g_epctl.ep0sendlen = 0;
}
void Clear_Feature(void)
{
unsigned char ep;
unsigned char *rep;
/*Host to Device, StandardType, Endpoint Recipient*/
if (g_usbrequest.bmRequestType != 0x02)
{
Default_Proc();
return;
}
rep = (unsigned char *)&g_usbrequest;
/*取端点*/
ep = rep[WINDEXL] & 0x0f;
if (ep == rep[WINDEXL] & 0x80)
{
ep =ep*2;
}
else
{
ep =ep*2+1;
}
/*设置端点的状态*/
UsbSetEndpointStatus(ep, 0);
g_epctl.ep0wantlen = 0;
g_epctl.ep0sendlen = 0;
}
void Set_Feature(void)
{
unsigned char ep;
unsigned char *rep;
/*Host to Device, StandardType, Endpoint Recipient*/
if (g_usbrequest.bmRequestType != 0x02)
{
Default_Proc();
return;
}
rep = (unsigned char *)&g_usbrequest;
/*取端点*/
ep = rep[WINDEXL] & 0x0f;
if (ep == rep[WINDEXL] & 0x80)
{
ep =ep*2;
}
else
{
ep =ep*2+1;
}
/*设置端点的状态*/
UsbSetEndpointStatus(ep, 1);
g_epctl.ep0wantlen = 0;
g_epctl.ep0sendlen = 0;
}
void Set_Address(void)
{
unsigned char addr;
unsigned char * rep;
/*Host to Device, StandardType, Device Recipient*/
if (g_usbrequest.bmRequestType != 0x00)
{
Default_Proc();
return;
}
rep = (unsigned char *)&g_usbrequest;
addr = rep[WVALUEL];
UsbSetAddressEnable(rep[WVALUEL] ,1);
g_epctl.ep0wantlen = 0;
g_epctl.ep0sendlen = 0;
}
void Set_Configuration(void)
{
UsbWriteEndpoint(1,0,0);
UsbSetEndpointEnable(1);
g_epctl.ep0wantlen = 0;
g_epctl.ep0sendlen = 0;
}
void SetInterface(void)
{
g_epctl.ep0wantlen = 0;
g_epctl.ep0sendlen = 0;
}
void Get_Max_Lun(void)
{
/*GET_MAX_LUN = 0xA1FE*/
unsigned char * rep;
if (g_usbrequest.bmRequestType != 0xA1)
{
rep = (unsigned char *)&g_usbrequest;
g_epctl.ep0pData = (unsigned char *)c_maxLUN;
g_epctl.ep0wantlen= rep[WLENGTHL];
if ( g_epctl.ep0wantlen > 1 )
g_epctl.ep0sendlen = 1;
return;
}
}
void UsbOnCmd(void)
{
//WrUTXH0( 'c' ) ;
/*if(!(g_epctl.request))
return;
*/
g_epctl.request = 0;
switch(g_usbrequest.bRequest)
{
case GET_DESCRIPTOR: /* 6, Device to Host, StandardType, Device Recipient*/
Get_DesCriptor();
break;
case GET_STATUS:
Get_Status();
break;
case GET_CONFIGURATION: /* Device to Host, StandardType, Device Recipient*/
Get_Configuration();
USB_Submit();
break;
case GET_INTERFACE: /* Device to Host, StandardType, Interface Recipient*/
Get_InterFace();
USB_Submit();
break;
case SYNCH_FRAME: /*Device to Host, StandardType, Endpoint Recipient*/
Synch_Frame();
break;
case CLEAR_FEATURE: /* Host to Device, StandardType, Endpoint Recipient*/
Clear_Feature();
break;
case SET_FEATURE: /*Host to Device, StandardType, Endpoint Recipient*/
Set_Feature();
break;
case SET_ADDRESS: /*Host to Device, StandardType, Device Recipient*/
Set_Address();
USB_Submit();
break;
case SET_CONFIGURATION:
Set_Configuration();
/*USB_Submit();*/
break;
case 0xFE: /*GET_MAX_LUN = 0xA1FE*/
Get_Max_Lun();
USB_Submit();
break;
default:
Default_Proc();
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -