PORTS Forum

Ports and Interfaces => USB => Topic started by: puppdn on October 06, 2016, 09:04:54 am

Title: HID device. Can't read data
Post by: puppdn on October 06, 2016, 09:04:54 am
Hu Jan and everyone. I believe you're can help me.

Here is set of descriptors
Code: [Select]
const uint8_t HID_DeviceDescriptor[0x12] =   {
    0x12,         // overall descriptor length
    USB_DEVICE_DESCRIPTOR_TYPE, // bDescriptorType -  Device descriptor
    0x00, 0x02,                 // bcdUSB usb 2.0
// class, subclass
    0x00,                       //bDeviceClass
    0x00,                       //bDeviceSubClass
    0x00,                       //bDeviceProtocol
    0x40,                       //bMaxPacketSize - max size of packets
// vid pid
    0x25, 0x09,                 //idVendor
    0x37, 0x12,                 //idProduct
    0, 0x01,                 // bcdDevice rel. DEVICE_VER_H.DEVICE_VER_L 
    1,                          //Index of string descriptor describing manufacturer //old value 1
    2,                          //Index of string descriptor describing product //old value 2
    3,                          //Index of string descriptor describing the device serial number //old value 3
    0x01                        // bNumConfigurations - configurations.
};


const uint8_t HID_ConfigDescriptor[0x29] =  {//CustomHID_ConfigDescriptor
0x09,//config descr standart value
0x02,// Type (config descr)
0x22,//Totallength low half word (the length of the configuration descriptor, )
0x00,//Totallength hi half word (the interface descriptor, the HID descriptor, and one endpoint descriptor.)
0x01,//NumInterfaces This item defines the number of interface settings contained in this configuration.
0x01,//bConfigurationValue (Used in Get_Configuration and Set_Configuration to identify this configuration.)
0xB1,//iConfiguration (string index for a string that describes this configuration.) old 0x01
0x80,//bmAttributes
0x32,//MaxPower (in 2 mA units) (100ma)
0x09,// bLength
0x04,// bDescriptorType (Interface)
0x00,// bInterfaceNumber (only one interface)
0x00,// bAlternateSetting (for multiply interfaces)
0x01,// bNumEndpoints (only one EP)
0x03,// bInterfaceClass (3 = HID)
0x00,// bInterfaceSubClass
0x00,// bInterfaceProcotol
0xC1, // iInterface old 0x00
0x09, // bLength
0x21, // bDescriptorType
0x01,// bcdHID low
0x01,// bcdHID hi
0x00,// bCountryCode
0x01,//bNumDescriptors
0x22,// bDescriptorType - Hid report descriptor
Custom_HID_RepDesc_LEN_INOUT,// total length of report low//this is total length of report descriptor
0x0,// total length of report  hi
0x07,//EP descr
0x05,// bDescriptorType (EP)
0x01,//ep address and direction  addr=1 dir=OUT (old 0x81 addr=1 dir=IN)
0x03,//bmAttributes
0x0A,// MaxPacketSize (low)//old 2
0x00,// MaxPacketSize (hi)
10,// bInterval
0x07,//EP descr
0x05,// bDescriptorType (EP)
0x81,//ep address and direction  addr=1 dir=IN
0x03,//bmAttributes
0x0A,// MaxPacketSize (low)//old 2
0x00,// MaxPacketSize (hi)
10// bInterval
};

const uint8_t  Custom_HID_ReportDescriptorINOUT[Custom_HID_RepDesc_LEN_INOUT] = {
   0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
    0x09, 0x00,                    // USAGE (Undefined)
    0xa1, 0x01,                    // COLLECTION (Application)
    0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
    0x26, 0xff, 0x00,              //   LOGICAL_MAXIMUM (255)
    0x75, 0x08,                    //   REPORT_SIZE (8)
    0x95, 0x02,                    //   REPORT_COUNT (2)
    0x09, 0x00,                    //   USAGE (Undefined)
    0x81, 0x82,                    //   INPUT (Data,Var,Abs,Vol)
    0x95, 0x05,                    //   REPORT_COUNT (5)
    0x09, 0x00,                    //   USAGE (Undefined)
    0x91, 0x82,                    //   OUTPUT (Data,Var,Abs,Vol)
    0xc0                           //   END_COLLECTION
};//

