📄 usb.c
字号:
for (i=0;i<USB_EP15_RX_SIZE;i++) {
usb_ep15_rx_buffer[i]=0;
}
usb_ep15_rx_status.rx=0;
usb_ep15_rx_status.ov=0;
#endif
String_ptr=0; //used for get_descriptor
String_end=0; //used for get_descriptor
USB_Curr_Config=0; //unconfigured device
USB_status_device=1; //previous state. init at none
USB_dev_req=NONE; //previous token request state. init at none
}
/**************************************************************
/* usb_isr_tkn_CheckVendor()
/*
/* Input:
/* Output:
/* Returns:
/* Calls:
/*
/* Summary:
/* These functions are the responsibility of the user to write if they
/* want them as they are application dependant
/***************************************************************/
void usb_isr_tkn_CheckVendor() {
}
///---------------------------------------------------------------///
/// Processing Message stages is the biggest portion of the ISR ///
///---------------------------------------------------------------///
/**************************************************************
/* usb_isr_tok_in_dne(endpoint)
/*
/* Input: endpoint - which endpoint we are processing a setup token. Should be 0.
/*
/* Summary: When receiving an IN token from the PC on endpoint 0 that means the
/* host is asking for a response from a setup token, or the next packet
/* from a currently processing token. (For example, a 24 byte descriptor
/* would require 3 IN tokens if the packet size is 8 bytes.) USB_dev_req
/* is a global variable that defines what setup token we are currently processing.
/*
/* Part of USB interrupt service routine.
/* Only checks endpoint 0.
/***************************************************************/
void usb_isr_tok_in_dne(int8 endpoint) {
if (endpoint==0) {
if (USB_dev_req == GET_DESCRIPTOR) {usb_copy_config_seg_to_ep(0,TOGGLE);} //check this, we are missing report descriptor?
else if (USB_dev_req == GET_STRING_DESCRIPTOR) {usb_copy_string_seg_to_ep(0,TOGGLE);}
else if (USB_dev_req == GET_DEVICE) {usb_copy_device_seg_to_ep(0,TOGGLE);}
#IF USB_HID_DEVICE
else if (USB_dev_req == GET_HID_DESC) {usb_copy_config_seg_to_ep(0,TOGGLE);}
else if (USB_dev_req == GET_HID_REP) {usb_copy_hid_seg_to_ep(0,TOGGLE);}
#ENDIF
else if (USB_dev_req == SET_ADDRESS) {usb_finish_set_address();}
else if (USB_dev_req == SET_FEATURE) {usb_put_packet(0,0,0,DATA1);}
else if (USB_dev_req == CLEAR_FEATURE) {usb_put_packet(0,0,0,DATA1);}
else if (USB_dev_req == SET_CONFIG) {usb_put_packet(0,0,0,DATA1);}
else if (USB_dev_req == SEND_0LEN) {usb_put_packet(0,0,0,DATA1); USB_dev_req=NONE;}
else if (USB_dev_req == SEND_0LEN_TGL) {usb_put_packet(0,0,0,TOGGLE); USB_dev_req=NONE;}
}
}
/**************************************************************
/* usb_isr_tok_out_dne(endpoint)
/*
/* Input: endpoint contains which endpoint we are receiving data.
/* This code doesn't allow reception of data from EP0. (Add later)
/*
/* Summary: Processes out tokens (out is respective of the host, so actualy incoming
/* to the pic), but not out setup tokens. Data is placed into a
/* a buffer if it is empty, and the rx flag is set. If the buffer
/* is not empty then the overrun bit of that EP status byte is set it.
/***************************************************************/
void usb_isr_tok_out_dne(int8 endpoint) {
#if USB_EP1_RX_SIZE
if (endpoint == 1) {
usb_ep1_rx_status.len=usb_get_packet(endpoint,usb_ep1_rx_buffer,USB_EP1_RX_SIZE);
if (usb_ep1_rx_status.rx == 1) {
usb_ep1_rx_status.ov = 1;
}
usb_ep1_rx_status.rx = 1;
}
#endif
#if USB_EP2_RX_SIZE
if (endpoint == 2) {
usb_ep2_rx_status.len=usb_get_packet(endpoint,usb_ep2_rx_buffer,USB_EP2_RX_SIZE);
if (usb_ep2_rx_status.rx == 1) {
usb_ep2_rx_status.ov = 1;
}
usb_ep2_rx_status.rx = 1;
}
#endif
#if USB_EP3_RX_SIZE
if (endpoint == 3) {
usb_ep3_rx_status.len=usb_get_packet(endpoint,usb_ep3_rx_buffer,USB_EP3_RX_SIZE);
if (usb_ep3_rx_status.rx == 1) {
usb_ep3_rx_status.ov = 1;
}
usb_ep3_rx_status.rx = 1;
}
#endif
#if USB_EP4_RX_SIZE
if (endpoint == 4) {
usb_ep4_rx_status.len=usb_get_packet(endpoint,usb_ep4_rx_buffer,USB_EP4_RX_SIZE);
if (usb_ep4_rx_status.rx == 1) {
usb_ep4_rx_status.ov = 1;
}
usb_ep4_rx_status.rx = 1;
}
#endif
#if USB_EP5_RX_SIZE
if (endpoint == 5) {
usb_ep5_rx_status.len=usb_get_packet(endpoint,usb_ep5_rx_buffer,USB_EP5_RX_SIZE);
if (usb_ep5_rx_status.rx == 1) {
usb_ep5_rx_status.ov = 1;
}
usb_ep5_rx_status.rx = 1;
}
#endif
#if USB_EP6_RX_SIZE
if (endpoint == 6) {
usb_ep6_rx_status.len=usb_get_packet(endpoint,usb_ep6_rx_buffer,USB_EP6_RX_SIZE);
if (usb_ep6_rx_status.rx == 1) {
usb_ep6_rx_status.ov = 1;
}
usb_ep6_rx_status.rx = 1;
}
#endif
#if USB_EP7_RX_SIZE
if (endpoint == 7) {
usb_ep7_rx_status.len=usb_get_packet(endpoint,usb_ep7_rx_buffer,USB_EP7_RX_SIZE);
if (usb_ep7_rx_status.rx == 1) {
usb_ep7_rx_status.ov = 1;
}
usb_ep7_rx_status.rx = 1;
}
#endif
#if USB_EP8_RX_SIZE
if (endpoint == 8) {
usb_ep8_rx_status.len=usb_get_packet(endpoint,usb_ep8_rx_buffer,USB_EP8_RX_SIZE);
if (usb_ep8_rx_status.rx == 1) {
usb_ep8_rx_status.ov = 1;
}
usb_ep8_rx_status.rx = 1;
}
#endif
#if USB_EP9_RX_SIZE
if (endpoint == 9) {
usb_ep9_rx_status.len=usb_get_packet(endpoint,usb_ep9_rx_buffer,USB_EP9_RX_SIZE);
if (usb_ep9_rx_status.rx == 1) {
usb_ep9_rx_status.ov = 1;
}
usb_ep9_rx_status.rx = 1;
}
#endif
#if USB_EP10_RX_SIZE
if (endpoint == 10) {
usb_ep10_rx_status.len=usb_get_packet(endpoint,usb_ep10_rx_buffer,USB_EP10_RX_SIZE);
if (usb_ep10_rx_status.rx == 1) {
usb_ep10_rx_status.ov = 1;
}
usb_ep10_rx_status.rx = 1;
}
#endif
#if USB_EP11_RX_SIZE
if (endpoint == 11) {
usb_ep11_rx_status.len=usb_get_packet(endpoint,usb_ep11_rx_buffer,USB_EP11_RX_SIZE);
if (usb_ep11_rx_status.rx == 1) {
usb_ep11_rx_status.ov = 1;
}
usb_ep11_rx_status.rx = 1;
}
#endif
#if USB_EP12_RX_SIZE
if (endpoint == 12) {
usb_ep12_rx_status.len=usb_get_packet(endpoint,usb_ep12_rx_buffer,USB_EP12_RX_SIZE);
if (usb_ep12_rx_status.rx == 1) {
usb_ep12_rx_status.ov = 1;
}
usb_ep12_rx_status.rx = 1;
}
#endif
#if USB_EP13_RX_SIZE
if (endpoint == 13) {
usb_ep13_rx_status.len=usb_get_packet(endpoint,usb_ep13_rx_buffer,USB_EP13_RX_SIZE);
if (usb_ep13_rx_status.rx == 1) {
usb_ep13_rx_status.ov = 1;
}
usb_ep13_rx_status.rx = 1;
}
#endif
#if USB_EP14_RX_SIZE
if (endpoint == 14) {
usb_ep14_rx_status.len=usb_get_packet(endpoint,usb_ep14_rx_buffer,USB_EP14_RX_SIZE);
if (usb_ep14_rx_status.rx == 1) {
usb_ep14_rx_status.ov = 1;
}
usb_ep14_rx_status.rx = 1;
}
#endif
#if USB_EP15_RX_SIZE
if (endpoint == 15) {
usb_ep15_rx_status.len=usb_get_packet(endpoint,usb_ep15_rx_buffer,USB_EP15_RX_SIZE);
if (usb_ep15_rx_status.rx == 1) {
usb_ep15_rx_status.ov = 1;
}
usb_ep15_rx_status.rx = 1;
}
#endif
}
//---- process setup message stage -----------//
////// HUGE - most of our code is to read setup messages ////
/**************************************************************
/* usb_isr_tok_setup_dne()
/*
/* Input: usb_ep0_rx_buffer[] contains the 8 bytes of the setup packet.
/*
/* Summary: This function is the start of code that handles the setup token.
/* We must handle all relevant requests, such as Set_Configuration, Get_Descriptor, etc.
/*
/* Note: The setup packet has this configuration:
/* usb_ep0_rx_buffer[8] now contains setup data packet, which has the following records
/* usb_ep0_rx_buffer[0]=bmRequestType; bit7 defines (0) host-to-device or (1) device-to-host
/* bit6-5 (00) usb standard request; (01) class request; (10) vendor request
/* bit4-0 requested directed to (0000) device; (0001) interface; (0010) endpoint; (0011) other element
/* usb_ep0_rx_buffer[1]=bRequest ; the request
/* usb_ep0_rx_buffer[2,3]=wValue ; a value which corresponds to request
/* usb_ep0_rx_buffer[4,5]=wIndex ; could correspond to interface or endpoint...
/* usb_ep0_rx_buffer[6,7]=wLength ; number of bytes in next data packet
/* for host-to-device, this exactly how many bytes in data packet
/* for device-to-host, this is the maximum bytes that can fit one packet
/***************************************************************/
void usb_isr_tok_setup_dne() {
debug(debug_txb, "S ");
if (usb_ep0_rx_buffer[7]>0) {host_max_length=0xFF;} else {host_max_length=usb_ep0_rx_buffer[6];} //the max length the host told us we can send, no more. we can send less
USB_dev_req=NONE; // clear the device request..
// if (usb_ep0_rx_buffer[0] == 0x00) {usb_isr_tkn_HostToDevice();}
if (usb_ep0_rx_buffer[0] == 0x01) {usb_isr_tkn_HostToInterface();}
else if (usb_ep0_rx_buffer[0] == 0x02) {usb_isr_tkn_HostToEndpoint();}
else if (usb_ep0_rx_buffer[0] == 0x80) {usb_isr_tkn_DeviceToHost();}
else if (usb_ep0_rx_buffer[0] == 0x81) {usb_isr_tkn_InterfaceToHost();}
else if (usb_ep0_rx_buffer[0] == 0x82) {usb_isr_tkn_EndpointToHost();}
#IF USB_HID_DEVICE
else if ((usb_ep0_rx_buffer[0] & 0x60) == 0x20) {usb_isr_tkn_ClassRequest();}
#ENDIF
else if ((usb_ep0_rx_buffer[0] & 0x60) == 0x40) {usb_isr_tkn_CheckVendor();}
else {usb_isr_tkn_HostToDevice();}//cuz he gave CheckForStandardRequest and HostToDevice the same label
}
/**************************************************************
/* usb_isr_tkn_HostToDevice()
/*
/* Input: usb_ep0_rx_buffer[1] == bRequest
/*
/* Summary: bmRequestType told us it was a Host to Device 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_HostToDevice() {
if (usb_ep0_rx_buffer[1]==0x01) {usb_Clear_Device_Feature();}
else if (usb_ep0_rx_buffer[1]==0x05) {usb_Set_Address_Token();}
else if (usb_ep0_rx_buffer[1]==0x09) {usb_Set_Configuration_Token();}
else if (usb_ep0_rx_buffer[1]==0x03) {usb_Set_Feature();}
else {usb_wrongstate();}
}
/**************************************************************
/* usb_isr_tkn_HostToInterface()
/*
/* Input: usb_ep0_rx_buffer[1] == bRequest
/*
/* Summary: bmRequestType told us it was a Host to Interface 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_HostToInterface() {
if (usb_ep0_rx_buffer[1]==0x01) {usb_Clear_Interface_Feature();}
else if (usb_ep0_rx_buffer[1]==0x0B) {usb_Set_Interface();}
else if (usb_ep0_rx_buffer[1]==0x03) {usb_Set_Interface_Feature();}
else {usb_wrongstate();}
}
/**************************************************************
/* usb_isr_tkn_HostToEndpoint()
/*
/* Input: usb_ep0_rx_buffer[1] == bRequest
/*
/* Summary: bmRequestType told us it was a Host to Endpoint 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_HostToEndpoint() {
if (usb_ep0_rx_buffer[1]==0x01) {usb_Clear_Endpoint_Feature();}
else if (usb_ep0_rx_buffer[1]==0x03) {usb_Set_Endpoint_Feature();}
else {usb_wrongstate();}
}
/**************************************************************
/* usb_isr_tkn_DeviceToHost()
/*
/* Input: usb_ep0_rx_buffer[1] == bRequest
/*
/* Summary: bmRequestType told us it was a Device 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_DeviceToHost() {
if (usb_ep0_rx_buffer[1]==0x08) {usb_Get_Configuration_Token();}
else if (usb_ep0_rx_buffer[1]==0x06) {usb_Get_Descriptor();}
else if (usb_ep0_rx_buffer[1]==0x00) {usb_Get_Device_Status();}
else {usb_wrongstate();}
}
/**************************************************************
/* usb_isr_tkn_InterfaceToHost()
/*
/* Input: usb_ep0_rx_buffer[1] == bRequest
/*
/* Summary: bmRequestType told us it was a Interface 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_InterfaceToHost() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -