From 42ecec69284df530bacbad81944fa206290536fd Mon Sep 17 00:00:00 2001 From: foamyguy Date: Thu, 17 Apr 2025 11:50:08 -0500 Subject: [PATCH 1/5] USB SNES-like controller example for arduino --- .../Arduino_USB_Host/gamepad_reports.h | 36 ++++ .../snes_gamepad_simpletest.ino.ino | 182 ++++++++++++++++++ .../Arduino_USB_Host/usbh_helper.h | 103 ++++++++++ 3 files changed, 321 insertions(+) create mode 100644 USB_SNES_Gamepad/Arduino_USB_Host/gamepad_reports.h create mode 100644 USB_SNES_Gamepad/Arduino_USB_Host/snes_gamepad_simpletest.ino.ino create mode 100644 USB_SNES_Gamepad/Arduino_USB_Host/usbh_helper.h diff --git a/USB_SNES_Gamepad/Arduino_USB_Host/gamepad_reports.h b/USB_SNES_Gamepad/Arduino_USB_Host/gamepad_reports.h new file mode 100644 index 000000000..aea7c259f --- /dev/null +++ b/USB_SNES_Gamepad/Arduino_USB_Host/gamepad_reports.h @@ -0,0 +1,36 @@ +// SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +// HID reports for USB SNES-like controller + +// Byte indices for the gamepad report +#define BYTE_DPAD_LEFT_RIGHT 0 // D-Pad left and right +#define BYTE_DPAD_UP_DOWN 1 // D-Pad up and down +// bytes 2,3,4 unused +#define BYTE_ABXY_BUTTONS 5 // A, B, X, Y +#define BYTE_OTHER_BUTTONS 6 // Shoulders, start, and select + + +#define DPAD_NEUTRAL 0x7F +// D-Pad directions +#define DPAD_UP 0x00 +#define DPAD_RIGHT 0xFF +#define DPAD_DOWN 0xFF +#define DPAD_LEFT 0x00 + + +// Face buttons (Byte[5]) +#define BUTTON_NEUTRAL 0x0F +#define BUTTON_X 0x1F +#define BUTTON_A 0x2F +#define BUTTON_B 0x4F +#define BUTTON_Y 0x8F + + +// Miscellaneous buttons (Byte[6]) +#define BUTTON_MISC_NEUTRAL 0x00 +#define BUTTON_LEFT_SHOULDER 0x01 +#define BUTTON_RIGHT_SHOULDER 0x02 +#define BUTTON_SELECT 0x10 +#define BUTTON_START 0x20 diff --git a/USB_SNES_Gamepad/Arduino_USB_Host/snes_gamepad_simpletest.ino.ino b/USB_SNES_Gamepad/Arduino_USB_Host/snes_gamepad_simpletest.ino.ino new file mode 100644 index 000000000..d06cb6ba2 --- /dev/null +++ b/USB_SNES_Gamepad/Arduino_USB_Host/snes_gamepad_simpletest.ino.ino @@ -0,0 +1,182 @@ +// SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +/********************************************************************* + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + Copyright (c) 2019 Ha Thach for Adafruit Industries + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +/* This example demonstrates use of usb host with a SNES-like game controller + * - Host depends on MCU: + * - rp2040: bit-banging 2 GPIOs with Pico-PIO-USB library (roothub port1) + * + * Requirements: + * - For rp2040: + * - Pico-PIO-USB library + * - 2 consecutive GPIOs: D+ is defined by PIN_USB_HOST_DP, D- = D+ +1 + * - Provide VBus (5v) and GND for peripheral + * - CPU Speed must be either 120 or 240 MHz. Selected via "Menu -> CPU Speed" + */ + +// USBHost is defined in usbh_helper.h +#include "usbh_helper.h" +#include "tusb.h" +#include "Adafruit_TinyUSB.h" +#include "gamepad_reports.h" + +// HID report descriptor using TinyUSB's template +// Single Report (no ID) descriptor +uint8_t const desc_hid_report[] = { + TUD_HID_REPORT_DESC_GAMEPAD() +}; + +// USB HID object +Adafruit_USBD_HID usb_hid; + +// Report payload defined in src/class/hid/hid.h +// - For Gamepad Button Bit Mask see hid_gamepad_button_bm_t +// - For Gamepad Hat Bit Mask see hid_gamepad_hat_t +hid_gamepad_report_t gp; + +bool printed_blank = false; + +void setup() { + if (!TinyUSBDevice.isInitialized()) { + TinyUSBDevice.begin(0); + } + Serial.begin(115200); + // Setup HID + usb_hid.setPollInterval(2); + usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report)); + usb_hid.begin(); + + // If already enumerated, additional class driverr begin() e.g msc, hid, midi won't take effect until re-enumeration + if (TinyUSBDevice.mounted()) { + TinyUSBDevice.detach(); + delay(10); + TinyUSBDevice.attach(); + } +} + +#if defined(ARDUINO_ARCH_RP2040) +//--------------------------------------------------------------------+ +// For RP2040 use both core0 for device stack, core1 for host stack +//--------------------------------------------------------------------// + +//------------- Core0 -------------// +void loop() { +} + +//------------- Core1 -------------// +void setup1() { + // configure pio-usb: defined in usbh_helper.h + rp2040_configure_pio_usb(); + + // run host stack on controller (rhport) 1 + // Note: For rp2040 pico-pio-usb, calling USBHost.begin() on core1 will have most of the + // host bit-banging processing works done in core1 to free up core0 for other works + USBHost.begin(1); +} + +void loop1() { + USBHost.task(); + Serial.flush(); + +} +#endif + +//--------------------------------------------------------------------+ +// HID Host Callback Functions +//--------------------------------------------------------------------+ + +void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len) +{ + Serial.printf("HID device mounted (address %d, instance %d)\n", dev_addr, instance); + + // Start receiving HID reports + if (!tuh_hid_receive_report(dev_addr, instance)) + { + Serial.printf("Error: cannot request to receive report\n"); + } +} + +void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) +{ + Serial.printf("HID device unmounted (address %d, instance %d)\n", dev_addr, instance); +} + + +void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len) { + + if (report[BYTE_DPAD_LEFT_RIGHT] != DPAD_NEUTRAL || + report[BYTE_DPAD_UP_DOWN] != DPAD_NEUTRAL || + report[BYTE_ABXY_BUTTONS] != BUTTON_NEUTRAL || + report[BYTE_OTHER_BUTTONS] != BUTTON_MISC_NEUTRAL){ + + printed_blank = false; + + //debug print report data + // Serial.print("Report data: "); + // for (int i = 0; i < len; i++) { + // Serial.print(report[i], HEX); + // Serial.print(" "); + // } + // Serial.println(); + + if (report[BYTE_DPAD_LEFT_RIGHT] == DPAD_LEFT){ + Serial.print("Left "); + }else if (report[BYTE_DPAD_LEFT_RIGHT] == DPAD_RIGHT){ + Serial.print("Right "); + } + + if (report[BYTE_DPAD_UP_DOWN] == DPAD_UP){ + Serial.print("Up "); + }else if (report[BYTE_DPAD_UP_DOWN] == DPAD_DOWN){ + Serial.print("Down "); + } + + if ((report[BYTE_ABXY_BUTTONS] & BUTTON_A) == BUTTON_A){ + Serial.print("A "); + } + if ((report[BYTE_ABXY_BUTTONS] & BUTTON_B) == BUTTON_B){ + Serial.print("B "); + } + if ((report[BYTE_ABXY_BUTTONS] & BUTTON_X) == BUTTON_X){ + Serial.print("X "); + } + if ((report[BYTE_ABXY_BUTTONS] & BUTTON_Y) == BUTTON_Y){ + Serial.print("Y "); + } + + if ((report[BYTE_OTHER_BUTTONS] & BUTTON_LEFT_SHOULDER) == BUTTON_LEFT_SHOULDER){ + Serial.print("Left Shoulder "); + } + if ((report[BYTE_OTHER_BUTTONS] & BUTTON_RIGHT_SHOULDER) == BUTTON_RIGHT_SHOULDER){ + Serial.print("Right Shoulder "); + } + if ((report[BYTE_OTHER_BUTTONS] & BUTTON_START) == BUTTON_START){ + Serial.print("Start "); + } + if ((report[BYTE_OTHER_BUTTONS] & BUTTON_SELECT) == BUTTON_SELECT){ + Serial.print("Select "); + } + Serial.println(); + } else { + if (! printed_blank){ + Serial.println("NEUTRAL"); + printed_blank = true; + } + } + + // Continue to receive the next report + if (!tuh_hid_receive_report(dev_addr, instance)) { + Serial.println("Error: cannot request to receive report"); + } +} diff --git a/USB_SNES_Gamepad/Arduino_USB_Host/usbh_helper.h b/USB_SNES_Gamepad/Arduino_USB_Host/usbh_helper.h new file mode 100644 index 000000000..107f5c09f --- /dev/null +++ b/USB_SNES_Gamepad/Arduino_USB_Host/usbh_helper.h @@ -0,0 +1,103 @@ +// SPDX-FileCopyrightText: 2024 Ha Thach for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +/********************************************************************* + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + Copyright (c) 2019 Ha Thach for Adafruit Industries + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +#ifndef USBH_HELPER_H +#define USBH_HELPER_H + +#ifdef ARDUINO_ARCH_RP2040 + // pio-usb is required for rp2040 host + #include "pio_usb.h" + + // Pin D+ for host, D- = D+ + 1 + #ifndef PIN_USB_HOST_DP + #define PIN_USB_HOST_DP 16 + #endif + + // Pin for enabling Host VBUS. comment out if not used + #ifndef PIN_5V_EN + #define PIN_5V_EN 18 + #endif + + #ifndef PIN_5V_EN_STATE + #define PIN_5V_EN_STATE 1 + #endif +#endif // ARDUINO_ARCH_RP2040 + +#include "Adafruit_TinyUSB.h" + +#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421 + // USB Host using MAX3421E: SPI, CS, INT + #include "SPI.h" + + #if defined(ARDUINO_METRO_ESP32S2) + Adafruit_USBH_Host USBHost(&SPI, 15, 14); + #elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2) + Adafruit_USBH_Host USBHost(&SPI, 33, 15); + #else + // Default CS and INT are pin 10, 9 + Adafruit_USBH_Host USBHost(&SPI, 10, 9); + #endif +#else + // Native USB Host such as rp2040 + Adafruit_USBH_Host USBHost; +#endif + +//--------------------------------------------------------------------+ +// Helper Functions +//--------------------------------------------------------------------+ + +#ifdef ARDUINO_ARCH_RP2040 +static void rp2040_configure_pio_usb(void) { + //while ( !Serial ) delay(10); // wait for native usb + Serial.println("Core1 setup to run TinyUSB host with pio-usb"); + + // Check for CPU frequency, must be multiple of 120Mhz for bit-banging USB + uint32_t cpu_hz = clock_get_hz(clk_sys); + if (cpu_hz != 120000000UL && cpu_hz != 240000000UL) { + while (!Serial) { + delay(10); // wait for native usb + } + Serial.printf("Error: CPU Clock = %lu, PIO USB require CPU clock must be multiple of 120 Mhz\r\n", cpu_hz); + Serial.printf("Change your CPU Clock to either 120 or 240 Mhz in Menu->CPU Speed \r\n"); + while (1) { + delay(1); + } + } + +#ifdef PIN_5V_EN + pinMode(PIN_5V_EN, OUTPUT); + digitalWrite(PIN_5V_EN, PIN_5V_EN_STATE); +#endif + + pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG; + pio_cfg.pin_dp = PIN_USB_HOST_DP; + +#if defined(ARDUINO_RASPBERRY_PI_PICO_W) + // For pico-w, PIO is also used to communicate with cyw43 + // Therefore we need to alternate the pio-usb configuration + // details https://github.com/sekigon-gonnoc/Pico-PIO-USB/issues/46 + pio_cfg.sm_tx = 3; + pio_cfg.sm_rx = 2; + pio_cfg.sm_eop = 3; + pio_cfg.pio_rx_num = 0; + pio_cfg.pio_tx_num = 1; + pio_cfg.tx_ch = 9; +#endif + + USBHost.configure_pio_usb(1, &pio_cfg); +} +#endif + +#endif From 7edbbf2401b81734db129807f714c056cba07048 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Thu, 17 Apr 2025 11:52:48 -0500 Subject: [PATCH 2/5] remove dupe file extension --- ...nes_gamepad_simpletest.ino.ino => snes_gamepad_simpletest.ino} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename USB_SNES_Gamepad/Arduino_USB_Host/{snes_gamepad_simpletest.ino.ino => snes_gamepad_simpletest.ino} (100%) diff --git a/USB_SNES_Gamepad/Arduino_USB_Host/snes_gamepad_simpletest.ino.ino b/USB_SNES_Gamepad/Arduino_USB_Host/snes_gamepad_simpletest.ino similarity index 100% rename from USB_SNES_Gamepad/Arduino_USB_Host/snes_gamepad_simpletest.ino.ino rename to USB_SNES_Gamepad/Arduino_USB_Host/snes_gamepad_simpletest.ino From da43407a168a9fc0adb00bc81700d8685d5077cf Mon Sep 17 00:00:00 2001 From: foamyguy Date: Thu, 17 Apr 2025 12:21:17 -0500 Subject: [PATCH 3/5] fix typo, add test.only file, add device to folder name to prepare for metro version --- .../Arduino_Feather_RP2040_USB_Host/.feather_rp2040.test.only | 0 .../gamepad_reports.h | 0 .../snes_gamepad_simpletest.ino | 2 +- .../usbh_helper.h | 0 4 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/.feather_rp2040.test.only rename USB_SNES_Gamepad/{Arduino_USB_Host => Arduino_Feather_RP2040_USB_Host}/gamepad_reports.h (100%) rename USB_SNES_Gamepad/{Arduino_USB_Host => Arduino_Feather_RP2040_USB_Host}/snes_gamepad_simpletest.ino (97%) rename USB_SNES_Gamepad/{Arduino_USB_Host => Arduino_Feather_RP2040_USB_Host}/usbh_helper.h (100%) diff --git a/USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/.feather_rp2040.test.only b/USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/.feather_rp2040.test.only new file mode 100644 index 000000000..e69de29bb diff --git a/USB_SNES_Gamepad/Arduino_USB_Host/gamepad_reports.h b/USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/gamepad_reports.h similarity index 100% rename from USB_SNES_Gamepad/Arduino_USB_Host/gamepad_reports.h rename to USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/gamepad_reports.h diff --git a/USB_SNES_Gamepad/Arduino_USB_Host/snes_gamepad_simpletest.ino b/USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/snes_gamepad_simpletest.ino similarity index 97% rename from USB_SNES_Gamepad/Arduino_USB_Host/snes_gamepad_simpletest.ino rename to USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/snes_gamepad_simpletest.ino index d06cb6ba2..3ba0ab41a 100644 --- a/USB_SNES_Gamepad/Arduino_USB_Host/snes_gamepad_simpletest.ino +++ b/USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/snes_gamepad_simpletest.ino @@ -57,7 +57,7 @@ void setup() { usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report)); usb_hid.begin(); - // If already enumerated, additional class driverr begin() e.g msc, hid, midi won't take effect until re-enumeration + // If already enumerated, additional class driver begin() e.g msc, hid, midi won't take effect until re-enumeration if (TinyUSBDevice.mounted()) { TinyUSBDevice.detach(); delay(10); diff --git a/USB_SNES_Gamepad/Arduino_USB_Host/usbh_helper.h b/USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/usbh_helper.h similarity index 100% rename from USB_SNES_Gamepad/Arduino_USB_Host/usbh_helper.h rename to USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/usbh_helper.h From 6a9015c68e2171a1c4ebd9f90ef15dab1e1d1ac3 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Thu, 17 Apr 2025 12:30:38 -0500 Subject: [PATCH 4/5] remove CPU freq restriction. move into subfolder that arduino ide wants. --- .../.feather_rp2040.test.only | 0 .../{ => snes_gamepad_simpletest}/gamepad_reports.h | 0 .../snes_gamepad_simpletest.ino | 0 .../{ => snes_gamepad_simpletest}/usbh_helper.h | 13 ------------- 4 files changed, 13 deletions(-) rename USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/{ => snes_gamepad_simpletest}/.feather_rp2040.test.only (100%) rename USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/{ => snes_gamepad_simpletest}/gamepad_reports.h (100%) rename USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/{ => snes_gamepad_simpletest}/snes_gamepad_simpletest.ino (100%) rename USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/{ => snes_gamepad_simpletest}/usbh_helper.h (83%) diff --git a/USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/.feather_rp2040.test.only b/USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/snes_gamepad_simpletest/.feather_rp2040.test.only similarity index 100% rename from USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/.feather_rp2040.test.only rename to USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/snes_gamepad_simpletest/.feather_rp2040.test.only diff --git a/USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/gamepad_reports.h b/USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/snes_gamepad_simpletest/gamepad_reports.h similarity index 100% rename from USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/gamepad_reports.h rename to USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/snes_gamepad_simpletest/gamepad_reports.h diff --git a/USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/snes_gamepad_simpletest.ino b/USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/snes_gamepad_simpletest/snes_gamepad_simpletest.ino similarity index 100% rename from USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/snes_gamepad_simpletest.ino rename to USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/snes_gamepad_simpletest/snes_gamepad_simpletest.ino diff --git a/USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/usbh_helper.h b/USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/snes_gamepad_simpletest/usbh_helper.h similarity index 83% rename from USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/usbh_helper.h rename to USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/snes_gamepad_simpletest/usbh_helper.h index 107f5c09f..6a2dd366a 100644 --- a/USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/usbh_helper.h +++ b/USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/snes_gamepad_simpletest/usbh_helper.h @@ -63,19 +63,6 @@ static void rp2040_configure_pio_usb(void) { //while ( !Serial ) delay(10); // wait for native usb Serial.println("Core1 setup to run TinyUSB host with pio-usb"); - // Check for CPU frequency, must be multiple of 120Mhz for bit-banging USB - uint32_t cpu_hz = clock_get_hz(clk_sys); - if (cpu_hz != 120000000UL && cpu_hz != 240000000UL) { - while (!Serial) { - delay(10); // wait for native usb - } - Serial.printf("Error: CPU Clock = %lu, PIO USB require CPU clock must be multiple of 120 Mhz\r\n", cpu_hz); - Serial.printf("Change your CPU Clock to either 120 or 240 Mhz in Menu->CPU Speed \r\n"); - while (1) { - delay(1); - } - } - #ifdef PIN_5V_EN pinMode(PIN_5V_EN, OUTPUT); digitalWrite(PIN_5V_EN, PIN_5V_EN_STATE); From f91a3e6b52849c4f539e61a0de028247ebb583fb Mon Sep 17 00:00:00 2001 From: foamyguy Date: Thu, 17 Apr 2025 12:42:13 -0500 Subject: [PATCH 5/5] use usbhost tinyusb config --- ...rp2040.test.only => .feather_rp2040_usbhost_tinyusb.test.only} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/snes_gamepad_simpletest/{.feather_rp2040.test.only => .feather_rp2040_usbhost_tinyusb.test.only} (100%) diff --git a/USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/snes_gamepad_simpletest/.feather_rp2040.test.only b/USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/snes_gamepad_simpletest/.feather_rp2040_usbhost_tinyusb.test.only similarity index 100% rename from USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/snes_gamepad_simpletest/.feather_rp2040.test.only rename to USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/snes_gamepad_simpletest/.feather_rp2040_usbhost_tinyusb.test.only