?? chap_9.c
字號:
INT8U bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
//讀取請求類型 get the request type
if (bRecipient == USB_RECIPIENT_DEVICE //對設備請求 the request of device
&& ControlData.DeviceRequest.wValue == USB_FEATURE_REMOTE_WAKEUP) {
bEPPflags.bits.remote_wakeup = 1; //置1遠程喚醒標志 set the flag of wakeup remove
single_transmit(0, 0); //返回一個空的數(shù)據表示執(zhí)行完畢
} //indicate that transmit end by transmitting a empty packet
else if (bRecipient == USB_RECIPIENT_ENDPOINT //對端點請求 get request of endpoint
&& ControlData.DeviceRequest.wValue == USB_FEATURE_ENDPOINT_STALL) {
endp = (INT8U)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);
if ((ControlData.DeviceRequest.wIndex & (INT8U)USB_ENDPOINT_DIRECTION_MASK) == 0){
if (endp == 0)
endp = -1; //被請求的端點為控制OUT the requested endpoint is control OUT
}
D13_SetEndpointStatus(endp + 1,D13REG_EPSTS_STALL); //禁止端點 stall endpoint
single_transmit(0, 0); //返回一個空的數(shù)據表示執(zhí)行完畢
} else //indicate that transmit end by transmitting a empty packet
stall_ep0(); //沒有該請求,返回STALL
} //the unknown request,transmit stall
/************************************************************************************************************************
** 函數(shù)名稱: set_address() Name: set_address()
** 功能描述: 設置地址 Function: set address
** 輸 入: 無 Input: NULL
** 輸 出: 無 Output: NULL
************************************************************************************************************************/
void set_address(void)
{
D13_SetAddressEnable((INT8U)(ControlData.DeviceRequest.wValue &
DEVICE_ADDRESS_MASK), 1); //使能USB設備地址 enable USB device address
single_transmit(0, 0); //發(fā)送一個空的數(shù)據響應 reponse by transmitting a empty packet
}
/************************************************************************************************************************
** 函數(shù)名稱: get_descriptor() Name: get_descriptor()
** 功能描述: 獲取描述符 Function: get descriptor
** 輸 入: 無 Input: NULL
** 輸 出: 無 Output: NULL
*************************************************************************************************************************/
void get_descriptor(void)
{
INT8U bDescriptor = MSB(ControlData.DeviceRequest.wValue); //讀取請求的描述符類型 get the request descriptor type
if (bDescriptor == USB_DEVICE_DESCRIPTOR_TYPE){ //要獲取設備描述符 want to get the device descriptor
code_transmit((INT8U *)&DeviceDescr, sizeof(USB_DEVICE_DESCRIPTOR));
}else if (bDescriptor == USB_CONFIGURATION_DESCRIPTOR_TYPE) { //要獲取其它描述符 want to get other descriptor
if (ControlData.DeviceRequest.wLength > CONFIG_DESCRIPTOR_LENGTH){
ControlData.DeviceRequest.wLength = CONFIG_DESCRIPTOR_LENGTH;
}
code_transmit((INT8U *)&(usb_descr.ConfigDescr), ControlData.DeviceRequest.wLength);
//發(fā)送描述符內容 transmit the content of descriptor
}else
stall_ep0(); //沒有該請求,返回STALL the unknown request,transmit stall
}
/*************************************************************************************************************************
** 函數(shù)名稱: get_configuration() Name: get_configuration()
** 功能描述: 獲取USB事件的配置值 Function: get the configuration value of USB events
** 輸 入: 無 Input: NULL
** 輸 出: 無 Output: NULL
*************************************************************************************************************************/
void get_configuration(void)
{
INT8U c = bEPPflags.bits.configuration; //取出配置值 get the configuration value
single_transmit(&c, 1); //發(fā)送配置值 transmit the configuration value
}
/************************************************************************************************************************
** 函數(shù)名稱: set_configuration() Name: set_configuration()
** 功能描述: 設置USB事件的配置值 Function: set the configuration value of USB events
** 輸 入: 無 Input: NULL
** 輸 出: 無 Output: NULL
*************************************************************************************************************************/
void set_configuration(void)
{
if (ControlData.DeviceRequest.wValue == 0) {
//配置值不對,設備進入未配置狀態(tài) the configuration value is error,device enter no configuration status
single_transmit(0, 0); //發(fā)向一個空包響應 respone by transmitting a empty packet
bEPPflags.bits.configuration = 0; //標記未配置 mark the USB be not configurated
init_unconfig();
} else if (ControlData.DeviceRequest.wValue == 1) { //配置設備 configurate the device
single_transmit(0, 0); //發(fā)向一個空包響應 response by transmittign a empty packet
init_unconfig();
init_config();
bEPPflags.bits.configuration = 1; //標志已配置 mark the USB has be configurated
} else
stall_ep0(); //沒有該請求,返回STALL the unknown request,transmit stall
}
/************************************************************************************************************************
** 函數(shù)名稱: get_interface() Name: get_interface()
** 功能描述: 獲取接口信息 Function: get the information of interface
** 輸 入: 無 Input: NULL
** 輸 出: 無 Output: NULL
*************************************************************************************************************************/
void get_interface(void)
{
INT8U txdat = 0; //本設備只有一個接口 the device has only a interface
single_transmit(&txdat, 1); //發(fā)送一個字節(jié) transmit a byte
}
/************************************************************************************************************************
** 函數(shù)名稱: set_interface() Name: set_interface()
** 功能描述: 設置接口信息 Function: set the information of interface
** 輸 入: 無 Input: NULL
** 輸 出: 無 Output: NULL
************************************************************************************************************************/
void set_interface(void)
{
if (ControlData.DeviceRequest.wValue == 0 && ControlData.DeviceRequest.wIndex == 0)
single_transmit(0, 0); //回復一個空的數(shù)據表示執(zhí)行完畢 respone by transmitting a empty packet
else
stall_ep0(); //沒有該請求,返回STALL the unknown request,transmit stall
}
/***********************************************************************************************************************
** 函數(shù)名稱: control_handler() Name: control_handler()
** 功能描述: 控制傳輸處理函數(shù) Function: the function of dealing with control transfer
** 輸 入: 無 Input: NULL
** 輸 出: 無 Output: NULL
************************************************************************************************************************/
void control_handler(void)
{
INT8U type, req;
type = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_TYPE_MASK;
//讀取請求代碼 get the request code
req = ControlData.DeviceRequest.bRequest & USB_REQUEST_MASK;
if (type == USB_STANDARD_REQUEST)
(*StandardDeviceRequest[req])(); //標準請求處理 the normal request
//else if (type == USB_VENDOR_REQUEST) //廠商請求 the vector request
// (*VendorDeviceRequest[req])();
//else if(type == USB_CLASS_REQUEST)
// (*ClassDeviceRequest[req])(); //類請求,如大容量類 class request,example mass class
else
stall_ep0(); //無效請求,返回STALL the unknown request,transmit stall
}
/************************************************************************************************************************
** 函數(shù)名稱: ep0_rxdone() Name: ep0_rxdone()
** 功能描述: 通過端點0接收數(shù)據 Function: receive datat via endpoint 0
** 輸 入: 無 Input: NULL
** 輸 出: 無 Output: NULL
************************************************************************************************************************/
void ep0_rxdone(void)
{
INT8U ep_last, i;
INT8U req[sizeof(DEVICE_REQUEST)]; //SETUP包接收緩沖區(qū)
ep_last = D13_GetEndpointStatusWInteruptClear(EPINDEX4EP0_CONTROL_OUT);
//清除控制OUT端點中斷寄存器標志,取得該端點處理狀態(tài)
//clear interrupt register flag of control out,get the endpoint status
if (ep_last & D13REG_EPSTS_SETUP) { //如果收到了建立包(Setup包) if receive the SETUP packet
ControlData.wLength = 0;
ControlData.wCount = 0;
if(D13_ReadEndpoint(0, sizeof(ControlData.DeviceRequest),req)
!= sizeof(DEVICE_REQUEST) ) {
//從控制OUT端點讀8個字節(jié)失敗 read 8 bytes fail from control out
stall_ep0(); //停止端點0 stall endpoint 0
bEPPflags.bits.control_state = USB_IDLE; //設置為等待狀態(tài) set idle status
return;
}
/******* 以下語句通信中的解決大小端問題,使該函數(shù)與編譯器無關 resolve big-little endian ****/
ControlData.DeviceRequest.bmRequestType = req[0];
ControlData.DeviceRequest.bRequest = req[1];
ControlData.DeviceRequest.wValue = req[3] * 256 + req[2];
ControlData.DeviceRequest.wIndex = req[5] * 256 + req[4];
ControlData.DeviceRequest.wLength = req[7] * 256 + req[6];
/******** 接收建立包成功 **********/
D13_ClearBuffer(0); //清空接收緩沖區(qū) clear receive buffer
D13_AcknowledgeSETUP(); //應答SETUP acknowledge SETUP
ControlData.wLength = ControlData.DeviceRequest.wLength; //取出要傳輸數(shù)據的總字節(jié)數(shù)
ControlData.wCount = 0;
if (ControlData.DeviceRequest.bmRequestType & (INT8U)USB_ENDPOINT_DIRECTION_MASK) {
//如果是控制讀取 control read
OSSemPost(pSetup_Event); //通知control_handler()處理Setup包
//inform control_handler() deal with SETUP packet
bEPPflags.bits.control_state = USB_TRANSMIT; //設置為發(fā)送狀態(tài) set transmit status
}
else{ //如果是控制寫入 control write
if (ControlData.DeviceRequest.wLength == 0) {
OSSemPost(pSetup_Event); //通知control_handler()處理Setup包
//inform control_handle() deal with SETUP packet
bEPPflags.bits.control_state = USB_IDLE; //設置為等待狀態(tài) set idle status
}
else {
if (ControlData.DeviceRequest.wLength > MAX_CONTROLDATA_SIZE) {
//數(shù)據長度出錯 the length of data is error
bEPPflags.bits.control_state = USB_IDLE; //設置為等待狀態(tài) set idele status
stall_ep0(); //停止端點0 stall endpoint 0
}
else
bEPPflags.bits.control_state = USB_RECEIVE; //設置為接收狀態(tài) set receive status
}// set command with data
}// else set command
}// if setup packet
/***** 下面為控制輸出的數(shù)據階段 data phase of control out ***************/
else if (bEPPflags.bits.control_state == USB_RECEIVE) { //如果為接收狀態(tài) receive status
i = D13_ReadEndpoint(0, EP0_PACKET_SIZE,
ControlData.dataBuffer + ControlData.wCount); //從端點0接收數(shù)據 receive data from endpoint 0
ControlData.wCount += i;
if( i != EP0_PACKET_SIZE || ControlData.wCount >= ControlData.wLength) {
OSSemPost(pSetup_Event); //通知control_handler()處理Setup包
//inform control_handle() deal with SETUP packet
bEPPflags.bits.control_state = USB_IDLE; //設置為等待狀態(tài) set idle status
}
}else
bEPPflags.bits.control_state = USB_IDLE; //設置等待狀態(tài) set idle status
D13_ClearBuffer(0); //清除緩沖區(qū)0 clear ep0 buffer
}
/************************************************************************************************************************
** 函數(shù)名稱: ep0_txdone() Name: ep0_txdone()
** 功能描述: 通過端點索引 0 發(fā)送數(shù)據 Function: send data via endpoint 0
** 輸 入: 無 Input: NULL
** 輸 出: 無 Output: NULL
***********************************************************************************************************************/
void ep0_txdone(void)
{
INT16 i = ControlData.wLength - ControlData.wCount; //計算未發(fā)送的字節(jié)數(shù) calculate the bytes that will be transmitted
D13_GetEndpointStatusWInteruptClear(1); //清除中斷寄存器標志位 clear the interrupt flag of the interrupt register
if (bEPPflags.bits.control_state != USB_TRANSMIT){ //非發(fā)送狀態(tài) if it is not the status of transmit,
single_transmit(0, 0); //回復一個空包 respone by transmitting a empty packet
return; //返回 return
}
if( i >= EP0_PACKET_SIZE) { //發(fā)送16個字節(jié) transmit 16 bytes
D13_WriteEndpoint(1, EP0_PACKET_SIZE, ControlData.pData + ControlData.wCount);
ControlData.wCount += EP0_PACKET_SIZE;
bEPPflags.bits.control_state = USB_TRANSMIT;
}
else if( i != 0) { //發(fā)送所有字節(jié) transmit all bytes
D13_WriteEndpoint(1, i, ControlData.pData + ControlData.wCount);
ControlData.wCount += i;
bEPPflags.bits.control_state = USB_IDLE; //置狀態(tài)為等待狀態(tài) set idle status
}
else if (i == 0){
D13_WriteEndpoint(1, 0, 0); //發(fā)送完畢,發(fā)0個字節(jié) transmit zero bytes
bEPPflags.bits.control_state = USB_IDLE; //置狀態(tài)為等待狀態(tài) set idle status
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -