arduino-audio-tools
USBDeviceAudioAdafruit.h
1 #pragma once
2 #define CFG_TUSB_DEBUG_PRINTF 3
3 #include "Adafruit_TinyUSB.h"
4 #include "USBDeviceAudio.h"
5 // Error message for Core that must select TinyUSB via menu
6 #if !defined(USE_TINYUSB) && \
7  (defined(ARDUINO_ARCH_SAMD) || (defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED)))
8 # error TinyUSB is not selected, please select it in "Tools->Menu->USB Stack"
9 #endif
10 
12 USBDeviceAudioAdafruit *self_USBDeviceAudioAdafruit = nullptr;
13 
14 /***
15  * @brief Adafruit TinyUSB Initialization logic.
16  */
17 class USBDeviceAudioAdafruit : public USBDeviceAudio, public Adafruit_USBD_Interface {
18  public:
20  self_USBDeviceAudioAdafruit = this;
21  }
22 
23  bool begin(USBAudioConfig config) override {
24  // setup config buffer
25  if (interface_descriptor.size() != 512){
26  interface_descriptor.resize(512);
27  TinyUSBDevice.setConfigurationBuffer(interface_descriptor.data(), 512);
28  }
29 
30  // add string descriptor
31  if (_stridx == 0) {
32  _stridx = TinyUSBDevice.addStringDescriptor("TinyUSB Audio");
33  }
34 
35  if (!USBDeviceAudio::begin(config)) {
36  LOG_AUDIO_ERROR("begin failed");
37  return false;
38  }
39 
40  // add the interface
41  if (!TinyUSBDevice.addInterface(*self_USBDeviceAudioAdafruit)) {
42  setStatus(AudioProcessingStatus::ERROR);
43  LOG_AUDIO_ERROR("addInterface failed");
44  return false;
45  }
46 
47  return true;
48  }
49 
50  uint8_t allocInterface(uint8_t count = 1) override {
51  return TinyUSBDevice.allocInterface(count);
52  }
53 
54  uint8_t allocEndpoint(uint8_t in) override {
55  return TinyUSBDevice.allocEndpoint(in);
56  }
57 
58  uint16_t getInterfaceDescriptor(uint8_t itfnum, uint8_t *buf,
59  uint16_t bufsize) override {
60  return USBDeviceAudio::getInterfaceDescriptor(buf, bufsize);
61  }
62 };
63 
64 
65 // callback to register custom application driver for AUDIO
66 usbd_class_driver_t const* usbd_app_driver_get_cb(uint8_t* driver_count) {
67  static usbd_class_driver_t audio_class_driver;
68  audio_class_driver.name = "AUDIO";
69  audio_class_driver.init = []() {
70  self_USBDeviceAudioAdafruit->begin(self_USBDeviceAudioAdafruit->api().config());
71  self_USBDeviceAudioAdafruit->api().audiod_init();
72  };
73  audio_class_driver.deinit = []() { return self_USBDeviceAudioAdafruit->api().audiod_deinit(); };
74  audio_class_driver.reset = [](uint8_t rhport) { self_USBDeviceAudioAdafruit->api().audiod_reset(rhport); };
75  audio_class_driver.open = [](uint8_t rhport, tusb_desc_interface_t const * desc_intf, uint16_t max_len) {
76  return self_USBDeviceAudioAdafruit->api().audiod_open(rhport, desc_intf, max_len);
77  };
78  audio_class_driver.control_xfer_cb = [](uint8_t rhport, uint8_t stage, tusb_control_request_t const * request){
79  return self_USBDeviceAudioAdafruit->api().audiod_control_xfer_cb(rhport, stage, request);
80  };
81  audio_class_driver.xfer_cb = [](uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes){
82  return self_USBDeviceAudioAdafruit->api().audiod_xfer_cb(rhport, ep_addr, result, xferred_bytes);
83  };
84  audio_class_driver.sof = [](uint8_t rhport, uint32_t frame_count) { self_USBDeviceAudioAdafruit->api().audiod_sof_isr(self_USBDeviceAudioAdafruit->api().config().rh_port, 0); };
85 
86 
87 
88  *driver_count = 1;
89  return &audio_class_driver;
90 }
91 
Configuration for TinyUSB Audio.
Definition: USBDeviceAudioAPI.h:43
Definition: USBDeviceAudioAdafruit.h:17
Definition: USBDeviceAudio.h:104
void setStatus(AudioProcessingStatus status)
Define the led delay.
Definition: USBDeviceAudio.h:615