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 /**                                                                       */
16 /** USBX Component                                                        */
17 /**                                                                       */
18 /**   Slave Simulator Controller Driver                                   */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define UX_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "ux_api.h"
29 #include "ux_dcd_sim_slave.h"
30 
31 
32 #if defined(UX_DEVICE_STANDALONE)
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                                RELEASE       */
36 /*                                                                        */
37 /*    _ux_dcd_sim_slave_transfer_run                       PORTABLE C     */
38 /*                                                            6.1.10      */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function will initiate a transfer to a specific endpoint.      */
46 /*    If the endpoint is IN, the endpoint register will be set to accept  */
47 /*    the request.                                                        */
48 /*                                                                        */
49 /*    If the endpoint is IN, the endpoint FIFO will be filled with the    */
50 /*    buffer and the endpoint register set.                               */
51 /*                                                                        */
52 /*    It's for standalone mode.                                           */
53 /*                                                                        */
54 /*  INPUT                                                                 */
55 /*                                                                        */
56 /*    dcd_sim_slave                         Pointer to device controller  */
57 /*    transfer_request                      Pointer to transfer request   */
58 /*                                                                        */
59 /*  OUTPUT                                                                */
60 /*                                                                        */
61 /*    State machine Status to check                                       */
62 /*    UX_STATE_NEXT                         Transfer done, to next state  */
63 /*    UX_STATE_EXIT                         Abnormal, to reset state      */
64 /*    (others)                              Keep running, waiting         */
65 /*                                                                        */
66 /*  CALLS                                                                 */
67 /*                                                                        */
68 /*    _ux_utility_semaphore_get             Get semaphore                 */
69 /*    _ux_dcd_sim_slave_transfer_abort      Abort transfer                */
70 /*                                                                        */
71 /*  CALLED BY                                                             */
72 /*                                                                        */
73 /*    Slave Simulator Controller Driver                                   */
74 /*                                                                        */
75 /*  RELEASE HISTORY                                                       */
76 /*                                                                        */
77 /*    DATE              NAME                      DESCRIPTION             */
78 /*                                                                        */
79 /*  01-31-2022     Chaoqiong Xiao           Initial Version 6.1.10        */
80 /*                                                                        */
81 /**************************************************************************/
_ux_dcd_sim_slave_transfer_run(UX_DCD_SIM_SLAVE * dcd_sim_slave,UX_SLAVE_TRANSFER * transfer_request)82 UINT  _ux_dcd_sim_slave_transfer_run(UX_DCD_SIM_SLAVE *dcd_sim_slave, UX_SLAVE_TRANSFER *transfer_request)
83 {
84 
85 UX_INTERRUPT_SAVE_AREA
86 
87 UX_SLAVE_ENDPOINT       *endpoint;
88 UX_DCD_SIM_SLAVE_ED     *ed;
89 ULONG                   ed_status;
90 
91 
92     UX_PARAMETER_NOT_USED(dcd_sim_slave);
93 
94     /* Get the pointer to the logical endpoint from the transfer request.  */
95     endpoint =  transfer_request -> ux_slave_transfer_request_endpoint;
96 
97     /* Get the slave endpoint.  */
98     ed = (UX_DCD_SIM_SLAVE_ED *) endpoint -> ux_slave_endpoint_ed;
99 
100     UX_DISABLE
101 
102     /* Get current status.  */
103     ed_status = ed -> ux_sim_slave_ed_status;
104 
105     /* ED freed, must disconnected.  */
106     if (ed_status == UX_DCD_SIM_SLAVE_ED_STATUS_UNUSED)
107     {
108         transfer_request -> ux_slave_transfer_request_status = UX_TRANSFER_BUS_RESET;
109         UX_RESTORE
110         return(UX_STATE_EXIT);
111     }
112 
113     /* For control endpoint, always go to next state.  */
114     if(ed -> ux_sim_slave_ed_index == 0)
115     {
116         UX_RESTORE
117         return(UX_STATE_NEXT);
118     }
119 
120     /* ED stalled.  */
121     if (ed_status & UX_DCD_SIM_SLAVE_ED_STATUS_STALLED)
122     {
123         ed -> ux_sim_slave_ed_status = UX_DCD_SIM_SLAVE_ED_STATUS_USED;
124         transfer_request -> ux_slave_transfer_request_status = UX_TRANSFER_STALLED;
125         UX_RESTORE
126         return(UX_STATE_NEXT);
127     }
128 
129     /* Transfer started.  */
130     if (ed_status & UX_DCD_SIM_SLAVE_ED_STATUS_TRANSFER)
131     {
132         if (ed_status & UX_DCD_SIM_SLAVE_ED_STATUS_DONE)
133         {
134             ed -> ux_sim_slave_ed_status = UX_DCD_SIM_SLAVE_ED_STATUS_USED;
135             UX_RESTORE
136             return(UX_STATE_NEXT);
137         }
138         UX_RESTORE
139         return(UX_STATE_WAIT);
140     }
141 
142     /* Start transfer.  */
143     ed->ux_sim_slave_ed_status |= UX_DCD_SIM_SLAVE_ED_STATUS_TRANSFER;
144     UX_RESTORE
145     return(UX_STATE_WAIT);
146 
147 }
148 #endif
149