summaryrefslogtreecommitdiff
path: root/bsp/gd32107c-eval/Libraries/GD32F10x_usbfs_driver/Source/usbh_ctrl.c
blob: 3f5277701f1d36f60bc09366f32b71c489a66c3c (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
/*!
    \file  usbh_ctrl.c
    \brief this file implements the functions for the control transmit process

    \version 2014-12-26, V1.0.0, firmware for GD32F10x
    \version 2017-06-20, V2.0.0, firmware for GD32F10x
    \version 2018-07-31, V2.1.0, firmware for GD32F10x
*/

/*
    Copyright (c) 2018, GigaDevice Semiconductor Inc.

    All rights reserved.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include "usbh_core.h"
#include "usbh_std.h"
#include "usbh_ctrl.h"

uint8_t ctrl_polling_handle_flag = 0U;
uint8_t ctrl_setup_wait_flag = 0U;
uint8_t ctrl_data_wait_flag = 0U;
uint8_t ctrl_status_wait_flag = 0U;

static uint16_t timeout = 0U;

static void ctrl_idle_handle      (usb_core_handle_struct *pudev, usbh_host_struct *puhost, usbh_state_handle_struct *pustate);
static void ctrl_setup_handle     (usb_core_handle_struct *pudev, usbh_host_struct *puhost, usbh_state_handle_struct *pustate);
static void ctrl_data_handle      (usb_core_handle_struct *pudev, usbh_host_struct *puhost, usbh_state_handle_struct *pustate);
static void ctrl_status_handle    (usb_core_handle_struct *pudev, usbh_host_struct *puhost, usbh_state_handle_struct *pustate);
static void ctrl_error_handle     (usb_core_handle_struct *pudev, usbh_host_struct *puhost, usbh_state_handle_struct *pustate);
static void ctrl_stalled_handle   (usb_core_handle_struct *pudev, usbh_host_struct *puhost, usbh_state_handle_struct *pustate);
static void ctrl_complete_handle  (usb_core_handle_struct *pudev, usbh_host_struct *puhost, usbh_state_handle_struct *pustate);

/* the ctrl state handle function array */
void (*ctrl_state_handle[]) (usb_core_handle_struct *pudev,
                             usbh_host_struct *puhost,
                             usbh_state_handle_struct *pustate) =
{
    ctrl_idle_handle,
    ctrl_setup_handle,
    ctrl_data_handle,
    ctrl_status_handle,
    ctrl_error_handle,
    ctrl_stalled_handle,
    ctrl_complete_handle,
};

/* the ctrl state handle table */
state_table_struct ctrl_handle_table[CTRL_HANDLE_TABLE_SIZE] =
{
    /* the current state   the current event           the next state        the event function */
    {CTRL_IDLE,            CTRL_EVENT_SETUP,           CTRL_SETUP,           only_state_move     },
    {CTRL_SETUP,           CTRL_EVENT_DATA,            CTRL_DATA,            only_state_move     },
    {CTRL_SETUP,           CTRL_EVENT_STATUS,          CTRL_STATUS,          only_state_move     },
    {CTRL_SETUP,           CTRL_EVENT_ERROR,           CTRL_ERROR,           only_state_move     },
    {CTRL_DATA,            CTRL_EVENT_STATUS,          CTRL_STATUS,          only_state_move     },
    {CTRL_DATA,            CTRL_EVENT_ERROR,           CTRL_ERROR,           only_state_move     },
    {CTRL_DATA,            CTRL_EVENT_STALLED,         CTRL_STALLED,         only_state_move     },
    {CTRL_STATUS,          CTRL_EVENT_COMPLETE,        CTRL_COMPLETE,        only_state_move     },
    {CTRL_STATUS,          CTRL_EVENT_ERROR,           CTRL_ERROR,           only_state_move     },
    {CTRL_STATUS,          CTRL_EVENT_STALLED,         CTRL_STALLED,         only_state_move     },
    {CTRL_ERROR,           GO_TO_UP_STATE_EVENT,       UP_STATE,             goto_up_state_fun   },
    {CTRL_STALLED,         GO_TO_UP_STATE_EVENT,       UP_STATE,             goto_up_state_fun   },
    {CTRL_COMPLETE,        GO_TO_UP_STATE_EVENT,       UP_STATE,             goto_up_state_fun   },
};

/*!
    \brief      the polling function of CTRL state
    \param[in]  pudev: pointer to usb device
    \param[in]  puhost: pointer to usb host
    \param[in]  pustate: pointer to usb state driver
    \param[out] none
    \retval     none
*/
usbh_status_enum ctrl_state_polling_fun (usb_core_handle_struct *pudev,
                                         usbh_host_struct *puhost,
                                         void *pustate)
{
    usbh_status_enum exe_state = USBH_BUSY;
    usbh_state_handle_struct *p_state;

    p_state = (usbh_state_handle_struct *)pustate;

    /* if first enter this function, begin the ctrl state */
    if (0U == ctrl_polling_handle_flag) {
        ctrl_polling_handle_flag = 1U;
        scd_table_push(p_state);
        scd_state_move(p_state, CTRL_IDLE);
    }

    /* base on the current state to handle the ctrl state */
    scd_begin(p_state, CTRL_FSM_ID);
    ctrl_state_handle[p_state->usbh_current_state](pudev, puhost, p_state);

    /* determine the control transfer whether to complete */
    switch (puhost->usbh_backup_state.ctrl_backup_state) {
        case CTRL_COMPLETE:
            ctrl_polling_handle_flag = 0U;
            puhost->usbh_backup_state.ctrl_backup_state = CTRL_IDLE;
            exe_state = USBH_OK;
            break;
        case CTRL_STALLED:
            ctrl_polling_handle_flag = 0U;
            puhost->usbh_backup_state.ctrl_backup_state = CTRL_IDLE;
            exe_state = USBH_NOT_SUPPORTED;
            break;
        case CTRL_ERROR:
            ctrl_polling_handle_flag = 0U;
            puhost->usbh_backup_state.ctrl_backup_state = CTRL_IDLE;
            exe_state = USBH_FAIL;
            break;
        default:
            exe_state = USBH_BUSY;
            break;
    }

    return exe_state;
}

/*!
    \brief      the handle function of CTRL_IDLE state
    \param[in]  pudev: pointer to usb device
    \param[in]  puhost: pointer to usb host
    \param[in]  pustate: pointer to usb state driver
    \param[out] none
    \retval     none
*/
static void ctrl_idle_handle (usb_core_handle_struct *pudev,
                              usbh_host_struct *puhost,
                              usbh_state_handle_struct *pustate)
{
    puhost->usbh_backup_state.ctrl_backup_state = CTRL_IDLE;
    scd_event_handle(pudev, puhost, pustate, CTRL_EVENT_SETUP, pustate->usbh_current_state);
}

/*!
    \brief      the handle function of CTRL_SETUP state
    \param[in]  pudev: pointer to usb device
    \param[in]  puhost: pointer to usb host
    \param[in]  pustate: pointer to usb state driver
    \param[out] none
    \retval     none
*/
static void ctrl_setup_handle (usb_core_handle_struct *pudev,
                               usbh_host_struct *puhost,
                               usbh_state_handle_struct *pustate)
{
    urb_state_enum urb_status = URB_IDLE;
    puhost->usbh_backup_state.ctrl_backup_state = CTRL_SETUP;

    if (0U == ctrl_setup_wait_flag) {
        ctrl_setup_wait_flag = 1U;

        /* send a setup packet */
        usbh_ctltx_setup (pudev,
                          puhost->control.setup.data,
                          puhost->control.hc_out_num);
    } else {
        urb_status = hcd_urb_state_get(pudev, puhost->control.hc_out_num);

        /* case setup packet sent successfully */
        if (URB_DONE == urb_status) {
            /* check if there is a data stage */
            if (0U != puhost->control.setup.b.wLength) {
                ctrl_setup_wait_flag = 0U;
                timeout = DATA_STAGE_TIMEOUT;
                scd_event_handle(pudev, puhost, pustate, CTRL_EVENT_DATA, pustate->usbh_current_state);
            /* no data stage */
            } else {
                timeout = NODATA_STAGE_TIMEOUT;
                ctrl_setup_wait_flag = 0U;
                scd_event_handle(pudev,
                                 puhost,
                                 pustate,
                                 CTRL_EVENT_STATUS,
                                 pustate->usbh_current_state);
            }

            /* set the delay timer to enable timeout for data stage completion */
            puhost->control.timer = (uint16_t)USB_CURRENT_FRAME_GET();
        } else if (URB_ERROR == urb_status) {
            ctrl_setup_wait_flag = 0U;
            scd_event_handle(pudev, puhost, pustate, CTRL_EVENT_ERROR, pustate->usbh_current_state);
        } else {
            /* no operation */
        }
    }
}

/*!
    \brief      the handle function of CTRL_DATA state
    \param[in]  pudev: pointer to usb device
    \param[in]  puhost: pointer to usb host
    \param[in]  pustate: pointer to usb state driver
    \param[out] none
    \retval     none
*/
static void ctrl_data_handle (usb_core_handle_struct *pudev,
                              usbh_host_struct *puhost,
                              usbh_state_handle_struct *pustate)
{
    uint8_t direction;
    urb_state_enum urb_status = URB_IDLE;
    puhost->usbh_backup_state.ctrl_backup_state = CTRL_DATA;

    direction = (puhost->control.setup.b.bmRequestType & USB_DIR_MASK);

    if (USB_DIR_IN == direction) {
        if (0U == ctrl_data_wait_flag) {
            ctrl_data_wait_flag = 1U;

            /* issue an IN token */
            usbh_xfer(pudev,
                      puhost->control.buff,
                      puhost->control.hc_in_num,
                      puhost->control.length);
        } else {
            urb_status = hcd_urb_state_get(pudev, puhost->control.hc_in_num);

            /* check is data packet transfered successfully */
            switch (urb_status) {
                case URB_DONE:
                    ctrl_data_wait_flag = 0U;

                    scd_event_handle(pudev,
                                     puhost,
                                     pustate,
                                     CTRL_EVENT_STATUS,
                                     pustate->usbh_current_state);
                    break;
                case URB_STALL:
                    ctrl_data_wait_flag = 0U;

                    scd_event_handle(pudev,
                                     puhost,
                                     pustate,
                                     CTRL_EVENT_STALLED,
                                     pustate->usbh_current_state);
                    break;
                case URB_ERROR:
                    ctrl_data_wait_flag = 0U;

                    /* device error */
                    scd_event_handle(pudev,
                                     puhost,
                                     pustate,
                                     CTRL_EVENT_ERROR,
                                     pustate->usbh_current_state);
                    break;
                default:
                    if (((uint16_t)USB_CURRENT_FRAME_GET() - puhost->control.timer) > timeout) {
                        ctrl_data_wait_flag = 0U;

                        /* timeout for in transfer */
                        scd_event_handle(pudev,
                                         puhost,
                                         pustate,
                                         CTRL_EVENT_ERROR,
                                         pustate->usbh_current_state);
                    }
                    break;
            }
        }
    } else {
        if (0U == ctrl_data_wait_flag) {
            ctrl_data_wait_flag = 1U;

            /* start data out transfer (only one data packet)*/
            pudev->host.host_channel[puhost->control.hc_out_num].data_tg_out = 1U;

            usbh_xfer(pudev,
                      puhost->control.buff,
                      puhost->control.hc_out_num,
                      puhost->control.length);
        } else {
            urb_status = hcd_urb_state_get(pudev, puhost->control.hc_out_num);

            switch (urb_status) {
                case URB_DONE:
                    ctrl_data_wait_flag = 0U;

                    /* if the setup pkt is sent successful, then change the state */
                    scd_event_handle(pudev,
                                     puhost,
                                     pustate,
                                     CTRL_EVENT_STATUS,
                                     pustate->usbh_current_state);
                    break;
                case URB_STALL:
                    ctrl_data_wait_flag = 0U;

                    scd_event_handle(pudev,
                                     puhost,
                                     pustate,
                                     CTRL_EVENT_STALLED,
                                     pustate->usbh_current_state);
                    break;
                case URB_NOTREADY:
                    /* nack received from device */
                    ctrl_data_wait_flag = 0U;
                    break;
                case URB_ERROR:
                    ctrl_data_wait_flag = 0U;

                    /* device error */
                    scd_event_handle(pudev,
                                     puhost,
                                     pustate,
                                     CTRL_EVENT_ERROR,
                                     pustate->usbh_current_state);
                    break;
                default:
                    break;
            }
        }
    }
}

/*!
    \brief      the handle function of CTRL_STATUS state
    \param[in]  pudev: pointer to usb device
    \param[in]  puhost: pointer to usb host
    \param[in]  pustate: pointer to usb state driver
    \param[out] none
    \retval     none
*/
static void ctrl_status_handle (usb_core_handle_struct *pudev,
                                usbh_host_struct *puhost,
                                usbh_state_handle_struct *pustate)
{
    uint8_t direction;
    urb_state_enum urb_status = URB_IDLE;

    puhost->usbh_backup_state.ctrl_backup_state = CTRL_STATUS;

    /* get the transfer direction in the data state, but the transfer direction in the status state is opposite */
    direction = (puhost->control.setup.b.bmRequestType & USB_DIR_MASK);

    if (USB_DIR_OUT == direction) {
        /* handle status in */
        if (0U == ctrl_status_wait_flag) {
            ctrl_status_wait_flag = 1U;
            usbh_xfer (pudev, 0U, puhost->control.hc_in_num, 0U);
        } else {
            urb_status = hcd_urb_state_get(pudev, puhost->control.hc_in_num);

            switch (urb_status) {
                case URB_DONE:
                    ctrl_status_wait_flag = 0U;

                    /* handle URB_DONE status */
                    scd_event_handle(pudev,
                                     puhost,
                                     pustate,
                                     CTRL_EVENT_COMPLETE,
                                     pustate->usbh_current_state);
                    break;
                case URB_ERROR:
                    ctrl_status_wait_flag = 0U;

                    /* handle URB_STALL status*/
                    scd_event_handle(pudev,
                                     puhost,
                                     pustate,
                                     CTRL_EVENT_ERROR,
                                     pustate->usbh_current_state);
                    break;
                case URB_STALL:
                    ctrl_status_wait_flag = 0U;

                    /* handle URB_STALL status */
                    scd_event_handle(pudev,
                                     puhost,
                                     pustate,
                                     CTRL_EVENT_STALLED,
                                     pustate->usbh_current_state);
                    break;
                default:
                    if (((uint16_t)USB_CURRENT_FRAME_GET() - puhost->control.timer) > timeout) {
                        ctrl_status_wait_flag = 0U;

                        /* handle timeout */
                        scd_event_handle(pudev,
                                         puhost,
                                         pustate,
                                         CTRL_EVENT_ERROR,
                                         pustate->usbh_current_state);
                    }
                    break;
            }
        }
    } else {
        /* handle status out */
        if (0U == ctrl_status_wait_flag) {
            ctrl_status_wait_flag = 1U;
            pudev->host.host_channel[puhost->control.hc_out_num].data_tg_out ^= 1U;
            usbh_xfer (pudev, 0U, puhost->control.hc_out_num, 0U);
        } else {
            urb_status = hcd_urb_state_get(pudev, puhost->control.hc_out_num);

            switch (urb_status) {
                case URB_DONE:
                    ctrl_status_wait_flag = 0U;

                    /* handle URB_DONE status */
                    scd_event_handle(pudev,
                                     puhost,
                                     pustate,
                                     CTRL_EVENT_COMPLETE,
                                     pustate->usbh_current_state);
                    break;
                case URB_NOTREADY:
                    /* handle URB_NOTREADY status */
                    ctrl_status_wait_flag = 0U;
                    break;
                case URB_ERROR:
                    ctrl_status_wait_flag = 0U;

                    /* handle URB_ERROR status */
                    scd_event_handle(pudev,
                                     puhost,
                                     pustate,
                                     CTRL_EVENT_ERROR,
                                     pustate->usbh_current_state);
                    break;
                default:
                    break;
            }
        }
    }
}

/*!
    \brief      the handle function of CTRL_ERROR state
    \param[in]  pudev: pointer to usb device
    \param[in]  puhost: pointer to usb host
    \param[in]  pustate: pointer to usb state driver
    \param[out] none
    \retval     none
*/
static void ctrl_error_handle (usb_core_handle_struct *pudev,
                               usbh_host_struct *puhost,
                               usbh_state_handle_struct *pustate)
{
    puhost->usbh_backup_state.ctrl_backup_state = CTRL_ERROR;

    if (++puhost->control.error_count <= USBH_MAX_ERROR_COUNT) {
        /* do the transmission again, starting from setup packet */
        scd_event_handle(pudev, puhost, pustate, CTRL_EVENT_SETUP, pustate->usbh_current_state);
    } else {
        scd_event_handle(pudev, puhost, pustate, GO_TO_UP_STATE_EVENT, pustate->usbh_current_state);
    }
}

/*!
    \brief      the handle function of CTRL_STALLED state
    \param[in]  pudev: pointer to usb device
    \param[in]  puhost: pointer to usb host
    \param[in]  pustate: pointer to usb state driver
    \param[out] none
    \retval     none
*/
static void ctrl_stalled_handle (usb_core_handle_struct *pudev,
                                 usbh_host_struct *puhost,
                                 usbh_state_handle_struct *pustate)
{
    puhost->usbh_backup_state.ctrl_backup_state = CTRL_STALLED;
    scd_event_handle(pudev, puhost, pustate, GO_TO_UP_STATE_EVENT, pustate->usbh_current_state);
}

/*!
    \brief      the handle function of CTRL_COMPLETE state
    \param[in]  pudev: pointer to usb device
    \param[in]  puhost: pointer to usb host
    \param[in]  pustate: pointer to usb state driver
    \param[out] none
    \retval     none
*/
static void ctrl_complete_handle (usb_core_handle_struct *pudev,
                                  usbh_host_struct *puhost,
                                  usbh_state_handle_struct *pustate)
{
    puhost->usbh_backup_state.ctrl_backup_state = CTRL_COMPLETE;
    scd_event_handle(pudev, puhost, pustate, GO_TO_UP_STATE_EVENT, pustate->usbh_current_state);
}

/*!
    \brief      send datas from the host channel
    \param[in]  pudev: pointer to usb device
    \param[in]  buf: data buffer address to send datas
    \param[in]  hc_num: the number of the host channel
    \param[in]  len: length of the send data
    \param[out] none
    \retval     host operation status
*/
usbh_status_enum usbh_xfer (usb_core_handle_struct *pudev,
                       uint8_t *buf,
                       uint8_t  hc_num,
                       uint16_t len)
{
    usb_hostchannel_struct *puhc = &pudev->host.host_channel[hc_num];

    puhc->xfer_buff = buf;
    puhc->xfer_len = len;

    switch (puhc->endp_type) {
        case USB_EPTYPE_CTRL:
            if (0U == puhc->endp_in) {
                if (0U == len) {
                    /* for status out stage, length = 0, status out pid = 1 */
                    puhc->data_tg_out = 1U;
                }

                /* set the data toggle bit as per the flag */
                if (0U == puhc->data_tg_out) {
                    /* put the pid 0 */
                    puhc->DPID = HC_PID_DATA0;
                } else {
                    /* put the pid 1 */
                    puhc->DPID = HC_PID_DATA1;
                }
            } else {
                puhc->DPID = HC_PID_DATA1;
            }
            break;

        case USB_EPTYPE_ISOC:
            puhc->DPID = HC_PID_DATA0;
            break;

        case USB_EPTYPE_BULK:
            if (0U == puhc->endp_in) {
                /* set the data toggle bit as per the flag */
                if (0U == puhc->data_tg_out) {
                    /* put the pid 0 */
                    puhc->DPID = HC_PID_DATA0;
                } else {
                    /* put the pid 1 */
                    puhc->DPID = HC_PID_DATA1;
                }
            } else {
                if (0U == puhc->data_tg_in) {
                    puhc->DPID = HC_PID_DATA0;
                } else {
                    puhc->DPID = HC_PID_DATA1;
                }
            }
            break;

        case USB_EPTYPE_INTR:
            if (0U == puhc->endp_in) {
                if (0U == puhc->data_tg_out) {
                    puhc->DPID = HC_PID_DATA0;
                } else {
                    puhc->DPID = HC_PID_DATA1;
                }

                /* toggle data pid */
                puhc->data_tg_out ^= 1U;
            } else {
                if (0U == puhc->data_tg_in) {
                    puhc->DPID = HC_PID_DATA0;
                } else {
                    puhc->DPID = HC_PID_DATA1;
                }

                /* toggle data pid */
                puhc->data_tg_in ^= 1U;
            }
            break;

        default:
            break;
    }

    hcd_submit_request (pudev, hc_num);

    return USBH_OK;
}

/*!
    \brief      send the setup packet to the device
    \param[in]  pudev: pointer to usb device
    \param[in]  buf: buffer pointer from which the data will be send to device
    \param[in]  hc_num: host channel number
    \param[out] none
    \retval     host operation status
*/
usbh_status_enum usbh_ctltx_setup (usb_core_handle_struct *pudev, uint8_t *buf, uint8_t  hc_num)
{
    usb_hostchannel_struct *puhc = &pudev->host.host_channel[hc_num];

    puhc->DPID = HC_PID_SETUP;
    puhc->xfer_buff = buf;
    puhc->xfer_len = USBH_SETUP_PACKET_SIZE;

    return (usbh_status_enum)hcd_submit_request (pudev, hc_num);
}

/*!
    \brief      this function prepare a hc and start a transfer
    \param[in]  pudev: pointer to usb device
    \param[in]  channel_num: host channel number which is in (0..7)
    \param[out] none
    \retval     host operation status
*/
uint32_t hcd_submit_request (usb_core_handle_struct *pudev, uint8_t channel_num)
{
    usb_hostchannel_struct *puhc = &pudev->host.host_channel[channel_num];

    puhc->urb_state = URB_IDLE;
    puhc->xfer_count = 0U;

    return (uint32_t)usb_hostchannel_startxfer(pudev, channel_num);
}