2 * @brief Host 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_USBClassAudioHost Audio 1.0 Class Host 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 * - LPCUSBlib/Drivers/USB/Class/Host/Audio.c <i>(Makefile source module name: LPCUSBlib_SRC_USBCLASS)</i>
\r
40 * @section Sec_ModDescription Module Description
\r
41 * Host Mode USB Class driver framework interface, for the Audio 1.0 USB Class driver.
\r
46 #ifndef __AUDIO_CLASS_HOST_H__
\r
47 #define __AUDIO_CLASS_HOST_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 Host Mode Configuration and State Structure.
\r
67 * Class state structure. An instance of this structure should be made within the user application,
\r
68 * and passed to each of the Audio class driver functions as the \c AudioInterfaceInfo parameter. This
\r
69 * stores each Audio interface's configuration and state information.
\r
75 uint8_t DataINPipeNumber; /**< Pipe number of the Audio interface's IN data pipe. If this interface should not
\r
76 * bind to an IN endpoint, this may be set to 0 to disable audio input streaming for
\r
77 * this driver instance.
\r
79 uint8_t DataOUTPipeNumber; /**< Pipe number of the Audio interface's OUT data pipe. If this interface should not
\r
80 * bind to an OUT endpoint, this may be set to 0 to disable audio output streaming for
\r
81 * this driver instance.
\r
83 uint8_t PortNumber; /**< Port number that this interface is running. */
\r
84 } Config; /**< Config data for the USB class interface within the device. All elements in this section
\r
85 * <b>must</b> be set or the interface will fail to enumerate and operate correctly.
\r
89 bool IsActive; /**< Indicates if the current interface instance is connected to an attached device, valid
\r
90 * after @ref Audio_Host_ConfigurePipes() is called and the Host state machine is in the
\r
93 uint8_t ControlInterfaceNumber; /**< Interface index of the Audio Control interface within the attached device. */
\r
94 uint8_t StreamingInterfaceNumber; /**< Interface index of the Audio Streaming interface within the attached device. */
\r
96 uint8_t EnabledStreamingAltIndex; /**< Alternative setting index of the Audio Streaming interface when the stream is enabled. */
\r
98 uint16_t DataINPipeSize; /**< Size in bytes of the Audio interface's IN data pipe. */
\r
99 uint16_t DataOUTPipeSize; /**< Size in bytes of the Audio interface's OUT data pipe. */
\r
100 } State; /**< State data for the USB class interface within the device. All elements in this section
\r
101 * <b>may</b> be set to initial values, but may also be ignored to default to sane values when
\r
102 * the interface is enumerated.
\r
104 } USB_ClassInfo_Audio_Host_t;
\r
107 /** Enum for the possible error codes returned by the @ref Audio_Host_ConfigurePipes() function. */
\r
108 enum AUDIO_Host_EnumerationFailure_ErrorCodes_t
\r
110 AUDIO_ENUMERROR_NoError = 0, /**< Configuration Descriptor was processed successfully. */
\r
111 AUDIO_ENUMERROR_InvalidConfigDescriptor = 1, /**< The device returned an invalid Configuration Descriptor. */
\r
112 AUDIO_ENUMERROR_NoCompatibleInterfaceFound = 2, /**< A compatible AUDIO interface was not found in the device's Configuration Descriptor. */
\r
113 AUDIO_ENUMERROR_PipeConfigurationFailed = 3, /**< One or more pipes for the specified interface could not be configured correctly. */
\r
116 /* Function Prototypes: */
\r
118 * @brief Host interface configuration routine, to configure a given Audio host interface instance using the Configuration
\r
119 * Descriptor read from an attached USB device. This function automatically updates the given Audio Host instance's
\r
120 * state values and configures the pipes required to communicate with the interface if it is found within the
\r
121 * device. This should be called once after the stack has enumerated the attached device, while the host state
\r
122 * machine is in the Addressed state.
\r
124 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class host configuration and state.
\r
125 * @param ConfigDescriptorSize : Length of the attached device's Configuration Descriptor.
\r
126 * @param DeviceConfigDescriptor : Pointer to a buffer containing the attached device's Configuration Descriptor.
\r
128 * @return A value from the @ref AUDIO_Host_EnumerationFailure_ErrorCodes_t enum.
\r
130 uint8_t Audio_Host_ConfigurePipes(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
\r
131 uint16_t ConfigDescriptorSize,
\r
132 void* DeviceConfigDescriptor) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(3);
\r
135 * @brief Starts or stops the audio streaming for the given configured Audio Host interface, allowing for audio samples to be
\r
136 * send and/or received.
\r
138 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class host configuration and state.
\r
139 * @param EnableStreaming : Boolean true to enable streaming of the specified interface, false to disable
\r
141 * @return A value from the @ref USB_Host_SendControlErrorCodes_t enum.
\r
143 uint8_t Audio_Host_StartStopStreaming(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
\r
144 const bool EnableStreaming) ATTR_NON_NULL_PTR_ARG(1);
\r
146 /** @brief Gets or sets the specified property of a streaming audio class endpoint that is bound to a pipe in the given
\r
149 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class host configuration and state.
\r
150 * @param DataPipeIndex : Index of the data pipe whose bound endpoint is to be altered.
\r
151 * @param EndpointProperty : Property of the endpoint to get or set, a value from @ref Audio_ClassRequests_t.
\r
152 * @param EndpointControl : Parameter of the endpoint to get or set, a value from @ref Audio_EndpointControls_t.
\r
153 * @param DataLength : For SET operations, the length of the parameter data to set. For GET operations, the maximum
\r
154 * length of the retrieved data.
\r
155 * @param Data : Pointer to a location where the parameter data is stored for SET operations, or where
\r
156 * the retrieved data is to be stored for GET operations.
\r
158 * @return A value from the @ref USB_Host_SendControlErrorCodes_t enum.
\r
160 uint8_t Audio_Host_GetSetEndpointProperty(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
\r
161 const uint8_t DataPipeIndex,
\r
162 const uint8_t EndpointProperty,
\r
163 const uint8_t EndpointControl,
\r
164 const uint16_t DataLength,
\r
165 void* const Data) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(6);
\r
167 /* Inline Functions: */
\r
168 /** @brief General management task for a given Audio host class interface, required for the correct operation of
\r
169 * the interface. This should be called frequently in the main program loop, before the master USB management task
\r
170 * @ref USB_USBTask().
\r
172 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class host configuration and state.
\r
175 static inline void Audio_Host_USBTask(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
\r
176 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
\r
177 static inline void Audio_Host_USBTask(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
\r
179 (void)AudioInterfaceInfo;
\r
182 /** @brief Determines if the given audio interface is ready for a sample to be read from it, and selects the streaming
\r
183 * IN pipe ready for reading.
\r
185 * @pre This function must only be called when the Host state machine is in the @ref HOST_STATE_Configured state or
\r
186 * the call will fail.
\r
188 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class configuration and state.
\r
190 * @return Boolean \c true if the given Audio interface has a sample to be read, \c false otherwise.
\r
192 static inline bool Audio_Host_IsSampleReceived(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
\r
193 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
\r
194 static inline bool Audio_Host_IsSampleReceived(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
\r
196 uint8_t portnum = AudioInterfaceInfo->Config.PortNumber;
\r
197 bool SampleReceived = false;
\r
199 if ((USB_HostState[portnum] != HOST_STATE_Configured) || !(AudioInterfaceInfo->State.IsActive))
\r
202 Pipe_SelectPipe(portnum,AudioInterfaceInfo->Config.DataINPipeNumber);
\r
204 SampleReceived = Pipe_IsINReceived(portnum);
\r
207 return SampleReceived;
\r
210 /** @brief Determines if the given audio interface is ready to accept the next sample to be written to it, and selects
\r
211 * the streaming OUT pipe ready for writing.
\r
213 * @pre This function must only be called when the Host state machine is in the @ref HOST_STATE_Configured state or
\r
214 * the call will fail.
\r
216 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class configuration and state.
\r
218 * @return Boolean \c true if the given Audio interface is ready to accept the next sample, \c false otherwise.
\r
220 static inline bool Audio_Host_IsReadyForNextSample(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
\r
221 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
\r
222 static inline bool Audio_Host_IsReadyForNextSample(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
\r
224 uint8_t portnum = AudioInterfaceInfo->Config.PortNumber;
\r
226 if ((USB_HostState[portnum] != HOST_STATE_Configured) || !(AudioInterfaceInfo->State.IsActive))
\r
229 Pipe_SelectPipe(portnum,AudioInterfaceInfo->Config.DataOUTPipeNumber);
\r
230 return Pipe_IsOUTReady(portnum);
\r
233 /** @brief Reads the next 8-bit audio sample from the current audio interface.
\r
235 * @pre This should be preceded immediately by a call to the @ref Audio_Host_IsSampleReceived() function to ensure
\r
236 * that the correct pipe is selected and ready for data.
\r
238 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class configuration and state.
\r
240 * @return Signed 8-bit audio sample from the audio interface.
\r
242 static inline int8_t Audio_Host_ReadSample8(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
\r
243 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
\r
244 static inline int8_t Audio_Host_ReadSample8(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
\r
247 uint8_t portnum = AudioInterfaceInfo->Config.PortNumber;
\r
249 (void)AudioInterfaceInfo;
\r
251 Sample = Pipe_Read_8(portnum);
\r
253 if (!(Pipe_BytesInPipe(portnum)))
\r
256 Pipe_ClearIN(portnum);
\r
263 /** @brief Reads the next 16-bit audio sample from the current audio interface.
\r
265 * @pre This should be preceded immediately by a call to the @ref Audio_Host_IsSampleReceived() function to ensure
\r
266 * that the correct pipe is selected and ready for data.
\r
268 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class configuration and state.
\r
270 * @return Signed 16-bit audio sample from the audio interface.
\r
272 static inline int16_t Audio_Host_ReadSample16(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
\r
273 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
\r
274 static inline int16_t Audio_Host_ReadSample16(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
\r
277 uint8_t portnum = AudioInterfaceInfo->Config.PortNumber;
\r
278 (void)AudioInterfaceInfo;
\r
280 Sample = (int16_t)Pipe_Read_16_LE(portnum);
\r
282 if (!(Pipe_BytesInPipe(portnum)))
\r
285 Pipe_ClearIN(portnum);
\r
292 /** @brief Reads the next 24-bit audio sample from the current audio interface.
\r
294 * @pre This should be preceded immediately by a call to the @ref Audio_Host_IsSampleReceived() function to ensure
\r
295 * that the correct pipe is selected and ready for data.
\r
297 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class configuration and state.
\r
299 * @return Signed 24-bit audio sample from the audio interface.
\r
301 static inline int32_t Audio_Host_ReadSample24(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
\r
302 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
\r
303 static inline int32_t Audio_Host_ReadSample24(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
\r
306 uint8_t portnum = AudioInterfaceInfo->Config.PortNumber;
\r
307 (void)AudioInterfaceInfo;
\r
309 Sample = (((uint32_t)Pipe_Read_8(portnum) << 16)
\r
310 | Pipe_Read_16_LE(portnum));
\r
312 if (!(Pipe_BytesInPipe(portnum)))
\r
315 Pipe_ClearIN(portnum);
\r
322 /** @brief Writes the next 8-bit audio sample to the current audio interface.
\r
324 * @pre This should be preceded immediately by a call to the @ref Audio_Host_IsReadyForNextSample() function to
\r
325 * ensure that the correct pipe is selected and ready for data.
\r
327 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class configuration and state.
\r
328 * @param Sample : Signed 8-bit audio sample.
\r
331 static inline void Audio_Host_WriteSample8(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
\r
332 const int8_t Sample) ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
\r
333 static inline void Audio_Host_WriteSample8(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
\r
334 const int8_t Sample)
\r
336 uint8_t portnum = AudioInterfaceInfo->Config.PortNumber;
\r
338 (void)AudioInterfaceInfo;
\r
340 Pipe_Write_8(portnum,Sample);
\r
342 if (!(Pipe_IsReadWriteAllowed(portnum)))
\r
345 Pipe_ClearOUT(portnum);
\r
346 Pipe_WaitUntilReady(portnum);
\r
351 /** @brief Writes the next 16-bit audio sample to the current audio interface.
\r
353 * @pre This should be preceded immediately by a call to the @ref Audio_Host_IsReadyForNextSample() function to
\r
354 * ensure that the correct pipe is selected and ready for data.
\r
356 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class configuration and state.
\r
357 * @param Sample : Signed 16-bit audio sample.
\r
360 static inline void Audio_Host_WriteSample16(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
\r
361 const int16_t Sample) ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
\r
362 static inline void Audio_Host_WriteSample16(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
\r
363 const int16_t Sample)
\r
365 uint8_t portnum = AudioInterfaceInfo->Config.PortNumber;
\r
367 (void)AudioInterfaceInfo;
\r
369 Pipe_Write_16_LE(portnum,Sample);
\r
371 if (!(Pipe_IsReadWriteAllowed(portnum)))
\r
374 Pipe_ClearOUT(portnum);
\r
375 Pipe_WaitUntilReady(portnum);
\r
380 /** @brief Writes the next 24-bit audio sample to the current audio interface.
\r
382 * @pre This should be preceded immediately by a call to the @ref Audio_Host_IsReadyForNextSample() function to
\r
383 * ensure that the correct pipe is selected and ready for data.
\r
385 * @param AudioInterfaceInfo : Pointer to a structure containing an Audio Class configuration and state.
\r
386 * @param Sample : Signed 24-bit audio sample.
\r
389 static inline void Audio_Host_WriteSample24(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
\r
390 const int32_t Sample) ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
\r
391 static inline void Audio_Host_WriteSample24(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
\r
392 const int32_t Sample)
\r
394 uint8_t portnum = AudioInterfaceInfo->Config.PortNumber;
\r
396 (void)AudioInterfaceInfo;
\r
398 Pipe_Write_16_LE(portnum,Sample);
\r
399 Pipe_Write_8(portnum,Sample >> 16);
\r
401 if (!(Pipe_IsReadWriteAllowed(portnum)))
\r
404 Pipe_ClearOUT(portnum);
\r
405 Pipe_WaitUntilReady(portnum);
\r
410 /* Private Interface - For use in library only: */
\r
411 #if !defined(__DOXYGEN__)
\r
412 /* Function Prototypes: */
\r
413 #if defined(__INCLUDE_FROM_AUDIO_HOST_C)
\r
414 static uint8_t DCOMP_Audio_Host_NextAudioControlInterface(void* CurrentDescriptor)
\r
415 ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);
\r
416 static uint8_t DCOMP_Audio_Host_NextAudioStreamInterface(void* CurrentDescriptor)
\r
417 ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);
\r
418 static uint8_t DCOMP_Audio_Host_NextAudioInterfaceDataEndpoint(void* CurrentDescriptor)
\r
419 ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);
\r
423 /* Disable C linkage for C++ Compilers: */
\r
424 #if defined(__cplusplus)
\r