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
Parent:
2:bdf03de86660
diff -r f60e8aef85f7 -r 05f4ace9508a main.cpp
--- a/main.cpp	Tue Dec 13 05:31:51 2016 +0000
+++ b/main.cpp	Wed Dec 14 00:49:38 2016 +0000
@@ -59,7 +59,7 @@
 //
 #include "mbed.h"
 #include "math.h"
-#include "USBJoystick.h"
+#include "USBGamepad.h"
 
 
 #define DECL_EXTERNS
@@ -72,20 +72,8 @@
 // number of elements in an array
 #define countof(x) (sizeof(x)/sizeof((x)[0]))
 
-// floating point square of a number
-inline float square(float x) { return x*x; }
-
-// floating point rounding
-inline float round(float x) { return x > 0 ? floor(x + 0.5) : ceil(x - 0.5); }
-
-
 // --------------------------------------------------------------------------
 // 
-// USB product version number
-//
-const uint16_t USB_VERSION_NO = 0x0007;
-
-
 //
 // Build the full USB product ID.  If we're using the LedWiz compatible
 // vendor ID, the full product ID is the combination of the LedWiz base
@@ -217,11 +205,11 @@
 // Customization joystick subbclass
 //
 
-class MyUSBJoystick: public USBJoystick
+class MyUSBGamepad: public USBGamepad
 {
 public:
-    MyUSBJoystick(uint16_t vendor_id, uint16_t product_id, uint16_t product_release) 
-        : USBJoystick(vendor_id, product_id, product_release, true)
+    MyUSBGamepad(uint16_t vendor_id, uint16_t product_id, uint16_t product_release) 
+        : USBGamepad(vendor_id, product_id, product_release, true)
     {
         suspended_ = false;
     }
@@ -266,10 +254,7 @@
     // we don't need a reset yet
     bool needReset = false;
     // Create the joystick USB client.  
-    MyUSBJoystick js(
-        USB_VENDOR_ID, 
-        MAKE_USB_PRODUCT_ID(USB_VENDOR_ID, USB_PRODUCT_ID, 0x08),
-        USB_VERSION_NO);
+    MyUSBGamepad js(USB_VENDOR_ID,USB_PRODUCT_ID,USB_PRODUCT_VER); // vendor, product, product release
         
     // last report timer - we use this to throttle reports, since VP
     // doesn't want to hear from us more than about every 10ms
@@ -281,7 +266,7 @@
     hbTimer.start();
     int hb = 0;
     uint16_t hbcnt = 0;
-    uint16_t x=0,y=0,zrep=0;
+
     // we're all set up - now just loop, processing sensor reports and 
     // host requests
     for (;;)
@@ -289,35 +274,12 @@
         
         // update the buttons
         uint32_t buttons = readButtons();
-        uint16_t statusFlags;
 
-        // If it's been long enough since our last USB status report,
-        // send the new report.  We throttle the report rate because
-        // it can overwhelm the PC side if we report too frequently.
-        // VP only wants to sync with the real world in 10ms intervals,
-        // so reporting more frequently only creates i/o overhead
-        // without doing anything to improve the simulation.
+        
+        // don't poll too fast, this can outrun the host
         if (reportTimer.read_ms() > 15)
         {
-            // read the accelerometer
-            int xa, ya;
-            
-            // confine the results to our joystick axis range
-            if (xa < -JOYMAX) xa = -JOYMAX;
-            if (xa > JOYMAX) xa = JOYMAX;
-            if (ya < -JOYMAX) ya = -JOYMAX;
-            if (ya > JOYMAX) ya = JOYMAX;
-            
-            // store the updated accelerometer coordinates
-            x = xa;
-            y = ya;
-            
-            // Send the status report.  Note that we have to map the X and Y
-            // axes from the accelerometer to match the Windows joystick axes.
-            // The mapping is determined according to the mounting direction
-            // set in config.h via the ORIENTATION_xxx macros.
-            js.update(x, y, zrep, buttons, statusFlags);
-            
+            js.update(buttons);
             // we've just started a new report interval, so reset the timer
             reportTimer.reset();
         }