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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
|
//*****************************************************************************
//
// usbdaudio.c - USB audio device class driver.
//
// Copyright (c) 2009-2012 Texas Instruments Incorporated. All rights reserved.
// Software License Agreement
//
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 9453 of the Stellaris USB Library.
//
//*****************************************************************************
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/usb.h"
#include "driverlib/udma.h"
#include "usblib/usblib.h"
#include "usblib/usbaudio.h"
#include "usblib/device/usbdevice.h"
#include "usblib/device/usbdaudio.h"
//*****************************************************************************
//
//! \addtogroup audio_device_class_api
//! @{
//
//*****************************************************************************
//*****************************************************************************
//
// The following are the USB audio descriptor identifiers.
//
//*****************************************************************************
#define AUDIO_IN_TERMINAL_ID 1
#define AUDIO_OUT_TERMINAL_ID 2
#define AUDIO_CONTROL_ID 3
//*****************************************************************************
//
// The following are the USB interface numbers for this audio device.
//
//*****************************************************************************
#define AUDIO_INTERFACE_CONTROL 0
#define AUDIO_INTERFACE_OUTPUT 1
//*****************************************************************************
//
// Endpoints to use for each of the required endpoints in the driver.
//
//*****************************************************************************
#define ISOC_OUT_ENDPOINT USB_EP_1
#define ISOC_OUT_DMA_CHANNEL UDMA_CHANNEL_USBEP1RX
//*****************************************************************************
//
// Max size is (48000 samples/sec * 4 bytes/sample) * 0.001 seconds/frame.
//
//*****************************************************************************
#define ISOC_OUT_EP_MAX_SIZE ((48000*4)/1000)
//*****************************************************************************
//
// Device Descriptor. This is stored in RAM to allow several fields to be
// changed at runtime based on the client's requirements.
//
//*****************************************************************************
static unsigned char g_pAudioDeviceDescriptor[] =
{
18, // Size of this structure.
USB_DTYPE_DEVICE, // Type of this structure.
USBShort(0x110), // USB version 1.1 (if we say 2.0, hosts assume
// high-speed - see USB 2.0 spec 9.2.6.6)
0, // USB Device Class (spec 5.1.1)
0, // USB Device Sub-class (spec 5.1.1)
0, // USB Device protocol (spec 5.1.1)
64, // Maximum packet size for default pipe.
USBShort(0), // Vendor ID (filled in during USBDAudioInit).
USBShort(0), // Product ID (filled in during USBDAudioInit).
USBShort(0x100), // Device Version BCD.
1, // Manufacturer string identifier.
2, // Product string identifier.
3, // Product serial number.
1 // Number of configurations.
};
//*****************************************************************************
//
// Audio class device configuration descriptor.
//
// It is vital that the configuration descriptor bConfigurationValue field
// (byte 6) is 1 for the first configuration and increments by 1 for each
// additional configuration defined here. This relationship is assumed in the
// device stack for simplicity even though the USB 2.0 specification imposes
// no such restriction on the bConfigurationValue values.
//
// Note that this structure is deliberately located in RAM since we need to
// be able to patch some values in it based on client requirements.
//
//*****************************************************************************
static unsigned char g_pAudioDescriptor[] =
{
//
// Configuration descriptor header.
//
9, // Size of the configuration descriptor.
USB_DTYPE_CONFIGURATION, // Type of this descriptor.
USBShort(32), // The total size of this full structure.
2, // The number of interfaces in this
// configuration.
1, // The unique value for this configuration.
0, // The string identifier that describes this
// configuration.
USB_CONF_ATTR_BUS_PWR, // Bus Powered, Self Powered, remote wake up.
250, // The maximum power in 2mA increments.
};
//*****************************************************************************
//
// This is the Interface Association Descriptor for the serial device used in
// composite devices.
//
//*****************************************************************************
unsigned char g_pIADAudioDescriptor[] =
{
8, // Size of the interface descriptor.
USB_DTYPE_INTERFACE_ASC, // Interface Association Type.
0x0, // Default starting interface is 0.
0x2, // Number of interfaces in this association.
USB_CLASS_AUDIO, // The device class for this association.
USB_SUBCLASS_UNDEFINED, // The device subclass for this association.
USB_PROTOCOL_UNDEFINED, // The protocol for this association.
0 // The string index for this association.
};
const tConfigSection g_sIADAudioConfigSection =
{
sizeof(g_pIADAudioDescriptor),
g_pIADAudioDescriptor
};
//*****************************************************************************
//
// The remainder of the configuration descriptor is stored in flash since we
// don't need to modify anything in it at runtime.
//
//*****************************************************************************
const unsigned char g_pAudioControlInterface[] =
{
//
// Vendor-specific Interface Descriptor.
//
9, // Size of the interface descriptor.
USB_DTYPE_INTERFACE, // Type of this descriptor.
AUDIO_INTERFACE_CONTROL, // The index for this interface.
0, // The alternate setting for this interface.
0, // The number of endpoints used by this
// interface.
USB_CLASS_AUDIO, // The interface class
USB_ASC_AUDIO_CONTROL, // The interface sub-class.
0, // The interface protocol for the sub-class
// specified above.
0, // The string index for this interface.
//
// Audio Header Descriptor.
//
9, // The size of this descriptor.
USB_DTYPE_CS_INTERFACE, // Interface descriptor is class specific.
USB_ACDSTYPE_HEADER, // Descriptor sub-type is HEADER.
USBShort(0x0100), // Audio Device Class Specification Release
// Number in Binary-Coded Decimal.
// Total number of bytes in
// g_pAudioControlInterface
USBShort((9 + 9 + 12 + 13 + 9)),
1, // Number of streaming interfaces.
1, // Index of the first and only streaming
// interface.
//
// Audio Input Terminal Descriptor.
//
12, // The size of this descriptor.
USB_DTYPE_CS_INTERFACE, // Interface descriptor is class specific.
USB_ACDSTYPE_IN_TERMINAL, // Descriptor sub-type is INPUT_TERMINAL.
AUDIO_IN_TERMINAL_ID, // Terminal ID for this interface.
// USB streaming interface.
USBShort(USB_TTYPE_STREAMING),
0, // ID of the Output Terminal to which this
// Input Terminal is associated.
2, // Number of logical output channels in the
// Terminal's output audio channel cluster.
USBShort((USB_CHANNEL_L | // Describes the spatial location of the
USB_CHANNEL_R)), // logical channels.
0, // Channel Name string index.
0, // Terminal Name string index.
//
// Audio Feature Unit Descriptor
//
13, // The size of this descriptor.
USB_DTYPE_CS_INTERFACE, // Interface descriptor is class specific.
USB_ACDSTYPE_FEATURE_UNIT, // Descriptor sub-type is FEATURE_UNIT.
AUDIO_CONTROL_ID, // Unit ID for this interface.
AUDIO_IN_TERMINAL_ID, // ID of the Unit or Terminal to which this
// Feature Unit is connected.
2, // Size in bytes of an element of the
// bmaControls() array that follows.
// Master Mute control.
USBShort(USB_ACONTROL_MUTE),
// Left channel volume control.
USBShort(USB_ACONTROL_VOLUME),
// Right channel volume control.
USBShort(USB_ACONTROL_VOLUME),
0, // Feature unit string index.
//
// Audio Output Terminal Descriptor.
//
9, // The size of this descriptor.
USB_DTYPE_CS_INTERFACE, // Interface descriptor is class specific.
USB_ACDSTYPE_OUT_TERMINAL, // Descriptor sub-type is INPUT_TERMINAL.
AUDIO_OUT_TERMINAL_ID, // Terminal ID for this interface.
// Output type is a generic speaker.
USBShort(USB_ATTYPE_SPEAKER),
AUDIO_IN_TERMINAL_ID, // ID of the input terminal to which this
// output terminal is connected.
AUDIO_CONTROL_ID, // ID of the feature unit that this output
// terminal is connected to.
0, // Output terminal string index.
};
//*****************************************************************************
//
// The audio streaming interface descriptor. This describes the two valid
// interfaces for this class. The first interface has no endpoints and is used
// by host operating systems to put the device in idle mode, while the second
// is used when the audio device is active.
//
//*****************************************************************************
const unsigned char g_pAudioStreamInterface[] =
{
//
// Vendor-specific Interface Descriptor.
//
9, // Size of the interface descriptor.
USB_DTYPE_INTERFACE, // Type of this descriptor.
AUDIO_INTERFACE_OUTPUT, // The index for this interface.
0, // The alternate setting for this interface.
0, // The number of endpoints used by this
// interface.
USB_CLASS_AUDIO, // The interface class
USB_ASC_AUDIO_STREAMING, // The interface sub-class.
0, // Unused must be 0.
0, // The string index for this interface.
//
// Vendor-specific Interface Descriptor.
//
9, // Size of the interface descriptor.
USB_DTYPE_INTERFACE, // Type of this descriptor.
1, // The index for this interface.
1, // The alternate setting for this interface.
1, // The number of endpoints used by this
// interface.
USB_CLASS_AUDIO, // The interface class
USB_ASC_AUDIO_STREAMING, // The interface sub-class.
0, // Unused must be 0.
0, // The string index for this interface.
//
// Class specific Audio Streaming Interface descriptor.
//
7, // Size of the interface descriptor.
USB_DTYPE_CS_INTERFACE, // Interface descriptor is class specific.
USB_ASDSTYPE_GENERAL, // General information.
AUDIO_IN_TERMINAL_ID, // ID of the terminal to which this streaming
// interface is connected.
1, // One frame delay.
USBShort(USB_ADF_PCM), //
//
// Format type Audio Streaming descriptor.
//
11, // Size of the interface descriptor.
USB_DTYPE_CS_INTERFACE, // Interface descriptor is class specific.
USB_ASDSTYPE_FORMAT_TYPE, // Audio Streaming format type.
USB_AF_TYPE_TYPE_I, // Type I audio format type.
2, // Two audio channels.
2, // Two bytes per audio sub-frame.
16, // 16 bits per sample.
1, // One sample rate provided.
USB3Byte(48000), // Only 48000 sample rate supported.
//
// Endpoint Descriptor
//
9, // The size of the endpoint descriptor.
USB_DTYPE_ENDPOINT, // Descriptor type is an endpoint.
// OUT endpoint with address
// ISOC_OUT_ENDPOINT.
USB_EP_DESC_OUT | USB_EP_TO_INDEX(ISOC_OUT_ENDPOINT),
USB_EP_ATTR_ISOC | // Endpoint is an adaptive isochronous data
USB_EP_ATTR_ISOC_ADAPT | // endpoint.
USB_EP_ATTR_USAGE_DATA,
USBShort(ISOC_OUT_EP_MAX_SIZE), // The maximum packet size.
1, // The polling interval for this endpoint.
0, // Refresh is unused.
0, // Synch endpoint address.
//
// Audio Streaming Isochronous Audio Data Endpoint Descriptor
//
7, // The size of the descriptor.
USB_ACSDT_ENDPOINT, // Audio Class Specific Endpoint Descriptor.
USB_ASDSTYPE_GENERAL, // This is a general descriptor.
USB_EP_ATTR_ACG_SAMPLING, // Sampling frequency is supported.
USB_EP_LOCKDELAY_UNDEF, // Undefined lock delay units.
USBShort(0), // No lock delay.
};
//*****************************************************************************
//
// The audio device configuration descriptor is defined as three sections,
// one containing just the 9 byte USB configuration descriptor. The second
// holds the audio streaming interface and the third holds the audio control
// interface.
//
//*****************************************************************************
const tConfigSection g_sAudioConfigSection =
{
sizeof(g_pAudioDescriptor),
g_pAudioDescriptor
};
const tConfigSection g_sAudioStreamInterfaceSection =
{
sizeof(g_pAudioStreamInterface),
g_pAudioStreamInterface
};
const tConfigSection g_sAudioControlInterfaceSection =
{
sizeof(g_pAudioControlInterface),
g_pAudioControlInterface
};
//*****************************************************************************
//
// This array lists all the sections that must be concatenated to make a
// single, complete audio device configuration descriptor.
//
//*****************************************************************************
const tConfigSection *g_psAudioSections[] =
{
&g_sAudioConfigSection,
&g_sIADAudioConfigSection,
&g_sAudioControlInterfaceSection,
&g_sAudioStreamInterfaceSection
};
#define NUM_AUDIO_SECTIONS (sizeof(g_psAudioSections) / \
sizeof(tConfigSection *))
//*****************************************************************************
//
// The header for the single configuration we support. This is the root of
// the data structure that defines all the bits and pieces that are pulled
// together to generate the configuration descriptor.
//
//*****************************************************************************
const tConfigHeader g_sAudioConfigHeader =
{
NUM_AUDIO_SECTIONS,
g_psAudioSections
};
//*****************************************************************************
//
// Configuration Descriptor.
//
//*****************************************************************************
const tConfigHeader * const g_pAudioConfigDescriptors[] =
{
&g_sAudioConfigHeader
};
//*****************************************************************************
//
// Various internal handlers needed by this class.
//
//*****************************************************************************
static void HandleDisconnect(void *pvInstance);
static void InterfaceChange(void *pvInstance, unsigned char ucInterface,
unsigned char ucAlternateSetting);
static void ConfigChangeHandler(void *pvInstance, unsigned long ulValue);
static void DataReceived(void *pvInstance, unsigned long ulInfo);
static void HandleEndpoints(void *pvInstance, unsigned long ulStatus);
static void HandleRequests(void *pvInstance, tUSBRequest *pUSBRequest);
static void HandleDevice(void *pvInstance, unsigned long ulRequest,
void *pvRequestData);
//*****************************************************************************
//
// The FIFO configuration for USB audio device class.
//
//*****************************************************************************
const tFIFOConfig g_sUSBAudioFIFOConfig =
{
//
// IN endpoints.
//
{
{ false, USB_EP_DEV_IN },
{ false, USB_EP_DEV_IN },
{ false, USB_EP_DEV_IN },
{ false, USB_EP_DEV_IN },
{ false, USB_EP_DEV_IN },
{ false, USB_EP_DEV_IN },
{ false, USB_EP_DEV_IN },
{ false, USB_EP_DEV_IN },
{ false, USB_EP_DEV_IN },
{ false, USB_EP_DEV_IN },
{ false, USB_EP_DEV_IN },
{ false, USB_EP_DEV_IN },
{ false, USB_EP_DEV_IN },
{ false, USB_EP_DEV_IN },
{ false, USB_EP_DEV_IN }
},
//
// OUT endpoints.
//
{
{ false, USB_EP_DEV_OUT | USB_EP_DMA_MODE_1 | USB_EP_AUTO_CLEAR },
{ false, USB_EP_DEV_OUT },
{ false, USB_EP_DEV_OUT },
{ false, USB_EP_DEV_OUT },
{ false, USB_EP_DEV_OUT },
{ false, USB_EP_DEV_OUT },
{ false, USB_EP_DEV_OUT },
{ false, USB_EP_DEV_OUT },
{ false, USB_EP_DEV_OUT },
{ false, USB_EP_DEV_OUT },
{ false, USB_EP_DEV_OUT },
{ false, USB_EP_DEV_OUT },
{ false, USB_EP_DEV_OUT },
{ false, USB_EP_DEV_OUT },
{ false, USB_EP_DEV_OUT }
},
};
//*****************************************************************************
//
// The device information structure for the USB Audio device.
//
//*****************************************************************************
tDeviceInfo g_sAudioDeviceInfo =
{
//
// Device event handler callbacks.
//
{
//
// GetDescriptor
//
0,
//
// RequestHandler
//
HandleRequests,
//
// InterfaceChange
//
InterfaceChange,
//
// ConfigChange
//
ConfigChangeHandler,
//
// DataReceived
//
DataReceived,
//
// DataSentCallback
//
0,
//
// ResetHandler
//
0,
//
// SuspendHandler
//
0,
//
// ResumeHandler
//
0,
//
// DisconnectHandler
//
HandleDisconnect,
//
// EndpointHandler
//
HandleEndpoints,
//
// Device handler
//
HandleDevice
},
g_pAudioDeviceDescriptor,
g_pAudioConfigDescriptors,
0,
0,
&g_sUSBAudioFIFOConfig
};
//*****************************************************************************
//
// This function is called to handle data being received back from the host so
// that the application callback can be called when the new data is ready.
//
//*****************************************************************************
static void
DataReceived(void *pvInstance, unsigned long ulInfo)
{
tAudioInstance *psInst;
const tUSBDAudioDevice *psDevice;
ASSERT(pvInstance != 0);
//
// Create the instance pointer.
//
psDevice = (const tUSBDAudioDevice *)pvInstance;
//
// Make a copy of this pointer for ease of use in this function.
//
psInst = psDevice->psPrivateData;
//
// If there is an update pending and the request was to set a current
// value then check which value was set.
//
if(psInst->usUpdate && (psInst->ucRequest == USB_AC_SET_CUR))
{
//
// Only handling interface requests.
//
if((psInst->usRequestType & USB_RTYPE_RECIPIENT_M) ==
USB_RTYPE_INTERFACE)
{
if(psInst->usUpdate == VOLUME_CONTROL)
{
//
// Inform the callback of the new volume.
//
psDevice->pfnCallback(0, USBD_AUDIO_EVENT_VOLUME,
psInst->sVolume, 0);
}
else if(psDevice->psPrivateData->usUpdate == MUTE_CONTROL)
{
//
// Inform the callback of the new data.
//
psDevice->pfnCallback(0, USBD_AUDIO_EVENT_MUTE, psInst->ucMute,
0);
}
}
psInst->usUpdate = 0;
}
}
//*****************************************************************************
//
// This function is called to handle the interrupts on the isochronous endpoint
// for the audio device class.
//
//*****************************************************************************
static void
HandleEndpoints(void *pvInstance, unsigned long ulStatus)
{
unsigned long ulEPStatus;
tAudioInstance *psInst;
unsigned char *pucData;
const tUSBDAudioDevice *psDevice;
ASSERT(pvInstance != 0);
//
// Create the instance pointer.
//
psDevice = (const tUSBDAudioDevice *)pvInstance;
//
// Make a copy of this pointer for ease of use later in this function.
//
psInst = psDevice->psPrivateData;
//
// Make sure this was for the isochronous out endpoint.
//
if((psInst->sBuffer.pvData != 0) &&
(MAP_uDMAChannelModeGet(psInst->ucOUTDMA) == UDMA_MODE_STOP))
{
//
// Save the pointer to the data buffer.
//
pucData = psInst->sBuffer.pvData;
//
// Clear out the buffer pointer to indicate it is no longer in use.
//
psInst->sBuffer.pvData = 0;
//
// Inform the callback of the new data.
//
psInst->sBuffer.pfnCallback(pucData, psInst->sBuffer.ulSize,
USBD_AUDIO_EVENT_DATAOUT);
//
// Read out the current endpoint status.
//
ulEPStatus = MAP_USBEndpointStatus(USB0_BASE, psInst->ucOUTEndpoint);
//
// Acknowledge that the data was read, this will not cause a bus
// acknowledgment.
//
MAP_USBDevEndpointDataAck(USB0_BASE, psInst->ucOUTEndpoint, 0);
//
// Clear the status bits.
//
MAP_USBDevEndpointStatusClear(USB0_BASE, psInst->ucOUTEndpoint,
ulEPStatus);
}
}
//*****************************************************************************
//
// Device instance specific handler.
//
//*****************************************************************************
static void
HandleDevice(void *pvInstance, unsigned long ulRequest, void *pvRequestData)
{
tAudioInstance *psInst;
unsigned char *pucData;
//
// Create the serial instance data.
//
psInst = ((tUSBDAudioDevice *)pvInstance)->psPrivateData;
//
// Create the char array used by the events supported by the USB CDC
// serial class.
//
pucData = (unsigned char *)pvRequestData;
switch(ulRequest)
{
//
// This was an interface change event.
//
case USB_EVENT_COMP_IFACE_CHANGE:
{
//
// Save the change to the appropriate interface number.
//
if(pucData[0] == AUDIO_INTERFACE_CONTROL)
{
psInst->ucInterfaceControl = pucData[1];
}
else if(pucData[0] == AUDIO_INTERFACE_OUTPUT)
{
psInst->ucInterfaceAudio = pucData[1];
}
break;
}
//
// This was an endpoint change event.
//
case USB_EVENT_COMP_EP_CHANGE:
{
//
// Determine if this is an IN or OUT endpoint that has changed.
//
if((pucData[0] & USB_EP_DESC_IN) == 0)
{
//
// Extract the new endpoint number without the DIR bit.
//
psInst->ucOUTEndpoint = INDEX_TO_USB_EP(pucData[1] & 0x7f);
//
// Extract the new DMA channel.
//
psInst->ucOUTDMA = UDMA_CHANNEL_USBEP1RX +
(((pucData[1] & 0x7f) - 1) * 2);
//
// Basic configuration for DMA on the OUT endpoint.
//
MAP_uDMAChannelControlSet(psInst->ucOUTDMA,
(UDMA_SIZE_32 | UDMA_SRC_INC_NONE|
UDMA_DST_INC_32 | UDMA_ARB_16));
//
// Select this channel for this endpoint, this only affects
// devices that have this feature.
//
MAP_USBEndpointDMAChannel(USB0_BASE, psInst->ucOUTEndpoint,
psInst->ucOUTDMA);
}
break;
}
//
// Handle class specific reconfiguring of the configuration descriptor
// once the composite class has built the full descriptor.
//
case USB_EVENT_COMP_CONFIG:
{
//
// This sets the bFirstInterface of the Interface Association
// descriptor to the first interface which is the control
// interface used by this instance.
//
pucData[2] = psInst->ucInterfaceControl;
break;
}
default:
{
break;
}
}
}
//*****************************************************************************
//
// This function is called by the USB device stack whenever the device is
// disconnected from the host.
//
//*****************************************************************************
static void
HandleDisconnect(void *pvInstance)
{
const tUSBDAudioDevice *psDevice;
ASSERT(pvInstance != 0);
//
// Create the instance pointer.
//
psDevice = (const tUSBDAudioDevice *)pvInstance;
//
// Inform the application that the device has been disconnected.
//
psDevice->pfnCallback(0, USB_EVENT_DISCONNECTED, 0, 0);
}
//*****************************************************************************
//
// This function is called by the USB device stack whenever the device
// interface changes. This occurs when the audio device transitions between
// being active and inactive. Interface AUDIO_INTERFACE_CONTROL is the
// inactive interface that has no endpoints, while interface
// AUDIO_INTERFACE_AUDIO has the single Isochronous OUT endpoint.
//
//*****************************************************************************
static void
InterfaceChange(void *pvInstance, unsigned char ucInterface,
unsigned char ucAlternateSetting)
{
const tUSBDAudioDevice *psDevice;
ASSERT(pvInstance != 0);
//
// Create the instance pointer.
//
psDevice = (const tUSBDAudioDevice *)pvInstance;
//
// Check which interface to change into.
//
if(ucAlternateSetting == 0)
{
//
// Alternate setting 0 is an inactive state.
//
if(psDevice->pfnCallback)
{
psDevice->pfnCallback(0, USBD_AUDIO_EVENT_IDLE, 0, 0);
}
}
else
{
//
// Alternate setting 1 is the active state.
//
if(psDevice->pfnCallback)
{
psDevice->pfnCallback(0, USBD_AUDIO_EVENT_ACTIVE, 0, 0);
}
//
// Enable uDMA on the endpoint now that the active configuration
// has been selected.
//
MAP_USBEndpointDMAEnable(USB0_BASE,
psDevice->psPrivateData->ucOUTEndpoint,
USB_EP_DEV_OUT);
}
}
//*****************************************************************************
//
// This function is called by the USB device stack whenever the device
// configuration changes.
//
//*****************************************************************************
static void
ConfigChangeHandler(void *pvInstance, unsigned long ulValue)
{
const tUSBDAudioDevice *psDevice;
ASSERT(pvInstance != 0);
//
// Create the instance pointer.
//
psDevice = (const tUSBDAudioDevice *)pvInstance;
//
// If we have a control callback, let the client know we are open for
// business.
//
if(psDevice->pfnCallback)
{
//
// Pass the connected event to the client.
//
psDevice->pfnCallback(pvInstance, USB_EVENT_CONNECTED, 0, 0);
}
}
//*****************************************************************************
//
//! This function should be called once for the audio class device to
//! initialized basic operation and prepare for enumeration.
//!
//! \param ulIndex is the index of the USB controller to initialize for
//! audio class device operation.
//! \param psDevice points to a structure containing parameters customizing
//! the operation of the audio device.
//!
//! In order for an application to initialize the USB audio device class, it
//! must first call this function with the a valid audio device class structure
//! in the \e psDevice parameter. This allows this function to initialize the
//! USB controller and device code to be prepared to enumerate and function as
//! a USB audio device.
//!
//! This function returns a void pointer that must be passed in to all other
//! APIs used by the audio class.
//!
//! See the documentation on the tUSBDAudioDevice structure for more
//! information on how to properly fill the structure members.
//!
//! \return Returns 0 on failure or a non-zero void pointer on success.
//
//*****************************************************************************
void *
USBDAudioInit(unsigned long ulIndex, const tUSBDAudioDevice *psDevice)
{
//
// Check parameter validity.
//
ASSERT(ulIndex == 0);
ASSERT(psDevice);
ASSERT(psDevice->ppStringDescriptors);
ASSERT(psDevice->psPrivateData);
USBDAudioCompositeInit(ulIndex, psDevice);
//
// All is well so now pass the descriptors to the lower layer and put
// the bulk device on the bus.
//
USBDCDInit(ulIndex, psDevice->psPrivateData->psDevInfo);
//
// Basic configuration for DMA on the OUT endpoint.
//
MAP_uDMAChannelControlSet(psDevice->psPrivateData->ucOUTDMA,
(UDMA_SIZE_32 | UDMA_SRC_INC_NONE|
UDMA_DST_INC_32 | UDMA_ARB_16));
//
// Select this channel for this endpoint, this only affects devices that
// have this feature.
//
MAP_USBEndpointDMAChannel(USB0_BASE, psDevice->psPrivateData->ucOUTEndpoint,
psDevice->psPrivateData->ucOUTDMA);
//
// Return the pointer to the instance indicating that everything went well.
//
return((void *)psDevice);
}
//*****************************************************************************
//
//! This function should be called once for the audio class device to
//! initialized basic operation and prepare for enumeration.
//!
//! \param ulIndex is the index of the USB controller to initialize for
//! audio class device operation.
//! \param psDevice points to a structure containing parameters customizing
//! the operation of the audio device.
//!
//! In order for an application to initialize the USB audio device class, it
//! must first call this function with the a valid audio device class structure
//! in the \e psDevice parameter. This allows this function to initialize the
//! USB controller and device code to be prepared to enumerate and function as
//! a USB audio device.
//!
//! This function returns a void pointer that must be passed in to all other
//! APIs used by the audio class.
//!
//! See the documentation on the tUSBDAudioDevice structure for more
//! information on how to properly fill the structure members.
//!
//! \return Returns 0 on failure or a non-zero void pointer on success.
//
//*****************************************************************************
void *
USBDAudioCompositeInit(unsigned long ulIndex, const tUSBDAudioDevice *psDevice)
{
tAudioInstance *psInst;
tDeviceDescriptor *psDevDesc;
//
// Check parameter validity.
//
ASSERT(ulIndex == 0);
ASSERT(psDevice);
ASSERT(psDevice->ppStringDescriptors);
ASSERT(psDevice->psPrivateData);
//
// Initialize the workspace in the passed instance structure.
//
psInst = psDevice->psPrivateData;
psInst->psConfDescriptor = (tConfigDescriptor *)g_pAudioDescriptor;
psInst->psDevInfo = &g_sAudioDeviceInfo;
psInst->ulUSBBase = USB0_BASE;
//
// The Control interface is at index 0.
//
psInst->ucInterfaceControl = AUDIO_INTERFACE_CONTROL;
//
// The Audio interface is at index 1.
//
psInst->ucInterfaceAudio = AUDIO_INTERFACE_OUTPUT;
//
// Set the default Isochronous OUT endpoint.
//
psInst->ucOUTEndpoint = ISOC_OUT_ENDPOINT;
psInst->ucOUTDMA = ISOC_OUT_DMA_CHANNEL;
//
// Set the initial buffer to null.
//
psInst->sBuffer.pvData = 0;
//
// Save the volume settings.
//
psInst->sVolumeMax = psDevice->sVolumeMax;
psInst->sVolumeMin = psDevice->sVolumeMin;
psInst->sVolumeStep = psDevice->sVolumeStep;
//
// No update pending to any command.
//
psInst->usUpdate = 0;
//
// Fix up the device descriptor with the client-supplied values.
//
psDevDesc = (tDeviceDescriptor *)psInst->psDevInfo->pDeviceDescriptor;
psDevDesc->idVendor = psDevice->usVID;
psDevDesc->idProduct = psDevice->usPID;
//
// Fix up the configuration descriptor with client-supplied values.
//
psInst->psConfDescriptor->bmAttributes = psDevice->ucPwrAttributes;
psInst->psConfDescriptor->bMaxPower =
(unsigned char)(psDevice->usMaxPowermA / 2);
//
// Plug in the client's string stable to the device information
// structure.
//
psInst->psDevInfo->ppStringDescriptors = psDevice->ppStringDescriptors;
psInst->psDevInfo->ulNumStringDescriptors =
psDevice->ulNumStringDescriptors;
psInst->psDevInfo->pvInstance = (void *)psDevice;
//
// Return the pointer to the instance indicating that everything went well.
//
return((void *)psDevice);
}
//*****************************************************************************
//
//! Shuts down the audio device.
//!
//! \param pvInstance is the pointer to the device instance structure as
//! returned by USBDAudioInit().
//!
//! This function terminates audio interface for the instance supplied. This
//! function should not be called if the audio device is part of a composite
//! device and instead the USBDCompositeTerm() function should be called for
//! the full composite device.
//! Following this call, the \e pvInstance instance should not me used in any
//! other calls.
//!
//! \return None.
//
//*****************************************************************************
void
USBDAudioTerm(void *pvInstance)
{
ASSERT(pvInstance != 0);
//
// Cleanly exit device mode.
//
USBDCDTerm(0);
}
//*****************************************************************************
//
// This function is called by the USB device stack whenever a non-standard
// request is received.
//
// \param pvInstance is the instance data for this request.
// \param pUSBRequest points to the request received.
//
// This call parses the provided request structure to the type of request and
// will respond to all commands that are understood by the class.
//
// \return None.
//
//*****************************************************************************
static void
HandleRequests(void *pvInstance, tUSBRequest *pUSBRequest)
{
unsigned long ulControl;
unsigned long ulRecipient;
unsigned long ulStall;
tAudioInstance *psInst;
const tUSBDAudioDevice *psDevice;
ASSERT(pvInstance != 0);
//
// Create the instance pointer.
//
psDevice = (const tUSBDAudioDevice *)pvInstance;
//
// Make a copy of this pointer for ease of use in this function.
//
psInst = psDevice->psPrivateData;
//
// Make sure to acknowledge that the data was read, this will not send and
// ACK that has already been done at this point. This just tells the
// hardware that the data was read.
//
MAP_USBDevEndpointDataAck(USB0_BASE, USB_EP_0, false);
//
// Don't stall by default.
//
ulStall = 0;
//
// Get the request type.
//
ulRecipient = pUSBRequest->bmRequestType & USB_RTYPE_RECIPIENT_M;
//
// Save the request type and request value.
//
psInst->usRequestType = pUSBRequest->bmRequestType;
psInst->ucRequest = pUSBRequest->bRequest;
//
// Check if this is an endpoint request to the audio streaming endpoint.
//
if((ulRecipient == USB_RTYPE_ENDPOINT) &&
(pUSBRequest->wIndex == USB_EP_TO_INDEX(psInst->ucOUTEndpoint)))
{
//
// Determine the type of request.
//
switch(psInst->ucRequest)
{
case USB_AC_SET_CUR:
{
//
// Handle retrieving the sample rate.
//
if(pUSBRequest->wValue == SAMPLING_FREQ_CONTROL)
{
//
// Retrieve the requested sample rate.
//
USBDCDRequestDataEP0(0,
(unsigned char *)&psInst->ulSampleRate,
3);
//
// Save what we are updating.
//
psInst->usUpdate = SAMPLING_FREQ_CONTROL;
}
break;
}
case USB_AC_GET_CUR:
{
//
// Handle retrieving the sample rate.
//
if(pUSBRequest->wValue == SAMPLING_FREQ_CONTROL)
{
//
// Send back the current sample rate.
//
USBDCDSendDataEP0(0,
(unsigned char *)&psInst->ulSampleRate,
3);
}
break;
}
default:
{
//
// Stall on unknown commands.
//
ulStall = 1;
break;
}
}
}
else if(ulRecipient == USB_RTYPE_INTERFACE)
{
//
// Make sure the request was for the control interface.
//
if((unsigned char)pUSBRequest->wIndex != psInst->ucInterfaceControl)
{
return;
}
//
// Extract the control value from the message.
//
ulControl = pUSBRequest->wValue & USB_CS_CONTROL_M;
//
// Handle an audio control request to the feature control unit.
//
if((AUDIO_CONTROL_ID << 8) == (pUSBRequest->wIndex & USB_CS_CONTROL_M))
{
//
// Determine the type of request.
//
switch(psInst->ucRequest)
{
case USB_AC_GET_MAX:
{
if(ulControl == VOLUME_CONTROL)
{
//
// Return the maximum volume setting.
//
USBDCDSendDataEP0(0,
(unsigned char *)&psInst->sVolumeMax,
2);
}
else
{
//
// Stall on unknown commands.
//
ulStall = 1;
}
break;
}
case USB_AC_GET_MIN:
{
if(ulControl == VOLUME_CONTROL)
{
//
// Return the minimum volume setting.
//
USBDCDSendDataEP0(0,
(unsigned char *)&psInst->sVolumeMin,
2);
}
else
{
//
// Stall on unknown commands.
//
ulStall = 1;
}
break;
}
case USB_AC_GET_RES:
{
if(ulControl == VOLUME_CONTROL)
{
//
// Return the volume step setting.
//
USBDCDSendDataEP0(0,
(unsigned char *)&psInst->sVolumeStep,
2);
}
else
{
//
// Stall on unknown commands.
//
ulStall = 1;
}
break;
}
case USB_AC_GET_CUR:
{
if(ulControl == VOLUME_CONTROL)
{
//
// Send back the current volume level.
//
USBDCDSendDataEP0(0,
(unsigned char *)&psInst->sVolume,
2);
}
else if(ulControl == MUTE_CONTROL)
{
//
// Send back the current mute value.
//
USBDCDSendDataEP0(0,
(unsigned char *)&psInst->ucMute, 1);
}
else
{
//
// Stall on unknown commands.
//
ulStall = 1;
}
break;
}
case USB_AC_SET_CUR:
{
if(ulControl == VOLUME_CONTROL)
{
//
// Read the new volume level.
//
USBDCDRequestDataEP0(0,
(unsigned char *)&psInst->sVolume,
2);
//
// Save what we are updating.
//
psInst->usUpdate = VOLUME_CONTROL;
}
else if(ulControl == MUTE_CONTROL)
{
//
// Read the new mute setting.
//
USBDCDRequestDataEP0(0,
(unsigned char *)&psInst->ucMute,
1);
//
// Save what we are updating.
//
psInst->usUpdate = MUTE_CONTROL;
}
else
{
//
// Stall on unknown commands.
//
ulStall = 1;
}
break;
}
case USB_AC_SET_RES:
{
if(ulControl == VOLUME_CONTROL)
{
//
// Read the new volume step setting.
//
USBDCDRequestDataEP0(0,
(unsigned char *)&psInst->sVolumeStep, 2);
//
// Save what we are updating.
//
psInst->usUpdate = VOLUME_CONTROL;
}
else
{
//
// Stall on unknown commands.
//
ulStall = 1;
}
break;
}
default:
{
//
// Stall on unknown commands.
//
ulStall = 1;
break;
}
}
}
}
//
// Stall on all unknown commands.
//
if(ulStall)
{
USBDCDStallEP0(0);
}
}
//*****************************************************************************
//
//! This function is used to supply buffers to the audio class to be filled
//! from the USB host device.
//!
//! \param pvInstance is the pointer to the device instance structure as
//! returned by USBDAudioInit() or USBDAudioInitComposite().
//! \param pvBuffer is a pointer to the buffer to fill with audio data.
//! \param ulSize is the size in bytes of the buffer pointed to by the pvBuffer
//! parameter.
//! \param pfnCallback is a callback that will provide notification when this
//! buffer has valid data.
//!
//! This function fills the buffer pointed to by the \e pvBuffer parameter with
//! at most \e ulSize one packet of data from the host controller. The ulSize
//! has a minimum value of \b ISOC_OUT_EP_MAX_SIZE since each USB packet can be
//! at most \b ISOC_OUT_EP_MAX_SIZE bytes in size. Since the audio data may
//! not be received in amounts that evenly fit in the buffer provided, the
//! buffer may not be completely filled. The \e pfnCallback function will
//! provide the amount of valid data that was actually stored in the buffer
//! provided. The function will return zero if the buffer could be scheduled
//! to be filled, otherwise the function will return a non-zero value if there
//! was some reason that the buffer could not be added.
//!
//! \return Returns 0 to indicate success any other value indicates that the
//! buffer will not be filled.
//
//*****************************************************************************
long
USBAudioBufferOut(void *pvInstance, void *pvBuffer, unsigned long ulSize,
tUSBAudioBufferCallback pfnCallback)
{
tAudioInstance *psInst;
const tUSBDAudioDevice *psDevice;
//
// Make sure we were not passed NULL pointers.
//
ASSERT(pvInstance != 0);
ASSERT(pvBuffer != 0);
//
// Create the instance pointer.
//
psDevice = (const tUSBDAudioDevice *)pvInstance;
//
// Buffer must be at least one packet in size.
//
ASSERT(ulSize >= ISOC_OUT_EP_MAX_SIZE);
ASSERT(pfnCallback);
//
// Create a pointer of the correct type from the private pointer.
//
psInst = psDevice->psPrivateData;
//
// Initialize the buffer instance.
//
psInst->sBuffer.pvData = pvBuffer;
psInst->sBuffer.ulSize = ulSize;
psInst->sBuffer.ulNumBytes = 0;
psInst->sBuffer.pfnCallback = pfnCallback;
//
// Configure and enable DMA for the OUT transfer.
//
MAP_uDMAChannelTransferSet(psInst->ucOUTDMA, UDMA_MODE_BASIC,
(void *)USBFIFOAddrGet(USB0_BASE,
psInst->ucOUTEndpoint),
psInst->sBuffer.pvData, ulSize >> 2);
//
// Start the DMA transfer.
//
MAP_uDMAChannelEnable(psInst->ucOUTDMA);
return(0);
}
//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************
|