3 Copyright (C) Dean Camera, 2012.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2012 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 Device mode driver for the library USB Audio 1.0 Class driver.
34 * Device mode driver for the library USB Audio 1.0 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_USBClassAudioDevice Audio 1.0 Class Device 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/Device/AudioClassDevice.c <i>(Makefile source module name: LUFA_SRC_USBCLASS)</i>
47 * \section Sec_ModDescription Module Description
48 * Device Mode USB Class driver framework interface, for the Audio 1.0 USB Class driver.
53 #ifndef _AUDIO_CLASS_DEVICE_H_
54 #define _AUDIO_CLASS_DEVICE_H_
57 #include "../../USB.h"
58 #include "../Common/AudioClassCommon.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 Device Mode Configuration and State Structure.
74 * Class state structure. An instance of this structure should be made for each Audio interface
75 * within the user application, and passed to each of the Audio class driver functions as the
76 * \c AudioInterfaceInfo parameter. This stores each Audio interface's configuration and state information.
82 uint8_t ControlInterfaceNumber
; /**< Index of the Audio Control interface within the device this
85 uint8_t StreamingInterfaceNumber
; /**< Index of the Audio Streaming interface within the device this
89 uint8_t DataINEndpointNumber
; /**< Endpoint number of the incoming Audio Streaming data, if available
92 uint16_t DataINEndpointSize
; /**< Size in bytes of the incoming Audio Streaming data endpoint, if available
96 uint8_t DataOUTEndpointNumber
; /**< Endpoint number of the outgoing Audio Streaming data, if available
99 uint16_t DataOUTEndpointSize
; /**< Size in bytes of the outgoing Audio Streaming data endpoint, if available
102 } Config
; /**< Config data for the USB class interface within the device. All elements in this section
103 * <b>must</b> be set or the interface will fail to enumerate and operate correctly.
107 bool InterfaceEnabled
; /**< Set and cleared by the class driver to indicate if the host has enabled the streaming endpoints
108 * of the Audio Streaming interface.
110 } State
; /**< State data for the USB class interface within the device. All elements in this section
111 * are reset to their defaults when the interface is enumerated.
113 } USB_ClassInfo_Audio_Device_t
;
115 /* Function Prototypes: */
116 /** Configures the endpoints of a given Audio interface, ready for use. This should be linked to the library
117 * \ref EVENT_USB_Device_ConfigurationChanged() event so that the endpoints are configured when the configuration containing the
118 * given Audio interface is selected.
120 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
122 * \return Boolean \c true if the endpoints were successfully configured, \c false otherwise.
124 bool Audio_Device_ConfigureEndpoints(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
) ATTR_NON_NULL_PTR_ARG(1);
126 /** Processes incoming control requests from the host, that are directed to the given Audio class interface. This should be
127 * linked to the library \ref EVENT_USB_Device_ControlRequest() event.
129 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
131 void Audio_Device_ProcessControlRequest(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
) ATTR_NON_NULL_PTR_ARG(1);
133 /** Audio class driver callback for the setting and retrieval of streaming endpoint properties. This callback must be implemented
134 * in the user application to handle property manipulations on streaming audio endpoints.
136 * When the DataLength parameter is NULL, this callback should only indicate whether the specified operation is valid for
137 * the given endpoint index, and should return as fast as possible. When non-NULL, this value may be altered for GET operations
138 * to indicate the size of the retreived data.
140 * \note The length of the retrieved data stored into the Data buffer on GET operations should not exceed the initial value
141 * of the \c DataLength parameter.
143 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
144 * \param[in] EndpointProperty Property of the endpoint to get or set, a value from \ref Audio_ClassRequests_t.
145 * \param[in] EndpointAddress Address of the streaming endpoint whose property is being referenced.
146 * \param[in] EndpointControl Parameter of the endpoint to get or set, a value from \ref Audio_EndpointControls_t.
147 * \param[in,out] DataLength For SET operations, the length of the parameter data to set. For GET operations, the maximum
148 * length of the retrieved data. When NULL, the function should return whether the given property
149 * and parameter is valid for the requested endpoint without reading or modifying the Data buffer.
150 * \param[in,out] Data Pointer to a location where the parameter data is stored for SET operations, or where
151 * the retrieved data is to be stored for GET operations.
153 * \return Boolean \c true if the property GET/SET was successful, \c false otherwise
155 bool CALLBACK_Audio_Device_GetSetEndpointProperty(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
,
156 const uint8_t EndpointProperty
,
157 const uint8_t EndpointAddress
,
158 const uint8_t EndpointControl
,
159 uint16_t* const DataLength
,
160 uint8_t* Data
) ATTR_NON_NULL_PTR_ARG(1);
162 /** Audio class driver callback for the setting and retrieval of streaming interface properties. This callback must be implemented
163 * in the user application to handle property manipulations on streaming audio interfaces.
165 * When the DataLength parameter is NULL, this callback should only indicate whether the specified operation is valid for
166 * the given entity and should return as fast as possible. When non-NULL, this value may be altered for GET operations
167 * to indicate the size of the retreived data.
169 * \note The length of the retrieved data stored into the Data buffer on GET operations should not exceed the initial value
170 * of the \c DataLength parameter.
172 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
173 * \param[in] Property Property of the interface to get or set, a value from \ref Audio_ClassRequests_t.
174 * \param[in] EntityAddress Address of the audio entity whose property is being referenced.
175 * \param[in] Parameter Parameter of the entity to get or set, specific to each type of entity (see USB Audio specification).
176 * \param[in,out] DataLength For SET operations, the length of the parameter data to set. For GET operations, the maximum
177 * length of the retrieved data. When NULL, the function should return whether the given property
178 * and parameter is valid for the requested endpoint without reading or modifying the Data buffer.
179 * \param[in,out] Data Pointer to a location where the parameter data is stored for SET operations, or where
180 * the retrieved data is to be stored for GET operations.
182 * \return Boolean \c true if the property GET/SET was successful, \c false otherwise
184 bool CALLBACK_Audio_Device_GetSetInterfaceProperty(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
,
185 const uint8_t Property
,
186 const uint8_t EntityAddress
,
187 const uint16_t Parameter
,
188 uint16_t* const DataLength
,
189 uint8_t* Data
) ATTR_NON_NULL_PTR_ARG(1);
191 /** Audio class driver event for an Audio Stream start/stop change. This event fires each time the device receives a stream enable or
192 * disable control request from the host, to start and stop the audio stream. The current state of the stream can be determined by the
193 * State.InterfaceEnabled value inside the Audio interface structure passed as a parameter.
195 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
197 void EVENT_Audio_Device_StreamStartStop(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
);
199 /* Inline Functions: */
200 /** General management task for a given Audio class interface, required for the correct operation of the interface. This should
201 * be called frequently in the main program loop, before the master USB management task \ref USB_USBTask().
203 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
205 static inline void Audio_Device_USBTask(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
)
206 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE
;
207 static inline void Audio_Device_USBTask(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
)
209 (void)AudioInterfaceInfo
;
212 /** Determines if the given audio interface is ready for a sample to be read from it, and selects the streaming
213 * OUT endpoint ready for reading.
215 * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
216 * the call will fail.
218 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
220 * \return Boolean \c true if the given Audio interface has a sample to be read, \c false otherwise.
222 static inline bool Audio_Device_IsSampleReceived(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
)
223 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE
;
224 static inline bool Audio_Device_IsSampleReceived(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
)
226 if ((USB_DeviceState
!= DEVICE_STATE_Configured
) || !(AudioInterfaceInfo
->State
.InterfaceEnabled
))
229 Endpoint_SelectEndpoint(AudioInterfaceInfo
->Config
.DataOUTEndpointNumber
);
230 return Endpoint_IsOUTReceived();
233 /** Determines if the given audio interface is ready to accept the next sample to be written to it, and selects
234 * the streaming IN endpoint ready for writing.
236 * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
237 * the call will fail.
239 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
241 * \return Boolean \c true if the given Audio interface is ready to accept the next sample, \c false otherwise.
243 static inline bool Audio_Device_IsReadyForNextSample(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
)
244 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE
;
245 static inline bool Audio_Device_IsReadyForNextSample(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
)
247 if ((USB_DeviceState
!= DEVICE_STATE_Configured
) || !(AudioInterfaceInfo
->State
.InterfaceEnabled
))
250 Endpoint_SelectEndpoint(AudioInterfaceInfo
->Config
.DataINEndpointNumber
);
251 return Endpoint_IsINReady();
254 /** Reads the next 8-bit audio sample from the current audio interface.
256 * \pre This should be preceded immediately by a call to the \ref Audio_Device_IsSampleReceived() function to ensure
257 * that the correct endpoint 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 8-bit audio sample from the audio interface.
263 static inline int8_t Audio_Device_ReadSample8(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
)
264 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE
;
265 static inline int8_t Audio_Device_ReadSample8(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
)
269 (void)AudioInterfaceInfo
;
271 Sample
= Endpoint_Read_8();
273 if (!(Endpoint_BytesInEndpoint()))
279 /** Reads the next 16-bit audio sample from the current audio interface.
281 * \pre This should be preceded immediately by a call to the \ref Audio_Device_IsSampleReceived() function to ensure
282 * that the correct endpoint is selected and ready for data.
284 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
286 * \return Signed 16-bit audio sample from the audio interface.
288 static inline int16_t Audio_Device_ReadSample16(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
)
289 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE
;
290 static inline int16_t Audio_Device_ReadSample16(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
)
294 (void)AudioInterfaceInfo
;
296 Sample
= (int16_t)Endpoint_Read_16_LE();
298 if (!(Endpoint_BytesInEndpoint()))
304 /** Reads the next 24-bit audio sample from the current audio interface.
306 * \pre This should be preceded immediately by a call to the \ref Audio_Device_IsSampleReceived() function to ensure
307 * that the correct endpoint is selected and ready for data.
309 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
311 * \return Signed 24-bit audio sample from the audio interface.
313 static inline int32_t Audio_Device_ReadSample24(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
)
314 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE
;
315 static inline int32_t Audio_Device_ReadSample24(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
)
319 (void)AudioInterfaceInfo
;
321 Sample
= (((uint32_t)Endpoint_Read_8() << 16) | Endpoint_Read_16_LE());
323 if (!(Endpoint_BytesInEndpoint()))
329 /** Writes the next 8-bit audio sample to the current audio interface.
331 * \pre This should be preceded immediately by a call to the \ref Audio_Device_IsReadyForNextSample() function to
332 * ensure that the correct endpoint is selected and ready for data.
334 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
335 * \param[in] Sample Signed 8-bit audio sample.
337 static inline void Audio_Device_WriteSample8(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
,
338 const int8_t Sample
) ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE
;
339 static inline void Audio_Device_WriteSample8(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
,
342 Endpoint_Write_8(Sample
);
344 if (Endpoint_BytesInEndpoint() == AudioInterfaceInfo
->Config
.DataINEndpointSize
)
348 /** Writes the next 16-bit audio sample to the current audio interface.
350 * \pre This should be preceded immediately by a call to the \ref Audio_Device_IsReadyForNextSample() function to
351 * ensure that the correct endpoint is selected and ready for data.
353 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
354 * \param[in] Sample Signed 16-bit audio sample.
356 static inline void Audio_Device_WriteSample16(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
,
357 const int16_t Sample
) ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE
;
358 static inline void Audio_Device_WriteSample16(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
,
359 const int16_t Sample
)
361 Endpoint_Write_16_LE(Sample
);
363 if (Endpoint_BytesInEndpoint() == AudioInterfaceInfo
->Config
.DataINEndpointSize
)
367 /** Writes the next 24-bit audio sample to the current audio interface.
369 * \pre This should be preceded immediately by a call to the \ref Audio_Device_IsReadyForNextSample() function to
370 * ensure that the correct endpoint is selected and ready for data.
372 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
373 * \param[in] Sample Signed 24-bit audio sample.
375 static inline void Audio_Device_WriteSample24(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
,
376 const int32_t Sample
) ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE
;
377 static inline void Audio_Device_WriteSample24(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
,
378 const int32_t Sample
)
380 Endpoint_Write_16_LE(Sample
);
381 Endpoint_Write_8(Sample
>> 16);
383 if (Endpoint_BytesInEndpoint() == AudioInterfaceInfo
->Config
.DataINEndpointSize
)
387 /* Private Interface - For use in library only: */
388 #if !defined(__DOXYGEN__)
389 /* Function Prototypes: */
390 #if defined(__INCLUDE_FROM_AUDIO_DEVICE_C)
391 void Audio_Device_Event_Stub(void) ATTR_CONST
;
393 void EVENT_Audio_Device_StreamStartStop(USB_ClassInfo_Audio_Device_t
* const AudioInterfaceInfo
)
394 ATTR_WEAK
ATTR_NON_NULL_PTR_ARG(1) ATTR_ALIAS(Audio_Device_Event_Stub
);
399 /* Disable C linkage for C++ Compilers: */
400 #if defined(__cplusplus)