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_read16                PORTABLE C      */
37 /*                                                           6.2.1        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function reads 16-bit sample value 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_read16(UX_DEVICE_CLASS_AUDIO_STREAM * stream,USHORT * buffer)75 UINT _ux_device_class_audio_sample_read16(UX_DEVICE_CLASS_AUDIO_STREAM *stream,
76                                           USHORT *buffer)
77 {
78 
79 UX_SLAVE_ENDPOINT           *endpoint;
80 UX_SLAVE_DEVICE             *device;
81 UCHAR                       *sample_ptr;
82 UCHAR                       *next_frame_buffer;
83 ULONG                       next_frame_sample;
84 
85 
86     /* Get the pointer to the device.  */
87     device =  &_ux_system_slave -> ux_system_slave_device;
88 
89     /* As long as the device is in the CONFIGURED state.  */
90     if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
91     {
92 
93         /* Cannot proceed with command, the interface is down.  */
94         return(UX_CONFIGURATION_HANDLE_UNKNOWN);
95     }
96 
97     /* Check if endpoint is available.  */
98     endpoint = stream -> ux_device_class_audio_stream_endpoint;
99     if (endpoint == UX_NULL)
100         return(UX_ERROR);
101 
102     /* Check if endpoint direction is OK.  */
103     if ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) != UX_ENDPOINT_OUT)
104         return(UX_ERROR);
105 
106     /* Underflow!!  */
107     if (stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_length == 0)
108     {
109         return(UX_BUFFER_OVERFLOW);
110     }
111 
112     /* Try to read a sample.  */
113     sample_ptr = stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_data +
114                     stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_pos;
115     if (buffer)
116         *buffer = *(USHORT *)(sample_ptr);
117 
118     /* Update sample read state.  */
119     next_frame_sample = stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_pos + 2;
120     if (next_frame_sample >= stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_length)
121     {
122 
123         /* Set frame length to 0 to indicate no data.  */
124         stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_length = 0;
125 
126         /* Move to next frame buffer.  */
127         next_frame_sample = 0;
128 
129         /* Move frame if it's not the last one.  */
130         if (stream -> ux_device_class_audio_stream_access_pos != stream -> ux_device_class_audio_stream_transfer_pos)
131         {
132             next_frame_buffer = (UCHAR *)stream -> ux_device_class_audio_stream_access_pos;
133             next_frame_buffer += stream -> ux_device_class_audio_stream_frame_buffer_size;
134             if (next_frame_buffer >= stream -> ux_device_class_audio_stream_buffer + stream -> ux_device_class_audio_stream_buffer_size)
135                 next_frame_buffer = stream -> ux_device_class_audio_stream_buffer;
136             stream -> ux_device_class_audio_stream_access_pos = (UX_DEVICE_CLASS_AUDIO_FRAME *)next_frame_buffer;
137         }
138     }
139 
140     /* Update next sample position.  */
141     stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_pos = next_frame_sample;
142 
143     return(UX_SUCCESS);
144 }
145 
146 
147 /**************************************************************************/
148 /*                                                                        */
149 /*  FUNCTION                                               RELEASE        */
150 /*                                                                        */
151 /*    _uxe_device_class_audio_sample_read16                PORTABLE C     */
152 /*                                                            6.2.1       */
153 /*  AUTHOR                                                                */
154 /*                                                                        */
155 /*    Chaoqiong Xiao, Microsoft Corporation                               */
156 /*                                                                        */
157 /*  DESCRIPTION                                                           */
158 /*                                                                        */
159 /*    This function checks errors in reading 16-bit sample function call. */
160 /*                                                                        */
161 /*  INPUT                                                                 */
162 /*                                                                        */
163 /*    stream                                Address of audio stream       */
164 /*                                            instance                    */
165 /*    buffer                                Pointer to buffer to save     */
166 /*                                            sample data                 */
167 /*                                                                        */
168 /*  OUTPUT                                                                */
169 /*                                                                        */
170 /*    None                                                                */
171 /*                                                                        */
172 /*  CALLS                                                                 */
173 /*                                                                        */
174 /*    _ux_device_class_audio_sample_read16   Read 16-bit sample           */
175 /*                                                                        */
176 /*  CALLED BY                                                             */
177 /*                                                                        */
178 /*    Application                                                         */
179 /*                                                                        */
180 /*  RELEASE HISTORY                                                       */
181 /*                                                                        */
182 /*    DATE              NAME                      DESCRIPTION             */
183 /*                                                                        */
184 /*  03-08-2023     Chaoqiong Xiao           Initial Version 6.2.1         */
185 /*                                                                        */
186 /**************************************************************************/
_uxe_device_class_audio_sample_read16(UX_DEVICE_CLASS_AUDIO_STREAM * stream,USHORT * buffer)187 UINT _uxe_device_class_audio_sample_read16(UX_DEVICE_CLASS_AUDIO_STREAM *stream,
188                                          USHORT *buffer)
189 {
190 
191     /* Sanity check.  */
192     if (stream == UX_NULL)
193         return(UX_INVALID_PARAMETER);
194 
195     /* Read 16-bit sample.  */
196     return(_ux_device_class_audio_sample_read16(stream, buffer));
197 }
198