]> git.sur5r.net Git - freertos/blob
fb469cb9e332fbbab9e336ec167e806181a719f9
[freertos] /
1 /*\r
2  * @brief Device mode driver for the library USB Audio 1.0 Class driver\r
3  *\r
4  * @note\r
5  * Copyright(C) NXP Semiconductors, 2012\r
6  * Copyright(C) Dean Camera, 2011, 2012\r
7  * All rights reserved.\r
8  *\r
9  * @par\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
23  *\r
24  * @par\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
30  * this code.\r
31  */\r
32 \r
33 /** @ingroup Group_USBClassAudio\r
34  *  @defgroup Group_USBClassAudioDevice Audio 1.0 Class Device Mode Driver\r
35  *\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
39  *\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
42  *\r
43  *  @{\r
44  */\r
45 \r
46 #ifndef _AUDIO_CLASS_DEVICE_H_\r
47 #define _AUDIO_CLASS_DEVICE_H_\r
48 \r
49         /* Includes: */\r
50                 #include "../../USB.h"\r
51                 #include "../Common/AudioClassCommon.h"\r
52 \r
53         /* Enable C linkage for C++ Compilers: */\r
54                 #if defined(__cplusplus)\r
55                         extern "C" {\r
56                 #endif\r
57 \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
61                 #endif\r
62 \r
63         /* Public Interface - May be used in end-application: */\r
64                 /* Type Defines: */\r
65                         /** @brief Audio Class Device Mode Configuration and State Structure.\r
66                          *\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
70                          */\r
71                         typedef struct\r
72                         {\r
73                                 const struct\r
74                                 {\r
75                                         uint8_t  ControlInterfaceNumber; /**< Index of the Audio Control interface within the device this\r
76                                                                                                                 *   structure controls.\r
77                                                                                                                 */\r
78                                         uint8_t  StreamingInterfaceNumber; /**< Index of the Audio Streaming interface within the device this\r
79                                                                                                                 *   structure controls.\r
80                                                                                                                 */\r
81 \r
82                                         uint8_t  DataINEndpointNumber; /**< Endpoint number of the incoming Audio Streaming data, if available\r
83                                                                                                         *   (zero if unused).\r
84                                                                                                         */\r
85                                         uint16_t DataINEndpointSize; /**< Size in bytes of the incoming Audio Streaming data endpoint, if available\r
86                                                                                                   *   (zero if unused).\r
87                                                                                                   */\r
88 \r
89                                         uint8_t  DataOUTEndpointNumber; /**< Endpoint number of the outgoing Audio Streaming data, if available\r
90                                                                                                          *   (zero if unused).\r
91                                                                                                          */\r
92                                         uint16_t DataOUTEndpointSize; /**< Size in bytes of the outgoing Audio Streaming data endpoint, if available\r
93                                                                                                    *   (zero if unused).\r
94                                                                                                    */\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
98                                            */\r
99                                 struct\r
100                                 {\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
103                                                                 */\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
106                                           */\r
107                         } USB_ClassInfo_Audio_Device_t;\r
108 \r
109                 /* Function Prototypes: */\r
110 \r
111                         /**\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
115                          *\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
118                          */\r
119                         bool Audio_Device_ConfigureEndpoints(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);\r
120 \r
121                         /**\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
124                          *\r
125                          * @param       AudioInterfaceInfo      : Pointer to a structure containing an Audio Class configuration and state.\r
126                          * @return      Nothing\r
127                          */\r
128                         void Audio_Device_ProcessControlRequest(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);\r
129                         \r
130                         /**\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
133                          *\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
137                          *\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
140                          *\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
151                          */\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
157                                                                           uint8_t* Data);\r
158 \r
159                         /**\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
163                          *\r
164                          * @param       AudioInterfaceInfo      : Pointer to a structure containing an Audio Class configuration and state.\r
165                          * @return      Nothing\r
166                          */\r
167                         void EVENT_Audio_Device_StreamStartStop(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo);\r
168 \r
169                 /* Inline Functions: */\r
170                         /**\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
173                          *\r
174                          * @param       AudioInterfaceInfo      : Pointer to a structure containing an Audio Class configuration and state.\r
175                          * @return      Nothing\r
176                          */\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
181                         {\r
182                                 (void)AudioInterfaceInfo;\r
183                         }\r
184 \r
185                         /**\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
188                          *\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
191                          *\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
194                          */\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
199                         {\r
200                                 if ((USB_DeviceState[AudioInterfaceInfo->Config.PortNumber] != DEVICE_STATE_Configured) || !(AudioInterfaceInfo->State.InterfaceEnabled))\r
201                                   return false;\r
202 \r
203                                 Endpoint_SelectEndpoint(AudioInterfaceInfo->Config.PortNumber, AudioInterfaceInfo->Config.DataOUTEndpointNumber);\r
204                                 return Endpoint_IsOUTReceived(AudioInterfaceInfo->Config.PortNumber);\r
205                         }\r
206 \r
207                         /**\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
210                          *\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
213                          *\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
216                          */\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
221                         {\r
222                                 if ((USB_DeviceState[AudioInterfaceInfo->Config.PortNumber] != DEVICE_STATE_Configured) || !(AudioInterfaceInfo->State.InterfaceEnabled))\r
223                                   return false;\r
224 \r
225                                 Endpoint_SelectEndpoint(AudioInterfaceInfo->Config.PortNumber, AudioInterfaceInfo->Config.DataINEndpointNumber);\r
226                                 return Endpoint_IsINReady(USB_DeviceState[AudioInterfaceInfo->Config.PortNumber]);\r
227                         }\r
228 \r
229                         /**\r
230                          * @brief       Reads the next 8-bit audio sample from the current audio interface.\r
231                          *\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
234                          *\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
237                          */\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
242                         {\r
243                                 int8_t Sample;\r
244 \r
245                                 (void)AudioInterfaceInfo;\r
246 \r
247                                 Sample = Endpoint_Read_8(AudioInterfaceInfo->Config.PortNumber);\r
248 \r
249                                 if (!(Endpoint_BytesInEndpoint(AudioInterfaceInfo->Config.PortNumber)))\r
250                                   Endpoint_ClearOUT(AudioInterfaceInfo->Config.PortNumber);\r
251 \r
252                                 return Sample;\r
253                         }\r
254 \r
255                         /**\r
256                          * @brief       Reads the next 16-bit audio sample from the current audio interface.\r
257                          *\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
260                          *\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
263                          */\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
268                         {\r
269                                 int16_t Sample;\r
270 \r
271                                 (void)AudioInterfaceInfo;\r
272 \r
273                                 Sample = (int16_t)Endpoint_Read_16_LE(AudioInterfaceInfo->Config.PortNumber);\r
274 \r
275                                 if (!(Endpoint_BytesInEndpoint(AudioInterfaceInfo->Config.PortNumber)))\r
276                                   Endpoint_ClearOUT(AudioInterfaceInfo->Config.PortNumber);\r
277 \r
278                                 return Sample;\r
279                         }\r
280 \r
281                         /**\r
282                          * @brief       Reads the next 24-bit audio sample from the current audio interface.\r
283                          *\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
286                          *\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
289                          */\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
294                         {\r
295                                 int32_t Sample;\r
296 \r
297                                 (void)AudioInterfaceInfo;\r
298 \r
299                                 Sample = (((uint32_t)Endpoint_Read_8(AudioInterfaceInfo->Config.PortNumber) << 16) | Endpoint_Read_16_LE(AudioInterfaceInfo->Config.PortNumber));\r
300 \r
301                                 if (!(Endpoint_BytesInEndpoint(AudioInterfaceInfo->Config.PortNumber)))\r
302                                   Endpoint_ClearOUT(AudioInterfaceInfo->Config.PortNumber);\r
303 \r
304                                 return Sample;\r
305                         }\r
306 \r
307                         /**\r
308                          * @brief       Writes the next 8-bit audio sample to the current audio interface.\r
309                          *\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
312                          *\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
315                          * @return      Nothing\r
316                          */\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
322                         {\r
323                                 Endpoint_Write_8(AudioInterfaceInfo->Config.PortNumber, Sample);\r
324 \r
325                                 if (Endpoint_BytesInEndpoint(AudioInterfaceInfo->Config.PortNumber) == AudioInterfaceInfo->Config.DataINEndpointSize)\r
326                                   Endpoint_ClearIN(AudioInterfaceInfo->Config.PortNumber);\r
327                         }\r
328 \r
329                         /**\r
330                          * @brief       Writes the next 16-bit audio sample to the current audio interface.\r
331                          *\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
334                          *\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
337                          * @return      Nothing\r
338                          */\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
344                         {\r
345                                 Endpoint_Write_16_LE(AudioInterfaceInfo->Config.PortNumber, Sample);\r
346 \r
347                                 if (Endpoint_BytesInEndpoint(AudioInterfaceInfo->Config.PortNumber) == AudioInterfaceInfo->Config.DataINEndpointSize)\r
348                                   Endpoint_ClearIN(AudioInterfaceInfo->Config.PortNumber);\r
349                         }\r
350 \r
351                         /**\r
352                          * @brief       Writes the next 24-bit audio sample to the current audio interface.\r
353                          *\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
356                          *\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
359                          * @return      Nothing\r
360                          */\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
366                         {\r
367                                 Endpoint_Write_16_LE(AudioInterfaceInfo->Config.PortNumber, Sample);\r
368                                 Endpoint_Write_8(AudioInterfaceInfo->Config.PortNumber, Sample >> 16);\r
369 \r
370                                 if (Endpoint_BytesInEndpoint(AudioInterfaceInfo->Config.PortNumber) == AudioInterfaceInfo->Config.DataINEndpointSize)\r
371                                   Endpoint_ClearIN(AudioInterfaceInfo->Config.PortNumber);\r
372                         }\r
373 \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
382                         #endif\r
383 \r
384         #endif  \r
385                         \r
386         \r
387         /* Disable C linkage for C++ Compilers: */\r
388                 #if defined(__cplusplus)\r
389                         }\r
390                 #endif\r
391 #endif\r
392 \r
393 /** @} */\r
394 \r