acd9f6956a3c20c5ca4e2ff451711c47892e3cbb
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Device / Audio.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 #include "Audio.h"
32
33 void USB_Audio_ProcessControlPacket(USB_ClassInfo_Audio_t* AudioInterfaceInfo)
34 {
35 if (!(Endpoint_IsSETUPReceived()))
36 return;
37
38 if (USB_ControlRequest.wIndex != AudioInterfaceInfo->StreamingInterfaceNumber)
39 return;
40
41 switch (USB_ControlRequest.bRequest)
42 {
43 case REQ_SetInterface:
44 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_INTERFACE))
45 {
46 Endpoint_ClearSETUP();
47
48 AudioInterfaceInfo->InterfaceEnabled = (USB_ControlRequest.wValue != 0);
49
50 while (!(Endpoint_IsINReady()));
51 Endpoint_ClearIN();
52 }
53
54 break;
55 }
56 }
57
58 bool USB_Audio_ConfigureEndpoints(USB_ClassInfo_Audio_t* AudioInterfaceInfo)
59 {
60 if (AudioInterfaceInfo->DataINEndpointNumber)
61 {
62 if (!(Endpoint_ConfigureEndpoint(AudioInterfaceInfo->DataINEndpointNumber, EP_TYPE_ISOCHRONOUS,
63 ENDPOINT_DIR_IN, AudioInterfaceInfo->DataINEndpointSize,
64 ENDPOINT_BANK_DOUBLE)))
65 {
66 return false;
67 }
68 }
69
70 if (AudioInterfaceInfo->DataOUTEndpointNumber)
71 {
72 if (!(Endpoint_ConfigureEndpoint(AudioInterfaceInfo->DataOUTEndpointNumber, EP_TYPE_ISOCHRONOUS,
73 ENDPOINT_DIR_OUT, AudioInterfaceInfo->DataOUTEndpointSize,
74 ENDPOINT_BANK_DOUBLE)))
75 {
76 return false;
77 }
78 }
79
80 return true;
81 }
82
83 int8_t USB_Audio_ReadSample8(void)
84 {
85 int8_t Sample;
86
87 Sample = Endpoint_Read_Byte();
88
89 if (!(Endpoint_IsReadWriteAllowed()))
90 Endpoint_ClearOUT();
91
92 return Sample;
93 }
94
95 int16_t USB_Audio_ReadSample16(void)
96 {
97 int16_t Sample;
98
99 Sample = (int16_t)Endpoint_Read_Word_LE();
100
101 if (!(Endpoint_IsReadWriteAllowed()))
102 Endpoint_ClearOUT();
103
104 return Sample;
105 }
106
107 int32_t USB_Audio_ReadSample24(void)
108 {
109 int32_t Sample;
110
111 Sample = (((uint32_t)Endpoint_Read_Byte() << 16) | Endpoint_Read_Word_LE());
112
113 if (!(Endpoint_IsReadWriteAllowed()))
114 Endpoint_ClearOUT();
115
116 return Sample;
117 }
118
119 void USB_Audio_WriteSample8(int8_t Sample)
120 {
121 Endpoint_Write_Byte(Sample);
122
123 if (!(Endpoint_IsReadWriteAllowed()))
124 Endpoint_ClearIN();
125 }
126
127 void USB_Audio_WriteSample16(int16_t Sample)
128 {
129 Endpoint_Write_Word_LE(Sample);
130
131 if (!(Endpoint_IsReadWriteAllowed()))
132 Endpoint_ClearIN();
133 }
134
135 void USB_Audio_WriteSample24(int32_t Sample)
136 {
137 Endpoint_Write_Byte(Sample >> 16);
138 Endpoint_Write_Word_LE(Sample);
139
140 if (!(Endpoint_IsReadWriteAllowed()))
141 Endpoint_ClearIN();
142 }
143
144 bool USB_Audio_IsSampleReceived(USB_ClassInfo_Audio_t* AudioInterfaceInfo)
145 {
146 Endpoint_SelectEndpoint(AudioInterfaceInfo->DataOUTEndpointNumber);
147 return Endpoint_IsOUTReceived();
148 }
149
150 bool USB_Audio_IsReadyForNextSample(USB_ClassInfo_Audio_t* AudioInterfaceInfo)
151 {
152 Endpoint_SelectEndpoint(AudioInterfaceInfo->DataINEndpointNumber);
153 return Endpoint_IsINReady();
154 }