Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 15 of 15

Thread: Rawhid

  1. #1
    Senior Member
    Join Date
    Apr 2018
    Location
    Eastern Time, USA
    Posts
    163

    Rawhid

    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 by DrM; 06-17-2021 at 12:55 AM.

  2. #2
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,623
    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/co...sb_desc.h#L587
    Last edited by luni; 06-17-2021 at 06:21 AM.

  3. #3
    Senior Member
    Join Date
    Apr 2018
    Location
    Eastern Time, USA
    Posts
    163
    @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

  4. #4
    Senior Member
    Join Date
    Apr 2018
    Location
    Eastern Time, USA
    Posts
    163
    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}

  5. #5
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,623
    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/TeensyShar...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#.

  6. #6
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,623
    Quote Originally Posted by DrM View Post
    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.

  7. #7
    Senior Member
    Join Date
    Apr 2018
    Location
    Eastern Time, USA
    Posts
    163
    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.

  8. #8
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,623
    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 .

  9. #9
    Senior Member
    Join Date
    Apr 2018
    Location
    Eastern Time, USA
    Posts
    163
    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.

  10. #10
    Senior Member
    Join Date
    Apr 2018
    Location
    Eastern Time, USA
    Posts
    163
    @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?

  11. #11
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,623
    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.

  12. #12
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    25,055
    Quote Originally Posted by DrM View Post
    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.

  13. #13
    Senior Member
    Join Date
    Apr 2018
    Location
    Eastern Time, USA
    Posts
    163
    I started a new thread with the zip,

    https://forum.pjrc.com/threads/67520...ration-program


    Do you prefer it be posted here?

    (I hesitate to duplicate the posting, but either is okay with me).

  14. #14
    Senior Member
    Join Date
    Apr 2018
    Location
    Eastern Time, USA
    Posts
    163
    @luni and @PaulStoffregen, I just posted a minimum code using only PyUSB to talk to a RAWHID Teensy.

    See https://forum.pjrc.com/threads/67561-RAWHID-and-PyUSB

  15. #15
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,623
    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...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •