Author Topic: HID device. Can't read data  (Read 27191 times)

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: HID device. Can't read data
« Reply #15 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.

puppdn

  • Member
  • ***
  • Posts: 19
Re: HID device. Can't read data
« Reply #16 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.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: HID device. Can't read data
« Reply #17 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.

puppdn

  • Member
  • ***
  • Posts: 19
Re: HID device. Can't read data
« Reply #18 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.
« Last Edit: October 14, 2016, 07:44:24 am by puppdn »

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: HID device. Can't read data
« Reply #19 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.

puppdn

  • Member
  • ***
  • Posts: 19
Re: HID device. Can't read data
« Reply #20 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!)

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: HID device. Can't read data
« Reply #21 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

puppdn

  • Member
  • ***
  • Posts: 19
Re: HID device. Can't read data
« Reply #22 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..?

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: HID device. Can't read data
« Reply #23 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.

puppdn

  • Member
  • ***
  • Posts: 19
Re: HID device. Can't read data
« Reply #24 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?

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: HID device. Can't read data
« Reply #25 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.

puppdn

  • Member
  • ***
  • Posts: 19
Re: HID device. Can't read data
« Reply #26 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.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: HID device. Can't read data
« Reply #27 on: October 17, 2016, 03:33:47 pm »
Good luck!

puppdn

  • Member
  • ***
  • Posts: 19
Re: HID device. Can't read data
« Reply #28 on: October 18, 2016, 03:36:28 am »
Well, all worked! We got it! Thank you very much!

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: HID device. Can't read data
« Reply #29 on: October 18, 2016, 09:45:01 am »
Excellent, thanks for reporting back.