My device recognized by Windows and I can send bytes:
Code: [Select]
HANDLE file=CreateFile(ptrDetail->DevicePath,GENERIC_READ|GENERIC_WRITE,
                                       FILE_SHARE_WRITE|FILE_SHARE_READ,
                                       (LPSECURITY_ATTRIBUTES)NULL,
                                       OPEN_EXISTING,
                                       FILE_ATTRIBUTE_NORMAL,
                                       0);


char arr[6] = {0x0,0x31,0x02,0xA1,0xA2,0x13};/

WriteFile(file,arr,6,&sended,NULL);

But I can't perform Readfile():
Code: [Select]
char rxArr[20];           
DWORD received;
memset(rxArr, 0, 20);
ReadFile(file,rxArr,3,&received,NULL);
Here program causing loop and I can't retrieve answer.
I.e. Reading not working.
If look at USB BUS using logic analyzer i see OUT packet when program perforw WriteFile() (PId, Address etc. all OK)
When host perform ReadFile() - NOTHING.
I think USB driver block my request.
Title: Re: HID device. Can't read data
Post by: Jan Axelson on October 06, 2016, 04:39:54 pm
I'm not sure what you mean by "program causing loop"?

Does ReadFile return an error code?

https://msdn.microsoft.com/en-us/library/windows/desktop/aa365467(v=vs.85).aspx

https://msdn.microsoft.com/en-us/library/windows/desktop/ms679360(v=vs.85).aspx



Title: Re: HID device. Can't read data
Post by: puppdn on October 06, 2016, 11:44:04 pm
I mean when my program run to readfile() func. (I'm use console app)  the cursor blink and program does not continue to next functions. Seems like readfile waiting for.. response from whatever
Title: Re: HID device. Can't read data
Post by: Jan Axelson on October 07, 2016, 09:35:09 am
If the IN endpoint returns NAKs in response to IN token packets, ReadFile will hang.

Are you using a hardware analyzer that will show IN token packets from the host and NAK responses from the device?

If not, use whatever debugging tools you have (monitor program, watch variables) to determine if the device is seeing IN token packets and if so, how it is responding to them.

 
Title: Re: HID device. Can't read data
Post by: puppdn on October 07, 2016, 11:07:44 am
Yes, I'm using Sales logic, that show all, that happens at usb bus. I can see enumeration process(setup, in transactions, sync, so and etc) If I perform writefile - all perfect, I see address, ep number, PID out, seeded data, ACK from mcu - all perfect. After that I perform readfile() and nothing I can see. No PID in, no address,  no data no acknowledgement - nothing.
Title: Re: HID device. Can't read data
Post by: Jan Axelson on October 07, 2016, 12:35:43 pm
The interface descriptor has:

0x01,// bNumEndpoints (only one EP)

Should be 0x02
Title: Re: HID device. Can't read data
Post by: puppdn on October 07, 2016, 03:23:46 pm
Quote
The interface descriptor has:

0x01,// bNumEndpoints (only one EP)

Should be 0x02

Hm... But i have 0-EP (Control) and 1-EP (Interrupt) (The two endpoints, okay). My device can transmit and receive trough 0-EP (100% worked), and i can only transmit to 1-EP (50% worked).
Here says:
https://msdn.microsoft.com/ru-ru/library/ff540065(v=vs.85).aspx (https://msdn.microsoft.com/ru-ru/library/ff540065(v=vs.85).aspx)

bNumEndpoints
The number of endpoints that are used by the interface, excluding the default status endpoint.


I will try change (at Monday) bNumEndpoint field to 0x02, but seems it's not helpful. Anyway thank you wery much for your replies. to get to work the device.
P.S. Sorry for my poor language  :)
Title: Re: HID device. Can't read data
Post by: Jan Axelson on October 07, 2016, 09:09:47 pm
bNumEndpoint is the number of endpoint descriptors in the interface, for example, endpoint 1 IN and endpoint 1 OUT.

The terminology used in the USB spec and other documentation is confusing and inconsistent.

