Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RawHID Expected to work? #133

Open
MartinYoungOB opened this issue Apr 16, 2018 · 7 comments
Open

RawHID Expected to work? #133

MartinYoungOB opened this issue Apr 16, 2018 · 7 comments

Comments

@MartinYoungOB
Copy link

I've been trying to get raw HID. It's partially working but not quite. I'm wondering if it's expecting to work and if not, what I need to do to make it work? I've been looking through the source and there are quite a comments that suggest that it might not be complete. I don't understand many of them.

I have a genuine Arduino Uno with a 16u2. I've put Hoodloader2 on it and it works exactly as expected - I can upload programs to it and communicate with the other AVR on the same board.

I'm now trying to use RawHID to set up a bidirectional link between my Uno and the PC using my own vendor id and product id. I'm using hidapi on Windows 10 (from the main repo) on the PC.

When I enumerate devices on the PC, I see the 16u2 and I can connect to it. I can send bytes with hid_write() on the PC and receive them using RawHID.read() on the 16u2. This is great. (hidapi sends on the first OUT endpoint or the control endpoint is there isn't an OUT one. I'm not sure which it happening this case.)

However when I try to send from the 16u2 using RawHID.write() it is not received by the PC. Using Wireshark I can see that each character is arriving as the single payload byte in a URB_INTERRUPT packet but hid_read() does not return them.

Is this expected to work? Is there something else I need to do?

Also:

  • RawHID doesn't work in a multi-report interface. Because of the serial port that the 16u2 is implemented is this a multi-report system. Do I need to turn off the serial port somehow?

  • Can I induce RawHID to send on endpoint 0, the control endpoint?

Thanks,
Martin.

@NicoHood
Copy link
Owner

RawHID support was always unstable. Also there were bugs introduced with the newer IDE versions which should be corrected now. But I've never retested it and its been quite a long time since I wrote the code.

You can simply just try it yourself and find the bugs. I sadly have no time to do so.

Multireport does not work with hidapi. It just confuses the API. But I am not using multireports, so nothing to do here.

RawHID receives data on a dedicated endpoint and sends data on endpoint0. You could send it on its own endpoint, but this library is not implemented like that.

@NicoHood NicoHood changed the title Expected to work? RawHID Expected to work? Apr 16, 2018
@MartinYoungOB
Copy link
Author

Thanks. That's useful information. It's good to know that I haven't (maybe) done anything to break it. I've got a little more time and will attempt to contribute if I get it working.

FWIW, Wireshark claims that the packets which RawHID receives were sent to endpoint 0, while the ones coming back are being sent to endpoint 4.

@NicoHood
Copy link
Owner

NicoHood commented Apr 17, 2018

Your wireshark observation is correct and expected. Good luck, please report back if you have any knowledge to share. Also checkout the wiki page, if there is more useful information for you.

@eivan7538
Copy link

Nico, Martin, did you have any success with Raw HID? I just tried it with the basic rawhid_test.c with Arduino MKR Zero and had the same issue: Arduino can receive packets from PC but when Arduino sends packets the PC does not receive anything. I would love to help but I'm an amateur so I'm not sure if I can debug and fix it.

@zaphodbe
Copy link

I seem to have a similar situation. It appears that if I do a RawHID.write(buf,len) with len less than 64 nothing gets sent, no LED flash nothing. If however I use 64 or larger it does send, but looks like only the first 64 bytes.

Looking at the code it appears to just be calling USB_Send so assuming this is a lower level USBCore issue.

IDE 1.8.10 with Arduino library 1.8.1 the default in the 1.8.10 package.

@spuder
Copy link

spuder commented Aug 7, 2022

For anyone else running into this problem. Check the following

  1. Read this sparkfun tutorial about the Raw HID library

https://learn.sparkfun.com/tutorials/hid-control-of-a-web-page/all

Set/Verify the RAWHID_TX_SIZE and RAWHID_RX_SIZE in the usb_private.h file (On my machine it is located at ./packages/framework-arduinoteensy/cores/usb_rawhid/usb_private.h and both values are set to 64)

  1. Try filling the array buffer with all zeros
    uint8_t megabuff[64];
    for (uint8_t i = 0; i < sizeof(megabuff); i++) {
        megabuff[i] = 0x00;
    }
    megabuff[42] = foobar;
    RawHID.write(megabuff, sizeof(megabuff));
  1. Download hidapitester and use it for debugging https://github.com/todbot/hidapitester

Update

See cbattlegears comment here for an improved workaround. Instead of hardcoding a buffer of 64 bits, you can do the following workaround

old workaround

    for (byte i = 0; i < sizeof(rawhidData); i++)
    {
        rawhidData[i] = 0x00;
    }
    RawHID.begin(rawhidData, sizeof(rawhidData));

new workaround

        while (bytesAvailable--)
        {
            rawhidBuffer[byte_count] = RawHID.read();
            byte_count++;
        }

norberttak added a commit to norberttak/HID that referenced this issue Sep 17, 2022
@mcuee
Copy link

mcuee commented Jun 17, 2023

It seems to work reasonaby well based on my testing using the example here.

There is a bug for the Feature report but the fix is simple.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants