Skip to content

Bluetooth hci_power_control() interferes with Adafruit TinyUSB #2022

Closed
@tttapa

Description

@tttapa

Hi,

I'm trying to use Bluetooth LE together with the Adafruit TinyUSB library. When initializing the Bluetooth stack first, this breaks the USB code.

I've narrowed it down to the call to the hci_power_control(HCI_POWER_ON) function. Here's a minimal example:

sketch.ino

void power_on_hci();

#include <Adafruit_TinyUSB.h>

Adafruit_USBD_MIDI usb_midi;

void setup() {
#if 1 // When 1, USB MIDI does not work, when 0, it does work
  power_on_hci();
#endif
  pinMode(LED_BUILTIN, OUTPUT);
  usb_midi.begin();
}

void loop() { // Blink as a liveness check
  static bool state = false;
  digitalWrite(LED_BUILTIN, state = !state);
  delay(500);
}

power_on_hci.cpp (second tab)

// This is a separate file because Adafruit_TinyUSB_Arduino/src/class/hid/hid.h
// conflicts with pico-sdk/lib/btstack/src/btstack_hid.h
#include <btstack.h>

void power_on_hci() { hci_power_control(HCI_POWER_ON); }

You can change the #if 1 to #if 0 to see the difference: when leaving out the call to hci_power_control(HCI_POWER_ON), the Pico W correctly show up as a USB MIDI device; when including the call to hci_power_control(HCI_POWER_ON), it only shows up as a serial port. (See the difference between the USB descriptor at the bottom of this post.)

Version and hardware:

  • Board: Raspberry Pi Pico W
  • Core version: earlephilhower/arduino-pico 3.7.2
  • FQBN: rp2040:rp2040:rpipicow:usbstack=tinyusb,ipbtstack=ipv4btcble

Context: I'm the developer of the https://github.com/tttapa/Control-Surface library, and a user reported this issue: tttapa/Control-Surface#1014 (reply in thread).
The full BLE code used there can be found here: https://github.com/tttapa/Control-Surface/blob/d6a5fb1313f8e483b13619dba1fa80d0ec09ca62/src/MIDI_Interfaces/BLEMIDI/BTstack/gatt_midi.cpp (although I don't think this really matters for this issue, simply calling hci_power_control suffices to reproduce the issue).

Difference in USB descriptors between #if 0 and #if 1 (click to open)
--- descr-midi.txt      2024-02-24 15:08:49.003533654 +0100
+++ descr-no-midi.txt   2024-02-24 15:08:00.876148087 +0100
@@ -17,8 +17,8 @@
   Configuration Descriptor:
     bLength                 9
     bDescriptorType         2
-    wTotalLength       0x00a7
-    bNumInterfaces          4
+    wTotalLength       0x004b
+    bNumInterfaces          2
     bConfigurationValue     1
     iConfiguration          0 
     bmAttributes         0xa0
@@ -95,107 +95,3 @@
           Usage Type               Data
         wMaxPacketSize     0x0040  1x 64 bytes
         bInterval               0
-    Interface Descriptor:
-      bLength                 9
-      bDescriptorType         4
-      bInterfaceNumber        2
-      bAlternateSetting       0
-      bNumEndpoints           0
-      bInterfaceClass         1 Audio
-      bInterfaceSubClass      1 Control Device
-      bInterfaceProtocol      0 
-      iInterface              0 
-      AudioControl Interface Descriptor:
-        bLength                 9
-        bDescriptorType        36
-        bDescriptorSubtype      1 (HEADER)
-        bcdADC               1.00
-        wTotalLength       0x0009
-        bInCollection           1
-        baInterfaceNr(0)        3
-    Interface Descriptor:
-      bLength                 9
-      bDescriptorType         4
-      bInterfaceNumber        3
-      bAlternateSetting       0
-      bNumEndpoints           2
-      bInterfaceClass         1 Audio
-      bInterfaceSubClass      3 MIDI Streaming
-      bInterfaceProtocol      0 
-      iInterface              0 
-      MIDIStreaming Interface Descriptor:
-        bLength                 7
-        bDescriptorType        36
-        bDescriptorSubtype      1 (HEADER)
-        bcdADC               1.00
-        wTotalLength       0x0041
-      MIDIStreaming Interface Descriptor:
-        bLength                 6
-        bDescriptorType        36
-        bDescriptorSubtype      2 (MIDI_IN_JACK)
-        bJackType               1 Embedded
-        bJackID                 1
-        iJack                   0 
-      MIDIStreaming Interface Descriptor:
-        bLength                 6
-        bDescriptorType        36
-        bDescriptorSubtype      2 (MIDI_IN_JACK)
-        bJackType               2 External
-        bJackID                 2
-        iJack                   0 
-      MIDIStreaming Interface Descriptor:
-        bLength                 9
-        bDescriptorType        36
-        bDescriptorSubtype      3 (MIDI_OUT_JACK)
-        bJackType               1 Embedded
-        bJackID                 3
-        bNrInputPins            1
-        baSourceID( 0)          2
-        BaSourcePin( 0)         1
-        iJack                   0 
-      MIDIStreaming Interface Descriptor:
-        bLength                 9
-        bDescriptorType        36
-        bDescriptorSubtype      3 (MIDI_OUT_JACK)
-        bJackType               2 External
-        bJackID                 4
-        bNrInputPins            1
-        baSourceID( 0)          1
-        BaSourcePin( 0)         1
-        iJack                   0 
-      Endpoint Descriptor:
-        bLength                 9
-        bDescriptorType         5
-        bEndpointAddress     0x02  EP 2 OUT
-        bmAttributes            2
-          Transfer Type            Bulk
-          Synch Type               None
-          Usage Type               Data
-        wMaxPacketSize     0x0040  1x 64 bytes
-        bInterval               0
-        bRefresh                0
-        bSynchAddress           0
-        MIDIStreaming Endpoint Descriptor:
-          bLength                 5
-          bDescriptorType        37
-          bDescriptorSubtype      1 (GENERAL)
-          bNumEmbMIDIJack         1
-          baAssocJackID( 0)       1
-      Endpoint Descriptor:
-        bLength                 9
-        bDescriptorType         5
-        bEndpointAddress     0x83  EP 3 IN
-        bmAttributes            2
-          Transfer Type            Bulk
-          Synch Type               None
-          Usage Type               Data
-        wMaxPacketSize     0x0040  1x 64 bytes
-        bInterval               0
-        bRefresh                0
-        bSynchAddress           0
-        MIDIStreaming Endpoint Descriptor:
-          bLength                 5
-          bDescriptorType        37
-          bDescriptorSubtype      1 (GENERAL)
-          bNumEmbMIDIJack         1
-          baAssocJackID( 0)       3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions