Skip to content

Using an alternate interface for HID ? #260

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

Closed
iabdalkader opened this issue Feb 4, 2014 · 34 comments
Closed

Using an alternate interface for HID ? #260

iabdalkader opened this issue Feb 4, 2014 · 34 comments
Labels
ports Relates to multiple ports, or a new/proposed port

Comments

@iabdalkader
Copy link
Contributor

I needed a third (4th?) interface for my project, but there not enough endpoints for that, so I used an alternate vendor-specific interface with MSC, just got this working, and I thought we could use the same technique for MSC/HID ? this way you won't need to recompile to switch HID... I can send a patch if you like..

@dpgeorge
Copy link
Member

dpgeorge commented Feb 5, 2014

The HS USB core has more endpoints, but that's not connected to the USB connector, so is no use for us.

Please, issue a pull request for your MSC/HID, that would be awesome!

@iabdalkader
Copy link
Contributor Author

yes, the HS needs an external phy, I guess they ran out of space :) anyway, I will send a pull request tomorrow...

@dpgeorge
Copy link
Member

dpgeorge commented Feb 5, 2014

You can use the HS core in FS mode without any external circutry. In fact, you could wire up HS and normal core together (ie the DM DP pins together) and use whichever you wanted. HS core in FS mode gives you more endpoints and FIFO buffer space.

@iabdalkader
Copy link
Contributor Author

cool, I didn't know that... so this is what I have so far, it kinda works, but there's one problem though, I can't re-attach the kernel module after switching the alt setting because it defaults to alt setting 0..my own project doesn't have this problem because the alt setting doesn't need a kernel driver so I don't re-attach it... any ideas ?

https://github.com/iabdalkader/micropython/compare

@iabdalkader
Copy link
Contributor Author

okay, so this should work after switching the alt setting:

echo 0483 5740 > /sys/bus/usb/drivers/usbhid/new_id

But it doesn't ._. for some reason I get:

usbhid 2-1.2:1.2: couldn't find an input interrupt endpoint

Does the two alt settings have to have the same EP types/numbers ?

@dpgeorge
Copy link
Member

dpgeorge commented Feb 9, 2014

Hmm, I don't really know how to fix this. It's nice to be able to switch the USB device from one setting to another using "alternate configs", but maybe that's too complicated.

I was instead thinking to have an option in boot.py which makes the pyboard look either like MSC+VCP, or HID(+MCS/VCP). It would do this simply by sending different USB configuration data.

@iabdalkader
Copy link
Contributor Author

I'm not sure how it's done in commercial products, but I've had some time to think about this, here's one possible and not very complex solution, when the set interface request is received we switch the MSC alternate interface value (in the config descriptor) to 1 (by writing to its offset something like usbd_pyb_CfgDesc[MSC_ALT]=1) and the same to the HID interface, set it to zero, then do a softreset and the kernel driver will use the HID by default because now it's the default alternate :)

Alternatively, we could read the value from boot.py and then do the same trick based on that, this will allow it to use the same cfg descriptor instead of allocating two of them.

@dpgeorge
Copy link
Member

dpgeorge commented Feb 9, 2014

This would require putting the USB descriptor information in RAM (or at least the part that needs to be modified). Or, when sending the descriptor, you could intercept the ROM memory location that needs to be changed and instead send the corrected value.

@iabdalkader
Copy link
Contributor Author

it's already in RAM not flash, right ?

