1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Device Audio Class                                                  */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define UX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "ux_api.h"
28 #include "ux_device_class_audio.h"
29 #include "ux_device_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_device_class_audio_sample_read32                PORTABLE C      */
37 /*                                                           6.2.1        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function reads 32-bit sample from the Audio class.             */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    stream                                Address of audio stream       */
49 /*                                            instance                    */
50 /*    buffer                                Pointer to buffer to save     */
51 /*                                            sample data                 */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    None                                                                */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    Application                                                         */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
69 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
70 /*                                            resulting in version 6.1    */
71 /*  03-08-2023     Chaoqiong Xiao           Modified comment(s),          */
72 /*                                            resulting in version 6.2.1  */
73 /*                                                                        */
74 /**************************************************************************/
_ux_device_class_audio_sample_read32(UX_DEVICE_CLASS_AUDIO_STREAM * stream,ULONG * buffer)75 UINT _ux_device_class_audio_sample_read32(UX_DEVICE_CLASS_AUDIO_STREAM *stream, ULONG *buffer)
76 {
77 
78 UX_SLAVE_ENDPOINT           *endpoint;
79 UX_SLAVE_DEVICE             *device;
80 UCHAR                       *sample_ptr;
81 UCHAR                       *next_frame_buffer;
82 ULONG                       next_frame_sample;
83 
84 
85     /* Get the pointer to the device.  */
86     device =  &_ux_system_slave -> ux_system_slave_device;
87 
88     /* As long as the device is in the CONFIGURED state.  */
89     if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
90     {
91 
92         /* Error trap. */
93         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONFIGURATION_HANDLE_UNKNOWN);
94 
95         /* Cannot proceed with command, the interface is down.  */
96         return(UX_CONFIGURATION_HANDLE_UNKNOWN);
97     }
98 
99     /* Check if endpoint is available.  */
100     endpoint = stream -> ux_device_class_audio_stream_endpoint;
101     if (endpoint == UX_NULL)
102         return(UX_ERROR);
103 
104     /* Check if endpoint direction is OK.  */
105     if ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) != UX_ENDPOINT_OUT)
106         return(UX_ERROR);
107 
108     /* Underflow!!  */
109     if (stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_length == 0)
110     {
111         return(UX_BUFFER_OVERFLOW);
112     }
113 
114     /* Try to read a sample.  */
115     sample_ptr = stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_data +
116                     stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_pos;
117     if (buffer)
118         *buffer = *(ULONG *)(sample_ptr);
119 
120     /* Update sample read state.  */
121     next_frame_sample = stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_pos + 4;
122     if (next_frame_sample >= stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_length)
123     {
124 
125         /* Set frame length to 0 to indicate no data.  */
126         stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_length = 0;
127 
128         /* Move to next frame buffer.  */
129         next_frame_sample = 0;
130 
131         /* Move frame if it's not the last one.  */
132         if (stream -> ux_device_class_audio_stream_access_pos != stream -> ux_device_class_audio_stream_transfer_pos)
133         {
134             next_frame_buffer = (UCHAR *)stream -> ux_device_class_audio_stream_access_pos;
135             next_frame_buffer += stream -> ux_device_class_audio_stream_frame_buffer_size;
136             if (next_frame_buffer >= stream -> ux_device_class_audio_stream_buffer + stream -> ux_device_class_audio_stream_buffer_size)
137                 next_frame_buffer = stream -> ux_device_class_audio_stream_buffer;
138             stream -> ux_device_class_audio_stream_access_pos = (UX_DEVICE_CLASS_AUDIO_FRAME *)next_frame_buffer;
139         }
140     }
141 
142     /* Update next sample position.  */
143     stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_pos = next_frame_sample;
144 
145     return(UX_SUCCESS);
146 }
147 
148 
149 /**************************************************************************/
150 /*                                                                        */
151 /*  FUNCTION                                               RELEASE        */
152 /*                                                                        */
153 /*    _uxe_device_class_audio_sample_read32                PORTABLE C     */
154 /*                                                            6.2.1       */
155 /*  AUTHOR                                                                */
156 /*                                                                        */
157 /*    Chaoqiong Xiao, Microsoft Corporation                               */
158 /*                                                                        */
159 /*  DESCRIPTION                                                           */
160 /*                                                                        */
161 /*    This function checks errors in reading 32-bit sample function call. */
162 /*                                                                        */
163 /*  INPUT                                                                 */
164 /*                                                                        */
165 /*    stream                                Address of audio stream       */
166 /*                                            instance                    */
167 /*    buffer                                Pointer to buffer to save     */
168 /*                                            sample data                 */
169 /*                                                                        */
170 /*  OUTPUT                                                                */
171 /*                                                                        */
172 /*    None                                                                */
173 /*                                                                        */
174 /*  CALLS                                                                 */
175 /*                                                                        */
176 /*    _ux_device_class_audio_sample_read32   Read 32-bit sample           */
177 /*                                                                        */
178 /*  CALLED BY                                                             */
179 /*                                                                        */
180 /*    Application                                                         */
181 /*                                                                        */
182 /*  RELEASE HISTORY                                                       */
183 /*                                                                        */
184 /*    DATE              NAME                      DESCRIPTION             */
185 /*                                                                        */
186 /*  03-08-2023     Chaoqiong Xiao           Initial Version 6.2.1         */
187 /*                                                                        */
188 /**************************************************************************/
_uxe_device_class_audio_sample_read32(UX_DEVICE_CLASS_AUDIO_STREAM * stream,ULONG * buffer)189 UINT _uxe_device_class_audio_sample_read32(UX_DEVICE_CLASS_AUDIO_STREAM *stream,
190                                          ULONG *buffer)
191 {
192 
193     /* Sanity check.  */
194     if (stream == UX_NULL)
195         return(UX_INVALID_PARAMETER);
196 
197     /* Read 32-bit sample.  */
198     return(_ux_device_class_audio_sample_read32(stream, buffer));
199 }
200