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_read8                 PORTABLE C      */
37 /*                                                           6.2.1        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function reads 8-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_read8(UX_DEVICE_CLASS_AUDIO_STREAM * stream,UCHAR * buffer)75 UINT _ux_device_class_audio_sample_read8(UX_DEVICE_CLASS_AUDIO_STREAM *stream,
76                                          UCHAR *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 = *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 + 1;
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 /*  FUNCTION                                               RELEASE        */
149 /*                                                                        */
150 /*    _uxe_device_class_audio_sample_read8                PORTABLE C      */
151 /*                                                           6.2.1        */
152 /*  AUTHOR                                                                */
153 /*                                                                        */
154 /*    Chaoqiong Xiao, Microsoft Corporation                               */
155 /*                                                                        */
156 /*  DESCRIPTION                                                           */
157 /*                                                                        */
158 /*    This function checks errors in reading 8-bit sample function call.  */
159 /*                                                                        */
160 /*  INPUT                                                                 */
161 /*                                                                        */
162 /*    stream                                Address of audio stream       */
163 /*                                            instance                    */
164 /*    buffer                                Pointer to buffer to save     */
165 /*                                            sample data                 */
166 /*                                                                        */
167 /*  OUTPUT                                                                */
168 /*                                                                        */
169 /*    None                                                                */
170 /*                                                                        */
171 /*  CALLS                                                                 */
172 /*                                                                        */
173 /*    _ux_device_class_audio_sample_read8   Read 8-bit sample             */
174 /*                                                                        */
175 /*  CALLED BY                                                             */
176 /*                                                                        */
177 /*    Application                                                         */
178 /*                                                                        */
179 /*  RELEASE HISTORY                                                       */
180 /*                                                                        */
181 /*    DATE              NAME                      DESCRIPTION             */
182 /*                                                                        */
183 /*  03-08-2023     Chaoqiong Xiao           Initial Version 6.2.1         */
184 /*                                                                        */
185 /**************************************************************************/
_uxe_device_class_audio_sample_read8(UX_DEVICE_CLASS_AUDIO_STREAM * stream,UCHAR * buffer)186 UINT _uxe_device_class_audio_sample_read8(UX_DEVICE_CLASS_AUDIO_STREAM *stream,
187                                          UCHAR *buffer)
188 {
189 
190     /* Sanity check.  */
191     if (stream == UX_NULL)
192         return(UX_INVALID_PARAMETER);
193 
194     /* Read 8-bit sample.  */
195     return(_ux_device_class_audio_sample_read8(stream, buffer));
196 }
197