📄 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 */
bEPPflags.bits.setup_packet = 1; /* 通知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)
{
bEPPflags.bits.setup_packet = 1; /* 通知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 */
bEPPflags.bits.setup_packet = 1; /* 通知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 + -