Break device mode class driver interfaces into seperate config and state structs...
[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 "../../HighLevel/USBMode.h"
32 #if defined(USB_CAN_BE_DEVICE)
33
34 #include "Audio.h"
35
36 void Audio_Device_ProcessControlPacket(USB_ClassInfo_Audio_Device_t* AudioInterfaceInfo)
37 {
38 if (!(Endpoint_IsSETUPReceived()))
39 return;
40
41 if (USB_ControlRequest.wIndex != AudioInterfaceInfo->Config.StreamingInterfaceNumber)
42 return;
43
44 switch (USB_ControlRequest.bRequest)
45 {
46 case REQ_SetInterface:
47 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_INTERFACE))
48 {
49 Endpoint_ClearSETUP();
50
51 AudioInterfaceInfo->State.InterfaceEnabled = (USB_ControlRequest.wValue != 0);
52
53 while (!(Endpoint_IsINReady()));
54 Endpoint_ClearIN();
55 }
56
57 break;
58 }
59 }
60
61 bool Audio_Device_ConfigureEndpoints(USB_ClassInfo_Audio_Device_t* AudioInterfaceInfo)
62 {
63 if (AudioInterfaceInfo->Config.DataINEndpointNumber)
64 {
65 if (!(Endpoint_ConfigureEndpoint(AudioInterfaceInfo->Config.DataINEndpointNumber, EP_TYPE_ISOCHRONOUS,
66 ENDPOINT_DIR_IN, AudioInterfaceInfo->Config.DataINEndpointSize,
67 ENDPOINT_BANK_DOUBLE)))
68 {
69 return false;
70 }
71 }
72
73 if (AudioInterfaceInfo->Config.DataOUTEndpointNumber)
74 {
75 if (!(Endpoint_ConfigureEndpoint(AudioInterfaceInfo->Config.DataOUTEndpointNumber, EP_TYPE_ISOCHRONOUS,
76 ENDPOINT_DIR_OUT, AudioInterfaceInfo->Config.DataOUTEndpointSize,
77 ENDPOINT_BANK_DOUBLE)))
78 {
79 return false;
80 }
81 }
82
83 return true;
84 }
85
86 void Audio_Device_USBTask(USB_ClassInfo_Audio_Device_t* AudioInterfaceInfo)
87 {
88
89 }
90
91 int8_t Audio_Device_ReadSample8(void)
92 {
93 int8_t Sample;
94
95 Sample = Endpoint_Read_Byte();
96
97 if (!(Endpoint_IsReadWriteAllowed()))
98 Endpoint_ClearOUT();
99
100 return Sample;
101 }
102
103 int16_t Audio_Device_ReadSample16(void)
104 {
105 int16_t Sample;
106
107 Sample = (int16_t)Endpoint_Read_Word_LE();
108
109 if (!(Endpoint_IsReadWriteAllowed()))
110 Endpoint_ClearOUT();
111
112 return Sample;
113 }
114
115 int32_t Audio_Device_ReadSample24(void)
116 {
117 int32_t Sample;
118
119 Sample = (((uint32_t)Endpoint_Read_Byte() << 16) | Endpoint_Read_Word_LE());
120
121 if (!(Endpoint_IsReadWriteAllowed()))
122 Endpoint_ClearOUT();
123
124 return Sample;
125 }
126
127 void Audio_Device_WriteSample8(int8_t Sample)
128 {
129 Endpoint_Write_Byte(Sample);
130
131 if (!(Endpoint_IsReadWriteAllowed()))
132 Endpoint_ClearIN();
133 }
134
135 void Audio_Device_WriteSample16(int16_t Sample)
136 {
137 Endpoint_Write_Word_LE(Sample);
138
139 if (!(Endpoint_IsReadWriteAllowed()))
140 Endpoint_ClearIN();
141 }
142
143 void Audio_Device_WriteSample24(int32_t Sample)
144 {
145 Endpoint_Write_Byte(Sample >> 16);
146 Endpoint_Write_Word_LE(Sample);
147
148 if (!(Endpoint_IsReadWriteAllowed()))
149 Endpoint_ClearIN();
150 }
151
152 bool Audio_Device_IsSampleReceived(USB_ClassInfo_Audio_Device_t* AudioInterfaceInfo)
153 {
154 Endpoint_SelectEndpoint(AudioInterfaceInfo->Config.DataOUTEndpointNumber);
155 return Endpoint_IsOUTReceived();
156 }
157
158 bool Audio_Device_IsReadyForNextSample(USB_ClassInfo_Audio_Device_t* AudioInterfaceInfo)
159 {
160 Endpoint_SelectEndpoint(AudioInterfaceInfo->Config.DataINEndpointNumber);
161 return Endpoint_IsINReady();
162 }
163
164 #endif