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
|
//*****************************************************************************
//
// usb_sound.c - USB host audio handling functions.
//
// Copyright (c) 2013-2014 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 2.1.0.12573 of the DK-TM4C129X Firmware Package.
//
//*****************************************************************************
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "drivers/usb_sound.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"
#include "driverlib/udma.h"
#include "driverlib/usb.h"
#include "usblib/usblib.h"
#include "usblib/usbmsc.h"
#include "usblib/host/usbhost.h"
#include "usblib/host/usbhaudio.h"
//*****************************************************************************
//
// The size of the host controller's memory pool in bytes.
//
//*****************************************************************************
#define HCD_MEMORY_SIZE 768
//*****************************************************************************
//
// The memory pool to provide to the Host controller driver.
//
//*****************************************************************************
uint8_t g_pHCDPool[HCD_MEMORY_SIZE];
//*****************************************************************************
//
// The instance data for the USB host audio driver.
//
//*****************************************************************************
tUSBHostAudioInstance *g_psAudioInstance;
//*****************************************************************************
//
// Declare the USB Events driver interface.
//
//*****************************************************************************
DECLARE_EVENT_DRIVER(g_sUSBEventDriver, 0, 0, USBHCDEvents);
//*****************************************************************************
//
// This structure holds the state information for the USB audio device.
//
//*****************************************************************************
static struct
{
//
// Save the application provided callback function.
//
tUSBBufferCallback pfnCallbackOut;
//
// Save the application provided callback function.
//
tUSBBufferCallback pfnCallbackIn;
//
// The event callback for the application.
//
tEventCallback pfnCallbackEvent;
//
// Volume control multipliers calculated from the information received
// from the audio device.
//
uint32_t pui32Steps[3];
//
// The currently pending audio device events.
//
uint32_t ui32EventFlags;
//
// The current state for the audio device.
//
volatile enum
{
//
// No device is present.
//
eStateNoDevice,
//
// Audio device is ready.
//
eStateDeviceReady,
//
// An unsupported device has been attached.
//
eStateUnknownDevice,
//
// A power fault has occurred.
//
eStatePowerFault
}
iState;
} g_sAudioState;
//*****************************************************************************
//
// These defines are used with the ui32EventFlags in the g_sAudioState
// structure.
//
//*****************************************************************************
#define EVENT_OPEN 0x00000001
#define EVENT_CLOSE 0x00000002
//*****************************************************************************
//
// The global that holds all of the host drivers in use in the application.
// In this case, only the host audio class is loaded.
//
//*****************************************************************************
static tUSBHostClassDriver const * const g_ppHostClassDrivers[] =
{
&g_sUSBHostAudioClassDriver,
&g_sUSBEventDriver
};
//*****************************************************************************
//
// This global holds the number of class drivers in the g_ppHostClassDrivers
// list.
//
//*****************************************************************************
static const uint32_t g_ui32NumHostClassDrivers =
sizeof(g_ppHostClassDrivers) / sizeof(tUSBHostClassDriver *);
//*****************************************************************************
//
// This function was the callback function registered with the USB host audio
// class driver. The only two events that are handled at this point are the
// USBH_AUDIO_EVENT_OPEN and USBH_AUDIO_EVENT_CLOSE which indicate that a new
// audio device has been found or that an existing audio device has been
// disconnected.
//
//*****************************************************************************
static void
AudioCallback(tUSBHostAudioInstance *psAudioInstance, uint32_t ui32Event,
uint32_t ui32MsgParam, void *pvBuffer)
{
switch(ui32Event)
{
//
// New USB audio device has been enabled.
//
case USBH_AUDIO_EVENT_OPEN:
{
//
// Set the EVENT_OPEN flag and let the main routine handle it.
//
HWREGBITW(&g_sAudioState.ui32EventFlags, EVENT_OPEN) = 1;
break;
}
//
// USB audio device has been removed.
//
case USBH_AUDIO_EVENT_CLOSE:
{
//
// Set the EVENT_CLOSE flag and let the main routine handle it.
//
HWREGBITW(&g_sAudioState.ui32EventFlags, EVENT_CLOSE) = 1;
break;
}
default:
{
break;
}
}
}
//*****************************************************************************
//
// Initializes the sound output.
//
// \param ui32Flags is unused as this point but is included for future
// functionality.
// \param pfnCallback is the event callback function for audio devices.
//
// This function prepares the sound driver to enumerate an audio device and
// prepares to play audio once a valid audio device is detected. The
// \e pfnCallback function can be used to receive callbacks when there are
// changes related to the audio device. The ui32Event parameter to the
// callback is one of the SOUND_EVENT_* values.
//
// \return None
//
//*****************************************************************************
void
USBSoundInit(uint32_t ui32Flags, tEventCallback pfnCallback)
{
//
// Initialize the USB stack mode to force host mode.
//
USBStackModeSet(0, eUSBModeForceHost, 0);
//
// Register the host class drivers.
//
USBHCDRegisterDrivers(0, g_ppHostClassDrivers, g_ui32NumHostClassDrivers);
//
// Open an instance of the audio class driver.
//
g_psAudioInstance = USBHostAudioOpen(0, AudioCallback);
//
// Initialize the power configuration. This sets the power enable signal
// to be active high and does not enable the power fault.
//
USBHCDPowerConfigInit(0, USBHCD_VBUS_AUTO_HIGH | USBHCD_VBUS_FILTER);
//
// Initialize the USB controller for host mode operation.
//
USBHCDInit(0, g_pHCDPool, HCD_MEMORY_SIZE);
//
// Save the event callback function.
//
g_sAudioState.pfnCallbackEvent = pfnCallback;
}
//*****************************************************************************
//
// Sets the volume of the audio device.
//
// \param ui32Percent is the volume percentage, which must be between 0%
// (silence) and 100% (full volume), inclusive.
//
// This function sets the volume of the sound output to a value between
// silence (0%) and full volume (100%).
//
// \return None.
//
//*****************************************************************************
void
USBSoundVolumeSet(uint32_t ui32Percent)
{
uint32_t ui32Value;
//
// Ignore volume changes if there is no device present.
//
if(g_sAudioState.iState == eStateDeviceReady)
{
//
// Scale the voltage percentage to the decibel range provided by the
// USB audio device.
//
ui32Value = (g_sAudioState.pui32Steps[1] * ui32Percent) / 100;
USBHostAudioVolumeSet(g_psAudioInstance, 0, 1, ui32Value);
ui32Value = (g_sAudioState.pui32Steps[2] * ui32Percent) / 100;
USBHostAudioVolumeSet(g_psAudioInstance, 0, 2, ui32Value);
}
}
//*****************************************************************************
//
// Returns the current volume level.
//
// \param ui32Channel is the 0 based channel number to query.
//
// This function returns the current volume, specified as a percentage between
// 0% (silence) and 100% (full volume), inclusive. The \e ui32Channel value
// starts with 0 which is the master audio volume control interface. The
// remaining \e ui32Channel values provide access to various other audio
// channels, with 1 and 2 being left and right audio channels.
//
// \return Returns the current volume.
//
//*****************************************************************************
uint32_t
USBSoundVolumeGet(uint32_t ui32Channel)
{
uint32_t ui32Volume;
//
// Initialize the return value in case there is no USB device present.
//
ui32Volume = 0xffffffff;
//
// Ignore volume request if there is no device present.
//
if(g_sAudioState.iState == eStateDeviceReady)
{
ui32Volume = USBHostAudioVolumeGet(g_psAudioInstance, 0, ui32Channel);
}
return(ui32Volume);
}
//*****************************************************************************
//
// This sets the current output audio format of the USB audio device.
//
// \param ui32SampleRate is the sample rate.
// \param ui32BitsPerSample is the number of bits per sample.
// \param ui32Channels is the number of channels.
//
// This sets the current audio format for the USB device that is currently
// connected. If there is no USB device connected or the format is not
// supported then the function returns a non-zero value. The function
// returns zero if the USB audio device was successfully configured to the
// requested audio format.
//
// \return Returns zero if the format was successfully set or returns an
// non-zero value if the format was not able to be set.
//
//*****************************************************************************
uint32_t
USBSoundOutputFormatSet(uint32_t ui32SampleRate,
uint32_t ui32BitsPerSample, uint32_t ui32Channels)
{
//
// Just return if there is no device at this time.
//
if(g_sAudioState.iState != eStateDeviceReady)
{
return(1);
}
//
// Call the USB Host Audio function to set the format.
//
return(USBHostAudioFormatSet(g_psAudioInstance, ui32SampleRate,
ui32BitsPerSample, ui32Channels,
USBH_AUDIO_FORMAT_OUT));
}
//*****************************************************************************
//
// This sets the current input audio format of the USB audio device
//
// \param ui32SampleRate is the sample rate.
// \param ui32BitsPerSample is the number of bits per sample.
// \param ui32Channels is the number of channels.
//
// This sets the current format for the USB device that is currently connect.
// If there is no USB device connected or the format is not supported then the
// function returns 0. The function returns 1 if the USB audio device
// was successfully configured to the requested format.
//
// \return Returns 1 if the format was successfully set or returns 0 if the
// format was not changed.
//
//*****************************************************************************
uint32_t
USBSoundInputFormatSet(uint32_t ui32SampleRate,
uint32_t ui32BitsPerSample, uint32_t ui32Channels)
{
//
// Just return if there is no device at this time.
//
if(g_sAudioState.iState != eStateDeviceReady)
{
return(0);
}
return(USBHostAudioFormatSet(g_psAudioInstance, ui32SampleRate,
ui32BitsPerSample, ui32Channels,
USBH_AUDIO_FORMAT_IN));
}
//*****************************************************************************
//
// Returns the current sample rate.
//
// This function returns the sample rate that was set by a call to
// USBSoundSetFormat(). This is needed to retrieve the exact sample rate that is
// in use in case the requested rate could not be matched exactly.
//
// \return The current sample rate in samples/second.
//
//*****************************************************************************
uint32_t
USBSoundOutputFormatGet(uint32_t ui32SampleRate, uint32_t ui32Bits,
uint32_t ui32Channels)
{
//
// Just return if there is no device at this time.
//
if(g_sAudioState.iState != eStateDeviceReady)
{
return(0);
}
return(USBHostAudioFormatGet(g_psAudioInstance, ui32SampleRate, ui32Bits,
ui32Channels, USBH_AUDIO_FORMAT_OUT));
}
//*****************************************************************************
//
// Returns the current sample rate.
//
// This function returns the sample rate that was set by a call to
// USBSoundSetFormat(). This is needed to retrieve the exact sample rate that is
// in use in case the requested rate could not be matched exactly.
//
// \return The current sample rate in samples/second.
//
//*****************************************************************************
uint32_t
USBSoundInputFormatGet(uint32_t ui32SampleRate, uint32_t ui32Bits,
uint32_t ui32Channels)
{
//
// Just return if there is no device at this time.
//
if(g_sAudioState.iState != eStateDeviceReady)
{
return(0);
}
return(USBHostAudioFormatGet(g_psAudioInstance, ui32SampleRate, ui32Bits,
ui32Channels, USBH_AUDIO_FORMAT_IN));
}
//*****************************************************************************
//
// This is the generic callback from host stack.
//
// \param pvData is actually a pointer to a tEventInfo structure.
//
// This function is called to inform the application when a USB event has
// occurred that is outside those related to the audio device. At this
// point this is used to detect unsupported devices being inserted and removed.
// It is also used to inform the application when a power fault has occurred.
// This function is required when the g_USBGenerii8EventDriver is included in
// the host controller driver array that is passed in to the
// USBHCDRegisterDrivers() function.
//
// \return None.
//
//*****************************************************************************
void
USBHCDEvents(void *pvData)
{
tEventInfo *psEventInfo;
//
// Cast this pointer to its actual type.
//
psEventInfo = (tEventInfo *)pvData;
switch(psEventInfo->ui32Event)
{
//
// Unknown device detected.
//
case USB_EVENT_UNKNOWN_CONNECTED:
{
//
// An unknown device was detected.
//
g_sAudioState.iState = eStateUnknownDevice;
//
// Call the general event handler if present.
//
if(g_sAudioState.pfnCallbackEvent)
{
g_sAudioState.pfnCallbackEvent(SOUND_EVENT_UNKNOWN_DEV, 1);
}
break;
}
//
// Device unplugged.
//
case USB_EVENT_DISCONNECTED:
{
//
// Handle the case where an unknown device is disconnected.
//
if(g_sAudioState.iState == eStateUnknownDevice)
{
g_sAudioState.iState = eStateNoDevice;
//
// Call the general event handler if present.
//
if(g_sAudioState.pfnCallbackEvent)
{
g_sAudioState.pfnCallbackEvent(SOUND_EVENT_UNKNOWN_DEV, 0);
}
}
else
{
//
// Call the general event handler if present.
//
if(g_sAudioState.pfnCallbackEvent)
{
g_sAudioState.pfnCallbackEvent(SOUND_EVENT_DISCONNECT, 0);
}
}
break;
}
//
// A power fault has occurred.
//
case USB_EVENT_POWER_FAULT:
{
//
// No power means no device is present.
//
g_sAudioState.iState = eStatePowerFault;
break;
}
default:
{
break;
}
}
}
//*****************************************************************************
//
// This function passes along a buffer callback from the USB host audio driver
// so that the application can process or release the buffers.
//
//*****************************************************************************
void
USBHostAudioCallback(tUSBHostAudioInstance *psAudioInstance,
uint32_t ui32Event, uint32_t ui32Param, void *pvBuffer)
{
//
// Only call the callback if it is actually present.
//
if(g_sAudioState.pfnCallbackOut)
{
g_sAudioState.pfnCallbackOut(pvBuffer, ui32Event, ui32Param);
}
//
// Only call the callback if it is actually present.
//
if(g_sAudioState.pfnCallbackIn)
{
g_sAudioState.pfnCallbackIn(pvBuffer, ui32Event, ui32Param);
}
}
//*****************************************************************************
//
// Starts output of a block of PCM audio samples.
//
// \param pvBuffer is a pointer to the audio data to play.
// \param ui32Size is the length of the data in bytes.
// \param pfnCallback is a function to call when this buffer has be played.
//
// This function starts the output of a block of PCM audio samples.
//
// \return This function returns a non-zero value if the buffer was accepted,
// and returns zero if the buffer was not accepted.
//
//*****************************************************************************
uint32_t
USBSoundBufferOut(const void *pvBuffer, uint32_t ui32Size,
tUSBBufferCallback pfnCallback)
{
//
// If there is no device present or there is a pending buffer then just
// return with a failure.
//
if(g_sAudioState.iState != eStateDeviceReady)
{
return(0);
}
//
// Save this buffer callback.
//
g_sAudioState.pfnCallbackOut = pfnCallback;
//
// Pass the buffer aint32_t to the USB host audio driver for playback.
//
return(USBHostAudioPlay(g_psAudioInstance, (void *)pvBuffer, ui32Size,
USBHostAudioCallback));
}
//*****************************************************************************
//
// Requests a new block of PCM audio samples from a USB audio device.
//
// \param pvBuffer is a pointer to a location to store the audio data.
// \param ui32Size is the size of the pvData buffer in bytes.
// \param pfnCallback is a function to call when this buffer has new data.
//
// This function request a new block of PCM audio samples from a USB audio
// device.
//
// \return This function returns a non-zero value if the buffer was accepted,
// and returns zero if the buffer was not accepted.
//
//*****************************************************************************
uint32_t
USBSoundBufferIn(const void *pvBuffer, uint32_t ui32Size,
tUSBBufferCallback pfnCallback)
{
//
// If there is no device present or there is a pending buffer then just
// return with a failure.
//
if(g_sAudioState.iState != eStateDeviceReady)
{
return(0);
}
//
// Save this buffer callback.
//
g_sAudioState.pfnCallbackIn = pfnCallback;
//
// Pass the buffer aint32_t to the USB host audio driver for input.
//
return(USBHostAudioRecord(g_psAudioInstance, (void *)pvBuffer, ui32Size,
USBHostAudioCallback));
}
//*****************************************************************************
//
// This function reads the audio volume settings for the USB audio device and
// saves them so that the volume can be scaled correctly.
//
//*****************************************************************************
static void
GetVolumeParameters(void)
{
uint32_t ui32Max, ui32Min, ui32Res, ui32Channel;
for(ui32Channel = 0; ui32Channel < 3; ui32Channel++)
{
ui32Max = USBHostAudioVolumeMaxGet(g_psAudioInstance, 0, ui32Channel);
ui32Min = USBHostAudioVolumeMinGet(g_psAudioInstance, 0, ui32Channel);
ui32Res = USBHostAudioVolumeResGet(g_psAudioInstance, 0, ui32Channel);
g_sAudioState.pui32Steps[ui32Channel] = (ui32Max - ui32Min) / ui32Res;
}
}
//*****************************************************************************
//
// The main routine for handling USB audio, this should be called periodically
// by the main program and pass in the amount of time in milliseconds that has
// elapsed since the last call.
//
//*****************************************************************************
void
USBSoundMain(void)
{
//
// Tell the OTG library code how much time has passed in
// milliseconds since the last call.
//
USBHCDMain();
switch(g_sAudioState.iState)
{
//
// This is the running state where buttons are checked and the
// screen is updated.
//
case eStateDeviceReady:
{ if(HWREGBITW(&g_sAudioState.ui32EventFlags, EVENT_CLOSE))
{
HWREGBITW(&g_sAudioState.ui32EventFlags, EVENT_CLOSE) = 0;
g_sAudioState.iState = eStateNoDevice;
//
// Call the general event handler if present.
//
if(g_sAudioState.pfnCallbackEvent)
{
g_sAudioState.pfnCallbackEvent(SOUND_EVENT_DISCONNECT, 0);
}
}
break;
}
//
// If there is no device then just wait for one.
//
case eStateNoDevice:
{
if(HWREGBITW(&g_sAudioState.ui32EventFlags, EVENT_OPEN))
{
g_sAudioState.iState = eStateDeviceReady;
HWREGBITW(&g_sAudioState.ui32EventFlags, EVENT_OPEN) = 0;
GetVolumeParameters();
//
// Call the general event handler if present.
//
if(g_sAudioState.pfnCallbackEvent)
{
g_sAudioState.pfnCallbackEvent(SOUND_EVENT_READY, 0);
}
}
break;
}
//
// An unknown device was connected.
//
case eStateUnknownDevice:
{
break;
}
//
// Something has caused a power fault.
//
case eStatePowerFault:
{
break;
}
default:
{
break;
}
}
}
|