3 Copyright (C) Dean Camera, 2011.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all 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.
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
32 * \brief Host mode driver for the library USB Audio Class driver.
34 * Host mode driver for the library USB Audio Class driver.
36 * \note This file should not be included directly. It is automatically included as needed by the USB module driver
37 * dispatch header located in LUFA/Drivers/USB.h.
40 /** \ingroup Group_USBClassAudio
41 * \defgroup Group_USBClassAudioHost Audio Class Host Mode Driver
43 * \section Sec_Dependencies Module Source Dependencies
44 * The following files must be built with any user project that uses this module:
45 * - LUFA/Drivers/USB/Class/Host/Audio.c <i>(Makefile source module name: LUFA_SRC_USBCLASS)</i>
47 * \section Sec_ModDescription Module Description
48 * Host Mode USB Class driver framework interface, for the Audio USB Class driver.
53 #ifndef __AUDIO_CLASS_HOST_H__
54 #define __AUDIO_CLASS_HOST_H__
57 #include "../../USB.h"
58 #include "../Common/Audio.h"
60 /* Enable C linkage for C++ Compilers: */
61 #if defined(__cplusplus)
65 /* Preprocessor Checks: */
66 #if !defined(__INCLUDE_FROM_AUDIO_DRIVER)
67 #error Do not include this file directly. Include LUFA/Drivers/USB.h instead.
70 /* Public Interface - May be used in end-application: */
72 /** \brief Audio Class Host Mode Configuration and State Structure.
74 * Class state structure. An instance of this structure should be made within the user application,
75 * and passed to each of the Audio class driver functions as the \c AudioInterfaceInfo parameter. This
76 * stores each Audio interface's configuration and state information.
82 uint8_t DataINPipeNumber
; /**< Pipe number of the Audio interface's IN data pipe. */
83 uint8_t DataOUTPipeNumber
; /**< Pipe number of the Audio interface's OUT data pipe. */
84 } Config
; /**< Config data for the USB class interface within the device. All elements in this section
85 * <b>must</b> be set or the interface will fail to enumerate and operate correctly.
89 bool IsActive
; /**< Indicates if the current interface instance is connected to an attached device, valid
90 * after \ref Audio_Host_ConfigurePipes() is called and the Host state machine is in the
93 uint8_t ControlInterfaceNumber
; /**< Interface index of the Audio Control interface within the attached device. */
94 uint8_t StreamingInterfaceNumber
; /**< Interface index of the Audio Streaming interface within the attached device. */
96 uint8_t EnabledStreamingAltIndex
; /**< Alternative setting index of the Audio Streaming interface when the stream is enabled. */
98 uint16_t DataINPipeSize
; /**< Size in bytes of the Audio interface's IN data pipe. */
99 uint16_t DataOUTPipeSize
; /**< Size in bytes of the Audio interface's OUT data pipe. */
100 } State
; /**< State data for the USB class interface within the device. All elements in this section
101 * <b>may</b> be set to initial values, but may also be ignored to default to sane values when
102 * the interface is enumerated.
104 } USB_ClassInfo_Audio_Host_t
;
107 /** Enum for the possible error codes returned by the \ref Audio_Host_ConfigurePipes() function. */
108 enum AUDIO_Host_EnumerationFailure_ErrorCodes_t
110 AUDIO_ENUMERROR_NoError
= 0, /**< Configuration Descriptor was processed successfully. */
111 AUDIO_ENUMERROR_InvalidConfigDescriptor
= 1, /**< The device returned an invalid Configuration Descriptor. */
112 AUDIO_ENUMERROR_NoCompatibleInterfaceFound
= 2, /**< A compatible AUDIO interface was not found in the device's Configuration Descriptor. */
113 AUDIO_ENUMERROR_PipeConfigurationFailed
= 3, /**< One or more pipes for the specified interface could not be configured correctly. */
116 /* Function Prototypes: */
117 /** Host interface configuration routine, to configure a given Audio host interface instance using the Configuration
118 * Descriptor read from an attached USB device. This function automatically updates the given Audio Host instance's
119 * state values and configures the pipes required to communicate with the interface if it is found within the
120 * device. This should be called once after the stack has enumerated the attached device, while the host state
121 * machine is in the Addressed state.
123 * \note The pipe index numbers as given in the interface's configuration structure must not overlap with any other
124 * interface, or pipe bank corruption will occur. Gaps in the allocated pipe numbers or non-sequential indexes
125 * within a single interface is allowed, but no two interfaces of any type have have interleaved pipe indexes.
128 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class host configuration and state.
129 * \param[in] ConfigDescriptorSize Length of the attached device's Configuration Descriptor.
130 * \param[in] DeviceConfigDescriptor Pointer to a buffer containing the attached device's Configuration Descriptor.
132 * \return A value from the \ref AUDIO_Host_EnumerationFailure_ErrorCodes_t enum.
134 uint8_t Audio_Host_ConfigurePipes(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
,
135 uint16_t ConfigDescriptorSize
,
136 void* DeviceConfigDescriptor
) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(3);
138 /** Starts or stops the audio streaming for the given configured Audio Host interface, allowing for audio samples to be
139 * send and/or received.
141 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class host configuration and state.
142 * \param[in] EnableStreaming Boolean true to enable streaming of the specified interface, false to disable
144 * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum.
146 uint8_t AUDIO_Host_StartStopStreaming(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
,
147 bool EnableStreaming
);
149 /* Inline Functions: */
150 /** General management task for a given Audio host class interface, required for the correct operation of
151 * the interface. This should be called frequently in the main program loop, before the master USB management task
152 * \ref USB_USBTask().
154 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class host configuration and state.
156 static inline void Audio_Host_USBTask(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
);
157 static inline void Audio_Host_USBTask(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
)
159 (void)AudioInterfaceInfo
;
162 /** Determines if the given audio interface is ready for a sample to be read from it, and selects the streaming
163 * IN pipe ready for reading.
165 * \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or
166 * the call will fail.
168 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
170 * \return Boolean \c true if the given Audio interface has a sample to be read, \c false otherwise.
172 static inline bool Audio_Host_IsSampleReceived(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
)
173 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE
;
174 static inline bool Audio_Host_IsSampleReceived(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
)
176 if ((USB_HostState
!= HOST_STATE_Configured
) || !(AudioInterfaceInfo
->State
.IsActive
))
179 Pipe_SelectPipe(AudioInterfaceInfo
->Config
.DataOUTPipeNumber
);
180 return Pipe_IsINReceived();
183 /** Determines if the given audio interface is ready to accept the next sample to be written to it, and selects
184 * the streaming OUT pipe ready for writing.
186 * \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or
187 * the call will fail.
189 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
191 * \return Boolean \c true if the given Audio interface is ready to accept the next sample, \c false otherwise.
193 static inline bool Audio_Host_IsReadyForNextSample(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
)
194 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE
;
195 static inline bool Audio_Host_IsReadyForNextSample(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
)
197 if ((USB_HostState
!= HOST_STATE_Configured
) || !(AudioInterfaceInfo
->State
.IsActive
))
200 Pipe_SelectPipe(AudioInterfaceInfo
->Config
.DataINPipeNumber
);
201 return Pipe_IsOUTReady();
204 /** Reads the next 8-bit audio sample from the current audio interface.
206 * \pre This should be preceded immediately by a call to the \ref Audio_Host_IsSampleReceived() function to ensure
207 * that the correct pipe is selected and ready for data.
209 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
211 * \return Signed 8-bit audio sample from the audio interface.
213 static inline int8_t Audio_Host_ReadSample8(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
)
214 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE
;
215 static inline int8_t Audio_Host_ReadSample8(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
)
219 (void)AudioInterfaceInfo
;
221 Sample
= Pipe_Read_8();
223 if (!(Pipe_BytesInPipe()))
229 /** Reads the next 16-bit audio sample from the current audio interface.
231 * \pre This should be preceded immediately by a call to the \ref Audio_Host_IsSampleReceived() function to ensure
232 * that the correct pipe is selected and ready for data.
234 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
236 * \return Signed 16-bit audio sample from the audio interface.
238 static inline int16_t Audio_Host_ReadSample16(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
)
239 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE
;
240 static inline int16_t Audio_Host_ReadSample16(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
)
244 (void)AudioInterfaceInfo
;
246 Sample
= (int16_t)Pipe_Read_16_LE();
248 if (!(Pipe_BytesInPipe()))
254 /** Reads the next 24-bit audio sample from the current audio interface.
256 * \pre This should be preceded immediately by a call to the \ref Audio_Host_IsSampleReceived() function to ensure
257 * that the correct pipe is selected and ready for data.
259 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
261 * \return Signed 24-bit audio sample from the audio interface.
263 static inline int32_t Audio_Host_ReadSample24(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
)
264 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE
;
265 static inline int32_t Audio_Host_ReadSample24(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
)
269 (void)AudioInterfaceInfo
;
271 Sample
= (((uint32_t)Pipe_Read_8() << 16) | Pipe_Read_16_LE());
273 if (!(Pipe_BytesInPipe()))
279 /** Writes the next 8-bit audio sample to the current audio interface.
281 * \pre This should be preceded immediately by a call to the \ref Audio_Host_IsReadyForNextSample() function to
282 * ensure that the correct pipe is selected and ready for data.
284 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
285 * \param[in] Sample Signed 8-bit audio sample.
287 static inline void Audio_Host_WriteSample8(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
,
288 const int8_t Sample
) ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE
;
289 static inline void Audio_Host_WriteSample8(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
,
292 Pipe_Write_8(Sample
);
294 if (Pipe_BytesInPipe() == AudioInterfaceInfo
->State
.DataINPipeSize
)
298 /** Writes the next 16-bit audio sample to the current audio interface.
300 * \pre This should be preceded immediately by a call to the \ref Audio_Host_IsReadyForNextSample() function to
301 * ensure that the correct pipe is selected and ready for data.
303 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
304 * \param[in] Sample Signed 16-bit audio sample.
306 static inline void Audio_Host_WriteSample16(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
,
307 const int16_t Sample
) ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE
;
308 static inline void Audio_Host_WriteSample16(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
,
309 const int16_t Sample
)
311 Pipe_Write_16_LE(Sample
);
313 if (Pipe_BytesInPipe() == AudioInterfaceInfo
->State
.DataINPipeSize
)
317 /** Writes the next 24-bit audio sample to the current audio interface.
319 * \pre This should be preceded immediately by a call to the \ref Audio_Host_IsReadyForNextSample() function to
320 * ensure that the correct pipe is selected and ready for data.
322 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
323 * \param[in] Sample Signed 24-bit audio sample.
325 static inline void Audio_Host_WriteSample24(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
,
326 const int32_t Sample
) ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE
;
327 static inline void Audio_Host_WriteSample24(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
,
328 const int32_t Sample
)
330 Pipe_Write_16_LE(Sample
);
331 Pipe_Write_8(Sample
>> 16);
333 if (Pipe_BytesInPipe() == AudioInterfaceInfo
->State
.DataINPipeSize
)
337 /* Private Interface - For use in library only: */
338 #if !defined(__DOXYGEN__)
339 /* Function Prototypes: */
340 #if defined(__INCLUDE_FROM_AUDIO_HOST_C)
341 static uint8_t DComp_NextAudioControlInterface(void* CurrentDescriptor
);
342 static uint8_t DComp_NextAudioStreamInterface(void* CurrentDescriptor
);
343 static uint8_t DComp_NextAudioInterfaceDataEndpoint(void* CurrentDescriptor
);
347 /* Disable C linkage for C++ Compilers: */
348 #if defined(__cplusplus)