Page 1 of 1

Vendor Defined HID - command and response

Posted: Mon Nov 06, 2017 12:02 pm
by uthrakumar
Hi Everyone,
We have added the vendor defined HID profile with the UAC2.0 audio reference design. Our requirement is to respond to every command received in the HID and will not provide any response unless it is processed. Below is the code snipped I am using to receive the HID data , process and respond back in case of GET and SET HID report in HID class requests

case HID_GET_REPORT:
// Not - Used
/* Mandatory. Allows sending of report over control pipe */
result = XUD_DoGetRequest(ep0_out, ep0_in, (buffer, unsigned char []), 64, sp.wLength);
break;

case HID_SET_REPORT:

if(sp.bmRequestType.Direction == USB_BM_REQTYPE_DIRECTION_H2D)
{
// Host to device - Copy data into the buffer
if (sp.wLength)
result = XUD_GetBuffer(ep0_out, (buffer_hid, unsigned char[]), datalength_hid);

}

//Acknowlwdge set request
if(result == XUD_RES_OKAY)
result = XUD_DoSetRequestStatus(ep0_in);

if(result == XUD_RES_OKAY)
{
//command Parser
cmdsParser();

//Initiate response
result = XUD_SetReady_In(ep_hid_in,buffer_hid,64);

// Wait till data is sent
while(1)
{
select
{
case XUD_SetData_Select ( c_hid , ep_hid_in , result ):
return result;
break ;
}
}

}
break;



The problem I am seeing the PC gets a HID response before even we process it. Meaning the command process takes roughly 10ms but PC receives a response with all zeros immediately. If I read the HID Packet after 10 ms I get the actual response.
Any thought on how it make it synchronous such that I get HID response only after the processing is complete.

Many Thanks for any help in this regards.

Thanks
Uthrakumar

Re: Vendor Defined HID - command and response

Posted: Wed Nov 08, 2017 10:56 am
by infiniteimprobability
Hi,
USB is host centric meaning that it will poll the device periodically for data (all transfers are initiated by host). So the device has to respond with either data or a NAK..
See http://www.beyondlogic.org/usbnutshell/usb4.shtml#Bulk
" If the token was received correctly, the function can either reply with a DATA packet containing the bulk data to be sent, or a stall packet indicating the endpoint has had a error or a NAK packet indicating to the host that the endpoint is working, but temporary has no data to send."
From https://www.xmos.com/download/private/X ... 0.a%29.pdf it says that the device side (XUD) behaves as following:
" Cores must be ready to communicate with the XUD library whenever the host demands its attention. If not, the XUD library will NAK."
So it sounds like you should be able to put your blocking code in the HID_GET_REPORT case and the host will be NAKed until it gets it's response.

One other option is to keep a shadow of the read value in your app and only accept it as valid if it has changed.

Re: Vendor Defined HID - command and response

Posted: Fri Nov 10, 2017 12:09 pm
by uthrakumar
Hi infiniteimprobability,


Thank you for pointing me in the right direction. Yes what you mentioned is what is happening with my app. Below is the snap of USB analyzer.

Image

Here I can see that USB receives a NAK for further requests sent from a PC Since it is working on the command received prior. Is there any way to avoid NAK in the XUD manager.

On a side note I was checking with an MSP430 micro controller [existing setup] with the same scenerio,I see that I am not receiving a NAK and so the PC USB driver waits and makes it as a synchronous communication.

Many many thanks in advance.

Thanks
Uthrakumar