forked from arpruss/USBComposite_stm32f1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb_audio.h
320 lines (280 loc) · 9.02 KB
/
usb_audio.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
/* Copyright (c) 2019, Scott Moreau
**
** Permission to use, copy, modify, and/or distribute this software for
** any purpose with or without fee is hereby granted, provided that the
** above copyright notice and this permission notice appear in all copies.
**
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
** SOFTWARE.
*/
#ifndef __USB_AUDIO_H
#define __USB_AUDIO_H
#include <libmaple/libmaple_types.h>
#include <libmaple/usb.h>
#include "usb_generic.h"
#define AUDIO_SAMPLE_FREQ_0(frq) (uint8_t)((frq >> 16))
#define AUDIO_SAMPLE_FREQ_1(frq) (uint8_t)((frq >> 8))
#define AUDIO_SAMPLE_FREQ_2(frq) (uint8_t)(frq)
#define MAX_AUDIO_BUFFERS 8
#define AUDIO_BUFFER_SIZE(n,reportID) ((n)+((reportID)!=0))
#define AUDIO_BUFFER_ALLOCATE_SIZE(n,reportID) ((AUDIO_BUFFER_SIZE((n),(reportID))+1)/2*2)
#define AUDIO_BUFFER_MODE_NO_WAIT 1
#define AUDIO_BUFFER_MODE_OUTPUT 2
#define AUDIO_BUFFER_EMPTY 0
#define AUDIO_BUFFER_UNREAD 1
#define AUDIO_BUFFER_READ 2
#define AUDIO_ENDPOINT_TX 0
#define USB_AUDIO_TX_ENDP (audioEndpoints[AUDIO_ENDPOINT_TX].address)
#define USB_AUDIO_TX_ADDR (audioEndpoints[AUDIO_ENDPOINT_TX].pmaAddress)
/* Descriptor constants */
#define USB_INTERFACE_ASSOCIATION_DESCRIPTOR 0x0B
#define USB_DEVICE_CLASS_AUDIO 0x01
#define AUDIO_SUBCLASS_AUDIOCONTROL 0x01
#define AUDIO_SUBCLASS_AUDIOSTREAMING 0x02
#define AUDIO_STREAMING_FORMAT_TYPE 0x02
#define AUDIO_INTERFACE_DESCRIPTOR_TYPE 0x24
#define AUDIO_ENDPOINT_DESCRIPTOR_TYPE 0x25
#define AUDIO_CONTROL_FEATURE_UNIT 0x06
#define AUDIO_FORMAT_TYPE_I 0x01
#define AUDIO_MAX_EP_BUFFER_SIZE 128
#define AUDIO_CLASS_1 0x0100
#define AUDIO_CLASS_2 0x1000
#define MIC_MONO 0x01
#define MIC_STEREO 0x02
#define SPEAKER_MONO 0x03
#define SPEAKER_STEREO 0x04
extern USBCompositePart usbAUDIOPart;
#ifdef __cplusplus
extern "C" {
#endif
/* Standard Interface Association Descriptor */
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bFirstInterface;
uint8_t bInterfaceCount;
uint8_t bFunctionClass;
uint8_t bFunctionSubClass;
uint8_t bFunctionProtocol;
uint8_t iFunction;
} __packed audio_interface_association_descriptor;
/* Interface Header Audio Class Descriptor v1 */
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubtype;
uint16_t bcdADC;
uint16_t wTotalLength;
uint8_t bInCollection;
uint8_t baInterfaceNr;
} __packed audio_control_descriptor;
/* Interface Header Audio Class Descriptor v2 */
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubtype;
uint16_t bcdADC;
uint8_t bCategory;
uint16_t wTotalLength;
uint8_t bmControls;
} __packed audio_control_descriptor_2;
/* Clock Source Descriptor */
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubtype;
uint8_t bClockID;
uint8_t bmAttributes;
uint8_t bmControls;
uint8_t bAssocTerminal;
uint8_t iClockSource;
} __packed audio_clock_source_descriptor;
/* Input Terminal Audio Class Descriptor v1 */
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubtype;
uint8_t bTerminalID;
uint16_t wTerminalType;
uint8_t bAssocTerminal;
uint8_t bNrChannels;
uint16_t wChannelConfig;
uint8_t iChannelNames;
uint8_t iTerminal;
} __packed input_terminal_descriptor;
/* Input Terminal Audio Class Descriptor v2 */
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubtype;
uint8_t bTerminalID;
uint16_t wTerminalType;
uint8_t bAssocTerminal;
uint8_t bCSourceID;
uint8_t bNrChannels;
uint32_t bmChannelConfig;
uint8_t iChannelNames;
uint16_t bmControls;
uint8_t iTerminal;
} __packed input_terminal_descriptor_2;
/* Output Terminal Audio Class Descriptor v1 */
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubtype;
uint8_t bTerminalID;
uint16_t wTerminalType;
uint8_t bAssocTerminal;
uint8_t bSourceID;
uint8_t iTerminal;
} __packed output_terminal_descriptor;
/* Output Terminal Audio Class Descriptor v2 */
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubtype;
uint8_t bTerminalID;
uint16_t wTerminalType;
uint8_t bAssocTerminal;
uint8_t bSourceID;
uint8_t bCSourceID;
uint16_t bmControls;
uint8_t iTerminal;
} __packed output_terminal_descriptor_2;
/* Feature Unit Audio Class Descriptor v1 */
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubtype;
uint8_t bUnitID;
uint8_t bSourceID;
uint8_t bControlSize;
uint8_t bmaControls0;
uint8_t bmaControls1;
uint8_t iFeature;
} __packed feature_unit_descriptor;
/* Feature Unit Audio Class Descriptor v2 */
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubtype;
uint8_t bUnitID;
uint8_t bSourceID;
uint32_t bmaControls0;
uint32_t bmaControls1;
uint8_t iFeature;
} __packed feature_unit_descriptor_2;
/* Alternate Audio Interface Descriptor */
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bInterfaceNumber;
uint8_t bAlternateSetting;
uint8_t bNumEndpoints;
uint8_t bInterfaceClass;
uint8_t bInterfaceSubclass;
uint8_t bInterfaceProtocol;
uint8_t iInterface;
} __packed audio_streaming_descriptor;
/* Audio Stream Audio Class Descriptor v1 */
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubtype;
uint8_t bTerminalLink;
uint8_t bDelay;
uint16_t wFormatTag;
} __packed audio_stream_audio_class_descriptor;
/* Audio Stream Audio Class Descriptor v2 */
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubtype;
uint8_t bTerminalLink;
uint8_t bmControls;
uint8_t bFormatType;
uint32_t bmFormats;
uint8_t bNrChannels;
uint32_t bmChannelConfig;
uint8_t iChannelNames;
} __packed audio_stream_audio_class_descriptor_2;
/* Format Type Audio Descriptor v1 */
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubtype;
uint8_t bFormatType;
uint8_t bNrChannels;
uint8_t bSubFrameSize;
uint8_t bBitResolution;
uint8_t bSamFreqType;
uint8_t tSamFreq2;
uint8_t tSamFreq1;
uint8_t tSamFreq0;
} __packed audio_format_type_descriptor;
/* Format Type Audio Descriptor v2 */
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubtype;
uint8_t bFormatType;
uint8_t bSubSlotSize;
uint8_t bBitResolution;
} __packed audio_format_type_descriptor_2;
/* Isochronous Endpoint Descriptor v1 */
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bEndpointAddress;
uint8_t bmAttributes;
uint16_t wMaxPacketSize;
uint8_t bInterval;
uint8_t bRefresh;
uint8_t bSynchAddress;
} __packed audio_iso_endpoint_descriptor;
/* Isochronous Endpoint Descriptor v2 */
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bEndpointAddress;
uint8_t bmAttributes;
uint16_t wMaxPacketSize;
uint8_t bInterval;
} __packed audio_iso_endpoint_descriptor_2;
/* Isochronous Endpoint Audio Class Descriptor v1 */
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubtype;
uint8_t bmAttributes;
uint8_t bLockDelayUnits;
uint16_t wLockDelay;
} __packed audio_iso_ac_endpoint_descriptor;
/* Isochronous Endpoint Audio Class Descriptor v2 */
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubtype;
uint8_t bmAttributes;
uint8_t bmControls;
uint8_t bLockDelayUnits;
uint16_t wLockDelay;
} __packed audio_iso_ac_endpoint_descriptor_2;
/*
* AUDIO interface
*/
uint32 usb_audio_write_tx_data(const uint8* buf, uint32 len);
uint32 usb_audio_read_rx_data(uint8* buf, uint32 len);
uint8 usb_audio_init(uint16 type, uint16 rate);
void usb_audio_setEPSize(uint32_t size);
void audio_set_packet_callback(void (*callback)(uint8));
#ifdef __cplusplus
}
#endif
#endif /* __USB_AUDIO_H */