I'm uncertain about how exactly a control request has to be finished. I
am using libusb to send a control request to my board:
int iResult = usb_control_msg(_ptUsbHandle,
USB_ENDPOINT_IN | USB_TYPE_CLASS | USB_RECIP_DEVICE,
CMD_GET,
0, // value
0, // index
cBuf, // destination*
2, // size
500); // timeout
This control request reads a status. The board should return the status
as a data packet that consists of the CMD that was issued (CMD_GET) and
the status byte. On the board side, I now have this:
char buf[2];
buf[0] = CMD_GET; // return in byte 0 the command that was issued
buf[1] = status;
Endpoint_ClearSETUP();
Endpoint_Write_Control_Stream_LE(buf, USB_ControlRequest.wLength);
Endpoint_ClearOUT();
which works without errors. I basically used the code in the low-level
mouse demo EVENT_USB_Device_ControlRequest(), case HID_REQ_GetReport,
and adopted it to my needs. However, it is - from the host's point of
view - an IN operation, so shouldn't I need to use ClearIN()? I tried
that, and got an error on the host side (usb_control_msg(...) returns
zero, which means zero bytes read).
Reading the low level mouse demo, I don't quite understand how he
different types of control requests and the LUFA functions come
together. There are (in EVENT_USB_Device_ControlRequest())
case HID_REQ_GetReport: <- "get"
Endpoint_ClearSETUP()
Endpoint_Write_Control_Stream_LE(...)
Endpoint_ClearOUT()
case HID_REQ_GetProtocol: <- again, "get"
Endpoint_ClearSETUP()
Endpoint_Write_8 <- again the device writes to the ctrl endpoint
Endpoint_ClearIN() <- why now that one?
Endpoint_ClearStatusStage() <- and why is this added?
case HID_REQ_SetProtocol:
Endpoint_ClearSETUP()
Endpoint_ClearStatusStage()
case HID_REQ_SetIdle:
same as HID_REQ_SetProtocol
case HID_REQ_GetIdle:
same as HID_REQ_GetProtocol
I was curious about what
Endpoint_ClearSETUP();
Endpoint_Write_Control_Stream_LE(buf, USB_ControlRequest.wLength);
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
would result in, and got no error and the desired behavior. I'm puzzled!
My impression is that ClearIN() and ClearStatusStage() have the same
effect as ClearOUT(), but I'm sure I missed something important about
these functions.
Regards
Christoph
thank you for your explanation! Together with the USB spec and "USB in a
nutshell" from beyondlogic.org, I have a bigger picture now. I've read
your explanation and the source code side by side, which worked pretty
well. It is now clear to me how the different Clear...() functions can
replace each other.
Should I write a tutorial about this? Do you think anyone would be
interested?
Regards
Christoph