This is a work in progress. Trying to make a working arcade button to USB interface with the Nucleo F411RE.

Dependencies:   USBDevice mbed

This project was inspired by USB Joystick Device

The goal of this project is to build an simple interface that I will connect numerous Arcade buttons and joysticks to the Nucleo board, and the Nucleo will present itself as a USB Gamepad to Windows or Linux. The joysticks are Arcade joysticks that have four switches, up/down/left/right. It is not an analog joystick, just a set of switches. These will be connected to the Nucleo's pins configured as inputs.

The code will then continuously loop and test which buttons are closed, and transfer this data to the host via USBGamepad protocol.

Much thanks to Wim Huiskamp for his original project, documentation, and later reaching out to me to guide me to solving a last minute problem. Wim clued me into the fact that I needed a 1k5 pullup resistor between the 3v3 pin and the PA_12/D+ pin. Once I did this Windows detected the device as a Gamepad and registered it correctly! Yay!

Connecting USB cable to the board is as follows:

You will need a USB data cable (the one I used had a micro usb on one end and regular usb on the other). I cut off the micro USB end, and cut the insulation back about 30mm. This exposed four wires, Red, Black, White and Green. You will then either crimp some header connectors or solder directly to the Nucleo header pins as follows:

  • Green USB D+ to PA_12
  • White USB D- to PA_11
  • Red USB 5V to E5V (with jumper JP5 set to E5V)
  • Black USB GND to GND

As an extra debugging measure, you can connect both the ST/Link USB and the PA_12/11 USB to the Windows machine to run both at the same time, and you can see printf messages from within ST/Link.

We can verify the HID Vendor and Product IDs by looking at the Device Manager, and look for the HID Game Controller:

/media/uploads/thetazzbot/arcade_controller_hid.jpg

I used pid.codes to register my own Vendor_ID and Product_ID

If you go to the USB Game Controller control panel widget, you will see the new entry for Arcade Gamepad:

/media/uploads/thetazzbot/arcade_controller_controlpanel.jpg

And here we can see all 32 buttons:

/media/uploads/thetazzbot/arcade_controller_buttons.jpg

On the Nucleo board you may have difficulties depending on the revision. The board I am using is an STM32F411RE Revision C03, which has resistors and solder joints (bottom) to allow the use of the Crystal on the STLink board for USB purposes. After programming via STLink, remove the USB cable from the STLink, the jumper must be set to E5V to power the board from the PC's usb port. Plug the new cable into the PC.

When you're ready to install it in the arcade cabinet, or project, just remember to setup the jumper JP5 to E5V and you only need the single USB connection to the host.

Here are some useful links that I used to grasp all the little things involved in this project:

Revision:
4:05f4ace9508a
Child:
6:29a04fe27b5e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBGamepad.h	Wed Dec 14 00:49:38 2016 +0000
@@ -0,0 +1,137 @@
+/* USBGamepad.h */
+/* USB device example: Gamepad*/
+/* Arcade style buttons to USB interface for use with RetroPie */
+
+ 
+#ifndef USBGAMEPAD_H
+#define USBGAMEPAD_H
+ 
+#include "USBHID.h"
+ 
+#define REPORT_ID_JOYSTICK  4
+ 
+ // Length of our report.  This equates to four bytes (32 bits=32 buttons)
+// Important: This must be kept in sync 
+// with the actual joystick report format sent in update().
+#define REPORT_LEN 0x04
+
+/* Common usage */
+enum JOY_BUTTON {
+     JOY_B0 = 0x0001,
+     JOY_B1 = 0x0002,
+     JOY_B2 = 0x0004,
+     JOY_B3 = 0x0008,
+     JOY_B4 = 0x0010,
+     JOY_B5 = 0x0020,
+     JOY_B6 = 0x0040,
+     JOY_B7 = 0x0080,
+     JOY_B8 = 0x0100,
+     JOY_B9 = 0x0200,
+     JOY_B10 = 0x0400,
+     JOY_B11 = 0x0800,
+     JOY_B12 = 0x1000,
+     JOY_B13 = 0x2000,
+     JOY_B14 = 0x4000,
+     JOY_B15 = 0x8000
+};
+ 
+/**
+ *
+ * USBJoystick example
+ * @code
+ * #include "mbed.h"
+ * #include "USBJoystick.h"
+ *
+ * USBJoystick joystick;
+ *
+ * int main(void)
+ * {
+ *   while (1)
+ *   {
+ *      joystick.move(20, 0);
+ *      wait(0.5);
+ *   }
+ * }
+ *
+ * @endcode
+ *
+ *
+ * @code
+ * #include "mbed.h"
+ * #include "USBJoystick.h"
+ * #include <math.h>
+ *
+ * USBJoystick joystick;
+ *
+ * int main(void)
+ * {   
+ *   while (1) {
+ *       // Basic Joystick
+ *       joystick.update(tx, y, z, buttonBits);
+ *       wait(0.001);
+ *   }
+ * }
+ * @endcode
+ */
+ 
+ 
+class USBGamepad: public USBHID {
+   public:
+ 
+        /**
+         *   Constructor
+         *
+         * @param vendor_id Your vendor_id (default: 0x1234)
+         * @param product_id Your product_id (default: 0x0002)
+         * @param product_release Your product_release (default: 0x0001)
+         */
+         USBGamepad(uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0100, uint16_t product_release = 0x0001, int waitForConnect = true): 
+             USBHID(0,REPORT_LEN, vendor_id, product_id, product_release, false)
+             { 
+                 _init();
+                 connect(waitForConnect);
+             };
+         
+         /**
+         * Write a state of the USBGamepad
+         *
+         * @param buttons buttons state, as a bit mask (combination with '|' of JOY_Bn values)
+         * @returns true if there is no error, false otherwise
+         */
+         bool update(uint32_t buttons);
+         
+         /**
+         * Write a state of the USBGamepad
+         *
+         * @returns true if there is no error, false otherwise
+         */
+         bool update();
+         
+         /**
+         * Press one or several buttons
+         *
+         * @param buttons button state, as a bitwise combination of JOY_Bn values
+         * @returns true if there is no error, false otherwise
+         */
+         bool buttons(uint32_t buttons);
+         
+         /*
+         * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
+         *
+         * @returns pointer to the report descriptor
+         */
+         virtual uint8_t * reportDesc();
+ 
+         /* USB descriptor string overrides */
+         virtual uint8_t *stringImanufacturerDesc();
+         virtual uint8_t *stringIserialDesc();
+         virtual uint8_t *stringIproductDesc();
+ 
+     private:
+         uint16_t _buttonsLo;
+         uint16_t _buttonsHi;
+         
+         void _init();                 
+};
+ 
+#endif
\ No newline at end of file