Skip to content

Commit 3f0ed86

Browse files
authored
Merge pull request #3023 from FoamyGuy/usbhost_snespad_arduino
USB SNES-like controller example for arduino
2 parents 3eaa064 + f91a3e6 commit 3f0ed86

File tree

4 files changed

+308
-0
lines changed

4 files changed

+308
-0
lines changed

USB_SNES_Gamepad/Arduino_Feather_RP2040_USB_Host/snes_gamepad_simpletest/.feather_rp2040_usbhost_tinyusb.test.only

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
// HID reports for USB SNES-like controller
6+
7+
// Byte indices for the gamepad report
8+
#define BYTE_DPAD_LEFT_RIGHT 0 // D-Pad left and right
9+
#define BYTE_DPAD_UP_DOWN 1 // D-Pad up and down
10+
// bytes 2,3,4 unused
11+
#define BYTE_ABXY_BUTTONS 5 // A, B, X, Y
12+
#define BYTE_OTHER_BUTTONS 6 // Shoulders, start, and select
13+
14+
15+
#define DPAD_NEUTRAL 0x7F
16+
// D-Pad directions
17+
#define DPAD_UP 0x00
18+
#define DPAD_RIGHT 0xFF
19+
#define DPAD_DOWN 0xFF
20+
#define DPAD_LEFT 0x00
21+
22+
23+
// Face buttons (Byte[5])
24+
#define BUTTON_NEUTRAL 0x0F
25+
#define BUTTON_X 0x1F
26+
#define BUTTON_A 0x2F
27+
#define BUTTON_B 0x4F
28+
#define BUTTON_Y 0x8F
29+
30+
31+
// Miscellaneous buttons (Byte[6])
32+
#define BUTTON_MISC_NEUTRAL 0x00
33+
#define BUTTON_LEFT_SHOULDER 0x01
34+
#define BUTTON_RIGHT_SHOULDER 0x02
35+
#define BUTTON_SELECT 0x10
36+
#define BUTTON_START 0x20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
// SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
/*********************************************************************
6+
Adafruit invests time and resources providing this open source code,
7+
please support Adafruit and open-source hardware by purchasing
8+
products from Adafruit!
9+
10+
MIT license, check LICENSE for more information
11+
Copyright (c) 2019 Ha Thach for Adafruit Industries
12+
All text above, and the splash screen below must be included in
13+
any redistribution
14+
*********************************************************************/
15+
16+
/* This example demonstrates use of usb host with a SNES-like game controller
17+
* - Host depends on MCU:
18+
* - rp2040: bit-banging 2 GPIOs with Pico-PIO-USB library (roothub port1)
19+
*
20+
* Requirements:
21+
* - For rp2040:
22+
* - Pico-PIO-USB library
23+
* - 2 consecutive GPIOs: D+ is defined by PIN_USB_HOST_DP, D- = D+ +1
24+
* - Provide VBus (5v) and GND for peripheral
25+
* - CPU Speed must be either 120 or 240 MHz. Selected via "Menu -> CPU Speed"
26+
*/
27+
28+
// USBHost is defined in usbh_helper.h
29+
#include "usbh_helper.h"
30+
#include "tusb.h"
31+
#include "Adafruit_TinyUSB.h"
32+
#include "gamepad_reports.h"
33+
34+
// HID report descriptor using TinyUSB's template
35+
// Single Report (no ID) descriptor
36+
uint8_t const desc_hid_report[] = {
37+
TUD_HID_REPORT_DESC_GAMEPAD()
38+
};
39+
40+
// USB HID object
41+
Adafruit_USBD_HID usb_hid;
42+
43+
// Report payload defined in src/class/hid/hid.h
44+
// - For Gamepad Button Bit Mask see hid_gamepad_button_bm_t
45+
// - For Gamepad Hat Bit Mask see hid_gamepad_hat_t
46+
hid_gamepad_report_t gp;
47+
48+
bool printed_blank = false;
49+
50+
void setup() {
51+
if (!TinyUSBDevice.isInitialized()) {
52+
TinyUSBDevice.begin(0);
53+
}
54+
Serial.begin(115200);
55+
// Setup HID
56+
usb_hid.setPollInterval(2);
57+
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
58+
usb_hid.begin();
59+
60+
// If already enumerated, additional class driver begin() e.g msc, hid, midi won't take effect until re-enumeration
61+
if (TinyUSBDevice.mounted()) {
62+
TinyUSBDevice.detach();
63+
delay(10);
64+
TinyUSBDevice.attach();
65+
}
66+
}
67+
68+
#if defined(ARDUINO_ARCH_RP2040)
69+
//--------------------------------------------------------------------+
70+
// For RP2040 use both core0 for device stack, core1 for host stack
71+
//--------------------------------------------------------------------//
72+
73+
//------------- Core0 -------------//
74+
void loop() {
75+
}
76+
77+
//------------- Core1 -------------//
78+
void setup1() {
79+
// configure pio-usb: defined in usbh_helper.h
80+
rp2040_configure_pio_usb();
81+
82+
// run host stack on controller (rhport) 1
83+
// Note: For rp2040 pico-pio-usb, calling USBHost.begin() on core1 will have most of the
84+
// host bit-banging processing works done in core1 to free up core0 for other works
85+
USBHost.begin(1);
86+
}
87+
88+
void loop1() {
89+
USBHost.task();
90+
Serial.flush();
91+
92+
}
93+
#endif
94+
95+
//--------------------------------------------------------------------+
96+
// HID Host Callback Functions
97+
//--------------------------------------------------------------------+
98+
99+
void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len)
100+
{
101+
Serial.printf("HID device mounted (address %d, instance %d)\n", dev_addr, instance);
102+
103+
// Start receiving HID reports
104+
if (!tuh_hid_receive_report(dev_addr, instance))
105+
{
106+
Serial.printf("Error: cannot request to receive report\n");
107+
}
108+
}
109+
110+
void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance)
111+
{
112+
Serial.printf("HID device unmounted (address %d, instance %d)\n", dev_addr, instance);
113+
}
114+
115+
116+
void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len) {
117+
118+
if (report[BYTE_DPAD_LEFT_RIGHT] != DPAD_NEUTRAL ||
119+
report[BYTE_DPAD_UP_DOWN] != DPAD_NEUTRAL ||
120+
report[BYTE_ABXY_BUTTONS] != BUTTON_NEUTRAL ||
121+
report[BYTE_OTHER_BUTTONS] != BUTTON_MISC_NEUTRAL){
122+
123+
printed_blank = false;
124+
125+
//debug print report data
126+
// Serial.print("Report data: ");
127+
// for (int i = 0; i < len; i++) {
128+
// Serial.print(report[i], HEX);
129+
// Serial.print(" ");
130+
// }
131+
// Serial.println();
132+
133+
if (report[BYTE_DPAD_LEFT_RIGHT] == DPAD_LEFT){
134+
Serial.print("Left ");
135+
}else if (report[BYTE_DPAD_LEFT_RIGHT] == DPAD_RIGHT){
136+
Serial.print("Right ");
137+
}
138+
139+
if (report[BYTE_DPAD_UP_DOWN] == DPAD_UP){
140+
Serial.print("Up ");
141+
}else if (report[BYTE_DPAD_UP_DOWN] == DPAD_DOWN){
142+
Serial.print("Down ");
143+
}
144+
145+
if ((report[BYTE_ABXY_BUTTONS] & BUTTON_A) == BUTTON_A){
146+
Serial.print("A ");
147+
}
148+
if ((report[BYTE_ABXY_BUTTONS] & BUTTON_B) == BUTTON_B){
149+
Serial.print("B ");
150+
}
151+
if ((report[BYTE_ABXY_BUTTONS] & BUTTON_X) == BUTTON_X){
152+
Serial.print("X ");
153+
}
154+
if ((report[BYTE_ABXY_BUTTONS] & BUTTON_Y) == BUTTON_Y){
155+
Serial.print("Y ");
156+
}
157+
158+
if ((report[BYTE_OTHER_BUTTONS] & BUTTON_LEFT_SHOULDER) == BUTTON_LEFT_SHOULDER){
159+
Serial.print("Left Shoulder ");
160+
}
161+
if ((report[BYTE_OTHER_BUTTONS] & BUTTON_RIGHT_SHOULDER) == BUTTON_RIGHT_SHOULDER){
162+
Serial.print("Right Shoulder ");
163+
}
164+
if ((report[BYTE_OTHER_BUTTONS] & BUTTON_START) == BUTTON_START){
165+
Serial.print("Start ");
166+
}
167+
if ((report[BYTE_OTHER_BUTTONS] & BUTTON_SELECT) == BUTTON_SELECT){
168+
Serial.print("Select ");
169+
}
170+
Serial.println();
171+
} else {
172+
if (! printed_blank){
173+
Serial.println("NEUTRAL");
174+
printed_blank = true;
175+
}
176+
}
177+
178+
// Continue to receive the next report
179+
if (!tuh_hid_receive_report(dev_addr, instance)) {
180+
Serial.println("Error: cannot request to receive report");
181+
}
182+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// SPDX-FileCopyrightText: 2024 Ha Thach for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
/*********************************************************************
6+
Adafruit invests time and resources providing this open source code,
7+
please support Adafruit and open-source hardware by purchasing
8+
products from Adafruit!
9+
10+
MIT license, check LICENSE for more information
11+
Copyright (c) 2019 Ha Thach for Adafruit Industries
12+
All text above, and the splash screen below must be included in
13+
any redistribution
14+
*********************************************************************/
15+
16+
#ifndef USBH_HELPER_H
17+
#define USBH_HELPER_H
18+
19+
#ifdef ARDUINO_ARCH_RP2040
20+
// pio-usb is required for rp2040 host
21+
#include "pio_usb.h"
22+
23+
// Pin D+ for host, D- = D+ + 1
24+
#ifndef PIN_USB_HOST_DP
25+
#define PIN_USB_HOST_DP 16
26+
#endif
27+
28+
// Pin for enabling Host VBUS. comment out if not used
29+
#ifndef PIN_5V_EN
30+
#define PIN_5V_EN 18
31+
#endif
32+
33+
#ifndef PIN_5V_EN_STATE
34+
#define PIN_5V_EN_STATE 1
35+
#endif
36+
#endif // ARDUINO_ARCH_RP2040
37+
38+
#include "Adafruit_TinyUSB.h"
39+
40+
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
41+
// USB Host using MAX3421E: SPI, CS, INT
42+
#include "SPI.h"
43+
44+
#if defined(ARDUINO_METRO_ESP32S2)
45+
Adafruit_USBH_Host USBHost(&SPI, 15, 14);
46+
#elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2)
47+
Adafruit_USBH_Host USBHost(&SPI, 33, 15);
48+
#else
49+
// Default CS and INT are pin 10, 9
50+
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
51+
#endif
52+
#else
53+
// Native USB Host such as rp2040
54+
Adafruit_USBH_Host USBHost;
55+
#endif
56+
57+
//--------------------------------------------------------------------+
58+
// Helper Functions
59+
//--------------------------------------------------------------------+
60+
61+
#ifdef ARDUINO_ARCH_RP2040
62+
static void rp2040_configure_pio_usb(void) {
63+
//while ( !Serial ) delay(10); // wait for native usb
64+
Serial.println("Core1 setup to run TinyUSB host with pio-usb");
65+
66+
#ifdef PIN_5V_EN
67+
pinMode(PIN_5V_EN, OUTPUT);
68+
digitalWrite(PIN_5V_EN, PIN_5V_EN_STATE);
69+
#endif
70+
71+
pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG;
72+
pio_cfg.pin_dp = PIN_USB_HOST_DP;
73+
74+
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
75+
// For pico-w, PIO is also used to communicate with cyw43
76+
// Therefore we need to alternate the pio-usb configuration
77+
// details https://github.com/sekigon-gonnoc/Pico-PIO-USB/issues/46
78+
pio_cfg.sm_tx = 3;
79+
pio_cfg.sm_rx = 2;
80+
pio_cfg.sm_eop = 3;
81+
pio_cfg.pio_rx_num = 0;
82+
pio_cfg.pio_tx_num = 1;
83+
pio_cfg.tx_ch = 9;
84+
#endif
85+
86+
USBHost.configure_pio_usb(1, &pio_cfg);
87+
}
88+
#endif
89+
90+
#endif

0 commit comments

Comments
 (0)