📄 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); //返回一个空的数据表示执行完毕
} //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); //返回一个空的数据表示执行完毕
} else //indicate that transmit end by transmitting a empty packet
stall_ep0(); //没有该请求,返回STALL
} //the unknown request,transmit stall
/************************************************************************************************************************
** 函数名称: 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); //发送一个空的数据响应 reponse by transmitting a empty packet
}
/************************************************************************************************************************
** 函数名称: 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);
//发送描述符内容 transmit the content of descriptor
}else
stall_ep0(); //没有该请求,返回STALL the unknown request,transmit stall
}
/*************************************************************************************************************************
** 函数名称: 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); //发送配置值 transmit the configuration value
}
/************************************************************************************************************************
** 函数名称: 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) {
//配置值不对,设备进入未配置状态 the configuration value is error,device enter no configuration status
single_transmit(0, 0); //发向一个空包响应 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); //发向一个空包响应 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
}
/************************************************************************************************************************
** 函数名称: 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); //发送一个字节 transmit a byte
}
/************************************************************************************************************************
** 函数名称: 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); //回复一个空的数据表示执行完毕 respone by transmitting a empty packet
else
stall_ep0(); //没有该请求,返回STALL the unknown request,transmit stall
}
/***********************************************************************************************************************
** 函数名称: control_handler() Name: control_handler()
** 功能描述: 控制传输处理函数 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
}
/************************************************************************************************************************
** 函数名称: ep0_rxdone() Name: ep0_rxdone()
** 功能描述: 通过端点0接收数据 Function: receive datat via endpoint 0
** 输 入: 无 Input: NULL
** 输 出: 无 Output: NULL
************************************************************************************************************************/
void ep0_rxdone(void)
{
INT8U ep_last, i;
INT8U req[sizeof(DEVICE_REQUEST)]; //SETUP包接收缓冲区
ep_last = D13_GetEndpointStatusWInteruptClear(EPINDEX4EP0_CONTROL_OUT);
//清除控制OUT端点中断寄存器标志,取得该端点处理状态
//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个字节失败 read 8 bytes fail from control out
stall_ep0(); //停止端点0 stall endpoint 0
bEPPflags.bits.control_state = USB_IDLE; //设置为等待状态 set idle status
return;
}
/******* 以下语句通信中的解决大小端问题,使该函数与编译器无关 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); //清空接收缓冲区 clear receive buffer
D13_AcknowledgeSETUP(); //应答SETUP acknowledge SETUP
ControlData.wLength = ControlData.DeviceRequest.wLength; //取出要传输数据的总字节数
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; //设置为发送状态 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; //设置为等待状态 set idle status
}
else {
if (ControlData.DeviceRequest.wLength > MAX_CONTROLDATA_SIZE) {
//数据长度出错 the length of data is error
bEPPflags.bits.control_state = USB_IDLE; //设置为等待状态 set idele status
stall_ep0(); //停止端点0 stall endpoint 0
}
else
bEPPflags.bits.control_state = USB_RECEIVE; //设置为接收状态 set receive status
}// set command with data
}// else set command
}// if setup packet
/***** 下面为控制输出的数据阶段 data phase of control out ***************/
else if (bEPPflags.bits.control_state == USB_RECEIVE) { //如果为接收状态 receive status
i = D13_ReadEndpoint(0, EP0_PACKET_SIZE,
ControlData.dataBuffer + ControlData.wCount); //从端点0接收数据 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; //设置为等待状态 set idle status
}
}else
bEPPflags.bits.control_state = USB_IDLE; //设置等待状态 set idle status
D13_ClearBuffer(0); //清除缓冲区0 clear ep0 buffer
}
/************************************************************************************************************************
** 函数名称: ep0_txdone() Name: ep0_txdone()
** 功能描述: 通过端点索引 0 发送数据 Function: send data via endpoint 0
** 输 入: 无 Input: NULL
** 输 出: 无 Output: NULL
***********************************************************************************************************************/
void ep0_txdone(void)
{
INT16 i = ControlData.wLength - ControlData.wCount; //计算未发送的字节数 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){ //非发送状态 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) { //发送16个字节 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) { //发送所有字节 transmit all bytes
D13_WriteEndpoint(1, i, ControlData.pData + ControlData.wCount);
ControlData.wCount += i;
bEPPflags.bits.control_state = USB_IDLE; //置状态为等待状态 set idle status
}
else if (i == 0){
D13_WriteEndpoint(1, 0, 0); //发送完毕,发0个字节 transmit zero bytes
bEPPflags.bits.control_state = USB_IDLE; //置状态为等待状态 set idle status
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -