📄 usb.c
字号:
if (usb_ep0_rx_buffer[1]==0x0A) {usb_Get_Interface();}
else if (usb_ep0_rx_buffer[1]==0x00) {usb_Get_Interface_Status();}
else if (usb_ep0_rx_buffer[1]==0x06) {usb_Get_Descriptor();}
else {usb_wrongstate();}
}
/**************************************************************
/* usb_isr_tkn_EndpointToHost()
/*
/* Input: usb_ep0_rx_buffer[1] == bRequest
/*
/* Summary: bmRequestType told us it was a Endpoint to Host request.
/* bRequest says which request. Only certain requests are valid,
/* if a non-valid request was made then return with an Wrong-Statue (IDLE)
/*
/* Part of usb_isr_tok_setup_dne()
/***************************************************************/
void usb_isr_tkn_EndpointToHost() {
if (usb_ep0_rx_buffer[1]==0x00) {usb_Get_Endpoint_Status();}
else {usb_wrongstate();}
}
/**************************************************************
/* usb_isr_tkn_ClassRequest()
/*
/* Input: usb_ep0_rx_buffer[1] == bRequest
/*
/* Summary: bmRequestType told us it was a Class request.
/* bRequest says which request. Only certain requests are valid,
/* if a non-valid request was made then return with an Wrong-Statue (IDLE)
/*
/* Class requests are for HID devices.
/*
/* Part of usb_isr_tok_setup_dne()
/* Only compiled if HID_DEVICE is TRUE
/***************************************************************/
#IF USB_HID_DEVICE
void usb_isr_tkn_ClassRequest() {
int request;
request=usb_ep0_rx_buffer[1];
if (request==0x09) {usb_isr_tok_out_dne(0);}
else if (request==0x0A) {usb_set_idle();}
else if (request==0x02) {usb_get_idle();}
else if (request==0x0B) {usb_set_protocol();}
else if (request==0x03) {usb_get_protocol();}
else {usb_wrongstate();}
}
/**************************************************************
/* usb_set_idle()
/*
/* Summary: Handles Set_Idle HID request. This request is optional.
/* This code is not provided by CCS.
/*
/* Part of usb_isr_tok_setup_dne()
/* Only compiled if HID_DEVICE is TRUE
/***************************************************************/
void usb_set_idle(void) {
debug(debug_txb,"SI ");
#IF USB_SUPPORT_IDLE
//TODO: idle code
//id=buffer[2]
//count=buffer[3]
//interface=buffer[4]
#ELSE
usb_wrongstate();
#ENDIF
}
/**************************************************************
/* usb_get_idle()
/*
/* Summary: Handles Get_Idle HID request. This request is optional.
/* This code is not provided by CCS.
/*
/* Part of usb_isr_tok_setup_dne()
/* Only compiled if HID_DEVICE is TRUE
/***************************************************************/
void usb_get_idle(void) {
debug(debug_txb,"GI ");
#IF USB_SUPPORT_IDLE
//TODO: idle code
//id=buffer[2]
#ELSE
usb_wrongstate();
#ENDIF
}
/**************************************************************
/* usb_set_protocol()
/*
/* Summary: Handles Set_Protocol HID request. This request is optional.
/* This code is not provided by CCS.
/*
/* Part of usb_isr_tok_setup_dne()
/* Only compiled if HID_DEVICE is TRUE
/***************************************************************/
void usb_set_protocol(void) {
debug(debug_txb,"SP ");
#IF USB_BOOT_PROTOCOL
hid_protocol[usb_ep0_rx_buffer[4]]=usb_ep0_rx_buffer[2];
usb_put_packet(0,usb_ep0_rx_buffer,0,DATA1); //send 0len packet
#ELSE
usb_wrongstate();
#ENDIF
}
/**************************************************************
/* usb_get_protocol()
/*
/* Summary: Handles Get_Protocol HID request. This request is optional.
/* This code is not provided by CCS.
/*
/* Part of usb_isr_tok_setup_dne()
/* Only compiled if HID_DEVICE is TRUE
/***************************************************************/
void usb_get_protocol(void) {
int8 interface;
debug(debug_txb,"GP ");
#IF USB_BOOT_PROTOCOL
interface=usb_ep0_rx_buffer[4];
usb_ep0_rx_buffer[0]=hid_protocol[interface];
usb_put_packet(0,usb_ep0_rx_buffer,1,DATA1);
#ELSE
usb_wrongstate();
#ENDIF
}
#ENDIF
/**************************************************************
/* usb_Get_Descriptor()
/*
/* Input: usb_ep0_rx_buffer[3] == wValue, which descriptor we want
/*
/* Summary: Checks to see if we want a standard descriptor (Interface, Endpoint, Config, Device, String, etc.),
/* or a class specific (HID) descriptor.
/*
/* Part of usb_isr_tok_setup_dne()
/* Part of Get_Descriptor request handler.
/***************************************************************/
void usb_Get_Descriptor() {
if ( bit_test(usb_ep0_rx_buffer[3],5) )// btfss BufferData+(wValue+1),5 ; get descriptor type
{usb_GetClassSpecificDescriptor();}
else
{usb_GetCh9Descriptor();}
}
/**************************************************************
/* usb_GetClassSpecificDescriptor()
/*
/* Input: usb_ep0_rx_buffer[3] == wValue, which descriptor we want
/*
/* Summary: Host asked for HID descriptor, wValue tells us if they want Descriptor or Report.
/* Only relevant if HID_DEVICE is defined as TRUE, else we return a Wrong-state (IDLE)
/*
/* Note: This code assumes that you only have 1 class desc/report, so it doesn't look at the index.
/*
/* Part of usb_isr_tok_setup_dne()
/* Part of Get_Descriptor request handler.
/***************************************************************/
void usb_GetClassSpecificDescriptor() {
#IF USB_HID_DEVICE
if (usb_ep0_rx_buffer[3]==0x21) {usb_Get_HID_Descriptor();}
else if (usb_ep0_rx_buffer[3]==0x22) {usb_Get_HID_Report();}
else {usb_wrongstate();}
#ELSE
usb_wrongstate();
#ENDIF
}
#IF USB_HID_DEVICE
/**************************************************************
/* usb_Get_HID_Descriptor()
/*
/* Output: USB_dev_req is changed to keep status of what request we are currently processing.
/*
/* Summary: Defines the beginning (String_ptr) and the end (String_end) of the descriptor
/* located in the CONFIG[] descriptor, and sends out the first packet containing the
/* first few bytes of the descriptor. The remaining bytes are sent after IN tokens.
/*
/* Part of usb_isr_tok_setup_dne()
/* Code compiled only if HID_DEVICE is defined as TRUE
/* Part of Get_Descriptor request handler.
/***************************************************************/
void usb_Get_HID_Descriptor() {
debug(debug_txb,"DH ");
USB_dev_req=GET_HID_DESC;
String_ptr=USB_CLASS_DESCRIPTORS[0];
String_end=String_ptr+USB_CONFIG_DESC[String_ptr];
usb_copy_config_seg_to_ep(0,DATA1);
}
/**************************************************************
/* usb_Get_HID_Report()
/*
/* Output: USB_dev_req is changed to keep status of what request we are currently processing.
/*
/* Summary: Defines the beginning (String_ptr) of the HID report and sends out the
/* first packet containing the first few bytes of the report. The remaining bytes
/* are sent after IN tokens.
/*
/* Part of usb_isr_tok_setup_dne()
/* Code compiled only if HID_DEVICE is defined as TRUE
/* Part of Get_Descriptor request handler.
/***************************************************************/
void usb_Get_HID_Report() {
debug(debug_txb,"HR ");
USB_dev_req=GET_HID_REP; //currently processing a get descriptor request
String_ptr=0;
usb_copy_hid_seg_to_ep(0,DATA1);
}
#ENDIF
/**************************************************************
/* usb_GetCh9Descriptor()
/*
/* Input: usb_ep0_rx_buffer[3] == wValue, which descriptor we want
/*
/* Summary: Checks to see if which standard descriptor we wanted (Interface, Endpoint, Config, Device, or String).
/*
/* Part of usb_isr_tok_setup_dne()
/* Part of Get_Descriptor request handler.
/***************************************************************/
void usb_GetCh9Descriptor() {
int type;
type=usb_ep0_rx_buffer[3] & 0x03;
if (type==USB_DEVICE_DESC_KEY) {usb_Get_Device_Descriptor();}
else if ( (type==USB_CONFIG_DESC_KEY) || (type==USB_INTERFACE_DESC_KEY) || (type==USB_ENDPOINT_DESC_KEY) ) {usb_Get_Config_Descriptor(type);}
else if (type==USB_STRING_DESC_KEY) {usb_Get_String_Descriptor();}
else {usb_wrongstate();}
}
/**************************************************************
/* usb_Get_Device_Descriptor()
/*
/* Output: USB_dev_req is changed to keep status of what request we are currently processing.
/*
/* Summary: Defines the beginning (String_ptr) of the Device descriptor and sends out the
/* first packet containing the first few bytes of the Device descriptor (located in device[]).
/* The remaining bytes are sent after IN tokens.
/*
/* Part of usb_isr_tok_setup_dne()
/* Part of Get_Descriptor request handler.
/***************************************************************/
void usb_Get_Device_Descriptor() {
debug(debug_txb,"DD ");
USB_dev_req=GET_DEVICE; //currently processing a get descriptor request
String_ptr=0;
usb_copy_device_seg_to_ep(0,DATA1);
}
/**************************************************************
/* usb_Get_Config_Descriptor(type)
/*
/* Input: type = which descriptor to send
/* usb_ep0_rx_buffer[2] == which index if multiple interfaces, configs, endpoints, etc.
/* Output: USB_dev_req is changed to keep status of what request we are currently processing.
/*
/* Summary: Defines the beginning (String_ptr) and end (String_end) of the specified config descriptor and sends out the
/* first packet containing the first few bytes of the config descriptor (located in config[]).
/* The remaining bytes are sent after IN tokens. Since config[] contains each config descriptor and
/* multiple indexes String_ptr and String_end must specify the beginning and end correctly.
/*
/* Part of usb_isr_tok_setup_dne()
/* Part of Get_Descriptor request handler.
/***************************************************************/
void usb_Get_Config_Descriptor(int type) {
int index;
debug(debug_txb,"DC");
USB_dev_req=GET_DESCRIPTOR; //currently processing a get descriptor request
index=usb_ep0_rx_buffer[2];
if (type==USB_CONFIG_DESC_KEY) { //windows hosts will send a FF max len and expect you to send all configs without asking for them individually.
debug_txb('C');
String_ptr=0;
if (host_max_length > USB_TOTAL_CONFIG_LEN) {host_max_length=USB_TOTAL_CONFIG_LEN;}
String_end=host_max_length;
}
//i've never seen windows ask for these two requests, but here they are anyways
//therefore these haven't been tested by CCS.
else if (type==USB_INTERFACE_DESC_KEY) {debug_txb('I'); String_ptr=USB_INTERFACE_DESCRIPTORS[index]; String_end=String_ptr+USB_CONFIG_DESC[String_ptr];}
else if (type==USB_ENDPOINT_DESC_KEY) {debug_txb('E'); String_ptr=USB_ENDPOINT_DESCRIPTORS[index]; String_end=String_ptr+USB_CONFIG_DESC[String_ptr];}
debug_txb(' ');
usb_copy_config_seg_to_ep(0,DATA1);
}
/**************************************************************
/* usb_Get_String_Descriptor()
/*
/* Input: usb_ep0_rx_buffer[2] == which index if multiple interfaces, configs, endpoints, etc.
/* Output: USB_dev_req is changed to keep status of what request we are currently processing.
/*
/* Summary: Defines the asked string (string) and end (String_end) of the specified string descriptor and sends out the
/* first packet containing the first few bytes of the string descriptor (located in STRINGx[]).
/* The remaining bytes are sent after IN tokens.
/*
/* Part of usb_isr_tok_setup_dne()
/* Part of Get_Descriptor request handler.
/***************************************************************/
void usb_Get_String_Descriptor() {
debug(debug_txb,"DS ");
USB_dev_req=GET_STRING_DESCRIPTOR;
string=usb_ep0_rx_buffer[2]; //find string index to look up
String_ptr=0;
usb_copy_string_seg_to_ep(0,DATA1);
}
/**************************************************************
/* usb_Get_Device_Status()
/*
/* Output: Status is put into usb_ep0_rx_buffer[], and usb_ep0_rx_buffer[] is sent to host.
/*
/* Summary: Handles Get_Status request where recipient is device.
/*
/* Part of usb_isr_tok_setup_dne()
/***************************************************************/
void usb_Get_Device_Status() {
debug(debug_txb,"GDS ");
usb_ep0_rx_buffer[0]=USB_status_device;
usb_ep0_rx_buffer[1]=0;
usb_put_packet(0,usb_ep0_rx_buffer,2,DATA1);
}
/**************************************************************
/* usb_Get_Interface_Status()
/*
/* Summary: Handles Get_Status request where recipient is interface.
/* A do nothing response. Always returns a two byte record, with all bits zero.
/*
/* Part of usb_isr_tok_setup_dne()
/***************************************************************/
void usb_Get_Interface_Status() {
debug(debug_txb,"GIS ");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -