Spark Logic

[SOLVED] generic blue pill clone as USB keyboard

taxibunny Wed Apr 25, 2018 6:29 pm

Hiya, had a search around the forum, and it seems like some have got it working, while others haven’t. Instructions seem to be scattered and maybe out of date. Please forgive me if I’m asking an already settled question.

I’d like to set up my generic blue pill clone to act as a USB HID keyboard. I’ve followed the installation instructions at https://github.com/rogerclarkmelbourne/ … stallation , flashed the bootloader with the ‘hello world’ message over serial, and got the ‘blink’ example code to upload over USB too. it’s working. However, I can’t get any HID keyboard code working. ‘simplekeyboard’ in the ‘examples for generic STM32F103C series’ example sketches uploads, but doesn’t seem to do anything – the board still shows up in USBTreeViewer as ‘COM10 Maple Mini’, other suggestions I’ve found around the forum fail to compile due to not being able to find the libraries/me not being able to install the libraries properly.

Can anyone help? I’ve done a little bit of 8-bit AVR programming in Arduino before, and some python and other interpreted languages, but I’m lost when it comes to makefiles and what seems to be a whole different ball game when using libraries for the STM32.

Andy.

Replies

ag123 Thu Apr 26, 2018 2:26 pm

normally usb stuff is ‘not for beginners’ but nothing stop you from trying it out
USB HID / MIDI / Mass Storage as a library
viewtopic.php?f=13&p=43847#p43847

my guess is take some 74hc595 + 74hc165 it may be possible to put together a 64 keys keyboard and 2 of 74hc595 + 1 of 74hc165 you may be able to stretch it to 128 keys

the trouble is with super cheap 100 keys keyboards going for as little as us $10-20 these days, it may be simply too convenient compared to making a keyboard oneself
:lol:

oh i figured out something, yup there is still an incentive to build a ‘bluepill keyboard’, the entire keyboard become fully ‘programmable’
https://youtu.be/xFs2aE1NKEg
once you have a bluepill sitting between the ‘dumb’ keys and the usb port, the stm32 can remap any keys and do all sorts of tricks like single key macros and even all sorts of automation, e.g. double up as both a mouse & keyboard
my guess is that it can even do go the extreme of key automations like remember a whole sequence of key presses and mouse clicks and play them to the computer

ahull Thu Apr 26, 2018 3:56 pm

You can also use the USB keyboard device to do data entry from sensors, spoof keypresses, covert your favourite DEC VT200 keyboard to work with your rocketship laptop, make HUGE keyboards with keys you can step on, interface that old barcode reader, unobtainium peripheral, coffee machine to your rocketship laptop.. etc.

There are so many things you can plug in, to Windoze XXX (substitute value greater than six and less than 95) that don’t have valid drivers, but could be made to work, if you pretend they are a keyboard. HID keyboard emulation is the answer to pretty much everything that Windoze doesn’t want to talk to (which is pretty much everything made before 2012).

However this doesn’t answer the OP’s question.. does it actually work? Since I am the best part of 4,500 miles from any of my STM32 boards, I’m going to have to let someone else answer that.

florisla Thu Apr 26, 2018 10:31 pm

Making a keyboard is definitely possible, I have a 35key ergonomic ‘half’ keyboard prototype that’s working just fine.

You don’t need shift register chips to scan a large amount of keys – connect the keys via rows and columns and you can already read 64 keys with 8+8 pins.

The forum thread about arpruss’ USB library (referenced by ag123 above) is the right place to start.
My prototype is still using libarra’s core but I’m going to switch to arpruss’ lib now that it’s included in Roger’s core.

Edit: take a look here, especially the README and the examples folder: https://github.com/rogerclarkmelbourne/ … BComposite .

ag123 Fri Apr 27, 2018 5:15 am

perhaps after we make that stm32 blue pill keyboard , this become very popular & the ebay, aliexpress manufacturers start to make these stm32 keyboards en-mass & it become the new keyboard ‘standard’ – fully re-programmable keyboards & mice :lol:

florisla Fri Apr 27, 2018 9:45 am

Most programmable keyboards are running on ATmega32u4 (SparkFun Pro Micro, Teensy/Teensy++ 2.0) or NXP Kinetis (Teensy 3.x).

The Pro Micro is very cheap on AliExpress as well, but it does not have enough IOs for a full-size keyboard. It’s often used in ‘half” (split) keyboards, numeric pads, or downsized keyboards (e.g. lacking numeric pad and/or F-keys).

The Teensy boards on the other are (relatively) expensive.

I think keyboard manufacturers would be very wise to offer reprogrammability — firmware like the QMK project has lots of interesting features that they could offer for free. https://github.com/qmk/qmk_firmware/

The problem with current mass-market keyboards is that they don’t have a bootloader nor exposed programming pins.

ag123 Fri Apr 27, 2018 3:08 pm

