Author Topic: STM32 - button box for PC  (Read 2307 times)

0 Members and 1 Guest are viewing this topic.

Offline HackedTopic starter

  • Contributor
  • Posts: 30
  • Country: hr
STM32 - button box for PC
« on: March 08, 2023, 11:20:56 pm »
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-alu

For 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 example https://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?


 

Offline lucazader

  • Regular Contributor
  • *
  • Posts: 221
  • Country: au
Re: STM32 - button box for PC
« Reply #1 on: March 08, 2023, 11:40:31 pm »
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.
 
The following users thanked this post: thm_w

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6264
  • Country: fi
    • My home page and email address
Re: STM32 - button box for PC
« Reply #2 on: March 09, 2023, 01:15:17 am »
I can warmly recommend using a Teensy for this.  A Teensy LC would be just about perfect (and cost only USD $13), but they're unavailable due to the chipageddon going on (except TinyTronics says 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 from PJRC; 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's github 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 however open source, licensed under GPL 3+.)

Essentially, you use the Arduino environment with the Teensyduino add-on to program the Teensy boards.  (There is an active discussion board at https://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 page for 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 with SMD resistors and diodes (four 10k resistors and sixteen dual common-cathode Schottky diodes in SOT-23, like BAS70-05), and the other with all 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 digital Gamepad 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...
« Last Edit: March 09, 2023, 01:22:49 am by Nominal Animal »
 
The following users thanked this post: lucazader

Offline HackedTopic starter

  • Contributor
  • Posts: 30
  • Country: hr
Re: STM32 - button box for PC
« Reply #3 on: March 09, 2023, 07:34:59 am »
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.

So 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?

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.
F042 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.

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.
My plan is stick to STM32 family for now, and try to learn and became comfortable.
 

Online darkspr1te

  • Frequent Contributor
  • **
  • Posts: 290
  • Country: zm
Re: STM32 - button box for PC
« Reply #4 on: March 09, 2023, 08:13:11 am »
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.

So 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?

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.
F042 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.

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.
My plan is stick to STM32 family for now, and try to learn and became comfortable.
the 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
 

Offline voltsandjolts

  • Supporter
  • ****
  • Posts: 2300
  • Country: gb
Re: STM32 - button box for PC
« Reply #5 on: March 09, 2023, 08:16:05 am »
 
The following users thanked this post: paf

Offline HackedTopic starter

  • Contributor
  • Posts: 30
  • Country: hr
Re: STM32 - button box for PC
« Reply #6 on: March 09, 2023, 01:57:48 pm »
https://github.com/FreeJoy-Team/FreeJoy

That's great solution, and I am aware of it, but I would like to make the software by myself for learning sake.
 

Offline HackedTopic starter

  • Contributor
  • Posts: 30
  • Country: hr
Re: STM32 - button box for PC
« Reply #7 on: March 09, 2023, 02:16:25 pm »
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[HID_MOUSE_REPORT_DESC_SIZE]  __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?
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6264
  • Country: fi
    • My home page and email address
Re: STM32 - button box for PC
« Reply #8 on: March 09, 2023, 11:24:09 pm »
Use USB Descriptor and Request Parser to 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 USB Device Class Definition for HID ver. 1.11 (hid1_11.pdf) and HID 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: [Select]
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 Collection

0xC0,              // End Collection
// 58 bytes
Each 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=0x81 for left/up and 127=0x7F for right/down, and 0 when 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.
 

Offline HackedTopic starter

  • Contributor
  • Posts: 30
  • Country: hr
Re: STM32 - button box for PC
« Reply #9 on: March 21, 2023, 08:23:21 pm »
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?
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14481
  • Country: fr
Re: STM32 - button box for PC
« Reply #10 on: March 21, 2023, 10:09:28 pm »
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.
 

Online eutectique

  • Frequent Contributor
  • **
  • Posts: 392
  • Country: be
Re: STM32 - button box for PC
« Reply #11 on: March 21, 2023, 10:10:19 pm »
Yes, it is too small.

Compile the thing with -Os option. Add -ffunction-sections -fdata-sections to compiler flags (CFLAGS usually) and -Wl,--gc-sections to linker flags (LDFLAGS usually).
 

Online eutectique

  • Frequent Contributor
  • **
  • Posts: 392
  • Country: be
Re: STM32 - button box for PC
« Reply #12 on: March 21, 2023, 10:16:07 pm »
Or add -flto to both compiler and linker flags, link-time optimisation. Here Be Dragons™
 

Offline HackedTopic starter

  • Contributor
  • Posts: 30
  • Country: hr
Re: STM32 - button box for PC
« Reply #13 on: March 21, 2023, 10:31:17 pm »
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.
 

Online darkspr1te

  • Frequent Contributor
  • **
  • Posts: 290
  • Country: zm
Re: STM32 - button box for PC
« Reply #14 on: April 06, 2023, 10:46:09 am »
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.
Theres 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
 
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf