USB HID and composite

danieleff
Sat Nov 26, 2016 8:06 am
I uploaded a HID and USB Composite experiment to https://github.com/danieleff/Arduino_Co … _Composite
#include "USBDevice.h"
#include "usbd_hid.h"

#define BUTTON_PIN 32

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);

USBDevice.beginSerialHID();
}

uint8_t hidreport[4] = {0, 0, 0, 0};

int previous_buttons = 0;

void loop() {

if (previous_buttons != digitalRead(BUTTON_PIN)) {
hidreport[0] = digitalRead(BUTTON_PIN) ? 1 : 0;
previous_buttons = hidreport[0];
USBD_HID_SendReport(&hUsbDeviceFS, hidreport, 4);
digitalWrite(LED_BUILTIN, hidreport[0]);

Serial.println("Button pressed");
}

}


RogerClark
Sat Nov 26, 2016 10:24 am
Sorry Daniel

Compiles OK now.

But I can’t get it to upload to my Maple mini as its got the old bootloader

I’ll have to look tomorrow as its getting too late today.


RogerClark
Sat Nov 26, 2016 8:26 pm
Daniel

I run the code but it seems to initially completely crash Java on Windows.

The Arduino IDE crashed straight after upload, and when I tried to restart the IDE it just crashed, so I ran the debug version and I get

#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x617493cd, pid=516, tid=0x00000d18
#
# JRE version: Java(TM) SE Runtime Environment (8.0_92-b14) (build 1.8.0_92-b14)
# Java VM: Java HotSpot(TM) Client VM (25.92-b14 mixed mode windows-x86 )
# Problematic frame:
# C [listSerialsj.dll+0x93cd] #
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\rclark\AppData\Local\Temp\\hs_err_pid516.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#


RogerClark
Sat Nov 26, 2016 8:42 pm
Daniel

I don’t know what you changed, but it completely crashes the IDE when I plug the Maple mini into my W7 PC.

If I comment out

USBDevice.beginSerialHID();


danieleff
Sat Nov 26, 2016 9:11 pm
Thanks for trying. Even if not working, it should not crash the IDE, wow.
I am using windows 10 (and tried on a linux). git diff shows empty so it should be uploaded.

Could you try changing usbd_desc.c #define USBD_PID_FS 22336 (which is 0x5740 by the way) to for example 0x5750, then recompile the static lib. It might try to use a previous driver with the new “device”.


RogerClark
Sat Nov 26, 2016 9:25 pm
Daniel

OK.

I’ll try that.


RogerClark
Sat Nov 26, 2016 9:40 pm
Hi Daniel

Yes. Changing the PID stops it crashing, but it then won’t install the driver for the Serial device (because the PID is wrong)

So I use the libwdi driver installer exe to install the serial driver, but the IDE immediately crashed again :-(

I have some other installations of W7 that I can try it on, which don’t have the STM drivers installed.

I’ll also try rebooting my machine, but I don’t think rebooting will fix it :-(


danieleff
Sun Nov 27, 2016 6:01 am
I just tried it on a windows 7 and the result was as your: button worked as mouse (did you try it?), virtual COM not working, had yellow warning.

Than I used the libmaple driver Arduino\hardware\Arduino_STM32\drivers\win\install_drivers.bat , with the following line:
"%~dp0wdi-simple" --vid 0x0483 --pid 0x5750 --iid 0 --type 3 --name "Maple Serial" --dest "%~dp0maple-serial"


RogerClark
Sun Nov 27, 2016 6:11 am
Hi Daniel

I could not get it to work as it kept crashing the Arduino IDE.

I will try uninstalling and then using libwdi with the interface setting you used.

Did it crash for you.

All I have to do to make the IDE crash is plug in the Maple mini with the SerialHID sketch uploaded.

At one point Windows displayed a popup to say it was installing the composite and HID device, but could not install the Serial because of the PID, but as soon as I used libwdi to install the Serial driver, it crashed the IDE again :-(


RogerClark
Sun Nov 27, 2016 6:29 am
Daniel,

What version of the IDE and Java are you running.

I just tried updating my Java (I had to update it for something else recently, but I thought I’d update it again, but it was actually the same version.

V 8.0_92-b14 on

Actually, it may be my IDE installation. As its crashing in listserials.dll and I recall trying an update for that to resolve some other problem.

I’ll re-install the IDE and let you know if that resolves the problem.

Edit.

I just noticed that the latest release of the IDE is 6.13 and I’m running 6.12, so it will be an upgrade for me ;-)


danieleff
Sun Nov 27, 2016 6:37 am
On Win10, Arduino 1.6.10, Java 1.8.0_92-b14.
It seems I do not have to use libwdi, as Win10 takes care of the serial driver automatically, in the event manager:
Driver Management concluded the process to install driver usbser.inf_amd64_5de2576d6f02918e\usbser.inf for Device Instance ID USB\VID_0483&PID_5750&MI_00\6&2B048C5C&0&0000 with the following status: 0x0.
Driver Management has concluded the process to add Service usbser for Device Instance ID USB\VID_0483&PID_5750&MI_00\6&2B048C5C&0&0000 with the following status: 0.

RogerClark
Sun Nov 27, 2016 6:39 am
Update.

Installing the latest IDE fixed it. I think I may have had a custom version of listports.dll installed, but I can’t remember why I did that .

Anyway….

When I push the button I just get a message via Serial USB, but I don’t seem to get a real button push on the mouse.

And if I push the button a few times it seems to crash after the 4th push

As all I get is

Button pressed
Button pressed
Button pressed
Button pressed


RogerClark
Sun Nov 27, 2016 6:49 am
Daniel

I tried

int c=0;
int previous_buttons = 0;
unsigned long m=0;
void loop() {

if (millis() - m>1000)
{
Serial.println(c++*2);
m=millis();
}
if (previous_buttons != digitalRead(BUTTON_PIN))
{
hidreport[0] = digitalRead(BUTTON_PIN) ? 1 : 0;
previous_buttons = hidreport[0];
USBD_HID_SendReport(&hUsbDeviceFS, hidreport, 4);
digitalWrite(LED_BUILTIN, hidreport[0]);

Serial.println("Button pressed");
}

}


danieleff
Sun Nov 27, 2016 7:15 am
It seems it thinks the whole device is serial, and tries to parse the button HID reports too.
I would still try to install with –iid 0 so only the serial interface part gets the serial driver (and uninstall the previous from 0x5750, or use differen PID usbd_desc.c).

RogerClark
Sun Nov 27, 2016 7:24 am
I tried using –iid 0 but I have not totally changed the PID (again)
I’ll move it to –pid 0x5751 and let you know what happens

RogerClark
Sun Nov 27, 2016 7:37 am
Tried 0x5751 and also 0x5760

HID is now working but Serial driver won’t load

serial_driver_not_recognised.png
serial_driver_not_recognised.png (54.9 KiB) Viewed 842 times

danieleff
Sun Nov 27, 2016 7:43 am
Thank you for your trouble. I will investigate further from my Win7 laptop next week. Seems like the only missing thing is to install the serial driver to only that part of the device (which should be –iid 0), not the whole. (I mean it works on my Win10 both simultaneously )
*EDIT sorry for the additional installed drivers. Do you know how to remove them from a PID (when the device is not plugged in)? :)

RogerClark
Sun Nov 27, 2016 8:58 am
No worries

I’ll recompile for 0x5760, and 51 and 50 and see if I can remove them manually after loading each version into the Maple mini

BTW.

I tried using this http://www.nirsoft.net/utils/usb_devices_view.html it didnt seem to remove the drivers, even though it claimed it could


RogerClark
Sun Nov 27, 2016 9:03 am
Daniel

BTW. It would be better if you updated your makefile from the one in the WIP branch as you makefile has a problem with the VL Discovery build unless I do “make clean” first.
I’m sure this has been fixed


RogerClark
Sun Nov 27, 2016 9:37 am
Daniel

I tied connecting to a different machine, albeit one that had a Maple Mini connected before (but not the new STM repo and STM Virtual serial port etc.

And I got this USB installation popup (Sorry for the low quality, I had screengrab on one machine and email it to another and it didnt come out of the email in high quality.

too_many_devices.png
too_many_devices.png (25.02 KiB) Viewed 822 times

RogerClark
Sun Nov 27, 2016 9:47 am
Daniel (or anyone else who wants to try it)

Here is my compile maple mini binary

(see attached)

Edit

BTW

I tried on Windows 10, and the Com port shows even before I load the driver and I see the HID devices

But I don’t have the IDE installed on that machine, and I don’t have a terminal program either, so I don’t know if it actually works – though I presume it does

Perhaps Windows 7 has a bug with composite devices – I know that Windows XP had problems with composite devices, which is why Leaflabs decided that the Bootloader was only DFU and did not include Serial.

But I hoped that Windows 7 didnt have the same problem (but perhaps it does)


danieleff
Sun Nov 27, 2016 9:48 am
The descriptor looked kinda OK in USBlyzer, but I will redo it next week. I am 100% sure it can work on Win7

danieleff
Sat Dec 03, 2016 8:04 am
I updated the branch, tried with Win10 (no driver needed), linux (no driver needed), Win7 (driver needed for usbserial).
PID: 0x5740 is used for Serial.begin(), 0x5741 for composite with Serial on interface 0, and 0x5742 where no driver is needed (HID, later MSC/audio…).

Added Mouse (set/press/release/click/move) and Keyboard(just experimental) files.

Current example code:
#include "USBDevice.h"
#include "Mouse.h"
#include "Keyboard.h"

#define BUTTON_PIN 32

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);

USBDevice.beginSerialHID(&Mouse, &Keyboard);
}

int previous_buttons = 0;

void loop() {

if (previous_buttons != digitalRead(BUTTON_PIN)) {
previous_buttons = digitalRead(BUTTON_PIN) ? 1 : 0;

Mouse.setButton(digitalRead(BUTTON_PIN));
Keyboard.println("Hello!");
digitalWrite(LED_BUILTIN, previous_buttons);
Serial.println("Button pressed");
}

}


RogerClark
Mon Dec 05, 2016 2:06 am
Thanks Daniel

I will test when I get some free time

Cheers

Roger


Elektrik
Mon Dec 12, 2016 1:16 am
Hello,

Tested on stm32f103c with 1.6.13 arduino ide on W10 x64, work very well.
Can’t wait for it to have Joystick library (passing pot value from ADC) :o


RogerClark
Mon Dec 12, 2016 9:23 am
Daniel

Thanks.

It does seem to work OK for me now on Windows 7.

In the WIndows device manager, the Serial port becomes “Maple Serial Interface 0 (COMxx)”

and another “USB Input Device” appears in the “Human Interface Devices” section

I guess we need to work out how to merge all this into the stm32duino F1 repo

I’m a bit concerned that some of this stuff could impact on the original Nucleo codebase

I think I asked Frederic @ STM a similar question a month ago, so I will need to find his answer, as the changes we need to make to the official repo are getting more and more invasive as we need to change a high proportion of their code (not just for this but for the I2C and the RAM usage, and the USB support, etc etc etc


Freakeyyy
Tue Dec 13, 2016 1:29 pm
Hey guys, noobie question here :
While trying to upload anything to a maple mini with this branch, I keep getting “Cannot set alternate interface: usb_set_altinterface: could not set alt interface 0/2: win error: No more data is available.”
Do you have any idea why ?

danieleff
Tue Dec 13, 2016 1:48 pm
Freakeyyy wrote:Hey guys, noobie question here :
While trying to upload anything to a maple mini with this branch, I keep getting “Cannot set alternate interface: usb_set_altinterface: could not set alt interface 0/2: win error: No more data is available.”
Do you have any idea why ?

danieleff
Tue Dec 13, 2016 2:02 pm
Elektrik wrote:Hello,

Tested on stm32f103c with 1.6.13 arduino ide on W10 x64, work very well.
Can’t wait for it to have Joystick library (passing pot value from ADC) :o


Freakeyyy
Tue Dec 13, 2016 3:13 pm
danieleff wrote:
Or copy the joystick library, and modify it. Its copy-paste duplicate.

danieleff
Tue Dec 13, 2016 4:52 pm
Freakeyyy wrote:danieleff wrote:
Or copy the joystick library, and modify it. Its copy-paste duplicate.

Elektrik
Tue Dec 13, 2016 11:14 pm
Good day, tested Joystick on windows 10 x64, seems to work well, buttons are read fine, now continue to test if pot value is passed as axis correctly. One issue I found – if I upload sketch when stm32 is already connected – it works fine. Once I reset/unplug-plug, it does not load as HID device. The actual sketch is flashed, just some issues during enumeration, will try to add delays before USBDevice.beginHID.
Image

Update:
Did not manage to check axis, since cannot read adc value (INPUT_ANALOG is not declared to turn on adc pin), tried to set bits manually, but it’s too different in comparison with maple core definitions on original arduino_stm32 repo.


danieleff
Wed Dec 14, 2016 5:55 am
Elektrik wrote:
Did not manage to check axis, since cannot read adc value (INPUT_ANALOG is not declared to turn on adc pin), tried to set bits manually, but it’s too different in comparison with maple core definitions on original arduino_stm32 repo.

Elektrik
Wed Dec 14, 2016 4:55 pm
Oh, thank you for reply, that’s might be an issue, since I tried it on A0 pin, will try now to use different pins.
danieleff wrote:
Are you using bluepill or maple mini?

danieleff
Wed Dec 14, 2016 5:06 pm
That is the bluepill, PB0 should work for now. And nothing else… for now. @RogerClark I would like to send PRs for these, just do not know which branch.

Elektrik
Wed Dec 14, 2016 6:12 pm
Tried on PB0, this cause board sometimes returning “Unkwnown USB descriptor”, or sometimes it runs until analogRead() and then crashes.

#include "USBDevice.h"
#include "Joystick.h"

#define BUTTON_PIN 32
#define LED_BUILTIN PC13

Joystick_ Joystick(0x01, 0x04, 10, 0, true, false, false, false, false, false, false, false, false, false, false);

int val = 0;

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);

USBDevice.beginSerialHID(&Joystick);

Joystick.begin();
}

int previous_buttons = 0;

void loop() {
if (previous_buttons != digitalRead(BUTTON_PIN)) {
previous_buttons = digitalRead(BUTTON_PIN) ? 1 : 0;
Joystick.setButton(1, previous_buttons);
digitalWrite(LED_BUILTIN, !previous_buttons);

val = analogRead(PB0);
Serial.println(val);

Joystick.setXAxis(val);
}

}


RogerClark
Wed Dec 14, 2016 7:33 pm
Daniel

I think Frederic @ STM is happy to have the new code in the master branch.
I think one of Frederic’s PRs effectively moves the changes in the WIP branch into master, but I have not had time to test the PR, so those changes have not been made to the master yet.

Sorry. I have been very busy with commerical work, as I have pre-christmas deadlines and also deadlines due to Chinese New Year, ( my contacts in China start their holiday on 12th Jan, for about 3 weeks).
So I have not had time to test and pull the PRs from Frederic.

As I do not derive any income from STM etc, my work has to come first.


X71
Fri Dec 30, 2016 2:55 am
This is what I’ve been trying to do on my F103VET6 board for a while now, but I’m confused on the actual architecture.

I’ve managed to get my board to boot as a USB Serial Device (thanks to Roger’s FAQ), but I don’t fully understand how to “turn” it in to a USB HID.

Is the code presented in this thread supposed to be the “bootloader” of the board, or do you just upload the code as a “sketch” and when plugged into the PC it’s operates as a USB HID?

Can some provide a step by step guide on how I could get this running on my board?


danieleff
Fri Dec 30, 2016 3:19 pm
X71 wrote:This is what I’ve been trying to do on my F103VET6 board for a while now, but I’m confused on the actual architecture.

I’ve managed to get my board to boot as a USB Serial Device (thanks to Roger’s FAQ), but I don’t fully understand how to “turn” it in to a USB HID.

Is the code presented in this thread supposed to be the “bootloader” of the board, or do you just upload the code as a “sketch” and when plugged into the PC it’s operates as a USB HID?

Can some provide a step by step guide on how I could get this running on my board?


X71
Sat Dec 31, 2016 12:46 am
danieleff wrote:X71 wrote:This is what I’ve been trying to do on my F103VET6 board for a while now, but I’m confused on the actual architecture.

I’ve managed to get my board to boot as a USB Serial Device (thanks to Roger’s FAQ), but I don’t fully understand how to “turn” it in to a USB HID.

Is the code presented in this thread supposed to be the “bootloader” of the board, or do you just upload the code as a “sketch” and when plugged into the PC it’s operates as a USB HID?

Can some provide a step by step guide on how I could get this running on my board?


gungsukma
Thu Jan 12, 2017 7:29 am
I want to use keyboard and mouse from my Blue Pill (STM32F103C8T6).
Searching “HID” in this forum and ends up here.
Well, will be very nice to have Keyboard/Mouse, and on the same time, serial. :D

So I install STM32Duino, here what I was done, if I remember correctly:
1. Extract Arduino-1.8.0 IDE
2. Make Arduino-1.8.0\Portable folder (I use external hard drive)
3. Install Arduino Zero board
4. Extract Arduino-1.8.0\hardware\Arduino_STM32-master (Arduino_STM32-master.zip)
5. Run Arduino-1.8.0\hardware\Arduino_STM32-master\drivers\win\install_drivers.bat
7. Test “blink”, success.

And for the HID:
1. Make Arduino-1.8.0\hardware\STM32_official folder
2. Extract Arduino-1.8.0\hardware\STM32_official\STM32F1 (Arduino_Core_STM32F1-USB_HID_Composite.zip)
3. Remove Arduino-1.8.0\hardware\Arduino_STM32-master\STM32F1
4. Extract Arduino-1.8.0\hardware\STM32_official\Tools (Arduino_Tools-master.zip)
5. Remove Arduino-1.8.0\hardware\Arduino_STM32-master\Tools

6. Try to compile this code:
#include "USBDevice.h"
#include "Mouse.h"
#include "Keyboard.h"
#define BUTTON_PIN 32
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
USBDevice.beginSerialHID(&Mouse, &Keyboard);
}


stevestrong
Thu Jan 12, 2017 8:08 am
Serial upload is only targeted to be used for bootloader.
Try the “Stm32duino bootloader” upload option, otherwise is not guarantied that the serial USB is compiled.

danieleff
Fri Jan 13, 2017 5:25 am
Can confirm that the error is because of the serial upload method.

If you really want to upload via Serial, you can workaround by adding `BluePill_F103C8.menu.upload_method.serialMethod.build.upload_flags=-DSERIAL_USB` to the `boards.txt`, although this will change `Serial` to USB , and `Serial1` will be the UART1.

I will have to modify the code so that USB is included if the chip has USB, and not only if Serial-is-USBSerial based on menu settings. Or something.


gungsukma
Fri Jan 13, 2017 6:35 am
1. D:\Arduino-1.8.0\hardware\Arduino_STM32-master\tools\win\stm32flash.exe -w generic_boot20_pc13.bin -v -g 0x0 COM5
2. Boot0: LOW
3. Use the USB cable data.
4. Upload method: STM32duino bootloader

5. serial-hid.ino as below
#include "USBDevice.h"
#include "Mouse.h"
#include "Keyboard.h"

void setup() {
pinMode(PC13, OUTPUT); // PC13 and LED_BUILTIN
pinMode(LED_BUILTIN, OUTPUT);
USBDevice.beginSerialHID(&Mouse, &Keyboard);
}

