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 /* CONFIG_UDC_DRIVER is for Zephyr, it will not be defined in NXP MCUXpresso SDK */
1124 #if !((defined CONFIG_UDC_DRIVER) && (CONFIG_UDC_DRIVER))
1125     usb_setup_struct_t *setupPacket;
1126     void *temp;
1127 #endif
1128     usb_device_lpc3511ip_endpoint_state_struct_t *epState =
1129         USB_DeviceLpc3511IpGetEndpointStateStruct(lpc3511IpState, endpointIndex);
1130 #if (defined USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE) && (USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE)
1131     uint32_t len = 0;
1132 #endif
1133 
1134 #if (defined(FSL_FEATURE_USBHSD_VERSION) && (FSL_FEATURE_USBHSD_VERSION >= 300U)) && \
1135     (!(defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) &&            \
1136        (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK)))
1137 #else
1138 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1139 #if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
1140      (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
1141     if (0U != lpc3511IpState->hsInterruptIssue)
1142     {
1143 #endif
1144         if ((0U != (epState->stateUnion.stateBitField.epControlDefault &
1145                     ((USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK) >> USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT))) &&
1146             (USB_ENDPOINT_INTERRUPT == epState->stateUnion.stateBitField.endpointType) &&
1147             (0U != lpc3511IpState->controllerSpeed) &&
1148             (0U != (lpc3511IpState->epCommandStatusList[epState->stateUnion.stateBitField.consumerOdd +
1149                                                         (((uint32_t)endpointIndex) * 2U)] &
1150                     USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK)))
1151         {
1152             if (0U == (lpc3511IpState->registerBase->EPTOGGLE & ((uint32_t)(0x01UL << endpointIndex))))
1153             {
1154                 uint32_t index;
1155                 length = 0U;
1156                 for (index = 0U; index < ((uint32_t)USB_DEVICE_IP3511_ENDPOINTS_NUM) * 4U; ++index)
1157                 {
1158                     if ((0U !=
1159                          (lpc3511IpState->epCommandStatusList[index] & USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK)) &&
1160                         (USB_ENDPOINT_INTERRUPT ==
1161                          lpc3511IpState->endpointState[index / 2U].stateUnion.stateBitField.endpointType))
1162                     {
1163                         length++;
1164                     }
1165                 }
1166 
1167                 if (length <= 1U)
1168                 {
1169                     lpc3511IpState->registerBase->DEVCMDSTAT &=
1170                         ~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_INTONNAK_AO_MASK | USB_LPC3511IP_DEVCMDSTAT_INTONNAK_AI_MASK);
1171                 }
1172                 epState->stateUnion.stateBitField.epControlDefault &=
1173                     (~((USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK) >> USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT));
1174 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1175                 /* high-speed */
1176                 epState->stateUnion.stateBitField.epControlDefault |=
1177                     ((USB_LPC3511IP_ENDPOINT_RFTV_MASK | USB_LPC3511IP_ENDPOINT_ENDPOINT_TYPE_MASK) >>
1178                      USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT);
1179 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1180                 if (kStatus_USB_Success != USB_DeviceLpc3511IpTransaction(lpc3511IpState, epState, endpointIndex))
1181                 {
1182 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
1183                     usb_echo("prime transaction error\n");
1184 #endif
1185                 }
1186 #else
1187                 (void)USB_DeviceLpc3511IpTransaction(lpc3511IpState, epState, endpointIndex);
1188 #endif
1189 #endif
1190             }
1191             return;
1192         }
1193 #if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
1194      (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
1195     }
1196 #endif
1197 #endif
1198 #endif
1199 
1200     if ((0U == isSetup) && (0U == epState->stateUnion.stateBitField.transferring))
1201     {
1202         return;
1203     }
1204     if (0U != isSetup)
1205     {
1206         message.length = 8U;
1207         message.buffer = (lpc3511IpState->setupData);
1208         /* clear the primed control transactions */
1209         if (0U != (epState->stateUnion.stateBitField.transferring))
1210         {
1211             epState->stateUnion.stateBitField.transferring = 0U;
1212             if (0U != (lpc3511IpState->epCommandStatusList[0] & USB_LPC3511IP_ENDPOINT_ACTIVE_MASK))
1213             {
1214 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1215                 if (kStatus_USB_Success != USB_DeviceLpc3511IpCancel(lpc3511IpState, USB_CONTROL_ENDPOINT))
1216                 {
1217                     return;
1218                 }
1219 #else
1220                 (void)USB_DeviceLpc3511IpCancel(lpc3511IpState, USB_CONTROL_ENDPOINT);
1221 #endif
1222             }
1223         }
1224         if (0U != (lpc3511IpState->endpointState[1].stateUnion.stateBitField.transferring))
1225         {
1226             lpc3511IpState->endpointState[1].stateUnion.stateBitField.transferring = 0U;
1227             if (0U != (lpc3511IpState->epCommandStatusList[2] & USB_LPC3511IP_ENDPOINT_ACTIVE_MASK))
1228             {
1229 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1230                 if (kStatus_USB_Success != USB_DeviceLpc3511IpCancel(lpc3511IpState, (0x80u | USB_CONTROL_ENDPOINT)))
1231                 {
1232 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
1233                     usb_echo("cancel the pending transfer error\n");
1234 #endif
1235                 }
1236 #else
1237                 (void)USB_DeviceLpc3511IpCancel(lpc3511IpState, (0x80u | USB_CONTROL_ENDPOINT));
1238 #endif
1239             }
1240         }
1241 
1242         USB_LPC3511IP_ENDPOINT_SET_ENDPOINT_AND(
1243             lpc3511IpState, 0, 0, (~(USB_LPC3511IP_ENDPOINT_STALL_MASK | USB_LPC3511IP_ENDPOINT_ACTIVE_MASK)));
1244         USB_LPC3511IP_ENDPOINT_SET_ENDPOINT_AND(
1245             lpc3511IpState, 1, 0, (~(USB_LPC3511IP_ENDPOINT_STALL_MASK | USB_LPC3511IP_ENDPOINT_ACTIVE_MASK)));
1246 
1247         lpc3511IpState->registerBase->INTSTAT = 0x03u; /* clear interrupt */
1248         /* W1 to clear the setup flag */
1249         lpc3511IpState->registerBase->DEVCMDSTAT = (lpc3511IpState->registerBase->DEVCMDSTAT & ~USB_LPC3511IP_DEVCMDSTAT_W1C_MASK) | USB_LPC3511IP_DEVCMDSTAT_SETUP_MASK;
1250     }
1251     else
1252     {
1253         length = 0U;
1254 #if (defined USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE) && (USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE)
1255         if (0U != (lpc3511IpState->epCommandStatusList[epState->stateUnion.stateBitField.consumerOdd +
1256                                                        (((uint32_t)endpointIndex) * 2U)] &
1257                    USB_LPC3511IP_ENDPOINT_ACTIVE_MASK))
1258         {
1259             return;
1260         }
1261 #else
1262         if (0U != (lpc3511IpState->epCommandStatusList[endpointIndex * 2U] & USB_LPC3511IP_ENDPOINT_ACTIVE_MASK))
1263         {
1264             return;
1265         }
1266 #endif
1267 
1268 #if (defined USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE) && (USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE)
1269         if ((endpointIndex >> 1U) != USB_CONTROL_ENDPOINT)
1270         {
1271             len = USB_DeviceLpc3511IpTokenUpdate(lpc3511IpState, epState, endpointIndex, 1U);
1272             length += len;
1273 
1274             if ((epState->stateUnion.stateBitField.doubleBufferBusy > 0U) &&
1275                 (0U == (lpc3511IpState->epCommandStatusList[epState->stateUnion.stateBitField.consumerOdd +
1276                                                             (((uint32_t)endpointIndex) * 2U)] &
1277                         USB_LPC3511IP_ENDPOINT_ACTIVE_MASK)))
1278             {
1279 #if ((defined(USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER)) && (USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER > 0U))
1280                 USB_DeviceLpc3511IpDoPreviousTransactionMemcpy(
1281                     lpc3511IpState, epState, len, endpointIndex,
1282                     (uint8_t)(epState->stateUnion.stateBitField.consumerOdd ^ 1U));
1283 #endif
1284                 len = USB_DeviceLpc3511IpTokenUpdate(lpc3511IpState, epState, endpointIndex, 1U);
1285                 length += len;
1286             }
1287         }
1288         else
1289 #endif
1290         {
1291             length = USB_DeviceLpc3511IpTokenUpdate(lpc3511IpState, epState, endpointIndex, 0U);
1292             len    = length;
1293         }
1294 
1295         /* update remaining length */
1296         remainLength = epState->transferLength - epState->transferDone;
1297 
1298         /* Whether the transfer is completed or not.
1299          * The transfer is completed when one of the following conditions meet:
1300          * 1. The remaining length is zero.
1301          * 2. The length of current tansaction is not the multiple of max packet size.
1302          */
1303         if ((length > 0U) && (0U == (length % epState->stateUnion.stateBitField.maxPacketSize)) && (remainLength > 0U))
1304         {
1305 #if ((defined(USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER)) && (USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER > 0U))
1306             if ((endpointIndex >> 1U) == USB_CONTROL_ENDPOINT)
1307             {
1308                 /* do memcpy firstly */
1309                 USB_DeviceLpc3511IpDoPreviousTransactionMemcpy(lpc3511IpState, epState, len, endpointIndex, 0U);
1310 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1311                 if (kStatus_USB_Success != USB_DeviceLpc3511IpTransaction(lpc3511IpState, epState, endpointIndex))
1312                 {
1313 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
1314                     usb_echo("prime transaction error\n");
1315 #endif
1316                 }
1317 #else
1318                 (void)USB_DeviceLpc3511IpTransaction(lpc3511IpState, epState, endpointIndex);
1319 #endif
1320             }
1321             else
1322             {
1323 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1324                 if (kStatus_USB_Success != USB_DeviceLpc3511IpTransaction(lpc3511IpState, epState, endpointIndex))
1325                 {
1326 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
1327                     usb_echo("prime transaction error\n");
1328 #endif
1329                 }
1330 #else
1331                 (void)USB_DeviceLpc3511IpTransaction(lpc3511IpState, epState, endpointIndex);
1332 #endif
1333 
1334                 USB_DeviceLpc3511IpDoPreviousTransactionMemcpy(
1335                     lpc3511IpState, epState, len, endpointIndex,
1336                     (uint8_t)(epState->stateUnion.stateBitField.consumerOdd ^ 1U));
1337             }
1338 #else
1339 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1340             if (kStatus_USB_Success != USB_DeviceLpc3511IpTransaction(lpc3511IpState, epState, endpointIndex))
1341             {
1342                 return kStatus_USB_Error;
1343             }
1344 #else
1345             (void)USB_DeviceLpc3511IpTransaction(lpc3511IpState, epState, endpointIndex);
1346 #endif
1347 #endif
1348             return;
1349         }
1350         else
1351         {
1352             epState->stateUnion.stateBitField.transferring = 0U;
1353             message.length                                 = epState->transferDone;
1354             message.buffer                                 = epState->transferBuffer;
1355 
1356             /* process ZLT
1357              * 1. IN endpoint;
1358              * 2. transfer length is the multiple of max packet size.
1359              */
1360             if ((0U != (endpointIndex & 0x01U)) && (0U != length) &&
1361                 (0U == (length % epState->stateUnion.stateBitField.maxPacketSize)))
1362             {
1363 /* CONFIG_UDC_DRIVER is for Zephyr, it will not be defined in NXP MCUXpresso SDK */
1364 #if !((defined CONFIG_UDC_DRIVER) && (CONFIG_UDC_DRIVER))
1365                 if ((endpointIndex >> 1U) == USB_CONTROL_ENDPOINT)
1366                 {
1367                     temp        = (void *)(&(lpc3511IpState->setupData[0]));
1368                     setupPacket = (usb_setup_struct_t *)temp;
1369                     /*
1370                      * Send ZLT transaction if setup transfer and the required length is longer than actual length
1371                      */
1372                     if (USB_SHORT_FROM_LITTLE_ENDIAN(setupPacket->wLength) > epState->transferLength)
1373                     {
1374 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1375                         if (kStatus_USB_Success !=
1376                             USB_DeviceLpc3511IpEndpointPrime(lpc3511IpState, epState, 1U, NULL, 0U))
1377                         {
1378 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
1379                             usb_echo("prime error\n");
1380 #endif
1381                         }
1382 #else
1383                         (void)USB_DeviceLpc3511IpEndpointPrime(lpc3511IpState, epState, 1U, NULL, 0U);
1384 #endif
1385                         return;
1386                     }
1387                 }
1388                 else
1389 #endif
1390                 if ((0U != epState->stateUnion.stateBitField.zlt))
1391                 {
1392 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1393                     if (kStatus_USB_Success !=
1394                         USB_DeviceLpc3511IpEndpointPrime(lpc3511IpState, epState, endpointIndex, NULL, 0U))
1395                     {
1396 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
1397                         usb_echo("prime error\n");
1398 #endif
1399                     }
1400 #else
1401                     (void)USB_DeviceLpc3511IpEndpointPrime(lpc3511IpState, epState, endpointIndex, NULL, 0U);
1402 #endif
1403                     return;
1404                 }
1405                 else
1406                 {
1407                     /*no action*/
1408                 }
1409             }
1410         }
1411     }
1412 
1413     message.isSetup = isSetup;
1414     message.code    = ((uint8_t)(endpointIndex >> 1) | (uint8_t)(((endpointIndex & 0x01U) << 0x07U)));
1415 
1416 #if ((defined(USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER)) && (USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER > 0U))
1417     USB_DeviceLpc3511IpDoPreviousTransactionMemcpy(lpc3511IpState, epState, len, endpointIndex,
1418                                                    (uint8_t)(epState->stateUnion.stateBitField.consumerOdd ^ 1U));
1419 #endif
1420     /* Notify the up layer the controller status changed. */
1421 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1422     if (kStatus_USB_Success != USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message))
1423     {
1424 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
1425         usb_echo("notification error\n");
1426 #endif
1427     }
1428 #else
1429     (void)USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message);
1430 #endif
1431 }
1432 
1433 /*!
1434  * @brief Handle the USB bus reset interrupt.
1435  *
1436  * The function is used to handle the USB bus reset interrupt.
1437  *
1438  * @param lpc3511IpState       Pointer of the controller state structure.
1439  *
1440  */
USB_DeviceLpc3511IpInterruptReset(usb_device_lpc3511ip_state_struct_t * lpc3511IpState)1441 static void USB_DeviceLpc3511IpInterruptReset(usb_device_lpc3511ip_state_struct_t *lpc3511IpState)
1442 {
1443     usb_device_callback_message_struct_t message;
1444 
1445     /* Set reset flag */
1446     lpc3511IpState->isResetting = 1U;
1447 
1448 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1449     if (0U != lpc3511IpState->controllerSpeed)
1450     {
1451         if (((lpc3511IpState->registerBase->DEVCMDSTAT & USBHSD_DEVCMDSTAT_Speed_MASK) >>
1452              USBHSD_DEVCMDSTAT_Speed_SHIFT) == 0x02U)
1453         {
1454             lpc3511IpState->deviceSpeed = USB_SPEED_HIGH;
1455         }
1456         else if (((lpc3511IpState->registerBase->DEVCMDSTAT & USBHSD_DEVCMDSTAT_Speed_MASK) >>
1457                   USBHSD_DEVCMDSTAT_Speed_SHIFT) == 0x01U)
1458         {
1459             lpc3511IpState->deviceSpeed = USB_SPEED_FULL;
1460         }
1461         else
1462         {
1463             /*no action*/
1464         }
1465     }
1466     else
1467 #endif
1468     {
1469         lpc3511IpState->deviceSpeed = USB_SPEED_FULL;
1470     }
1471 
1472     message.buffer  = (uint8_t *)NULL;
1473     message.code    = (uint8_t)kUSB_DeviceNotifyBusReset;
1474     message.length  = 0U;
1475     message.isSetup = 0U;
1476     /* Notify up layer the USB bus reset signal detected. */
1477 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1478     if (kStatus_USB_Success != USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message))
1479     {
1480 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
1481         usb_echo("notification error\n");
1482 #endif
1483     }
1484 #else
1485     (void)USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message);
1486 #endif
1487 }
1488 
1489 #if (defined(USB_DEVICE_CONFIG_DETACH_ENABLE) && (USB_DEVICE_CONFIG_DETACH_ENABLE))
1490 /*!
1491  * @brief Handle detach interrupt.
1492  *
1493  * The function is used to handle the detach interrupt.
1494  *
1495  * @param lpc3511IpState       Pointer of the controller state structure.
1496  *
1497  */
USB_DeviceLpc3511IpInterruptDetach(usb_device_lpc3511ip_state_struct_t * lpc3511IpState)1498 static void USB_DeviceLpc3511IpInterruptDetach(usb_device_lpc3511ip_state_struct_t *lpc3511IpState)
1499 {
1500     usb_device_callback_message_struct_t message;
1501 
1502     message.buffer  = (uint8_t *)NULL;
1503     message.code    = (uint8_t)kUSB_DeviceNotifyDetach;
1504     message.length  = 0U;
1505     message.isSetup = 0U;
1506 
1507     /* Notify up layer the USB VBUS falling signal detected. */
1508 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1509     if (kStatus_USB_Success != USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message))
1510     {
1511 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
1512         usb_echo("notification error\n");
1513 #endif
1514     }
1515 #else
1516     (void)USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message);
1517 #endif
1518 }
1519 
1520 /*!
1521  * @brief Handle Attach interrupt.
1522  *
1523  * The function is used to handle the attach interrupt.
1524  *
1525  * @param lpc3511IpState       Pointer of the controller state structure.
1526  *
1527  */
USB_DeviceLpc3511IpInterruptAttach(usb_device_lpc3511ip_state_struct_t * lpc3511IpState)1528 static void USB_DeviceLpc3511IpInterruptAttach(usb_device_lpc3511ip_state_struct_t *lpc3511IpState)
1529 {
1530     usb_device_callback_message_struct_t message;
1531 
1532     message.buffer  = (uint8_t *)NULL;
1533     message.code    = (uint8_t)kUSB_DeviceNotifyAttach;
1534     message.length  = 0U;
1535     message.isSetup = 0U;
1536 
1537     /* Notify up layer the USB VBUS rising signal detected. */
1538 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1539     if (kStatus_USB_Success != USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message))
1540     {
1541 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
1542         usb_echo("notification error\n");
1543 #endif
1544     }
1545 #else
1546     (void)USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message);
1547 #endif
1548 }
1549 #endif
1550 #if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U)) && \
1551     (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
1552 /* The device dcd callback */
USB_DeviceLpcIp3511IsrHSDCDCallback(void * handle,uint32_t event,void * param)1553 static usb_hsdcd_status_t USB_DeviceLpcIp3511IsrHSDCDCallback(void *handle, uint32_t event, void *param)
1554 {
1555     usb_hsdcd_status_t error = kStatus_hsdcd_Success;
1556     usb_device_callback_message_struct_t message;
1557     usb_device_lpc3511ip_state_struct_t *lpc3511IpState = (usb_device_lpc3511ip_state_struct_t *)handle;
1558 
1559     if (lpc3511IpState == NULL)
1560     {
1561         return kStatus_hsdcd_Error;
1562     }
1563 
1564     /*messsgae buffer contain event information*/
1565     message.buffer  = (uint8_t *)param;
1566     message.length  = 0U;
1567     message.isSetup = 0U;
1568     message.code    = (uint8_t)kUSB_DeviceNotifyDcdDetectFinished;
1569 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1570     if (kStatus_USB_Success != USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message))
1571     {
1572         return kStatus_USB_Error;
1573     }
1574 #else
1575     (void)USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message);
1576 #endif
1577     return error;
1578 }
1579 
USB_DeviceLpcIp3511IsrDCDFunction(void * deviceHandle)1580 void USB_DeviceLpcIp3511IsrDCDFunction(void *deviceHandle)
1581 {
1582     usb_device_struct_t *handle = (usb_device_struct_t *)deviceHandle;
1583     usb_device_lpc3511ip_state_struct_t *lpc3511IpState;
1584     if (NULL == deviceHandle)
1585     {
1586         return;
1587     }
1588     lpc3511IpState = (usb_device_lpc3511ip_state_struct_t *)(handle->controllerHandle);
1589     USB_HSDcdIsrFunction(lpc3511IpState->dcdHandle);
1590 }
1591 #endif
USB_DeviceLpc3511IpInit(uint8_t controllerId,usb_device_handle handle,usb_device_controller_handle * controllerHandle)1592 usb_status_t USB_DeviceLpc3511IpInit(uint8_t controllerId,
1593                                      usb_device_handle handle,
1594                                      usb_device_controller_handle *controllerHandle)
1595 {
1596     usb_device_lpc3511ip_state_struct_t *lpc3511IpState = NULL;
1597 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511FS)) && (USB_DEVICE_CONFIG_LPCIP3511FS > 0U))
1598     uint32_t ip3511FsBases[] = USB_BASE_ADDRS;
1599 #endif
1600     uint32_t *endpointListArray[] = LPC_CONTROLLER_ENDPOINT_LIST_ARRAY;
1601 
1602 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1603 #if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U)) && \
1604     (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
1605     uint32_t hsdcd_base[] = USBHSDCD_BASE_ADDRS;
1606     USBHSDCD_Type *base;
1607     usb_hsdcd_config_struct_t dcdParamConfig;
1608     usb_hsdcd_status_t dcdError = kStatus_hsdcd_Success;
1609 #endif
1610 
1611     uint32_t ip3511HsBases[] = USBHSD_BASE_ADDRS;
1612     if ((controllerId >= (uint8_t)kUSB_ControllerLpcIp3511Hs0) &&
1613         (controllerId <= (uint8_t)kUSB_ControllerLpcIp3511Hs1))
1614     {
1615         if (((uint32_t)controllerId - (uint32_t)kUSB_ControllerLpcIp3511Hs0) >=
1616             (sizeof(ip3511HsBases) / sizeof(uint32_t)))
1617         {
1618             return kStatus_USB_ControllerNotFound;
1619         }
1620         lpc3511IpState = &s_UsbDeviceLpc3511IpState[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Hs0 +
1621                                                     USB_DEVICE_CONFIG_LPCIP3511FS];
1622 #if (!(defined(USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY) && (USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY)))
1623         lpc3511IpState->controlData =
1624             (uint8_t *)&s_SetupAndEpReservedData[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Hs0 +
1625                                                  USB_DEVICE_CONFIG_LPCIP3511FS][CONTROL_TRANSFER_DATA_OFFSET];
1626         lpc3511IpState->setupData =
1627             (uint8_t *)&s_SetupAndEpReservedData[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Hs0 +
1628                                                  USB_DEVICE_CONFIG_LPCIP3511FS][SETUP_TRANSFER_DATA_OFFSET];
1629         lpc3511IpState->zeroTransactionData =
1630             (uint8_t *)&s_SetupAndEpReservedData[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Hs0 +
1631                                                  USB_DEVICE_CONFIG_LPCIP3511FS][ZERO_TRANSFER_DATA_OFFSET];
1632 #endif
1633         /* set the endpoint list */
1634         lpc3511IpState->epCommandStatusList =
1635             endpointListArray[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Hs0 + USB_DEVICE_CONFIG_LPCIP3511FS];
1636         /* get the ip base address */
1637         lpc3511IpState->registerBase =
1638             (USB_LPC3511IP_Type *)ip3511HsBases[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Hs0];
1639 #if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U)) && \
1640     (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
1641         base                       = (USBHSDCD_Type *)hsdcd_base[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Hs0];
1642         dcdParamConfig.dcdCallback = USB_DeviceLpcIp3511IsrHSDCDCallback;
1643         dcdParamConfig.dcdCallbackParam = (void *)lpc3511IpState;
1644         dcdError                        = USB_HSDCD_Init(base, &dcdParamConfig, &lpc3511IpState->dcdHandle);
1645         if (kStatus_hsdcd_Success != dcdError)
1646         {
1647             return kStatus_USB_Error;
1648         }
1649 #endif
1650     }
1651     else
1652 #endif
1653 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511FS)) && (USB_DEVICE_CONFIG_LPCIP3511FS > 0U))
1654     {
1655         /* get the controller instance */
1656         if ((controllerId < (uint8_t)kUSB_ControllerLpcIp3511Fs0) ||
1657             ((controllerId - (uint8_t)kUSB_ControllerLpcIp3511Fs0) >= (uint8_t)USB_DEVICE_CONFIG_LPCIP3511FS) ||
1658             (((uint32_t)controllerId - (uint32_t)kUSB_ControllerLpcIp3511Fs0) >=
1659              (sizeof(ip3511FsBases) / sizeof(uint32_t))))
1660         {
1661             return kStatus_USB_ControllerNotFound;
1662         }
1663         lpc3511IpState = &s_UsbDeviceLpc3511IpState[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Fs0];
1664 #if (!(defined(USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY) && (USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY)))
1665         lpc3511IpState->controlData =
1666             (uint8_t *)&s_SetupAndEpReservedData[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Fs0]
1667                                                 [CONTROL_TRANSFER_DATA_OFFSET];
1668         lpc3511IpState->setupData =
1669             (uint8_t *)&s_SetupAndEpReservedData[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Fs0]
1670                                                 [SETUP_TRANSFER_DATA_OFFSET];
1671         lpc3511IpState->zeroTransactionData =
1672             (uint8_t *)&s_SetupAndEpReservedData[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Fs0]
1673                                                 [ZERO_TRANSFER_DATA_OFFSET];
1674 #endif
1675         /* set the endpoint list */
1676         lpc3511IpState->epCommandStatusList = endpointListArray[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Fs0];
1677         /* get the ip base address */
1678         lpc3511IpState->registerBase =
1679             (USB_LPC3511IP_Type *)ip3511FsBases[controllerId - (uint8_t)kUSB_ControllerLpcIp3511Fs0];
1680     }
1681 #else
1682     {
1683         return kStatus_USB_ControllerNotFound;
1684     }
1685 #endif
1686 
1687     lpc3511IpState->controllerId = controllerId;
1688 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1689     if ((lpc3511IpState->controllerId >= (uint8_t)kUSB_ControllerLpcIp3511Hs0) &&
1690         (lpc3511IpState->controllerId <= (uint8_t)kUSB_ControllerLpcIp3511Hs1))
1691     {
1692         lpc3511IpState->controllerSpeed = 1U;
1693 #if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
1694      (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
1695         lpc3511IpState->hsInterruptIssue = ((Chip_GetVersion() == FSL_ROM_VERSION_1B) ? 0U : 1U);
1696 #endif
1697     }
1698     else
1699     {
1700         lpc3511IpState->controllerSpeed = 0U;
1701     }
1702 #endif
1703 
1704 #if defined(USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY) && (USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY)
1705     lpc3511IpState->controlData         = USB_DeviceLpcIp3511MallocMaxPacketBuffer(CONTROL_TRANSFER_DATA_SIZE);
1706     lpc3511IpState->setupData           = USB_DeviceLpcIp3511MallocMaxPacketBuffer(SETUP_TRANSFER_DATA_SIZE);
1707     lpc3511IpState->zeroTransactionData = USB_DeviceLpcIp3511MallocMaxPacketBuffer(ZERO_TRANSFER_DATA_SIZE);
1708 #endif
1709 
1710     /* disable the controller */
1711     lpc3511IpState->registerBase->DEVCMDSTAT &=
1712         (~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_DCON_MASK | USB_LPC3511IP_DEVCMDSTAT_DEV_EN_MASK |
1713            USB_LPC3511IP_DEVCMDSTAT_LPM_SUP_MASK));
1714     /* reset and enalbe the controller */
1715     USB_DeviceLpc3511IpSetDefaultState(lpc3511IpState);
1716     /* enable USB */
1717     lpc3511IpState->registerBase->DEVCMDSTAT = (lpc3511IpState->registerBase->DEVCMDSTAT & ~USB_LPC3511IP_DEVCMDSTAT_W1C_MASK) | (USB_LPC3511IP_DEVCMDSTAT_DEV_EN_MASK
1718 #if ((defined(USB_DEVICE_CONFIG_LOW_POWER_MODE)) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
1719 #else
1720                                                  | USB_LPC3511IP_DEVCMDSTAT_FORCE_NEEDCLK_MASK
1721 #endif
1722     );
1723 #if (defined(USB_DEVICE_CONFIG_LPM_L1) && (USB_DEVICE_CONFIG_LPM_L1 > 0U))
1724     lpc3511IpState->registerBase->DEVCMDSTAT = (lpc3511IpState->registerBase->DEVCMDSTAT & ~USB_LPC3511IP_DEVCMDSTAT_W1C_MASK) | USB_LPC3511IP_DEVCMDSTAT_LPM_SUP_MASK;
1725     lpc3511IpState->registerBase->LPM |= USB_LPC3511IP_USB_LPM_HIRD_SW(4);
1726     lpc3511IpState->registerBase->DEVCMDSTAT &= ~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_FORCE_NEEDCLK_MASK);
1727 #endif
1728     lpc3511IpState->deviceHandle = handle;
1729     *controllerHandle            = lpc3511IpState;
1730 
1731     return kStatus_USB_Success;
1732 }
1733 
USB_DeviceLpc3511IpDeinit(usb_device_controller_handle controllerHandle)1734 usb_status_t USB_DeviceLpc3511IpDeinit(usb_device_controller_handle controllerHandle)
1735 {
1736     usb_device_lpc3511ip_state_struct_t *lpc3511IpState = (usb_device_lpc3511ip_state_struct_t *)controllerHandle;
1737     uint32_t usbAddress;
1738     usb_status_t status = kStatus_USB_Success;
1739     if (controllerHandle == NULL)
1740     {
1741         return kStatus_USB_InvalidHandle;
1742     }
1743     /* Clear all interrupt flags. */
1744     lpc3511IpState->registerBase->INTSTAT = (USB_LPC3511IP_INTSTAT_DEV_INT_MASK | USB_LPC3511IP_INTSTAT_FRAME_INT_MASK |
1745                                              USB_LPC3511IP_MAX_PHY_ENDPOINT_MASK);
1746     /* Disable all interrupts. */
1747     lpc3511IpState->registerBase->INTEN = 0U;
1748     /* Clear device address. */
1749     usbAddress = 0U;
1750     status     = USB_DeviceLpc3511IpControlPreSetDeviceAddress(controllerHandle, &usbAddress);
1751     if (kStatus_USB_Success == status)
1752     {
1753         /*no action, just for misra4.7*/
1754     }
1755 #if (defined(USB_DEVICE_CONFIG_LPM_L1) && (USB_DEVICE_CONFIG_LPM_L1 > 0U))
1756     lpc3511IpState->registerBase->DEVCMDSTAT &= ~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_LPM_SUP_MASK);
1757 #endif
1758     /* disable the controller */
1759     lpc3511IpState->registerBase->DEVCMDSTAT &=
1760         (~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_DCON_MASK | USB_LPC3511IP_DEVCMDSTAT_DEV_EN_MASK |
1761            USB_LPC3511IP_DEVCMDSTAT_FORCE_NEEDCLK_MASK));
1762 #if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U)) && \
1763     (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
1764 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
1765     if (kStatus_USB_Success != USB_HSDCD_Deinit(lpc3511IpState->dcdHandle))
1766     {
1767         return kStatus_USB_Error;
1768     }
1769 #else
1770     (void)USB_HSDCD_Deinit(lpc3511IpState->dcdHandle);
1771 #endif
1772 #endif
1773 
1774 #if defined(USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY) && (USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY)
1775     USB_DeviceLpcIp3511ReleaseMaxPacketBuffer(lpc3511IpState->controlData, CONTROL_TRANSFER_DATA_SIZE);
1776     USB_DeviceLpcIp3511ReleaseMaxPacketBuffer(lpc3511IpState->setupData, SETUP_TRANSFER_DATA_SIZE);
1777     USB_DeviceLpcIp3511ReleaseMaxPacketBuffer(lpc3511IpState->zeroTransactionData, ZERO_TRANSFER_DATA_SIZE);
1778 #endif
1779 
1780     return status;
1781 }
1782 
USB_DeviceLpc3511IpGetActualBufferAndPrime(usb_device_lpc3511ip_state_struct_t * lpc3511IpState,usb_device_lpc3511ip_endpoint_state_struct_t * epState,uint8_t endpointIndex,uint8_t changedOdd)1783 static usb_status_t USB_DeviceLpc3511IpGetActualBufferAndPrime(usb_device_lpc3511ip_state_struct_t *lpc3511IpState,
1784                                                                usb_device_lpc3511ip_endpoint_state_struct_t *epState,
1785                                                                uint8_t endpointIndex,
1786                                                                uint8_t changedOdd)
1787 {
1788     uint8_t *destBuffer;
1789     uint8_t *actualBuffer;
1790     uint32_t length;
1791     uint8_t odd;
1792     uint8_t index;
1793 
1794 #if (defined USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE) && (USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE)
1795     if (0U != changedOdd)
1796     {
1797         odd = (uint8_t)epState->stateUnion.stateBitField.producerOdd;
1798     }
1799     else
1800 #endif
1801     {
1802         odd = 0;
1803     }
1804     actualBuffer = epState->transferBuffer + epState->transferPrimedLength;
1805     length       = epState->transferLength - epState->transferPrimedLength;
1806     /* Data length needs to less than max packet size. */
1807 #if ((defined(USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX)) && \
1808      (USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX > 0U))
1809     if ((USB_ENDPOINT_BULK == epState->stateUnion.stateBitField.endpointType) && ((endpointIndex & 0x01U) == 0x00U))
1810     {
1811         if (length > USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX)
1812         {
1813             length = USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX;
1814         }
1815     }
1816     else
1817     {
1818         if (length > epState->stateUnion.stateBitField.maxPacketSize)
1819         {
1820             length = epState->stateUnion.stateBitField.maxPacketSize;
1821         }
1822     }
1823 #else
1824     if (length > epState->stateUnion.stateBitField.maxPacketSize)
1825     {
1826         length = epState->stateUnion.stateBitField.maxPacketSize;
1827     }
1828 #endif
1829 
1830     epState->epBufferStatusUnion[odd].epBufferStatusField.epPacketCopyed = 0;
1831 
1832     index = (endpointIndex & 0x01u); /* index mean IN endpoint here */
1833     if (length > 0U)
1834     {
1835 #if ((defined(USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY)) && (USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY > 0U))
1836 /* if all the enabled IP support the reserved buffer, then don't need the judgement. */
1837 #if (!USB_DEVICE_IP3511_ALL_IP_SUPPORT_RESERVED_BUFFER)
1838 #define USB_DEVICE_IP3511_NEED_CHECK_BUFFER (1u)
1839         /* lengt > 0 && ((buffer not align with 64) || (buffer is not in the deticated ram))) */
1840         if (USB_DeviceLpcIp3511MaxPacketNeedCopy(lpc3511IpState))
1841 #endif
1842 #else
1843 #define USB_DEVICE_IP3511_NEED_CHECK_BUFFER (1u)
1844         /* align the buffer for control transfer */
1845         if (((endpointIndex >> 1U) == USB_CONTROL_ENDPOINT))
1846 #endif
1847         {
1848 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1849 #if ((defined(FSL_FEATURE_USBHSD_USB_RAM_BASE_ADDRESS)) && (FSL_FEATURE_USBHSD_USB_RAM_BASE_ADDRESS))
1850             uint32_t bufferValue = (uint32_t)actualBuffer;
1851 #if ((defined(__SAUREGION_PRESENT)) && (__SAUREGION_PRESENT > 0U))
1852             bufferValue &= (0xEFFFFFFFu); /* bit28 is the secure address label */
1853 #endif
1854 #endif
1855 #endif
1856             /* not 64 bytes align || not in the dedicated ram || ( OUT && not mutiple of 4 ) */
1857             if ((((uint32_t)actualBuffer & 0x0000003FU) != 0U) ||
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 #endif
1862 #endif
1863                     (((uint32_t)actualBuffer & 0xFFC00000U) !=
1864                      (lpc3511IpState->registerBase->DATABUFSTART & 0xFFC00000U))
1865 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1866 #if ((defined(FSL_FEATURE_USBHSD_USB_RAM_BASE_ADDRESS)) && (FSL_FEATURE_USBHSD_USB_RAM_BASE_ADDRESS))
1867 
1868                     || ((0U != lpc3511IpState->controllerSpeed) &&
1869                         ((bufferValue < (uint32_t)FSL_FEATURE_USBHSD_USB_RAM_BASE_ADDRESS) ||
1870                          (bufferValue >
1871                           ((uint32_t)FSL_FEATURE_USBHSD_USB_RAM_BASE_ADDRESS + (uint32_t)FSL_FEATURE_USBHSD_USB_RAM))))
1872 #endif
1873 #endif
1874 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1875 #if ((defined(FSL_FEATURE_USBHSD_USB_RAM_BASE_ADDRESS)) && (FSL_FEATURE_USBHSD_USB_RAM_BASE_ADDRESS))
1876                         )
1877 #endif
1878 #endif
1879 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1880                 || ((0U != lpc3511IpState->controllerSpeed) && (0U == index) &&
1881                     (length != epState->stateUnion.stateBitField.maxPacketSize)))
1882 #else
1883             )
1884 #endif
1885             {
1886                 epState->epBufferStatusUnion[odd].epBufferStatusField.epPacketCopyed = 1U;
1887 /* usb copy buffer for this packet */
1888 #if (defined USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE) && (USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE)
1889 #if ((defined(USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX)) && \
1890      (USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX > 0U))
1891                 if ((USB_ENDPOINT_BULK == epState->stateUnion.stateBitField.endpointType) &&
1892                     ((endpointIndex & 0x01U) == 0x00U))
1893                 {
1894                     destBuffer = (uint8_t *)(epState->epPacketBuffer +
1895                                              (odd * USB_LPC3511IP_GET_MULTIPLE_OF_64(
1896                                                         USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX)));
1897                 }
1898                 else
1899                 {
1900                     destBuffer = (uint8_t *)(epState->epPacketBuffer +
1901                                              (odd * USB_LPC3511IP_GET_MULTIPLE_OF_64(
1902                                                         epState->stateUnion.stateBitField.maxPacketSize)));
1903                 }
1904 #else
1905                 destBuffer =
1906                     (uint8_t *)(epState->epPacketBuffer + (odd * USB_LPC3511IP_GET_MULTIPLE_OF_64(
1907                                                                      epState->stateUnion.stateBitField.maxPacketSize)));
1908 #endif
1909 #else
1910                 destBuffer = (uint8_t *)(epState->epPacketBuffer);
1911 #endif
1912                 if (0U != index) /* USB_IN */
1913                 {
1914                     (void)memcpy(destBuffer, actualBuffer, length);
1915                 }
1916                 else
1917                 {
1918 #if ((defined(FSL_FEATURE_USB_VERSION) && (FSL_FEATURE_USB_VERSION >= 200U)) || \
1919      (defined(FSL_FEATURE_USBHSD_VERSION) && (FSL_FEATURE_USBHSD_VERSION >= 300U)))
1920 #else
1921 #if ((defined(USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX)) && \
1922      (USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX > 0U))
1923                     if (length < epState->stateUnion.stateBitField.maxPacketSize)
1924                     {
1925                         length = epState->stateUnion.stateBitField.maxPacketSize;
1926                     }
1927                     else
1928                     {
1929                         length = ((length + epState->stateUnion.stateBitField.maxPacketSize - 1) /
1930                                   epState->stateUnion.stateBitField.maxPacketSize) *
1931                                  (epState->stateUnion.stateBitField.maxPacketSize);
1932                     }
1933 #else
1934                     length = epState->stateUnion.stateBitField.maxPacketSize;
1935 #endif
1936 #endif
1937                 }
1938                 actualBuffer = destBuffer;
1939             }
1940         }
1941 #if (defined USB_DEVICE_IP3511_NEED_CHECK_BUFFER) && (USB_DEVICE_IP3511_NEED_CHECK_BUFFER)
1942         else /* cannot do memory copy */
1943         {
1944             /* not 64 bytes align || not in the dedicated ram || ( OUT && HS && not mutiple of 4 ) */
1945             if ((((uint32_t)actualBuffer & 0x0000003FU) != 0U) ||
1946                 (((uint32_t)actualBuffer & 0xFFC00000U) != (lpc3511IpState->registerBase->DATABUFSTART & 0xFFC00000U))
1947 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1948                 || ((0U != lpc3511IpState->controllerSpeed) && (0U == index) && ((length & 0x00000003u) != 0U)))
1949 #else
1950             )
1951 #endif
1952             {
1953                 return kStatus_USB_Error;
1954             }
1955         }
1956 #endif
1957     }
1958 
1959     /* Send/Receive data when the device is not resetting. */
1960     if (0U == lpc3511IpState->isResetting)
1961     {
1962         return USB_DeviceLpc3511IpEndpointPrime(lpc3511IpState, epState, endpointIndex, actualBuffer, length);
1963     }
1964     else
1965     {
1966         return kStatus_USB_Error;
1967     }
1968 }
USB_DeviceLpc3511IpTransaction(usb_device_lpc3511ip_state_struct_t * lpc3511IpState,usb_device_lpc3511ip_endpoint_state_struct_t * epState,uint8_t endpointIndex)1969 static usb_status_t USB_DeviceLpc3511IpTransaction(usb_device_lpc3511ip_state_struct_t *lpc3511IpState,
1970                                                    usb_device_lpc3511ip_endpoint_state_struct_t *epState,
1971                                                    uint8_t endpointIndex)
1972 {
1973     usb_status_t status = kStatus_USB_Error;
1974     OSA_SR_ALLOC();
1975 
1976 #if (defined(FSL_FEATURE_USBHSD_VERSION) && (FSL_FEATURE_USBHSD_VERSION >= 300U)) && \
1977     (!(defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) &&            \
1978        (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK)))
1979 #else
1980 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
1981 #if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
1982      (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
1983     if (lpc3511IpState->hsInterruptIssue)
1984     {
1985 #endif
1986         /* high-speed */
1987         if ((0U != (epState->stateUnion.stateBitField.epControlDefault &
1988                     ((USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK) >> USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT))) &&
1989             (USB_ENDPOINT_INTERRUPT == epState->stateUnion.stateBitField.endpointType) &&
1990             (0U != lpc3511IpState->controllerSpeed))
1991         {
1992             /* users can use NVIC to disable/enable the USB interrupt to improve the system performance */
1993             OSA_ENTER_CRITICAL();
1994 
1995             lpc3511IpState->registerBase->DEVCMDSTAT =
1996                 (lpc3511IpState->registerBase->DEVCMDSTAT & ~USB_LPC3511IP_DEVCMDSTAT_W1C_MASK) |
1997                 (USB_LPC3511IP_DEVCMDSTAT_INTONNAK_AO_MASK | USB_LPC3511IP_DEVCMDSTAT_INTONNAK_AI_MASK);
1998 
1999 #if (defined USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE) && (USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE)
2000             USB_LPC3511IP_ENDPOINT_SET_ENDPOINT(
2001                 lpc3511IpState, endpointIndex, epState->stateUnion.stateBitField.producerOdd,
2002                 (epState->stateUnion.stateBitField.epControlDefault << USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT) |
2003                     (epState->stateUnion.stateBitField.stalled << USB_LPC3511IP_ENDPOINT_STALL_SHIFT),
2004                 0U, (uint32_t)lpc3511IpState->zeroTransactionData);
2005 #else
2006         USB_LPC3511IP_ENDPOINT_SET_ENDPOINT(
2007             lpc3511IpState, endpointIndex, 0,
2008             (epState->stateUnion.stateBitField.epControlDefault << USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT) |
2009                 (epState->stateUnion.stateBitField.stalled << USB_LPC3511IP_ENDPOINT_STALL_SHIFT),
2010             0U, (uint32_t)lpc3511IpState->zeroTransactionData);
2011 #endif
2012             /* users can use NVIC to disable/enable the USB interrupt to improve the system performance */
2013             OSA_EXIT_CRITICAL();
2014             return kStatus_USB_Success;
2015         }
2016 #if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
2017      (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
2018     }
2019 #endif
2020 #endif
2021 #endif
2022 
2023     /* Enter critical */
2024     OSA_ENTER_CRITICAL();
2025     if (0U != epState->stateUnion.stateBitField.stalled)
2026     {
2027         if ((endpointIndex >> 1U) != USB_ENDPOINT_CONTROL)
2028         {
2029             epState->stateUnion.stateBitField.stallPrimed = 1u;
2030             status                                        = kStatus_USB_Success;
2031         }
2032         status = kStatus_USB_Error;
2033         OSA_EXIT_CRITICAL();
2034         return status;
2035     }
2036     OSA_EXIT_CRITICAL();
2037 
2038     /* 1. transfer size is 0; 2. All are primed */
2039     if ((epState->transferLength <= epState->transferPrimedLength) && (epState->transferLength != 0U))
2040     {
2041         return kStatus_USB_Success;
2042     }
2043 #if (defined USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE) && (USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE)
2044     if ((endpointIndex >> 1U) != USB_CONTROL_ENDPOINT)
2045     {
2046         /* disable endpoint interrupts, users can use NVIC to disable/enable the USB interrupt to improve the system
2047          * performance */
2048         OSA_ENTER_CRITICAL();
2049         /* lpc3511IpState->registerBase->INTEN &= (uint32_t)(~(USB_LPC3511IP_MAX_PHY_ENDPOINT_MASK)); */
2050 #if ((defined(USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER)) && (USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER > 0U))
2051         /* for out endpoint,only use buffer toggle, disable prime double buffer at the same time*/
2052         /*host send data less than maxpacket size and in endpoint prime length more more than maxpacketsize, there will
2053          * be state mismtach*/
2054         if (0U == (endpointIndex & 0x1U))
2055         {
2056             status = USB_DeviceLpc3511IpGetActualBufferAndPrime(lpc3511IpState, epState, endpointIndex, 1U);
2057         }
2058         else
2059 #endif
2060         {
2061             do
2062             {
2063                 status = USB_DeviceLpc3511IpGetActualBufferAndPrime(lpc3511IpState, epState, endpointIndex, 1U);
2064                 if (status != kStatus_USB_Success)
2065                 {
2066                     break;
2067                 }
2068             } while ((epState->transferLength > epState->transferPrimedLength) &&
2069                      (epState->stateUnion.stateBitField.doubleBufferBusy < 2U));
2070         }
2071         /* enable endpoint interrupt again, users can use NVIC to disable/enable the USB interrupt to improve the system
2072          * performance */
2073         OSA_EXIT_CRITICAL();
2074     }
2075     else
2076 #endif
2077     {
2078         status = USB_DeviceLpc3511IpGetActualBufferAndPrime(lpc3511IpState, epState, endpointIndex, 0U);
2079     }
2080     return status;
2081 }
2082 
USB_DeviceLpc3511IpSend(usb_device_controller_handle controllerHandle,uint8_t endpointAddress,uint8_t * buffer,uint32_t length)2083 usb_status_t USB_DeviceLpc3511IpSend(usb_device_controller_handle controllerHandle,
2084                                      uint8_t endpointAddress,
2085                                      uint8_t *buffer,
2086                                      uint32_t length)
2087 {
2088     usb_device_lpc3511ip_state_struct_t *lpc3511IpState = (usb_device_lpc3511ip_state_struct_t *)controllerHandle;
2089     uint8_t endpointIndex                               = USB_LPC3511IP_ENDPOINT_DES_INDEX(endpointAddress);
2090     usb_device_lpc3511ip_endpoint_state_struct_t *epState =
2091         USB_DeviceLpc3511IpGetEndpointStateStruct(lpc3511IpState, endpointIndex);
2092 
2093     if (1U == epState->stateUnion.stateBitField.transferring)
2094     {
2095         return kStatus_USB_Error;
2096     }
2097 
2098     /* Save the transfer information */
2099     epState->transferDone         = 0U;
2100     epState->transferBuffer       = buffer;
2101     epState->transferLength       = length;
2102     epState->transferPrimedLength = 0U;
2103 
2104     return USB_DeviceLpc3511IpTransaction(lpc3511IpState, epState, endpointIndex);
2105 
2106 /* prime the control setup transfer if it is control in endpoint and data length is zero
2107  * For IP3511 there is no need to prime, the buffer is always in the command/status list
2108  */
2109 #if 0
2110     if ((0U == length) && (USB_CONTROL_ENDPOINT == (endpointAddress & USB_ENDPOINT_NUMBER_MASK)))
2111     {
2112         USB_DeviceLpc3511IpPrimeNextSetup(lpc3511IpState);
2113     }
2114 #endif
2115 }
2116 
USB_DeviceLpc3511IpRecv(usb_device_controller_handle controllerHandle,uint8_t endpointAddress,uint8_t * buffer,uint32_t length)2117 usb_status_t USB_DeviceLpc3511IpRecv(usb_device_controller_handle controllerHandle,
2118                                      uint8_t endpointAddress,
2119                                      uint8_t *buffer,
2120                                      uint32_t length)
2121 {
2122     return USB_DeviceLpc3511IpSend(controllerHandle, endpointAddress, buffer, length);
2123 }
2124 
USB_DeviceLpc3511IpCancel(usb_device_controller_handle controllerHandle,uint8_t ep)2125 usb_status_t USB_DeviceLpc3511IpCancel(usb_device_controller_handle controllerHandle, uint8_t ep)
2126 {
2127     /* users can use NVIC to disable/enable the USB interrupt to improve the system performance */
2128     OSA_SR_ALLOC();
2129 
2130     usb_device_lpc3511ip_state_struct_t *lpc3511IpState = (usb_device_lpc3511ip_state_struct_t *)controllerHandle;
2131     usb_device_callback_message_struct_t message;
2132     uint8_t endpointIndex = USB_LPC3511IP_ENDPOINT_DES_INDEX(ep);
2133     usb_device_lpc3511ip_endpoint_state_struct_t *epState =
2134         USB_DeviceLpc3511IpGetEndpointStateStruct(lpc3511IpState, endpointIndex);
2135 
2136     /* disable endpoint interrupts, users can use NVIC to disable/enable the USB interrupt to improve the system
2137      * performance */
2138     OSA_ENTER_CRITICAL();
2139     /* Cancel the transfer and notify the up layer when the endpoint is busy. */
2140     if ((0U != epState->stateUnion.stateBitField.transferring)
2141 #if (defined(FSL_FEATURE_USBHSD_VERSION) && (FSL_FEATURE_USBHSD_VERSION >= 300U)) && \
2142     (!(defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) &&            \
2143        (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK)))
2144 #else
2145 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
2146         || (
2147 #if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
2148      (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
2149                (0U != lpc3511IpState->hsInterruptIssue) &&
2150 #endif
2151                (0U != (epState->stateUnion.stateBitField.epControlDefault &
2152                        ((USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK) >> USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT))) &&
2153                (USB_ENDPOINT_INTERRUPT == epState->stateUnion.stateBitField.endpointType) &&
2154                (0U != lpc3511IpState->controllerSpeed) &&
2155                (0U != (lpc3511IpState->epCommandStatusList[epState->stateUnion.stateBitField.consumerOdd +
2156                                                            (((uint32_t)endpointIndex) * 2U)] &
2157                        USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK)) &&
2158                (0U == (lpc3511IpState->registerBase->EPTOGGLE & ((uint32_t)(0x01UL << endpointIndex)))))
2159 #endif
2160 #endif
2161     )
2162     {
2163 #if (defined(FSL_FEATURE_USBHSD_VERSION) && (FSL_FEATURE_USBHSD_VERSION >= 300U)) && \
2164     (!(defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) &&            \
2165        (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK)))
2166 #else
2167 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
2168 #if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
2169      (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
2170         if (0U != lpc3511IpState->hsInterruptIssue)
2171         {
2172 #endif
2173             epState->stateUnion.stateBitField.epControlDefault &=
2174                 (~((USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK) >> USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT));
2175 #if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
2176      (FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
2177         }
2178 #endif
2179 #endif
2180 #endif
2181         if ((((lpc3511IpState->epCommandStatusList[(uint32_t)endpointIndex * 2U] &
2182                USB_LPC3511IP_ENDPOINT_ACTIVE_MASK) != 0U) &&
2183              ((lpc3511IpState->epCommandStatusList[(uint32_t)endpointIndex * 2U] &
2184                USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK) != 0U)) ||
2185             (((lpc3511IpState->epCommandStatusList[(uint32_t)endpointIndex * 2U + 1U] &
2186                USB_LPC3511IP_ENDPOINT_ACTIVE_MASK) != 0U) &&
2187              ((lpc3511IpState->epCommandStatusList[(uint32_t)endpointIndex * 2U + 1U] &
2188                USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK) != 0U)))
2189         {
2190             epState->stateUnion.stateBitField.epControlDefault |=
2191                 ((USB_LPC3511IP_ENDPOINT_TOGGLE_RESET_MASK) >> USB_LPC3511IP_ENDPOINT_CONFIGURE_BITS_SHIFT);
2192         }
2193 
2194         if ((lpc3511IpState->registerBase->DEVCMDSTAT & USB_LPC3511IP_DEVCMDSTAT_DCON_MASK) != 0U)
2195         {
2196             while (1U == 1U)
2197             {
2198                 if ((lpc3511IpState->epCommandStatusList[(uint32_t)endpointIndex * 2U +
2199                                                          ((lpc3511IpState->registerBase->EPINUSE &
2200                                                            (((uint32_t)0x00000001U << endpointIndex))) >>
2201                                                           endpointIndex)] &
2202                      USB_LPC3511IP_ENDPOINT_ACTIVE_MASK) != 0U)
2203                 {
2204                     /* cancel the transfer in the endpoint command/status */
2205                     lpc3511IpState->registerBase->EPSKIP |= ((uint32_t)0x00000001U << endpointIndex);
2206                     while (((lpc3511IpState->registerBase->EPSKIP & ((uint32_t)0x00000001U << endpointIndex)) != 0U) &&
2207                            ((lpc3511IpState->epCommandStatusList[(uint32_t)endpointIndex * 2U +
2208                                                                  ((lpc3511IpState->registerBase->EPINUSE &
2209                                                                    (((uint32_t)0x00000001U << endpointIndex))) >>
2210                                                                   endpointIndex)] &
2211                              USB_LPC3511IP_ENDPOINT_ACTIVE_MASK) != 0U))
2212                     {
2213                     }
2214                     if ((lpc3511IpState->registerBase->EPSKIP & ((uint32_t)0x00000001U << endpointIndex)) != 0U)
2215                     {
2216                         lpc3511IpState->registerBase->EPSKIP &= (~((uint32_t)0x00000001U << endpointIndex));
2217                     }
2218                 }
2219 
2220                 if (((lpc3511IpState->epCommandStatusList[endpointIndex * 2U] & USB_LPC3511IP_ENDPOINT_ACTIVE_MASK) !=
2221                      0U) ||
2222                     ((lpc3511IpState->epCommandStatusList[endpointIndex * 2U + 1U] &
2223                       USB_LPC3511IP_ENDPOINT_ACTIVE_MASK) != 0U))
2224                 {
2225                     if ((lpc3511IpState->registerBase->EPINUSE & (((uint32_t)0x00000001U << endpointIndex))) != 0U)
2226                     {
2227                         lpc3511IpState->registerBase->EPINUSE &= ~((uint32_t)0x00000001U << endpointIndex);
2228                     }
2229                     else
2230                     {
2231                         lpc3511IpState->registerBase->EPINUSE |= ((uint32_t)0x00000001U << endpointIndex);
2232                     }
2233                 }
2234                 else
2235                 {
2236                     break;
2237                 }
2238             }
2239         }
2240         else
2241         {
2242             /* Make sure the device is detached from host, host will not send any transaction to device.
2243              * Then the endpoint status entry can be modified directly by software.
2244              */
2245             lpc3511IpState->epCommandStatusList[endpointIndex * 2U]      = USB_LPC3511IP_ENDPOINT_DISABLE_MASK;
2246             lpc3511IpState->epCommandStatusList[endpointIndex * 2U + 1U] = USB_LPC3511IP_ENDPOINT_DISABLE_MASK;
2247         }
2248 
2249         epState->stateUnion.stateBitField.transferring = 0U;
2250         epState->stateUnion.stateBitField.producerOdd =
2251             ((lpc3511IpState->registerBase->EPINUSE & ((uint32_t)((uint32_t)0x00000001U << endpointIndex))) >>
2252              endpointIndex);
2253         epState->stateUnion.stateBitField.consumerOdd =
2254             ((lpc3511IpState->registerBase->EPINUSE & ((uint32_t)((uint32_t)0x00000001U << endpointIndex))) >>
2255              endpointIndex);
2256         epState->stateUnion.stateBitField.doubleBufferBusy = 0U;
2257         /* clear interrupt status, enable endpoint interrupt again */
2258         lpc3511IpState->registerBase->INTSTAT = ((uint32_t)0x00000001U << endpointIndex);
2259 
2260         /* users can use NVIC to disable/enable the USB interrupt to improve the system performance */
2261         OSA_EXIT_CRITICAL();
2262 
2263         message.length  = USB_CANCELLED_TRANSFER_LENGTH;
2264         message.buffer  = epState->transferBuffer;
2265         message.code    = ep;
2266         message.isSetup = 0U;
2267 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
2268         if (kStatus_USB_Success != USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message))
2269         {
2270             return kStatus_USB_Error;
2271         }
2272 #else
2273         (void)USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message);
2274 #endif
2275     }
2276     else
2277     {
2278         /* users can use NVIC to disable/enable the USB interrupt to improve the system performance */
2279         OSA_EXIT_CRITICAL();
2280     }
2281     return kStatus_USB_Success;
2282 }
2283 
2284 /*seperate this function from USB_DeviceLpc3511IpControl for misra17.2 recursive */
USB_DeviceLpc3511IpControlPreSetDeviceAddress(usb_device_controller_handle controllerHandle,void * param)2285 static usb_status_t USB_DeviceLpc3511IpControlPreSetDeviceAddress(usb_device_controller_handle controllerHandle,
2286                                                                   void *param)
2287 {
2288     usb_device_lpc3511ip_state_struct_t *lpc3511IpState = (usb_device_lpc3511ip_state_struct_t *)controllerHandle;
2289     usb_status_t error                                  = kStatus_USB_Error;
2290     uint32_t tmp32Value;
2291     uint8_t tmp8Value;
2292     if (NULL != param)
2293     {
2294         tmp8Value  = *((uint8_t *)param);
2295         tmp32Value = lpc3511IpState->registerBase->DEVCMDSTAT;
2296         tmp32Value &= ~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_DEV_ADDR_MASK);
2297         tmp32Value |= ((uint32_t)tmp8Value & USB_LPC3511IP_DEVCMDSTAT_DEV_ADDR_MASK);
2298         lpc3511IpState->registerBase->DEVCMDSTAT = tmp32Value;
2299         error                                    = kStatus_USB_Success;
2300     }
2301     return error;
2302 }
2303 
USB_DeviceLpc3511IpControl(usb_device_controller_handle controllerHandle,usb_device_control_type_t type,void * param)2304 usb_status_t USB_DeviceLpc3511IpControl(usb_device_controller_handle controllerHandle,
2305                                         usb_device_control_type_t type,
2306                                         void *param)
2307 {
2308     usb_device_lpc3511ip_state_struct_t *lpc3511IpState = (usb_device_lpc3511ip_state_struct_t *)controllerHandle;
2309     usb_status_t error                                  = kStatus_USB_Error;
2310     uint32_t tmp32Value;
2311     uint8_t tmp8Value;
2312 #if defined(USB_DEVICE_CONFIG_GET_SOF_COUNT) && (USB_DEVICE_CONFIG_GET_SOF_COUNT > 0U)
2313     uint32_t *tmp32Pointer;
2314 #endif
2315 
2316 #if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U))
2317     usb_device_struct_t *deviceHandle;
2318 #endif
2319     usb_device_lpc3511ip_endpoint_state_struct_t *epState;
2320 
2321     if (controllerHandle == NULL)
2322     {
2323         return kStatus_USB_InvalidHandle;
2324     }
2325 
2326 #if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U))
2327     deviceHandle = (usb_device_struct_t *)lpc3511IpState->deviceHandle;
2328 #endif
2329 
2330     switch (type)
2331     {
2332         case kUSB_DeviceControlRun:
2333             lpc3511IpState->registerBase->DEVCMDSTAT = (lpc3511IpState->registerBase->DEVCMDSTAT & ~USB_LPC3511IP_DEVCMDSTAT_W1C_MASK) | (USB_LPC3511IP_DEVCMDSTAT_DCON_MASK);
2334             lpc3511IpState->registerBase->DEVCMDSTAT &= ~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_FORCE_NEEDCLK_MASK);
2335             error = kStatus_USB_Success;
2336             break;
2337 
2338         case kUSB_DeviceControlStop:
2339             lpc3511IpState->registerBase->DEVCMDSTAT = (lpc3511IpState->registerBase->DEVCMDSTAT & ~USB_LPC3511IP_DEVCMDSTAT_W1C_MASK) | USB_LPC3511IP_DEVCMDSTAT_FORCE_NEEDCLK_MASK;
2340             lpc3511IpState->registerBase->DEVCMDSTAT &= ~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_DCON_MASK);
2341             error = kStatus_USB_Success;
2342             break;
2343 
2344         case kUSB_DeviceControlEndpointInit:
2345             if (NULL != param)
2346             {
2347                 error = USB_DeviceLpc3511IpEndpointInit(lpc3511IpState, (usb_device_endpoint_init_struct_t *)param);
2348             }
2349             break;
2350 
2351         case kUSB_DeviceControlEndpointDeinit:
2352             if (NULL != param)
2353             {
2354                 tmp8Value = *((uint8_t *)param);
2355                 error     = USB_DeviceLpc3511IpEndpointDeinit(lpc3511IpState, tmp8Value);
2356             }
2357             break;
2358 
2359         case kUSB_DeviceControlEndpointStall:
2360             if (NULL != param)
2361             {
2362                 tmp8Value = *((uint8_t *)param);
2363                 error     = USB_DeviceLpc3511IpEndpointStall(lpc3511IpState, tmp8Value);
2364             }
2365             break;
2366 
2367         case kUSB_DeviceControlEndpointUnstall:
2368             if (NULL != param)
2369             {
2370                 tmp8Value = *((uint8_t *)param);
2371                 error     = USB_DeviceLpc3511IpEndpointUnstall(lpc3511IpState, tmp8Value);
2372             }
2373             break;
2374 
2375         case kUSB_DeviceControlGetDeviceStatus:
2376             if (NULL != param)
2377             {
2378                 *((uint16_t *)param) =
2379                     (USB_DEVICE_CONFIG_SELF_POWER << (USB_REQUEST_STANDARD_GET_STATUS_DEVICE_SELF_POWERED_SHIFT))
2380 #if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U))
2381                     | ((uint16_t)(((uint32_t)deviceHandle->remotewakeup)
2382                                   << (USB_REQUEST_STANDARD_GET_STATUS_DEVICE_REMOTE_WARKUP_SHIFT)))
2383 #endif
2384                     ;
2385                 error = kStatus_USB_Success;
2386             }
2387             break;
2388 
2389         case kUSB_DeviceControlGetEndpointStatus:
2390             if (NULL != param)
2391             {
2392                 usb_device_endpoint_status_struct_t *endpointStatus = (usb_device_endpoint_status_struct_t *)param;
2393 
2394                 if ((((endpointStatus->endpointAddress) & USB_ENDPOINT_NUMBER_MASK)) <
2395                     (uint8_t)USB_DEVICE_IP3511_ENDPOINTS_NUM)
2396                 {
2397                     epState = USB_DeviceLpc3511IpGetEndpointStateStruct(
2398                         lpc3511IpState, USB_LPC3511IP_ENDPOINT_DES_INDEX(endpointStatus->endpointAddress));
2399 #if (defined(USB_DEVICE_CONFIG_ROOT2_TEST) && (USB_DEVICE_CONFIG_ROOT2_TEST > 0U))
2400                     if (0U == epState->stateUnion.stateBitField.isOpened)
2401                     {
2402                         error = kStatus_USB_InvalidRequest;
2403                     }
2404                     else
2405                     {
2406                         endpointStatus->endpointStatus = (uint16_t)((epState->stateUnion.stateBitField.stalled == 1U) ?
2407                                                                         kUSB_DeviceEndpointStateStalled :
2408                                                                         kUSB_DeviceEndpointStateIdle);
2409                         error                          = kStatus_USB_Success;
2410                     }
2411 #else
2412                     endpointStatus->endpointStatus =
2413                         (uint16_t)((epState->stateUnion.stateBitField.stalled == 1U) ? kUSB_DeviceEndpointStateStalled :
2414                                                                                        kUSB_DeviceEndpointStateIdle);
2415                     error = kStatus_USB_Success;
2416 #endif
2417                 }
2418 #if (defined(USB_DEVICE_CONFIG_ROOT2_TEST) && (USB_DEVICE_CONFIG_ROOT2_TEST > 0U))
2419                 else
2420                 {
2421                     error = kStatus_USB_InvalidRequest;
2422                 }
2423 #endif
2424             }
2425             break;
2426 
2427         case kUSB_DeviceControlPreSetDeviceAddress:
2428             error = USB_DeviceLpc3511IpControlPreSetDeviceAddress(controllerHandle, param);
2429             if (kStatus_USB_Success == error)
2430             {
2431                 /*no action, just for misra4.7*/
2432             }
2433             break;
2434 
2435         case kUSB_DeviceControlSetDeviceAddress:
2436             error = kStatus_USB_Success;
2437             break;
2438 
2439         case kUSB_DeviceControlGetSynchFrame:
2440 #if (defined(USB_DEVICE_CONFIG_ROOT2_TEST) && (USB_DEVICE_CONFIG_ROOT2_TEST > 0U))
2441             if (NULL != param)
2442             {
2443                 usb_device_endpoint_status_struct_t *endpointStatus = (usb_device_endpoint_status_struct_t *)param;
2444                 if ((((endpointStatus->endpointAddress) & USB_ENDPOINT_NUMBER_MASK)) <
2445                     (uint8_t)USB_DEVICE_IP3511_ENDPOINTS_NUM)
2446                 {
2447                     epState = USB_DeviceLpc3511IpGetEndpointStateStruct(
2448                         lpc3511IpState, USB_LPC3511IP_ENDPOINT_DES_INDEX(endpointStatus->endpointAddress));
2449                     /* In configured state (should stall in other states), device must STALL status stage of Synch Frame
2450                      * Request with Invalid wIndex */
2451                     if (0U == epState->stateUnion.stateBitField.isOpened)
2452                     {
2453                         error = kStatus_USB_InvalidRequest;
2454                     }
2455                     else
2456                     {
2457                         error = kStatus_USB_Success;
2458                     }
2459                 }
2460                 else
2461                 {
2462                     error = kStatus_USB_InvalidRequest;
2463                 }
2464             }
2465 #endif
2466             break;
2467 
2468 #if defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U)
2469         case kUSB_DeviceControlResume:
2470             /* todo: turn on USB clock and enable the USB clock source */
2471             lpc3511IpState->registerBase->DEVCMDSTAT = (lpc3511IpState->registerBase->DEVCMDSTAT & ~USB_LPC3511IP_DEVCMDSTAT_W1C_MASK) | USB_LPC3511IP_DEVCMDSTAT_FORCE_NEEDCLK_MASK;
2472             lpc3511IpState->registerBase->DEVCMDSTAT &= ~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_DSUS_MASK);
2473             while (0U != (lpc3511IpState->registerBase->DEVCMDSTAT & USB_LPC3511IP_DEVCMDSTAT_DSUS_MASK))
2474             {
2475             }
2476             /* the W1C bits */
2477             lpc3511IpState->registerBase->DEVCMDSTAT &=
2478                 ~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_FORCE_NEEDCLK_MASK | USB_LPC3511IP_DEVCMDSTAT_INTERRUPT_WC_MASK);
2479             error = kStatus_USB_Success;
2480             break;
2481 #if (defined(USB_DEVICE_CONFIG_LPM_L1) && (USB_DEVICE_CONFIG_LPM_L1 > 0U))
2482         case kUSB_DeviceControlSleepResume:
2483             /* todo: turn on USB clock and enable the USB clock source */
2484             lpc3511IpState->registerBase->DEVCMDSTAT = (lpc3511IpState->registerBase->DEVCMDSTAT & ~USB_LPC3511IP_DEVCMDSTAT_W1C_MASK) | USB_LPC3511IP_DEVCMDSTAT_FORCE_NEEDCLK_MASK;
2485             lpc3511IpState->registerBase->DEVCMDSTAT &= ~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_LPM_SUS_MASK);
2486             while (0U != (lpc3511IpState->registerBase->DEVCMDSTAT & USB_LPC3511IP_DEVCMDSTAT_LPM_SUS_MASK))
2487             {
2488                 __NOP();
2489             }
2490             /* the W1C bits */
2491             lpc3511IpState->registerBase->DEVCMDSTAT &=
2492                 ~(USB_LPC3511IP_DEVCMDSTAT_W1C_MASK | USB_LPC3511IP_DEVCMDSTAT_FORCE_NEEDCLK_MASK | USB_LPC3511IP_DEVCMDSTAT_INTERRUPT_WC_MASK);
2493             error = kStatus_USB_Success;
2494             break;
2495 #endif
2496         case kUSB_DeviceControlGetRemoteWakeUp:
2497             *((uint8_t *)param) =
2498                 (uint8_t)((lpc3511IpState->registerBase->DEVCMDSTAT & USB_LPC3511IP_DEVCMDSTAT_LPM_REWP_MASK) >>
2499                           USB_LPC3511IP_DEVCMDSTAT_LPM_REWP_SHIFT);
2500             break;
2501 #endif /* USB_DEVICE_CONFIG_REMOTE_WAKEUP */
2502 
2503         case kUSB_DeviceControlSetDefaultStatus:
2504             for (tmp32Value = 0U; tmp32Value < (uint32_t)USB_DEVICE_IP3511_ENDPOINTS_NUM; tmp32Value++)
2505             {
2506 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
2507                 if ((kStatus_USB_Success !=
2508                      USB_DeviceLpc3511IpEndpointDeinit(lpc3511IpState, (uint8_t)(tmp32Value | (USB_IN << 0x07U)))) ||
2509                     (USB_DeviceLpc3511IpEndpointDeinit(lpc3511IpState, (uint8_t)(tmp32Value | (USB_OUT << 0x07U)))))
2510                 {
2511                     return kStatus_USB_Error;
2512                 }
2513 #else
2514                 (void)USB_DeviceLpc3511IpEndpointDeinit(lpc3511IpState, (uint8_t)(tmp32Value | (USB_IN << 0x07U)));
2515                 (void)USB_DeviceLpc3511IpEndpointDeinit(lpc3511IpState, (uint8_t)(tmp32Value | (USB_OUT << 0x07U)));
2516 #endif
2517             }
2518             USB_DeviceLpc3511IpSetDefaultState(lpc3511IpState);
2519             error = kStatus_USB_Success;
2520             break;
2521 
2522         case kUSB_DeviceControlGetSpeed:
2523             if (NULL != param)
2524             {
2525                 *((uint8_t *)param) = lpc3511IpState->deviceSpeed;
2526                 error               = kStatus_USB_Success;
2527             }
2528             break;
2529         case kUSB_DeviceControlGetOtgStatus:
2530             break;
2531         case kUSB_DeviceControlSetOtgStatus:
2532             break;
2533 #if (defined(USB_DEVICE_CONFIG_USB20_TEST_MODE) && (USB_DEVICE_CONFIG_USB20_TEST_MODE > 0U))
2534         case kUSB_DeviceControlSetTestMode:
2535             if (NULL != param)
2536             {
2537                 tmp8Value = *((uint8_t *)param);
2538 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
2539                 lpc3511IpState->registerBase->DEVCMDSTAT =
2540                     (lpc3511IpState->registerBase->DEVCMDSTAT & ~USB_LPC3511IP_DEVCMDSTAT_W1C_MASK) |
2541                     ((uint32_t)(tmp8Value) << USBHSD_DEVCMDSTAT_PHY_TEST_MODE_SHIFT);
2542 #endif
2543                 error = kStatus_USB_Success;
2544             }
2545             break;
2546 #endif
2547 #if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U)) && \
2548     (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
2549         case kUSB_DeviceControlDcdEnable:
2550             if (kStatus_hsdcd_Success == USB_HSDCD_Control(lpc3511IpState->dcdHandle, kUSB_DeviceHSDcdEnable, NULL))
2551             {
2552                 error = kStatus_USB_Success;
2553             }
2554             break;
2555         case kUSB_DeviceControlDcdDisable:
2556             if (kStatus_hsdcd_Success == USB_HSDCD_Control(lpc3511IpState->dcdHandle, kUSB_DeviceHSDcdDisable, NULL))
2557             {
2558                 error = kStatus_USB_Success;
2559             }
2560             break;
2561         case kUSB_DeviceControlUpdateHwTick:
2562             /*udpate 1ms time tick*/
2563             error = kStatus_USB_Success;
2564             break;
2565 
2566 #endif
2567 #if defined(USB_DEVICE_CONFIG_GET_SOF_COUNT) && (USB_DEVICE_CONFIG_GET_SOF_COUNT > 0U)
2568         case kUSB_DeviceControlGetCurrentFrameCount:
2569             if (NULL != param)
2570             {
2571                 tmp32Pointer = (uint32_t *)param;
2572                 if (USB_SPEED_HIGH == lpc3511IpState->deviceSpeed) /* if high speed, change to use microframe count */
2573                 {
2574                     *tmp32Pointer =
2575                         ((uint32_t)(((lpc3511IpState->registerBase->INFO >> USB_LPC3511IP_INFO_FRAME_NR_SHIFT) &
2576                                      USB_LPC3511IP_INFO_FRAME_NR_MASK) &
2577                                     (USB_DEVICE_MAX_FRAME_COUNT))) *
2578                         8U;
2579                 }
2580                 else
2581                 {
2582                     *tmp32Pointer =
2583                         ((uint32_t)(((lpc3511IpState->registerBase->INFO >> USB_LPC3511IP_INFO_FRAME_NR_SHIFT) &
2584                                      USB_LPC3511IP_INFO_FRAME_NR_MASK) &
2585                                     (USB_DEVICE_MAX_FRAME_COUNT)));
2586                 }
2587                 error = kStatus_USB_Success;
2588             }
2589             break;
2590 #endif
2591         default:
2592             /*no action*/
2593             break;
2594     }
2595 
2596     return error;
2597 }
2598 
2599 #if (defined(USB_DEVICE_CONFIG_SOF_NOTIFICATIONS) && (USB_DEVICE_CONFIG_SOF_NOTIFICATIONS > 0U))
2600 /*!
2601  * @brief Handle Start of Frame (SOF) Interrupt.
2602  *
2603  * The function is used to notify the upper layer that an SOF has occurred.
2604  *
2605  * @param lpc3511IpState      Pointer of the controller state structure.
2606  *
2607  */
USB_DeviceLpc3511IpInterruptSOF(usb_device_lpc3511ip_state_struct_t * lpc3511IpState)2608 static void USB_DeviceLpc3511IpInterruptSOF(usb_device_lpc3511ip_state_struct_t *lpc3511IpState)
2609 {
2610     usb_device_callback_message_struct_t message;
2611 
2612     message.buffer  = (uint8_t *)NULL;
2613     message.code    = (uint8_t)kUSB_DeviceNotifySOF;
2614     message.length  = 0U;
2615     message.isSetup = 0U;
2616 
2617     /* Notify upper layer */
2618 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
2619     if (kStatus_USB_Success != USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message))
2620     {
2621 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
2622         usb_echo("notification error\n");
2623 #endif
2624     }
2625 #else
2626     (void)USB_DeviceNotificationTrigger(lpc3511IpState->deviceHandle, &message);
2627 #endif
2628 }
2629 #endif /* USB_DEVICE_CONFIG_SOF_NOTIFICATIONS */
2630 
USB_DeviceLpcIp3511IsrFunction(void * deviceHandle)2631 void USB_DeviceLpcIp3511IsrFunction(void *deviceHandle)
2632 {
2633     usb_device_struct_t *handle = (usb_device_struct_t *)deviceHandle;
2634     usb_device_lpc3511ip_state_struct_t *lpc3511IpState;
2635     uint32_t interruptStatus;
2636     uint32_t usbErrorCode;
2637     uint32_t devState;
2638 
2639     if (NULL == deviceHandle)
2640     {
2641         return;
2642     }
2643 
2644     lpc3511IpState = (usb_device_lpc3511ip_state_struct_t *)(handle->controllerHandle);
2645     /* get and clear interrupt status */
2646     interruptStatus                       = lpc3511IpState->registerBase->INTSTAT;
2647     lpc3511IpState->registerBase->INTSTAT = interruptStatus;
2648     interruptStatus &= lpc3511IpState->registerBase->INTEN;
2649 
2650     usbErrorCode = (lpc3511IpState->registerBase->INFO & USB_LPC3511IP_INFO_ERR_CODE_MASK);
2651 
2652     /* device status change interrupt */
2653     if (0U != (interruptStatus & USB_LPC3511IP_INTSTAT_DEV_INT_MASK))
2654     {
2655         /* get and clear device state change status */
2656         devState = lpc3511IpState->registerBase->DEVCMDSTAT;
2657         devState &= ~(USB_LPC3511IP_DEVCMDSTAT_SETUP_MASK);
2658         lpc3511IpState->registerBase->DEVCMDSTAT = (devState | USB_LPC3511IP_DEVCMDSTAT_INTERRUPT_WC_MASK);
2659 
2660         /* For HS: there is interrupt with DEV_INT=1, OTG_C=1 and ADPPROBE=1 when vbus rising.
2661          * For FS: there is no interrupt when vbus rising. The only way is: check the VBUS_DEBOUNCED in the DRES_C
2662          * interrupt with DCON set.
2663          */
2664 #if (defined(USB_DEVICE_CONFIG_DETACH_ENABLE) && (USB_DEVICE_CONFIG_DETACH_ENABLE))
2665         if ((0U == lpc3511IpState->deviceState) &&
2666             ((0U != (devState & USB_LPC3511IP_DEVCMDSTAT_VBUS_DEBOUNCED_MASK))
2667 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
2668              || ((0U != (lpc3511IpState->registerBase->LPM & USB_LPC3511IP_USB_LPM_ADPPROBE_MASK)) &&
2669                  (1U == lpc3511IpState->controllerSpeed))
2670 #endif
2671                  ))
2672         {
2673 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
2674 #if ((defined(USB_DEVICE_IP3511HS_LPM_ADPPROBE_ATTACH_DEBOUNCE_COUNT)) && \
2675      (USB_DEVICE_IP3511HS_LPM_ADPPROBE_ATTACH_DEBOUNCE_COUNT > 0U))
2676             /* add one little debounce for HS's attach detection because ADPPROBE is undebounced value */
2677             uint32_t debounceCount = USB_DEVICE_IP3511HS_LPM_ADPPROBE_ATTACH_DEBOUNCE_COUNT;
2678             if (1U == lpc3511IpState->controllerSpeed)
2679             {
2680                 while ((0U != debounceCount) && (0U == (devState & USB_LPC3511IP_DEVCMDSTAT_VBUS_DEBOUNCED_MASK)))
2681                 {
2682                     if (0U == (lpc3511IpState->registerBase->LPM & USB_LPC3511IP_USB_LPM_ADPPROBE_MASK))
2683                     {
2684                         break;
2685                     }
2686                     debounceCount--;
2687                 }
2688             }
2689 
2690             if ((debounceCount == 0U) || (0U != (devState & USB_LPC3511IP_DEVCMDSTAT_VBUS_DEBOUNCED_MASK)))
2691 #endif
2692 #endif
2693             {
2694                 lpc3511IpState->deviceState = 1U;
2695                 USB_DeviceLpc3511IpInterruptAttach(lpc3511IpState);
2696 #if (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U)) && \
2697     (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U))
2698 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
2699                 if (kStatus_USB_Success != USB_HSDCD_Control(lpc3511IpState->dcdHandle, kUSB_DeviceHSDcdRun, NULL))
2700                 {
2701 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
2702                     usb_echo("notification error\n");
2703 #endif
2704                 }
2705 #else
2706                 (void)USB_HSDCD_Control(lpc3511IpState->dcdHandle, kUSB_DeviceHSDcdRun, NULL);
2707 #endif
2708 #endif
2709             }
2710         }
2711         /* For HS: there is interrupt with DEV_INT=1, OTG_C=1 and ADPPROBE=0 when vbus falling.
2712          * For HS and FS: there is interrupt when vbus falling if DCON is set.
2713          */
2714         else if ((1U == lpc3511IpState->deviceState) &&
2715                  (((0U != (lpc3511IpState->registerBase->DEVCMDSTAT & USB_LPC3511IP_DEVCMDSTAT_DCON_MASK)) &&
2716                    (0U == (devState & USB_LPC3511IP_DEVCMDSTAT_VBUS_DEBOUNCED_MASK)))
2717 #if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
2718                   || ((0U == (lpc3511IpState->registerBase->DEVCMDSTAT & USB_LPC3511IP_DEVCMDSTAT_DCON_MASK)) &&
2719                       (0U == (lpc3511IpState->registerBase->LPM & USB_LPC3511IP_USB_LPM_ADPPROBE_MASK)) &&
2720                       (1U == lpc3511IpState->controllerSpeed))
2721 #endif
2722                       ))
2723         {
2724 #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
2725 #if ((defined FSL_FEATURE_USBHSD_HAS_EXIT_HS_ISSUE) && (FSL_FEATURE_USBHSD_HAS_EXIT_HS_ISSUE > 0U))
2726 #if ((defined USB_DEVICE_IP3511HS_FORCE_EXIT_HS_MODE_ENABLE) && (USB_DEVICE_IP3511HS_FORCE_EXIT_HS_MODE_ENABLE > 0U))
2727             uint32_t delay = 100000U;
2728 #endif
2729 #endif
2730 #endif
2731             lpc3511IpState->deviceState = 0U;
2732 #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
2733 #if ((defined FSL_FEATURE_USBHSD_HAS_EXIT_HS_ISSUE) && (FSL_FEATURE_USBHSD_HAS_EXIT_HS_ISSUE > 0U))
2734 #if ((defined USB_DEVICE_IP3511HS_FORCE_EXIT_HS_MODE_ENABLE) && (USB_DEVICE_IP3511HS_FORCE_EXIT_HS_MODE_ENABLE > 0U))
2735             /* wait at least 125us to let the host to detect the detach */
2736             USB_PhyDeviceForceEnterFSMode(lpc3511IpState->controllerId, 1);
2737             while (delay--)
2738             {
2739                 __NOP();
2740             }
2741             USB_PhyDeviceForceEnterFSMode(lpc3511IpState->controllerId, 0);
2742 #endif
2743 #endif
2744 #endif
2745             USB_DeviceLpc3511IpInterruptDetach(lpc3511IpState);
2746         }
2747         else
2748         {
2749             /*no action*/
2750         }
2751 #endif
2752 
2753         /* reset change */
2754         if (0U != (devState & USB_LPC3511IP_DEVCMDSTAT_DRES_C_MASK))
2755         {
2756             USB_DeviceLpc3511IpInterruptReset(lpc3511IpState);
2757         }
2758 
2759 /* Suspend/Resume */
2760 #if ((defined(USB_DEVICE_CONFIG_LOW_POWER_MODE)) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
2761         if (0U != (devState & USB_LPC3511IP_DEVCMDSTAT_DSUS_C_MASK))
2762         {
2763             if (0U != (lpc3511IpState->registerBase->DEVCMDSTAT & USB_LPC3511IP_DEVCMDSTAT_DSUS_MASK))
2764             {
2765 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
2766                 if (kStatus_USB_Success != USB_DeviceLpc3511IpInterruptSuspend(lpc3511IpState))
2767                 {
2768 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
2769                     usb_echo("notification error\n");
2770 #endif
2771                 }
2772 #else
2773                 (void)USB_DeviceLpc3511IpInterruptSuspend(lpc3511IpState);
2774 #endif
2775             }
2776 #if (defined(USB_DEVICE_CONFIG_LPM_L1) && (USB_DEVICE_CONFIG_LPM_L1 > 0U))
2777             else if (0U != (lpc3511IpState->registerBase->DEVCMDSTAT & USB_LPC3511IP_DEVCMDSTAT_LPM_SUS_MASK))
2778             {
2779 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
2780                 if (kStatus_USB_Success != (void)USB_DeviceLpc3511IpInterruptLPMSleep(lpc3511IpState))
2781                 {
2782 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
2783                     usb_echo("notification error\n");
2784 #endif
2785                 }
2786 #else
2787                 (void)USB_DeviceLpc3511IpInterruptLPMSleep(lpc3511IpState);
2788 #endif
2789             }
2790 #endif
2791             else
2792             {
2793 #if (defined(USB_DEVICE_CONFIG_RETURN_VALUE_CHECK) && (USB_DEVICE_CONFIG_RETURN_VALUE_CHECK > 0U))
2794                 if (kStatus_USB_Success != USB_DeviceLpc3511IpInterruptResume(lpc3511IpState))
2795                 {
2796 #if (defined(DEVICE_ECHO) && (DEVICE_ECHO > 0U))
2797                     usb_echo("notification error\n");
2798 #endif
2799                 }
2800 #else
2801                 (void)USB_DeviceLpc3511IpInterruptResume(lpc3511IpState);
2802 #endif
2803             }
2804         }
2805 
2806 #endif
2807 
2808 #if 0U /* some soc don't support this bit, need check according to the feature macro */
2809         /* OTG Status change */
2810         if (lpc3511IpState->registerBase->DEVCMDSTAT & USB_LPC3511IP_DEVCMDSTAT_OTG_C_MASK)
2811         {
2812         }
2813 #endif
2814     }
2815 
2816     /* endpoint transfers interrupt */
2817     if (0U != (interruptStatus & USB_LPC3511IP_MAX_PHY_ENDPOINT_MASK))
2818     {
2819         devState = 0;                        /* devState means index here */
2820         if (0U != (interruptStatus & 0x01U)) /* control OUT */
2821         {
2822             if (0U != (lpc3511IpState->registerBase->DEVCMDSTAT & USB_LPC3511IP_DEVCMDSTAT_SETUP_MASK))
2823             {
2824                 devState = 2U;
2825                 if ((lpc3511IpState->endpointState[0].stateUnion.stateBitField.stalled == 1U) ||
2826                     (lpc3511IpState->endpointState[1].stateUnion.stateBitField.stalled == 1U))
2827                 {
2828                     USB_LPC3511IP_ENDPOINT_SET_ENDPOINT_AND(
2829                         lpc3511IpState, 0, 0,
2830                         (~(USB_LPC3511IP_ENDPOINT_STALL_MASK | USB_LPC3511IP_ENDPOINT_ACTIVE_MASK)));
2831                     USB_LPC3511IP_ENDPOINT_SET_ENDPOINT_AND(
2832                         lpc3511IpState, 1, 0,
2833                         (~(USB_LPC3511IP_ENDPOINT_STALL_MASK | USB_LPC3511IP_ENDPOINT_ACTIVE_MASK)));
2834                     lpc3511IpState->endpointState[0].stateUnion.stateBitField.stalled = 0U;
2835                     lpc3511IpState->endpointState[1].stateUnion.stateBitField.stalled = 0U;
2836                 }
2837 
2838                 /* todo: setup token interrupt */
2839                 USB_DeviceLpc3511IpInterruptToken(lpc3511IpState, 0U, 1, usbErrorCode);
2840             }
2841         }
2842 
2843         for (; devState < ((uint32_t)USB_DEVICE_IP3511_ENDPOINTS_NUM * 2U); ++devState)
2844         {
2845             /* check the endpoint interrupt */
2846             if (0U != (interruptStatus & (0x01UL << devState)))
2847             {
2848                 USB_DeviceLpc3511IpInterruptToken(lpc3511IpState, (uint8_t)devState, 0U, usbErrorCode);
2849             }
2850         }
2851     }
2852 
2853 #if (defined(USB_DEVICE_CONFIG_SOF_NOTIFICATIONS) && (USB_DEVICE_CONFIG_SOF_NOTIFICATIONS > 0U))
2854     if (interruptStatus & USB_LPC3511IP_INTSTAT_FRAME_INT_MASK)
2855     {
2856         USB_DeviceLpc3511IpInterruptSOF(lpc3511IpState);
2857     }
2858 #endif
2859 }
2860 
2861 #endif
2862