STM32 - button box for PC - Page 1
Forum section: electronics / general

author: Hacked
author_rank: Contributor
timestamp: March 08, 2023, 11:20:56 pm
content: Hello all !I am doing little project, button box with ofcourse buttons, rotary encoders etc...Plan is to be connected to PC via USB and map the buttons in game (for example iRacing, asseto corsa etc...).For example:https://www.polsimer.pl/strona-produktu/gt2-button-plate-aluFor that I can't use regular STM32 M0+ without USB interface and use CH340 or similar UART to USB chip?From my understanding of topic I need to get this micro for examplehttps://www.st.com/en/microcontrollers-microprocessors/stm32f042k6.html,which has USB interface, because I need use USB HID classes?Is that micro enough for what i planned? Or do I need some other? Or it's possible do that without USB interface and just use CH340 or similar UART to USB chip?

author: lucazader
author_rank: Regular Contributor
timestamp: March 08, 2023, 11:40:31 pm
content: The main reason to go with USB HID is that you wont really need to write windows drivers. it will mostly "just work (tm)"If you want to use a CH340 USB<->UART converter this will show up as a com-port device on you computer.Which means youll need some way of interpreting this data and then feeing those commands into your OS of choice.ST have quite a lot of USB FS device capable micros that would do this task nicely.The F042 is one of them but there are plenty in the F0, G0 and other product familys that would work.You could also look into using other MCUs from other manufacturers.For example the ESP32-S3 has a USB-FS peripheral now. and this can be had for quite cheap in large quantities nad generally is much more available than most ST parts.You then get the benefit of enabling the BT/BLE peripheral to also potentially make you button box wireless in the future!The esp32 is also used by lots of hobbyist's so there are lots of good guides out there on youtube etc.

author: Nominal Animal
author_rank: Super Contributor
timestamp: March 09, 2023, 01:15:17 am
content: I can warmly recommend using aTeensyfor this. A Teensy LC would be just about perfect (and cost only USD $13), but they're unavailable due to the chipageddon going on (exceptTinyTronicssays they have them in stock for 20€ incl. VAT). A Teensy 4.0 is a good alternative. I recommend getting them from reputable sellers (and not e.g. eBay), because the bootloader chip (NXP MKL02) contains a custom, preprogrammed proprietary bootloader, and the Chinese Teensy clones do not. (You can, however, make your own Teensy compatible boards yourself, and buy the chip fromPJRC; you just musn't include "teensy" in the name of your gadget, for trademark protection reasons.)Aside from the bootloader chip, basically all of the Teensy-related sources are available on the net, at Paul Stoffregen'sgithub repositories. (I do believe the only thing not open source, aside from the proprietary bootloader chip, is the GUI loader utility. The command-line one, as well as the protocol used to program Teensies, is howeveropen source, licensed under GPL 3+.)Essentially, you use theArduino environment with the Teensyduino add-onto program the Teensy boards. (There is an active discussion board athttps://forum.pjrc.com/regarding all Teensy boards and programming them. They are commonly used for HID devices like keyboards and flight simulator cockpits.)Thing is, the Teensyduino add-on provides easy support for many USB types; for example, Serial + Keyboard + Mouse + Joystick, or Flight Sim Controls + Joystick, or even Raw HID. You just need to select the Teensy board in the Arduino IDE, the USB type, and the keyboard layout if the USB type includes a keyboard, and very little Arduino code to get it working.See the Joystick example pagefor example.Keyboard, Mouse, Joystick, Touch Screen, Raw HID, and Flight Sim Controls are all native USB HID interfaces, and do not require any drivers from the target operating system. You just plug in the Teensy, and it will work. (Some OSes have idiosyncracies, and I've heard Mac OS has issues with the Touch Screen, and Windows is a bit particular on the number of axes etc. on Joystics, but AIUI they are rare and can be worked around.)You can get something working literally within minutes.There are even Teensy-optimized libraries for debouncing buttons, reading encoders, and so on. They are permissively licensed, so there is no obligation to open source your own code.Years ago, I designed a couple of cheap and simple carrier board for Teensy LC, one withSMD resistors and diodes(four 10k resistors and sixteen dual common-cathode Schottky diodes in SOT-23, like BAS70-05), and the other withall through-hole components(four 10k resistors and thirty-two Schottky diodes). Both support up to 32 buttons –– using two wires per button, wires soldered to the board –– and 9 potentiometers (I suggest 10k linear), three wires per potentiometer. The carrier itself is in Public Domain, and you can manufacture a few at JLCPCB or PCBWay for a couple of USD plus shipping. IIRC, this was to help someone with their arcade cabinet.I've also designed a 4+4+2-button digitalGamepad using a Pro Micro clone(back when ATmega32u4-based Pro Micro clones were cheap and plentiful at eBay) with option for a 128×32 I2C OLED display for choosing the button-to-keypress mapping at run time (using the two smaller buttons) –– it can generate both joystick and keyboard events. The idea is that there is one board smaller than 100×100mm that forms a bottom and a top plate. Top plate has no circuitry on it, and even the bottom plate is just tracks only, no components. The two buttons are 6x6mm tactiles with tall buttons, and the eight are standard 12×12mm tactiles with round hats. I never did manufacture this one, though.I do recommend trying out stuff on breadboards (or rather, using wires, perhaps ribbon cable, soldered to the board) first, then using EasyEDA or KiCAD to design simple carrier board with suitable shape and/or fastening holes, manufacturing it at e.g. JLCPCB or PCBWay (cheap!), and maybe 3D-printing a shell or fascia for the HID device. I personally end up playing with modeling clays and air-curing clays to get the right "feel" for my sausage-fingered hands...

