1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016 - 2017,2019 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "usb_device_config.h"
10 #include "usb.h"
11 /* CONFIG_UDC_DRIVER is for Zephyr, it will not be defined in NXP MCUXpresso SDK */
12 #if ((defined CONFIG_UDC_DRIVER) && (CONFIG_UDC_DRIVER))
13 #include "fsl_device_registers.h"
14 #include "usb_device_mcux_drv_port.h"
15 #else
16 #include "usb_device.h"
17 #include "fsl_device_registers.h"
18 #endif
19 #if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U)) && \
20     ((defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U)))
21 #include "usb_hsdcd.h"
22 #endif
23 #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
24 #if ((defined FSL_FEATURE_USBHSD_HAS_EXIT_HS_ISSUE) && (FSL_FEATURE_USBHSD_HAS_EXIT_HS_ISSUE > 0U))
25 #include "usb_phy.h"
26 #endif
27 #endif
28 #if (((defined(USB_DEVICE_CONFIG_LPCIP3511FS)) && (USB_DEVICE_CONFIG_LPCIP3511FS > 0U)) || \
29      ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U)))
30 /* CONFIG_UDC_DRIVER is for Zephyr, it will not be defined in NXP MCUXpresso SDK */
31 #if ((defined CONFIG_UDC_DRIVER) && (CONFIG_UDC_DRIVER))
32 #include "usb_device_lpcip3511.h"
33 #else
34 #include "usb_device_dci.h"
35 #include "usb_device_lpcip3511.h"
36 #endif
37 
38 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
39 
40 #define USB_LPC3511IP_INTSTAT_DEV_INT_MASK   USBHSD_INTSTAT_DEV_INT_MASK
41 #define USB_LPC3511IP_INTSTAT_FRAME_INT_MASK USBHSD_INTSTAT_FRAME_INT_MASK
42 
43 #define USB_LPC3511IP_DEVCMDSTAT_INTONNAK_AO_MASK USBHSD_DEVCMDSTAT_INTONNAK_AO_MASK
44 #define USB_LPC3511IP_DEVCMDSTAT_INTONNAK_AI_MASK USBHSD_DEVCMDSTAT_INTONNAK_AI_MASK
45 
46 #define USB_LPC3511IP_DEVCMDSTAT_LPM_REWP_MASK  USBHSD_DEVCMDSTAT_LPM_REWP_MASK
47 #define USB_LPC3511IP_DEVCMDSTAT_LPM_REWP_SHIFT USBHSD_DEVCMDSTAT_LPM_REWP_SHIFT
48 
49 #define USB_LPC3511IP_DEVCMDSTAT_Speed_MASK USBHSD_DEVCMDSTAT_Speed_MASK
50 
51 #define USB_LPC3511IP_DEVCMDSTAT_DCON_MASK          USBHSD_DEVCMDSTAT_DCON_MASK
52 #define USB_LPC3511IP_DEVCMDSTAT_DEV_EN_MASK        USBHSD_DEVCMDSTAT_DEV_EN_MASK
53 #define USB_LPC3511IP_DEVCMDSTAT_LPM_SUP_MASK       USBHSD_DEVCMDSTAT_LPM_SUP_MASK
54 #define USB_LPC3511IP_DEVCMDSTAT_FORCE_NEEDCLK_MASK USBHSD_DEVCMDSTAT_FORCE_NEEDCLK_MASK
55 #define USB_LPC3511IP_DEVCMDSTAT_LPM_SUS_MASK       USBHSD_DEVCMDSTAT_LPM_SUS_MASK
56 
57 #define USB_LPC3511IP_USB_LPM_HIRD_SW USBHSD_LPM_HIRD_SW
58 
59 #define USB_LPC3511IP_DEVCMDSTAT_DEV_ADDR_MASK       USBHSD_DEVCMDSTAT_DEV_ADDR_MASK
60 #define USB_LPC3511IP_DEVCMDSTAT_DSUS_MASK           USBHSD_DEVCMDSTAT_DSUS_MASK
61 #define USB_LPC3511IP_INFO_ERR_CODE_MASK             USBHSD_INFO_ERR_CODE_MASK
62 #define USB_LPC3511IP_DEVCMDSTAT_SETUP_MASK          USBHSD_DEVCMDSTAT_SETUP_MASK
63 #define USB_LPC3511IP_DEVCMDSTAT_DRES_C_MASK         USBHSD_DEVCMDSTAT_DRES_C_MASK
64 #define USB_LPC3511IP_DEVCMDSTAT_DSUS_C_MASK         USBHSD_DEVCMDSTAT_DSUS_C_MASK
65 #define USB_LPC3511IP_DEVCMDSTAT_DCON_C_MASK         USBHSD_DEVCMDSTAT_DCON_C_MASK
66 #define USB_LPC3511IP_DEVCMDSTAT_VBUS_DEBOUNCED_MASK USBHSD_DEVCMDSTAT_VBUS_DEBOUNCED_MASK
67 #define USB_LPC3511IP_INFO_FRAME_NR_MASK             USBHSD_INFO_FRAME_NR_MASK
68 #define USB_LPC3511IP_INFO_FRAME_NR_SHIFT            USBHSD_INFO_FRAME_NR_SHIFT
69 #else
70 #define USB_LPC3511IP_INTSTAT_DEV_INT_MASK   USB_INTSTAT_DEV_INT_MASK
71 #define USB_LPC3511IP_INTSTAT_FRAME_INT_MASK USB_INTSTAT_FRAME_INT_MASK
72 
73 #define USB_LPC3511IP_DEVCMDSTAT_INTONNAK_AO_MASK USB_DEVCMDSTAT_INTONNAK_AO_MASK
74 #define USB_LPC3511IP_DEVCMDSTAT_INTONNAK_AI_MASK USB_DEVCMDSTAT_INTONNAK_AI_MASK
75 
76 #define USB_LPC3511IP_DEVCMDSTAT_LPM_REWP_MASK  USB_DEVCMDSTAT_LPM_REWP_MASK
77 #define USB_LPC3511IP_DEVCMDSTAT_LPM_REWP_SHIFT USB_DEVCMDSTAT_LPM_REWP_SHIFT
78 
79 #define USB_LPC3511IP_DEVCMDSTAT_DCON_MASK          USB_DEVCMDSTAT_DCON_MASK
80 #define USB_LPC3511IP_DEVCMDSTAT_DEV_EN_MASK        USB_DEVCMDSTAT_DEV_EN_MASK
81 #define USB_LPC3511IP_DEVCMDSTAT_LPM_SUP_MASK       USB_DEVCMDSTAT_LPM_SUP_MASK
82 #define USB_LPC3511IP_DEVCMDSTAT_FORCE_NEEDCLK_MASK USB_DEVCMDSTAT_FORCE_NEEDCLK_MASK
83 #define USB_LPC3511IP_DEVCMDSTAT_LPM_SUP_MASK       USB_DEVCMDSTAT_LPM_SUP_MASK
84 #define USB_LPC3511IP_DEVCMDSTAT_LPM_SUS_MASK       USB_DEVCMDSTAT_LPM_SUS_MASK
85 
86 #define USB_LPC3511IP_USB_LPM_HIRD_SW USB_LPM_HIRD_SW
87 
88 #define USB_LPC3511IP_DEVCMDSTAT_DEV_ADDR_MASK       USB_DEVCMDSTAT_DEV_ADDR_MASK
89 #define USB_LPC3511IP_DEVCMDSTAT_DSUS_MASK           USB_DEVCMDSTAT_DSUS_MASK
90 #define USB_LPC3511IP_INFO_ERR_CODE_MASK             USB_INFO_ERR_CODE_MASK
91 #define USB_LPC3511IP_DEVCMDSTAT_SETUP_MASK          USB_DEVCMDSTAT_SETUP_MASK
92 #define USB_LPC3511IP_DEVCMDSTAT_DRES_C_MASK         USB_DEVCMDSTAT_DRES_C_MASK
93 #define USB_LPC3511IP_DEVCMDSTAT_DSUS_C_MASK         USB_DEVCMDSTAT_DSUS_C_MASK
94 #define USB_LPC3511IP_DEVCMDSTAT_DCON_C_MASK         USB_DEVCMDSTAT_DCON_C_MASK
95 #define USB_LPC3511IP_DEVCMDSTAT_VBUS_DEBOUNCED_MASK USB_DEVCMDSTAT_VBUSDEBOUNCED_MASK
96 #define USB_LPC3511IP_INFO_FRAME_NR_MASK             USB_INFO_FRAME_NR_MASK
97 #define USB_LPC3511IP_INFO_FRAME_NR_SHIFT            USB_INFO_FRAME_NR_SHIFT
98 #endif
99 
100 #define USB_LPC3511IP_USB_LPM_ADPPROBE_MASK (0x00100000u)
101 
102 /*******************************************************************************
103  * Definitions
104  ******************************************************************************/
105 
106 /* on Aruba IP3511 (USB0 FS), there are 8 physical EPs, on IP3511 HS (USB1 FS), there are 10 physical EPs. */
107 #define USB_LPC3511IP_MAX_PHY_ENDPOINT_MASK (0xFFFFu)
108 
109 /*! @brief endpoint command status, buffer address offset */
110 #define USB_LPC3511IPHS_ENDPOINT_BUFFER_ADDRESS_OFFSET_MASK (0x000007FFu)
111 #define USB_LPC3511IPHS_ENDPOINT_BUFFER_NBYTES_SHIFT        (11)
112 #define USB_LPC3511IPHS_ENDPOINT_BUFFER_NBYTES_MASK         (0x03FFF800u)
113 #define USB_LPC3511IPFS_ENDPOINT_BUFFER_ADDRESS_OFFSET_MASK (0x0000FFFFu)
114 #define USB_LPC3511IPFS_ENDPOINT_BUFFER_NBYTES_SHIFT        (16)
115 #define USB_LPC3511IPFS_ENDPOINT_BUFFER_NBYTES_MASK         (0x03FF0000u)
116 
117 #define USB_LPC3511IP_ENDPOINT_ENDPOINT_TYPE_MASK   (0x01UL << 26)
118 #define USB_LPC3511IP_ENDPOINT_RFTV_MASK            (0x01UL << 27)
119 #define USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK    (0x01UL << 28)
120 #define USB_LPC3511IP_ENDPOINT_STALL_MASK           (0x01UL << 29)
121 #define USB_LPC3511IP_ENDPOINT_STALL_SHIFT          (29)
122 #define USB_LPC3511IP_ENDPOINT_DISABLE_MASK         (0x01UL << 30)
123 #define USB_LPC3511IP_ENDPOINT_ACTIVE_MASK          (0x01UL << 31)
124 #define USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT (26)
125 
126 #define USB_LPC3511IP_DEVCMDSTAT_INTERRUPT_WC_MASK (0x0F000000u)
127 
128 #define USB_LPC3511IP_DEVCMDSTAT_W1C_MASK (USB_LPC3511IP_DEVCMDSTAT_SETUP_MASK | \
129                                            USB_LPC3511IP_DEVCMDSTAT_DCON_C_MASK | \
130                                            USB_LPC3511IP_DEVCMDSTAT_DSUS_C_MASK | \
131                                            USB_LPC3511IP_DEVCMDSTAT_DRES_C_MASK)
132 
133 #define USB_LPC3511IP_ENDPOINT_SET_ENDPOINT_AND(lpcState, index, odd, value)                           \
134     *((volatile uint32_t *)(((uint32_t)((lpcState)->epCommandStatusList)) | ((uint32_t)(index) << 3) | \
135                             ((((uint32_t)(odd)) & 1UL) << 2U))) &= (value)
136 
137 /*! @brief Set endpoint command/status value */
138 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
139 #define USB_LPC3511IP_ENDPOINT_SET_ENDPOINT(lpcState, index, odd, value, NBytes, address)                \
140                                                                                                          \
141     *((volatile uint32_t *)(((uint32_t)((lpcState)->epCommandStatusList)) | ((uint32_t)(index) << 3) |   \
142                             (((((uint32_t)(odd)) & 1UL)) << 2U))) =                                      \
143         ((0U != lpc3511IpState->controllerSpeed) ?                                                       \
144                                                                                                          \
145              ((uint32_t)(value) | ((uint32_t)(NBytes) << USB_LPC3511IPHS_ENDPOINT_BUFFER_NBYTES_SHIFT) | \
146               (((uint32_t)(address) >> 6) & USB_LPC3511IPHS_ENDPOINT_BUFFER_ADDRESS_OFFSET_MASK)) :      \
147                                                                                                          \
148              ((uint32_t)(value) | ((uint32_t)(NBytes) << USB_LPC3511IPFS_ENDPOINT_BUFFER_NBYTES_SHIFT) | \
149               (((uint32_t)(address) >> 6) & USB_LPC3511IPFS_ENDPOINT_BUFFER_ADDRESS_OFFSET_MASK)))
150 #else
151 #define USB_LPC3511IP_ENDPOINT_SET_ENDPOINT(lpcState, index, odd, value, NBytes, address)              \
152                                                                                                        \
153     *((volatile uint32_t *)(((uint32_t)((lpcState)->epCommandStatusList)) | ((uint32_t)(index) << 3) | \
154                             (((((uint32_t)(odd)) & 1U)) << 2U))) =                                     \
155         ((uint32_t)(value) | ((uint32_t)(NBytes) << USB_LPC3511IPFS_ENDPOINT_BUFFER_NBYTES_SHIFT) |    \
156          (((uint32_t)(address) >> 6) & USB_LPC3511IPFS_ENDPOINT_BUFFER_ADDRESS_OFFSET_MASK))
157 #endif
158 
159 #define USB_LPC3511IP_ENDPOINT_DES_INDEX(endpoint) \
160     (((((endpoint)) & 0x0FU) << 1) +               \
161      ((0U != ((endpoint)&USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK)) ? (1U) : (0U)))
162 
163 #define USB_LPC3511IP_GET_MULTIPLE_OF_64(n) ((((uint32_t)n) + 63U) & 0xFFFFFFC0U)
164 
165 /*******************************************************************************
166  * Prototypes
167  ******************************************************************************/
168 
169 static usb_status_t USB_DeviceLpc3511IpTransaction(usb_device_lpc3511ip_state_struct_t *lpc3511IpState,
170                                                    usb_device_lpc3511ip_endpoint_state_struct_t *epState,
171                                                    uint8_t endpointIndex);
172 static usb_status_t USB_DeviceLpc3511IpControlPreSetDeviceAddress(usb_device_controller_handle controllerHandle,
173                                                                   void *param);
174 /*******************************************************************************
175  * Variables
176  ******************************************************************************/
177 
178 /* define the reserved buffer for endpoint max packet copy */
179 #define SETUP_TRANSFER_DATA_SIZE     (64U) /* The actual size is 8. Here use 64 aligned to 64-byte boundaries. */
180 #define CONTROL_TRANSFER_DATA_SIZE   (64U) /* The maximum size in the control data stage. */
181 #define ZERO_TRANSFER_DATA_SIZE      (64U) /* The actual size is 4. Here use 64 aligned to 64-byte boundaries. */
182 #define SETUP_TRANSFER_DATA_OFFSET   (0U)
183 #define CONTROL_TRANSFER_DATA_OFFSET (SETUP_TRANSFER_DATA_SIZE >> 2U)
184 #define ZERO_TRANSFER_DATA_OFFSET    ((SETUP_TRANSFER_DATA_SIZE + CONTROL_TRANSFER_DATA_SIZE) >> 2U)
185 #define RESERVED_EP_DATA_OFFSET \
186     ((SETUP_TRANSFER_DATA_SIZE + CONTROL_TRANSFER_DATA_SIZE + ZERO_TRANSFER_DATA_SIZE) >> 2U)
187 
188 #define ENDPOINT_RESERVED_BUFFER_ADDRESS ((uint8_t *)&s_SetupAndEpReservedData[0])
189 
190 #if defined(USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY) && (USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY)
191 USB_GLOBAL USB_RAM_ADDRESS_ALIGNMENT(64) static uint32_t
192     s_SetupAndEpReservedData[(USB_DEVICE_IP3511_BITS_FOR_RESERVED_BUFFER << 6U) >> 2U];
193 static uint8_t s_EpReservedBufferBits[(USB_DEVICE_IP3511_BITS_FOR_RESERVED_BUFFER + 7U) / 8U];
194 #else
195 USB_GLOBAL USB_RAM_ADDRESS_ALIGNMENT(64) static uint32_t
196     s_SetupAndEpReservedData[USB_DEVICE_CONFIG_LPCIP3511FS + USB_DEVICE_CONFIG_LPCIP3511HS][RESERVED_EP_DATA_OFFSET];
197 #endif
198 
199 static usb_device_lpc3511ip_state_struct_t
200     s_UsbDeviceLpc3511IpState[USB_DEVICE_CONFIG_LPCIP3511FS + USB_DEVICE_CONFIG_LPCIP3511HS];
201 
202 /* LPC3511IP controller driver instances and endpoint command/status list, EPLISTSTART's value is the buffer pointer. */
203 #if ((USB_DEVICE_CONFIG_LPCIP3511FS + USB_DEVICE_CONFIG_LPCIP3511HS) == 1U)
204 USB_CONTROLLER_DATA USB_RAM_ADDRESS_ALIGNMENT(256) static uint32_t
205     s_EpCommandStatusList1[((USB_DEVICE_IP3511_ENDPOINTS_NUM)) * 4];
206 #define LPC_CONTROLLER_ENDPOINT_LIST_ARRAY \
207     {                                      \
208         &s_EpCommandStatusList1[0]         \
209     }
210 
211 #elif ((USB_DEVICE_CONFIG_LPCIP3511FS + USB_DEVICE_CONFIG_LPCIP3511HS) == 2U)
212 USB_CONTROLLER_DATA USB_RAM_ADDRESS_ALIGNMENT(256) static uint32_t
213     s_EpCommandStatusList1[(USB_DEVICE_IP3511_ENDPOINTS_NUM)*4];
214 USB_CONTROLLER_DATA USB_RAM_ADDRESS_ALIGNMENT(256) static uint32_t
215     s_EpCommandStatusList2[(USB_DEVICE_IP3511_ENDPOINTS_NUM)*4];
216 #define LPC_CONTROLLER_ENDPOINT_LIST_ARRAY                     \
217     {                                                          \
218         &s_EpCommandStatusList1[0], &s_EpCommandStatusList2[0] \
219     }
220 
221 #else
222 #error "increase the instance count."
223 #endif
224 
225 #if (defined USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY)
226 #if (USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY == (USB_DEVICE_CONFIG_LPCIP3511FS + USB_DEVICE_CONFIG_LPCIP3511HS))
227 #define USB_DEVICE_IP3511_ALL_IP_SUPPORT_RESERVED_BUFFER 1U
228 #elif ((USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY == USB_DEVICE_CONFIG_LPCIP3511FS) && \
229        (!USB_DEVICE_CONFIG_LPCIP3511HS))
230 #define USB_DEVICE_IP3511_ALL_IP_SUPPORT_RESERVED_BUFFER 1U
231 #elif ((USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY == USB_DEVICE_CONFIG_LPCIP3511HS) && \
232        (!USB_DEVICE_CONFIG_LPCIP3511FS))
233 #define USB_DEVICE_IP3511_ALL_IP_SUPPORT_RESERVED_BUFFER 1U
234 #else
235 #define USB_DEVICE_IP3511_ALL_IP_SUPPORT_RESERVED_BUFFER 0U
236 #endif
237 
238 #else
239 #define USB_DEVICE_IP3511_ALL_IP_SUPPORT_RESERVED_BUFFER 0U
240 
241 #endif
242 
243 /*******************************************************************************
244  * Code
245  ******************************************************************************/
246 #if (defined USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY) && (USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY)
247 
USB_DeviceLpcIp3511MaxPacketNeedCopy(usb_device_lpc3511ip_state_struct_t * lpc3511IpState)248 static inline uint8_t USB_DeviceLpcIp3511MaxPacketNeedCopy(usb_device_lpc3511ip_state_struct_t *lpc3511IpState)
249 {
250 #if (USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY == (USB_DEVICE_CONFIG_LPCIP3511HS + USB_DEVICE_CONFIG_LPCIP3511FS))
251     return 1U;
252 #elif (USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY == USB_DEVICE_CONFIG_LPCIP3511HS)
253     return (lpc3511IpState->controllerSpeed);
254 #elif (USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY == USB_DEVICE_CONFIG_LPCIP3511FS)
255 #if (defined USB_DEVICE_CONFIG_LPCIP3511HS) && (USB_DEVICE_CONFIG_LPCIP3511HS)
256     if (0U != lpc3511IpState->controllerSpeed)
257     {
258         return 0U;
259     }
260     else
261     {
262         return 1U;
263     }
264 #else
265     return 1U;
266 #endif
267 #endif
268 }
269 
USB_DeviceLpcIp3511MallocMaxPacketBuffer(uint32_t multile64)270 static uint8_t *USB_DeviceLpcIp3511MallocMaxPacketBuffer(uint32_t multile64)
271 {
272     uint32_t bitsIndex;
273     uint32_t numIndex;
274     OSA_SR_ALLOC();
275 
276     multile64 = ((multile64 + 63U) / 64U);
277     bitsIndex = 0U;
278     OSA_ENTER_CRITICAL();
279     do
280     {
281         numIndex = 0U;
282         for (; numIndex < multile64; ++numIndex)
283         {
284             if (bitsIndex >= USB_DEVICE_IP3511_BITS_FOR_RESERVED_BUFFER)
285             {
286                 OSA_EXIT_CRITICAL();
287                 return NULL; /* fail */
288             }
289             if (0U != (s_EpReservedBufferBits[(bitsIndex / 8U)] &
290                        (uint8_t)(0x01U << (bitsIndex & 0x00000007U)))) /* has allocated */
291             {
292                 bitsIndex++;
293                 break;
294             }
295             bitsIndex++;
296         }
297     } while (numIndex < multile64);
298 
299     if (numIndex >= multile64)
300     {
301         /* set the bits */
302         for (numIndex = 0U; numIndex < multile64; ++numIndex)
303         {
304             s_EpReservedBufferBits[((bitsIndex - multile64 + numIndex) / 8U)] |=
305                 (uint8_t)(0x01U << ((bitsIndex - multile64 + numIndex) & 0x00000007U));
306         }
307         OSA_EXIT_CRITICAL();
308         return ENDPOINT_RESERVED_BUFFER_ADDRESS + ((bitsIndex - multile64) * 64U);
309     }
310     else
311     {
312         OSA_EXIT_CRITICAL();
313         return NULL;
314     }
315 }
316 
USB_DeviceLpcIp3511ReleaseMaxPacketBuffer(uint8_t * buffer,uint32_t bufferSize)317 static void USB_DeviceLpcIp3511ReleaseMaxPacketBuffer(uint8_t *buffer, uint32_t bufferSize)
318 {
319     uint32_t bitsIndex;
320     int32_t temp;
321     uint8_t bitsNum;
322     OSA_SR_ALLOC();
323 
324     if ((buffer < ENDPOINT_RESERVED_BUFFER_ADDRESS) ||
325         (buffer >= (ENDPOINT_RESERVED_BUFFER_ADDRESS + USB_DEVICE_IP3511_ENDPOINT_RESERVED_BUFFER_SIZE)))
326     {
327         return;
328     }
329     /*misra 10.8*/
330     temp      = ((buffer - ENDPOINT_RESERVED_BUFFER_ADDRESS) / 64);
331     bitsIndex = (uint32_t)temp;
332 
333     OSA_ENTER_CRITICAL();
334     for (bitsNum = 0; bitsNum < ((bufferSize + 63U) / 64U); ++bitsNum)
335     {
336         s_EpReservedBufferBits[((bitsIndex + bitsNum) / 8U)] &=
337             (uint8_t)(~(0x01U << ((bitsIndex + bitsNum) & 0x00000007U))); /* clear the bit */
338     }
339     OSA_EXIT_CRITICAL();
340 }
341 #endif
342 
USB_DeviceLpc3511IpGetEndpointStateStruct(usb_device_lpc3511ip_state_struct_t * lpc3511IpState,uint8_t endpointIndex)343 static usb_device_lpc3511ip_endpoint_state_struct_t *USB_DeviceLpc3511IpGetEndpointStateStruct(
344     usb_device_lpc3511ip_state_struct_t *lpc3511IpState, uint8_t endpointIndex)
345 {
346     if (endpointIndex <= ((uint32_t)USB_DEVICE_IP3511_ENDPOINTS_NUM * 2U))
347     {
348         return &(lpc3511IpState->endpointState[endpointIndex]);
349     }
350 
351     return NULL;
352 }
353 
354 /*!
355  * @brief Write the command/status entry to start a transfer.
356  *
357  * The function is used to start a transfer by writing the command/status entry.
358  *
359  * @param lpc3511IpState      Pointer of the controller state structure.
360  * @param endpoint         Endpoint number.
361  * @param direction        The direction of the endpoint, 0U - USB_OUT, 1U - USB_IN.
362  * @param buffer           The memory address to save the received data, or the memory address to hold the data need to
363  * be sent.
364  * @param length           The length of the data.
365  *
366  * @return A USB error code or kStatus_USB_Success.
367  */
USB_DeviceLpc3511IpEndpointPrime(usb_device_lpc3511ip_state_struct_t * lpc3511IpState,usb_device_lpc3511ip_endpoint_state_struct_t * epState,uint8_t endpointIndex,uint8_t * buffer,uint32_t length)368 static usb_status_t USB_DeviceLpc3511IpEndpointPrime(usb_device_lpc3511ip_state_struct_t *lpc3511IpState,
369                                                      usb_device_lpc3511ip_endpoint_state_struct_t *epState,
370                                                      uint8_t endpointIndex,
371                                                      uint8_t *buffer,
372                                                      uint32_t length)
373 {
374     uint8_t odd;
375 
376     OSA_SR_ALLOC();
377 
378     /* Enter critical */
379     OSA_ENTER_CRITICAL();
380 
381     /* Flag the endpoint is busy. */
382     epState->stateUnion.stateBitField.transferring = 1U;
383 
384 #if ((defined(USB_DEVICE_IP3511HS_CONTROL_OUT_NYET_WORKAROUND)) && \
385      (USB_DEVICE_IP3511HS_CONTROL_OUT_NYET_WORKAROUND > 0U))
386     if (((endpointIndex >> 1U) == USB_ENDPOINT_CONTROL) && ((endpointIndex & 0x01U) == 0x00U) && (0U == length))
387     {
388         length = 65U;
389     }
390 #endif
391 
392     /* update the endpoint status */
393     epState->transferPrimedLength += length;
394 #if (defined USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE) && (USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE)
395     if ((endpointIndex >> 1U) != USB_ENDPOINT_CONTROL)
396     {
397         odd = (uint8_t)epState->stateUnion.stateBitField.producerOdd;
398         epState->stateUnion.stateBitField.doubleBufferBusy++;
399         epState->stateUnion.stateBitField.producerOdd ^= 1U;
400     }
401     else
402 #endif
403     {
404         odd = 0U;
405     }
406     epState->epBufferStatusUnion[odd].epBufferStatusField.transactionLength = (uint16_t)length;
407 
408     /* when receive the zero length packet, the controller will set 4 bytes buffer as 0x00 */
409     if (buffer == NULL)
410     {
411         buffer = lpc3511IpState->zeroTransactionData;
412     }
413 
414     USB_LPC3511IP_ENDPOINT_SET_ENDPOINT(
415         lpc3511IpState, endpointIndex, odd,
416         (epState->stateUnion.stateBitField.epControlDefault << USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT) |
417             USB_LPC3511IP_ENDPOINT_ACTIVE_MASK,
418         length, (uint32_t)buffer);
419     if (0U != (epState->stateUnion.stateBitField.epControlDefault &
420                ((USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK) >> USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT)))
421     {
422         epState->stateUnion.stateBitField.epControlDefault &=
423             (~((USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK) >> USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT));
424     }
425     /* Exit critical */
426     OSA_EXIT_CRITICAL();
427     return kStatus_USB_Success;
428 }
429 
430 #if 0
431 /*!
432  * @brief Prime a next setup transfer.
433  *
434  * The function is used to prime a buffer in control out pipe to wait for receiving the host's setup packet.
435  *
436  * @param lpc3511IpState       Pointer of the controller state structure.
437  *
438  */
439 static void USB_DeviceLpc3511IpPrimeNextSetup(usb_device_lpc3511ip_state_struct_t *lpc3511IpState)
440 {
441     USB_LPC3511IP_ENDPOINT_SET_ENDPOINT(lpc3511IpState, 0, 1, 0, 8, lpc3511IpState->setupData);
442 }
443 #endif
444 
445 /*!
446  * @brief reset ip3511.
447  *
448  * @param lpc3511IpState       Pointer of the controller state structure.
449  *
450  */
USB_DeviceLpc3511IpSetDefaultState(usb_device_lpc3511ip_state_struct_t * lpc3511IpState)451 static void USB_DeviceLpc3511IpSetDefaultState(usb_device_lpc3511ip_state_struct_t *lpc3511IpState)
452 {
453     uint32_t index = 0;
454     uint8_t usbAddress;
455     usb_status_t error = kStatus_USB_Error;
456     /* zero the command/status list buffer and disable all endpoints */
457     for (index = 0; index < 4U; ++index)
458     {
459         lpc3511IpState->epCommandStatusList[index] = 0x00000000U;
460     }
461     for (index = 4U; index < (uint32_t)USB_DEVICE_IP3511_ENDPOINTS_NUM * 4U; ++index)
462     {
463         lpc3511IpState->epCommandStatusList[index] = USB_LPC3511IP_ENDPOINT_DISABLE_MASK;
464     }
465 
466     /* set address as 0 */
467     usbAddress = 0U;
468     error      = USB_DeviceLpc3511IpControlPreSetDeviceAddress(lpc3511IpState, &usbAddress);
469     if (kStatus_USB_Success == error)
470     {
471         /*no action, just for misra4.7*/
472     }
473     lpc3511IpState->registerBase->EPLISTSTART = (uint32_t)lpc3511IpState->epCommandStatusList;
474 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
475     if (0U != lpc3511IpState->controllerSpeed)
476     {
477         if ((USBHSD_DATABUFSTART_DA_BUF_MASK & (uint32_t)lpc3511IpState->setupData) !=
478             lpc3511IpState->registerBase->DATABUFSTART)
479         {
480             /* please use the dedicated ram */
481         }
482     }
483     else
484 #endif
485     {
486         /* all data buffer is in the same 4M range with this setup data buffer */
487         lpc3511IpState->registerBase->DATABUFSTART = (uint32_t)lpc3511IpState->setupData;
488     }
489     /* reset registers */
490     lpc3511IpState->registerBase->EPINUSE = 0x0;
491     lpc3511IpState->registerBase->EPSKIP  = 0x0;
492 /* enable all double-buffer */
493 #if (defined USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE) && (USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE)
494     lpc3511IpState->registerBase->EPBUFCFG = USB_LPC3511IP_MAX_PHY_ENDPOINT_MASK;
495 #else
496     lpc3511IpState->registerBase->EPBUFCFG = 0x00000000U;
497 #endif
498     /* clear interrupts
499      * don't clear DEV_INT because the vbus valid interrupt may occurs with keeping usb connected and reseting device.
500      */
501     lpc3511IpState->registerBase->INTSTAT =
502         (USB_LPC3511IP_INTSTAT_FRAME_INT_MASK | USB_LPC3511IP_MAX_PHY_ENDPOINT_MASK);
503     /* enable interrupts */
504     lpc3511IpState->registerBase->INTEN =
505 #if (defined(USB_DEVICE_CONFIG_SOF_NOTIFICATIONS) && (USB_DEVICE_CONFIG_SOF_NOTIFICATIONS > 0U))
506         USB_LPC3511IP_INTSTAT_FRAME_INT_MASK |
507 #endif
508         USB_LPC3511IP_INTSTAT_DEV_INT_MASK | USB_LPC3511IP_MAX_PHY_ENDPOINT_MASK;
509 
510     /* Clear reset flag */
511     lpc3511IpState->isResetting = 0U;
512 }
513 
514 /* Config and Enable endpoint */
USB_DeviceLpc3511IpEndpointInit(usb_device_lpc3511ip_state_struct_t * lpc3511IpState,usb_device_endpoint_init_struct_t * epInit)515 static usb_status_t USB_DeviceLpc3511IpEndpointInit(usb_device_lpc3511ip_state_struct_t *lpc3511IpState,
516                                                     usb_device_endpoint_init_struct_t *epInit)
517 {
518     uint8_t endpointIndex = USB_LPC3511IP_ENDPOINT_DES_INDEX(epInit->endpointAddress);
519     usb_device_lpc3511ip_endpoint_state_struct_t *epState =
520         USB_DeviceLpc3511IpGetEndpointStateStruct(lpc3511IpState, endpointIndex);
521     uint16_t maxPacketSize =
522         (uint16_t)((((epInit->maxPacketSize & USB_DESCRIPTOR_ENDPOINT_MAXPACKETSIZE_MULT_TRANSACTIONS_MASK) >>
523                      USB_DESCRIPTOR_ENDPOINT_MAXPACKETSIZE_MULT_TRANSACTIONS_SHFIT) +
524                     1U) *
525                    (epInit->maxPacketSize & USB_DESCRIPTOR_ENDPOINT_MAXPACKETSIZE_SIZE_MASK));
526 #if (defined USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY) && (USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY)
527     uint32_t allocateLength;
528     uint8_t *maxPacketBuffer;
529 #endif
530 
531     /* clear the endpoint status bits */
532     epState->stateUnion.state = 0x00000000U;
533     lpc3511IpState->registerBase->EPINUSE &= (~((uint32_t)(0x01UL << endpointIndex)));
534     /* Save the max packet size of the endpoint */
535     epState->stateUnion.stateBitField.maxPacketSize = maxPacketSize;
536     /* Set the ZLT field */
537     epState->stateUnion.stateBitField.zlt          = epInit->zlt;
538     epState->stateUnion.stateBitField.endpointType = epInit->transferType;
539 
540     /* get the endpoint default control value */
541     if (USB_ENDPOINT_ISOCHRONOUS == epInit->transferType)
542     {
543         epState->stateUnion.stateBitField.epControlDefault =
544             (USB_LPC3511IP_ENDPOINT_ENDPOINT_TYPE_MASK >> USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT);
545     }
546 #if (defined(FSL_FEATURE_USBHSD_VERSION) && (FSL_FEATURE_USBHSD_VERSION >= 300U))
547 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
548     else if (
549 #if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
550      (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
551         (0U == (lpc3511IpState->hsInterruptIssue)) &&
552 #endif
553         (0U != lpc3511IpState->controllerSpeed) && (USB_ENDPOINT_INTERRUPT == epInit->transferType))
554     {
555         epState->stateUnion.stateBitField.epControlDefault =
556             ((USB_LPC3511IP_ENDPOINT_ENDPOINT_TYPE_MASK | USB_LPC3511IP_ENDPOINT_RFTV_MASK) >>
557              USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT);
558     }
559 #endif
560 #endif
561     else
562     {
563         epState->stateUnion.stateBitField.epControlDefault = 0x00U;
564     }
565     /* set the command/status value */
566     USB_LPC3511IP_ENDPOINT_SET_ENDPOINT(
567         lpc3511IpState, endpointIndex, 0U,
568         (epState->stateUnion.stateBitField.epControlDefault << USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT), 0U, 0U);
569     if ((epInit->endpointAddress & USB_ENDPOINT_NUMBER_MASK) == USB_CONTROL_ENDPOINT)
570     {
571         if (0U == (epInit->endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK))
572         {
573             /* Prime setup packet when the endpoint is control out endpoint. */
574             USB_LPC3511IP_ENDPOINT_SET_ENDPOINT(lpc3511IpState, 0U, 1U, 0U, 0U, (uint32_t)lpc3511IpState->setupData);
575         }
576     }
577     else
578     {
579         USB_LPC3511IP_ENDPOINT_SET_ENDPOINT(
580             lpc3511IpState, endpointIndex, 1U,
581             (epState->stateUnion.stateBitField.epControlDefault << USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT), 0U,
582             0U);
583     }
584     if ((endpointIndex >> 1) != USB_CONTROL_ENDPOINT)
585     {
586         /* toggle reset for the toggle */
587         epState->stateUnion.stateBitField.epControlDefault |=
588             ((USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK) >> USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT);
589     }
590 
591     epState->epPacketBuffer = NULL;
592     if ((endpointIndex >> 1U) == USB_CONTROL_ENDPOINT) /* control endpoint */
593     {
594         epState->epPacketBuffer = lpc3511IpState->controlData;
595     }
596 #if (defined USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY) && (USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY)
597     else
598     {
599         if (0U != USB_DeviceLpcIp3511MaxPacketNeedCopy(lpc3511IpState))
600         {
601 #if ((defined(USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX)) && \
602      (USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX > 0U))
603             if ((USB_ENDPOINT_BULK == epInit->transferType) && ((endpointIndex & 0x01U) == 0x00U))
604             {
605                 allocateLength = USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX;
606             }
607             else
608             {
609                 allocateLength = USB_LPC3511IP_GET_MULTIPLE_OF_64(maxPacketSize);
610             }
611 #else
612             allocateLength = USB_LPC3511IP_GET_MULTIPLE_OF_64(maxPacketSize);
613 #endif
614 #if (defined USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE) && (USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE)
615             allocateLength *= 2U;
616 #endif
617             maxPacketBuffer = USB_DeviceLpcIp3511MallocMaxPacketBuffer(allocateLength);
618             if (maxPacketBuffer == NULL)
619             {
620                 return kStatus_USB_AllocFail;
621             }
622             epState->epPacketBuffer = maxPacketBuffer;
623         }
624     }
625 #endif
626 #if (defined(USB_DEVICE_CONFIG_ROOT2_TEST) && (USB_DEVICE_CONFIG_ROOT2_TEST > 0U))
627     epState->stateUnion.stateBitField.isOpened = 1U;
628 #endif
629     return kStatus_USB_Success;
630 }
631 
632 /*!
633  * @brief De-initialize a specified endpoint.
634  *
635  * The function is used to de-initialize a specified endpoint.
636  * Current transfer of the endpoint will be canceled and the specified endpoint will be disabled.
637  *
638  * @param lpc3511IpState      Pointer of the controller state structure.
639  * @param ep               The endpoint address, Bit7, 0U - USB_OUT, 1U - USB_IN.
640  *
641  * @return A USB error code or kStatus_USB_Success.
642  */
USB_DeviceLpc3511IpEndpointDeinit(usb_device_lpc3511ip_state_struct_t * lpc3511IpState,uint8_t ep)643 static usb_status_t USB_DeviceLpc3511IpEndpointDeinit(usb_device_lpc3511ip_state_struct_t *lpc3511IpState, uint8_t ep)
644 {
645     uint8_t endpointIndex = USB_LPC3511IP_ENDPOINT_DES_INDEX(ep);
646     usb_device_lpc3511ip_endpoint_state_struct_t *epState =
647         USB_DeviceLpc3511IpGetEndpointStateStruct(lpc3511IpState, endpointIndex);
648 
649     /* Cancel the transfer of the endpoint */
650 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
651     if (kStatus_USB_Success != USB_DeviceLpc3511IpCancel(lpc3511IpState, ep))
652     {
653         return kStatus_USB_Error;
654     }
655 #else
656     (void)USB_DeviceLpc3511IpCancel(lpc3511IpState, ep);
657 #endif
658 
659 #if (defined USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY) && (USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY)
660     if (0U != USB_DeviceLpcIp3511MaxPacketNeedCopy(lpc3511IpState))
661     {
662         if ((endpointIndex >> 1U) != USB_CONTROL_ENDPOINT) /* control endpoint */
663         {
664 #if (defined USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE) && (USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE)
665 #if ((defined(USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX)) && \
666      (USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX > 0U))
667             if ((USB_ENDPOINT_BULK == epState->stateUnion.stateBitField.endpointType) &&
668                 ((endpointIndex & 0x01U) == 0x00U))
669             {
670                 USB_DeviceLpcIp3511ReleaseMaxPacketBuffer(
671                     epState->epPacketBuffer,
672                     USB_LPC3511IP_GET_MULTIPLE_OF_64(USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX * 2U));
673             }
674             else
675             {
676                 USB_DeviceLpcIp3511ReleaseMaxPacketBuffer(
677                     epState->epPacketBuffer,
678                     USB_LPC3511IP_GET_MULTIPLE_OF_64(epState->stateUnion.stateBitField.maxPacketSize) * 2U);
679             }
680 #else
681             USB_DeviceLpcIp3511ReleaseMaxPacketBuffer(
682                 epState->epPacketBuffer,
683                 USB_LPC3511IP_GET_MULTIPLE_OF_64(epState->stateUnion.stateBitField.maxPacketSize) * 2U);
684 #endif
685 #else
686             USB_DeviceLpcIp3511ReleaseMaxPacketBuffer(epState->epPacketBuffer,
687                                                       epState->stateUnion.stateBitField.maxPacketSize);
688 #endif
689         }
690         epState->epPacketBuffer = NULL;
691     }
692 #endif
693 
694     /* reset the double buffer */
695     lpc3511IpState->registerBase->EPINUSE &= ~((uint32_t)(0x01UL << endpointIndex));
696     /* Disable the endpoint */
697     USB_LPC3511IP_ENDPOINT_SET_ENDPOINT(lpc3511IpState, endpointIndex, 0U, USB_LPC3511IP_ENDPOINT_DISABLE_MASK, 0U, 0U);
698     /* Clear the max packet size */
699     epState->stateUnion.stateBitField.maxPacketSize = 0U;
700 #if (defined(USB_DEVICE_CONFIG_ROOT2_TEST) && (USB_DEVICE_CONFIG_ROOT2_TEST > 0U))
701     /* Clear the endpoint open status */
702     epState->stateUnion.stateBitField.isOpened = 0U;
703 #endif
704 
705     return kStatus_USB_Success;
706 }
707 
708 /*!
709  * @brief Stall a specified endpoint.
710  *
711  * The function is used to stall a specified endpoint.
712  * Current transfer of the endpoint will be canceled and the specified endpoint will be stalled.
713  *
714  * @param lpc3511IpState      Pointer of the controller state structure.
715  * @param ep               The endpoint address, Bit7, 0U - USB_OUT, 1U - USB_IN.
716  *
717  * @return A USB error code or kStatus_USB_Success.
718  */
USB_DeviceLpc3511IpEndpointStall(usb_device_lpc3511ip_state_struct_t * lpc3511IpState,uint8_t ep)719 static usb_status_t USB_DeviceLpc3511IpEndpointStall(usb_device_lpc3511ip_state_struct_t *lpc3511IpState, uint8_t ep)
720 {
721     uint8_t endpointIndex = USB_LPC3511IP_ENDPOINT_DES_INDEX(ep);
722     usb_device_lpc3511ip_endpoint_state_struct_t *epState;
723     OSA_SR_ALLOC();
724 
725     /* stall the endpoint */
726     if ((ep & USB_ENDPOINT_NUMBER_MASK) == USB_CONTROL_ENDPOINT)
727     {
728         epState                                   = USB_DeviceLpc3511IpGetEndpointStateStruct(lpc3511IpState, 0);
729         epState->stateUnion.stateBitField.stalled = 1U;
730         epState                                   = USB_DeviceLpc3511IpGetEndpointStateStruct(lpc3511IpState, 1);
731         epState->stateUnion.stateBitField.stalled = 1U;
732         /* Enter critical */
733         OSA_ENTER_CRITICAL();
734         USB_LPC3511IP_ENDPOINT_SET_ENDPOINT(lpc3511IpState, 0, 0U, USB_LPC3511IP_ENDPOINT_STALL_MASK, 0U, 0U);
735         USB_LPC3511IP_ENDPOINT_SET_ENDPOINT(lpc3511IpState, 1, 0U, USB_LPC3511IP_ENDPOINT_STALL_MASK, 0U, 0U);
736         /* Exit critical */
737         OSA_EXIT_CRITICAL();
738 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
739         if ((kStatus_USB_Success != USB_DeviceLpc3511IpCancel(lpc3511IpState, 0)) ||
740             (kStatus_USB_Success != USB_DeviceLpc3511IpCancel(lpc3511IpState, 0x80)))
741         {
742             return kStatus_USB_Error;
743         }
744 #else
745         /* cancel the transfer in the endpoint */
746         (void)USB_DeviceLpc3511IpCancel(lpc3511IpState, 0);
747         /* cancel the transfer in the endpoint */
748         (void)USB_DeviceLpc3511IpCancel(lpc3511IpState, 0x80);
749 #endif
750     }
751     else
752     {
753         epState = USB_DeviceLpc3511IpGetEndpointStateStruct(lpc3511IpState, endpointIndex);
754         /* Set endpoint stall flag. */
755         epState->stateUnion.stateBitField.stalled = 1U;
756         /* lpc3511IpState->registerBase->EPINUSE &= (~(0x01u << endpointIndex)); */
757         /* stall the endpoint */
758         USB_LPC3511IP_ENDPOINT_SET_ENDPOINT(lpc3511IpState, endpointIndex, 0U, USB_LPC3511IP_ENDPOINT_STALL_MASK, 0U,
759                                             0U);
760         if ((ep & USB_ENDPOINT_NUMBER_MASK) != USB_CONTROL_ENDPOINT)
761         {
762             /* toggle reset for the toggle */
763             epState->stateUnion.stateBitField.epControlDefault |=
764                 ((USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK) >> USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT);
765             USB_LPC3511IP_ENDPOINT_SET_ENDPOINT(lpc3511IpState, endpointIndex, 1U, USB_LPC3511IP_ENDPOINT_STALL_MASK,
766                                                 0U, 0U);
767         }
768 #if (defined(FSL_FEATURE_USBHSD_VERSION) && (FSL_FEATURE_USBHSD_VERSION >= 300U)) && \
769     (!(defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) &&            \
770        (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK)))
771 #else
772 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
773 #if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
774      (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
775         if (0U != lpc3511IpState->hsInterruptIssue)
776         {
777 #endif
778             if ((0U != lpc3511IpState->controllerSpeed) &&
779                 (USB_ENDPOINT_INTERRUPT == epState->stateUnion.stateBitField.endpointType))
780             {
781                 lpc3511IpState->registerBase->DEVCMDSTAT =
782                     (lpc3511IpState->registerBase->DEVCMDSTAT & ~USB_LPC3511IP_DEVCMDSTAT_W1C_MASK) |
783                     (USB_LPC3511IP_DEVCMDSTAT_INTONNAK_AO_MASK | USB_LPC3511IP_DEVCMDSTAT_INTONNAK_AI_MASK);
784                 epState->stateUnion.stateBitField.epControlDefault &=
785                     (~((USB_LPC3511IP_ENDPOINT_ENDPOINT_TYPE_MASK | USB_LPC3511IP_ENDPOINT_RFTV_MASK) >>
786                        USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT));
787             }
788 #if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
789      (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
790         }
791 #endif
792 #endif
793 #endif
794 
795         /* cancel the transfer in the endpoint */
796 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
797         if (kStatus_USB_Success != USB_DeviceLpc3511IpCancel(lpc3511IpState, ep))
798         {
799             return kStatus_USB_Error;
800         }
801 #else
802         (void)USB_DeviceLpc3511IpCancel(lpc3511IpState, ep);
803 #endif
804     }
805     return kStatus_USB_Success;
806 }
807 
808 /*!
809  * @brief Un-stall a specified endpoint.
810  *
811  * The function is used to un-stall a specified endpoint.
812  * Current transfer of the endpoint will be canceled and the specified endpoint will be un-stalled.
813  *
814  * @param lpc3511IpState      Pointer of the controller state structure.
815  * @param ep               The endpoint address, Bit7, 0U - USB_OUT, 1U - USB_IN.
816  *
817  * @return A USB error code or kStatus_USB_Success.
818  */
USB_DeviceLpc3511IpEndpointUnstall(usb_device_lpc3511ip_state_struct_t * lpc3511IpState,uint8_t ep)819 static usb_status_t USB_DeviceLpc3511IpEndpointUnstall(usb_device_lpc3511ip_state_struct_t *lpc3511IpState, uint8_t ep)
820 {
821     uint8_t endpointIndex = USB_LPC3511IP_ENDPOINT_DES_INDEX(ep);
822     usb_device_lpc3511ip_endpoint_state_struct_t *epState =
823         USB_DeviceLpc3511IpGetEndpointStateStruct(lpc3511IpState, endpointIndex);
824 
825     /* Clear the endpoint stall state, the hardware resets the endpoint
826      * toggle to one for both directions when a setup token is received */
827     epState->stateUnion.stateBitField.stalled = 0U;
828 
829     /* unstall the endpoint for double buffers */
830     USB_LPC3511IP_ENDPOINT_SET_ENDPOINT_AND(lpc3511IpState, endpointIndex, 0, (~USB_LPC3511IP_ENDPOINT_STALL_MASK));
831     if ((ep & USB_ENDPOINT_NUMBER_MASK) != USB_CONTROL_ENDPOINT)
832     {
833         USB_LPC3511IP_ENDPOINT_SET_ENDPOINT_AND(lpc3511IpState, endpointIndex, 1, (~USB_LPC3511IP_ENDPOINT_STALL_MASK));
834 
835         /* toggle reset for the toggle */
836         epState->stateUnion.stateBitField.epControlDefault |=
837             ((USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK) >> USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT);
838 #if (defined(FSL_FEATURE_USBHSD_VERSION) && (FSL_FEATURE_USBHSD_VERSION >= 300U)) && \
839     (!(defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) &&            \
840        (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK)))
841 #else
842 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
843 #if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
844      (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
845         if (0U != lpc3511IpState->hsInterruptIssue)
846         {
847 #endif
848             if ((0U != lpc3511IpState->controllerSpeed) &&
849                 (USB_ENDPOINT_INTERRUPT == epState->stateUnion.stateBitField.endpointType))
850             {
851                 epState->stateUnion.stateBitField.epControlDefault &=
852                     (~((USB_LPC3511IP_ENDPOINT_ENDPOINT_TYPE_MASK | USB_LPC3511IP_ENDPOINT_RFTV_MASK) >>
853                        USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT));
854             }
855 #if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
856      (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
857         }
858 #endif
859 #endif
860 #endif
861     }
862 
863     if (0U != epState->stateUnion.stateBitField.stallPrimed)
864     {
865         epState->stateUnion.stateBitField.stallPrimed = 0u;
866 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
867         if (kStatus_USB_Success != USB_DeviceLpc3511IpTransaction(lpc3511IpState, epState, endpointIndex))
868         {
869             return kStatus_USB_Error;
870         }
871 #else
872         (void)USB_DeviceLpc3511IpTransaction(lpc3511IpState, epState, endpointIndex);
873 #endif
874     }
875     /* cancel the transfer in the endpoint */
876 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
877     if (kStatus_USB_Success != USB_DeviceLpc3511IpCancel(lpc3511IpState, ep))
878     {
879         return kStatus_USB_Error;
880     }
881 #else
882     (void)USB_DeviceLpc3511IpCancel(lpc3511IpState, ep);
883 #endif
884     return kStatus_USB_Success;
885 }
886 
887 #if ((defined(USB_DEVICE_CONFIG_LOW_POWER_MODE)) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
888 /*!
889  * @brief Un-stall a specified endpoint.
890  *
891  * The function is used to un-stall a specified endpoint.
892  * Current transfer of the endpoint will be canceled and the specified endpoint will be un-stalled.
893  *
894  * @param lpc3511IpState      Pointer of the controller state structure.
895  *
896  * @return A USB error code or kStatus_USB_Success.
897  */
USB_DeviceLpc3511IpInterruptSuspend(usb_device_lpc3511ip_state_struct_t * lpc3511IpState)898 static usb_status_t USB_DeviceLpc3511IpInterruptSuspend(usb_device_lpc3511ip_state_struct_t *lpc3511IpState)
899 {
900     usb_device_callback_message_struct_t message;
901 
902     message.buffer  = (uint8_t *)NULL;
903     message.code    = (uint8_t)kUSB_DeviceNotifySuspend;
904     message.length  = 0U;
905     message.isSetup = 0U;
906 
907     /* Notify up layer the USB suspend signal detected. */
908 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
909     if (kStatus_USB_Success != USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message))
910     {
911         return kStatus_USB_Error;
912     }
913 #else
914     (void)USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message);
915 #endif
916 
917     return kStatus_USB_Success;
918 }
919 
920 /*!
921  * @brief Un-stall a specified endpoint.
922  *
923  * The function is used to un-stall a specified endpoint.
924  * Current transfer of the endpoint will be canceled and the specified endpoint will be un-stalled.
925  *
926  * @param lpc3511IpState      Pointer of the controller state structure.
927  *
928  * @return A USB error code or kStatus_USB_Success.
929  */
USB_DeviceLpc3511IpInterruptResume(usb_device_lpc3511ip_state_struct_t * lpc3511IpState)930 static usb_status_t USB_DeviceLpc3511IpInterruptResume(usb_device_lpc3511ip_state_struct_t *lpc3511IpState)
931 {
932     usb_device_callback_message_struct_t message;
933 
934     message.buffer  = (uint8_t *)NULL;
935     message.code    = (uint8_t)kUSB_DeviceNotifyResume;
936     message.length  = 0U;
937     message.isSetup = 0U;
938 
939     /* Notify up layer the USB suspend signal detected. */
940 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
941     if (kStatus_USB_Success != USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message))
942     {
943         return kStatus_USB_Error;
944     }
945 #else
946     (void)USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message);
947 #endif
948 
949     return kStatus_USB_Success;
950 }
951 #if (defined(USB_DEVICE_CONFIG_LPM_L1) && (USB_DEVICE_CONFIG_LPM_L1 > 0U))
952 /*!
953  * @brief Un-stall a specified endpoint.
954  *
955  * The function is used to un-stall a specified endpoint.
956  * Current transfer of the endpoint will be canceled and the specified endpoint will be un-stalled.
957  *
958  * @param lpc3511IpState      Pointer of the controller state structure.
959  *
960  * @return A USB error code or kStatus_USB_Success.
961  */
USB_DeviceLpc3511IpInterruptLPMSleep(usb_device_lpc3511ip_state_struct_t * lpc3511IpState)962 static usb_status_t USB_DeviceLpc3511IpInterruptLPMSleep(usb_device_lpc3511ip_state_struct_t *lpc3511IpState)
963 {
964     usb_device_callback_message_struct_t message;
965 
966     message.buffer  = &lpc3511IpState->lpmRemoteWakeUp;
967     message.code    = (uint8_t)kUSB_DeviceNotifyLPMSleep;
968     message.length  = 0U;
969     message.isSetup = 0U;
970 
971     lpc3511IpState->lpmRemoteWakeUp =
972         (uint8_t)((lpc3511IpState->registerBase->DEVCMDSTAT & USB_LPC3511IP_DEVCMDSTAT_LPM_REWP_MASK) >>
973                   USB_LPC3511IP_DEVCMDSTAT_LPM_REWP_SHIFT);
974 
975     /* Notify up layer the USB suspend signal detected. */
976 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
977     if (kStatus_USB_Success != USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message))
978     {
979         return kStatus_USB_Error;
980     }
981 #else
982     (void)USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message);
983 #endif
984 
985     return kStatus_USB_Success;
986 }
987 
988 #endif
989 #endif
990 
991 /* need copy the data before the trasaction buffer is used again */
USB_DeviceLpc3511IpDoPreviousTransactionMemcpy(usb_device_lpc3511ip_state_struct_t * lpc3511IpState,usb_device_lpc3511ip_endpoint_state_struct_t * epState,uint32_t length,uint8_t endpointIndex,uint8_t odd)992 static void USB_DeviceLpc3511IpDoPreviousTransactionMemcpy(usb_device_lpc3511ip_state_struct_t *lpc3511IpState,
993                                                            usb_device_lpc3511ip_endpoint_state_struct_t *epState,
994                                                            uint32_t length,
995                                                            uint8_t endpointIndex,
996                                                            uint8_t odd)
997 {
998     uint8_t *destBuffer;
999     uint8_t *sourceBuffer;
1000 
1001 #if ((defined(USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY)) && (USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY > 0U))
1002     /*control out doesn't support buffer toggle*/
1003     if (0U == endpointIndex)
1004     {
1005         odd = 0u;
1006     }
1007 #if USB_DEVICE_IP3511_ALL_IP_SUPPORT_RESERVED_BUFFER
1008     if ((0U != epState->epBufferStatusUnion[odd].epBufferStatusField.epPacketCopyed) && (length > 0U) &&
1009         ((endpointIndex & 0x01U) == 0U))
1010 #else
1011     if ((0U != USB_DeviceLpcIp3511MaxPacketNeedCopy(lpc3511IpState)) &&
1012         (0U != epState->epBufferStatusUnion[odd].epBufferStatusField.epPacketCopyed) && (length > 0U) &&
1013         ((endpointIndex & 0x01U) == 0U))
1014 #endif
1015 #else
1016     /* control data buffer align is used */
1017     if (((endpointIndex >> 1U) == USB_CONTROL_ENDPOINT) &&
1018         (epState->epBufferStatusUnion[odd].epBufferStatusField.epPacketCopyed) && (length > 0U) &&
1019         ((endpointIndex & 0x01u) == 0U))
1020 #endif
1021     {
1022 #if ((defined(USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER)) && (USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER > 0U))
1023         destBuffer = &(epState->transferBuffer[epState->transferDone - length]);
1024 #else
1025         destBuffer   = &(epState->transferBuffer[epState->transferDone]);
1026 #endif
1027 #if (defined USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE) && (USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE)
1028 #if (defined USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX) && \
1029     (USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX)
1030         if ((USB_ENDPOINT_BULK == epState->stateUnion.stateBitField.endpointType) && ((endpointIndex & 0x01U) == 0x00U))
1031         {
1032             sourceBuffer = epState->epPacketBuffer + odd * USB_LPC3511IP_GET_MULTIPLE_OF_64(
1033                                                                USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX);
1034         }
1035         else
1036         {
1037             sourceBuffer = epState->epPacketBuffer +
1038                            odd * USB_LPC3511IP_GET_MULTIPLE_OF_64(epState->stateUnion.stateBitField.maxPacketSize);
1039         }
1040 #else
1041         sourceBuffer = epState->epPacketBuffer +
1042                        odd * USB_LPC3511IP_GET_MULTIPLE_OF_64(epState->stateUnion.stateBitField.maxPacketSize);
1043 #endif
1044 #else
1045         sourceBuffer = epState->epPacketBuffer;
1046 #endif
1047         (void)memcpy(destBuffer, sourceBuffer, length);
1048     }
1049 }
1050 
USB_DeviceLpc3511IpTokenUpdate(usb_device_lpc3511ip_state_struct_t * lpc3511IpState,usb_device_lpc3511ip_endpoint_state_struct_t * epState,uint8_t endpointIndex,uint8_t changedOdd)1051 static uint32_t USB_DeviceLpc3511IpTokenUpdate(usb_device_lpc3511ip_state_struct_t *lpc3511IpState,
1052                                                usb_device_lpc3511ip_endpoint_state_struct_t *epState,
1053                                                uint8_t endpointIndex,
1054                                                uint8_t changedOdd)
1055 {
1056     uint32_t length;
1057     uint8_t odd;
1058 
1059 #if (defined USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE) && (USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE)
1060     if (0U != changedOdd)
1061     {
1062         odd = (uint8_t)epState->stateUnion.stateBitField.consumerOdd;
1063         epState->stateUnion.stateBitField.consumerOdd ^= 1U;
1064         epState->stateUnion.stateBitField.doubleBufferBusy--;
1065     }
1066     else
1067 #endif
1068     {
1069         odd = 0U;
1070     }
1071 
1072 /* for OUT packet, compute the actual packet size. */
1073 #if ((defined(FSL_FEATURE_USB_VERSION) && (FSL_FEATURE_USB_VERSION >= 200U)) || \
1074      (defined(FSL_FEATURE_USBHSD_VERSION) && (FSL_FEATURE_USBHSD_VERSION >= 300U)))
1075 #else
1076     if ((endpointIndex & 0x01U) == 0x00u) /* OUT */
1077 #endif
1078     {
1079         /* get the transaction length */
1080         length = *(lpc3511IpState->epCommandStatusList + endpointIndex * 2U + odd);
1081 
1082 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1083         if (0U != lpc3511IpState->controllerSpeed)
1084         {
1085             length =
1086                 (length & USB_LPC3511IPHS_ENDPOINT_BUFFER_NBYTES_MASK) >> USB_LPC3511IPHS_ENDPOINT_BUFFER_NBYTES_SHIFT;
1087         }
1088         else
1089 #endif
1090         {
1091             length =
1092                 (length & USB_LPC3511IPFS_ENDPOINT_BUFFER_NBYTES_MASK) >> USB_LPC3511IPFS_ENDPOINT_BUFFER_NBYTES_SHIFT;
1093         }
1094         length = epState->epBufferStatusUnion[odd].epBufferStatusField.transactionLength - length;
1095     }
1096 #if ((defined(FSL_FEATURE_USB_VERSION) && (FSL_FEATURE_USB_VERSION >= 200U)) || \
1097      (defined(FSL_FEATURE_USBHSD_VERSION) && (FSL_FEATURE_USBHSD_VERSION >= 300U)))
1098 #else
1099     else /* for IN packet, if there is no error, the packet lenght is the primed length. */
1100     {
1101         /* don't judge the actual packet size */
1102         length = epState->epBufferStatusUnion[odd].epBufferStatusField.transactionLength;
1103     }
1104 #endif
1105 
1106 #if !((defined(USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER)) && (USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER > 0U))
1107     USB_DeviceLpc3511IpDoPreviousTransactionMemcpy(lpc3511IpState, epState, length, endpointIndex, odd);
1108 #endif
1109     /* update the transferred length */
1110     epState->transferDone += length;
1111 
1112     return length;
1113 }
1114 
USB_DeviceLpc3511IpInterruptToken(usb_device_lpc3511ip_state_struct_t * lpc3511IpState,uint8_t endpointIndex,uint8_t isSetup,uint32_t errorStatus)1115 static void USB_DeviceLpc3511IpInterruptToken(usb_device_lpc3511ip_state_struct_t *lpc3511IpState,
1116                                               uint8_t endpointIndex,
1117                                               uint8_t isSetup,
1118                                               uint32_t errorStatus)
1119 {
1120     usb_device_callback_message_struct_t message;
1121     uint32_t length;
1122     uint32_t remainLength;
1123     usb_setup_struct_t *setupPacket;
1124     void *temp;
1125     usb_device_lpc3511ip_endpoint_state_struct_t *epState =
1126         USB_DeviceLpc3511IpGetEndpointStateStruct(lpc3511IpState, endpointIndex);
1127 #if (defined USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE) && (USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE)
1128     uint32_t len = 0;
1129 #endif
1130 
1131 #if (defined(FSL_FEATURE_USBHSD_VERSION) && (FSL_FEATURE_USBHSD_VERSION >= 300U)) && \
1132     (!(defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) &&            \
1133        (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK)))
1134 #else
1135 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1136 #if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
1137      (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
1138     if (0U != lpc3511IpState->hsInterruptIssue)
1139     {
1140 #endif
1141         if ((0U != (epState->stateUnion.stateBitField.epControlDefault &
1142                     ((USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK) >> USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT))) &&
1143             (USB_ENDPOINT_INTERRUPT == epState->stateUnion.stateBitField.endpointType) &&
1144             (0U != lpc3511IpState->controllerSpeed) &&
1145             (0U != (lpc3511IpState->epCommandStatusList[epState->stateUnion.stateBitField.consumerOdd +
1146                                                         (((uint32_t)endpointIndex) * 2U)] &
1147                     USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK)))
1148         {
1149             if (0U == (lpc3511IpState->registerBase->EPTOGGLE & ((uint32_t)(0x01UL << endpointIndex))))
1150             {
1151                 uint32_t index;
1152                 length = 0U;
1153                 for (index = 0U; index < ((uint32_t)USB_DEVICE_IP3511_ENDPOINTS_NUM) * 4U; ++index)
1154                 {
1155                     if ((0U !=
1156                          (lpc3511IpState->epCommandStatusList[index] & USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK)) &&
1157                         (USB_ENDPOINT_INTERRUPT ==
1158                          lpc3511IpState->endpointState[index / 2U].stateUnion.stateBitField.endpointType))
1159                     {
1160                         length++;
1161                     }
1162                 }
1163 
1164                 if (length <= 1U)
1165                 {
1166                     lpc3511IpState->registerBase->DEVCMDSTAT &=
1167                         ~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_INTONNAK_AO_MASK | USB_LPC3511IP_DEVCMDSTAT_INTONNAK_AI_MASK);
1168                 }
1169                 epState->stateUnion.stateBitField.epControlDefault &=
1170                     (~((USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK) >> USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT));
1171 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1172                 /* high-speed */
1173                 epState->stateUnion.stateBitField.epControlDefault |=
1174                     ((USB_LPC3511IP_ENDPOINT_RFTV_MASK | USB_LPC3511IP_ENDPOINT_ENDPOINT_TYPE_MASK) >>
1175                      USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT);
1176 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1177                 if (kStatus_USB_Success != USB_DeviceLpc3511IpTransaction(lpc3511IpState, epState, endpointIndex))
1178                 {
1179 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
1180                     usb_echo("prime transaction error\n");
1181 #endif
1182                 }
1183 #else
1184                 (void)USB_DeviceLpc3511IpTransaction(lpc3511IpState, epState, endpointIndex);
1185 #endif
1186 #endif
1187             }
1188             return;
1189         }
1190 #if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
1191      (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
1192     }
1193 #endif
1194 #endif
1195 #endif
1196 
1197     if ((0U == isSetup) && (0U == epState->stateUnion.stateBitField.transferring))
1198     {
1199         return;
1200     }
1201     if (0U != isSetup)
1202     {
1203         message.length = 8U;
1204         message.buffer = (lpc3511IpState->setupData);
1205         /* clear the primed control transactions */
1206         if (0U != (epState->stateUnion.stateBitField.transferring))
1207         {
1208             epState->stateUnion.stateBitField.transferring = 0U;
1209             if (0U != (lpc3511IpState->epCommandStatusList[0] & USB_LPC3511IP_ENDPOINT_ACTIVE_MASK))
1210             {
1211 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1212                 if (kStatus_USB_Success != USB_DeviceLpc3511IpCancel(lpc3511IpState, USB_CONTROL_ENDPOINT))
1213                 {
1214                     return;
1215                 }
1216 #else
1217                 (void)USB_DeviceLpc3511IpCancel(lpc3511IpState, USB_CONTROL_ENDPOINT);
1218 #endif
1219             }
1220         }
1221         if (0U != (lpc3511IpState->endpointState[1].stateUnion.stateBitField.transferring))
1222         {
1223             lpc3511IpState->endpointState[1].stateUnion.stateBitField.transferring = 0U;
1224             if (0U != (lpc3511IpState->epCommandStatusList[2] & USB_LPC3511IP_ENDPOINT_ACTIVE_MASK))
1225             {
1226 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1227                 if (kStatus_USB_Success != USB_DeviceLpc3511IpCancel(lpc3511IpState, (0x80u | USB_CONTROL_ENDPOINT)))
1228                 {
1229 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
1230                     usb_echo("cancel the pending transfer error\n");
1231 #endif
1232                 }
1233 #else
1234                 (void)USB_DeviceLpc3511IpCancel(lpc3511IpState, (0x80u | USB_CONTROL_ENDPOINT));
1235 #endif
1236             }
1237         }
1238 
1239         USB_LPC3511IP_ENDPOINT_SET_ENDPOINT_AND(
1240             lpc3511IpState, 0, 0, (~(USB_LPC3511IP_ENDPOINT_STALL_MASK | USB_LPC3511IP_ENDPOINT_ACTIVE_MASK)));
1241         USB_LPC3511IP_ENDPOINT_SET_ENDPOINT_AND(
1242             lpc3511IpState, 1, 0, (~(USB_LPC3511IP_ENDPOINT_STALL_MASK | USB_LPC3511IP_ENDPOINT_ACTIVE_MASK)));
1243 
1244         lpc3511IpState->registerBase->INTSTAT = 0x03u; /* clear interrupt */
1245         /* W1 to clear the setup flag */
1246         lpc3511IpState->registerBase->DEVCMDSTAT = (lpc3511IpState->registerBase->DEVCMDSTAT & ~USB_LPC3511IP_DEVCMDSTAT_W1C_MASK) | USB_LPC3511IP_DEVCMDSTAT_SETUP_MASK;
1247     }
1248     else
1249     {
1250         length = 0U;
1251 #if (defined USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE) && (USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE)
1252         if (0U != (lpc3511IpState->epCommandStatusList[epState->stateUnion.stateBitField.consumerOdd +
1253                                                        (((uint32_t)endpointIndex) * 2U)] &
1254                    USB_LPC3511IP_ENDPOINT_ACTIVE_MASK))
1255         {
1256             return;
1257         }
1258 #else
1259         if (0U != (lpc3511IpState->epCommandStatusList[endpointIndex * 2U] & USB_LPC3511IP_ENDPOINT_ACTIVE_MASK))
1260         {
1261             return;
1262         }
1263 #endif
1264 
1265 #if (defined USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE) && (USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE)
1266         if ((endpointIndex >> 1U) != USB_CONTROL_ENDPOINT)
1267         {
1268             len = USB_DeviceLpc3511IpTokenUpdate(lpc3511IpState, epState, endpointIndex, 1U);
1269             length += len;
1270 
1271             if ((epState->stateUnion.stateBitField.doubleBufferBusy > 0U) &&
1272                 (0U == (lpc3511IpState->epCommandStatusList[epState->stateUnion.stateBitField.consumerOdd +
1273                                                             (((uint32_t)endpointIndex) * 2U)] &
1274                         USB_LPC3511IP_ENDPOINT_ACTIVE_MASK)))
1275             {
1276 #if ((defined(USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER)) && (USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER > 0U))
1277                 USB_DeviceLpc3511IpDoPreviousTransactionMemcpy(
1278                     lpc3511IpState, epState, len, endpointIndex,
1279                     (uint8_t)(epState->stateUnion.stateBitField.consumerOdd ^ 1U));
1280 #endif
1281                 len = USB_DeviceLpc3511IpTokenUpdate(lpc3511IpState, epState, endpointIndex, 1U);
1282                 length += len;
1283             }
1284         }
1285         else
1286 #endif
1287         {
1288             length = USB_DeviceLpc3511IpTokenUpdate(lpc3511IpState, epState, endpointIndex, 0U);
1289             len    = length;
1290         }
1291 
1292         /* update remaining length */
1293         remainLength = epState->transferLength - epState->transferDone;
1294 
1295         /* Whether the transfer is completed or not.
1296          * The transfer is completed when one of the following conditions meet:
1297          * 1. The remaining length is zero.
1298          * 2. The length of current tansaction is not the multiple of max packet size.
1299          */
1300         if ((length > 0U) && (0U == (length % epState->stateUnion.stateBitField.maxPacketSize)) && (remainLength > 0U))
1301         {
1302 #if ((defined(USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER)) && (USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER > 0U))
1303             if ((endpointIndex >> 1U) == USB_CONTROL_ENDPOINT)
1304             {
1305                 /* do memcpy firstly */
1306                 USB_DeviceLpc3511IpDoPreviousTransactionMemcpy(lpc3511IpState, epState, len, endpointIndex, 0U);
1307 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1308                 if (kStatus_USB_Success != USB_DeviceLpc3511IpTransaction(lpc3511IpState, epState, endpointIndex))
1309                 {
1310 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
1311                     usb_echo("prime transaction error\n");
1312 #endif
1313                 }
1314 #else
1315                 (void)USB_DeviceLpc3511IpTransaction(lpc3511IpState, epState, endpointIndex);
1316 #endif
1317             }
1318             else
1319             {
1320 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1321                 if (kStatus_USB_Success != USB_DeviceLpc3511IpTransaction(lpc3511IpState, epState, endpointIndex))
1322                 {
1323 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
1324                     usb_echo("prime transaction error\n");
1325 #endif
1326                 }
1327 #else
1328                 (void)USB_DeviceLpc3511IpTransaction(lpc3511IpState, epState, endpointIndex);
1329 #endif
1330 
1331                 USB_DeviceLpc3511IpDoPreviousTransactionMemcpy(
1332                     lpc3511IpState, epState, len, endpointIndex,
1333                     (uint8_t)(epState->stateUnion.stateBitField.consumerOdd ^ 1U));
1334             }
1335 #else
1336 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1337             if (kStatus_USB_Success != USB_DeviceLpc3511IpTransaction(lpc3511IpState, epState, endpointIndex))
1338             {
1339                 return kStatus_USB_Error;
1340             }
1341 #else
1342             (void)USB_DeviceLpc3511IpTransaction(lpc3511IpState, epState, endpointIndex);
1343 #endif
1344 #endif
1345             return;
1346         }
1347         else
1348         {
1349             epState->stateUnion.stateBitField.transferring = 0U;
1350             message.length                                 = epState->transferDone;
1351             message.buffer                                 = epState->transferBuffer;
1352 
1353             /* process ZLT
1354              * 1. IN endpoint;
1355              * 2. transfer length is the multiple of max packet size.
1356              */
1357             if ((0U != (endpointIndex & 0x01U)) && (0U != length) &&
1358                 (0U == (length % epState->stateUnion.stateBitField.maxPacketSize)))
1359             {
1360                 if ((endpointIndex >> 1U) == USB_CONTROL_ENDPOINT)
1361                 {
1362                     temp        = (void *)(&(lpc3511IpState->setupData[0]));
1363                     setupPacket = (usb_setup_struct_t *)temp;
1364                     /*
1365                      * Send ZLT transaction if setup transfer and the required length is longer than actual length
1366                      */
1367                     if (USB_SHORT_FROM_LITTLE_ENDIAN(setupPacket->wLength) > epState->transferLength)
1368                     {
1369 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1370                         if (kStatus_USB_Success !=
1371                             USB_DeviceLpc3511IpEndpointPrime(lpc3511IpState, epState, 1U, NULL, 0U))
1372                         {
1373 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
1374                             usb_echo("prime error\n");
1375 #endif
1376                         }
1377 #else
1378                         (void)USB_DeviceLpc3511IpEndpointPrime(lpc3511IpState, epState, 1U, NULL, 0U);
1379 #endif
1380                         return;
1381                     }
1382                 }
1383                 else if ((0U != epState->stateUnion.stateBitField.zlt))
1384                 {
1385 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1386                     if (kStatus_USB_Success !=
1387                         USB_DeviceLpc3511IpEndpointPrime(lpc3511IpState, epState, endpointIndex, NULL, 0U))
1388                     {
1389 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
1390                         usb_echo("prime error\n");
1391 #endif
1392                     }
1393 #else
1394                     (void)USB_DeviceLpc3511IpEndpointPrime(lpc3511IpState, epState, endpointIndex, NULL, 0U);
1395 #endif
1396                     return;
1397                 }
1398                 else
1399                 {
1400                     /*no action*/
1401                 }
1402             }
1403         }
1404     }
1405 
1406     message.isSetup = isSetup;
1407     message.code    = ((uint8_t)(endpointIndex >> 1) | (uint8_t)(((endpointIndex & 0x01U) << 0x07U)));
1408 
1409 #if ((defined(USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER)) && (USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER > 0U))
1410     USB_DeviceLpc3511IpDoPreviousTransactionMemcpy(lpc3511IpState, epState, len, endpointIndex,
1411                                                    (uint8_t)(epState->stateUnion.stateBitField.consumerOdd ^ 1U));
1412 #endif
1413     /* Notify the up layer the controller status changed. */
1414 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1415     if (kStatus_USB_Success != USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message))
1416     {
1417 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
1418         usb_echo("notification error\n");
1419 #endif
1420     }
1421 #else
1422     (void)USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message);
1423 #endif
1424 }
1425 
1426 /*!
1427  * @brief Handle the USB bus reset interrupt.
1428  *
1429  * The function is used to handle the USB bus reset interrupt.
1430  *
1431  * @param lpc3511IpState       Pointer of the controller state structure.
1432  *
1433  */
USB_DeviceLpc3511IpInterruptReset(usb_device_lpc3511ip_state_struct_t * lpc3511IpState)1434 static void USB_DeviceLpc3511IpInterruptReset(usb_device_lpc3511ip_state_struct_t *lpc3511IpState)
1435 {
1436     usb_device_callback_message_struct_t message;
1437 
1438     /* Set reset flag */
1439     lpc3511IpState->isResetting = 1U;
1440 
1441 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1442     if (0U != lpc3511IpState->controllerSpeed)
1443     {
1444         if (((lpc3511IpState->registerBase->DEVCMDSTAT & USBHSD_DEVCMDSTAT_Speed_MASK) >>
1445              USBHSD_DEVCMDSTAT_Speed_SHIFT) == 0x02U)
1446         {
1447             lpc3511IpState->deviceSpeed = USB_SPEED_HIGH;
1448         }
1449         else if (((lpc3511IpState->registerBase->DEVCMDSTAT & USBHSD_DEVCMDSTAT_Speed_MASK) >>
1450                   USBHSD_DEVCMDSTAT_Speed_SHIFT) == 0x01U)
1451         {
1452             lpc3511IpState->deviceSpeed = USB_SPEED_FULL;
1453         }
1454         else
1455         {
1456             /*no action*/
1457         }
1458     }
1459     else
1460 #endif
1461     {
1462         lpc3511IpState->deviceSpeed = USB_SPEED_FULL;
1463     }
1464 
1465     message.buffer  = (uint8_t *)NULL;
1466     message.code    = (uint8_t)kUSB_DeviceNotifyBusReset;
1467     message.length  = 0U;
1468     message.isSetup = 0U;
1469     /* Notify up layer the USB bus reset signal detected. */
1470 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1471     if (kStatus_USB_Success != USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message))
1472     {
1473 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
1474         usb_echo("notification error\n");
1475 #endif
1476     }
1477 #else
1478     (void)USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message);
1479 #endif
1480 }
1481 
1482 #if (defined(USB_DEVICE_CONFIG_DETACH_ENABLE) && (USB_DEVICE_CONFIG_DETACH_ENABLE))
1483 /*!
1484  * @brief Handle detach interrupt.
1485  *
1486  * The function is used to handle the detach interrupt.
1487  *
1488  * @param lpc3511IpState       Pointer of the controller state structure.
1489  *
1490  */
USB_DeviceLpc3511IpInterruptDetach(usb_device_lpc3511ip_state_struct_t * lpc3511IpState)1491 static void USB_DeviceLpc3511IpInterruptDetach(usb_device_lpc3511ip_state_struct_t *lpc3511IpState)
1492 {
1493     usb_device_callback_message_struct_t message;
1494 
1495     message.buffer  = (uint8_t *)NULL;
1496     message.code    = (uint8_t)kUSB_DeviceNotifyDetach;
1497     message.length  = 0U;
1498     message.isSetup = 0U;
1499 
1500     /* Notify up layer the USB VBUS falling signal detected. */
1501 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1502     if (kStatus_USB_Success != USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message))
1503     {
1504 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
1505         usb_echo("notification error\n");
1506 #endif
1507     }
1508 #else
1509     (void)USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message);
1510 #endif
1511 }
1512 
1513 /*!
1514  * @brief Handle Attach interrupt.
1515  *
1516  * The function is used to handle the attach interrupt.
1517  *
1518  * @param lpc3511IpState       Pointer of the controller state structure.
1519  *
1520  */
USB_DeviceLpc3511IpInterruptAttach(usb_device_lpc3511ip_state_struct_t * lpc3511IpState)1521 static void USB_DeviceLpc3511IpInterruptAttach(usb_device_lpc3511ip_state_struct_t *lpc3511IpState)
1522 {
1523     usb_device_callback_message_struct_t message;
1524 
1525     message.buffer  = (uint8_t *)NULL;
1526     message.code    = (uint8_t)kUSB_DeviceNotifyAttach;
1527     message.length  = 0U;
1528     message.isSetup = 0U;
1529 
1530     /* Notify up layer the USB VBUS rising signal detected. */
1531 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1532     if (kStatus_USB_Success != USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message))
1533     {
1534 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
1535         usb_echo("notification error\n");
1536 #endif
1537     }
1538 #else
1539     (void)USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message);
1540 #endif
1541 }
1542 #endif
1543 #if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U)) && \
1544     (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
1545 /* The device dcd callback */
USB_DeviceLpcIp3511IsrHSDCDCallback(void * handle,uint32_t event,void * param)1546 static usb_hsdcd_status_t USB_DeviceLpcIp3511IsrHSDCDCallback(void *handle, uint32_t event, void *param)
1547 {
1548     usb_hsdcd_status_t error = kStatus_hsdcd_Success;
1549     usb_device_callback_message_struct_t message;
1550     usb_device_lpc3511ip_state_struct_t *lpc3511IpState = (usb_device_lpc3511ip_state_struct_t *)handle;
1551 
1552     if (lpc3511IpState == NULL)
1553     {
1554         return kStatus_hsdcd_Error;
1555     }
1556 
1557     /*messsgae buffer contain event information*/
1558     message.buffer  = (uint8_t *)param;
1559     message.length  = 0U;
1560     message.isSetup = 0U;
1561     message.code    = (uint8_t)kUSB_DeviceNotifyDcdDetectFinished;
1562 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1563     if (kStatus_USB_Success != USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message))
1564     {
1565         return kStatus_USB_Error;
1566     }
1567 #else
1568     (void)USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message);
1569 #endif
1570     return error;
1571 }
1572 
USB_DeviceLpcIp3511IsrDCDFunction(void * deviceHandle)1573 void USB_DeviceLpcIp3511IsrDCDFunction(void *deviceHandle)
1574 {
1575     usb_device_struct_t *handle = (usb_device_struct_t *)deviceHandle;
1576     usb_device_lpc3511ip_state_struct_t *lpc3511IpState;
1577     if (NULL == deviceHandle)
1578     {
1579         return;
1580     }
1581     lpc3511IpState = (usb_device_lpc3511ip_state_struct_t *)(handle->controllerHandle);
1582     USB_HSDcdIsrFunction(lpc3511IpState->dcdHandle);
1583 }
1584 #endif
USB_DeviceLpc3511IpInit(uint8_t controllerId,usb_device_handle handle,usb_device_controller_handle * controllerHandle)1585 usb_status_t USB_DeviceLpc3511IpInit(uint8_t controllerId,
1586                                      usb_device_handle handle,
1587                                      usb_device_controller_handle *controllerHandle)
1588 {
1589     usb_device_lpc3511ip_state_struct_t *lpc3511IpState = NULL;
1590 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511FS)) && (USB_DEVICE_CONFIG_LPCIP3511FS > 0U))
1591     uint32_t ip3511FsBases[] = USB_BASE_ADDRS;
1592 #endif
1593     uint32_t *endpointListArray[] = LPC_CONTROLLER_ENDPOINT_LIST_ARRAY;
1594 
1595 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1596 #if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U)) && \
1597     (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
1598     uint32_t hsdcd_base[] = USBHSDCD_BASE_ADDRS;
1599     USBHSDCD_Type *base;
1600     usb_hsdcd_config_struct_t dcdParamConfig;
1601     usb_hsdcd_status_t dcdError = kStatus_hsdcd_Success;
1602 #endif
1603 
1604     uint32_t ip3511HsBases[] = USBHSD_BASE_ADDRS;
1605     if ((controllerId >= (uint8_t)kUSB_ControllerLpcIp3511Hs0) &&
1606         (controllerId <= (uint8_t)kUSB_ControllerLpcIp3511Hs1))
1607     {
1608         if (((uint32_t)controllerId - (uint32_t)kUSB_ControllerLpcIp3511Hs0) >=
1609             (sizeof(ip3511HsBases) / sizeof(uint32_t)))
1610         {
1611             return kStatus_USB_ControllerNotFound;
1612         }
1613         lpc3511IpState = &s_UsbDeviceLpc3511IpState[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Hs0 +
1614                                                     USB_DEVICE_CONFIG_LPCIP3511FS];
1615 #if (!(defined(USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY) && (USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY)))
1616         lpc3511IpState->controlData =
1617             (uint8_t *)&s_SetupAndEpReservedData[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Hs0 +
1618                                                  USB_DEVICE_CONFIG_LPCIP3511FS][CONTROL_TRANSFER_DATA_OFFSET];
1619         lpc3511IpState->setupData =
1620             (uint8_t *)&s_SetupAndEpReservedData[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Hs0 +
1621                                                  USB_DEVICE_CONFIG_LPCIP3511FS][SETUP_TRANSFER_DATA_OFFSET];
1622         lpc3511IpState->zeroTransactionData =
1623             (uint8_t *)&s_SetupAndEpReservedData[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Hs0 +
1624                                                  USB_DEVICE_CONFIG_LPCIP3511FS][ZERO_TRANSFER_DATA_OFFSET];
1625 #endif
1626         /* set the endpoint list */
1627         lpc3511IpState->epCommandStatusList =
1628             endpointListArray[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Hs0 + USB_DEVICE_CONFIG_LPCIP3511FS];
1629         /* get the ip base address */
1630         lpc3511IpState->registerBase =
1631             (USB_LPC3511IP_Type *)ip3511HsBases[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Hs0];
1632 #if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U)) && \
1633     (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
1634         base                       = (USBHSDCD_Type *)hsdcd_base[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Hs0];
1635         dcdParamConfig.dcdCallback = USB_DeviceLpcIp3511IsrHSDCDCallback;
1636         dcdParamConfig.dcdCallbackParam = (void *)lpc3511IpState;
1637         dcdError                        = USB_HSDCD_Init(base, &dcdParamConfig, &lpc3511IpState->dcdHandle);
1638         if (kStatus_hsdcd_Success != dcdError)
1639         {
1640             return kStatus_USB_Error;
1641         }
1642 #endif
1643     }
1644     else
1645 #endif
1646 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511FS)) && (USB_DEVICE_CONFIG_LPCIP3511FS > 0U))
1647     {
1648         /* get the controller instance */
1649         if ((controllerId < (uint8_t)kUSB_ControllerLpcIp3511Fs0) ||
1650             ((controllerId - (uint8_t)kUSB_ControllerLpcIp3511Fs0) >= (uint8_t)USB_DEVICE_CONFIG_LPCIP3511FS) ||
1651             (((uint32_t)controllerId - (uint32_t)kUSB_ControllerLpcIp3511Fs0) >=
1652              (sizeof(ip3511FsBases) / sizeof(uint32_t))))
1653         {
1654             return kStatus_USB_ControllerNotFound;
1655         }
1656         lpc3511IpState = &s_UsbDeviceLpc3511IpState[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Fs0];
1657 #if (!(defined(USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY) && (USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY)))
1658         lpc3511IpState->controlData =
1659             (uint8_t *)&s_SetupAndEpReservedData[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Fs0]
1660                                                 [CONTROL_TRANSFER_DATA_OFFSET];
1661         lpc3511IpState->setupData =
1662             (uint8_t *)&s_SetupAndEpReservedData[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Fs0]
1663                                                 [SETUP_TRANSFER_DATA_OFFSET];
1664         lpc3511IpState->zeroTransactionData =
1665             (uint8_t *)&s_SetupAndEpReservedData[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Fs0]
1666                                                 [ZERO_TRANSFER_DATA_OFFSET];
1667 #endif
1668         /* set the endpoint list */
1669         lpc3511IpState->epCommandStatusList = endpointListArray[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Fs0];
1670         /* get the ip base address */
1671         lpc3511IpState->registerBase =
1672             (USB_LPC3511IP_Type *)ip3511FsBases[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Fs0];
1673     }
1674 #else
1675     {
1676         return kStatus_USB_ControllerNotFound;
1677     }
1678 #endif
1679 
1680     lpc3511IpState->controllerId = controllerId;
1681 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1682     if ((lpc3511IpState->controllerId >= (uint8_t)kUSB_ControllerLpcIp3511Hs0) &&
1683         (lpc3511IpState->controllerId <= (uint8_t)kUSB_ControllerLpcIp3511Hs1))
1684     {
1685         lpc3511IpState->controllerSpeed = 1U;
1686 #if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
1687      (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
1688         lpc3511IpState->hsInterruptIssue = ((Chip_GetVersion() == FSL_ROM_VERSION_1B) ? 0U : 1U);
1689 #endif
1690     }
1691     else
1692     {
1693         lpc3511IpState->controllerSpeed = 0U;
1694     }
1695 #endif
1696 
1697 #if defined(USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY) && (USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY)
1698     lpc3511IpState->controlData         = USB_DeviceLpcIp3511MallocMaxPacketBuffer(CONTROL_TRANSFER_DATA_SIZE);
1699     lpc3511IpState->setupData           = USB_DeviceLpcIp3511MallocMaxPacketBuffer(SETUP_TRANSFER_DATA_SIZE);
1700     lpc3511IpState->zeroTransactionData = USB_DeviceLpcIp3511MallocMaxPacketBuffer(ZERO_TRANSFER_DATA_SIZE);
1701 #endif
1702 
1703     /* disable the controller */
1704     lpc3511IpState->registerBase->DEVCMDSTAT &=
1705         (~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_DCON_MASK | USB_LPC3511IP_DEVCMDSTAT_DEV_EN_MASK |
1706            USB_LPC3511IP_DEVCMDSTAT_LPM_SUP_MASK));
1707     /* reset and enalbe the controller */
1708     USB_DeviceLpc3511IpSetDefaultState(lpc3511IpState);
1709     /* enable USB */
1710     lpc3511IpState->registerBase->DEVCMDSTAT = (lpc3511IpState->registerBase->DEVCMDSTAT & ~USB_LPC3511IP_DEVCMDSTAT_W1C_MASK) | (USB_LPC3511IP_DEVCMDSTAT_DEV_EN_MASK
1711 #if ((defined(USB_DEVICE_CONFIG_LOW_POWER_MODE)) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
1712 #else
1713                                                  | USB_LPC3511IP_DEVCMDSTAT_FORCE_NEEDCLK_MASK
1714 #endif
1715     );
1716 #if (defined(USB_DEVICE_CONFIG_LPM_L1) && (USB_DEVICE_CONFIG_LPM_L1 > 0U))
1717     lpc3511IpState->registerBase->DEVCMDSTAT = (lpc3511IpState->registerBase->DEVCMDSTAT & ~USB_LPC3511IP_DEVCMDSTAT_W1C_MASK) | USB_LPC3511IP_DEVCMDSTAT_LPM_SUP_MASK;
1718     lpc3511IpState->registerBase->LPM |= USB_LPC3511IP_USB_LPM_HIRD_SW(4);
1719     lpc3511IpState->registerBase->DEVCMDSTAT &= ~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_FORCE_NEEDCLK_MASK);
1720 #endif
1721     lpc3511IpState->deviceHandle = handle;
1722     *controllerHandle            = lpc3511IpState;
1723 
1724     return kStatus_USB_Success;
1725 }
1726 
USB_DeviceLpc3511IpDeinit(usb_device_controller_handle controllerHandle)1727 usb_status_t USB_DeviceLpc3511IpDeinit(usb_device_controller_handle controllerHandle)
1728 {
1729     usb_device_lpc3511ip_state_struct_t *lpc3511IpState = (usb_device_lpc3511ip_state_struct_t *)controllerHandle;
1730     uint32_t usbAddress;
1731     usb_status_t status = kStatus_USB_Success;
1732     if (controllerHandle == NULL)
1733     {
1734         return kStatus_USB_InvalidHandle;
1735     }
1736     /* Clear all interrupt flags. */
1737     lpc3511IpState->registerBase->INTSTAT = (USB_LPC3511IP_INTSTAT_DEV_INT_MASK | USB_LPC3511IP_INTSTAT_FRAME_INT_MASK |
1738                                              USB_LPC3511IP_MAX_PHY_ENDPOINT_MASK);
1739     /* Disable all interrupts. */
1740     lpc3511IpState->registerBase->INTEN = 0U;
1741     /* Clear device address. */
1742     usbAddress = 0U;
1743     status     = USB_DeviceLpc3511IpControlPreSetDeviceAddress(controllerHandle, &usbAddress);
1744     if (kStatus_USB_Success == status)
1745     {
1746         /*no action, just for misra4.7*/
1747     }
1748 #if (defined(USB_DEVICE_CONFIG_LPM_L1) && (USB_DEVICE_CONFIG_LPM_L1 > 0U))
1749     lpc3511IpState->registerBase->DEVCMDSTAT &= ~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_LPM_SUP_MASK);
1750 #endif
1751     /* disable the controller */
1752     lpc3511IpState->registerBase->DEVCMDSTAT &=
1753         (~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_DCON_MASK | USB_LPC3511IP_DEVCMDSTAT_DEV_EN_MASK |
1754            USB_LPC3511IP_DEVCMDSTAT_FORCE_NEEDCLK_MASK));
1755 #if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U)) && \
1756     (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
1757 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1758     if (kStatus_USB_Success != USB_HSDCD_Deinit(lpc3511IpState->dcdHandle))
1759     {
1760         return kStatus_USB_Error;
1761     }
1762 #else
1763     (void)USB_HSDCD_Deinit(lpc3511IpState->dcdHandle);
1764 #endif
1765 #endif
1766 
1767 #if defined(USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY) && (USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY)
1768     USB_DeviceLpcIp3511ReleaseMaxPacketBuffer(lpc3511IpState->controlData, CONTROL_TRANSFER_DATA_SIZE);
1769     USB_DeviceLpcIp3511ReleaseMaxPacketBuffer(lpc3511IpState->setupData, SETUP_TRANSFER_DATA_SIZE);
1770     USB_DeviceLpcIp3511ReleaseMaxPacketBuffer(lpc3511IpState->zeroTransactionData, ZERO_TRANSFER_DATA_SIZE);
1771 #endif
1772 
1773     return status;
1774 }
1775 
USB_DeviceLpc3511IpGetActualBufferAndPrime(usb_device_lpc3511ip_state_struct_t * lpc3511IpState,usb_device_lpc3511ip_endpoint_state_struct_t * epState,uint8_t endpointIndex,uint8_t changedOdd)1776 static usb_status_t USB_DeviceLpc3511IpGetActualBufferAndPrime(usb_device_lpc3511ip_state_struct_t *lpc3511IpState,
1777                                                                usb_device_lpc3511ip_endpoint_state_struct_t *epState,
1778                                                                uint8_t endpointIndex,
1779                                                                uint8_t changedOdd)
1780 {
1781     uint8_t *destBuffer;
1782     uint8_t *actualBuffer;
1783     uint32_t length;
1784     uint8_t odd;
1785     uint8_t index;
1786 
1787 #if (defined USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE) && (USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE)
1788     if (0U != changedOdd)
1789     {
1790         odd = (uint8_t)epState->stateUnion.stateBitField.producerOdd;
1791     }
1792     else
1793 #endif
1794     {
1795         odd = 0;
1796     }
1797     actualBuffer = epState->transferBuffer + epState->transferPrimedLength;
1798     length       = epState->transferLength - epState->transferPrimedLength;
1799     /* Data length needs to less than max packet size. */
1800 #if ((defined(USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX)) && \
1801      (USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX > 0U))
1802     if ((USB_ENDPOINT_BULK == epState->stateUnion.stateBitField.endpointType) && ((endpointIndex & 0x01U) == 0x00U))
1803     {
1804         if (length > USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX)
1805         {
1806             length = USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX;
1807         }
1808     }
1809     else
1810     {
1811         if (length > epState->stateUnion.stateBitField.maxPacketSize)
1812         {
1813             length = epState->stateUnion.stateBitField.maxPacketSize;
1814         }
1815     }
1816 #else
1817     if (length > epState->stateUnion.stateBitField.maxPacketSize)
1818     {
1819         length = epState->stateUnion.stateBitField.maxPacketSize;
1820     }
1821 #endif
1822 
1823     epState->epBufferStatusUnion[odd].epBufferStatusField.epPacketCopyed = 0;
1824 
1825     index = (endpointIndex & 0x01u); /* index mean IN endpoint here */
1826     if (length > 0U)
1827     {
1828 #if ((defined(USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY)) && (USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY > 0U))
1829 /* if all the enabled IP support the reserved buffer, then don't need the judgement. */
1830 #if (!USB_DEVICE_IP3511_ALL_IP_SUPPORT_RESERVED_BUFFER)
1831 #define USB_DEVICE_IP3511_NEED_CHECK_BUFFER (1u)
1832         /* lengt > 0 && ((buffer not align with 64) || (buffer is not in the deticated ram))) */
1833         if (USB_DeviceLpcIp3511MaxPacketNeedCopy(lpc3511IpState))
1834 #endif
1835 #else
1836 #define USB_DEVICE_IP3511_NEED_CHECK_BUFFER (1u)
1837         /* align the buffer for control transfer */
1838         if (((endpointIndex >> 1U) == USB_CONTROL_ENDPOINT))
1839 #endif
1840         {
1841 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1842 #if ((defined(FSL_FEATURE_USBHSD_USB_RAM_BASE_ADDRESS)) && (FSL_FEATURE_USBHSD_USB_RAM_BASE_ADDRESS))
1843             uint32_t bufferValue = (uint32_t)actualBuffer;
1844 #if ((defined(__SAUREGION_PRESENT)) && (__SAUREGION_PRESENT > 0U))
1845             bufferValue &= (0xEFFFFFFFu); /* bit28 is the secure address label */
1846 #endif
1847 #endif
1848 #endif
1849             /* not 64 bytes align || not in the dedicated ram || ( OUT && not mutiple of 4 ) */
1850             if ((((uint32_t)actualBuffer & 0x0000003FU) != 0U) ||
1851 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1852 #if ((defined(FSL_FEATURE_USBHSD_USB_RAM_BASE_ADDRESS)) && (FSL_FEATURE_USBHSD_USB_RAM_BASE_ADDRESS))
1853                 (
1854 #endif
1855 #endif
1856                     (((uint32_t)actualBuffer & 0xFFC00000U) !=
1857                      (lpc3511IpState->registerBase->DATABUFSTART & 0xFFC00000U))
1858 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1859 #if ((defined(FSL_FEATURE_USBHSD_USB_RAM_BASE_ADDRESS)) && (FSL_FEATURE_USBHSD_USB_RAM_BASE_ADDRESS))
1860 
1861                     || ((0U != lpc3511IpState->controllerSpeed) &&
1862                         ((bufferValue < (uint32_t)FSL_FEATURE_USBHSD_USB_RAM_BASE_ADDRESS) ||
1863                          (bufferValue >
1864                           ((uint32_t)FSL_FEATURE_USBHSD_USB_RAM_BASE_ADDRESS + (uint32_t)FSL_FEATURE_USBHSD_USB_RAM))))
1865 #endif
1866 #endif
1867 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1868 #if ((defined(FSL_FEATURE_USBHSD_USB_RAM_BASE_ADDRESS)) && (FSL_FEATURE_USBHSD_USB_RAM_BASE_ADDRESS))
1869                         )
1870 #endif
1871 #endif
1872 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1873                 || ((0U != lpc3511IpState->controllerSpeed) && (0U == index) &&
1874                     (length != epState->stateUnion.stateBitField.maxPacketSize)))
1875 #else
1876             )
1877 #endif
1878             {
1879                 epState->epBufferStatusUnion[odd].epBufferStatusField.epPacketCopyed = 1U;
1880 /* usb copy buffer for this packet */
1881 #if (defined USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE) && (USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE)
1882 #if ((defined(USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX)) && \
1883      (USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX > 0U))
1884                 if ((USB_ENDPOINT_BULK == epState->stateUnion.stateBitField.endpointType) &&
1885                     ((endpointIndex & 0x01U) == 0x00U))
1886                 {
1887                     destBuffer = (uint8_t *)(epState->epPacketBuffer +
1888                                              (odd * USB_LPC3511IP_GET_MULTIPLE_OF_64(
1889                                                         USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX)));
1890                 }
1891                 else
1892                 {
1893                     destBuffer = (uint8_t *)(epState->epPacketBuffer +
1894                                              (odd * USB_LPC3511IP_GET_MULTIPLE_OF_64(
1895                                                         epState->stateUnion.stateBitField.maxPacketSize)));
1896                 }
1897 #else
1898                 destBuffer =
1899                     (uint8_t *)(epState->epPacketBuffer + (odd * USB_LPC3511IP_GET_MULTIPLE_OF_64(
1900                                                                      epState->stateUnion.stateBitField.maxPacketSize)));
1901 #endif
1902 #else
1903                 destBuffer = (uint8_t *)(epState->epPacketBuffer);
1904 #endif
1905                 if (0U != index) /* USB_IN */
1906                 {
1907                     (void)memcpy(destBuffer, actualBuffer, length);
1908                 }
1909                 else
1910                 {
1911 #if ((defined(FSL_FEATURE_USB_VERSION) && (FSL_FEATURE_USB_VERSION >= 200U)) || \
1912      (defined(FSL_FEATURE_USBHSD_VERSION) && (FSL_FEATURE_USBHSD_VERSION >= 300U)))
1913 #else
1914 #if ((defined(USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX)) && \
1915      (USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX > 0U))
1916                     if (length < epState->stateUnion.stateBitField.maxPacketSize)
1917                     {
1918                         length = epState->stateUnion.stateBitField.maxPacketSize;
1919                     }
1920                     else
1921                     {
1922                         length = ((length + epState->stateUnion.stateBitField.maxPacketSize - 1) /
1923                                   epState->stateUnion.stateBitField.maxPacketSize) *
1924                                  (epState->stateUnion.stateBitField.maxPacketSize);
1925                     }
1926 #else
1927                     length = epState->stateUnion.stateBitField.maxPacketSize;
1928 #endif
1929 #endif
1930                 }
1931                 actualBuffer = destBuffer;
1932             }
1933         }
1934 #if (defined USB_DEVICE_IP3511_NEED_CHECK_BUFFER) && (USB_DEVICE_IP3511_NEED_CHECK_BUFFER)
1935         else /* cannot do memory copy */
1936         {
1937             /* not 64 bytes align || not in the dedicated ram || ( OUT && HS && not mutiple of 4 ) */
1938             if ((((uint32_t)actualBuffer & 0x0000003FU) != 0U) ||
1939                 (((uint32_t)actualBuffer & 0xFFC00000U) != (lpc3511IpState->registerBase->DATABUFSTART & 0xFFC00000U))
1940 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1941                 || ((0U != lpc3511IpState->controllerSpeed) && (0U == index) && ((length & 0x00000003u) != 0U)))
1942 #else
1943             )
1944 #endif
1945             {
1946                 return kStatus_USB_Error;
1947             }
1948         }
1949 #endif
1950     }
1951 
1952     /* Send/Receive data when the device is not resetting. */
1953     if (0U == lpc3511IpState->isResetting)
1954     {
1955         return USB_DeviceLpc3511IpEndpointPrime(lpc3511IpState, epState, endpointIndex, actualBuffer, length);
1956     }
1957     else
1958     {
1959         return kStatus_USB_Error;
1960     }
1961 }
USB_DeviceLpc3511IpTransaction(usb_device_lpc3511ip_state_struct_t * lpc3511IpState,usb_device_lpc3511ip_endpoint_state_struct_t * epState,uint8_t endpointIndex)1962 static usb_status_t USB_DeviceLpc3511IpTransaction(usb_device_lpc3511ip_state_struct_t *lpc3511IpState,
1963                                                    usb_device_lpc3511ip_endpoint_state_struct_t *epState,
1964                                                    uint8_t endpointIndex)
1965 {
1966     usb_status_t status = kStatus_USB_Error;
1967     OSA_SR_ALLOC();
1968 
1969 #if (defined(FSL_FEATURE_USBHSD_VERSION) && (FSL_FEATURE_USBHSD_VERSION >= 300U)) && \
1970     (!(defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) &&            \
1971        (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK)))
1972 #else
1973 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1974 #if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
1975      (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
1976     if (lpc3511IpState->hsInterruptIssue)
1977     {
1978 #endif
1979         /* high-speed */
1980         if ((0U != (epState->stateUnion.stateBitField.epControlDefault &
1981                     ((USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK) >> USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT))) &&
1982             (USB_ENDPOINT_INTERRUPT == epState->stateUnion.stateBitField.endpointType) &&
1983             (0U != lpc3511IpState->controllerSpeed))
1984         {
1985             /* users can use NVIC to disable/enable the USB interrupt to improve the system performance */
1986             OSA_ENTER_CRITICAL();
1987 
1988             lpc3511IpState->registerBase->DEVCMDSTAT =
1989                 (lpc3511IpState->registerBase->DEVCMDSTAT & ~USB_LPC3511IP_DEVCMDSTAT_W1C_MASK) |
1990                 (USB_LPC3511IP_DEVCMDSTAT_INTONNAK_AO_MASK | USB_LPC3511IP_DEVCMDSTAT_INTONNAK_AI_MASK);
1991 
1992 #if (defined USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE) && (USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE)
1993             USB_LPC3511IP_ENDPOINT_SET_ENDPOINT(
1994                 lpc3511IpState, endpointIndex, epState->stateUnion.stateBitField.producerOdd,
1995                 (epState->stateUnion.stateBitField.epControlDefault << USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT) |
1996                     (epState->stateUnion.stateBitField.stalled << USB_LPC3511IP_ENDPOINT_STALL_SHIFT),
1997                 0U, (uint32_t)lpc3511IpState->zeroTransactionData);
1998 #else
1999         USB_LPC3511IP_ENDPOINT_SET_ENDPOINT(
2000             lpc3511IpState, endpointIndex, 0,
2001             (epState->stateUnion.stateBitField.epControlDefault << USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT) |
2002                 (epState->stateUnion.stateBitField.stalled << USB_LPC3511IP_ENDPOINT_STALL_SHIFT),
2003             0U, (uint32_t)lpc3511IpState->zeroTransactionData);
2004 #endif
2005             /* users can use NVIC to disable/enable the USB interrupt to improve the system performance */
2006             OSA_EXIT_CRITICAL();
2007             return kStatus_USB_Success;
2008         }
2009 #if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
2010      (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
2011     }
2012 #endif
2013 #endif
2014 #endif
2015 
2016     /* Enter critical */
2017     OSA_ENTER_CRITICAL();
2018     if (0U != epState->stateUnion.stateBitField.stalled)
2019     {
2020         if ((endpointIndex >> 1U) != USB_ENDPOINT_CONTROL)
2021         {
2022             epState->stateUnion.stateBitField.stallPrimed = 1u;
2023             status                                        = kStatus_USB_Success;
2024         }
2025         status = kStatus_USB_Error;
2026         OSA_EXIT_CRITICAL();
2027         return status;
2028     }
2029     OSA_EXIT_CRITICAL();
2030 
2031     /* 1. transfer size is 0; 2. All are primed */
2032     if ((epState->transferLength <= epState->transferPrimedLength) && (epState->transferLength != 0U))
2033     {
2034         return kStatus_USB_Success;
2035     }
2036 #if (defined USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE) && (USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE)
2037     if ((endpointIndex >> 1U) != USB_CONTROL_ENDPOINT)
2038     {
2039         /* disable endpoint interrupts, users can use NVIC to disable/enable the USB interrupt to improve the system
2040          * performance */
2041         OSA_ENTER_CRITICAL();
2042         /* lpc3511IpState->registerBase->INTEN &= (uint32_t)(~(USB_LPC3511IP_MAX_PHY_ENDPOINT_MASK)); */
2043 #if ((defined(USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER)) && (USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER > 0U))
2044         /* for out endpoint,only use buffer toggle, disable prime double buffer at the same time*/
2045         /*host send data less than maxpacket size and in endpoint prime length more more than maxpacketsize, there will
2046          * be state mismtach*/
2047         if (0U == (endpointIndex & 0x1U))
2048         {
2049             status = USB_DeviceLpc3511IpGetActualBufferAndPrime(lpc3511IpState, epState, endpointIndex, 1U);
2050         }
2051         else
2052 #endif
2053         {
2054             do
2055             {
2056                 status = USB_DeviceLpc3511IpGetActualBufferAndPrime(lpc3511IpState, epState, endpointIndex, 1U);
2057                 if (status != kStatus_USB_Success)
2058                 {
2059                     break;
2060                 }
2061             } while ((epState->transferLength > epState->transferPrimedLength) &&
2062                      (epState->stateUnion.stateBitField.doubleBufferBusy < 2U));
2063         }
2064         /* enable endpoint interrupt again, users can use NVIC to disable/enable the USB interrupt to improve the system
2065          * performance */
2066         OSA_EXIT_CRITICAL();
2067     }
2068     else
2069 #endif
2070     {
2071         status = USB_DeviceLpc3511IpGetActualBufferAndPrime(lpc3511IpState, epState, endpointIndex, 0U);
2072     }
2073     return status;
2074 }
2075 
USB_DeviceLpc3511IpSend(usb_device_controller_handle controllerHandle,uint8_t endpointAddress,uint8_t * buffer,uint32_t length)2076 usb_status_t USB_DeviceLpc3511IpSend(usb_device_controller_handle controllerHandle,
2077                                      uint8_t endpointAddress,
2078                                      uint8_t *buffer,
2079                                      uint32_t length)
2080 {
2081     usb_device_lpc3511ip_state_struct_t *lpc3511IpState = (usb_device_lpc3511ip_state_struct_t *)controllerHandle;
2082     uint8_t endpointIndex                               = USB_LPC3511IP_ENDPOINT_DES_INDEX(endpointAddress);
2083     usb_device_lpc3511ip_endpoint_state_struct_t *epState =
2084         USB_DeviceLpc3511IpGetEndpointStateStruct(lpc3511IpState, endpointIndex);
2085 
2086     if (1U == epState->stateUnion.stateBitField.transferring)
2087     {
2088         return kStatus_USB_Error;
2089     }
2090 
2091     /* Save the transfer information */
2092     epState->transferDone         = 0U;
2093     epState->transferBuffer       = buffer;
2094     epState->transferLength       = length;
2095     epState->transferPrimedLength = 0U;
2096 
2097     return USB_DeviceLpc3511IpTransaction(lpc3511IpState, epState, endpointIndex);
2098 
2099 /* prime the control setup transfer if it is control in endpoint and data length is zero
2100  * For IP3511 there is no need to prime, the buffer is always in the command/status list
2101  */
2102 #if 0
2103     if ((0U == length) && (USB_CONTROL_ENDPOINT == (endpointAddress & USB_ENDPOINT_NUMBER_MASK)))
2104     {
2105         USB_DeviceLpc3511IpPrimeNextSetup(lpc3511IpState);
2106     }
2107 #endif
2108 }
2109 
USB_DeviceLpc3511IpRecv(usb_device_controller_handle controllerHandle,uint8_t endpointAddress,uint8_t * buffer,uint32_t length)2110 usb_status_t USB_DeviceLpc3511IpRecv(usb_device_controller_handle controllerHandle,
2111                                      uint8_t endpointAddress,
2112                                      uint8_t *buffer,
2113                                      uint32_t length)
2114 {
2115     return USB_DeviceLpc3511IpSend(controllerHandle, endpointAddress, buffer, length);
2116 }
2117 
USB_DeviceLpc3511IpCancel(usb_device_controller_handle controllerHandle,uint8_t ep)2118 usb_status_t USB_DeviceLpc3511IpCancel(usb_device_controller_handle controllerHandle, uint8_t ep)
2119 {
2120     /* users can use NVIC to disable/enable the USB interrupt to improve the system performance */
2121     OSA_SR_ALLOC();
2122 
2123     usb_device_lpc3511ip_state_struct_t *lpc3511IpState = (usb_device_lpc3511ip_state_struct_t *)controllerHandle;
2124     usb_device_callback_message_struct_t message;
2125     uint8_t endpointIndex = USB_LPC3511IP_ENDPOINT_DES_INDEX(ep);
2126     usb_device_lpc3511ip_endpoint_state_struct_t *epState =
2127         USB_DeviceLpc3511IpGetEndpointStateStruct(lpc3511IpState, endpointIndex);
2128 
2129     /* disable endpoint interrupts, users can use NVIC to disable/enable the USB interrupt to improve the system
2130      * performance */
2131     OSA_ENTER_CRITICAL();
2132     /* Cancel the transfer and notify the up layer when the endpoint is busy. */
2133     if ((0U != epState->stateUnion.stateBitField.transferring)
2134 #if (defined(FSL_FEATURE_USBHSD_VERSION) && (FSL_FEATURE_USBHSD_VERSION >= 300U)) && \
2135     (!(defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) &&            \
2136        (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK)))
2137 #else
2138 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
2139         || (
2140 #if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
2141      (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
2142                (0U != lpc3511IpState->hsInterruptIssue) &&
2143 #endif
2144                (0U != (epState->stateUnion.stateBitField.epControlDefault &
2145                        ((USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK) >> USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT))) &&
2146                (USB_ENDPOINT_INTERRUPT == epState->stateUnion.stateBitField.endpointType) &&
2147                (0U != lpc3511IpState->controllerSpeed) &&
2148                (0U != (lpc3511IpState->epCommandStatusList[epState->stateUnion.stateBitField.consumerOdd +
2149                                                            (((uint32_t)endpointIndex) * 2U)] &
2150                        USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK)) &&
2151                (0U == (lpc3511IpState->registerBase->EPTOGGLE & ((uint32_t)(0x01UL << endpointIndex)))))
2152 #endif
2153 #endif
2154     )
2155     {
2156 #if (defined(FSL_FEATURE_USBHSD_VERSION) && (FSL_FEATURE_USBHSD_VERSION >= 300U)) && \
2157     (!(defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) &&            \
2158        (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK)))
2159 #else
2160 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
2161 #if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
2162      (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
2163         if (0U != lpc3511IpState->hsInterruptIssue)
2164         {
2165 #endif
2166             epState->stateUnion.stateBitField.epControlDefault &=
2167                 (~((USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK) >> USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT));
2168 #if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
2169      (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
2170         }
2171 #endif
2172 #endif
2173 #endif
2174         if ((((lpc3511IpState->epCommandStatusList[(uint32_t)endpointIndex * 2U] &
2175                USB_LPC3511IP_ENDPOINT_ACTIVE_MASK) != 0U) &&
2176              ((lpc3511IpState->epCommandStatusList[(uint32_t)endpointIndex * 2U] &
2177                USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK) != 0U)) ||
2178             (((lpc3511IpState->epCommandStatusList[(uint32_t)endpointIndex * 2U + 1U] &
2179                USB_LPC3511IP_ENDPOINT_ACTIVE_MASK) != 0U) &&
2180              ((lpc3511IpState->epCommandStatusList[(uint32_t)endpointIndex * 2U + 1U] &
2181                USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK) != 0U)))
2182         {
2183             epState->stateUnion.stateBitField.epControlDefault |=
2184                 ((USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK) >> USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT);
2185         }
2186 
2187         if ((lpc3511IpState->registerBase->DEVCMDSTAT & USB_LPC3511IP_DEVCMDSTAT_DCON_MASK) != 0U)
2188         {
2189             while (1U == 1U)
2190             {
2191                 if ((lpc3511IpState->epCommandStatusList[(uint32_t)endpointIndex * 2U +
2192                                                          ((lpc3511IpState->registerBase->EPINUSE &
2193                                                            (((uint32_t)0x00000001U << endpointIndex))) >>
2194                                                           endpointIndex)] &
2195                      USB_LPC3511IP_ENDPOINT_ACTIVE_MASK) != 0U)
2196                 {
2197                     /* cancel the transfer in the endpoint command/status */
2198                     lpc3511IpState->registerBase->EPSKIP |= ((uint32_t)0x00000001U << endpointIndex);
2199                     while (((lpc3511IpState->registerBase->EPSKIP & ((uint32_t)0x00000001U << endpointIndex)) != 0U) &&
2200                            ((lpc3511IpState->epCommandStatusList[(uint32_t)endpointIndex * 2U +
2201                                                                  ((lpc3511IpState->registerBase->EPINUSE &
2202                                                                    (((uint32_t)0x00000001U << endpointIndex))) >>
2203                                                                   endpointIndex)] &
2204                              USB_LPC3511IP_ENDPOINT_ACTIVE_MASK) != 0U))
2205                     {
2206                     }
2207                     if ((lpc3511IpState->registerBase->EPSKIP & ((uint32_t)0x00000001U << endpointIndex)) != 0U)
2208                     {
2209                         lpc3511IpState->registerBase->EPSKIP &= (~((uint32_t)0x00000001U << endpointIndex));
2210                     }
2211                 }
2212 
2213                 if (((lpc3511IpState->epCommandStatusList[endpointIndex * 2U] & USB_LPC3511IP_ENDPOINT_ACTIVE_MASK) !=
2214                      0U) ||
2215                     ((lpc3511IpState->epCommandStatusList[endpointIndex * 2U + 1U] &
2216                       USB_LPC3511IP_ENDPOINT_ACTIVE_MASK) != 0U))
2217                 {
2218                     if ((lpc3511IpState->registerBase->EPINUSE & (((uint32_t)0x00000001U << endpointIndex))) != 0U)
2219                     {
2220                         lpc3511IpState->registerBase->EPINUSE &= ~((uint32_t)0x00000001U << endpointIndex);
2221                     }
2222                     else
2223                     {
2224                         lpc3511IpState->registerBase->EPINUSE |= ((uint32_t)0x00000001U << endpointIndex);
2225                     }
2226                 }
2227                 else
2228                 {
2229                     break;
2230                 }
2231             }
2232         }
2233         else
2234         {
2235             /* Make sure the device is detached from host, host will not send any transaction to device.
2236              * Then the endpoint status entry can be modified directly by software.
2237              */
2238             lpc3511IpState->epCommandStatusList[endpointIndex * 2U]      = USB_LPC3511IP_ENDPOINT_DISABLE_MASK;
2239             lpc3511IpState->epCommandStatusList[endpointIndex * 2U + 1U] = USB_LPC3511IP_ENDPOINT_DISABLE_MASK;
2240         }
2241 
2242         epState->stateUnion.stateBitField.transferring = 0U;
2243         epState->stateUnion.stateBitField.producerOdd =
2244             ((lpc3511IpState->registerBase->EPINUSE & ((uint32_t)((uint32_t)0x00000001U << endpointIndex))) >>
2245              endpointIndex);
2246         epState->stateUnion.stateBitField.consumerOdd =
2247             ((lpc3511IpState->registerBase->EPINUSE & ((uint32_t)((uint32_t)0x00000001U << endpointIndex))) >>
2248              endpointIndex);
2249         epState->stateUnion.stateBitField.doubleBufferBusy = 0U;
2250         /* clear interrupt status, enable endpoint interrupt again */
2251         lpc3511IpState->registerBase->INTSTAT = ((uint32_t)0x00000001U << endpointIndex);
2252 
2253         /* users can use NVIC to disable/enable the USB interrupt to improve the system performance */
2254         OSA_EXIT_CRITICAL();
2255 
2256         message.length  = USB_CANCELLED_TRANSFER_LENGTH;
2257         message.buffer  = epState->transferBuffer;
2258         message.code    = ep;
2259         message.isSetup = 0U;
2260 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
2261         if (kStatus_USB_Success != USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message))
2262         {
2263             return kStatus_USB_Error;
2264         }
2265 #else
2266         (void)USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message);
2267 #endif
2268     }
2269     else
2270     {
2271         /* users can use NVIC to disable/enable the USB interrupt to improve the system performance */
2272         OSA_EXIT_CRITICAL();
2273     }
2274     return kStatus_USB_Success;
2275 }
2276 
2277 /*seperate this function from USB_DeviceLpc3511IpControl for misra17.2 recursive */
USB_DeviceLpc3511IpControlPreSetDeviceAddress(usb_device_controller_handle controllerHandle,void * param)2278 static usb_status_t USB_DeviceLpc3511IpControlPreSetDeviceAddress(usb_device_controller_handle controllerHandle,
2279                                                                   void *param)
2280 {
2281     usb_device_lpc3511ip_state_struct_t *lpc3511IpState = (usb_device_lpc3511ip_state_struct_t *)controllerHandle;
2282     usb_status_t error                                  = kStatus_USB_Error;
2283     uint32_t tmp32Value;
2284     uint8_t tmp8Value;
2285     if (NULL != param)
2286     {
2287         tmp8Value  = *((uint8_t *)param);
2288         tmp32Value = lpc3511IpState->registerBase->DEVCMDSTAT;
2289         tmp32Value &= ~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_DEV_ADDR_MASK);
2290         tmp32Value |= ((uint32_t)tmp8Value & USB_LPC3511IP_DEVCMDSTAT_DEV_ADDR_MASK);
2291         lpc3511IpState->registerBase->DEVCMDSTAT = tmp32Value;
2292         error                                    = kStatus_USB_Success;
2293     }
2294     return error;
2295 }
2296 
USB_DeviceLpc3511IpControl(usb_device_controller_handle controllerHandle,usb_device_control_type_t type,void * param)2297 usb_status_t USB_DeviceLpc3511IpControl(usb_device_controller_handle controllerHandle,
2298                                         usb_device_control_type_t type,
2299                                         void *param)
2300 {
2301     usb_device_lpc3511ip_state_struct_t *lpc3511IpState = (usb_device_lpc3511ip_state_struct_t *)controllerHandle;
2302     usb_status_t error                                  = kStatus_USB_Error;
2303     uint32_t tmp32Value;
2304     uint8_t tmp8Value;
2305 #if defined(USB_DEVICE_CONFIG_GET_SOF_COUNT) && (USB_DEVICE_CONFIG_GET_SOF_COUNT > 0U)
2306     uint32_t *tmp32Pointer;
2307 #endif
2308 
2309 #if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U))
2310     usb_device_struct_t *deviceHandle;
2311 #endif
2312     usb_device_lpc3511ip_endpoint_state_struct_t *epState;
2313 
2314     if (controllerHandle == NULL)
2315     {
2316         return kStatus_USB_InvalidHandle;
2317     }
2318 
2319 #if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U))
2320     deviceHandle = (usb_device_struct_t *)lpc3511IpState->deviceHandle;
2321 #endif
2322 
2323     switch (type)
2324     {
2325         case kUSB_DeviceControlRun:
2326             lpc3511IpState->registerBase->DEVCMDSTAT = (lpc3511IpState->registerBase->DEVCMDSTAT & ~USB_LPC3511IP_DEVCMDSTAT_W1C_MASK) | (USB_LPC3511IP_DEVCMDSTAT_DCON_MASK);
2327             lpc3511IpState->registerBase->DEVCMDSTAT &= ~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_FORCE_NEEDCLK_MASK);
2328             error = kStatus_USB_Success;
2329             break;
2330 
2331         case kUSB_DeviceControlStop:
2332             lpc3511IpState->registerBase->DEVCMDSTAT = (lpc3511IpState->registerBase->DEVCMDSTAT & ~USB_LPC3511IP_DEVCMDSTAT_W1C_MASK) | USB_LPC3511IP_DEVCMDSTAT_FORCE_NEEDCLK_MASK;
2333             lpc3511IpState->registerBase->DEVCMDSTAT &= ~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_DCON_MASK);
2334             error = kStatus_USB_Success;
2335             break;
2336 
2337         case kUSB_DeviceControlEndpointInit:
2338             if (NULL != param)
2339             {
2340                 error = USB_DeviceLpc3511IpEndpointInit(lpc3511IpState, (usb_device_endpoint_init_struct_t *)param);
2341             }
2342             break;
2343 
2344         case kUSB_DeviceControlEndpointDeinit:
2345             if (NULL != param)
2346             {
2347                 tmp8Value = *((uint8_t *)param);
2348                 error     = USB_DeviceLpc3511IpEndpointDeinit(lpc3511IpState, tmp8Value);
2349             }
2350             break;
2351 
2352         case kUSB_DeviceControlEndpointStall:
2353             if (NULL != param)
2354             {
2355                 tmp8Value = *((uint8_t *)param);
2356                 error     = USB_DeviceLpc3511IpEndpointStall(lpc3511IpState, tmp8Value);
2357             }
2358             break;
2359 
2360         case kUSB_DeviceControlEndpointUnstall:
2361             if (NULL != param)
2362             {
2363                 tmp8Value = *((uint8_t *)param);
2364                 error     = USB_DeviceLpc3511IpEndpointUnstall(lpc3511IpState, tmp8Value);
2365             }
2366             break;
2367 
2368         case kUSB_DeviceControlGetDeviceStatus:
2369             if (NULL != param)
2370             {
2371                 *((uint16_t *)param) =
2372                     (USB_DEVICE_CONFIG_SELF_POWER << (USB_REQUEST_STANDARD_GET_STATUS_DEVICE_SELF_POWERED_SHIFT))
2373 #if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U))
2374                     | ((uint16_t)(((uint32_t)deviceHandle->remotewakeup)
2375                                   << (USB_REQUEST_STANDARD_GET_STATUS_DEVICE_REMOTE_WARKUP_SHIFT)))
2376 #endif
2377                     ;
2378                 error = kStatus_USB_Success;
2379             }
2380             break;
2381 
2382         case kUSB_DeviceControlGetEndpointStatus:
2383             if (NULL != param)
2384             {
2385                 usb_device_endpoint_status_struct_t *endpointStatus = (usb_device_endpoint_status_struct_t *)param;
2386 
2387                 if ((((endpointStatus->endpointAddress) & USB_ENDPOINT_NUMBER_MASK)) <
2388                     (uint8_t)USB_DEVICE_IP3511_ENDPOINTS_NUM)
2389                 {
2390                     epState = USB_DeviceLpc3511IpGetEndpointStateStruct(
2391                         lpc3511IpState, USB_LPC3511IP_ENDPOINT_DES_INDEX(endpointStatus->endpointAddress));
2392 #if (defined(USB_DEVICE_CONFIG_ROOT2_TEST) && (USB_DEVICE_CONFIG_ROOT2_TEST > 0U))
2393                     if (0U == epState->stateUnion.stateBitField.isOpened)
2394                     {
2395                         error = kStatus_USB_InvalidRequest;
2396                     }
2397                     else
2398                     {
2399                         endpointStatus->endpointStatus = (uint16_t)((epState->stateUnion.stateBitField.stalled == 1U) ?
2400                                                                         kUSB_DeviceEndpointStateStalled :
2401                                                                         kUSB_DeviceEndpointStateIdle);
2402                         error                          = kStatus_USB_Success;
2403                     }
2404 #else
2405                     endpointStatus->endpointStatus =
2406                         (uint16_t)((epState->stateUnion.stateBitField.stalled == 1U) ? kUSB_DeviceEndpointStateStalled :
2407                                                                                        kUSB_DeviceEndpointStateIdle);
2408                     error = kStatus_USB_Success;
2409 #endif
2410                 }
2411 #if (defined(USB_DEVICE_CONFIG_ROOT2_TEST) && (USB_DEVICE_CONFIG_ROOT2_TEST > 0U))
2412                 else
2413                 {
2414                     error = kStatus_USB_InvalidRequest;
2415                 }
2416 #endif
2417             }
2418             break;
2419 
2420         case kUSB_DeviceControlPreSetDeviceAddress:
2421             error = USB_DeviceLpc3511IpControlPreSetDeviceAddress(controllerHandle, param);
2422             if (kStatus_USB_Success == error)
2423             {
2424                 /*no action, just for misra4.7*/
2425             }
2426             break;
2427 
2428         case kUSB_DeviceControlSetDeviceAddress:
2429             error = kStatus_USB_Success;
2430             break;
2431 
2432         case kUSB_DeviceControlGetSynchFrame:
2433 #if (defined(USB_DEVICE_CONFIG_ROOT2_TEST) && (USB_DEVICE_CONFIG_ROOT2_TEST > 0U))
2434             if (NULL != param)
2435             {
2436                 usb_device_endpoint_status_struct_t *endpointStatus = (usb_device_endpoint_status_struct_t *)param;
2437                 if ((((endpointStatus->endpointAddress) & USB_ENDPOINT_NUMBER_MASK)) <
2438                     (uint8_t)USB_DEVICE_IP3511_ENDPOINTS_NUM)
2439                 {
2440                     epState = USB_DeviceLpc3511IpGetEndpointStateStruct(
2441                         lpc3511IpState, USB_LPC3511IP_ENDPOINT_DES_INDEX(endpointStatus->endpointAddress));
2442                     /* In configured state (should stall in other states), device must STALL status stage of Synch Frame
2443                      * Request with Invalid wIndex */
2444                     if (0U == epState->stateUnion.stateBitField.isOpened)
2445                     {
2446                         error = kStatus_USB_InvalidRequest;
2447                     }
2448                     else
2449                     {
2450                         error = kStatus_USB_Success;
2451                     }
2452                 }
2453                 else
2454                 {
2455                     error = kStatus_USB_InvalidRequest;
2456                 }
2457             }
2458 #endif
2459             break;
2460 
2461 #if defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U)
2462         case kUSB_DeviceControlResume:
2463             /* todo: turn on USB clock and enable the USB clock source */
2464             lpc3511IpState->registerBase->DEVCMDSTAT = (lpc3511IpState->registerBase->DEVCMDSTAT & ~USB_LPC3511IP_DEVCMDSTAT_W1C_MASK) | USB_LPC3511IP_DEVCMDSTAT_FORCE_NEEDCLK_MASK;
2465             lpc3511IpState->registerBase->DEVCMDSTAT &= ~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_DSUS_MASK);
2466             while (0U != (lpc3511IpState->registerBase->DEVCMDSTAT & USB_LPC3511IP_DEVCMDSTAT_DSUS_MASK))
2467             {
2468             }
2469             /* the W1C bits */
2470             lpc3511IpState->registerBase->DEVCMDSTAT &=
2471                 ~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_FORCE_NEEDCLK_MASK | USB_LPC3511IP_DEVCMDSTAT_INTERRUPT_WC_MASK);
2472             error = kStatus_USB_Success;
2473             break;
2474 #if (defined(USB_DEVICE_CONFIG_LPM_L1) && (USB_DEVICE_CONFIG_LPM_L1 > 0U))
2475         case kUSB_DeviceControlSleepResume:
2476             /* todo: turn on USB clock and enable the USB clock source */
2477             lpc3511IpState->registerBase->DEVCMDSTAT = (lpc3511IpState->registerBase->DEVCMDSTAT & ~USB_LPC3511IP_DEVCMDSTAT_W1C_MASK) | USB_LPC3511IP_DEVCMDSTAT_FORCE_NEEDCLK_MASK;
2478             lpc3511IpState->registerBase->DEVCMDSTAT &= ~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_LPM_SUS_MASK);
2479             while (0U != (lpc3511IpState->registerBase->DEVCMDSTAT & USB_LPC3511IP_DEVCMDSTAT_LPM_SUS_MASK))
2480             {
2481                 __NOP();
2482             }
2483             /* the W1C bits */
2484             lpc3511IpState->registerBase->DEVCMDSTAT &=
2485                 ~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_FORCE_NEEDCLK_MASK | USB_LPC3511IP_DEVCMDSTAT_INTERRUPT_WC_MASK);
2486             error = kStatus_USB_Success;
2487             break;
2488 #endif
2489         case kUSB_DeviceControlGetRemoteWakeUp:
2490             *((uint8_t *)param) =
2491                 (uint8_t)((lpc3511IpState->registerBase->DEVCMDSTAT & USB_LPC3511IP_DEVCMDSTAT_LPM_REWP_MASK) >>
2492                           USB_LPC3511IP_DEVCMDSTAT_LPM_REWP_SHIFT);
2493             break;
2494 #endif /* USB_DEVICE_CONFIG_REMOTE_WAKEUP */
2495 
2496         case kUSB_DeviceControlSetDefaultStatus:
2497             for (tmp32Value = 0U; tmp32Value < (uint32_t)USB_DEVICE_IP3511_ENDPOINTS_NUM; tmp32Value++)
2498             {
2499 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
2500                 if ((kStatus_USB_Success !=
2501                      USB_DeviceLpc3511IpEndpointDeinit(lpc3511IpState, (uint8_t)(tmp32Value | (USB_IN << 0x07U)))) ||
2502                     (USB_DeviceLpc3511IpEndpointDeinit(lpc3511IpState, (uint8_t)(tmp32Value | (USB_OUT << 0x07U)))))
2503                 {
2504                     return kStatus_USB_Error;
2505                 }
2506 #else
2507                 (void)USB_DeviceLpc3511IpEndpointDeinit(lpc3511IpState, (uint8_t)(tmp32Value | (USB_IN << 0x07U)));
2508                 (void)USB_DeviceLpc3511IpEndpointDeinit(lpc3511IpState, (uint8_t)(tmp32Value | (USB_OUT << 0x07U)));
2509 #endif
2510             }
2511             USB_DeviceLpc3511IpSetDefaultState(lpc3511IpState);
2512             error = kStatus_USB_Success;
2513             break;
2514 
2515         case kUSB_DeviceControlGetSpeed:
2516             if (NULL != param)
2517             {
2518                 *((uint8_t *)param) = lpc3511IpState->deviceSpeed;
2519                 error               = kStatus_USB_Success;
2520             }
2521             break;
2522         case kUSB_DeviceControlGetOtgStatus:
2523             break;
2524         case kUSB_DeviceControlSetOtgStatus:
2525             break;
2526 #if (defined(USB_DEVICE_CONFIG_USB20_TEST_MODE) && (USB_DEVICE_CONFIG_USB20_TEST_MODE > 0U))
2527         case kUSB_DeviceControlSetTestMode:
2528             if (NULL != param)
2529             {
2530                 tmp8Value = *((uint8_t *)param);
2531 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
2532                 lpc3511IpState->registerBase->DEVCMDSTAT =
2533                     (lpc3511IpState->registerBase->DEVCMDSTAT & ~USB_LPC3511IP_DEVCMDSTAT_W1C_MASK) |
2534                     ((uint32_t)(tmp8Value) << USBHSD_DEVCMDSTAT_PHY_TEST_MODE_SHIFT);
2535 #endif
2536                 error = kStatus_USB_Success;
2537             }
2538             break;
2539 #endif
2540 #if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U)) && \
2541     (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
2542         case kUSB_DeviceControlDcdEnable:
2543             if (kStatus_hsdcd_Success == USB_HSDCD_Control(lpc3511IpState->dcdHandle, kUSB_DeviceHSDcdEnable, NULL))
2544             {
2545                 error = kStatus_USB_Success;
2546             }
2547             break;
2548         case kUSB_DeviceControlDcdDisable:
2549             if (kStatus_hsdcd_Success == USB_HSDCD_Control(lpc3511IpState->dcdHandle, kUSB_DeviceHSDcdDisable, NULL))
2550             {
2551                 error = kStatus_USB_Success;
2552             }
2553             break;
2554         case kUSB_DeviceControlUpdateHwTick:
2555             /*udpate 1ms time tick*/
2556             error = kStatus_USB_Success;
2557             break;
2558 
2559 #endif
2560 #if defined(USB_DEVICE_CONFIG_GET_SOF_COUNT) && (USB_DEVICE_CONFIG_GET_SOF_COUNT > 0U)
2561         case kUSB_DeviceControlGetCurrentFrameCount:
2562             if (NULL != param)
2563             {
2564                 tmp32Pointer = (uint32_t *)param;
2565                 if (USB_SPEED_HIGH == lpc3511IpState->deviceSpeed) /* if high speed, change to use microframe count */
2566                 {
2567                     *tmp32Pointer =
2568                         ((uint32_t)(((lpc3511IpState->registerBase->INFO >> USB_LPC3511IP_INFO_FRAME_NR_SHIFT) &
2569                                      USB_LPC3511IP_INFO_FRAME_NR_MASK) &
2570                                     (USB_DEVICE_MAX_FRAME_COUNT))) *
2571                         8U;
2572                 }
2573                 else
2574                 {
2575                     *tmp32Pointer =
2576                         ((uint32_t)(((lpc3511IpState->registerBase->INFO >> USB_LPC3511IP_INFO_FRAME_NR_SHIFT) &
2577                                      USB_LPC3511IP_INFO_FRAME_NR_MASK) &
2578                                     (USB_DEVICE_MAX_FRAME_COUNT)));
2579                 }
2580                 error = kStatus_USB_Success;
2581             }
2582             break;
2583 #endif
2584         default:
2585             /*no action*/
2586             break;
2587     }
2588 
2589     return error;
2590 }
2591 
2592 #if (defined(USB_DEVICE_CONFIG_SOF_NOTIFICATIONS) && (USB_DEVICE_CONFIG_SOF_NOTIFICATIONS > 0U))
2593 /*!
2594  * @brief Handle Start of Frame (SOF) Interrupt.
2595  *
2596  * The function is used to notify the upper layer that an SOF has occurred.
2597  *
2598  * @param lpc3511IpState      Pointer of the controller state structure.
2599  *
2600  */
USB_DeviceLpc3511IpInterruptSOF(usb_device_lpc3511ip_state_struct_t * lpc3511IpState)2601 static void USB_DeviceLpc3511IpInterruptSOF(usb_device_lpc3511ip_state_struct_t *lpc3511IpState)
2602 {
2603     usb_device_callback_message_struct_t message;
2604 
2605     message.buffer  = (uint8_t *)NULL;
2606     message.code    = (uint8_t)kUSB_DeviceNotifySOF;
2607     message.length  = 0U;
2608     message.isSetup = 0U;
2609 
2610     /* Notify upper layer */
2611 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
2612     if (kStatus_USB_Success != USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message))
2613     {
2614 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
2615         usb_echo("notification error\n");
2616 #endif
2617     }
2618 #else
2619     (void)USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message);
2620 #endif
2621 }
2622 #endif /* USB_DEVICE_CONFIG_SOF_NOTIFICATIONS */
2623 
USB_DeviceLpcIp3511IsrFunction(void * deviceHandle)2624 void USB_DeviceLpcIp3511IsrFunction(void *deviceHandle)
2625 {
2626     usb_device_struct_t *handle = (usb_device_struct_t *)deviceHandle;
2627     usb_device_lpc3511ip_state_struct_t *lpc3511IpState;
2628     uint32_t interruptStatus;
2629     uint32_t usbErrorCode;
2630     uint32_t devState;
2631 
2632     if (NULL == deviceHandle)
2633     {
2634         return;
2635     }
2636 
2637     lpc3511IpState = (usb_device_lpc3511ip_state_struct_t *)(handle->controllerHandle);
2638     /* get and clear interrupt status */
2639     interruptStatus                       = lpc3511IpState->registerBase->INTSTAT;
2640     lpc3511IpState->registerBase->INTSTAT = interruptStatus;
2641     interruptStatus &= lpc3511IpState->registerBase->INTEN;
2642 
2643     usbErrorCode = (lpc3511IpState->registerBase->INFO & USB_LPC3511IP_INFO_ERR_CODE_MASK);
2644 
2645     /* device status change interrupt */
2646     if (0U != (interruptStatus & USB_LPC3511IP_INTSTAT_DEV_INT_MASK))
2647     {
2648         /* get and clear device state change status */
2649         devState = lpc3511IpState->registerBase->DEVCMDSTAT;
2650         devState &= ~(USB_LPC3511IP_DEVCMDSTAT_SETUP_MASK);
2651         lpc3511IpState->registerBase->DEVCMDSTAT = (devState | USB_LPC3511IP_DEVCMDSTAT_INTERRUPT_WC_MASK);
2652 
2653         /* For HS: there is interrupt with DEV_INT=1, OTG_C=1 and ADPPROBE=1 when vbus rising.
2654          * For FS: there is no interrupt when vbus rising. The only way is: check the VBUS_DEBOUNCED in the DRES_C
2655          * interrupt with DCON set.
2656          */
2657 #if (defined(USB_DEVICE_CONFIG_DETACH_ENABLE) && (USB_DEVICE_CONFIG_DETACH_ENABLE))
2658         if ((0U == lpc3511IpState->deviceState) &&
2659             ((0U != (devState & USB_LPC3511IP_DEVCMDSTAT_VBUS_DEBOUNCED_MASK))
2660 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
2661              || ((0U != (lpc3511IpState->registerBase->LPM & USB_LPC3511IP_USB_LPM_ADPPROBE_MASK)) &&
2662                  (1U == lpc3511IpState->controllerSpeed))
2663 #endif
2664                  ))
2665         {
2666 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
2667 #if ((defined(USB_DEVICE_IP3511HS_LPM_ADPPROBE_ATTACH_DEBOUNCE_COUNT)) && \
2668      (USB_DEVICE_IP3511HS_LPM_ADPPROBE_ATTACH_DEBOUNCE_COUNT > 0U))
2669             /* add one little debounce for HS's attach detection because ADPPROBE is undebounced value */
2670             uint32_t debounceCount = USB_DEVICE_IP3511HS_LPM_ADPPROBE_ATTACH_DEBOUNCE_COUNT;
2671             if (1U == lpc3511IpState->controllerSpeed)
2672             {
2673                 while ((0U != debounceCount) && (0U == (devState & USB_LPC3511IP_DEVCMDSTAT_VBUS_DEBOUNCED_MASK)))
2674                 {
2675                     if (0U == (lpc3511IpState->registerBase->LPM & USB_LPC3511IP_USB_LPM_ADPPROBE_MASK))
2676                     {
2677                         break;
2678                     }
2679                     debounceCount--;
2680                 }
2681             }
2682 
2683             if ((debounceCount == 0U) || (0U != (devState & USB_LPC3511IP_DEVCMDSTAT_VBUS_DEBOUNCED_MASK)))
2684 #endif
2685 #endif
2686             {
2687                 lpc3511IpState->deviceState = 1U;
2688                 USB_DeviceLpc3511IpInterruptAttach(lpc3511IpState);
2689 #if (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U)) && \
2690     (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U))
2691 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
2692                 if (kStatus_USB_Success != USB_HSDCD_Control(lpc3511IpState->dcdHandle, kUSB_DeviceHSDcdRun, NULL))
2693                 {
2694 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
2695                     usb_echo("notification error\n");
2696 #endif
2697                 }
2698 #else
2699                 (void)USB_HSDCD_Control(lpc3511IpState->dcdHandle, kUSB_DeviceHSDcdRun, NULL);
2700 #endif
2701 #endif
2702             }
2703         }
2704         /* For HS: there is interrupt with DEV_INT=1, OTG_C=1 and ADPPROBE=0 when vbus falling.
2705          * For HS and FS: there is interrupt when vbus falling if DCON is set.
2706          */
2707         else if ((1U == lpc3511IpState->deviceState) &&
2708                  (((0U != (lpc3511IpState->registerBase->DEVCMDSTAT & USB_LPC3511IP_DEVCMDSTAT_DCON_MASK)) &&
2709                    (0U == (devState & USB_LPC3511IP_DEVCMDSTAT_VBUS_DEBOUNCED_MASK)))
2710 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
2711                   || ((0U == (lpc3511IpState->registerBase->DEVCMDSTAT & USB_LPC3511IP_DEVCMDSTAT_DCON_MASK)) &&
2712                       (0U == (lpc3511IpState->registerBase->LPM & USB_LPC3511IP_USB_LPM_ADPPROBE_MASK)) &&
2713                       (1U == lpc3511IpState->controllerSpeed))
2714 #endif
2715                       ))
2716         {
2717 #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
2718 #if ((defined FSL_FEATURE_USBHSD_HAS_EXIT_HS_ISSUE) && (FSL_FEATURE_USBHSD_HAS_EXIT_HS_ISSUE > 0U))
2719 #if ((defined USB_DEVICE_IP3511HS_FORCE_EXIT_HS_MODE_ENABLE) && (USB_DEVICE_IP3511HS_FORCE_EXIT_HS_MODE_ENABLE > 0U))
2720             uint32_t delay = 100000U;
2721 #endif
2722 #endif
2723 #endif
2724             lpc3511IpState->deviceState = 0U;
2725 #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
2726 #if ((defined FSL_FEATURE_USBHSD_HAS_EXIT_HS_ISSUE) && (FSL_FEATURE_USBHSD_HAS_EXIT_HS_ISSUE > 0U))
2727 #if ((defined USB_DEVICE_IP3511HS_FORCE_EXIT_HS_MODE_ENABLE) && (USB_DEVICE_IP3511HS_FORCE_EXIT_HS_MODE_ENABLE > 0U))
2728             /* wait at least 125us to let the host to detect the detach */
2729             USB_PhyDeviceForceEnterFSMode(lpc3511IpState->controllerId, 1);
2730             while (delay--)
2731             {
2732                 __NOP();
2733             }
2734             USB_PhyDeviceForceEnterFSMode(lpc3511IpState->controllerId, 0);
2735 #endif
2736 #endif
2737 #endif
2738             USB_DeviceLpc3511IpInterruptDetach(lpc3511IpState);
2739         }
2740         else
2741         {
2742             /*no action*/
2743         }
2744 #endif
2745 
2746         /* reset change */
2747         if (0U != (devState & USB_LPC3511IP_DEVCMDSTAT_DRES_C_MASK))
2748         {
2749             USB_DeviceLpc3511IpInterruptReset(lpc3511IpState);
2750         }
2751 
2752 /* Suspend/Resume */
2753 #if ((defined(USB_DEVICE_CONFIG_LOW_POWER_MODE)) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
2754         if (0U != (devState & USB_LPC3511IP_DEVCMDSTAT_DSUS_C_MASK))
2755         {
2756             if (0U != (lpc3511IpState->registerBase->DEVCMDSTAT & USB_LPC3511IP_DEVCMDSTAT_DSUS_MASK))
2757             {
2758 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
2759                 if (kStatus_USB_Success != USB_DeviceLpc3511IpInterruptSuspend(lpc3511IpState))
2760                 {
2761 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
2762                     usb_echo("notification error\n");
2763 #endif
2764                 }
2765 #else
2766                 (void)USB_DeviceLpc3511IpInterruptSuspend(lpc3511IpState);
2767 #endif
2768             }
2769 #if (defined(USB_DEVICE_CONFIG_LPM_L1) && (USB_DEVICE_CONFIG_LPM_L1 > 0U))
2770             else if (0U != (lpc3511IpState->registerBase->DEVCMDSTAT & USB_LPC3511IP_DEVCMDSTAT_LPM_SUS_MASK))
2771             {
2772 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
2773                 if (kStatus_USB_Success != (void)USB_DeviceLpc3511IpInterruptLPMSleep(lpc3511IpState))
2774                 {
2775 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
2776                     usb_echo("notification error\n");
2777 #endif
2778                 }
2779 #else
2780                 (void)USB_DeviceLpc3511IpInterruptLPMSleep(lpc3511IpState);
2781 #endif
2782             }
2783 #endif
2784             else
2785             {
2786 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
2787                 if (kStatus_USB_Success != USB_DeviceLpc3511IpInterruptResume(lpc3511IpState))
2788                 {
2789 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
2790                     usb_echo("notification error\n");
2791 #endif
2792                 }
2793 #else
2794                 (void)USB_DeviceLpc3511IpInterruptResume(lpc3511IpState);
2795 #endif
2796             }
2797         }
2798 
2799 #endif
2800 
2801 #if 0U /* some soc don't support this bit, need check according to the feature macro */
2802         /* OTG Status change */
2803         if (lpc3511IpState->registerBase->DEVCMDSTAT & USB_LPC3511IP_DEVCMDSTAT_OTG_C_MASK)
2804         {
2805         }
2806 #endif
2807     }
2808 
2809     /* endpoint transfers interrupt */
2810     if (0U != (interruptStatus & USB_LPC3511IP_MAX_PHY_ENDPOINT_MASK))
2811     {
2812         devState = 0;                        /* devState means index here */
2813         if (0U != (interruptStatus & 0x01U)) /* control OUT */
2814         {
2815             if (0U != (lpc3511IpState->registerBase->DEVCMDSTAT & USB_LPC3511IP_DEVCMDSTAT_SETUP_MASK))
2816             {
2817                 devState = 2U;
2818                 if ((lpc3511IpState->endpointState[0].stateUnion.stateBitField.stalled == 1U) ||
2819                     (lpc3511IpState->endpointState[1].stateUnion.stateBitField.stalled == 1U))
2820                 {
2821                     USB_LPC3511IP_ENDPOINT_SET_ENDPOINT_AND(
2822                         lpc3511IpState, 0, 0,
2823                         (~(USB_LPC3511IP_ENDPOINT_STALL_MASK | USB_LPC3511IP_ENDPOINT_ACTIVE_MASK)));
2824                     USB_LPC3511IP_ENDPOINT_SET_ENDPOINT_AND(
2825                         lpc3511IpState, 1, 0,
2826                         (~(USB_LPC3511IP_ENDPOINT_STALL_MASK | USB_LPC3511IP_ENDPOINT_ACTIVE_MASK)));
2827                     lpc3511IpState->endpointState[0].stateUnion.stateBitField.stalled = 0U;
2828                     lpc3511IpState->endpointState[1].stateUnion.stateBitField.stalled = 0U;
2829                 }
2830 
2831                 /* todo: setup token interrupt */
2832                 USB_DeviceLpc3511IpInterruptToken(lpc3511IpState, 0U, 1, usbErrorCode);
2833             }
2834         }
2835 
2836         for (; devState < ((uint32_t)USB_DEVICE_IP3511_ENDPOINTS_NUM * 2U); ++devState)
2837         {
2838             /* check the endpoint interrupt */
2839             if (0U != (interruptStatus & (0x01UL << devState)))
2840             {
2841                 USB_DeviceLpc3511IpInterruptToken(lpc3511IpState, (uint8_t)devState, 0U, usbErrorCode);
2842             }
2843         }
2844     }
2845 
2846 #if (defined(USB_DEVICE_CONFIG_SOF_NOTIFICATIONS) && (USB_DEVICE_CONFIG_SOF_NOTIFICATIONS > 0U))
2847     if (interruptStatus & USB_LPC3511IP_INTSTAT_FRAME_INT_MASK)
2848     {
2849         USB_DeviceLpc3511IpInterruptSOF(lpc3511IpState);
2850     }
2851 #endif
2852 }
2853 
2854 #endif
2855