2 * @brief Device mode driver for the library USB Audio 1.0 Class driver
\r
5 * Copyright(C) NXP Semiconductors, 2012
\r
6 * Copyright(C) Dean Camera, 2011, 2012
\r
7 * All rights reserved.
\r
10 * Software that is described herein is for illustrative purposes only
\r
11 * which provides customers with programming information regarding the
\r
12 * LPC products. This software is supplied "AS IS" without any warranties of
\r
13 * any kind, and NXP Semiconductors and its licensor disclaim any and
\r
14 * all warranties, express or implied, including all implied warranties of
\r
15 * merchantability, fitness for a particular purpose and non-infringement of
\r
16 * intellectual property rights. NXP Semiconductors assumes no responsibility
\r
17 * or liability for the use of the software, conveys no license or rights under any
\r
18 * patent, copyright, mask work right, or any other intellectual property rights in
\r
19 * or to any products. NXP Semiconductors reserves the right to make changes
\r
20 * in the software without notification. NXP Semiconductors also makes no
\r
21 * representation or warranty that such application will be suitable for the
\r
22 * specified use without further testing or modification.
\r
25 * Permission to use, copy, modify, and distribute this software and its
\r
26 * documentation is hereby granted, under NXP Semiconductors' and its
\r
27 * licensor's relevant copyrights in the software, without fee, provided that it
\r
28 * is used in conjunction with NXP Semiconductors microcontrollers. This
\r
29 * copyright, permission, and disclaimer notice must appear in all copies of
\r
33 /** @ingroup Group_USBClassAudio
\r
34 * @defgroup Group_USBClassAudioDevice Audio 1.0 Class Device Mode Driver
\r
36 * @section Sec_Dependencies Module Source Dependencies
\r
37 * The following files must be built with any user project that uses this module:
\r
38 * - nxpUSBlib/Drivers/USB/Class/Device/Audio.c <i>(Makefile source module name: NXPUSBLIB_SRC_USBCLASS)</i>
\r
40 * @section Sec_ModDescription Module Description
\r
41 * Device Mode USB Class driver framework interface, for the Audio 1.0 USB Class driver.
\r
46 #ifndef _AUDIO_CLASS_DEVICE_H_
\r
47 #define _AUDIO_CLASS_DEVICE_H_
\r
50 #include "../../USB.h"
\r
51 #include "../Common/AudioClassCommon.h"
\r
53 /* Enable C linkage for C++ Compilers: */
\r
54 #if defined(__cplusplus)
\r
58 /* Preprocessor Checks: */
\r
59 #if !defined(__INCLUDE_FROM_AUDIO_DRIVER)
\r
60 #error Do not include this file directly. Include LPCUSBlib/Drivers/USB.h instead.
\r
63 /* Public Interface - May be used in end-application: */
\r
65 /** @brief Audio Class Device Mode Configuration and State Structure.
\r
67 * Class state structure. An instance of this structure should be made for each Audio interface
\r
68 * within the user application, and passed to each of the Audio class driver functions as the
\r
69 * \c AudioInterfaceInfo parameter. This stores each Audio interface's configuration and state information.
\r
75 uint8_t ControlInterfaceNumber; /**< Index of the Audio Control interface within the device this
\r
76 * structure controls.
\r
78 uint8_t StreamingInterfaceNumber; /**< Index of the Audio Streaming interface within the device this
\r
79 * structure controls.
\r
82 uint8_t DataINEndpointNumber; /**< Endpoint number of the incoming Audio Streaming data, if available
\r
85 uint16_t DataINEndpointSize; /**< Size in bytes of the incoming Audio Streaming data endpoint, if available
\r
89 uint8_t DataOUTEndpointNumber; /**< Endpoint number of the outgoing Audio Streaming data, if available
\r
92 uint16_t DataOUTEndpointSize; /**< Size in bytes of the outgoing Audio Streaming data endpoint, if available
\r
95 uint8_t PortNumber; /**< Port number that this interface is running.*/
\r
96 } Config; /**< Config data for the USB class interface within the device. All elements in this section
\r
97 * <b>must</b> be set or the interface will fail to enumerate and operate correctly.
\r
101 bool InterfaceEnabled; /**< Set and cleared by the class driver to indicate if the host has enabled the streaming endpoints
\r
102 * of the Audio Streaming interface.
\r
104 } State; /**< State data for the USB class interface within the device. All elements in this section
\r
105 * are reset to their defaults when the interface is enumerated.
\r
107 } USB_ClassInfo_Audio_Device_t;
\r
109 /* Function Prototypes: */
\r
112 * @brief Configures the endpoints of a given Audio interface, ready for use. This should be linked to the library
\r
113 * @ref EVENT_USB_Device_ConfigurationChanged() event so that the endpoints are configured when the configuration containing the
\r
114 * given Audio interface is selected.
\r
116 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class configuration and state.
\r
117 * @return Boolean \c true if the endpoints were successfully configured, \c false otherwise.
\r
119 bool Audio_Device_ConfigureEndpoints(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
\r
122 * @brief Processes incoming control requests from the host, that are directed to the given Audio class interface. This should be
\r
123 * linked to the library @ref EVENT_USB_Device_ControlRequest() event.
\r
125 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class configuration and state.
\r
128 void Audio_Device_ProcessControlRequest(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
\r
131 * @brief Audio class driver callback for the setting and retrieval of streaming endpoint properties. This callback must be implemented
\r
132 * in the user application to handle property manipulations on streaming audio endpoints.
\r
134 * When the DataLength parameter is NULL, this callback should only indicate whether the specified operation is valid for
\r
135 * the given endpoint index, and should return as fast as possible. When non-NULL, this value may be altered for GET operations
\r
136 * to indicate the size of the retreived data.
\r
138 * @note The length of the retrieved data stored into the Data buffer on GET operations should not exceed the initial value
\r
139 * of the \c DataLength parameter.
\r
141 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class configuration and state.
\r
142 * @param EndpointProperty : Property of the endpoint to get or set, a value from @ref Audio_ClassRequests_t.
\r
143 * @param EndpointAddress : Address of the streaming endpoint whose property is being referenced.
\r
144 * @param EndpointControl : Parameter of the endpoint to get or set, a value from @ref Audio_EndpointControls_t.
\r
145 * @param DataLength : For SET operations, the length of the parameter data to set. For GET operations, the maximum
\r
146 * length of the retrieved data. When NULL, the function should return whether the given property
\r
147 * and parameter is valid for the requested endpoint without reading or modifying the Data buffer.
\r
148 * @param Data : Pointer to a location where the parameter data is stored for SET operations, or where
\r
149 * the retrieved data is to be stored for GET operations.
\r
150 * @return Boolean true if the property get/set was successful, false otherwise.
\r
152 bool CALLBACK_Audio_Device_GetSetEndpointProperty(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo,
\r
153 const uint8_t EndpointProperty,
\r
154 const uint8_t EndpointAddress,
\r
155 const uint8_t EndpointControl,
\r
156 uint16_t* const DataLength,
\r
160 * @brief Audio class driver event for an Audio Stream start/stop change. This event fires each time the device receives a stream enable or
\r
161 * disable control request from the host, to start and stop the audio stream. The current state of the stream can be determined by the
\r
162 * State.InterfaceEnabled value inside the Audio interface structure passed as a parameter.
\r
164 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class configuration and state.
\r
167 void EVENT_Audio_Device_StreamStartStop(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo);
\r
169 /* Inline Functions: */
\r
171 * @brief General management task for a given Audio class interface, required for the correct operation of the interface. This should
\r
172 * be called frequently in the main program loop, before the master USB management task @ref USB_USBTask().
\r
174 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class configuration and state.
\r
177 PRAGMA_ALWAYS_INLINE
\r
178 static inline void Audio_Device_USBTask(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
\r
179 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
\r
180 static inline void Audio_Device_USBTask(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
\r
182 (void)AudioInterfaceInfo;
\r
186 * @brief Determines if the given audio interface is ready for a sample to be read from it, and selects the streaming
\r
187 * OUT endpoint ready for reading.
\r
189 * \pre This function must only be called when the Device state machine is in the @ref DEVICE_STATE_Configured state or
\r
190 * the call will fail.
\r
192 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class configuration and state.
\r
193 * @return Boolean \c true if the given Audio interface has a sample to be read, \c false otherwise.
\r
195 PRAGMA_ALWAYS_INLINE
\r
196 static inline bool Audio_Device_IsSampleReceived(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
\r
197 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
\r
198 static inline bool Audio_Device_IsSampleReceived(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
\r
200 if ((USB_DeviceState[AudioInterfaceInfo->Config.PortNumber] != DEVICE_STATE_Configured) || !(AudioInterfaceInfo->State.InterfaceEnabled))
\r
203 Endpoint_SelectEndpoint(AudioInterfaceInfo->Config.PortNumber, AudioInterfaceInfo->Config.DataOUTEndpointNumber);
\r
204 return Endpoint_IsOUTReceived(AudioInterfaceInfo->Config.PortNumber);
\r
208 * @brief Determines if the given audio interface is ready to accept the next sample to be written to it, and selects
\r
209 * the streaming IN endpoint ready for writing.
\r
211 * \pre This function must only be called when the Device state machine is in the @ref DEVICE_STATE_Configured state or
\r
212 * the call will fail.
\r
214 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class configuration and state.
\r
215 * @return Boolean \c true if the given Audio interface is ready to accept the next sample, \c false otherwise.
\r
217 PRAGMA_ALWAYS_INLINE
\r
218 static inline bool Audio_Device_IsReadyForNextSample(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
\r
219 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
\r
220 static inline bool Audio_Device_IsReadyForNextSample(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
\r
222 if ((USB_DeviceState[AudioInterfaceInfo->Config.PortNumber] != DEVICE_STATE_Configured) || !(AudioInterfaceInfo->State.InterfaceEnabled))
\r
225 Endpoint_SelectEndpoint(AudioInterfaceInfo->Config.PortNumber, AudioInterfaceInfo->Config.DataINEndpointNumber);
\r
226 return Endpoint_IsINReady(USB_DeviceState[AudioInterfaceInfo->Config.PortNumber]);
\r
230 * @brief Reads the next 8-bit audio sample from the current audio interface.
\r
232 * \pre This should be preceded immediately by a call to the @ref Audio_Device_IsSampleReceived() function to ensure
\r
233 * that the correct endpoint is selected and ready for data.
\r
235 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class configuration and state.
\r
236 * @return Signed 8-bit audio sample from the audio interface.
\r
238 PRAGMA_ALWAYS_INLINE
\r
239 static inline int8_t Audio_Device_ReadSample8(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
\r
240 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
\r
241 static inline int8_t Audio_Device_ReadSample8(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
\r
245 (void)AudioInterfaceInfo;
\r
247 Sample = Endpoint_Read_8(AudioInterfaceInfo->Config.PortNumber);
\r
249 if (!(Endpoint_BytesInEndpoint(AudioInterfaceInfo->Config.PortNumber)))
\r
250 Endpoint_ClearOUT(AudioInterfaceInfo->Config.PortNumber);
\r
256 * @brief Reads the next 16-bit audio sample from the current audio interface.
\r
258 * \pre This should be preceded immediately by a call to the @ref Audio_Device_IsSampleReceived() function to ensure
\r
259 * that the correct endpoint is selected and ready for data.
\r
261 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class configuration and state.
\r
262 * @return Signed 16-bit audio sample from the audio interface.
\r
264 PRAGMA_ALWAYS_INLINE
\r
265 static inline int16_t Audio_Device_ReadSample16(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
\r
266 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
\r
267 static inline int16_t Audio_Device_ReadSample16(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
\r
271 (void)AudioInterfaceInfo;
\r
273 Sample = (int16_t)Endpoint_Read_16_LE(AudioInterfaceInfo->Config.PortNumber);
\r
275 if (!(Endpoint_BytesInEndpoint(AudioInterfaceInfo->Config.PortNumber)))
\r
276 Endpoint_ClearOUT(AudioInterfaceInfo->Config.PortNumber);
\r
282 * @brief Reads the next 24-bit audio sample from the current audio interface.
\r
284 * \pre This should be preceded immediately by a call to the @ref Audio_Device_IsSampleReceived() function to ensure
\r
285 * that the correct endpoint is selected and ready for data.
\r
287 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class configuration and state.
\r
288 * @return Signed 24-bit audio sample from the audio interface.
\r
290 PRAGMA_ALWAYS_INLINE
\r
291 static inline int32_t Audio_Device_ReadSample24(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
\r
292 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
\r
293 static inline int32_t Audio_Device_ReadSample24(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
\r
297 (void)AudioInterfaceInfo;
\r
299 Sample = (((uint32_t)Endpoint_Read_8(AudioInterfaceInfo->Config.PortNumber) << 16) | Endpoint_Read_16_LE(AudioInterfaceInfo->Config.PortNumber));
\r
301 if (!(Endpoint_BytesInEndpoint(AudioInterfaceInfo->Config.PortNumber)))
\r
302 Endpoint_ClearOUT(AudioInterfaceInfo->Config.PortNumber);
\r
308 * @brief Writes the next 8-bit audio sample to the current audio interface.
\r
310 * \pre This should be preceded immediately by a call to the @ref Audio_Device_IsReadyForNextSample() function to
\r
311 * ensure that the correct endpoint is selected and ready for data.
\r
313 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class configuration and state.
\r
314 * @param Sample : Signed 8-bit audio sample.
\r
317 PRAGMA_ALWAYS_INLINE
\r
318 static inline void Audio_Device_WriteSample8(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo,
\r
319 const int8_t Sample) ATTR_NON_NULL_PTR_ARG(1);
\r
320 static inline void Audio_Device_WriteSample8(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo,
\r
321 const int8_t Sample)
\r
323 Endpoint_Write_8(AudioInterfaceInfo->Config.PortNumber, Sample);
\r
325 if (Endpoint_BytesInEndpoint(AudioInterfaceInfo->Config.PortNumber) == AudioInterfaceInfo->Config.DataINEndpointSize)
\r
326 Endpoint_ClearIN(AudioInterfaceInfo->Config.PortNumber);
\r
330 * @brief Writes the next 16-bit audio sample to the current audio interface.
\r
332 * \pre This should be preceded immediately by a call to the @ref Audio_Device_IsReadyForNextSample() function to
\r
333 * ensure that the correct endpoint is selected and ready for data.
\r
335 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class configuration and state.
\r
336 * @param Sample : Signed 16-bit audio sample.
\r
339 PRAGMA_ALWAYS_INLINE
\r
340 static inline void Audio_Device_WriteSample16(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo,
\r
341 const int16_t Sample) ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
\r
342 static inline void Audio_Device_WriteSample16(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo,
\r
343 const int16_t Sample)
\r
345 Endpoint_Write_16_LE(AudioInterfaceInfo->Config.PortNumber, Sample);
\r
347 if (Endpoint_BytesInEndpoint(AudioInterfaceInfo->Config.PortNumber) == AudioInterfaceInfo->Config.DataINEndpointSize)
\r
348 Endpoint_ClearIN(AudioInterfaceInfo->Config.PortNumber);
\r
352 * @brief Writes the next 24-bit audio sample to the current audio interface.
\r
354 * \pre This should be preceded immediately by a call to the @ref Audio_Device_IsReadyForNextSample() function to
\r
355 * ensure that the correct endpoint is selected and ready for data.
\r
357 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class configuration and state.
\r
358 * @param Sample : Signed 24-bit audio sample.
\r
361 PRAGMA_ALWAYS_INLINE
\r
362 static inline void Audio_Device_WriteSample24(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo,
\r
363 const int32_t Sample) ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
\r
364 static inline void Audio_Device_WriteSample24(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo,
\r
365 const int32_t Sample)
\r
367 Endpoint_Write_16_LE(AudioInterfaceInfo->Config.PortNumber, Sample);
\r
368 Endpoint_Write_8(AudioInterfaceInfo->Config.PortNumber, Sample >> 16);
\r
370 if (Endpoint_BytesInEndpoint(AudioInterfaceInfo->Config.PortNumber) == AudioInterfaceInfo->Config.DataINEndpointSize)
\r
371 Endpoint_ClearIN(AudioInterfaceInfo->Config.PortNumber);
\r
374 /* Private Interface - For use in library only: */
\r
375 #if !defined(__DOXYGEN__)
\r
376 /* Function Prototypes: */
\r
377 #if defined(__INCLUDE_FROM_AUDIO_DEVICE_C)
\r
378 void Audio_Device_Event_Stub(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo) ATTR_CONST;
\r
379 PRAGMA_WEAK(EVENT_Audio_Device_StreamStartStop,Audio_Device_Event_Stub)
\r
380 void EVENT_Audio_Device_StreamStartStop(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
\r
381 ATTR_WEAK ATTR_NON_NULL_PTR_ARG(1) ATTR_ALIAS(Audio_Device_Event_Stub);
\r
387 /* Disable C linkage for C++ Compilers: */
\r
388 #if defined(__cplusplus)
\r