summaryrefslogtreecommitdiff
path: root/bsp/nuvoton/libraries/nuc980/Driver/Source/nu_usbd.c
blob: 29c7946ce5b7e3f244ef5965a5818ab6a7cb6f0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/**************************************************************************//**
 * @file     usbd.c
 * @version  V1.00
 * $Revision: 2 $
 * $Date: 18/08/05 2:58p $
 * @brief    NuMicro ARM9 USBD driver source file
 *
 * @note
 * SPDX-License-Identifier: Apache-2.0
 * Copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include "nuc980.h"
#include "nu_usbd.h"

/** @addtogroup Standard_Driver Standard Driver
  @{
*/

/** @addtogroup USBD_Driver USBD Driver
  @{
*/

/** @addtogroup USBD_EXPORTED_CONSTANTS USBD Exported Constants
  @{
*/
/*--------------------------------------------------------------------------*/
/// @cond HIDDEN_SYMBOLS

/*!< Global variables for Control Pipe */
S_USBD_CMD_T gUsbCmd;
S_USBD_INFO_T *g_usbd_sInfo;

VENDOR_REQ g_usbd_pfnVendorRequest = NULL;
CLASS_REQ g_usbd_pfnClassRequest = NULL;
SET_INTERFACE_REQ g_usbd_pfnSetInterface = NULL;
uint32_t g_u32EpStallLock = 0;       /*!< Bit map flag to lock specified EP when SET_FEATURE */
uint8_t volatile g_usbd_RemoteWakeupEn = 0ul;

static uint8_t *g_usbd_CtrlInPointer = 0;
static uint32_t g_usbd_CtrlMaxPktSize = 64;
static uint8_t g_usbd_UsbConfig = 0;
static uint8_t g_usbd_UsbAltInterface = 0;
static uint8_t g_usbd_EnableTestMode = 0;
static uint8_t g_usbd_TestSelector = 0;

#ifdef __ICCARM__
    #pragma data_alignment=4
    static uint8_t g_usbd_buf[12];
#else
    static uint8_t g_usbd_buf[12] __attribute__((aligned(4)));
#endif


uint8_t volatile g_usbd_Configured = 0;
uint8_t g_usbd_CtrlZero = 0;
uint8_t g_usbd_UsbAddr = 0;
uint8_t g_usbd_ShortPacket = 0;
uint32_t volatile g_usbd_DmaDone = 0;
uint32_t g_usbd_CtrlInSize = 0;
/// @endcond HIDDEN_SYMBOLS

/**
 * @brief       USBD Initial
 *
 * @param[in]   param               Descriptor
 * @param[in]   pfnClassReq         Class Request Callback Function
 * @param[in]   pfnSetInterface     SetInterface Request Callback Function
 *
 * @return      None
 *
 * @details     This function is used to initial USBD.
 */
void USBD_Open(S_USBD_INFO_T *param, CLASS_REQ pfnClassReq, SET_INTERFACE_REQ pfnSetInterface)
{
    int volatile i;
    /* Select Vbus detect pin -> GPE11 */
    outpw(REG_SYS_GPE_MFPH, (inpw(REG_SYS_GPE_MFPH) & ~0xf000) | 0x1000);
    /* Enable USB device clock */
    outpw(REG_CLK_HCLKEN, inpw(REG_CLK_HCLKEN) | 0x80000);
    outpw(REG_SYS_AHBIPRST, inpw(REG_SYS_AHBIPRST) | 0x80000);
    outpw(REG_SYS_AHBIPRST, inpw(REG_SYS_AHBIPRST) & ~0x80000);
    for (i = 0; i < 1000; i++);

    g_usbd_sInfo = param;
    g_usbd_pfnClassRequest = pfnClassReq;
    g_usbd_pfnSetInterface = pfnSetInterface;

    /* get EP0 maximum packet size */
    g_usbd_CtrlMaxPktSize = g_usbd_sInfo->gu8DevDesc[7];

    /* Initial USB engine */
    /* Enable PHY */
    USBD_ENABLE_PHY();
    /* wait PHY clock ready */
    while (1)
    {
        USBD->EP[EPA].EPMPS = 0x20;
        if (USBD->EP[EPA].EPMPS == 0x20)
            break;
    }
    /* Force SE0, and then clear it to connect*/
    USBD_SET_SE0();
}

