|
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+//! USB audio class for asynchronous, bidirectional (microphone + speakers) 48kHz audio.
|
|
|
2
|
+//!
|
|
|
3
|
+//! See "Universal Serial Bus Device Class Definition for Audio Devices", release 1.0 (March 18,
|
|
|
4
|
+//! 1998) for a specification of the USB audio class.
|
|
|
5
|
+use cortex_m::interrupt;
|
|
|
6
|
+use usb_device::class_prelude::*;
|
|
|
7
|
+use usb_device::endpoint::{IsochronousSynchronizationType, IsochronousUsageType};
|
|
|
8
|
+use usb_device::Result;
|
|
|
9
|
+
|
|
|
10
|
+const DEVICE_CLASS_AUDIO: u8 = 0x01;
|
|
|
11
|
+const AUDIO_SUBCLASS_CONTROL: u8 = 0x01;
|
|
|
12
|
+const AUDIO_SUBCLASS_STREAMING: u8 = 0x02;
|
|
|
13
|
+const AUDIO_PROTOCOL_NONE: u8 = 0x00;
|
|
|
14
|
+
|
|
|
15
|
+const AUDIO_INTERFACE_DESC_TYPE: u8 = 0x24;
|
|
|
16
|
+const AUDIO_ENDPOINT_DESC_TYPE: u8 = 0x25;
|
|
|
17
|
+
|
|
|
18
|
+const AUDIO_CONTROL_HEADER: u8 = 0x01;
|
|
|
19
|
+const AUDIO_CONTROL_INPUT_TERMINAL: u8 = 0x02;
|
|
|
20
|
+const AUDIO_CONTROL_OUTPUT_TERMINAL: u8 = 0x03;
|
|
|
21
|
+const AUDIO_CONTROL_FEATURE_UNIT: u8 = 0x06;
|
|
|
22
|
+
|
|
|
23
|
+const AUDIO_STREAMING_GENERAL: u8 = 0x01;
|
|
|
24
|
+const AUDIO_STREAMING_FORMAT_TYPE: u8 = 0x02;
|
|
|
25
|
+
|
|
|
26
|
+const AUDIO_ENDPOINT_GENERAL: u8 = 0x01;
|
|
|
27
|
+
|
|
|
28
|
+// TODO: Update bDelay.
|
|
|
29
|
+
|
|
|
30
|
+pub struct UsbAudioClass<'a, B: UsbBus, OUTBUF: AsRef<[u32]>> {
|
|
|
31
|
+ audio_control: InterfaceNumber,
|
|
|
32
|
+ audio_streaming_inactive: InterfaceNumber,
|
|
|
33
|
+ audio_streaming: InterfaceNumber,
|
|
|
34
|
+ audio_out: EndpointOut<'a, B>,
|
|
|
35
|
+ //audio_out_sync: EndpointIn<'a, B>,
|
|
|
36
|
+ //audio_in: EndpointIn<'a, B>,
|
|
|
37
|
+ samples_per_frame: [u8; 3],
|
|
|
38
|
+
|
|
|
39
|
+ audio_out_buf: Option<OUTBUF>,
|
|
|
40
|
+}
|
|
|
41
|
+
|
|
|
42
|
+impl<'a, B: UsbBus, OUTBUF: AsRef<[u32]>> UsbAudioClass<'a, B, OUTBUF> {
|
|
|
43
|
+ pub fn new(bus_alloc: &'a UsbBusAllocator<B>) -> Self {
|
|
|
44
|
+ UsbAudioClass {
|
|
|
45
|
+ audio_control: bus_alloc.interface(),
|
|
|
46
|
+ audio_streaming_inactive: bus_alloc.interface(),
|
|
|
47
|
+ audio_streaming: bus_alloc.interface(),
|
|
|
48
|
+ // 48kHz * 2 * 16bit = 192B per packet. We allocate a bit more in case the device clock
|
|
|
49
|
+ // is faster than the host clock.
|
|
|
50
|
+ /*audio_out: bus_alloc.isochronous(
|
|
|
51
|
+ IsochronousSynchronizationType::Asynchronous,
|
|
|
52
|
+ IsochronousUsageType::Data,
|
|
|
53
|
+ 256,
|
|
|
54
|
+ 1,
|
|
|
55
|
+ ),*/
|
|
|
56
|
+ audio_out: bus_alloc.isochronous(
|
|
|
57
|
+ IsochronousSynchronizationType::Synchronous,
|
|
|
58
|
+ IsochronousUsageType::Data,
|
|
|
59
|
+ 256,
|
|
|
60
|
+ 1,
|
|
|
61
|
+ ),
|
|
|
62
|
+ /*audio_out_sync: bus_alloc.isochronous(
|
|
|
63
|
+ IsochronousSynchronizationType::Asynchronous,
|
|
|
64
|
+ IsochronousUsageType::Feedback,
|
|
|
65
|
+ 3,
|
|
|
66
|
+ 1,
|
|
|
67
|
+ ),*/
|
|
|
68
|
+ /*audio_in: bus_alloc.isochronous(
|
|
|
69
|
+ IsochronousSynchronizationType::Asynchronous,
|
|
|
70
|
+ IsochronousUsageType::Data,
|
|
|
71
|
+ 256,
|
|
|
72
|
+ 1,
|
|
|
73
|
+ ),*/
|
|
|
74
|
+ samples_per_frame: [48 >> 2, 48 << 6, 0], // 48 kHz
|
|
|
75
|
+ audio_out_buf: None,
|
|
|
76
|
+ }
|
|
|
77
|
+ }
|
|
|
78
|
+
|
|
|
79
|
+ // TODO: Functions to read and write data?
|
|
|
80
|
+}
|
|
|
81
|
+
|
|
|
82
|
+impl<B: UsbBus, OUTBUF: AsRef<[u32]>> UsbClass<B> for UsbAudioClass<'_, B, OUTBUF> {
|
|
|
83
|
+ fn get_configuration_descriptors(&self, writer: &mut DescriptorWriter) -> Result<()> {
|
|
|
84
|
+ /*interrupt::free(|cs| {
|
|
|
85
|
+ writeln!(crate::uart(cs), "A\r").unwrap();
|
|
|
86
|
+ });*/
|
|
|
87
|
+ writer.iad(
|
|
|
88
|
+ self.audio_control,
|
|
|
89
|
+ 2, // Two interfaces (control + streaming).
|
|
|
90
|
+ 0x0, // Each interface specifies its own class.
|
|
|
91
|
+ 0x0, // Each interface specifies its own subclass.
|
|
|
92
|
+ 0x0, // No class-specific protocols for this device.
|
|
|
93
|
+ )?;
|
|
|
94
|
+
|
|
|
95
|
+ // Audio control interface.
|
|
|
96
|
+ writer.interface(
|
|
|
97
|
+ self.audio_control,
|
|
|
98
|
+ DEVICE_CLASS_AUDIO,
|
|
|
99
|
+ AUDIO_SUBCLASS_CONTROL,
|
|
|
100
|
+ AUDIO_PROTOCOL_NONE,
|
|
|
101
|
+ )?;
|
|
|
102
|
+
|
|
|
103
|
+ writer.write(
|
|
|
104
|
+ AUDIO_INTERFACE_DESC_TYPE,
|
|
|
105
|
+ &[
|
|
|
106
|
+ AUDIO_CONTROL_HEADER, // bDescriptorSubtype
|
|
|
107
|
+ 0x00, // bcdADC
|
|
|
108
|
+ 0x01, //
|
|
|
109
|
+ 37, // wTotalLength
|
|
|
110
|
+ 0, //
|
|
|
111
|
+ 0x01, // bInCollection
|
|
|
112
|
+ self.audio_streaming.into(), // baInterfaceNr
|
|
|
113
|
+ ],
|
|
|
114
|
+ )?;
|
|
|
115
|
+
|
|
|
116
|
+ // Input terminal (USB streaming).
|
|
|
117
|
+ writer.write(
|
|
|
118
|
+ AUDIO_INTERFACE_DESC_TYPE,
|
|
|
119
|
+ &[
|
|
|
120
|
+ AUDIO_CONTROL_INPUT_TERMINAL, // bDescriptorSubtype
|
|
|
121
|
+ 0x01, // bTerminalID
|
|
|
122
|
+ 0x01, // wTerminalType
|
|
|
123
|
+ 0x01, //
|
|
|
124
|
+ 0x00, // bAssocTerminal
|
|
|
125
|
+ 0x02, // bNrChannels
|
|
|
126
|
+ 0x03, // wChannelConfig
|
|
|
127
|
+ 0x00, //
|
|
|
128
|
+ 0x00, // iChannelNames
|
|
|
129
|
+ 0x00, // iTerminal
|
|
|
130
|
+ ],
|
|
|
131
|
+ )?;
|
|
|
132
|
+
|
|
|
133
|
+ // Feature unit (volume and mute).
|
|
|
134
|
+ writer.write(
|
|
|
135
|
+ AUDIO_INTERFACE_DESC_TYPE,
|
|
|
136
|
+ &[
|
|
|
137
|
+ AUDIO_CONTROL_FEATURE_UNIT, // bDescriptorSubtype
|
|
|
138
|
+ 0x02, // bUnitID
|
|
|
139
|
+ 0x01, // bSourceID
|
|
|
140
|
+ 0x01, // bControlSize
|
|
|
141
|
+ 0x03, // bmaControls(0)
|
|
|
142
|
+ 0x00, // iFeature
|
|
|
143
|
+ ],
|
|
|
144
|
+ )?;
|
|
|
145
|
+
|
|
|
146
|
+ // Output terminal (speaker).
|
|
|
147
|
+ writer.write(
|
|
|
148
|
+ AUDIO_INTERFACE_DESC_TYPE,
|
|
|
149
|
+ &[
|
|
|
150
|
+ AUDIO_CONTROL_OUTPUT_TERMINAL, // bDescriptorSubtype
|
|
|
151
|
+ 0x03, // bTerminalID
|
|
|
152
|
+ 0x01, // wTerminalType
|
|
|
153
|
+ 0x03, //
|
|
|
154
|
+ 0x00, // bAssocTerminal
|
|
|
155
|
+ 0x02, // bSourceID
|
|
|
156
|
+ 0x00, // iTerminal
|
|
|
157
|
+ ],
|
|
|
158
|
+ )?;
|
|
|
159
|
+
|
|
|
160
|
+ // Audio streaming interface (zero-bandwidth).
|
|
|
161
|
+ writer.interface(
|
|
|
162
|
+ self.audio_streaming_inactive,
|
|
|
163
|
+ DEVICE_CLASS_AUDIO,
|
|
|
164
|
+ AUDIO_SUBCLASS_STREAMING,
|
|
|
165
|
+ AUDIO_PROTOCOL_NONE,
|
|
|
166
|
+ )?;
|
|
|
167
|
+
|
|
|
168
|
+ // Audio streaming interface (operational).
|
|
|
169
|
+ writer.interface_alt(
|
|
|
170
|
+ self.audio_streaming,
|
|
|
171
|
+ 1,
|
|
|
172
|
+ DEVICE_CLASS_AUDIO,
|
|
|
173
|
+ AUDIO_SUBCLASS_STREAMING,
|
|
|
174
|
+ AUDIO_PROTOCOL_NONE,
|
|
|
175
|
+ None,
|
|
|
176
|
+ )?;
|
|
|
177
|
+
|
|
|
178
|
+ writer.write(
|
|
|
179
|
+ AUDIO_INTERFACE_DESC_TYPE,
|
|
|
180
|
+ &[
|
|
|
181
|
+ AUDIO_STREAMING_GENERAL, // bDescriptorSubtype
|
|
|
182
|
+ 0x01, // bTerminalLink
|
|
|
183
|
+ 0x03, // bDelay
|
|
|
184
|
+ 0x01, // wFormatTag
|
|
|
185
|
+ 0x00, //
|
|
|
186
|
+ ],
|
|
|
187
|
+ )?;
|
|
|
188
|
+
|
|
|
189
|
+ writer.write(
|
|
|
190
|
+ AUDIO_INTERFACE_DESC_TYPE,
|
|
|
191
|
+ &[
|
|
|
192
|
+ AUDIO_STREAMING_FORMAT_TYPE, // bDescriptorSubtype
|
|
|
193
|
+ 0x01, // bFormatType
|
|
|
194
|
+ 0x02, // bNrChannels
|
|
|
195
|
+ 0x02, // bSubframeSize
|
|
|
196
|
+ 0x10, // bBitResolution
|
|
|
197
|
+ 0x01, // bSamFreqType
|
|
|
198
|
+ ],
|
|
|
199
|
+ )?;
|
|
|
200
|
+
|
|
|
201
|
+ /*writer.endpoint_ex(&self.audio_out, |data| {
|
|
|
202
|
+ // TODO: Faster refresh
|
|
|
203
|
+ data[0] = 0x09; // bRefresh
|
|
|
204
|
+ data[1] = self.audio_out_sync.address().into(); // bSynchAddress
|
|
|
205
|
+ Ok(2)
|
|
|
206
|
+ })?;*/
|
|
|
207
|
+ writer.endpoint(&self.audio_out)?;
|
|
|
208
|
+
|
|
|
209
|
+ writer.write(
|
|
|
210
|
+ AUDIO_ENDPOINT_DESC_TYPE,
|
|
|
211
|
+ &[
|
|
|
212
|
+ AUDIO_ENDPOINT_GENERAL, // bDescriptorSubtype
|
|
|
213
|
+ 0x00, // bmAttributes
|
|
|
214
|
+ 0x01, // bLockDelayUnits
|
|
|
215
|
+ 0x00, // wLockDelay
|
|
|
216
|
+ 0x00,
|
|
|
217
|
+ ],
|
|
|
218
|
+ )?;
|
|
|
219
|
+
|
|
|
220
|
+ /*writer.endpoint_ex(&self.audio_out_sync, |data| {
|
|
|
221
|
+ data[0] = 0x00; // bRefresh
|
|
|
222
|
+ data[1] = 0x00; // bSynchAddress
|
|
|
223
|
+ Ok(2)
|
|
|
224
|
+ })?;*/
|
|
|
225
|
+
|
|
|
226
|
+ /*interrupt::free(|cs| {
|
|
|
227
|
+ writeln!(crate::uart(cs), "B\r").unwrap();
|
|
|
228
|
+ });*/
|
|
|
229
|
+
|
|
|
230
|
+ Ok(())
|
|
|
231
|
+ }
|
|
|
232
|
+
|
|
|
233
|
+ fn get_string(&self, _index: StringIndex, _lang_id: u16) -> Option<&str> {
|
|
|
234
|
+ // TODO
|
|
|
235
|
+ None
|
|
|
236
|
+ }
|
|
|
237
|
+
|
|
|
238
|
+ fn reset(&mut self) {
|
|
|
239
|
+ // Start sending synchronization data.
|
|
|
240
|
+ //self.endpoint_in_complete(self.audio_out_sync.address());
|
|
|
241
|
+ }
|
|
|
242
|
+
|
|
|
243
|
+ fn control_out(&mut self, xfer: ControlOut<B>) {
|
|
|
244
|
+ let req = xfer.request();
|
|
|
245
|
+
|
|
|
246
|
+ if !(req.request_type == control::RequestType::Class
|
|
|
247
|
+ && req.recipient == control::Recipient::Interface
|
|
|
248
|
+ && req.index == u8::from(self.audio_control) as u16)
|
|
|
249
|
+ {
|
|
|
250
|
+ return;
|
|
|
251
|
+ }
|
|
|
252
|
+
|
|
|
253
|
+ match req.request {
|
|
|
254
|
+ // TODO
|
|
|
255
|
+ _ => {
|
|
|
256
|
+ xfer.reject().ok();
|
|
|
257
|
+ }
|
|
|
258
|
+ };
|
|
|
259
|
+ }
|
|
|
260
|
+
|
|
|
261
|
+ fn control_in(&mut self, xfer: ControlIn<B>) {
|
|
|
262
|
+ let req = xfer.request();
|
|
|
263
|
+
|
|
|
264
|
+ if !(req.request_type == control::RequestType::Class
|
|
|
265
|
+ && req.recipient == control::Recipient::Interface
|
|
|
266
|
+ && req.index == u8::from(self.audio_control) as u16)
|
|
|
267
|
+ {
|
|
|
268
|
+ return;
|
|
|
269
|
+ }
|
|
|
270
|
+
|
|
|
271
|
+ match req.request {
|
|
|
272
|
+ // TODO
|
|
|
273
|
+ _ => {
|
|
|
274
|
+ xfer.reject().ok();
|
|
|
275
|
+ }
|
|
|
276
|
+ };
|
|
|
277
|
+ }
|
|
|
278
|
+
|
|
|
279
|
+ /*fn endpoint_out(&mut self, addr: EndpointAddress) {
|
|
|
280
|
+ if addr == self.audio_out.address() {
|
|
|
281
|
+ if self.audio_out_buf.is_some() {
|
|
|
282
|
+ // TODO: Write data into buffer, move buffer somewhere else.
|
|
|
283
|
+ } else {
|
|
|
284
|
+ // TODO
|
|
|
285
|
+ }
|
|
|
286
|
+ // TODO: Process incoming audio data.
|
|
|
287
|
+ let mut buffer = [0u8; 256];
|
|
|
288
|
+ self.audio_out.read(&mut buffer);
|
|
|
289
|
+ }
|
|
|
290
|
+ }
|
|
|
291
|
+
|
|
|
292
|
+ fn endpoint_in_complete(&mut self, addr: EndpointAddress) {
|
|
|
293
|
+ if addr == self.audio_out_sync.address() {
|
|
|
294
|
+ // Immediately write the next sync value.
|
|
|
295
|
+ //self.audio_out_sync.write(&self.samples_per_frame).unwrap();
|
|
|
296
|
+ }
|
|
|
297
|
+ }*/
|
|
|
298
|
+}
|
|
|
299
|
+
|
|
|
300
|
+/*use usb_device::class_prelude::*;
|
|
|
301
|
+use usb_device::endpoint::{IsochronousSynchronizationType, IsochronousUsageType};
|
|
|
302
|
+use usb_device::Result;
|
|
|
303
|
+
|
|
|
304
|
+/// This should be used as `device_class` when building the `UsbDevice`.
|
|
|
305
|
+pub const USB_INTERFACE_CLASS_AUDIO: u8 = 0x01;
|
|
|
306
|
+
|
|
|
307
|
+const USB_AUDIO_SUBCLASS_UNDEFINED: u8 = 0x0;
|
|
|
308
|
+const USB_AUDIO_SUBCLASS_AUDIOCONTROL: u8 = 0x01;
|
|
|
309
|
+const USB_AUDIO_SUBCLASS_AUDIOSTREAMING: u8 = 0x02;
|
|
|
310
|
+const USB_AUDIO_SUBCLASS_MIDISTREAMING: u8 = 0x03;
|
|
|
311
|
+
|
|
|
312
|
+const USB_CLASS_CDC_DATA: u8 = 0x0a;
|
|
|
313
|
+const CDC_SUBCLASS_ACM: u8 = 0x02;
|
|
|
314
|
+const USB_AUDIO_PROTOCOL_NONE: u8 = 0x00;
|
|
|
315
|
+
|
|
|
316
|
+const CS_DEVICE: u8 = 0x21;
|
|
|
317
|
+const CS_CONFIGURATION: u8 = 0x22;
|
|
|
318
|
+const CS_STRING: u8 = 0x23;
|
|
|
319
|
+const CS_INTERFACE: u8 = 0x24;
|
|
|
320
|
+const CS_ENDPOINT: u8 = 0x25;
|
|
|
321
|
+
|
|
|
322
|
+const EP_GENERAL: u8 = 0x1;
|
|
|
323
|
+
|
|
|
324
|
+const AC_DESC_TYPE_HEADER: u8 = 0x1;
|
|
|
325
|
+const AC_DESC_TYPE_INPUT_TERMINAL: u8 = 0x2;
|
|
|
326
|
+const AC_DESC_TYPE_OUTPUT_TERMINAL: u8 = 0x3;
|
|
|
327
|
+const AC_DESC_TYPE_MIXER_UNIT: u8 = 0x4;
|
|
|
328
|
+const AC_DESC_TYPE_SELECTOR_UNIT: u8 = 0x5;
|
|
|
329
|
+const AC_DESC_TYPE_FEATURE_UNIT: u8 = 0x6;
|
|
|
330
|
+const AC_DESC_TYPE_PROCESSING_UNIT: u8 = 0x7;
|
|
|
331
|
+const AC_DESC_TYPE_EXTENSION_UNIT: u8 = 0x8;
|
|
|
332
|
+
|
|
|
333
|
+const AC_DESC_ST_GENERAL: u8 = 0x1;
|
|
|
334
|
+const AC_DESC_ST_FORMAT_TYPE: u8 = 0x2;
|
|
|
335
|
+const AC_DESC_ST_FORMAT_SPECIFIC: u8 = 0x3;
|
|
|
336
|
+
|
|
|
337
|
+const AUDIO_SUB_TYPE_HEADER: u8 = 0x01;
|
|
|
338
|
+const AUDIO_SUB_TYPE_INPUT_TERMINAL: u8 = 0x02;
|
|
|
339
|
+const AUDIO_SUB_TYPE_FEATURE_UNIT: u8 = 0x06;
|
|
|
340
|
+const AUDIO_SUB_TYPE_OUTPUT_TERMINAL: u8 = 0x03;
|
|
|
341
|
+const AUDIO_SUB_TYPE_AS_GENERAL: u8 = 0x01;
|
|
|
342
|
+const AUDIO_SUB_TYPE_FORMAT_TYPE: u8 = 0x02;
|
|
|
343
|
+
|
|
|
344
|
+pub struct UsbAudioClass<'a, B: UsbBus> {
|
|
|
345
|
+ audio_control_if: InterfaceNumber,
|
|
|
346
|
+ audio_stream: InterfaceNumber,
|
|
|
347
|
+ alt_audio_stream: InterfaceNumber,
|
|
|
348
|
+ audio_in_ep: EndpointIn<'a, B>,
|
|
|
349
|
+}
|
|
|
350
|
+
|
|
|
351
|
+impl<B: UsbBus> UsbAudioClass<'_, B> {
|
|
|
352
|
+ /// Creates a new UsbAudioClass with the provided UsbBus and max_packet_size in bytes. For
|
|
|
353
|
+ /// full-speed devices, max_packet_size has to be one of 8, 16, 32 or 64.
|
|
|
354
|
+ pub fn new(alloc: &UsbBusAllocator<B>, max_packet_size: u16) -> UsbAudioClass<'_, B> {
|
|
|
355
|
+ UsbAudioClass {
|
|
|
356
|
+ audio_control_if: alloc.interface(),
|
|
|
357
|
+ audio_stream: alloc.interface(),
|
|
|
358
|
+ alt_audio_stream: alloc.interface(),
|
|
|
359
|
+ audio_in_ep: alloc.isochronous(
|
|
|
360
|
+ IsochronousSynchronizationType::Synchronous,
|
|
|
361
|
+ IsochronousUsageType::Data,
|
|
|
362
|
+ max_packet_size,
|
|
|
363
|
+ 4,
|
|
|
364
|
+ ),
|
|
|
365
|
+ }
|
|
|
366
|
+ }
|
|
|
367
|
+
|
|
|
368
|
+ /// Gets the maximum packet size in bytes.
|
|
|
369
|
+ pub fn max_packet_size(&self) -> u16 {
|
|
|
370
|
+ self.audio_in_ep.max_packet_size()
|
|
|
371
|
+ }
|
|
|
372
|
+
|
|
|
373
|
+ /// Writes a single packet into the IN endpoint.
|
|
|
374
|
+ pub fn write_packet(&mut self, data: &[u8]) -> Result<usize> {
|
|
|
375
|
+ //defmt::info!("write_packet");
|
|
|
376
|
+ self.audio_in_ep.write(data)
|
|
|
377
|
+ }
|
|
|
378
|
+
|
|
|
379
|
+ /// Gets the address of the IN endpoint.
|
|
|
380
|
+ pub(crate) fn write_ep_address(&self) -> EndpointAddress {
|
|
|
381
|
+ self.audio_in_ep.address()
|
|
|
382
|
+ }
|
|
|
383
|
+}
|
|
|
384
|
+
|
|
|
385
|
+impl<B: UsbBus> UsbClass<B> for UsbAudioClass<'_, B> {
|
|
|
386
|
+ fn get_configuration_descriptors(&self, writer: &mut DescriptorWriter) -> Result<()> {
|
|
|
387
|
+ writer.iad(
|
|
|
388
|
+ self.audio_control_if,
|
|
|
389
|
+ 3,
|
|
|
390
|
+ 0x0, // device defined at interface level
|
|
|
391
|
+ 0x0, // subclass unused
|
|
|
392
|
+ USB_AUDIO_PROTOCOL_NONE,
|
|
|
393
|
+ )?;
|
|
|
394
|
+
|
|
|
395
|
+ writer.interface(
|
|
|
396
|
+ self.audio_control_if,
|
|
|
397
|
+ USB_INTERFACE_CLASS_AUDIO,
|
|
|
398
|
+ USB_AUDIO_SUBCLASS_AUDIOCONTROL,
|
|
|
399
|
+ USB_AUDIO_PROTOCOL_NONE,
|
|
|
400
|
+ )?;
|
|
|
401
|
+
|
|
|
402
|
+ writer.write(
|
|
|
403
|
+ CS_INTERFACE,
|
|
|
404
|
+ &[
|
|
|
405
|
+ AUDIO_SUB_TYPE_HEADER, // bDescriptorSubtype
|
|
|
406
|
+ 0x00,
|
|
|
407
|
+ 0x1, // bcdADC (1.0),
|
|
|
408
|
+ 43,
|
|
|
409
|
+ 0, // wTotalLength (Compute this!)
|
|
|
410
|
+ 0x01, // bInCollection (1 streaming interface)
|
|
|
411
|
+ 0x01, // baInterfaceNr (Interface 1 is stream)
|
|
|
412
|
+ ],
|
|
|
413
|
+ )?;
|
|
|
414
|
+
|
|
|
415
|
+ writer.write(
|
|
|
416
|
+ CS_INTERFACE,
|
|
|
417
|
+ &[
|
|
|
418
|
+ AUDIO_SUB_TYPE_INPUT_TERMINAL, // bDescriptorSubtype
|
|
|
419
|
+ 0x01, // bTerminalID 1,
|
|
|
420
|
+ 0x10,
|
|
|
421
|
+ 0x07, // wTerminalType (radio receiver)
|
|
|
422
|
+ 0x00, // bAssocTerminal (none)
|
|
|
423
|
+ 0x02, // bNrChannels - 2 for I and Q
|
|
|
424
|
+ 0x03,
|
|
|
425
|
+ 0x00, // wChannelConfig (left, right)
|
|
|
426
|
+ 0x00, // iChannelNames (none)
|
|
|
427
|
+ 0x00, // iTerminal (none)
|
|
|
428
|
+ ],
|
|
|
429
|
+ )?;
|
|
|
430
|
+
|
|
|
431
|
+ writer.write(
|
|
|
432
|
+ CS_INTERFACE,
|
|
|
433
|
+ &[
|
|
|
434
|
+ AUDIO_SUB_TYPE_FEATURE_UNIT, // bDescriptorSubtype
|
|
|
435
|
+ 0x02, // bUnitID,
|
|
|
436
|
+ 0x01, // bSourceID (input terminal 1)
|
|
|
437
|
+ 0x02, // bControlSize (2 bytes)
|
|
|
438
|
+ 0x01,
|
|
|
439
|
+ 0x00, // Master controls
|
|
|
440
|
+ 0x00,
|
|
|
441
|
+ 0x00, // Channel 0 controls
|
|
|
442
|
+ 0x00,
|
|
|
443
|
+ 0x00, // Channel 1 controls
|
|
|
444
|
+ 0x00, // iFeature (none)
|
|
|
445
|
+ ],
|
|
|
446
|
+ )?;
|
|
|
447
|
+
|
|
|
448
|
+ writer.write(
|
|
|
449
|
+ CS_INTERFACE,
|
|
|
450
|
+ &[
|
|
|
451
|
+ AUDIO_SUB_TYPE_OUTPUT_TERMINAL, // bDescriptorSubtype
|
|
|
452
|
+ 0x03, // bTerminalID,
|
|
|
453
|
+ 0x01,
|
|
|
454
|
+ 0x01, // wTerminalType (USB Streaming)
|
|
|
455
|
+ 0x00, // bAssocTerminal (none)
|
|
|
456
|
+ 0x02, // bSourceID (feature unit 2)
|
|
|
457
|
+ 0x00, // iTerminal (none)
|
|
|
458
|
+ ],
|
|
|
459
|
+ )?;
|
|
|
460
|
+
|
|
|
461
|
+ writer.interface(
|
|
|
462
|
+ self.audio_stream,
|
|
|
463
|
+ USB_INTERFACE_CLASS_AUDIO,
|
|
|
464
|
+ USB_AUDIO_SUBCLASS_AUDIOSTREAMING,
|
|
|
465
|
+ USB_AUDIO_PROTOCOL_NONE,
|
|
|
466
|
+ )?;
|
|
|
467
|
+
|
|
|
468
|
+ //alternate audio stream
|
|
|
469
|
+ /*writer.interface_alt(
|
|
|
470
|
+ self.alt_audio_stream,
|
|
|
471
|
+ 1,
|
|
|
472
|
+ USB_INTERFACE_CLASS_AUDIO,
|
|
|
473
|
+ USB_AUDIO_SUBCLASS_AUDIOSTREAMING,
|
|
|
474
|
+ USB_AUDIO_PROTOCOL_NONE,
|
|
|
475
|
+ None,
|
|
|
476
|
+ )?;*/
|
|
|
477
|
+
|
|
|
478
|
+ // Audio Stream Audio Class Descriptor
|
|
|
479
|
+ writer.write(
|
|
|
480
|
+ CS_INTERFACE,
|
|
|
481
|
+ &[
|
|
|
482
|
+ AUDIO_SUB_TYPE_AS_GENERAL, // bDescriptorSubtype
|
|
|
483
|
+ 0x03, // bTerminalID,
|
|
|
484
|
+ 0x00, // bDelay
|
|
|
485
|
+ 0x01,
|
|
|
486
|
+ 0x00, // wFormatTag (PCM Format)
|
|
|
487
|
+ ],
|
|
|
488
|
+ )?;
|
|
|
489
|
+
|
|
|
490
|
+ // Format Type Audio Descriptor
|
|
|
491
|
+ writer.write(
|
|
|
492
|
+ CS_INTERFACE,
|
|
|
493
|
+ &[
|
|
|
494
|
+ AUDIO_SUB_TYPE_FORMAT_TYPE, // bDescriptorSubtype
|
|
|
495
|
+ 0x01, // bFormatType (TYPE_I)
|
|
|
496
|
+ 0x02, // bNrChannels (2)
|
|
|
497
|
+ 0x02, // bSubFrameSize (2)
|
|
|
498
|
+ 0x10, // bBitResolution (16 bits)
|
|
|
499
|
+ 0x01, // bSamFreqType (1 sample frequency)
|
|
|
500
|
+ 0x80, // 8*2 = 16 KHz byte 0
|
|
|
501
|
+ 0x3E, // 8*2 = 16 KHz byte 1
|
|
|
502
|
+ 0x00, // 8*2 = 16 KHz byte 2
|
|
|
503
|
+ ],
|
|
|
504
|
+ )?;
|
|
|
505
|
+
|
|
|
506
|
+ // TODO: Set the necessary flags for Isochronous
|
|
|
507
|
+ writer.endpoint(&self.audio_in_ep)?;
|
|
|
508
|
+
|
|
|
509
|
+ // Isochronous endpoint Audio Class descriptor
|
|
|
510
|
+ writer.write(
|
|
|
511
|
+ CS_ENDPOINT,
|
|
|
512
|
+ &[
|
|
|
513
|
+ EP_GENERAL, // bDescriptorSubtype
|
|
|
514
|
+ 0x00, // bmAttributes (none)
|
|
|
515
|
+ 0x02, // bLockDelayUnits (PCM Samples)
|
|
|
516
|
+ 0x00, 0x00, // wLockDelay (0) - should be zero for asynchronous
|
|
|
517
|
+ ],
|
|
|
518
|
+ )?;
|
|
|
519
|
+ Ok(())
|
|
|
520
|
+ }
|
|
|
521
|
+
|
|
|
522
|
+ fn reset(&mut self) {}
|
|
|
523
|
+
|
|
|
524
|
+ fn control_in(&mut self, xfer: ControlIn<B>) {
|
|
|
525
|
+ let req = xfer.request();
|
|
|
526
|
+
|
|
|
527
|
+ if !(req.request_type == control::RequestType::Class
|
|
|
528
|
+ && req.recipient == control::Recipient::Interface
|
|
|
529
|
+ && req.index == u8::from(self.audio_control_if) as u16)
|
|
|
530
|
+ {
|
|
|
531
|
+ return;
|
|
|
532
|
+ }
|
|
|
533
|
+
|
|
|
534
|
+ //defmt::info!("control_in - req : {:?}", req.request);
|
|
|
535
|
+ match req.request {
|
|
|
536
|
+ /*
|
|
|
537
|
+ REQ_GET_LINE_CODING if req.length == 7 => {
|
|
|
538
|
+ xfer.accept(|data| {
|
|
|
539
|
+ data[0..4].copy_from_slice(&self.line_coding.data_rate.to_le_bytes());
|
|
|
540
|
+ data[4] = self.line_coding.stop_bits as u8;
|
|
|
541
|
+ data[5] = self.line_coding.parity_type as u8;
|
|
|
542
|
+ data[6] = self.line_coding.data_bits;
|
|
|
543
|
+
|
|
|
544
|
+ Ok(7)
|
|
|
545
|
+ }).ok();
|
|
|
546
|
+ },
|
|
|
547
|
+ */
|
|
|
548
|
+ _ => {
|
|
|
549
|
+ xfer.reject().ok();
|
|
|
550
|
+ }
|
|
|
551
|
+ }
|
|
|
552
|
+ }
|
|
|
553
|
+
|
|
|
554
|
+ fn control_out(&mut self, xfer: ControlOut<B>) {
|
|
|
555
|
+ let req = xfer.request();
|
|
|
556
|
+
|
|
|
557
|
+ if !(req.request_type == control::RequestType::Class
|
|
|
558
|
+ && req.recipient == control::Recipient::Interface
|
|
|
559
|
+ && req.index == u8::from(self.audio_control_if) as u16)
|
|
|
560
|
+ {
|
|
|
561
|
+ return;
|
|
|
562
|
+ }
|
|
|
563
|
+
|
|
|
564
|
+ match req.request {
|
|
|
565
|
+ /*
|
|
|
566
|
+ REQ_SEND_ENCAPSULATED_COMMAND => {
|
|
|
567
|
+ // We don't actually support encapsulated commands but pretend we do for standards
|
|
|
568
|
+ // compatibility.
|
|
|
569
|
+ xfer.accept().ok();
|
|
|
570
|
+ },*/
|
|
|
571
|
+ _ => {
|
|
|
572
|
+ xfer.reject().ok();
|
|
|
573
|
+ }
|
|
|
574
|
+ };
|
|
|
575
|
+ }
|
|
|
576
|
+}*/
|