Merge PCB updates
[SCSI2SD.git] / software / SCSI2SD / v4 / SCSI2SD.cydsn / Generated_Source / PSoC5 / USBFS_audio.c
1 /*******************************************************************************
2 * File Name: USBFS_audio.c
3 * Version 2.60
4 *
5 * Description:
6 *  USB AUDIO Class request handler.
7 *
8 * Note:
9 *
10 ********************************************************************************
11 * Copyright 2008-2013, Cypress Semiconductor Corporation.  All rights reserved.
12 * You may use this file only in accordance with the license, terms, conditions,
13 * disclaimers, and limitations in the end user license agreement accompanying
14 * the software package with which this file was provided.
15 *******************************************************************************/
16
17 #include "USBFS.h"
18
19 #if defined(USBFS_ENABLE_AUDIO_CLASS)
20
21 #include "USBFS_audio.h"
22 #include "USBFS_pvt.h"
23 #if defined(USBFS_ENABLE_MIDI_STREAMING) 
24     #include "USBFS_midi.h"
25 #endif /* End USBFS_ENABLE_MIDI_STREAMING*/
26
27
28 /***************************************
29 * Custom Declarations
30 ***************************************/
31
32 /* `#START CUSTOM_DECLARATIONS` Place your declaration here */
33
34 /* `#END` */
35
36
37 #if !defined(USER_SUPPLIED_AUDIO_HANDLER)
38
39
40 /***************************************
41 *    AUDIO Variables
42 ***************************************/
43
44 #if defined(USBFS_ENABLE_AUDIO_STREAMING)
45     volatile uint8 USBFS_currentSampleFrequency[USBFS_MAX_EP][USBFS_SAMPLE_FREQ_LEN];
46     volatile uint8 USBFS_frequencyChanged;
47     volatile uint8 USBFS_currentMute;
48     volatile uint8 USBFS_currentVolume[USBFS_VOLUME_LEN];
49     volatile uint8 USBFS_minimumVolume[USBFS_VOLUME_LEN] = {USBFS_VOL_MIN_LSB,
50                                                                                   USBFS_VOL_MIN_MSB};
51     volatile uint8 USBFS_maximumVolume[USBFS_VOLUME_LEN] = {USBFS_VOL_MAX_LSB,
52                                                                                   USBFS_VOL_MAX_MSB};
53     volatile uint8 USBFS_resolutionVolume[USBFS_VOLUME_LEN] = {USBFS_VOL_RES_LSB,
54                                                                                      USBFS_VOL_RES_MSB};
55 #endif /* End USBFS_ENABLE_AUDIO_STREAMING */
56
57
58 /*******************************************************************************
59 * Function Name: USBFS_DispatchAUDIOClassRqst
60 ********************************************************************************
61 *
62 * Summary:
63 *  This routine dispatches class requests
64 *
65 * Parameters:
66 *  None.
67 *
68 * Return:
69 *  requestHandled
70 *
71 * Global variables:
72 *   USBFS_currentSampleFrequency: Contains the current audio Sample
73 *       Frequency. It is set by the Host using SET_CUR request to the endpoint.
74 *   USBFS_frequencyChanged: This variable is used as a flag for the
75 *       user code, to be aware that Host has been sent request for changing
76 *       Sample Frequency. Sample frequency will be sent on the next OUT
77 *       transaction. It is contains endpoint address when set. The following
78 *       code is recommended for detecting new Sample Frequency in main code:
79 *       if((USBFS_frequencyChanged != 0) &&
80 *       (USBFS_transferState == USBFS_TRANS_STATE_IDLE))
81 *       {
82 *          USBFS_frequencyChanged = 0;
83 *       }
84 *       USBFS_transferState variable is checked to be sure that
85 *             transfer completes.
86 *   USBFS_currentMute: Contains mute configuration set by Host.
87 *   USBFS_currentVolume: Contains volume level set by Host.
88 *
89 * Reentrant:
90 *  No.
91 *
92 *******************************************************************************/
93 uint8 USBFS_DispatchAUDIOClassRqst(void) 
94 {
95     uint8 requestHandled = USBFS_FALSE;
96
97     #if defined(USBFS_ENABLE_AUDIO_STREAMING)
98         uint8 epNumber;
99         epNumber = CY_GET_REG8(USBFS_wIndexLo) & USBFS_DIR_UNUSED;
100     #endif /* End USBFS_ENABLE_AUDIO_STREAMING */
101
102     if ((CY_GET_REG8(USBFS_bmRequestType) & USBFS_RQST_DIR_MASK) == USBFS_RQST_DIR_D2H)
103     {
104         /* Control Read */
105         if((CY_GET_REG8(USBFS_bmRequestType) & USBFS_RQST_RCPT_MASK) == \
106                                                                                     USBFS_RQST_RCPT_EP)
107         {
108             /* Endpoint */
109             switch (CY_GET_REG8(USBFS_bRequest))
110             {
111                 case USBFS_GET_CUR:
112                 #if defined(USBFS_ENABLE_AUDIO_STREAMING)
113                     if(CY_GET_REG8(USBFS_wValueHi) == USBFS_SAMPLING_FREQ_CONTROL)
114                     {
115                          /* Endpoint Control Selector is Sampling Frequency */
116                         USBFS_currentTD.wCount = USBFS_SAMPLE_FREQ_LEN;
117                         USBFS_currentTD.pData  = USBFS_currentSampleFrequency[epNumber];
118                         requestHandled   = USBFS_InitControlRead();
119                     }
120                 #endif /* End USBFS_ENABLE_AUDIO_STREAMING */
121
122                 /* `#START AUDIO_READ_REQUESTS` Place other request handler here */
123
124                 /* `#END` */
125                     break;
126                 default:
127                     break;
128             }
129         }
130         else if((CY_GET_REG8(USBFS_bmRequestType) & USBFS_RQST_RCPT_MASK) == \
131                                                                                     USBFS_RQST_RCPT_IFC)
132         {
133             /* Interface or Entity ID */
134             switch (CY_GET_REG8(USBFS_bRequest))
135             {
136                 case USBFS_GET_CUR:
137                 #if defined(USBFS_ENABLE_AUDIO_STREAMING)
138                     if(CY_GET_REG8(USBFS_wValueHi) == USBFS_MUTE_CONTROL)
139                     {
140                         /* `#START MUTE_CONTROL_GET_REQUEST` Place multi-channel handler here */
141
142                         /* `#END` */
143                         
144                          /* Entity ID Control Selector is MUTE */
145                         USBFS_currentTD.wCount = 1u;
146                         USBFS_currentTD.pData  = &USBFS_currentMute;
147                         requestHandled   = USBFS_InitControlRead();
148                     }
149                     else if(CY_GET_REG8(USBFS_wValueHi) == USBFS_VOLUME_CONTROL)
150                     {
151                         /* `#START VOLUME_CONTROL_GET_REQUEST` Place multi-channel handler here */
152
153                         /* `#END` */
154
155                         /* Entity ID Control Selector is VOLUME, */
156                         USBFS_currentTD.wCount = USBFS_VOLUME_LEN;
157                         USBFS_currentTD.pData  = USBFS_currentVolume;
158                         requestHandled   = USBFS_InitControlRead();
159                     }
160                     else
161                     {
162                         /* `#START OTHER_GET_CUR_REQUESTS` Place other request handler here */
163
164                         /* `#END` */
165                     }
166                     break;
167                 case USBFS_GET_MIN:    /* GET_MIN */
168                     if(CY_GET_REG8(USBFS_wValueHi) == USBFS_VOLUME_CONTROL)
169                     {
170                         /* Entity ID Control Selector is VOLUME, */
171                         USBFS_currentTD.wCount = USBFS_VOLUME_LEN;
172                         USBFS_currentTD.pData  = &USBFS_minimumVolume[0];
173                         requestHandled   = USBFS_InitControlRead();
174                     }
175                     break;
176                 case USBFS_GET_MAX:    /* GET_MAX */
177                     if(CY_GET_REG8(USBFS_wValueHi) == USBFS_VOLUME_CONTROL)
178                     {
179                         /* Entity ID Control Selector is VOLUME, */
180                         USBFS_currentTD.wCount = USBFS_VOLUME_LEN;
181                         USBFS_currentTD.pData  = &USBFS_maximumVolume[0];
182                         requestHandled   = USBFS_InitControlRead();
183                     }
184                     break;
185                 case USBFS_GET_RES:    /* GET_RES */
186                     if(CY_GET_REG8(USBFS_wValueHi) == USBFS_VOLUME_CONTROL)
187                     {
188                          /* Entity ID Control Selector is VOLUME, */
189                         USBFS_currentTD.wCount = USBFS_VOLUME_LEN;
190                         USBFS_currentTD.pData  = &USBFS_resolutionVolume[0];
191                         requestHandled   = USBFS_InitControlRead();
192                     }
193                     break;
194                 /* The contents of the status message is reserved for future use.
195                 *  For the time being, a null packet should be returned in the data stage of the
196                 *  control transfer, and the received null packet should be ACKed.
197                 */
198                 case USBFS_GET_STAT:
199                         USBFS_currentTD.wCount = 0u;
200                         requestHandled   = USBFS_InitControlWrite();
201
202                 #endif /* End USBFS_ENABLE_AUDIO_STREAMING */
203
204                 /* `#START AUDIO_WRITE_REQUESTS` Place other request handler here */
205
206                 /* `#END` */
207                     break;
208                 default:
209                     break;
210             }
211         }
212         else
213         {   /* USBFS_RQST_RCPT_OTHER */
214         }
215     }
216     else if ((CY_GET_REG8(USBFS_bmRequestType) & USBFS_RQST_DIR_MASK) == \
217                                                                                     USBFS_RQST_DIR_H2D)
218     {
219         /* Control Write */
220         if((CY_GET_REG8(USBFS_bmRequestType) & USBFS_RQST_RCPT_MASK) == \
221                                                                                     USBFS_RQST_RCPT_EP)
222         {
223             /* Endpoint */
224             switch (CY_GET_REG8(USBFS_bRequest))
225             {
226                 case USBFS_SET_CUR:
227                 #if defined(USBFS_ENABLE_AUDIO_STREAMING)
228                     if(CY_GET_REG8(USBFS_wValueHi) == USBFS_SAMPLING_FREQ_CONTROL)
229                     {
230                          /* Endpoint Control Selector is Sampling Frequency */
231                         USBFS_currentTD.wCount = USBFS_SAMPLE_FREQ_LEN;
232                         USBFS_currentTD.pData  = USBFS_currentSampleFrequency[epNumber];
233                         requestHandled   = USBFS_InitControlWrite();
234                         USBFS_frequencyChanged = epNumber;
235                     }
236                 #endif /* End USBFS_ENABLE_AUDIO_STREAMING */
237
238                 /* `#START AUDIO_SAMPLING_FREQ_REQUESTS` Place other request handler here */
239
240                 /* `#END` */
241                     break;
242                 default:
243                     break;
244             }
245         }
246         else if((CY_GET_REG8(USBFS_bmRequestType) & USBFS_RQST_RCPT_MASK) == \
247                                                                                     USBFS_RQST_RCPT_IFC)
248         {
249             /* Interface or Entity ID */
250             switch (CY_GET_REG8(USBFS_bRequest))
251             {
252                 case USBFS_SET_CUR:
253                 #if defined(USBFS_ENABLE_AUDIO_STREAMING)
254                     if(CY_GET_REG8(USBFS_wValueHi) == USBFS_MUTE_CONTROL)
255                     {
256                         /* `#START MUTE_SET_REQUEST` Place multi-channel handler here */
257
258                         /* `#END` */
259
260                         /* Entity ID Control Selector is MUTE */
261                         USBFS_currentTD.wCount = 1u;
262                         USBFS_currentTD.pData  = &USBFS_currentMute;
263                         requestHandled   = USBFS_InitControlWrite();
264                     }
265                     else if(CY_GET_REG8(USBFS_wValueHi) == USBFS_VOLUME_CONTROL)
266                     {
267                         /* `#START VOLUME_CONTROL_SET_REQUEST` Place multi-channel handler here */
268
269                         /* `#END` */
270
271                         /* Entity ID Control Selector is VOLUME */
272                         USBFS_currentTD.wCount = USBFS_VOLUME_LEN;
273                         USBFS_currentTD.pData  = USBFS_currentVolume;
274                         requestHandled   = USBFS_InitControlWrite();
275                     }
276                     else
277                     {
278                         /* `#START OTHER_SET_CUR_REQUESTS` Place other request handler here */
279
280                         /* `#END` */
281                     }
282                 #endif /* End USBFS_ENABLE_AUDIO_STREAMING */
283
284                 /* `#START AUDIO_CONTROL_SEL_REQUESTS` Place other request handler here */
285
286                 /* `#END` */
287                     break;
288                 default:
289                     break;
290             }
291         }
292         else
293         {   /* USBFS_RQST_RCPT_OTHER */
294         }
295     }
296     else
297     {   /* requestHandled is initialized as FALSE by default */
298     }
299
300     return(requestHandled);
301 }
302
303
304 #endif /* USER_SUPPLIED_AUDIO_HANDLER */
305
306
307 /*******************************************************************************
308 * Additional user functions supporting AUDIO Requests
309 ********************************************************************************/
310
311 /* `#START AUDIO_FUNCTIONS` Place any additional functions here */
312
313 /* `#END` */
314
315 #endif  /* End USBFS_ENABLE_AUDIO_CLASS*/
316
317
318 /* [] END OF FILE */