/**
 * @brief       USBD Start
 *
 * @return      None
 *
 * @details     This function is used to start transfer
 */
void USBD_Start(void)
{
    USBD_CLR_SE0();
}

/**
 * @brief       Process Setup Packet
 *
 * @return      None
 *
 * @details     This function is used to process Setup packet.
 */
void USBD_ProcessSetupPacket(void)
{
    // Setup packet process
    gUsbCmd.bmRequestType = (uint8_t)(USBD->SETUP1_0 & 0xff);
    gUsbCmd.bRequest = (int8_t)(USBD->SETUP1_0 >> 8) & 0xff;
    gUsbCmd.wValue = (uint16_t)USBD->SETUP3_2;
    gUsbCmd.wIndex = (uint16_t)USBD->SETUP5_4;
    gUsbCmd.wLength = (uint16_t)USBD->SETUP7_6;

    /* USB device request in setup packet: offset 0, D[6..5]: 0=Standard, 1=Class, 2=Vendor, 3=Reserved */
    switch (gUsbCmd.bmRequestType & 0x60)
    {
    case REQ_STANDARD:   // Standard
    {
        USBD_StandardRequest();
        break;
    }
    case REQ_CLASS:   // Class
    {
        if (g_usbd_pfnClassRequest != NULL)
        {
            g_usbd_pfnClassRequest();
        }
        break;
    }
    case REQ_VENDOR:   // Vendor
    {
        if (g_usbd_pfnVendorRequest != NULL)
        {
            g_usbd_pfnVendorRequest();
        }
        break;
    }
    default:   // reserved
    {
        /* Setup error, stall the device */
        USBD_SET_CEP_STATE(USBD_CEPCTL_STALLEN_Msk);
        break;
    }
    }
}

/**
 * @brief       Get Descriptor request
 *
 * @return      None
 *
 * @details     This function is used to process GetDescriptor request.
 */
