1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 /**************************************************************************/
12 /**************************************************************************/
13 /**                                                                       */
14 /** USBX Component                                                        */
15 /**                                                                       */
16 /**   Device Audio Class                                                  */
17 /**                                                                       */
18 /**************************************************************************/
19 /**************************************************************************/
20 
21 #define UX_SOURCE_CODE
22 
23 
24 /* Include necessary system files.  */
25 
26 #include "ux_api.h"
27 #include "ux_device_class_audio.h"
28 #include "ux_device_stack.h"
29 
30 
31 #if defined(UX_DEVICE_STANDALONE)
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_device_class_audio_tasks_run                    PORTABLE C      */
37 /*                                                           6.2.0        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yajun Xia, Microsoft Corporation                                    */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function is the background task of the audio.                  */
45 /*                                                                        */
46 /*    It's for standalone mode.                                           */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    instance                              Pointer to audio class        */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    State machine status                                                */
55 /*    UX_STATE_EXIT                         Device not configured         */
56 /*    UX_STATE_IDLE                         No streaming transfer running */
57 /*    UX_STATE_WAIT                         Streaming transfer running    */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    (ux_device_class_audio_stream_task_function)                        */
62 /*                                          Run stream task               */
63 /*    (ux_device_class_audio_stream_feedback_task_function)               */
64 /*                                          Run stream feedback task      */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    USBX Device Stack                                                   */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  10-31-2022     Yajun Xia                Initial Version 6.2.0         */
75 /*                                                                        */
76 /**************************************************************************/
_ux_device_class_audio_tasks_run(VOID * instance)77 UINT _ux_device_class_audio_tasks_run(VOID *instance)
78 {
79 UX_SLAVE_DEVICE                 *device;
80 UX_DEVICE_CLASS_AUDIO           *audio;
81 UX_DEVICE_CLASS_AUDIO_STREAM    *stream;
82 ULONG                           stream_index;
83 UINT                            status;
84 #if defined(UX_DEVICE_CLASS_AUDIO_INTERRUPT_SUPPORT)
85 ULONG                           interrupt_running_count = 0;
86 #endif
87 ULONG                           stream_running_count = 0;
88 #if defined(UX_DEVICE_CLASS_AUDIO_FEEDBACK_SUPPORT)
89 ULONG                           feedback_running_count = 0;
90 #endif
91 
92     /* Get audio instance.  */
93     audio = (UX_DEVICE_CLASS_AUDIO *) instance;
94 
95     /* Get the pointer to the device.  */
96     device =  &_ux_system_slave -> ux_system_slave_device;
97 
98     /* Check if the device is configured.  */
99     if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
100         return(UX_STATE_EXIT);
101 
102 #if defined(UX_DEVICE_CLASS_AUDIO_INTERRUPT_SUPPORT)
103 
104     /* Run audio status tasks.  */
105     status = _ux_device_class_audio_interrupt_task_function(audio);
106     if (status == UX_STATE_EXIT)
107         return(status);
108     if (status == UX_STATE_WAIT)
109         interrupt_running_count++;
110 #endif
111 
112     for (stream_index = 0;
113          stream_index < audio -> ux_device_class_audio_streams_nb;
114          stream_index ++)
115     {
116         stream = &audio -> ux_device_class_audio_streams[stream_index];
117         status = stream -> ux_device_class_audio_stream_task_function(stream);
118         if (status == UX_STATE_EXIT)
119             return(status);
120         if (status == UX_STATE_WAIT)
121             stream_running_count ++;
122 
123 #if defined(UX_DEVICE_CLASS_AUDIO_FEEDBACK_SUPPORT)
124         if (stream -> ux_device_class_audio_stream_feedback_task_function != UX_NULL)
125         {
126             status = stream -> ux_device_class_audio_stream_feedback_task_function(stream);
127             if (status == UX_STATE_EXIT)
128                 return(status);
129             if (status == UX_STATE_WAIT)
130                 feedback_running_count ++;
131         }
132 #endif
133     }
134 
135     if ((stream_running_count > 0)
136 #if defined(UX_DEVICE_CLASS_AUDIO_INTERRUPT_SUPPORT)
137         || (interrupt_running_count > 0)
138 #endif
139 #if defined(UX_DEVICE_CLASS_AUDIO_FEEDBACK_SUPPORT)
140         || (feedback_running_count > 0)
141 #endif
142         )
143     {
144         return UX_STATE_WAIT;
145     }
146     else
147     {
148         return UX_STATE_IDLE;
149     }
150 }
151 
152 #endif