__ALIGN_BEGIN static uint8_t usbd_pyb_CfgDesc[USB_PYB_CONFIG_DESC_SIZ] __ALIGN_END =```

@dpgeorge
Copy link
Member

dpgeorge commented Feb 9, 2014

Yes you are correct, it's in RAM.

@iabdalkader
Copy link
Contributor Author

okay, but now if we do a soft reset the config desc be reloaded from flash ? :D I think USB core has a softreset I will try that and get back to you

@iabdalkader
Copy link
Contributor Author

so it turns out there's a softdisconnect feature which makes the OTG disconnect internal USB pull-up, and the device disconnects and re-enumerates, before using that I switch the altsettings like mentioned before, now this is the kernel log when it's first connected:

Feb  9 21:50:11 destiny kernel: [73874.838795] usb 2-1.2: new full speed USB device using ehci_hcd and address 55
Feb  9 21:50:11 destiny kernel: [73874.951659] cdc_acm 2-1.2:1.0: This device cannot do calls on its own. It is not a modem.
Feb  9 21:50:11 destiny kernel: [73874.951719] cdc_acm 2-1.2:1.0: ttyACM0: USB ACM device
Feb  9 21:50:11 destiny kernel: [73874.955642] scsi33 : usb-storage 2-1.2:1.2
Feb  9 21:50:12 destiny kernel: [73875.948217] scsi 33:0:0:0: Direct-Access     STM      microSD Flash    1.00 PQ: 0 ANSI: 2
Feb  9 21:50:12 destiny kernel: [73875.949917] sd 33:0:0:0: Attached scsi generic sg2 type 0
Feb  9 21:50:12 destiny kernel: [73875.953796] sd 33:0:0:0: [sdc] 480 512-byte logical blocks: (245 kB/240 KiB)
Feb  9 21:50:12 destiny kernel: [73875.954676] sd 33:0:0:0: [sdc] Write Protect is off
Feb  9 21:50:12 destiny kernel: [73875.954692] sd 33:0:0:0: [sdc] Mode Sense: 00 00 00 00
Feb  9 21:50:12 destiny kernel: [73875.954698] sd 33:0:0:0: [sdc] Assuming drive cache: write through
Feb  9 21:50:12 destiny kernel: [73875.957403] sd 33:0:0:0: [sdc] Assuming drive cache: write through
Feb  9 21:50:12 destiny kernel: [73875.961680]  sdc: sdc1
Feb  9 21:50:12 destiny kernel: [73875.963371] sd 33:0:0:0: [sdc] Assuming drive cache: write through
Feb  9 21:50:12 destiny kernel: [73875.963379] sd 33:0:0:0: [sdc] Attached SCSI disk

and when doing usb-switch.py hid

Feb  9 21:50:22 destiny kernel: [73885.847198] usb 2-1.2: USB disconnect, address 55
Feb  9 21:50:22 destiny kernel: [73886.081980] usb 2-1.2: new full speed USB device using ehci_hcd and address 56
Feb  9 21:50:22 destiny kernel: [73886.194767] cdc_acm 2-1.2:1.0: This device cannot do calls on its own. It is not a modem.
Feb  9 21:50:22 destiny kernel: [73886.194804] cdc_acm 2-1.2:1.0: ttyACM0: USB ACM device
Feb  9 21:50:22 destiny kernel: [73886.196808] generic-usb: probe of 0003:0483:5740.0004 failed with error -32

And again doing usb-switch.py msc

Feb  9 21:50:28 destiny kernel: [73891.470925] usb 2-1.2: USB disconnect, address 56
Feb  9 21:50:28 destiny kernel: [73891.703560] usb 2-1.2: new full speed USB device using ehci_hcd and address 57
Feb  9 21:50:28 destiny kernel: [73891.816652] cdc_acm 2-1.2:1.0: This device cannot do calls on its own. It is not a modem.
Feb  9 21:50:28 destiny kernel: [73891.816689] cdc_acm 2-1.2:1.0: ttyACM0: USB ACM device
Feb  9 21:50:28 destiny kernel: [73891.825263] scsi34 : usb-storage 2-1.2:1.2
Feb  9 21:50:29 destiny kernel: [73892.822872] scsi 34:0:0:0: Direct-Access     STM      microSD Flash    1.00 PQ: 0 ANSI: 2
Feb  9 21:50:29 destiny kernel: [73892.825657] sd 34:0:0:0: [sdc] 480 512-byte logical blocks: (245 kB/240 KiB)
Feb  9 21:50:29 destiny kernel: [73892.826733] sd 34:0:0:0: [sdc] Write Protect is off
Feb  9 21:50:29 destiny kernel: [73892.826739] sd 34:0:0:0: [sdc] Mode Sense: 00 00 00 00
Feb  9 21:50:29 destiny kernel: [73892.826743] sd 34:0:0:0: [sdc] Assuming drive cache: write through
Feb  9 21:50:29 destiny kernel: [73892.832096] sd 34:0:0:0: Attached scsi generic sg2 type 0
Feb  9 21:50:29 destiny kernel: [73892.836206] sd 34:0:0:0: [sdc] Assuming drive cache: write through
Feb  9 21:50:29 destiny kernel: [73892.840552]  sdc: sdc1
Feb  9 21:50:29 destiny kernel: [73892.842765] sd 34:0:0:0: [sdc] Assuming drive cache: write through
Feb  9 21:50:29 destiny kernel: [73892.842779] sd 34:0:0:0: [sdc] Attached SCSI disk

@dpgeorge
Copy link
Member

dpgeorge commented Feb 9, 2014

Well, at least you can get back safely to VCP+MSC. There must be something wrong with the descriptor for HID. Have you tried the HID device without this alternate function switching? Have you tried just HID (no VCP composite device)?

@iabdalkader
Copy link
Contributor Author

Yes, this softdisconnect trick will be very useful even if you decide to use two separate config descriptors, it's documented in the datasheet search for SDIS

About the HID error, I always had this one with VCP/HID, even before I try the altsetting thing, it could be something with the HID descriptor, or maybe because I haven't updated my fork for a while ?

@iabdalkader
Copy link
Contributor Author

Okay, so I checked out a fresh micropython, changed just one line:

#define USB_PYB_USE_MSC (0)

compile, flash and:


Feb 13 03:48:17 destiny kernel: [50278.703960] usb 2-1.2: new full speed USB device using ehci_hcd and address 125
Feb 13 03:48:17 destiny kernel: [50278.817502] cdc_acm 2-1.2:1.0: This device cannot do calls on its own. It is not a modem.
Feb 13 03:48:17 destiny kernel: [50278.817530] cdc_acm 2-1.2:1.0: ttyACM0: USB ACM device
Feb 13 03:48:17 destiny kernel: [50278.821270] generic-usb: probe of 0003:0483:5740.0003 failed with error -32

@iabdalkader
Copy link
Contributor Author

oh and I moved the usb altsetting code to a new branch, til we decide what to do with it:

https://github.com/iabdalkader/micropython/tree/usb_altsetting

@iabdalkader
Copy link
Contributor Author

it works with a single interface:

kernel: [57941.760894] generic-usb 0003:0483:5740.0039: input,hidraw2: USB HID v1.11 Mouse [STMicroelectronics STM32 Virtual ComPort in FS Mode] on usb-0000:00:1d.0-1.2/input0

@AWCharlie
Copy link

Ok, you got me! Nobody and no messages in the forum :-) Where is everyone
communicating? Got an IRC channel?

C. Towne Springer

On Wed, Feb 12, 2014 at 8:00 PM, Ibrahim Abd Elkader <
notifications@github.com> wrote:

it works with a single interface:

kernel: [57941.760894] generic-usb 0003:0483:5740.0039: input,hidraw2: USB HID v1.11 Mouse [STMicroelectronics STM32 Virtual ComPort in FS Mode] on usb-0000:00:1d.0-1.2/input0

Reply to this email directly or view it on GitHubhttps://github.com//issues/260#issuecomment-34946361
.

@dhylands
Copy link
Contributor

@pfalcon pfalcon added the ports label Feb 15, 2014
@iabdalkader
Copy link
Contributor Author

Can anyone test the HID and confirm that it's enumerated correctly ? or not ?

@dpgeorge
Copy link
Member

It works only with a single interface (ie HID only). VCP+MSC works, but not VCP+HID... I played around with the descriptors a bit but couldn't get both interfaces working.

See eg http://www.microchip.com/forums/m189607.aspx for some ideas on fixing this.

@iabdalkader
Copy link
Contributor Author

I'm not sure why, but if you move the HID interface descriptor before the CDC (make it the first interface) it works and CDC doesn't !

[179656.526369] usb 2-1.2: USB disconnect, address 24
[179657.015141] usb 2-1.2: new full speed USB device using ehci_hcd and address 25
[179657.138749] input: STMicroelectronics STM32 Virtual ComPort in FS Mode as /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/input/input20
[179657.139170] generic-usb 0003:0483:5740.0025: input,hidraw2: USB HID v1.11 Mouse [STMicroelectronics STM32 Virtual ComPort in FS Mode] on usb-0000:00:1d.0-1.2/input0
[179657.139404] cdc_acm 2-1.2:1.1: This device cannot do calls on its own. It is not a modem.
[179657.139431] cdc_acm: probe of 2-1.2:1.1 failed with error -22 

@dpgeorge
Copy link
Member

dpgeorge commented Mar 8, 2014

It's strange. It could be a linux USB driver issue, not enumerating composite devices correctly. Each device on its own has the correct descriptor, but putting them together, linux seems to get confused for HID and CDC.

@iabdalkader
Copy link
Contributor Author

It could be or it could be an issue with the USB stack, descriptor or anything else, so what if we use the softdisconnect as mentioned, but instead we switch descriptors ? MSC/HID or MSC/CDC ? as simple as that

@dpgeorge
Copy link
Member

The latest version in stmhal/ now has support for CDC+MSC or CDC+HID. I managed to somehow get the composite CDC+HID working with the new Cube HAL driver.

@iabdalkader
Copy link
Contributor Author

what about switching between CDC/MSC and CDC/HID ? I can try the softdisconnect in stmhal if you want ?

@markusgritsch
Copy link

I am using Windows 7. When trying http://micropython.org/doc/tut-mouse and putting pyb.usb_mode('CDC+HID') into boot.py, the board does not enumerate correctly anymore.

The CDC part still works, but HID is not working, and the Windows device manager shows a problem with a mass storage device (German: "Massenspeichergerät"):
device_manager

Has anyone the HID demo working on Windows?

@dpgeorge
Copy link
Member

@markusgritsch I can confirm that HID + CDC is not working under Windows.

I had tested it on Linux and Mac, but it seems that Windows was not tested for this feature. Windows handles composite USB devices differently to Linux and Mac, and fixing it for Windows I think then breaks it for Linux and Mac. The solution is to provide either a pure HID configuration (ie no CDC), and/or a HID+MSC configuration, and/or a different version of HID+CDC. I'll see what I can do.

@ghost
Copy link

ghost commented Oct 27, 2014

Has been found some solution in windows 7: you can use HID interface, but you have to change driver manualy.
Set pyb in CDC+HID mode.
Open Device Manager, choose unknown usb mass storage device, in submenu Update Driver Software, Browse my computer for driver software, Let me pick from a list of device... Apply hid-driver.
mouse
At least it allows test CDC+HID mode on windows. For access flash memory, even in save mode, you have to change for hid driver into mass storage driver manualy with similar way.

@dpgeorge
Copy link
Member

@inaugurator which version of the firmware did you use? The very latest version of master has a fix for this: it changes the PID of the USB device when it is in CDC+HID mode so that Windows uses a different configuration, and hence a different driver. It uses PID 0x9800 in CDC+MSC mode, and PID 0x9801 in CDC+HID mode.

@ghost
Copy link

ghost commented Oct 29, 2014

Great! With firmware pybv10-network-2014-10-29-v1.3.5.dfu it works fine for me.
Before I used pybv10-network-2014-10-20-v1.3.3-99-g072bd07.dfu
Thank you!

@bowman85
Copy link

Could you, please, share a project CDC+HID mode?

@dpgeorge
Copy link
Member

dpgeorge commented Apr 18, 2017

Could you, please, share a project CDC+HID mode?

@bowman85 please see the official tutorial: http://docs.micropython.org/en/latest/pyboard/pyboard/tutorial/usb_mouse.html

@dpgeorge
Copy link
Member

This can be closed: stmhal supports dynamic selection (in boot.py) of VCP+MSC, VCP+HID or VCP only.

tannewt added a commit to tannewt/circuitpython that referenced this issue Sep 28, 2017
This is the first automatic update that has caused a few deletions
where code was previously copied instead of moved.

This is a prerequisite for micropython#260.
tannewt added a commit to tannewt/circuitpython that referenced this issue Sep 29, 2017
* atmel-samd: Support patching after updating ASF4.

This makes it possible to automatically fix newer code.

* atmel-samd: Update ASF4 to include flash APIs for SAMD51.

This is the first automatic update that has caused a few deletions
where code was previously copied instead of moved.

This is a prerequisite for micropython#260.
tannewt added a commit to tannewt/circuitpython that referenced this issue Oct 9, 2017
This allows us to re-enable `os`. `random` is also enabled because
it solely depends on `os`.

Fixes micropython#266. Its also a pre-requisite for micropython#260.
tannewt added a commit to tannewt/circuitpython that referenced this issue Oct 14, 2017
* atmel-samd: Add support for internal filesystems.

This allows us to re-enable `os`. `random` is also enabled because
it solely depends on `os`.

Fixes micropython#266. Its also a pre-requisite for micropython#260.

* atmel-samd: Update SAMD51 linker script comments and MICROPY_MAX_STACK_USAGE enabling.
tannewt added a commit to tannewt/circuitpython that referenced this issue Oct 18, 2017
tannewt added a commit to tannewt/circuitpython that referenced this issue Oct 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ports Relates to multiple ports, or a new/proposed port
Projects
None yet
Development

No branches or pull requests

7 participants