adafruit / Adafruit_TinyUSB_Arduino

Arduino library for TinyUSB
MIT License
449 stars 119 forks source link

Tiny USB Dual Role/HID/device/hid_device_report Won't Compile #398

Closed pkscout closed 4 months ago

pkscout commented 4 months ago

Operating System

MacOS

Arduino IDE version

Arduino 2.3.2

Board

Adafruit Feather ESP32-S3 2MB PSRAM

ArduinoCore version

EPS32 2.0.11

TinyUSB Library version

3.1.0

Sketch as ATTACHED TXT

/*********************************************************************
 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 both device and host, where
 * - Device run on native usb controller (roothub port0)
 * - Host depending on MCUs run on either:
 *   - rp2040: bit-banging 2 GPIOs with the help of Pico-PIO-USB library (roothub port1)
 *   - samd21/51, nrf52840, esp32: using MAX3421e controller (host shield)
 *
 * Requirements:
 * - For rp2040:
 *   - [Pico-PIO-USB](https://github.com/sekigon-gonnoc/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"
 * - For samd21/51, nrf52840, esp32:
 *   - Additional MAX2341e USB Host shield or featherwing is required
 *   - SPI instance, CS pin, INT pin are correctly configured in usbh_helper.h
 */

// USBHost is defined in usbh_helper.h
#include "usbh_helper.h"

#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
//--------------------------------------------------------------------+
// Using Host shield MAX3421E controller
//--------------------------------------------------------------------+
void setup() {
  Serial.begin(115200);

  // init host stack on controller (rhport) 1
  USBHost.begin(1);

//  while ( !Serial ) delay(10);   // wait for native usb
  Serial.println("TinyUSB Dual: HID Device Report Example");
}

void loop() {
  USBHost.task();
  Serial.flush();
}

#elif defined(ARDUINO_ARCH_RP2040)
//--------------------------------------------------------------------+
// For RP2040 use both core0 for device stack, core1 for host stack
//--------------------------------------------------------------------+

//------------- Core0 -------------//
void setup() {
  Serial.begin(115200);
  //while ( !Serial ) delay(10);   // wait for native usb
  Serial.println("TinyUSB Dual: HID Device Report Example");
}

void loop() {
  Serial.flush();
}

//------------- 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();
}

#endif

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);
  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);
}

// 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 : ");
  for (uint16_t i = 0; i < len; i++) {
    Serial.printf("0x%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

Compiled Log as ATTACHED TXT

N/A

What happened ?

I get this error at compile:

/Users/pkscout/Library/Arduino15/packages/esp32/tools/xtensa-esp32s3-elf-gcc/esp-2021r2-patch5-8.4.0/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld: /private/var/folders/4r/vhd77fvj2y52kc8s1h3nb8300000gq/T/arduino/sketches/6CF9AB0B12386F47CD6A06834F934308/libraries/Adafruit_TinyUSB_Library/host/usbh.c.o:(.literal.usbh_control_xfer_cb$constprop$4+0x0): undefined reference to `TU_LOG1_BUF'
/Users/pkscout/Library/Arduino15/packages/esp32/tools/xtensa-esp32s3-elf-gcc/esp-2021r2-patch5-8.4.0/bin/../lib/gcc/xtensa-esp32s3-elf/8.4.0/../../../../xtensa-esp32s3-elf/bin/ld: /private/var/folders/4r/vhd77fvj2y52kc8s1h3nb8300000gq/T/arduino/sketches/6CF9AB0B12386F47CD6A06834F934308/libraries/Adafruit_TinyUSB_Library/host/usbh.c.o: in function `usbh_control_xfer_cb':
/Users/pkscout/Documents/Arduino/libraries/Adafruit_TinyUSB_Library/src/host/usbh.c:695: undefined reference to `TU_LOG1_BUF'
collect2: error: ld returned 1 exit status

exit status 1

Compilation error: exit status 1

How to reproduce ?

  1. Open the example sketch
  2. try and compile

Debug Log

I can fix this by editing Adafruit_TinyUSB_Library/src/host/usbh.c and changing line 695 from:

TU_LOG1_BUF(request, 8);

to:

TU_LOG1(request, 8);

Screenshots

No response

hathach commented 4 months ago

Have you tried 2.0.14 ?

pkscout commented 4 months ago

2.0.11 is the newest one in my board manager drop down. Let me see if I can find 2.0.14 and try it.

EDIT: I figured out how to add the newer version from Github. I had been following this guide:

https://learn.adafruit.com/adafruit-esp32-s3-feather/arduino-ide-setup

but I think I selected to wrong board (i.e. from the ESP32 that was installed, not the one from the sketch book), which gave me the 2.0.11 version. Anyway, with 2.0.14 the example now compiles. Thanks @hathach for the tip. I'm going to close this.