author: Hacked
author_rank: Contributor
timestamp: March 09, 2023, 07:34:59 am
content: Quote from: lucazader on March 08, 2023, 11:40:31 pmSo I would need to write application in C# to interpreter data? And for example some user using my button box would first need to install that driver I wrote?Quote from: lucazader on March 08, 2023, 11:40:31 pmF042 I linked is the cheapest one available. 32 pins is ideal for this project, enough I/O pins for inputs/outputs, and easy to solder.Although I was worried if you need to add extra components between MCU and USB connector, but it seems according to manual, I need only LDO(5V to 3.3V) to power the MCU.So this solution would be easiest and probably cheapest? F042 is 4-5€ a piece, and STM32F030K6T6 is 2€ but I need also CH340 USB<->UART converter so it's probably more expensive in the end, and more complicated to do.Quote from: lucazader on March 08, 2023, 11:40:31 pmMy plan is stick to STM32 family for now, and try to learn and became comfortable.

author: darkspr1te
author_rank: Frequent Contributor
timestamp: March 09, 2023, 08:13:11 am
content: Quote from: Hacked on March 09, 2023, 07:34:59 amthe stm32 range has USB device ability and there are many midi, gamepad, HID sample code out there in Arduino, stm32 cube and platformio systems.these are driverless solutions so should work on mac, windows, linux .darkspr1te

author: voltsandjolts
author_rank: Unknown
timestamp: March 09, 2023, 08:16:05 am
content: https://github.com/FreeJoy-Team/FreeJoy

author: Hacked
author_rank: Contributor
timestamp: March 09, 2023, 01:57:48 pm
content: Quote from: voltsandjolts on March 09, 2023, 08:16:05 amThat's great solution, and I am aware of it, but I would like to make the software by myself for learning sake.

author: Hacked
author_rank: Contributor
timestamp: March 09, 2023, 02:16:25 pm
content: Okay I grasped few concepts about USB HID.If I understanded correctly, in usbd_hid.c file I need to make changes to mouse report description:__ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc __ALIGN_END ={0x05, 0x01,0x09, 0x02,0xA1, 0x01,0x09, 0x01,0xA1, 0x00,0x05, 0x09,0x19, 0x01,0x29, 0x03,0x15, 0x00,0x25, 0x01,0x95, 0x03,0x75, 0x01,0x81, 0x02,0x95, 0x01,0x75, 0x05,0x81, 0x01,0x05, 0x01,0x09, 0x30,0x09, 0x31,0x09, 0x38,0x15, 0x81,0x25, 0x7F,0x75, 0x08,0x95, 0x03,0x81, 0x06,0xC0, 0x09,0x3c, 0x05,0xff, 0x09,0x01, 0x15,0x00, 0x25,0x01, 0x75,0x01, 0x95,0x02, 0xb1,0x22, 0x75,0x06, 0x95,0x01, 0xb1,0x01, 0xc0};Second row I need to change from 0x09, 0x02 to 0x09, 0x05 USAGE (Game Pad), right?And change inpus, for example i will have 16 buttons:USAGE_PAGE (Button)USAGE_MINIMUM (Button 1)USAGE_MAXIMUM (Button 16)LOGICAL_MINIMUM (0)LOGICAL_MAXIMUM (1)REPORT_COUNT (16)REPORT_SIZE (1)INPUT (Data,Var,Abs)and wrap it around collection.Is this the right approach?

