?? chap_9.c
字號:
ENABLE();
single_transmit(0, 0); /* 返回一個空包 return an empty packet */
}
else if (bRecipient == USB_RECIPIENT_ENDPOINT /* 請求端點 request endpoint */
&& ControlData.DeviceRequest.wValue == USB_FEATURE_ENDPOINT_STALL)
{
endp = (INT8U)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);
if (ControlData.DeviceRequest.wIndex & (INT8U)USB_ENDPOINT_DIRECTION_MASK)
USB_SetEndpointStatus(endp * 2 + 1, 1); /* IN 端點被禁止 the IN endpoint is stalled */
else
USB_SetEndpointStatus(endp * 2, 1); /* OUT端點被禁止 the OUT endpoint is stalled */
single_transmit(0, 0); /* 返回一個空包 return an empty packet */
} else
stall_ep0(); /* 非標準請求,禁止邏輯端點0 not the request, stall logical endpoint 0 */
}
/***************************************************************************************************************
** 函數名稱: set_address() Name: set_address()
** 功能描述: 設置地址 Function: set the address of the USB device
****************************************************************************************************************/
void set_address(void)
{
USB_SetAddressEnable((INT8U)(ControlData.DeviceRequest.wValue &
DEVICE_ADDRESS_MASK), 1);
single_transmit(0, 0); /* 返回一個空包 return an empty packet */
}
/***************************************************************************************************************
** 函數名稱: get_descriptor() Name: get_descriptor()
** 功能描述: 獲取描述符 Function: get the descriptor of the USB device
****************************************************************************************************************/
void get_descriptor(void)
{
/* get the type of descriptor */ /* 取得描述符類型 */
INT8U bDescriptor = MSB(ControlData.DeviceRequest.wValue);
if (bDescriptor == USB_DEVICE_DESCRIPTOR_TYPE)
{ /* get the device descriptor */ /* 獲取設備描述符 */
code_transmit((INT8U *)&DeviceDescr, sizeof(USB_DEVICE_DESCRIPTOR));
}
else if (bDescriptor == USB_CONFIGURATION_DESCRIPTOR_TYPE)
{ /* get other descriptors */ /* 獲取其它描述符 */
if (ControlData.DeviceRequest.wLength > CONFIG_DESCRIPTOR_LENGTH)
ControlData.DeviceRequest.wLength = CONFIG_DESCRIPTOR_LENGTH;
/* transmit content of the descripotrs */ /* 傳輸描述符內容 */
code_transmit((INT8U *)&(usb_descr.ConfigDescr), ControlData.DeviceRequest.wLength);
}else /* no the descriptor type, stall logical endpoint 0 */
stall_ep0(); /* 沒有要求的描述符,禁止邏輯端點0 */
}
/***************************************************************************************************************
** 函數名稱: get_configuration() Name: get_configuration()
** 功能描述: 獲取配置 Function: get the configuration value of the USB device
****************************************************************************************************************/
void get_configuration(void)
{
INT8U c = bEPPflags.bits.configuration; /* 取得配置值 get the configuration value */
single_transmit(&c, 1); /* 傳輸配置值 transmit configuration value */
}
/***************************************************************************************************************
** 函數名稱: set_configuration() Name: set_configuration()
** 功能描述: 設置配置 Function: set the configuration value of the USB device
****************************************************************************************************************/
void set_configuration(void)
{
if (ControlData.DeviceRequest.wValue == 0)
{ /* the recieved data is error */
single_transmit(0, 0); /* 傳輸一個空包 transmit an empty packet */
DISABLE();
bEPPflags.bits.configuration = 0; /* 標識設備未配置 flag the device not configured */
ENABLE();
init_unconfig(); /* 禁止除0外的所有邏輯端點 disable all endpoint except logical endpoint 0 */
}
else if (ControlData.DeviceRequest.wValue == 1)
{ /* configure the device */
single_transmit(0, 0); /* 傳輸一個空包 transmit an empty packet */
init_unconfig(); /* 禁止除0外的所有邏輯端點 disable all endpoint except logical endpoint 0 */
init_config(); /* 使能全部端點 enable all endpoint */
DISABLE();
bEPPflags.bits.configuration = 1; /* 標識設備已被配置 flag the device configured */
ENABLE();
} else /* no the descriptor type, stall logical endpoint 0 */
stall_ep0(); /* 沒有要求的描述符,禁止邏輯端點0 */
}
/***************************************************************************************************************
** 函數名稱: get_interface() Name : get_interface()
** 功能描述: 獲取接口信息 Function : get the information of the interface
****************************************************************************************************************/
void get_interface(void)
{
INT8U txdat = 0; /* 只有一個接口 there only is a interface */
single_transmit(&txdat, 1); /* 傳輸一個字節 transmit a byte */
}
/***************************************************************************************************************
** 函數名稱: set_interface() Name: set_interface()
** 功能描述: 設置接口信息 Function: set the information of the interface
****************************************************************************************************************/
void set_interface(void)
{
if (ControlData.DeviceRequest.wValue == 0 && ControlData.DeviceRequest.wIndex == 0)
single_transmit(0, 0); /* 返回一個空包,表示執行成功 return an empty packet indicate perform sucessfully */
else
stall_ep0(); /* 沒有要求的描述符,禁止邏輯端點0 no the request, stall logical endpoint 0 */
}
/***************************************************************************************************************
** 函數名稱: control_handler() Name : control_handler()
** 功能描述: 控制傳輸 Function : deal with control transfer
****************************************************************************************************************/
void control_handler()
{
INT8U type, req;
type = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_TYPE_MASK;
/* 讀請求類型碼 read request type code */
req = ControlData.DeviceRequest.bRequest & USB_REQUEST_MASK;
if (type == USB_STANDARD_REQUEST)
(*StandardDeviceRequest[req])(); /* 標準請求 standard request */
//else if (type == USB_VENDOR_REQUEST) /* 廠商請求 vendor request */
// (*VendorDeviceRequest[req])();
//else if(type == USB_CLASS_REQUEST)
// (*ClassDeviceRequest[req])(); /* 類請求 class request */
else
stall_ep0(); /* 沒有要求的描述符,禁止邏輯端點0 no the request, stall logical endpoint 0 */
}
/***************************************************************************************************************
** 函數名稱: ep0_rxdone() Name : ep0_rxdone()
** 功能描述: 通過端點索引 0 接收數據 Function : receive data by logic endpoint 0
****************************************************************************************************************/
void ep0_rxdone(void)
{
INT8U ep_last, i;
INT8U req[sizeof(DEVICE_REQUEST)];
ep_last = USB_SelectClrIntEndpoint(0); /* 清除該端點的中斷 clear the interrupt of the endpoint */
if (ep_last & USB_SETUPPACKET)
{ /* 接收到SETUP包 if receive SETUP packet */
ControlData.wLength = 0;
ControlData.wCount = 0;
if(USB_ReadEndpoint(0, sizeof(ControlData.DeviceRequest),req)
!= sizeof(DEVICE_REQUEST)) /* 從端點 0 讀取數據 read data from endpoint 0 */
{
USB_SetEndpointStatus(0, 1); /* 禁止控制端點0 stall control endpoint 0 */
USB_SetEndpointStatus(1, 1); /* 禁止控制端點1 stall control endpoint 1 */
bEPPflags.bits.control_state = USB_IDLE; /* 標識空閑狀態 flag Idle status */
return;
}
/****** receive SETUP packet sucessfully 接收 SETUP 包成功 ******/
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];
ControlData.wLength = ControlData.DeviceRequest.wLength;
if (ControlData.DeviceRequest.bmRequestType & (INT8U)USB_ENDPOINT_DIRECTION_MASK)
{ /* 如果為控制讀取 if it is control read */
OSSemPost(pSetup_Event); /* 通知contorl_handler()處理SETUP包 inform control_handler() to process SETUP packet */
bEPPflags.bits.control_state = USB_TRANSMIT;
}
else
{ /* 如果為控制寫 if it is control write */
if (ControlData.DeviceRequest.wLength == 0)
{
OSSemPost(pSetup_Event); /* 通知contorl_handler()處理SETUP包 inform control_handler() to process SETUP packet */
bEPPflags.bits.control_state = USB_IDLE;
}
else
{
if (ControlData.DeviceRequest.wLength > MAX_CONTROLDATA_SIZE)
{ /* data length is error */
bEPPflags.bits.control_state = USB_IDLE;
USB_SetEndpointStatus(0, 1); /* 禁止控制端點0 stall control endpoint 0 */
USB_SetEndpointStatus(1, 1); /* 禁止控制端點1 stall control endpoint 1 */
}
else
bEPPflags.bits.control_state = USB_RECEIVE; /* 標識接收狀態 flag receive status */
} // set command with data
} // else set command
} // if setup packet
/****** control receive data phase 下面為控制接收數據階段 *******/
else if (bEPPflags.bits.control_state == USB_RECEIVE)
{ /* 如果當前為接收狀態 if it is receive status */
i = USB_ReadEndpoint(0, EP0_PACKET_SIZE,
ControlData.dataBuffer + ControlData.wCount); /* 從端點 0 讀取數據 read from endpoint 0 */
ControlData.wCount += i;
if( i != EP0_PACKET_SIZE || ControlData.wCount >= ControlData.wLength)
{ /* 完成接收數據 finish receiving data */
OSSemPost(pSetup_Event); /* 通知contorl_handler()處理SETUP包 inform control_handler() to process SETUP packet */
bEPPflags.bits.control_state = USB_IDLE;
}
}
else
bEPPflags.bits.control_state = USB_IDLE;
}
/***************************************************************************************************************
** 函數名稱: ep0_txdone() Name : ep0_txdone()
** 功能描述: 通過物理端點 0 發送數據 Function : tranmit data by physical endpoint 1
****************************************************************************************************************/
void ep0_txdone(void)
{
INT16 i = ControlData.wLength - ControlData.wCount;
USB_SelectClrIntEndpoint(1); /* 清除端點中斷標志 clear the endpoint interrupt flag bit */
if (bEPPflags.bits.control_state != USB_TRANSMIT)
{ /* 非發送狀態 not transmit status */
single_transmit(0, 0);
return; /* 直接返回 return */
}
if( i >= EP0_PACKET_SIZE)
{ /* 未發送字節數大于64 the byte length is above 64 */
USB_WriteEndpoint(1, EP0_PACKET_SIZE, ControlData.pData + ControlData.wCount);
ControlData.wCount += EP0_PACKET_SIZE;
bEPPflags.bits.control_state = USB_TRANSMIT;
}
else if( i != 0)
{ /* 發送所有未發送的字節 send all bytes */
USB_WriteEndpoint(1, i, ControlData.pData + ControlData.wCount);
ControlData.wCount += i;
bEPPflags.bits.control_state = USB_IDLE; /* 置狀態為等待狀態 set Idle status */
}
else if (i == 0){
USB_WriteEndpoint(1, 0, 0); /* 完成發送,再發送一個空包 finish transmitting, send an empty packet */
bEPPflags.bits.control_state = USB_IDLE;
}
}
/*******************************************************************************************************
** End Of File
********************************************************************************************************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -