From 8495261c6e841ec885bfa98959719e1acf977096 Mon Sep 17 00:00:00 2001 From: John Park Date: Wed, 25 Sep 2024 12:18:45 -0700 Subject: [PATCH 1/5] first commit HID Reporter code --- .../USB_Host_HID_Reporter/.theia/launch.json | 6 + .../USB_Host_HID_Reporter.ino | 119 ++++++++++++++++++ .../USB_Host_HID_Reporter/usbh_helper.h | 99 +++++++++++++++ 3 files changed, 224 insertions(+) create mode 100644 HID_Reporter/USB_Host_HID_Reporter/.theia/launch.json create mode 100644 HID_Reporter/USB_Host_HID_Reporter/USB_Host_HID_Reporter.ino create mode 100644 HID_Reporter/USB_Host_HID_Reporter/usbh_helper.h diff --git a/HID_Reporter/USB_Host_HID_Reporter/.theia/launch.json b/HID_Reporter/USB_Host_HID_Reporter/.theia/launch.json new file mode 100644 index 000000000..a2ea02c46 --- /dev/null +++ b/HID_Reporter/USB_Host_HID_Reporter/.theia/launch.json @@ -0,0 +1,6 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + "version": "0.2.0", + "configurations": [] +} diff --git a/HID_Reporter/USB_Host_HID_Reporter/USB_Host_HID_Reporter.ino b/HID_Reporter/USB_Host_HID_Reporter/USB_Host_HID_Reporter.ino new file mode 100644 index 000000000..27eb0ffe1 --- /dev/null +++ b/HID_Reporter/USB_Host_HID_Reporter/USB_Host_HID_Reporter.ino @@ -0,0 +1,119 @@ +/********************************************************************* + 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) 2024 John Park for Adafruit Industries + Copyright (c) 2019 Ha Thach for Adafruit Industries + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +/* Keyboard HID Keycode Reporter + * - Device runs on native usb controller (roothub port0) + * - esp32-s2 TFT Feather : using MAX3421e controller featherwing + * - SPI instance, CS pin, INT pin are correctly configured in usbh_helper.h + */ + +// USBHost is defined in usbh_helper.h +#include "usbh_helper.h" +#include +#include +#include +#include + +Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST); + +void setup() { + Serial.begin(115200); + // turn on backlight + pinMode(TFT_BACKLITE, OUTPUT); + digitalWrite(TFT_BACKLITE, HIGH); + + // turn on the TFT / I2C power supply + pinMode(TFT_I2C_POWER, OUTPUT); + digitalWrite(TFT_I2C_POWER, HIGH); + delay(10); + + // initialize TFT + tft.init(135, 240); // Init ST7789 240x135 + tft.setRotation(3); + tft.fillScreen(ST77XX_BLACK); + + tft.setFont(&FreeMono18pt7b); + tft.setTextWrap(true); + tft.fillScreen(ST77XX_BLACK); + tft.setCursor(0, 20); + tft.setTextColor(ST77XX_GREEN); + tft.setTextSize(1); + tft.println("HIDreporter"); + + // init host stack on controller (rhport) 1 + USBHost.begin(1); + +// while ( !Serial ) delay(10); // wait for native usb + Serial.println("TinyUSB Dual: HID Device Reporter"); +} + +void loop() { + USBHost.task(); + Serial.flush(); +} + +extern "C" { + +// Invoked when device with hid interface is mounted +// Report descriptor is also available for use. +// tuh_hid_parse_report_descriptor() can be used to parse common/simple enough +// descriptor. Note: if report descriptor length > CFG_TUH_ENUMERATION_BUFSIZE, +// it will be skipped therefore report_desc = NULL, desc_len = 0 +void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const *desc_report, uint16_t desc_len) { + (void) desc_report; + (void) desc_len; + uint16_t vid, pid; + tuh_vid_pid_get(dev_addr, &vid, &pid); + + Serial.printf("HID device address = %d, instance = %d is mounted\r\n", dev_addr, instance); + Serial.printf("VID = %04x, PID = %04x\r\n", vid, pid); + tft.fillRect(0, 34, 240, 80, ST77XX_BLACK); + tft.setFont(&FreeMono12pt7b); + tft.setCursor(0, 50); + tft.printf("VID=%04x,PID=%04x\r\n", vid, pid); + if (!tuh_hid_receive_report(dev_addr, instance)) { + Serial.printf("Error: cannot request to receive report\r\n"); + } +} + +// Invoked when device with hid interface is un-mounted +void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) { + Serial.printf("HID device address = %d, instance = %d is unmounted\r\n", dev_addr, instance); + tft.fillRect(0, 34, 240, 140, ST77XX_BLACK); + tft.setFont(&FreeMono12pt7b); + tft.setTextColor(ST77XX_YELLOW); + tft.setCursor(0, 50); + tft.printf("-- unmounted --"); + tft.setTextColor(ST77XX_GREEN); + +} + +// Invoked when received report from device via interrupt endpoint +void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const *report, uint16_t len) { + Serial.printf("HIDreport : "); + tft.fillRect(0, 64, 240, 80, ST77XX_BLACK); + tft.setCursor(0, 88); + tft.setFont(&FreeMono18pt7b); + + for (uint16_t i = 0; i < len; i++) { + Serial.printf("0x%02X ", report[i]); + tft.printf("%02X ", report[i]); + } + + Serial.println(); + // continue to request to receive report + if (!tuh_hid_receive_report(dev_addr, instance)) { + Serial.printf("Error: cannot request to receive report\r\n"); + } +} + +} // extern C \ No newline at end of file diff --git a/HID_Reporter/USB_Host_HID_Reporter/usbh_helper.h b/HID_Reporter/USB_Host_HID_Reporter/usbh_helper.h new file mode 100644 index 000000000..fc0cd8ed9 --- /dev/null +++ b/HID_Reporter/USB_Host_HID_Reporter/usbh_helper.h @@ -0,0 +1,99 @@ +/********************************************************************* + 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 b25a79bb8bb6fb742db58da22f2e581fb1331fd3 Mon Sep 17 00:00:00 2001 From: John Park Date: Wed, 25 Sep 2024 12:21:21 -0700 Subject: [PATCH 2/5] first commit HID Reporter code --- HID_Reporter/.feather_esp32s2_tft.test.only | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 HID_Reporter/.feather_esp32s2_tft.test.only diff --git a/HID_Reporter/.feather_esp32s2_tft.test.only b/HID_Reporter/.feather_esp32s2_tft.test.only new file mode 100644 index 000000000..e69de29bb From 9591fa1da833b039681b9c811db7d7a9e5a5f32e Mon Sep 17 00:00:00 2001 From: John Park Date: Wed, 25 Sep 2024 12:27:01 -0700 Subject: [PATCH 3/5] added SPDX --- .../USB_Host_HID_Reporter/USB_Host_HID_Reporter.ino | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/HID_Reporter/USB_Host_HID_Reporter/USB_Host_HID_Reporter.ino b/HID_Reporter/USB_Host_HID_Reporter/USB_Host_HID_Reporter.ino index 27eb0ffe1..cb3bbfa5d 100644 --- a/HID_Reporter/USB_Host_HID_Reporter/USB_Host_HID_Reporter.ino +++ b/HID_Reporter/USB_Host_HID_Reporter/USB_Host_HID_Reporter.ino @@ -1,3 +1,7 @@ +// SPDX-FileCopyrightText: 2024 John Park 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 @@ -18,8 +22,8 @@ // USBHost is defined in usbh_helper.h #include "usbh_helper.h" -#include -#include +#include +#include #include #include @@ -116,4 +120,4 @@ void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t cons } } -} // extern C \ No newline at end of file +} // extern C From 78cff5e023b5951bf8d553e6d113422931789994 Mon Sep 17 00:00:00 2001 From: John Park Date: Wed, 25 Sep 2024 12:28:57 -0700 Subject: [PATCH 4/5] added SPDX for helper file, too --- HID_Reporter/USB_Host_HID_Reporter/usbh_helper.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/HID_Reporter/USB_Host_HID_Reporter/usbh_helper.h b/HID_Reporter/USB_Host_HID_Reporter/usbh_helper.h index fc0cd8ed9..eb65574f2 100644 --- a/HID_Reporter/USB_Host_HID_Reporter/usbh_helper.h +++ b/HID_Reporter/USB_Host_HID_Reporter/usbh_helper.h @@ -1,3 +1,6 @@ +// SPDX-FileCopyrightText: 2024 John Park 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 From cc08a5430f1f048e3f3cefa68e859b5a392980e8 Mon Sep 17 00:00:00 2001 From: John Park Date: Wed, 25 Sep 2024 12:44:44 -0700 Subject: [PATCH 5/5] moved the test only file to arduino directory --- .../{ => USB_Host_HID_Reporter}/.feather_esp32s2_tft.test.only | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename HID_Reporter/{ => USB_Host_HID_Reporter}/.feather_esp32s2_tft.test.only (100%) diff --git a/HID_Reporter/.feather_esp32s2_tft.test.only b/HID_Reporter/USB_Host_HID_Reporter/.feather_esp32s2_tft.test.only similarity index 100% rename from HID_Reporter/.feather_esp32s2_tft.test.only rename to HID_Reporter/USB_Host_HID_Reporter/.feather_esp32s2_tft.test.only