|
硬件:ATMEGA32U4,8MHZ
目标:通过CONTROL端口0,实现HID免驱通信
现上位机从GET REPORT接收到的数据是对的,经SET REPORT发给下位机数据不对。麻烦各位有经验的朋友看看什么问题。
代码如下:
static void USB_Read_Bytes(uint8 len_r , uint8 *pbuffp_r)
{
for (uint8 i=0;i<len_r;i++)
{
*(pbuffp_r+i) = Usb_read_byte();
}
}
static void USB_Write_Bytes(void)
{
for(uint8 i=0;i<len_w;i++)
{
Usb_write_byte(*(pbuffp_w + i));
}
Usb_send_control_in(); //UEINTX &= ~(1<<TXINI);
}
static void hid_set_report (void)
{
uint8 temp_len;
ZLP();
while(!(UEINTX & (1<<RXOUTI)));
if (USB_ControlRequest.wLength > sizeof(USB_HID_SET_BUFFER))
{
temp_len = sizeof(USB_HID_SET_BUFFER);
}
else
{
temp_len = (USB_ControlRequest.wLength & 0xFF);
}
USB_Read_Bytes(temp_len,USB_HID_SET_BUFFER);
Usb_ack_receive_out();//UEINTX &= ~(1<<RXOUTI)
ZLP();
}
static void hid_get_report(void)
{
if (USB_ControlRequest.wLength > sizeof(USB_HID_GET_BUFFER))
{
len_w = sizeof(USB_HID_GET_BUFFER);
}
else
{
len_w = (USB_ControlRequest.wLength & 0xFF);
}
pbuffp_w = USB_HID_GET_BUFFER;
USB_Write_Bytes();
}
static void usb_process_request(void)
{
USB_Read_Bytes(8,USB_READ_BUFFER);
USB_ControlRequest.bmRequestType = USB_READ_BUFFER[0];
USB_ControlRequest.bRequest = USB_READ_BUFFER[1];
USB_ControlRequest.wValue = USB_READ_BUFFER[3];
USB_ControlRequest.wValue = ((USB_ControlRequest.wValue << 8) | USB_READ_BUFFER[2]);
USB_ControlRequest.wIndex = USB_READ_BUFFER[5];
USB_ControlRequest.wIndex = ((USB_ControlRequest.wIndex << 8) | USB_READ_BUFFER[4]);
USB_ControlRequest.wLength = USB_READ_BUFFER[7];
USB_ControlRequest.wLength = ((USB_ControlRequest.wLength << 8) | USB_READ_BUFFER[6]);
Usb_ack_receive_setup(); //UEINTX &= ~(1<<RXSTPI)
if ((USB_ControlRequest.bmRequestType & 0x60) == Std_Requests)
{
switch (USB_ControlRequest.bRequest)
{
case GET_STATUS:
usb_get_status();
break;
case SET_ADDRESS:
usb_set_address();
break;
case GET_DESCRIPTOR:
usb_get_descriptor();
break;
case GET_CONFIGURATION:
usb_get_configuration();
break;
/*
case CLEAR_FEATURE:
case SET_FEATURE:
case SET_DESCRIPTOR:
case SET_CONFIGURATION:
case GET_INTERFACE:
case SET_INTERFACE:
case SYNCH_FRAME:*/
default:
ZLP();
break;
}
}
else
{
switch(USB_ControlRequest.bRequest)
{
case GET_REPORT:
hid_get_report();
break;
case SET_REPORT:
hid_set_report();
break;
/*case GET_IDLE:
case GET_PROTOCOL:
case SET_IDLE:
case SET_PROTOCOL:*/
default:
ZLP();
break;
}
}
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|