Title: Re: HID device. Can't read data
Post by: puppdn on October 08, 2016, 02:53:19 am
Quote
The terminology used in the USB spec and other documentation is confusing and inconsistent.
I agree.
Title: Re: HID device. Can't read data
Post by: Jan Axelson on October 09, 2016, 02:08:39 pm
 :)
Title: Re: HID device. Can't read data
Post by: puppdn on October 10, 2016, 04:46:11 am
I tried to change bNumEndpoint, load firmware in device.  Windows perform all required requests, and then Windows show error, like device was not succeed installed. In device manager my device have yellow triangle and error code 10.

Update:
In usbview I can see only devuce descriptor, ep descriptor and connection status.
Title: Re: HID device. Can't read data
Post by: Jan Axelson on October 10, 2016, 10:07:04 am
To force Windows to forget the previous descriptors, with the device attached, open Windows Device Manager and uninstall the device (under Driver). Then reattach.
Title: Re: HID device. Can't read data
Post by: puppdn on October 10, 2016, 10:19:43 am
I'm just change PID of device in firmvare and OS perform installing device as new device.
In USBView presented:
Quote
Device Descriptor:
bcdUSB:             0x0200
bDeviceClass:         0x00
bDeviceSubClass:      0x00
bDeviceProtocol:      0x00
bMaxPacketSize0:      0x40 (64)
idVendor:           0x0925
idProduct:          0x1234
bcdDevice:          0x0100
iManufacturer:        0xA1
iProduct:             0xA2
iSerialNumber:        0xA3
bNumConfigurations:   0x01

ConnectionStatus: DeviceConnected
Current Config Value: 0x01
Device Bus Speed:     Full
Device Address:       0x04
Open Pipes:              1

Endpoint Descriptor:
bEndpointAddress:     0x81  IN
Transfer Type:   Interrupt
wMaxPacketSize:     0x000A (10)
bInterval:            0x0A
That's all. No interface descriptor, no configuration descriptor. But USBView must show all descriptors.
For example - mouse:
Quote
Device Descriptor:
bcdUSB:             0x0200
bDeviceClass:         0x00
bDeviceSubClass:      0x00
bDeviceProtocol:      0x00
bMaxPacketSize0:      0x08 (8 )
idVendor:           0x046D (Logitech Inc.)
idProduct:          0xC077
bcdDevice:          0x7200
iManufacturer:        0x01
0x0409: "Logitech"
iProduct:             0x02
0x0409: "USB Optical Mouse"
iSerialNumber:        0x00
bNumConfigurations:   0x01

ConnectionStatus: DeviceConnected
Current Config Value: 0x01
Device Bus Speed:     Low
Device Address:       0x02
Open Pipes:              1

Endpoint Descriptor:
bEndpointAddress:     0x81  IN
Transfer Type:   Interrupt
wMaxPacketSize:     0x0004 (4)
bInterval:            0x0A

Configuration Descriptor:
wTotalLength:       0x0022
bNumInterfaces:       0x01
bConfigurationValue:  0x01
iConfiguration:       0x00
bmAttributes:         0xA0 (Bus Powered Remote Wakeup)
MaxPower:             0x32 (100 Ma)

Interface Descriptor:
bInterfaceNumber:     0x00
bAlternateSetting:    0x00
bNumEndpoints:        0x01
bInterfaceClass:      0x03 (HID)
bInterfaceSubClass:   0x01
bInterfaceProtocol:   0x02
iInterface:           0x00

HID Descriptor:
bcdHID:             0x0111
bCountryCode:         0x00
bNumDescriptors:      0x01
bDescriptorType:      0x22
wDescriptorLength:  0x002E

