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 PIMA 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_pima.h"
29 #include "ux_device_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_device_class_pima_event_set                     PORTABLE C      */
37 /*                                                           6.3.0        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function sends an event to the pima class. It is processed     */
45 /*    asynchronously by the interrupt thread.                             */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    pima                                     Address of pima class      */
50 /*    event                                    Pointer of the event       */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    status                                   UX_SUCCESS if there is an  */
55 /*                                             event                      */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_device_semaphore_put                 Put semaphore              */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    ThreadX                                                             */
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 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
72 /*                                            refined macros names,       */
73 /*                                            added transaction ID,       */
74 /*                                            resulting in version 6.1.10 */
75 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            checked if INT EP exists,   */
77 /*                                            resulting in version 6.3.0  */
78 /*                                                                        */
79 /**************************************************************************/
_ux_device_class_pima_event_set(UX_SLAVE_CLASS_PIMA * pima,UX_SLAVE_CLASS_PIMA_EVENT * pima_event)80 UINT  _ux_device_class_pima_event_set(UX_SLAVE_CLASS_PIMA *pima,
81                                       UX_SLAVE_CLASS_PIMA_EVENT *pima_event)
82 {
83 
84 UX_SLAVE_CLASS_PIMA_EVENT       *current_pima_event;
85 UX_SLAVE_CLASS_PIMA_EVENT       *next_pima_event;
86 UX_SLAVE_DEVICE                 *device;
87 
88     /* If trace is enabled, insert this event into the trace buffer.  */
89     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_PIMA_EVENT_SET, pima, pima_event, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
90 
91     /* Get the pointer to the device.  */
92     device =  &_ux_system_slave -> ux_system_slave_device;
93 
94     /* Check the device state.  */
95     if (device -> ux_slave_device_state !=  UX_DEVICE_CONFIGURED)
96     {
97 
98         /* If trace is enabled, insert this event into the trace buffer.  */
99         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DEVICE_HANDLE_UNKNOWN, device, 0, 0, UX_TRACE_ERRORS, 0, 0)
100 
101         return(UX_DEVICE_HANDLE_UNKNOWN);
102     }
103 
104     /* Check if interrupt endpoint is available.  */
105     if (pima -> ux_device_class_pima_interrupt_endpoint == UX_NULL)
106     {
107         return (UX_FUNCTION_NOT_SUPPORTED);
108     }
109 
110     /* Current position of the head.  */
111     current_pima_event =  pima -> ux_device_class_pima_event_array_head;
112 
113     /* If the pointer is NULL, the round robin buffer has not been activated.  */
114     if (current_pima_event == UX_NULL)
115         return (UX_ERROR);
116 
117     /* Calculate the next position.  */
118     if ((current_pima_event + 1) == pima -> ux_device_class_pima_event_array_end)
119 
120         /* We are at the end, go back to the beginning.  */
121         next_pima_event =  pima -> ux_device_class_pima_event_array;
122 
123     else
124         /* We are not at the end, increment the head position.  */
125         next_pima_event = current_pima_event + 1;
126 
127 
128     /* Any place left for this event ? */
129     if (next_pima_event == pima -> ux_device_class_pima_event_array_tail)
130         return (UX_ERROR);
131 
132     /* Update the head.  */
133     pima -> ux_device_class_pima_event_array_head = next_pima_event;
134 
135     /* There is an event to report, get the current pointer to the event.  */
136     current_pima_event =  pima -> ux_device_class_pima_event_array_tail;
137 
138     /* fill in the event structure from the user.  */
139     current_pima_event -> ux_device_class_pima_event_code           = pima_event -> ux_device_class_pima_event_code;
140     current_pima_event -> ux_device_class_pima_event_transaction_id = pima_event -> ux_device_class_pima_event_transaction_id;
141     current_pima_event -> ux_device_class_pima_event_parameter_1    = pima_event -> ux_device_class_pima_event_parameter_1;
142     current_pima_event -> ux_device_class_pima_event_parameter_2    = pima_event -> ux_device_class_pima_event_parameter_2;
143     current_pima_event -> ux_device_class_pima_event_parameter_3    = pima_event -> ux_device_class_pima_event_parameter_3;
144 
145     /* Set a semaphore to wake up the interrupt thread.  */
146     _ux_device_semaphore_put(&pima -> ux_device_class_pima_interrupt_thread_semaphore);
147 
148     /* Return event status to the user.  */
149     return(UX_SUCCESS);
150 }
151 
152