📄 chap_9.c
字号:
////CHAP9.C
#include "common.h"
#include "chap_9.h"
#include "mainloop.h"
#include "D12CI.h"
#include "usb100.h"
#include "protodma.h"
#include "disp.h"
extern EPPFLAGS bEPPflags;
CONTROL_XFER ControlData;
uchar setupcounter;
code HID_REPORT_DESCRIPTOR MyReportDescriptor[] = { /*添加的report*/
0x05, 0x01, // Usage Page (Generic Desktop),
0x09, 0x06, // Usage (Keyboard),
0xA1, 0x01, // Collection (Application),
0x05, 0x07, // Usage Page (Key Codes);
0x19, 0xE0, // Usage Minimum (224),
0x29, 0xE7, // Usage Maximum (231),
0x15, 0x00, // Logical Minimum (0),
0x25, 0x01, // Logical Maximum (1),
0x75, 0x01, // Report Size (1),
0x95, 0x08, // Report Count (8),
0x81, 0x02, // Input (Data, Variable, Absolute),;Modifier byte
0x95, 0x01, // Report Count (1),
0x75, 0x08, // Report Size (8),
0x81, 0x01, // Input (Constant), ;Reserved byte
0x95, 0x05, // Report Count (5),
0x75, 0x01, // Report Size (1),
0x05, 0x08, // Usage Page (Page# for LEDs),
0x19, 0x01, // Usage Minimum (1),
0x29, 0x05, // Usage Maximum (5),
0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
0x95, 0x01, // Report Count (1),
0x75, 0x03, // Report Size (3),
0x91, 0x01, // Output (Constant), ;LED report padding
0x95, 0x06, // Report Count (6),
0x75, 0x08, // Report Size (8),
0x15, 0x00, // Logical Minimum (0),
0x25, 0x65, // Logical Maximum(101),
0x05, 0x07, // Usage Page (Key Codes),
0x19, 0x00, // Usage Minimum (0),
0x29, 0x65, // Usage Maximum (101),
0x81, 0x00, // Input (Data, Array), ;Key arrays (6 bytes)
0xC0 // End Collection
};
code USB_DEVICE_DESCRIPTOR DeviceDescr =
{
sizeof(USB_DEVICE_DESCRIPTOR),
USB_DEVICE_DESCRIPTOR_TYPE,
SWAP(0x0110),
0,//USB_CLASS_CODE_TEST_CLASS_DEVICE,
0, 0,
EP0_PACKET_SIZE,
SWAP(0x0471),
SWAP(0x0011),//
SWAP(0x0100),
1, 2, 0,
1
};
code USB_CONFIGURATION_DESCRIPTOR ConfigDescr =
{
sizeof(USB_CONFIGURATION_DESCRIPTOR),
USB_CONFIGURATION_DESCRIPTOR_TYPE,
SWAP(CONFIG_DESCRIPTOR_LENGTH),
1,
1,
0,
0x80,//0x60,//
0x80//0x32
};
code USB_INTERFACE_DESCRIPTOR InterfaceDescr =
{
sizeof(USB_INTERFACE_DESCRIPTOR),
USB_INTERFACE_DESCRIPTOR_TYPE,
0,
0,
NUM_ENDPOINTS,
3,
1,
1,
//USB_CLASS_CODE_TEST_CLASS_DEVICE,
//USB_SUBCLASS_CODE_TEST_CLASS_D12,
//USB_PROTOCOL_CODE_TEST_CLASS_D12,
0
};
//
// The HID descriptor has some basic device info and tells how long the report
// descriptor is.
//
code USB_HID_DESCRIPTOR ClassDescriptor = { /*添加的class descriptor*/
0x09, // length of HID descriptor
0x21, // descriptor type == HID
SWAP(0x0100), // hid spec release
0x00, // country code == Not Specified
0x01, // number of HID class descriptors
0x22, // report descriptor type
SWAP(sizeof(MyReportDescriptor)) // total length of report descriptor
};
code USB_ENDPOINT_DESCRIPTOR EP1_TXDescr =
{
sizeof(USB_ENDPOINT_DESCRIPTOR),
USB_ENDPOINT_DESCRIPTOR_TYPE,
0x81,
USB_ENDPOINT_TYPE_INTERRUPT,
SWAP(EP1_PACKET_SIZE),
10
};
code USB_ENDPOINT_DESCRIPTOR EP1_RXDescr =
{
sizeof(USB_ENDPOINT_DESCRIPTOR),
USB_ENDPOINT_DESCRIPTOR_TYPE,
0x1,
USB_ENDPOINT_TYPE_INTERRUPT,
SWAP(EP1_PACKET_SIZE),
10
};
code USB_ENDPOINT_DESCRIPTOR EP2_TXDescr =
{
sizeof(USB_ENDPOINT_DESCRIPTOR),
USB_ENDPOINT_DESCRIPTOR_TYPE,
0x82,
USB_ENDPOINT_TYPE_BULK,
SWAP(EP2_PACKET_SIZE),
10
};
code USB_ENDPOINT_DESCRIPTOR EP2_RXDescr =
{
sizeof(USB_ENDPOINT_DESCRIPTOR),
USB_ENDPOINT_DESCRIPTOR_TYPE,
0x2,
USB_ENDPOINT_TYPE_BULK,
SWAP(EP2_PACKET_SIZE),
10
};
code void(* StandardDeviceRequest[])(void)=
{
get_status,
clear_feature,
reserved,
set_feature,
reserved,
set_address,
get_descriptor,
reserved,
get_configuration,
set_configuration,
get_interface,
set_interface,
reserved,
reserved,
reserved,
reserved
};
extern code void(*VendorDeviceRequest[])(void);
void stall_ep0(void)
{
D12_SetEndpointStatus(0, 1);
D12_SetEndpointStatus(1, 1);
}
void reserved(void)
{
stall_ep0();
}
void control_handler()
{
uchar type,req;
type = ControlData.DeviceRequest.bmRequestType&USB_REQUEST_TYPE_MASK;
req = ControlData.DeviceRequest.bRequest&USB_REQUEST_MASK;
if(type==USB_STANDARD_REQUEST)
(*StandardDeviceRequest[req])();
else if(type==USB_VENDOR_REQUEST)
(*VendorDeviceRequest[req])();
else
stall_ep0();
}
void init_unconfig(void)
{
D12_SetEndpointEnable(0); /* Disable all endpoints but EPP0. */
}
void init_config(void)
{
D12_SetEndpointEnable(1);
}
void single_transmit(uchar * buf,uchar len)
{
if(len<=EP0_PACKET_SIZE){
D12_WriteEndpoint(1,len,buf);
}
}
void code_transmit(uchar code * pRomData,ushort len)
{
ControlData.wCount = 0;
if(ControlData.wLength>len)
ControlData.wLength = len;
ControlData.pData = pRomData;
if(ControlData.wLength >= EP0_PACKET_SIZE)
{
D12_WriteEndpoint(1,EP0_PACKET_SIZE,ControlData.pData);
ControlData.wCount +=EP0_PACKET_SIZE;
DISABLE;
bEPPflags.bits.control_state = USB_TRANSMIT;
ENABLE;
}
else{
D12_WriteEndpoint(1,ControlData.wLength,pRomData);
ControlData.wCount += ControlData.wLength;
DISABLE;
bEPPflags.bits.control_state = USB_IDLE;
ENABLE;
}
}
///////////////////////////****************///////////////////////////////////////////////
void get_status(void)
{
unsigned char endp, txdat[2];
unsigned char bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
unsigned char c;
dispinfo(2,"get status ");
disphex(setupcounter++);
loadmsg("get state\r\n",20);
if (bRecipient == USB_RECIPIENT_DEVICE) {
if(bEPPflags.bits.remote_wakeup == 1)
txdat[0] = 3;
else
txdat[0] = 1;
txdat[1]=0;
single_transmit(txdat, 2);
} else if (bRecipient == USB_RECIPIENT_INTERFACE) {
txdat[0]=0;
txdat[1]=0;
single_transmit(txdat, 2);
} else if (bRecipient == USB_RECIPIENT_ENDPOINT) {
endp = (unsigned char)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);
if (ControlData.DeviceRequest.wIndex & (unsigned char)USB_ENDPOINT_DIRECTION_MASK)
c = D12_SelectEndpoint(endp*2 + 1); /* Control-in */
else
c = D12_SelectEndpoint(endp*2); /* Control-out */
if(c & D12_STALL)
txdat[0] = 1;
else
txdat[0] = 0;
txdat[1] = 0;
single_transmit(txdat, 2);
} else
stall_ep0();
}
void clear_feature(void)
{
unsigned char endp;
unsigned char bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
dispinfo(2,"clearfeature ");
disphex(setupcounter++);
loadmsg("clearfeature\r\n",20);
if (bRecipient == USB_RECIPIENT_DEVICE
&& ControlData.DeviceRequest.wValue == USB_FEATURE_REMOTE_WAKEUP) {
DISABLE;
bEPPflags.bits.remote_wakeup = 0;
ENABLE;
single_transmit(0, 0);
}
else if (bRecipient == USB_RECIPIENT_ENDPOINT
&& ControlData.DeviceRequest.wValue == USB_FEATURE_ENDPOINT_STALL) {
endp = (unsigned char)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);
if (ControlData.DeviceRequest.wIndex & (unsigned char)USB_ENDPOINT_DIRECTION_MASK)
/* clear TX stall for IN on EPn. */
D12_SetEndpointStatus(endp*2 + 1, 0);
else
/* clear RX stall for OUT on EPn. */
D12_SetEndpointStatus(endp*2, 0);
single_transmit(0, 0);
} else
stall_ep0();
}
void set_feature(void)
{
unsigned char endp;
unsigned char bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
dispinfo(2,"set feature ");
disphex(setupcounter++);
loadmsg("set feature\r\n",20);
if (bRecipient == USB_RECIPIENT_DEVICE
&& ControlData.DeviceRequest.wValue == USB_FEATURE_REMOTE_WAKEUP) {
DISABLE;
bEPPflags.bits.remote_wakeup = 1;
ENABLE;
single_transmit(0, 0);
}
else if (bRecipient == USB_RECIPIENT_ENDPOINT
&& ControlData.DeviceRequest.wValue == USB_FEATURE_ENDPOINT_STALL) {
endp = (unsigned char)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);
if (ControlData.DeviceRequest.wIndex & (unsigned char)USB_ENDPOINT_DIRECTION_MASK)
/* clear TX stall for IN on EPn. */
D12_SetEndpointStatus(endp*2 + 1, 1);
else
/* clear RX stall for OUT on EPn. */
D12_SetEndpointStatus(endp*2, 1);
single_transmit(0, 0);
} else
stall_ep0();
}
void set_address(void)
{
dispinfo(2,"set address ");
disphex(setupcounter++);
loadmsg("set address\r\n",20);
D12_SetAddressEnable((unsigned char)(ControlData.DeviceRequest.wValue &
DEVICE_ADDRESS_MASK), 1);
single_transmit(0, 0);
}
void get_descriptor(void)
{
uchar chh,chl;
unsigned char bDescriptor = MSB(ControlData.DeviceRequest.wValue);
dispinfo(1,"des:");
disphex(setupcounter++);
dispinfo(0," X:");
htoc(bDescriptor,&chh,&chl);
loadmsg("get descriptor Index:",30);
loadmsg(&chh,1);
loadmsg(&chl,1);
loadmsg(" Long:",20);
writedispdata(chh);
writedispdata(chl);
htoc(ControlData.DeviceRequest.wLength,&chh,&chl);
loadmsg(&chh,1);
loadmsg(&chl,1);
loadmsg("\r\n",2);
dispinfo(0," L:");
writedispdata(chh);
writedispdata(chl);
if (bDescriptor == USB_DEVICE_DESCRIPTOR_TYPE) {
code_transmit((unsigned char code *)&DeviceDescr, sizeof(USB_DEVICE_DESCRIPTOR));
} else if (bDescriptor == USB_CONFIGURATION_DESCRIPTOR_TYPE) {
code_transmit((unsigned char code *)&ConfigDescr, CONFIG_DESCRIPTOR_LENGTH);
} else if(bDescriptor == 0x22){
code_transmit((uchar code *)MyReportDescriptor,sizeof(MyReportDescriptor));
} else
stall_ep0();
}
void get_configuration(void)
{
unsigned char c = bEPPflags.bits.configuration;
dispinfo(2,"get config ");
disphex(setupcounter++);
loadmsg("get configur\r\n",20);
single_transmit(&c, 1);
}
void set_configuration(void)
{
dispinfo(2,"set config ");
disphex(setupcounter++);
loadmsg("set config\r\n",20);
if (ControlData.DeviceRequest.wValue == 0) {
/* put device in unconfigured state */
single_transmit(0, 0);
DISABLE;
bEPPflags.bits.configuration = 0;
ENABLE;
init_unconfig();
} else if (ControlData.DeviceRequest.wValue == 1) {
/* Configure device */
single_transmit(0, 0);
init_unconfig();
init_config();
DISABLE;
bEPPflags.bits.configuration = 1;
ENABLE;
} else
stall_ep0();
}
void get_interface(void)
{
unsigned char txdat = 0; /* Only/Current interface = 0 */
dispinfo(2,"get interface ");
disphex(setupcounter++);
loadmsg("get interface\r\n",20);
single_transmit(&txdat, 1);
}
void set_interface(void)
{
dispinfo(2,"set interface ");
disphex(setupcounter++);
loadmsg("set interface\r\n",20);
if (ControlData.DeviceRequest.wValue == 0 && ControlData.DeviceRequest.wIndex == 0)
single_transmit(0, 0);
else
stall_ep0();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -