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_transmission_start           PORTABLE C      */
37 /*                                                           6.2.1        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function start sending valid frames in the Audio class.        */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    stream                                Address of audio stream       */
49 /*                                            instance                    */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    None                                                                */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _ux_device_thread_resume              Resume thread used            */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    Application                                                         */
62 /*                                                                        */
63 /*  RELEASE HISTORY                                                       */
64 /*                                                                        */
65 /*    DATE              NAME                      DESCRIPTION             */
66 /*                                                                        */
67 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
68 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
69 /*                                            resulting in version 6.1    */
70 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            refined macros names,       */
72 /*                                            resulting in version 6.1.10 */
73 /*  10-31-2022     Yajun Xia                Modified comment(s),          */
74 /*                                            added standalone support,   */
75 /*                                            resulting in version 6.2.0  */
76 /*  03-08-2023     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            resulting in version 6.2.1  */
78 /*                                                                        */
79 /**************************************************************************/
_ux_device_class_audio_transmission_start(UX_DEVICE_CLASS_AUDIO_STREAM * stream)80 UINT _ux_device_class_audio_transmission_start(UX_DEVICE_CLASS_AUDIO_STREAM *stream)
81 {
82 
83 UX_SLAVE_ENDPOINT           *endpoint;
84 UX_SLAVE_DEVICE             *device;
85 
86 
87     /* Get the pointer to the device.  */
88     device =  &_ux_system_slave -> ux_system_slave_device;
89 
90     /* As long as the device is in the CONFIGURED state.  */
91     if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
92     {
93 
94         /* Cannot proceed with command, the interface is down.  */
95         return(UX_CONFIGURATION_HANDLE_UNKNOWN);
96     }
97 
98     /* Check if endpoint is available.  */
99     endpoint = stream -> ux_device_class_audio_stream_endpoint;
100     if (endpoint == UX_NULL)
101         return(UX_ERROR);
102 
103     /* Check if endpoint direction is OK.  */
104     if ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_OUT)
105         return(UX_ERROR);
106 
107     /* Check if there is frame to send (underflow).  */
108     if (stream -> ux_device_class_audio_stream_transfer_pos -> ux_device_class_audio_frame_length == 0)
109         return(UX_BUFFER_OVERFLOW);
110 
111 #if defined(UX_DEVICE_STANDALONE)
112 
113     /* Start write task.  */
114     if (stream -> ux_device_class_audio_stream_task_state == UX_DEVICE_CLASS_AUDIO_STREAM_RW_STOP)
115         stream -> ux_device_class_audio_stream_task_state = UX_DEVICE_CLASS_AUDIO_STREAM_RW_START;
116 #else
117 
118     /* Start write thread.  */
119     _ux_device_thread_resume(&stream -> ux_device_class_audio_stream_thread);
120 #endif
121     return(UX_SUCCESS);
122 }
123 
124 
125 /**************************************************************************/
126 /*                                                                        */
127 /*  FUNCTION                                               RELEASE        */
128 /*                                                                        */
129 /*    _uxe_device_class_audio_transmission_start          PORTABLE C      */
130 /*                                                           6.2.1        */
131 /*  AUTHOR                                                                */
132 /*                                                                        */
133 /*    Chaoqiong Xiao, Microsoft Corporation                               */
134 /*                                                                        */
135 /*  DESCRIPTION                                                           */
136 /*                                                                        */
137 /*    This function checks errors in starting sending function call.      */
138 /*                                                                        */
139 /*  INPUT                                                                 */
140 /*                                                                        */
141 /*    stream                                Address of audio stream       */
142 /*                                            instance                    */
143 /*                                                                        */
144 /*  OUTPUT                                                                */
145 /*                                                                        */
146 /*    None                                                                */
147 /*                                                                        */
148 /*  CALLS                                                                 */
149 /*                                                                        */
150 /*    _ux_device_class_audio_transmission_start                           */
151 /*                                          Start transmission            */
152 /*                                                                        */
153 /*  CALLED BY                                                             */
154 /*                                                                        */
155 /*    Application                                                         */
156 /*                                                                        */
157 /*  RELEASE HISTORY                                                       */
158 /*                                                                        */
159 /*    DATE              NAME                      DESCRIPTION             */
160 /*                                                                        */
161 /*  03-08-2023     Chaoqiong Xiao           Initial Version 6.2.1         */
162 /*                                                                        */
163 /**************************************************************************/
_uxe_device_class_audio_transmission_start(UX_DEVICE_CLASS_AUDIO_STREAM * stream)164 UINT _uxe_device_class_audio_transmission_start(UX_DEVICE_CLASS_AUDIO_STREAM *stream)
165 {
166 
167     /* Sanity check.  */
168     if (stream == UX_NULL)
169         return(UX_INVALID_PARAMETER);
170 
171     /* Start transmission.  */
172     return(_ux_device_class_audio_transmission_start(stream));
173 }
174