forked from ARMmbed/mbed-os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
USBAudio.h
389 lines (324 loc) · 10.4 KB
/
USBAudio.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* Copyright (c) 2018-2019, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef USBAudio_H
#define USBAudio_H
/* These headers are included for child class. */
#include "USBDescriptor.h"
#include "USBDevice_Types.h"
#include "USBDevice.h"
#include "Callback.h"
#include "OperationList.h"
#include "ByteBuffer.h"
#include "rtos/EventFlags.h"
/** \defgroup drivers-public-api-usb USB
* \ingroup drivers-public-api
*/
/**
* \defgroup drivers_USBAudio USBAudio class
* \ingroup drivers-public-api-usb
* @{
*/
/**
* USBAudio example
*
* @code
* #include "mbed.h"
* #include "USBAudio.h"
*
* // Audio loopback example use:
* // 1. Select "Mbed Audio" as your sound device
* // 2. Play a song or audio file
* // 3. Record the output using a program such as Audacity
*
* int main() {
*
* USBAudio audio(true, 44100, 2, 44100, 2);
*
* printf("Looping audio\r\n");
* static uint8_t buf[128];
* while (true) {
* if (!audio.read(buf, sizeof(buf))) {
* memset(buf, 0, sizeof(buf));
* }
* audio.write(buf, sizeof(buf));
* }
* }
* @endcode
*/
class USBAudio: protected USBDevice {
public:
enum AudioEvent {
Start,
Transfer,
End
};
/**
* Basic constructor
*
* Construct this object optionally connecting.
*
* @note Do not use this constructor in derived classes.
*
* @param connect Call connect on initialization
* @param frequency_rx frequency in Hz (default: 48000)
* @param channel_count_rx channel number (1 or 2) (default: 1)
* @param frequency_tx frequency in Hz (default: 8000)
* @param channel_count_tx channel number (1 or 2) (default: 1)
* @param buffer_ms time audio can be buffered without overflowing in milliseconds
* @param vendor_id Your vendor_id
* @param product_id Your product_id
* @param product_release Your product_release
*/
USBAudio(bool connect = true, uint32_t frequency_rx = 48000, uint8_t channel_count_rx = 1, uint32_t frequency_tx = 8000, uint8_t channel_count_tx = 1, uint32_t buffer_ms = 10, uint16_t vendor_id = 0x7bb8, uint16_t product_id = 0x1111, uint16_t product_release = 0x0100);
/**
* Fully featured constructor
*
* Construct this object with the supplied USBPhy and parameters. The user
* this object is responsible for calling connect() or init().
*
* @note Derived classes must use this constructor and call init() or
* connect() themselves. Derived classes should also call deinit() in
* their destructor. This ensures that no interrupts can occur when the
* object is partially constructed or destroyed.
*
* @param phy USB phy to use
* @param frequency_rx frequency in Hz (default: 48000)
* @param channel_count_rx channel number (1 or 2) (default: 1)
* @param frequency_tx frequency in Hz (default: 8000)
* @param channel_count_tx channel number (1 or 2) (default: 1)
* @param buffer_ms time audio can be buffered without overflowing in milliseconds
* @param vendor_id Your vendor_id
* @param product_id Your product_id
* @param product_release Your product_release
*/
USBAudio(USBPhy *phy, uint32_t frequency_rx, uint8_t channel_count_rx, uint32_t frequency_tx, uint8_t channel_count_tx, uint32_t buffer_ms, uint16_t vendor_id, uint16_t product_id, uint16_t product_release);
/**
* Destroy this object
*
* Any classes which inherit from this class must call deinit
* before this destructor runs.
*/
virtual ~USBAudio();
/**
* Connect USBAudio
*/
void connect();
/**
* Disconnect USBAudio
*
* This unblocks all calls to read_ready and write_ready.
*/
void disconnect();
/**
* Read audio data
*
* @param buf pointer on a buffer which will be filled with audio data
* @param size size to read
*
* @returns true if successful
*/
bool read(uint8_t *buf, uint32_t size);
/**
* Nonblocking audio data read
*
* Read the available audio data.
*
* @param buf pointer on a buffer which will be filled with audio data
* @param size size to read
* @param actual size actually read
* @note This function is safe to call from USBAudio callbacks.
*/
void read_nb(uint8_t *buf, uint32_t size, uint32_t *actual);
/**
* Return the number read packets dropped due to overflow
*
* @param clear Reset the overflow count back to 0
* @return Number of packets dropped due to overflow
*/
uint32_t read_overflows(bool clear = false);
/**
* Check if the audio read channel is open
*
* @return true if the audio read channel open, false otherwise
*/
bool read_ready();
/**
* Wait until the audio read channel is open
*/
void read_wait_ready();
/**
* Write audio data
*
* @param buf pointer to audio data to write
* @param size size to write
*
* @returns true if successful
*/
bool write(uint8_t *buf, uint32_t size);
/**
* Nonblocking audio data write
*
* Write the available audio data.
*
* @param buf pointer to audio data to write
* @param size size to write
* @param actual actual size written
* @note This function is safe to call from USBAudio callbacks.
*/
void write_nb(uint8_t *buf, uint32_t size, uint32_t *actual);
/**
* Return the number write packets not sent due to underflow
*
* @param clear Reset the underflow count back to 0
* @return Number of packets that should have been
* sent but weren't due to overflow
*/
uint32_t write_underflows(bool clear = false);
/**
* Check if the audio write channel is open
*
* @return true if the audio write channel open, false otherwise
*/
bool write_ready();
/**
* Wait until the audio write channel is open
*/
void write_wait_ready();
/**
* Get current volume between 0.0 and 1.0
*
* @returns volume
*/
float get_volume();
/** Attach a Callback to update the volume
*
* @param cb Callback to attach
*
*/
void attach(mbed::Callback<void()> &cb);
/** attach a Callback to Tx Done
*
* @param cb Callback to attach
*
*/
void attach_tx(mbed::Callback<void(AudioEvent)> &cb);
/** attach a Callback to Rx Done
*
* @param cb Callback to attach
*
*/
void attach_rx(mbed::Callback<void(AudioEvent)> &cb);
protected:
virtual void callback_state_change(DeviceState new_state);
virtual void callback_request(const setup_packet_t *setup);
virtual void callback_request_xfer_done(const setup_packet_t *setup, bool aborted);
virtual void callback_set_configuration(uint8_t configuration);
virtual void callback_set_interface(uint16_t interface, uint8_t alternate);
virtual const uint8_t *string_iproduct_desc();
virtual const uint8_t *string_iinterface_desc();
virtual const uint8_t *configuration_desc(uint8_t index);
private:
class AsyncWrite;
class AsyncRead;
enum ChannelState {
Powerdown,
Closed,
Opened
};
void _init(uint32_t frequency_rx, uint8_t channel_count_rx, uint32_t frequency_tx, uint8_t channel_count_tx, uint32_t buffer_ms);
/*
* Call to rebuild the configuration descriptor
*
* This function should be called on creation or when any
* value that is part of the configuration descriptor
* changes.
* @note This function uses ~200 bytes of stack so
* make sure your stack is big enough for it.
*/
void _build_configuration_desc();
void _receive_change(ChannelState new_state);
void _receive_isr();
void _send_change(ChannelState new_state);
void _send_isr_start();
void _send_isr_next_sync();
void _send_isr();
// has connect been called
bool _connected;
// audio volume
float _volume;
// mute state
uint8_t _mute;
// Volume Current Value
uint16_t _vol_cur;
// Volume Minimum Value
uint16_t _vol_min;
// Volume Maximum Value
uint16_t _vol_max;
// Volume Resolution
uint16_t _vol_res;
// callback to update volume
mbed::Callback<void()> _update_vol;
// callback transmit Done
mbed::Callback<void(AudioEvent)> _tx_done;
// callback receive Done
mbed::Callback<void(AudioEvent)> _rx_done;
// Number of times data was dropped due to an overflow
uint32_t _rx_overflow;
// Number of times data was not sent due to an underflow
uint32_t _tx_underflow;
// frequency in Hz
uint32_t _tx_freq;
uint32_t _rx_freq;
// mono, stereo,...
uint8_t _rx_channel_count;
uint8_t _tx_channel_count;
bool _tx_idle;
uint16_t _tx_frame_fract;
uint16_t _tx_whole_frames_per_xfer;
uint16_t _tx_fract_frames_per_xfer;
// size of the maximum packet for the isochronous endpoint
uint16_t _tx_packet_size_max;
uint16_t _rx_packet_size_max;
// Buffer used for the isochronous transfer
uint8_t *_tx_packet_buf;
uint8_t *_rx_packet_buf;
// Holding buffer
ByteBuffer _tx_queue;
ByteBuffer _rx_queue;
// State of the audio channels
ChannelState _tx_state;
ChannelState _rx_state;
// sample - a single PCM audio sample
// frame - a group of samples from each channel
// packet - a group of frames sent over USB in one transfer
// Blocking primitives
OperationList<AsyncWrite> _write_list;
OperationList<AsyncRead> _read_list;
rtos::EventFlags _flags;
// endpoint numbers
usb_ep_t _episo_out; // rx endpoint
usb_ep_t _episo_in; // tx endpoint
// channel config in the configuration descriptor: master, left, right
uint16_t _channel_config_rx;
uint16_t _channel_config_tx;
// configuration descriptor
uint8_t _config_descriptor[183];
// buffer for control requests
uint8_t _control_receive[2];
};
/** @}*/
#endif