Endpoint Descriptor:
bEndpointAddress:     0x81  IN
Transfer Type:   Interrupt
wMaxPacketSize:     0x0004 (4)
bInterval:            0x0A
Title: Re: HID device. Can't read data
Post by: Jan Axelson on October 10, 2016, 08:17:57 pm
What does the analyzer show during enumeration? Does the host read all of the descriptors and send a Set Configuration request with a value of 1 and does the device ACK the request?
Title: Re: HID device. Can't read data
Post by: puppdn on October 11, 2016, 11:05:38 am
Strange behaviour :
If I define only 1 OUT EP - everything ok.
If only 1 OUT EP -everything bad.
If I change ep address 0x81 -> 0x01, and in report descriptor
Quote
   const uint8_t  Custom_HID_ReportDescriptorINOUT[Custom_HID_RepDesc_LEN_INOUT] = {
      0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
    0x09, 0x00,                    // USAGE (Undefined)
    0xa1, 0x01,                    // COLLECTION (Application)
    0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
    0x26, 0xff, 0x00,              //   LOGICAL_MAXIMUM (255)
    0x75, 0x08,                    //   REPORT_SIZE (8)
    0x95, 0x02,                    //   REPORT_COUNT (2)
    0x09, 0x00,                    //   USAGE (Undefined)
    0x81, 0x82,                    //   INPUT (Data,Var,Abs,Vol)
    0xc0                           //   END_COLLECTION
};//
Change input ->output, according 0x81,0x82 ->0x91,0x82
Host sent requests, but device no respond.
Title: Re: HID device. Can't read data
Post by: Jan Axelson on October 12, 2016, 11:55:48 am
I would suggest stepping back from making experimental changes.

Use the descriptors that the device needs, one endpoint in each direction, bNumEndpoints = 2. After changing the descriptors, change the PID or uninstall and reattach. View enumeration in the protocol analzyer as I suggested earlier.
Title: Re: HID device. Can't read data
Post by: puppdn on October 13, 2016, 08:51:15 am
Well, i founded mistake in firmware. I was send shorter configuration descriptor than define.

Now i'm use following descriptors:
Code: [Select]
const uint8_t HID_DeviceDescriptor[HID_SIZ_DEVICE_DESC] =   {
          0x12,         // overall descriptor length
          0x01, // bDescriptorType -  Device descriptor
          0x00, 0x02,                 // bcdUSB usb 2.0
          0x00,                       //bDeviceClass
          0x00,                       //bDeviceSubClass
          0x00,                       //bDeviceProtocol
          0x40,                       //bMaxPacketSize - max size of packets
          0x83, 0x04,                 //idVendor
          0x3D, 0x22,                 //idProduct
          DEVICE_VER_L, DEVICE_VER_H,                 // bcdDevice rel. DEVICE_VER_H.DEVICE_VER_L 
          HID_iMan_STRING,                          //Index of string descriptor describing manufacturer
          HID_iProd_STRING,                          //Index of string descriptor describing product
          HID_iSN_STRING,                          //Index of string descriptor describing the device serial number
          0x01                        // bNumConfigurations - configurations.
  };

Config. descriptor.
Code: [Select]
#define Custom_HID_RepDesc_LEN_INOUT 26
#define HID_SIZ_CONFIG_DESCsecond 41

const uint8_t HID_ConfigDescriptor[HID_SIZ_CONFIG_DESCsecond] =  {
   0x09,//config descr standart value
   0x02,// Type (config descr)
   HID_SIZ_CONFIG_DESCsecond,//Total length low half word (the length of the configuration descriptor, )
   0x00,//Totallength hi half word (the interface descriptor, the HID descriptor, and one endpoint descriptor.)
   0x01,//NumInterfaces This item defines the number of interface settings contained in this configuration.
   0x01,//bConfigurationValue (Used in Get_Configuration and Set_Configuration to identify this configuration.)
   0xB1,//iConfiguration (string index for a string that describes this configuration.) old 0x01
   0x80,//bmAttributes
   0x32,//MaxPower (in 2 mA units) (100ma)
      0x09,// bLength
      0x04,// bDescriptorType (Interface)
      0x00,// bInterfaceNumber (only one interface)
      0x00,// bAlternateSetting (for multiply interfaces)
      0x02,// bNumEndpoints (two EP)
      0x03,// bInterfaceClass (3 = HID)
      0x00,// bInterfaceSubClass
      0x00,// bInterfaceProcotol
      0xC1, // iInterface old 0x00
   0x09, // bLength
   0x21, // bDescriptorType
   0x01,// bcdHID low
   0x01,// bcdHID hi
   0x00,// bCountryCode
   0x01,//bNumDescriptors
   0x22,// bDescriptorType - Hid report descriptor
   Custom_HID_RepDesc_LEN_INOUT,// total length of report low//this is total length of report descriptor
   0x0,// total length of report  hi
      0x07,//EP descr
      0x05,// bDescriptorType (EP)
      0x81,//ep address and direction  addr=1 dir=OUT (old 0x81 addr=1 dir=IN)
      0x03,//bmAttributes
      0x0A,// MaxPacketSize (low)//old 2
      0x00,// MaxPacketSize (hi)
      10,// bInterval
      0x07,//EP descr
      0x05,// bDescriptorType (EP)
      0x01,//ep address and direction  addr=1 dir=IN
      0x03,//bmAttributes
      0x0A,// MaxPacketSize (low)//old 2
      0x00,// MaxPacketSize (hi)
      10// bInterval
};
And Report Descriptor
Code: [Select]
Custom_HID_ReportDescriptorINOUT[Custom_HID_RepDesc_LEN_INOUT] = {
    0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
    0x09, 0x00,                    // USAGE (Undefined)
    0xa1, 0x01,                    // COLLECTION (Application)
    0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
    0x26, 0xff, 0x00,              //   LOGICAL_MAXIMUM (255)
    0x75, 0x08,                    //   REPORT_SIZE (
    0x95, 0x02,                    //   REPORT_COUNT (2)
    0x09, 0x00,                    //   USAGE (Undefined)
    0x81, 0x82,                    //   INPUT (Data,Var,Abs,Vol)
    0x95, 0x05,                    //   REPORT_COUNT (5)
    0x09, 0x00,                    //   USAGE (Undefined)
    0x91, 0x82,                    //   OUTPUT (Data,Var,Abs,Vol)
    0xc0                     //   COLLECTION (Application)
};

At the end of enumeration host

Now in USB View:
Code: [Select]
Device Descriptor:
bcdUSB:             0x0200
bDeviceClass:         0x00
bDeviceSubClass:      0x00
bDeviceProtocol:      0x00
bMaxPacketSize0:      0x40 (64)
idVendor:           0x0483 (STMicroelectronics)
idProduct:          0x223D
bcdDevice:          0x0100
iManufacturer:        0xA1
iProduct:             0xA2
iSerialNumber:        0xA3
bNumConfigurations:   0x01

ConnectionStatus: DeviceConnected
Current Config Value: 0x01
Device Bus Speed:     Full
Device Address:       0x04
Open Pipes:              2

Endpoint Descriptor:
bEndpointAddress:     0x81  IN
Transfer Type:   Interrupt
wMaxPacketSize:     0x000A (10)
bInterval:            0x0A

Endpoint Descriptor:
bEndpointAddress:     0x01  OUT
Transfer Type:   Interrupt
wMaxPacketSize:     0x000A (10)
bInterval:            0x20
In the end of enumeration host performs IN request to addr=0x04 and ep=0x01. MCU succesfully return test array of bytes. Host ACKs. Later host perform RESET 3 times

If I try write data - Windows return error code 31. If i try write data again - writefile() does not return.
Quote
ERROR_GEN_FAILURE
31 (0x1F)
A device attached to the system is not functioning.

IF i disconnect device and connect again happens:
- host perform Setup request,
- device ACK,
- host again perform Setup packet,
- device not respond...
Thats-s repeats a few times, then host perform Reset. And all is repeted 3 times.
Title: Re: HID device. Can't read data
Post by: Jan Axelson on October 13, 2016, 09:16:56 pm
Quote
In the end of enumeration host performs IN request to addr=0x04 and ep=0x01. MCU succesfully return test array of bytes. Host ACKs. Later host perform RESET 3 times

Be sure the endpoint returns data or NAK in response to every IN token packet. If the endpoint doesn't respond at all, the host will reset the device in an attempt to restart communications.

Quote
If I try write data - Windows return error code 31. If i try write data again - writefile() does not return.
Quote

    ERROR_GEN_FAILURE
    31 (0x1F)
    A device attached to the system is not functioning.

See if the protocol analyzer shows the host attempting to send data to the endpoint and whether the endpoint ACKs or NAKs the data.

