arduino-audio-tools
USBDeviceAudioESP32.h
1 #pragma once
2 #include "USBDeviceAudio.h"
3 #include "USB.h"
4 #include "esp32-hal-tinyusb.h"
5 
6 #ifndef ARDUINO_USB_MODE
7 #error This ESP32 SoC has no Native USB interface
8 #else
9 #if ARDUINO_USB_MODE == 1
10 #error This sketch should be used when USB is in OTG mode
11 #endif
12 #endif
13 
14 
16  uint8_t *descr = nullptr;
17  int descr_len = 0;
18  int itf_count = 0;
19 };
20 
21 static USBConfigESP32 usb_audio_config_esp32;
22 
24 uint16_t tinyusb_audio_descriptor_cb(uint8_t *dst, uint8_t *itf) {
25  uint8_t str_index = tinyusb_add_string_descriptor("TinyUSB Audio");
26  *itf += usb_audio_config_esp32.itf_count;
27  memcpy(dst, usb_audio_config_esp32.descr, usb_audio_config_esp32.descr_len);
28  return usb_audio_config_esp32.descr_len;
29 };
30 
31 /***
32  * @brief ESP32 Initialization logic: We need to provide the USBAudioConfig in the
33  * constructor, so that we can determine the descriptor properly when the
34  * object is constructed.
35  */
37  public:
39  begin(config);
40 
41  int len = setupDescriptorCB();
42 
43  tinyusb_enable_interface(USB_INTERFACE_CUSTOM, len,
44  tinyusb_audio_descriptor_cb);
45  // arduino_usb_event_handler_register_with(ARDUINO_USB_EVENTS,
46  // ARDUINO_USB_STOPPED_EVENT,
47  // usb_unplugged_cb, this);
49  }
50 
51  virtual uint8_t allocInterface(uint8_t count = 1) {
52  uint8_t ret = _itf_count;
53  _itf_count += count;
54  return ret;
55  }
56 
57  virtual uint8_t allocEndpoint(uint8_t in) {
58  uint8_t ret = in ? (0x80 | _epin_count++) : _epout_count++;
59 #if defined(ARDUINO_ARCH_ESP32) && ARDUINO_USB_CDC_ON_BOOT && !ARDUINO_USB_MODE
60  // ESP32 reserves 0x03, 0x84, 0x85 for CDC Serial
61  if (ret == 0x03) {
62  ret = _epout_count++;
63  } else if (ret == 0x84 || ret == 0x85) {
64  // Note: ESP32 does not have this much of EP IN
65  _epin_count = 6;
66  ret = 0x86;
67  }
68 #endif
69  return ret;
70  }
71 
72  protected:
73  int _itf_count = 0;
74  int _epout_count = 0;
75  int _epin_count = 0;
76 
77  int setupDescriptorCB() {
78  int len = getInterfaceDescriptorLength(0);
79  usb_audio_config_esp32.descr_len = len;
80  interface_descriptor.resize(len);
81  usb_audio_config_esp32.descr = interface_descriptor.data();
82  usb_audio_config_esp32.itf_count = _itf_count;
83  getInterfaceDescriptor(0, usb_audio_config_esp32.descr, len);
84  return len;
85  }
86 
87 };
88 
89 
90 static USBDeviceAudioAdafruit USBAudio;
91 static usbd_class_driver_t audio_class_driver;
92 
93 TU_ATTR_FAST_FUNC void audiod_init() { USBAudio.api().audiod_init(); }
94 TU_ATTR_FAST_FUNC bool audiod_deinit() { return USBAudio.api().audiod_deinit(); }
95 TU_ATTR_FAST_FUNC void audiod_reset(uint8_t rhport) { USBAudio.api().audiod_reset(rhport); }
96 TU_ATTR_FAST_FUNC uint16_t audiod_open(uint8_t rhport, tusb_desc_interface_t const * desc_intf, uint16_t max_len) {
97  return USBAudio.api().audiod_open(rhport, desc_intf, max_len);
98 }
99 TU_ATTR_FAST_FUNC bool audiod_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request){
100  return USBAudio.api().audiod_control_xfer_cb(rhport, stage, request);
101 }
102 TU_ATTR_FAST_FUNC bool audiod_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes){
103  return USBAudio.api().audiod_xfer_cb(rhport, ep_addr, result, xferred_bytes);
104 }
105 
106 TU_ATTR_FAST_FUNC void tud_audio_feedback_interval_isr(uint8_t func_id,
107 uint32_t frame_number, uint8_t interval_shift){
108  return USBAudio.audiod_sof_isr(USBAudio.cfg.rh_port, 0);
109 }
110 
111 
112 // callback to register custom application driver for AUDIO
113 usbd_class_driver_t const* usbd_app_driver_get_cb(uint8_t* driver_count) {
114  audio_class_driver.name = "AUDIO";
115  audio_class_driver.init = audiod_init;
116  audio_class_driver.deinit = audiod_deinit;
117  audio_class_driver.open = audiod_open;
118  audio_class_driver.control_xfer_cb = audiod_control_xfer_cb;
119  audio_class_driver.xfer_cb = audiod_xfer_cb;
120  //audio_class_driver.sof = audiod_sof;
121  return &audio_class_driver;
122 }
123 
Configuration for TinyUSB Audio.
Definition: USBDeviceAudioAPI.h:43
Definition: USBDeviceAudioAdafruit.h:17
Definition: USBDeviceAudioESP32.h:36
Definition: USBDeviceAudio.h:104
void setupDebugPins()
Definition: USBDeviceAudio.h:619
size_t getInterfaceDescriptorLength(uint8_t itfnum) override
Determine the interface descriptor length.
Definition: USBDeviceAudio.h:555
Definition: USBDeviceAudioESP32.h:15