somewhat off topic, but i googled and run into this
https://kotaku.com/how-to-build-your-ow … 1797070430
for sure it turns out very pricy vs the today’s internally silicone membrane style keyboards :lol:
then following the clues i did some searches on aliexpress
and run into things like
https://www.aliexpress.com/w/wholesale- … d-pcb.html
^^ has various diy keyboard pcbs for gateron / cherry switches, but the controllers aren’t stm32, didn’t seem to find any !
https://www.aliexpress.com/wholesale?Se … rry+switch
^^ the keyboard switches
https://www.aliexpress.com/wholesale?Se … ps+gateron
then the keycaps – apparently this turns up some keyboard switches as well
and some pretty good clues stumbled upon while googling
https://www.reddit.com/r/MechanicalKeyb … b/cs9hnzx/
https://deskthority.net/workshop-f7/bro … t6050.html

i’m wondering if it may be easier / cheaper to hack an existing keyboard and pull wires to the bluepill/maple mini headers
:lol:

ag123 Sat Apr 28, 2018 5:54 pm

come to think of it blue pill / maple mini (stm32f103) is an *ideal* keyboard controller, the scanning can be done with assistance of shift registers e.g. 74hc595 + 74hc165, this save lots of pins, then the pins that is freed up can be used for other purposes e.g. it can run usb mass storage & map a sd card to a drive and it can even at the same time double up as a mouse when paired with an optical sensor e.g. the ADNS 3050
https://media.digikey.com/pdf/Data%20Sh … S-3050.pdf
so it become a 3 in 1 or even many in 1 device (keyboard, usb-mass storage, mouse)
:)

madias Sun Apr 29, 2018 6:57 pm

[ag123 – Sat Apr 28, 2018 5:54 pm] –
come to think of it blue pill / maple mini (stm32f103) is an *ideal* keyboard controller,

I agree, but combined with one of the best keyboards ever made (I really still missing it):
https://hackadaycom.files.wordpress.com … l_f_xt.png

taxibunny Mon Apr 30, 2018 10:32 pm

I think the HID library is working, sortof. it’s just that Windows won’t pick it up, on either of my laptops. Some generic error, apparently. Wasn’t much help. The HID storage *is* picked up by something, as it gives the ‘can’t boot from this disk, please try another one’ type message. Next step, I suppose, is to install Linux on a USB stick, and see what that makes of it. Either that or dig around the forums again to see if I can get it working 100%.

One obvious oddity is that it continues to show up as ‘maple serial’ even though the example code I used is supposed to not do that. A fix that has been suggested is to delete all knowledge of ‘maple serial’ from Windows and hope when it autodetects again that it gets it right. I haven’t tried that yet though.

The physical keyboard is already made – I’m using the keyboard and membrane from a Cherry MY series ex cash-register type keyboard that uses to have a built-in magnetic stripe card reader. I’ve gutted it, extended the sides up to give more room for a raspberry pi, soundcard, and dc-dc power supply converter. I’m going to put a few knobs and a hd44780 screen in the top, and a couple of MIDI sockets in the back, and it’ll eventually be some sort of programmable musical instrument.

I killed the keyboard controller PCB by accident though, hence the need for a replacement. It’s a 19×8 matrix, and the arduino pro micro doesn’t have enough pins, and my bit of prototype PCB with shift registers on it was looking a mess. So I thought I’d try this.

Thanks for the replies so far. They have been helpful!

Andy.

madias Mon Apr 30, 2018 10:44 pm

[taxibunny – Mon Apr 30, 2018 10:32 pm] –
I think the HID library is working, sortof. it’s just that Windows won’t pick it up, on either of my laptops. Some generic error, apparently. Wasn’t much help. The HID storage *is* picked up by something, as it gives the ‘can’t boot from this disk, please try another one’ type message. Next step, I suppose, is to install Linux on a USB stick, and see what that makes of it. Either that or dig around the forums again to see if I can get it working 100%.

One obvious oddity is that it continues to show up as ‘maple serial’ even though the example code I used is supposed to not do that. A fix that has been suggested is to delete all knowledge of ‘maple serial’ from Windows and hope when it autodetects again that it gets it right. I haven’t tried that yet though.

Andy.

Hm. I also played with USB HID (more MIDI stuff) in the past and I went to eliminate the bootloader (the bootloader does “Serial.begin()” for the “magic sequence”!) and I only used a ST-Link for uploading getting rid of any unwanted maple-serial stuff. Maybe a simple
Serial.end();

arpruss Wed May 02, 2018 1:50 am

Does the SimpleKeyboard example work?

You might try changing the product ID if you’re having trouble getting the keyboard recognized. Just call USBComposite.setProductId() before calling the other setup functions.

taxibunny Wed May 02, 2018 6:10 am

[arpruss – Wed May 02, 2018 1:50 am] –
Does the SimpleKeyboard example work?

You might try changing the product ID if you’re having trouble getting the keyboard recognized. Just call USBComposite.setProductId() before calling the other setup functions.

Well, following this suggestion, and the ‘serial.end()’ suggestion, things have maybe improved. It’s still not working, but there are less errors reported in USB Device Tree Viewer. I did your thing setProductId() and put a USB keyboard product ID hex value in that I got from this page: https://github.com/obdev/v-usb/blob/mas … r-free.txt . I also added a line to set vendor ID, just in case. I guess at this point I’ll post the code. It’s the SimpleKeyboard example, with the suggested tweaks:

#include <USBComposite.h>

void setup() {
Serial.end();
USBComposite.setProductId(0x27db);
USBComposite.setVendorId(0x16c0);
USBHID_begin_with_serial(HID_KEYBOARD);
Keyboard.begin(); // useful to detect host capslock state and LEDs
delay(1000);
}

void loop() {
Keyboard.println("Hello world");
delay(10000);
}

taxibunny Wed May 02, 2018 9:55 am

Uploaded using over serial – no good. Device Tree viewer now just shows ‘Unknown USB Device (Device Descriptor Request Failed)’ :/

Will now reflash the bootloader.

arpruss Wed May 02, 2018 1:49 pm

Don’t call Serial.end(). The USB Composite library overwrites the standard USB serial init code, so Serial.end() isn’t needed (and might even screw something up, since Serial.begin() has not been called).

taxibunny Wed May 02, 2018 9:50 pm

Success!

I removed the line Serial.end(), but no change.

Next, I ran Zadig 2.3 winUSB driver installer, creating a new device with the product and vendor ID from the sketch, and gave it the name ‘teensy keyboard’ (which is what the IDs correspond to, I found). I don’t actually think that this helped, I’m just including this info for completeness.

What seemed to do the trick was uninstalling the serial device and deleting drivers, as per this thread from the forum: viewtopic.php?f=13&t=576&start=160&hili … d+keyboard , specifically the part “
To enable the HID device, I removed and deleted the serial driver and then scanned for hardware changes —
in which case the HID keyboard appeared (1EAF 0004 MI_02 Col02).” – though in my case, it shows up in the device manager as ‘USB input device’ in the ‘human interface devices’ section, and ‘USB composite device (COM10)’ in the ‘Ports (COM & LPT)’ section, which seems to mean it’s fully working, although I haven’t tried the serial over USB yet. Uploading the plain unmodified simplekeyboard example sketch without the added product ID/vendor ID works too.

Thanks, arpruss, I see it is your library that I’m using, that you’ve put a lot of work into. I’m very grateful for that and for your input in this thread. Time to mark this one as [SOLVED] :)

TL;DR for others: Try removing and deleting the serial driver, then resetting the board and letting windows autodetect again, as previously discussed in another thread.

florisla Thu May 03, 2018 3:02 pm

I just migrated from libarra’s core to arpruss’ lib in Roger’s core, and was bitten by this issue again.

I can confirm that given the above installation, CompositeSerial works fine together with USB HID keyboard.

[taxibunny – Wed May 02, 2018 9:50 pm] –
Thanks, arpruss, I see it is your library that I’m using, that you’ve put a lot of work into. I’m very grateful for that and for your input in this thread.

I second that — very grateful for your (and roger’s, and stevestrong’s, and…) contributions!

STMdude Fri May 04, 2018 4:48 pm

To be brutally honest, the Arduino “Leonardo/Micro” clones and mini-boards that use the ATmega 32U4 do exactly what you want, and can be had for peanuts!

See: https://www.ebay.ca/itm/141977600303

There’s a very robust USB library for that chip. So, unless you need the STM32 functionality for something in particular, there’s not much point re-inventing the wheel… ;)

taxibunny Fri May 04, 2018 9:48 pm

Blue pill has enough digital pins to connect my 19×9 matrix straight to it though, and it might even be cheaper than the pro micro nowadays..

ag123 Sat May 05, 2018 5:29 am

this thread is pretty motivating as making a custom stm32duino (bluepill / maple mini) keyboard looks not only pretty interesting but is apparently rather feasible :D

2 relevant links:
https://www.reddit.com/r/MechanicalKeyb … b/cs9hnzx/

So you want to build a custom keyboard? It’s both easier and harder than you think.

  • Open the DXF in a CAD tool, save units as MM
  • Send the DXF to bigbluesaw, wait a couple weeks for your plate to get here << my thoughts are you could approach any shops in your city with laser cutting services

The switches you have will work fine unmodified. PCB mount switches can be used everywhere a plate mount switch can no problem.
https://deskthority.net/workshop-f7/bro … t6050.html

on aliexpress searches for cherry mx and gateron turns things as such up
https://www.aliexpress.com/item/87Pcs-K … 36743.html

and as we define its role over usb, it is much more than a keyboard, literally doubling up as any other usb device
(keyboard + oscilloscope? why not)

i’m thinking however using shift registers 74hc595, 74hc165 would still be useful as that would save lots of pins on the bp / mm leaving those pins available for other purposes

Sign in to reply