Quote
IF i disconnect device and connect again happens:
- host perform Setup request,
- device ACK,
- host again perform Setup packet,
- device not respond...
Thats-s repeats a few times, then host perform Reset. And all is repeted 3 times.

A device must always ACK the Setup stage.
Title: Re: HID device. Can't read data
Post by: puppdn on October 14, 2016, 06:59:18 am
Hi, Jan! Thank you for your patience :-)
Well, some time ago I fix firmware MCU.
Now in USB View:
Code: [Select]
Device Descriptor:
bcdUSB:             0x0200
bDeviceClass:         0x00
bDeviceSubClass:      0x00
bDeviceProtocol:      0x00
bMaxPacketSize0:      0x40 (64)
idVendor:           0x0483 (STMicroelectronics)
idProduct:          0x2231
bcdDevice:          0x0100
iManufacturer:        0xA1
0x0409: "ManN"
iProduct:             0xA2
0x0409: "Prodstr"
iSerialNumber:        0xA3
0x0409: "SN-2"
bNumConfigurations:   0x01

ConnectionStatus: DeviceConnected
Current Config Value: 0x01
Device Bus Speed:     Full
Device Address:       0x05
Open Pipes:              2

Endpoint Descriptor:
bEndpointAddress:     0x81  IN
Transfer Type:   Interrupt
wMaxPacketSize:     0x000A (10)
bInterval:            0x0A

Endpoint Descriptor:
bEndpointAddress:     0x01  OUT
Transfer Type:   Interrupt
wMaxPacketSize:     0x000A (10)
bInterval:            0x20

Configuration Descriptor:
wTotalLength:       0x0029
bNumInterfaces:       0x01
bConfigurationValue:  0x01
iConfiguration:       0xB1
bmAttributes:         0x80 (Bus Powered )
MaxPower:             0x32 (100 Ma)

Interface Descriptor:
bInterfaceNumber:     0x00
bAlternateSetting:    0x00
bNumEndpoints:        0x02
bInterfaceClass:      0x03 (HID)
bInterfaceSubClass:   0x00
bInterfaceProtocol:   0x00
iInterface:           0xC1

HID Descriptor:
bcdHID:             0x0101
bCountryCode:         0x00
bNumDescriptors:      0x01
bDescriptorType:      0x22
wDescriptorLength:  0x001A

Endpoint Descriptor:
bEndpointAddress:     0x81  IN
Transfer Type:   Interrupt
wMaxPacketSize:     0x000A (10)
bInterval:            0x0A

Endpoint Descriptor:
bEndpointAddress:     0x01  OUT
Transfer Type:   Interrupt
wMaxPacketSize:     0x000A (10)
bInterval:            0x20


Now host send Clear_Feature (clear STALL, right? But EP allready not STALLed) and i don't know what to do.
In your book says:
"p. 416, 4rd edition, Device Framework
..Verify that all bulk and interrupt endpoints can be halted and unhalted
with Set Feature and Clear Feature requests..."

On response at Clear_Feature i'm send Zero-length packet.
After that host perform request with PID IN token to EP1 and I send ZLP,
after that host once more  perform request with PID IN token to EP1 and I send 3 bytes (according with report descriptor) 0x00 0x00 0x43 (first defailt Packet ID last - data)
Code: [Select]
0x95, 0x02,                    //   REPORT_COUNT (2)After that host perform Reset. Then again host request Device descriptor, Reset, Set address and so on...


I think that at first host must send Set_Feature (set EP to STALL), chek that EP halted and then send Clear_Feature (set EP to Valid).
Seems i'm think wrong.
Title: Re: HID device. Can't read data
Post by: Jan Axelson on October 14, 2016, 10:30:58 am
Quote
host once more  perform request with PID IN token to EP1 and I send 3 bytes (according with report descriptor) 0x00 0x00 0x43 (first defailt Packet ID last - data)

When a HID uses only the default report ID, the report ID does not travel on the bus. The endpoint should send the report bytes only.

At the host side, the report ID is in the buffer that the application passes to the driver, but the driver doesn't send the report ID in write operations or expect to receive the report ID in read operations. That is why the driver is ignoring the Input reports; they don't have the expected number of bytes.
Title: Re: HID device. Can't read data
Post by: puppdn on October 14, 2016, 02:05:20 pm
Jan, thanks for reply.
So in other words:
- if I perform writefile() I must send "report count" +1 (6) bytes and that's is - Host sends only "report count" (5) bytes. It's clear.

It was unclear what I must reply for "Clear feature request"? Send any 2 bytes?

And if I use readfile() how many bytes I must specify as number of bytes to be read?

Looking forward to your replies. Best regards!)
Title: Re: HID device. Can't read data
Post by: Jan Axelson on October 14, 2016, 02:22:08 pm
Quote
- if I perform writefile() I must send "report count" +1 (6) bytes and that's is - Host sends only "report count" (5) bytes. It's clear.

yes

Quote
It was unclear what I must reply for "Clear feature request"? Send any 2 bytes?

zero length data

Quote
And if I use readfile() how many bytes I must specify as number of bytes to be read?

report length or greater will work
Title: Re: HID device. Can't read data
Post by: puppdn on October 14, 2016, 03:42:20 pm
Thank you for your reply.

Quote
>It was unclear what I must reply for "Clear >feature request"? Send any 2 bytes?

zero length data

I was tried to send ZLP, but host again send PID IN. I must send ZLP every time till..?
Title: Re: HID device. Can't read data
Post by: Jan Axelson on October 14, 2016, 10:18:55 pm
The Clear Feature request is to endpoint zero.

Respond with data or NAK to an IN token packet on EP1.
Title: Re: HID device. Can't read data
Post by: puppdn on October 17, 2016, 07:27:26 am
Thank you very much fo help Jan!  :) Now i can read and write data, but...
... there are strange things.
1. Host sends PID IN every 32ms,

Code: [Select]
0x07,//EP descr
0x05,// bDescriptorType (EP)
0x81,//ep address and direction  addr=1 dir=IN
0x03,//bmAttributes
0x0A,// MaxPacketSize (low)//old 2
0x00,// MaxPacketSize (hi)
50,// bInterval

But i specify 50.

2. On receive a FW of MCU switch contain of IN packet's.

Code: [Select]
if ((answ&0xFF)==0x31) {
*((int*)0x400060A0)=0x1234; //will be sended to host
}
else {
*((int*)0x400060A0)=0x5678; //will be sended to host
}

0x400060A0 - address of transmitter buffer
Number of bytes for transmit:
COUNT1TX=2;

Algorithm at host side:

Writefile(0x31)
Readfile()
Writefile(0x30)
Readfile()

First I write 0x31 to device and next read from device. I must receive 0x1234. But i receive 0x5678;
According when i write 0x30 to device and next read from device. I must receive 0x5678. But i receive 0x1237; Reversed data?
But, when I insert dummy read:

Why i must use dummy Read?
Writefile(0x31)
Readfile()//dummy read
Readfile()
Writefile(0x30)
Readfile()

seems all right.
 I think that's wrong way. Maybe RX USB buffer in host side is overflow and can't receive new data?
Title: Re: HID device. Can't read data
Post by: Jan Axelson on October 17, 2016, 11:15:06 am
1. bInterval is the maximum latency requested by the device. The host can use that value or anything smaller.

2. Configure the IN endpoint to return NAK. In the routine that handles data received at the OUT endpoint, configure the IN endpoint to return data. After sending data from the IN endpoint, configure the endpoint to return NAK again until more data is received on the OUT endpoint. Exactly how to do this will depend on your device architecture.
Title: Re: HID device. Can't read data
Post by: puppdn on October 17, 2016, 12:21:42 pm
1.  It's clear.
2. It's clear too. I'll try it! :-)
Thanks for help, Jan! I'll be back.
Title: Re: HID device. Can't read data
Post by: Jan Axelson on October 17, 2016, 03:33:47 pm
Good luck!
Title: Re: HID device. Can't read data
Post by: puppdn on October 18, 2016, 03:36:28 am
Well, all worked! We got it! Thank you very much!
Title: Re: HID device. Can't read data
Post by: Jan Axelson on October 18, 2016, 09:45:01 am
Excellent, thanks for reporting back.