void loop(){
digitalWrite(PC13, HIGH);
delay(1000);
digitalWrite(PC13, LOW);
Keyboard.write('a');
Keyboard.write('b');
Keyboard.write('c');
Keyboard.write('d');
Keyboard.write('e');
Keyboard.write('f');
Keyboard.write('g');
delay(5000);
}


mobi123
Fri Feb 17, 2017 9:10 am
D: \ Arduino \ hardware \ Arduino_STM32-master \ STM32F1 \ cores \ maple \ USBDevice.cpp: 2: 22: fatal error: usbd_hid.h: No such file or directory

  #include "usbd_hid.h"

                       ^

Compilation terminated

Exit status 1
Compiler error for development board Generic STM32F103C series.


gungsukma
Tue Feb 21, 2017 9:55 am
mobi123 wrote:D: \ Arduino \ hardware \ Arduino_STM32-master \ STM32F1 \ cores \ maple \ USBDevice.cpp: 2: 22: fatal error: usbd_hid.h: No such file or directory

  #include "usbd_hid.h"

                       ^

Compilation terminated

Exit status 1
Compiler error for development board Generic STM32F103C series.


weslleymata
Mon May 29, 2017 7:18 pm
My is not working, I’m using windows 10, uploader: STLink IDE: Arduino 1.8.1

