Rawhid

Status
Not open for further replies.

DrM

Well-known member
Hi,

Running the USB RAWHID example in a Teensy 3.2, and python hidapi library, hid,enumerate() returns two device interfaces,

The packets seem to appear on both, even if open at the same time. Why are there two interfaces? Is there support on the teensy side to read or write one or the other explicitly?

Next question

hid.read(64) returns a list, length 64, type is int, and sys.getsizeof( buffer[0] ) is 28!

Shouldn't that be a byte array of length 64?
 
Last edited:
I can't help with the Python part of your question but the two interfaces are easy to explain: One is the actual RAW HID interface, the other one is an interface (SerEmu) which is used by uploaders to activate the bootloader. You can also use the SerEmu interface to print to the Arduino serial monitor. The TyTools also handle SerEmu for printing.

You can distinguish between the two interfaces by their HID usage and HID usage pages:

  • RAWHID: usage page = 0xFFAB, usage: 0x0200
  • SEREMU: usage page = 0xFFC9, usage: 0x0004
Here some more information and examples (Win10/c#) showing how to send and receive packages over RAWHID: https://github.com/TeensyUser/doc/wiki/Raw-HID

Se also: https://github.com/PaulStoffregen/c...814ca483c96dea7b47d3c/teensy4/usb_desc.h#L587
 
Last edited:
@luni

Thank you, that is very helpful, especially the link to the code.

Instead of HID, and the HID API, can I just write and read to and from the endpoints?

There is a usblib and PyUSB that works at that level, I prefer not to layer libraries on libraries on libraries .... And the HID is not very relevant to my application
 
The usage_page and usage are both 0 for both interfaces. Here is a code snippet:

Code:
    import hid

    for h in hid.enumerate(vendor_id = 0x16c0):
        print( '\n**********************\nhid device' )
        print(h)


And here is the output with a Teensy3.2 and a Teensy4.0 connected and running a simple RAWHID enabled program

Code:
hid device
{'path': b'0003:0036:00', 'vendor_id': 5824, 'product_id': 1158, 'serial_number': '6834800', 'release_number': 633, 
'manufacturer_string': 'Teensyduino', 'product_string': 'Teensyduino RawHID', 
'usage_page': 0, 'usage': 0, 'interface_number': 0}

**********************
hid device
{'path': b'0003:0036:01', 'vendor_id': 5824, 'product_id': 1158, 'serial_number': '6834800', 'release_number': 633, 
'manufacturer_string': 'Teensyduino', 'product_string': 'Teensyduino RawHID', 
'usage_page': 0, 'usage': 0, 'interface_number': 1}

**********************
hid device
{'path': b'0003:0035:00', 'vendor_id': 5824, 'product_id': 1158, 'serial_number': '9632020', 'release_number': 629, 
'manufacturer_string': 'Teensyduino', 'product_string': 'Teensyduino RawHID',
 'usage_page': 0, 'usage': 0, 'interface_number': 0}

**********************
hid device
{'path': b'0003:0035:01', 'vendor_id': 5824, 'product_id': 1158, 'serial_number': '9632020', 'release_number': 629, 
'manufacturer_string': 'Teensyduino', 'product_string': 'Teensyduino RawHID', 
'usage_page': 0, 'usage': 0, 'interface_number': 1}
 
In principle you can of course directly talk to the endpoints. After all, this is what any USB driver needs to do. But, why spending a lot of time reinventing the wheel. Operating systems provide simple, high level access to HID devices and the HID libraries I've used so far are just thin wrappers around those system calls.

Anyway, I have a dotNet library to handle (find/reset/reboot/upload etc) Teensies under Win10. To get rid of library dependencies I wrote those wrappers myself. See the following link to show how to e.g. start the bootloader by sending a special HID report to the SerEMU interface of the Teensy: https://github.com/luni64/TeensySha...src/TeensySharp/Implementation/Teensy.cs#L351 The code only uses low level system calls. (please note, if you want to browse through the code make sure to use the develop branch. The master branch is still using Mike o Brians HID library.

Another very useful source of information are the sources for the Teeensy CLI loader. It also only uses low level access to the device. https://github.com/PaulStoffregen/teensy_loader_cli

I personally would try to find a decent Python HID library and just write the (probably less than 10) lines to talk to the HID interface as shown in the WIKI article for C#.
 
The usage_page and usage are both 0 for both interfaces. Here is a code snippet:

Code:
    import hid

    for h in hid.enumerate(vendor_id = 0x16c0):
        print( '\n**********************\nhid device' )
        print(h)

I'm not a Python guy and can't really help you with this library. But, since I use them quite often I definitely know that the HID usage pages and HID usages are as listed above for Raw HID devices. Maybe someone with more Python background can chime in here.
 
I have it, the python api, it is called HIDAPI. I have other issues with it now. It returns ints rather than bytes despite the representations of the documentation.
 
Did you finally manage to read the enumerators correctly? If so, and if you are willig to share the code, I would like to add it as Python example to the user WIKI .
 
I rewrote the firmware a little, and wrote a Python Class to talk to it. I can send them if you like and you can see if you want to use them. I've moved on now to using it in an application so I will need to revisit and clean it up.
 
@luni, @paul

Okay, I have an example RAWHID and Python class and program to demonstrate how to configure and communicate with the RAWHID.

How or where should I upload it?
 
As far as I'm concerned you can directly upload it to the wiki I linked above. Alternatively, just zip it and attach it here, I'll copy it to the Wiki then.
 
How or where should I upload it?

Please do post it here on the forum. If using the Quick Reply editor, click the Go Advanced button to get to the full editor which lets you attach a ZIP file to your message.

Before you package it into a ZIP file, please consider putting one of the usual open source licenses in the comments at the top of your code. I personally like the MIT license because it's short & simple.
 
Thanks, I'll add a high level example and your low level access example to the user WIKI. Do you have a GitHub handle which I can mention as author? I'm currently occupied with family things, so give me a few days...
 
Status
Not open for further replies.
Back
Top