Skip to content

Commit 8495261

Browse files
committed
first commit HID Reporter code
1 parent bd9285f commit 8495261

3 files changed

Lines changed: 224 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
"version": "0.2.0",
5+
"configurations": []
6+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*********************************************************************
2+
Adafruit invests time and resources providing this open source code,
3+
please support Adafruit and open-source hardware by purchasing
4+
products from Adafruit!
5+
6+
MIT license, check LICENSE for more information
7+
Copyright (c) 2024 John Park for Adafruit Industries
8+
Copyright (c) 2019 Ha Thach for Adafruit Industries
9+
All text above, and the splash screen below must be included in
10+
any redistribution
11+
*********************************************************************/
12+
13+
/* Keyboard HID Keycode Reporter
14+
* - Device runs on native usb controller (roothub port0)
15+
* - esp32-s2 TFT Feather : using MAX3421e controller featherwing
16+
* - SPI instance, CS pin, INT pin are correctly configured in usbh_helper.h
17+
*/
18+
19+
// USBHost is defined in usbh_helper.h
20+
#include "usbh_helper.h"
21+
#include <Adafruit_GFX.h>
22+
#include <Adafruit_ST7789.h>
23+
#include <Fonts/FreeMono18pt7b.h>
24+
#include <Fonts/FreeMono12pt7b.h>
25+
26+
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
27+
28+
void setup() {
29+
Serial.begin(115200);
30+
// turn on backlight
31+
pinMode(TFT_BACKLITE, OUTPUT);
32+
digitalWrite(TFT_BACKLITE, HIGH);
33+
34+
// turn on the TFT / I2C power supply
35+
pinMode(TFT_I2C_POWER, OUTPUT);
36+
digitalWrite(TFT_I2C_POWER, HIGH);
37+
delay(10);
38+
39+
// initialize TFT
40+
tft.init(135, 240); // Init ST7789 240x135
41+
tft.setRotation(3);
42+
tft.fillScreen(ST77XX_BLACK);
43+
44+
tft.setFont(&FreeMono18pt7b);
45+
tft.setTextWrap(true);
46+
tft.fillScreen(ST77XX_BLACK);
47+
tft.setCursor(0, 20);
48+
tft.setTextColor(ST77XX_GREEN);
49+
tft.setTextSize(1);
50+
tft.println("HIDreporter");
51+
52+
// init host stack on controller (rhport) 1
53+
USBHost.begin(1);
54+
55+
// while ( !Serial ) delay(10); // wait for native usb
56+
Serial.println("TinyUSB Dual: HID Device Reporter");
57+
}
58+
59+
void loop() {
60+
USBHost.task();
61+
Serial.flush();
62+
}
63+
64+
extern "C" {
65+
66+
// Invoked when device with hid interface is mounted
67+
// Report descriptor is also available for use.
68+
// tuh_hid_parse_report_descriptor() can be used to parse common/simple enough
69+
// descriptor. Note: if report descriptor length > CFG_TUH_ENUMERATION_BUFSIZE,
70+
// it will be skipped therefore report_desc = NULL, desc_len = 0
71+
void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const *desc_report, uint16_t desc_len) {
72+
(void) desc_report;
73+
(void) desc_len;
74+
uint16_t vid, pid;
75+
tuh_vid_pid_get(dev_addr, &vid, &pid);
76+
77+
Serial.printf("HID device address = %d, instance = %d is mounted\r\n", dev_addr, instance);
78+
Serial.printf("VID = %04x, PID = %04x\r\n", vid, pid);
79+
tft.fillRect(0, 34, 240, 80, ST77XX_BLACK);
80+
tft.setFont(&FreeMono12pt7b);
81+
tft.setCursor(0, 50);
82+
tft.printf("VID=%04x,PID=%04x\r\n", vid, pid);
83+
if (!tuh_hid_receive_report(dev_addr, instance)) {
84+
Serial.printf("Error: cannot request to receive report\r\n");
85+
}
86+
}
87+
88+
// Invoked when device with hid interface is un-mounted
89+
void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) {
90+
Serial.printf("HID device address = %d, instance = %d is unmounted\r\n", dev_addr, instance);
91+
tft.fillRect(0, 34, 240, 140, ST77XX_BLACK);
92+
tft.setFont(&FreeMono12pt7b);
93+
tft.setTextColor(ST77XX_YELLOW);
94+
tft.setCursor(0, 50);
95+
tft.printf("-- unmounted --");
96+
tft.setTextColor(ST77XX_GREEN);
97+
98+
}
99+
100+
// Invoked when received report from device via interrupt endpoint
101+
void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const *report, uint16_t len) {
102+
Serial.printf("HIDreport : ");
103+
tft.fillRect(0, 64, 240, 80, ST77XX_BLACK);
104+
tft.setCursor(0, 88);
105+
tft.setFont(&FreeMono18pt7b);
106+
107+
for (uint16_t i = 0; i < len; i++) {
108+
Serial.printf("0x%02X ", report[i]);
109+
tft.printf("%02X ", report[i]);
110+
}
111+
112+
Serial.println();
113+
// continue to request to receive report
114+
if (!tuh_hid_receive_report(dev_addr, instance)) {
115+
Serial.printf("Error: cannot request to receive report\r\n");
116+
}
117+
}
118+
119+
} // extern C
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*********************************************************************
2+
Adafruit invests time and resources providing this open source code,
3+
please support Adafruit and open-source hardware by purchasing
4+
products from Adafruit!
5+
6+
MIT license, check LICENSE for more information
7+
Copyright (c) 2019 Ha Thach for Adafruit Industries
8+
All text above, and the splash screen below must be included in
9+
any redistribution
10+
*********************************************************************/
11+
12+
#ifndef USBH_HELPER_H
13+
#define USBH_HELPER_H
14+
15+
#ifdef ARDUINO_ARCH_RP2040
16+
// pio-usb is required for rp2040 host
17+
#include "pio_usb.h"
18+
19+
// Pin D+ for host, D- = D+ + 1
20+
#ifndef PIN_USB_HOST_DP
21+
#define PIN_USB_HOST_DP 16
22+
#endif
23+
24+
// Pin for enabling Host VBUS. comment out if not used
25+
#ifndef PIN_5V_EN
26+
#define PIN_5V_EN 18
27+
#endif
28+
29+
#ifndef PIN_5V_EN_STATE
30+
#define PIN_5V_EN_STATE 1
31+
#endif
32+
#endif // ARDUINO_ARCH_RP2040
33+
34+
#include "Adafruit_TinyUSB.h"
35+
36+
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
37+
// USB Host using MAX3421E: SPI, CS, INT
38+
#include "SPI.h"
39+
40+
#if defined(ARDUINO_METRO_ESP32S2)
41+
Adafruit_USBH_Host USBHost(&SPI, 15, 14);
42+
#elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2)
43+
Adafruit_USBH_Host USBHost(&SPI, 33, 15);
44+
#else
45+
// Default CS and INT are pin 10, 9
46+
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
47+
#endif
48+
#else
49+
// Native USB Host such as rp2040
50+
Adafruit_USBH_Host USBHost;
51+
#endif
52+
53+
//--------------------------------------------------------------------+
54+
// Helper Functions
55+
//--------------------------------------------------------------------+
56+
57+
#ifdef ARDUINO_ARCH_RP2040
58+
static void rp2040_configure_pio_usb(void) {
59+
//while ( !Serial ) delay(10); // wait for native usb
60+
Serial.println("Core1 setup to run TinyUSB host with pio-usb");
61+
62+
// Check for CPU frequency, must be multiple of 120Mhz for bit-banging USB
63+
uint32_t cpu_hz = clock_get_hz(clk_sys);
64+
if (cpu_hz != 120000000UL && cpu_hz != 240000000UL) {
65+
while (!Serial) {
66+
delay(10); // wait for native usb
67+
}
68+
Serial.printf("Error: CPU Clock = %lu, PIO USB require CPU clock must be multiple of 120 Mhz\r\n", cpu_hz);
69+
Serial.printf("Change your CPU Clock to either 120 or 240 Mhz in Menu->CPU Speed \r\n");
70+
while (1) {
71+
delay(1);
72+
}
73+
}
74+
75+
#ifdef PIN_5V_EN
76+
pinMode(PIN_5V_EN, OUTPUT);
77+
digitalWrite(PIN_5V_EN, PIN_5V_EN_STATE);
78+
#endif
79+
80+
pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG;
81+
pio_cfg.pin_dp = PIN_USB_HOST_DP;
82+
83+
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
84+
// For pico-w, PIO is also used to communicate with cyw43
85+
// Therefore we need to alternate the pio-usb configuration
86+
// details https://github.com/sekigon-gonnoc/Pico-PIO-USB/issues/46
87+
pio_cfg.sm_tx = 3;
88+
pio_cfg.sm_rx = 2;
89+
pio_cfg.sm_eop = 3;
90+
pio_cfg.pio_rx_num = 0;
91+
pio_cfg.pio_tx_num = 1;
92+
pio_cfg.tx_ch = 9;
93+
#endif
94+
95+
USBHost.configure_pio_usb(1, &pio_cfg);
96+
}
97+
#endif
98+
99+
#endif

0 commit comments

Comments
 (0)