Replies: 42 comments
-
Ref: libusb project uses umockdev for Linux testing using a container. |
Beta Was this translation helpful? Give feedback.
-
The alternative: |
Beta Was this translation helpful? Give feedback.
-
I am not so sure if this is possible or not. |
Beta Was this translation helpful? Give feedback.
-
BTW, other than CI, it is also good to see if we have some known good HID device or FW to test HIDAPI. For libusb, here is the reference, it can be the same for USB HID device development. But for Bluetooth/BLE and even I2C/SPI HID device, we need different tools.
Other than vendor provided USB stacks (sometimes with proprietary licenses), there are also Opensource MCU USB stacks.
Just wondering what is the test device you are using to test HIDAPI? For me I usually use whatever HID device I have with me like the following for general test using hidtest.
For more real tests with hidapitester or simple test codes. I do not know much about the FW development other than simple modifications of the USB PIC USB codes and maybe a little bit with USB AVRs.
Then I use some HID devices to test hidapi indirectly using avrdude and OpenOCD (CMSIS DAP v1 compliant adapters). |
Beta Was this translation helpful? Give feedback.
-
Unfortunately I don't have any devices dedicated for testing, only a few of my peripherals, and a few devices from my employer (I'm under NDA not to disclose what those are), which have a few fixed input/output/interrupt reports, that I usually use for some sanity testing. |
Beta Was this translation helpful? Give feedback.
-
Yeah, would be great to have 1-2 FW samples for some well known simple/cheap device to be able to test most (all?) of the HIDAPI. |
Beta Was this translation helpful? Give feedback.
-
@todbot has some test firmware for hidapitester which can be used for hidapi testing as well.
TinyUSB node-hid has some examples as well. |
Beta Was this translation helpful? Give feedback.
-
Jan Axelson has quite some HID examples for things like Cypress EZ-USB FX2LP and Microchip USB PIC. |
Beta Was this translation helpful? Give feedback.
-
Simple test for Jan Axelson FX2HID example (0925:1234) -- loop back of Input/Output report (no report ID) using hidapitest. For this FW, I have to explicitly send output report ID 0.
Her USB PIC generic HID example has feature report loopback as well.
I have done the modification of the generic HID FW to add report ID (Input 1, Output 2, Feature OUT 3, Feature IN 4). I used that to test libusb Windows HID backend many year ago during the initial stage of the libusb Windows backend development.
|
Beta Was this translation helpful? Give feedback.
-
Simple test using hidapitester for the Microchip Simple HID demo (which is the base for the FW used in hidtest). I am using the mla version for the 8bit/16bit USB PICs.
|
Beta Was this translation helpful? Give feedback.
-
For libusb testing, I usually recommend Cypress EZ-USB FX3 (SuperSpeed). Then EZ-USB FX2LP (High-Speed). I think for HIDAPI testing, maybe FX3 is a bit overkill but you may want to get cheap EZ-USB FX2LP breakout from online marketplace like AliExpress at about US$5. For example, I have the following board. I just did a simple modification of Jan Axelson's fx2hid example to extend the Input/Output report size to 64 bytes.
Edit: take note that in order to develop high speed HID USB device using FX2LP with wMaxPacketSize > 64 Bytes, we can not use EP1IN or EP1OUT, as their buf size is only 64 bytes. We should probably use EP2 and EP6 (buffer size equals to 512 Bytes or 1024 Bytes). So the modification is not as simple. But if we just need to get report size >64 bytes, then it is still possible to use EP1 IN and EP1 OUT. We need to handle the fragamentation in firmware. |
Beta Was this translation helpful? Give feedback.
-
For EZ-USB FX2LP, previous examples are using Keil UV2 and Keil C51 toolchain (the FW is rather small so the free 4K version bundled will work). Lastes from Cypress (now part of Infineon) is using Eclipse based IDE with Open Source SDCC toolchain. Then there are quite some open source FW library available, using sdcc. |
Beta Was this translation helpful? Give feedback.
-
It should be a device, where you can program any HID ReportDescriptor, PhysicalDescriptor and StringDescriptor on low-level (byte-wise). Not something where you configure everything on high-level, because for testing it makes a difference how we order the items in the ReportDescriptor. |
Beta Was this translation helpful? Give feedback.
-
I believe most of the MCU development board will allow this, including the EZ-USB FX2LP (actually the example code from Jan Axelson uses assembly language for the descriptor, same for examples from Cypress). Unfortunately I am not a programmer so I can only carry out minor modification of existing FW codes. |
Beta Was this translation helpful? Give feedback.
-
I need to check but the following FX2LP FW seems to work, EP1 IN and EP1 OUT have wMacPacketSize = 64 bytes, but INPUT/OUTPUT report size are both 128 bytes.
fx2hid_128bytes_report.zip Similar thing for 512Bytes report.
|
Beta Was this translation helpful? Give feedback.
-
Hi @mcuee, As for why you need two descriptors in usb_hid.enable( (custom_device,) ) |
Beta Was this translation helpful? Give feedback.
-
Yes you are right. The following works.
Relevant discussion here. |
Beta Was this translation helpful? Give feedback.
-
I have had problems with 64-byte reports with report IDs. Not sure if this is a MCU buffer limitation (which is 64 bytes usually), an issue with hidapi, or something else. Here is a modified set of above code that does work, up to 63 byte reports: # rawhid_code.py
import time
import usb_hid
import adafruit_hid
custom_device = adafruit_hid.find_device(usb_hid.devices, usage_page=0xff00, usage=0x01)
print("custom_device: %04x %04x" % (custom_device.usage_page, custom_device.usage) )
while True:
out_report = custom_device.get_last_received_report(2) # out from computer
if out_report:
print("len:",len(out_report),["%02x" % x for x in out_report])
time.sleep(0.5)
print("sending copy on reportid 1")
in_report = bytearray(out_report) # copy in case we want to modify
custom_device.send_report(in_report, 1); # in to computer # rawhid_boot.py --
# any changes here only reflected after board reset
import usb_hid
REPORT_COUNT = 63 # size of report in bytes
CUSTOM_REPORT_DESCRIPTOR = bytes((
0x06, 0x00, 0xff, # Usage page (Vendor Defined Page1)
0x09, 0x01, # Usage (Vendor Page 1)
0xA1, 0x01, # Collection (Application)
0x85, 0x01, # Report ID (1)
0x09, 0x00, # Usage Page (Undefined)
0x15, 0x00, # Logical Minimum (0)
0x26, 0xFF, 0x00, # Logical Maximum (255)
0x75, 0x08, # Report Size (8 bits)
0x95, REPORT_COUNT, # Report Count (64 fields)
0x82, 0x02, 0x01, # Input (Data,Var,Abs,Buf)
0x85, 0x02, # Report ID (2)
0x09, 0x00, # Usage (Undefined)
0x09, 0x00, # Usage Page (Undefined)
0x15, 0x00, # Logical Minimum (0)
0x26, 0xFF, 0x00, # Logical Maximum (255)
0x75, 0x08, # Report Size (8 bits)
0x95, REPORT_COUNT, # Report Count (64 fields)
0x92, 0x02, 0x01, # Output (Data,Var,Abs,Buf)
0xC0, # End Collection
))
custom_device = usb_hid.Device(
report_descriptor=CUSTOM_REPORT_DESCRIPTOR,
usage_page=0xff00, # Vendor defined
usage=0x01, # Vendor page 1
report_ids=(1, 2),
in_report_lengths=(REPORT_COUNT,0),
out_report_lengths=(0, REPORT_COUNT),
)
usb_hid.enable( (custom_device,) ) And running hidapitester: ./hidapitester --usagePage 0xff00 --usage 1 --open -l 64 --send-output 2,3,4,5 --timeout 1000 --read-input 1
Opening device, vid/pid:0x0000/0x0000, usagePage/usage: FF00/1
Device opened
Writing output report of 64-bytes...wrote 64 bytes:
02 03 04 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Reading 64-byte input report 0, 1000 msec timeout...read 64 bytes:
01 03 04 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Closing device Note that hidapitester is given When run, the CircuitPython device outputs this on its REPL:
|
Beta Was this translation helpful? Give feedback.
-
Thanks a lot for the help. Interesting and this is exactly the same problem I have with my USB PIC using Jan Axelson's generic HID example as well. 63 bytes works but not 64 bytes. I think there are a few potential reasons.
This is one potential issue of hidapitester I would like to check with you and @Youw. I am thinking this should be worked-around inside hidapitest for Windows (report ID 0 needs to be in the buffer but not really appear in the USB BUS. That is probably why we need one more bytes of bufer than the length of report size. I believe this is not necessary for Linux and Windows. I could be wrong though. |
Beta Was this translation helpful? Give feedback.
-
This reflects how |
Beta Was this translation helpful? Give feedback.
-
Please take a look when you got the time. Thanks. |
Beta Was this translation helpful? Give feedback.
-
@Youw and @todbot and @JoergAtGithub I think we have gone through this report length previously. Maybe we have to revisit them. I think the following PR fixes the issue for reading back the input report and it is correct. I think we need to revisit for Feature report. I think the following commit is kind of correct if we look at from Windows driver point of view. But from a cross-platform API point of view, we may need to revist to align with Linux and macOS, which means we need to focus on the data on the USB bus and we need to drop the extra 0 in front when the report ID is 0 for Windows. Same for Output report. Right now we may report one more byte than real data on the bus due to Windows driver behavior, when the report ID is 0. All in all, we may need to align Windows HIDAPI behavior along with Linux and macOS. Need to check with the libusb backend as well. Another related old PR for macOS. |
Beta Was this translation helpful? Give feedback.
-
There are many confusions under libusb Windows HID backend as well. I think there are more problems with libusb Windows HID backend (libusb project recommends people to use HIDAPI and not libusb, so it is a legacy thingy).
There is another interesting issue there as well (hopefully not affecting HIDAPI). |
Beta Was this translation helpful? Give feedback.
-
FYI as well. I believe libusb/libusb#1217 is incorrect. |
Beta Was this translation helpful? Give feedback.
-
Unfortunately I've never had good test device(s) to check all: input/output/feature/interrupt, with and w/o report ID and with different lengths. All being said, I think we need to focus to make the behavior (e.g. in terms of the return length, etc.) be consistent on all platforms, specifically to match the documentation.
I'm a little lost in the context and worst of all - cannot properly check it locally... |
Beta Was this translation helpful? Give feedback.
-
I will recommend the following devices which will work with the examples here. They are of relative low cost from online stores like AliExpress.
Yes I agree.
I understand. I will try to summarize and create another issue. And hopefully you can get one or two of the test devices without too much difficulties. |
Beta Was this translation helpful? Give feedback.
-
Hm, I actually have an Arduino somewhere. Haven't used it for years. Maybe I could use it. |
Beta Was this translation helpful? Give feedback.
-
No problem, take your time. The Arduino Uno and Maga 2560 (official version and some clones) come with ATmega16U2. It is a bit tricky to get the rawhid example working. Arduino Leonardo or Arduino Micro or Pro Micro or clones come with ATmega32U4 and then you should have no issues to get rawhid example working. |
Beta Was this translation helpful? Give feedback.
-
I am going to move this to discussion and create a new issue to make this one clean. Sorry I have put too much info here and people may get confused. |
Beta Was this translation helpful? Give feedback.
-
I was puzzled that I do not see ways to send Feature report to the host using the following example. Then I find out that the underlying HID library (https://github.com/NicoHood/HID) does not support Get_Report for Input Report and Feature Report. I have created a new issue there. |
Beta Was this translation helpful? Give feedback.
-
There is an example device for hidraw from umockdev project. Maybe we can integrate it for CI under Linux.
https://github.com/martinpitt/umockdev/tree/main/devices/hidraw
Beta Was this translation helpful? Give feedback.
All reactions