I want to use the Keyboard function.

Thank you.
C:\Users\weslley.mata\Documents\Arduino\libraries\Mouse\src/Mouse.h:25:17: fatal error: HID.h: No such file or directory

#include "HID.h"


KHODIDAS11
Mon Aug 21, 2017 11:12 am
Hi daniel

I want interface USB Keyborad to stm32f407 black, Please help.
I have done USB serial using SerialUSB.begin(): and works fine.


newhero
Thu Jan 18, 2018 3:59 am
kungsukma,

I followed your post and got a success to usb keyboard.
I’m thinking to make a ir-usb cordless keyboard.
I tried to use timer in my code in addition to the codes you showed.
But, with the way library included, the code below is not working showing

void setup() {
USBDevice.beginSerialHID(&Mouse, &Keyboard);
Timer2.setPrescaleFactor(1024);
}


arpruss
Thu Jan 25, 2018 3:47 am
In case it helps, I don’t have any trouble compiling the following with Roger’s main core and my USBHID library (which by default is composite serial / HID, though the serial must be accessed with CompositeSerial instead of Serial):
#include <USBHID.h>

void setup() {
USBHID.begin(HID_KEYBOARD_MOUSE);
Timer2.setPrescaleFactor(1024);
}


Leave a Reply

Your email address will not be published. Required fields are marked *