int USBD_GetDescriptor(void)
{
    uint32_t u32Len;
    int val = 0;

    u32Len = gUsbCmd.wLength;
    g_usbd_CtrlZero = 0;
    USBD->CEPCTL |= USBD_CEPCTL_FLUSH_Msk;

    switch ((gUsbCmd.wValue & 0xff00) >> 8)
    {
    /* Get Device Descriptor */
    case DESC_DEVICE:
    {
        u32Len = Minimum(u32Len, LEN_DEVICE);
        USBD_PrepareCtrlIn((uint8_t *)g_usbd_sInfo->gu8DevDesc, u32Len);
        break;
    }
    /* Get Configuration Descriptor */
    case DESC_CONFIG:
    {
        uint32_t u32TotalLen;
        if ((USBD->OPER & 0x04ul) == 0x04ul)
        {
            u32TotalLen = g_usbd_sInfo->gu8ConfigDesc[3];
            u32TotalLen = g_usbd_sInfo->gu8ConfigDesc[2] + (u32TotalLen << 8);

            if (u32Len > u32TotalLen)
            {
                u32Len = u32TotalLen;
                if ((u32Len % g_usbd_CtrlMaxPktSize) == 0)
                {
                    g_usbd_CtrlZero = 1;
                }
            }
            USBD_PrepareCtrlIn((uint8_t *)g_usbd_sInfo->gu8ConfigDesc, u32Len);
        }
        else
        {
            u32TotalLen = g_usbd_sInfo->gu8FullConfigDesc[3];
            u32TotalLen = g_usbd_sInfo->gu8FullConfigDesc[2] + (u32TotalLen << 8);

            if (u32Len > u32TotalLen)
            {
                u32Len = u32TotalLen;
                if ((u32Len % g_usbd_CtrlMaxPktSize) == 0ul)
                {
                    g_usbd_CtrlZero = (uint8_t)1ul;
                }
            }
            USBD_PrepareCtrlIn((uint8_t *)g_usbd_sInfo->gu8FullConfigDesc, u32Len);
        }
        break;
    }
    /* Get Qualifier Descriptor */
    case DESC_QUALIFIER:
    {
        u32Len = Minimum(u32Len, LEN_QUALIFIER);
        USBD_PrepareCtrlIn((uint8_t *)g_usbd_sInfo->gu8QualDesc, u32Len);
        break;
    }
    // Get Other Speed Descriptor - Full speed
    case DESC_OTHERSPEED:
    {
        uint32_t u32TotalLen;
        if ((USBD->OPER & 0x04ul) == 0x04ul)
        {
            u32TotalLen = g_usbd_sInfo->gu8HSOtherConfigDesc[3];
            u32TotalLen = g_usbd_sInfo->gu8HSOtherConfigDesc[2] + (u32TotalLen << 8);

            if (u32Len > u32TotalLen)
            {
                u32Len = u32TotalLen;
                if ((u32Len % g_usbd_CtrlMaxPktSize) == 0)
                {
                    g_usbd_CtrlZero = 1;
                }
            }
            USBD_PrepareCtrlIn((uint8_t *)g_usbd_sInfo->gu8HSOtherConfigDesc, u32Len);
        }
        else
        {
            u32TotalLen = g_usbd_sInfo->gu8FSOtherConfigDesc[3];
            u32TotalLen = g_usbd_sInfo->gu8FSOtherConfigDesc[2] + (u32TotalLen << 8);

            if (u32Len > u32TotalLen)
            {
                u32Len = u32TotalLen;
                if ((u32Len % g_usbd_CtrlMaxPktSize) == 0ul)
                {
                    g_usbd_CtrlZero = (uint8_t)1ul;
                }
            }
            USBD_PrepareCtrlIn((uint8_t *)g_usbd_sInfo->gu8FSOtherConfigDesc, u32Len);
        }
        break;
    }
    /* Get HID Descriptor */
    case DESC_HID:
    {
        u32Len = Minimum(u32Len, LEN_HID);
        USBD_MemCopy(g_usbd_buf, (uint8_t *)&g_usbd_sInfo->gu8ConfigDesc[LEN_CONFIG + LEN_INTERFACE], u32Len);
        USBD_PrepareCtrlIn(g_usbd_buf, u32Len);
        break;
    }
    // Get Report Descriptor
    case DESC_HID_RPT:
    {
        if ((u32Len % g_usbd_CtrlMaxPktSize) == 0)
            g_usbd_CtrlZero = 1;

        u32Len = Minimum(u32Len, g_usbd_sInfo->gu32HidReportSize[gUsbCmd.wIndex & 0xff]);
        USBD_PrepareCtrlIn((uint8_t *)g_usbd_sInfo->gu8HidReportDesc[gUsbCmd.wIndex & 0xff], u32Len);
        break;
    }
    /* Get String Descriptor */
    case DESC_STRING:
    {
        if ((gUsbCmd.wValue & 0xfful) < 8ul)
        {
            if (u32Len > g_usbd_sInfo->gu8StringDesc[gUsbCmd.wValue & 0xfful][0])
            {
                u32Len = g_usbd_sInfo->gu8StringDesc[gUsbCmd.wValue & 0xfful][0];
                if ((u32Len % g_usbd_CtrlMaxPktSize) == 0)
                {
                    g_usbd_CtrlZero = 1;
                }
            }
            USBD_PrepareCtrlIn((uint8_t *)g_usbd_sInfo->gu8StringDesc[gUsbCmd.wValue & 0xff], u32Len);
        }
        else
        {
            USBD_SET_CEP_STATE(USBD_CEPCTL_STALLEN_Msk);
            val = 1;
        }
        break;
    }
    default:
        /* Not support. Reply STALL. */
        USBD_SET_CEP_STATE(USBD_CEPCTL_STALLEN_Msk);
        val = 1;
        break;
    }
    return val;
}


/**
 * @brief       Process USB standard request
 *
 * @return      None
 *
 * @details     This function is used to process USB Standard Request.
 */
void USBD_StandardRequest(void)
{
    /* clear global variables for new request */
    g_usbd_CtrlInPointer = 0;
    g_usbd_CtrlInSize = 0;

    if (gUsbCmd.bmRequestType & 0x80)   /* request data transfer direction */
    {
        // Device to host
        switch (gUsbCmd.bRequest)
        {
        case GET_CONFIGURATION:
        {
            // Return current configuration setting
            USBD_PrepareCtrlIn((uint8_t *)&g_usbd_UsbConfig, 1);

            USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_INTKIF_Msk);
            USBD_ENABLE_CEP_INT(USBD_CEPINTEN_INTKIEN_Msk);
            break;
        }
        case GET_DESCRIPTOR:
        {
            if (!USBD_GetDescriptor())
            {
                USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_INTKIF_Msk);
                USBD_ENABLE_CEP_INT(USBD_CEPINTEN_INTKIEN_Msk);
            }
            break;
        }
        case GET_INTERFACE:
        {
            // Return current interface setting
            USBD_PrepareCtrlIn((uint8_t *)&g_usbd_UsbAltInterface, 1);

            USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_INTKIF_Msk);
            USBD_ENABLE_CEP_INT(USBD_CEPINTEN_INTKIEN_Msk);
            break;
        }
        case GET_STATUS:
        {
            /* Device */
            if (gUsbCmd.bmRequestType == 0x80)
            {
                g_usbd_buf[0] = (uint8_t)0ul; /* bus-Powered */
                if ((g_usbd_sInfo->gu8ConfigDesc[7] & 0x40ul) == 0x40ul)
                {
                    g_usbd_buf[0] = (uint8_t)1ul; /* Self-Powered */
                }

                if ((g_usbd_sInfo->gu8ConfigDesc[7] & 0x20ul) == 0x20ul)
                    g_usbd_buf[0] |= (uint8_t)(g_usbd_RemoteWakeupEn << 1); // Remote wake up
            }
            // Interface
            else if (gUsbCmd.bmRequestType == 0x81)
                g_usbd_buf[0] = 0;
            // Endpoint
            else if (gUsbCmd.bmRequestType == 0x82)
            {
                uint8_t ep = gUsbCmd.wIndex & 0xF;
                g_usbd_buf[0] = USBD_GetStall(ep) ? 1 : 0;
            }
            g_usbd_buf[1] = 0;
            USBD_PrepareCtrlIn(g_usbd_buf, 2);
            USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_INTKIF_Msk);
            USBD_ENABLE_CEP_INT(USBD_CEPINTEN_INTKIEN_Msk);
            break;
        }
        default:
        {
            /* Setup error, stall the device */
            USBD_SET_CEP_STATE(USBD_CEPCTL_STALLEN_Msk);
            break;
        }
        }
    }
    else
    {
        // Host to device
        switch (gUsbCmd.bRequest)
        {
        case CLEAR_FEATURE:
        {
            if ((gUsbCmd.wValue & 0xff) == FEATURE_ENDPOINT_HALT)
            {

                int32_t epNum, i;

                /* EP number stall is not allow to be clear in MSC class "Error Recovery Test".
                   a flag: g_u32EpStallLock is added to support it */
                epNum = gUsbCmd.wIndex & 0xF;
                for (i = 0; i < USBD_MAX_EP; i++)
                {
                    if ((((USBD->EP[i].EPCFG & 0xf0) >> 4) == epNum) && ((g_u32EpStallLock & (1 << i)) == 0))
                    {
                        USBD->EP[i].EPRSPCTL = (USBD->EP[i].EPRSPCTL & 0xef) | USB_EP_RSPCTL_TOGGLE;
                    }
                }
            }
            /* Status stage */
            USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_STSDONEIF_Msk);
            USBD_SET_CEP_STATE(USB_CEPCTL_NAKCLR);
            USBD_ENABLE_CEP_INT(USBD_CEPINTEN_STSDONEIEN_Msk);
            break;
        }
        case SET_ADDRESS:
        {
            g_usbd_UsbAddr = (uint8_t)gUsbCmd.wValue;

            // DATA IN for end of setup
            /* Status Stage */
            USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_STSDONEIF_Msk);
            USBD_SET_CEP_STATE(USB_CEPCTL_NAKCLR);
            USBD_ENABLE_CEP_INT(USBD_CEPINTEN_STSDONEIEN_Msk);
            break;
        }
        case SET_CONFIGURATION:
        {
            g_usbd_UsbConfig = (uint8_t)gUsbCmd.wValue;
            g_usbd_Configured = 1;
            // DATA IN for end of setup
            /* Status stage */
            USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_STSDONEIF_Msk);
            USBD_SET_CEP_STATE(USB_CEPCTL_NAKCLR);
            USBD_ENABLE_CEP_INT(USBD_CEPINTEN_STSDONEIEN_Msk);
            break;
        }
        case SET_FEATURE:
        {
            if ((gUsbCmd.wValue & 0x3) == 2)    /* TEST_MODE*/
            {
                g_usbd_EnableTestMode = 1;
                g_usbd_TestSelector = gUsbCmd.wIndex >> 8;
            }
            /* Status stage */
            USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_STSDONEIF_Msk);
            USBD_SET_CEP_STATE(USB_CEPCTL_NAKCLR);
            USBD_ENABLE_CEP_INT(USBD_CEPINTEN_STSDONEIEN_Msk);
            break;
        }
        case SET_INTERFACE:
        {
            g_usbd_UsbAltInterface = (uint8_t)gUsbCmd.wValue;
            if (g_usbd_pfnSetInterface != NULL)
                g_usbd_pfnSetInterface(g_usbd_UsbAltInterface);
            /* Status stage */
            USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_STSDONEIF_Msk);
            USBD_SET_CEP_STATE(USB_CEPCTL_NAKCLR);
            USBD_ENABLE_CEP_INT(USBD_CEPINTEN_STSDONEIEN_Msk);
            break;
        }
        default:
        {
            /* Setup error, stall the device */
            USBD_SET_CEP_STATE(USBD_CEPCTL_STALLEN_Msk);
            break;
        }
        }
    }
}

#define TEST_J                  0x01    /*!< TEST J  \hideinitializer */
#define TEST_K                  0x02    /*!< TEST K  \hideinitializer */
#define TEST_SE0_NAK            0x03    /*!< TEST SE0  \hideinitializer */
#define TEST_PACKET             0x04    /*!< TEST Packet  \hideinitializer */
#define TEST_FORCE_ENABLE       0x05    /*!< TEST Force enable  \hideinitializer */


/**
 * @brief       Update Device State
 *
 * @return      None
 *
 * @details     This function is used to update Device state when Setup packet complete
 */
void USBD_UpdateDeviceState(void)
{
    switch (gUsbCmd.bRequest)
    {
    case SET_ADDRESS:
    {
        USBD_SET_ADDR(g_usbd_UsbAddr);
        break;
    }
    case SET_CONFIGURATION:
    {
        if (g_usbd_UsbConfig == 0)
        {
            int volatile i;
            /* Reset PID DATA0 */
            for (i = 0; i < USBD_MAX_EP; i++)
            {
                if (USBD->EP[i].EPCFG & 0x1)
                {
                    USBD->EP[i].EPRSPCTL = USB_EP_RSPCTL_TOGGLE;
                }
            }
        }
        break;
    }
    case SET_FEATURE:
    {
        if (gUsbCmd.wValue == FEATURE_ENDPOINT_HALT)
            USBD_SetStall(gUsbCmd.wIndex & 0xF);
        else if (g_usbd_EnableTestMode)
        {
            g_usbd_EnableTestMode = 0;
            if (g_usbd_TestSelector == TEST_J)
                USBD->TEST = TEST_J;
            else if (g_usbd_TestSelector == TEST_K)
                USBD->TEST = TEST_K;
            else if (g_usbd_TestSelector == TEST_SE0_NAK)
                USBD->TEST = TEST_SE0_NAK;
            else if (g_usbd_TestSelector == TEST_PACKET)
                USBD->TEST = TEST_PACKET;
            else if (g_usbd_TestSelector == TEST_FORCE_ENABLE)
                USBD->TEST = TEST_FORCE_ENABLE;
        }
        break;
    }
    case CLEAR_FEATURE:
    {
        if (gUsbCmd.wValue == FEATURE_ENDPOINT_HALT)
            USBD_ClearStall(gUsbCmd.wIndex & 0xF);
        break;
    }
    default:
        ;
    }
}


/**
 * @brief       Prepare Control IN transaction
 *
 * @param[in]   pu8Buf      Control IN data pointer
 * @param[in]   u32Size     IN transfer size
 *
 * @return      None
 *
 * @details     This function is used to prepare Control IN transfer
 */
void USBD_PrepareCtrlIn(uint8_t *pu8Buf, uint32_t u32Size)
{
    g_usbd_CtrlInPointer = pu8Buf;
    g_usbd_CtrlInSize = u32Size;
}



/**
 * @brief       Start Control IN transfer
 *
 * @return      None
 *
 * @details     This function is used to start Control IN
 */
void USBD_CtrlIn(void)
{
    int volatile i;
    uint32_t volatile count;

    // Process remained data
    if (g_usbd_CtrlInSize >= g_usbd_CtrlMaxPktSize)
    {
        // Data size > MXPLD
        for (i = 0; i < (g_usbd_CtrlMaxPktSize >> 2); i++, g_usbd_CtrlInPointer += 4)
            USBD->cep.CEPDAT = *(uint32_t *)g_usbd_CtrlInPointer;
        USBD_START_CEP_IN(g_usbd_CtrlMaxPktSize);
        g_usbd_CtrlInSize -= g_usbd_CtrlMaxPktSize;
    }
    else
    {
        // Data size <= MXPLD
        for (i = 0; i < (g_usbd_CtrlInSize >> 2); i++, g_usbd_CtrlInPointer += 4)
            USBD->cep.CEPDAT = *(uint32_t *)g_usbd_CtrlInPointer;

        count = g_usbd_CtrlInSize % 4;
        for (i = 0; i < count; i++)
            USBD->cep.CEPDAT_BYTE = *(uint8_t *)(g_usbd_CtrlInPointer + i);

        USBD_START_CEP_IN(g_usbd_CtrlInSize);
        g_usbd_CtrlInPointer = 0;
        g_usbd_CtrlInSize = 0;
    }
}

/**
 * @brief       Start Control OUT transaction
 *
 * @param[in]   pu8Buf      Control OUT data pointer
 * @param[in]   u32Size     OUT transfer size
 *
 * @return      None
 *
 * @details     This function is used to start Control OUT transfer
 */
void USBD_CtrlOut(uint8_t *pu8Buf, uint32_t u32Size)
{
    int volatile i;

    while (1)
    {
        if (USBD->CEPINTSTS & USBD_CEPINTSTS_RXPKIF_Msk)
        {
            for (i = 0; i < u32Size; i++)
                *(uint8_t *)(pu8Buf + i) = USBD->cep.CEPDAT_BYTE;
            USBD->CEPINTSTS = USBD_CEPINTSTS_RXPKIF_Msk;
            break;
        }
    }
}

/**
 * @brief       Clear all software flags
 *
 * @return      None
 *
 * @details     This function is used to clear all software control flag
 */
void USBD_SwReset(void)
{
    // Reset all variables for protocol
    g_usbd_UsbAddr = 0;
    g_usbd_DmaDone = 0;
    g_usbd_ShortPacket = 0;
    g_usbd_Configured = 0;

    // Reset USB device address
    USBD_SET_ADDR(0);
}

/**
 * @brief       USBD Set Vendor Request
 *
 * @param[in]   pfnVendorReq         Vendor Request Callback Function
 *
 * @return      None
 *
 * @details     This function is used to set USBD vendor request callback function
 */
void USBD_SetVendorRequest(VENDOR_REQ pfnVendorReq)
{
    g_usbd_pfnVendorRequest = pfnVendorReq;
}


/*@}*/ /* end of group USBD_EXPORTED_FUNCTIONS */

/*@}*/ /* end of group USBD_Driver */

/*@}*/ /* end of group Standard_Driver */

/*** (C) COPYRIGHT 2018 Nuvoton Technology Corp. ***/