author: Nominal Animal
author_rank: Super Contributor
timestamp: March 09, 2023, 11:24:09 pm
content: UseUSB Descriptor and Request Parserto check. Just paste the values as-is (between the braces, not including the braces, i.e. 0x05, 0x01, ..., 0x01, 0xc0) into the input box, and click 'USB HID Report Descriptor' button (or 'I do not know, make a guess for me'), and it will parse, reformat, and comment the fields.(As usual, the USBDevice Class Definition for HID ver. 1.11 (hid1_11.pdf)andHID Usage Tables ver. 1.4 (hut1_4.pdf)are definitely useful here.)For example, a gamepad with X and Y axes (be they analog or digital), up to 14 buttons, plus Start and Select, might have a report descriptor (UNTESTED!):Code:0x05, 0x01, // Usage Page (Generic Desktop Ctrls)0x09, 0x05, // Usage (Game Pad)0xA1, 0x01, // Collection (Application)0x05, 0x09, // Usage Page (Button)0x19, 0x01, // Usage Minimum (0x01)0x29, 0x0E, // Usage Maximum (0x0E)0x15, 0x00, // Logical Minimum (0)0x25, 0x01, // Logical Maximum (1)0x75, 0x01, // Report Size (1)0x95, 0x0E, // Report Count (14)0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)0x05, 0x01, // Usage Page (Generic Desktop Ctrls)0x09, 0x3D, // Usage (Start)0x09, 0x3E, // Usage (Select)0x15, 0x00, // Logical Minimum (0)0x25, 0x01, // Logical Maximum (1)0x75, 0x01, // Report Size (1)0x95, 0x02, // Report Count (2)0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)0xA1, 0x00, // Collection (Physical)0x05, 0x01, // Usage Page (Generic Desktop Ctrls)0x09, 0x30, // Usage (X)0x09, 0x31, // Usage (Y)0x15, 0x81, // Logical Minimum (-127)0x25, 0x7F, // Logical Maximum (127)0x75, 0x08, // Report Size (8)0x95, 0x02, // Report Count (2)0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)0xC0, // End Collection0xC0, // End Collection// 58 bytesEach report has four data bytes. There is no report ID byte. First two bytes contain the button states, one bit per button, with Start and Select last (the two MSBs in the second byte). Next two bytes are 8-bit signed integers in two's complement format, containing the X and Y axes. For binary buttons, use-127=0x81for left/up and127=0x7Ffor right/down, and0when neither button on that axis is pressed.Note that if you have 10 physical buttons in addition to Start, Select, and digital (not analog) X and Y axes, you can duplicate left/right/up/down as buttons 11-14 for added flexibility in button mapping.

author: Hacked
author_rank: Contributor
timestamp: March 21, 2023, 08:23:21 pm
content: Hey all,Finally i have time to test it, found blue pill home (stm32f103c6t6, 32kB version), and when I set up USB in HAL configuration it gives me compiling error: "region `FLASH' overflowed by 936 bytes".So 32kB is too small for USB HAL drivers?

author: SiliconWizard
author_rank: Super Contributor
timestamp: March 21, 2023, 10:09:28 pm
content: Apart from the above, the usual suspect for this kind of stuff, and very cheap, is the RP2040.Absolutely overkill for a "button box" or even for a complete keyboard, but so cheap that it's not worth fretting on wasted CPU power. Supports USB FS and developing a HID device with TinyUSB is quite simple.

author: eutectique
author_rank: Frequent Contributor
timestamp: March 21, 2023, 10:10:19 pm
content: Yes, it is too small.Compile the thing with-Osoption. Add-ffunction-sections -fdata-sectionsto compiler flags (CFLAGSusually) and-Wl,--gc-sectionsto linker flags (LDFLAGSusually).

author: eutectique
author_rank: Frequent Contributor
timestamp: March 21, 2023, 10:16:07 pm
content: Or add-fltoto both compiler and linker flags, link-time optimisation.Here Be Dragons™

author: Hacked
author_rank: Contributor
timestamp: March 21, 2023, 10:31:17 pm
content: Actually I have Pi pico board RP2040 so I can give it a go, dunno. I also have stm32 black pill. They are both similar price ( both boards are available for 5€ approx).Probably will try first with black pill because I am familiar with stm.

author: darkspr1te
author_rank: Frequent Contributor
timestamp: April 06, 2023, 10:46:09 am
content: Quote from: SiliconWizard on March 21, 2023, 10:09:28 pmTheres rumours around that you can actually flash beyond the stated flashsize , i use this trick with black magic probe firmware on myblackpills but it's prob better with optimized build file rather than hacks that could not always work and provide a nice rabbit hole .darkspr1te