mxpopstar
2022-05-04 fc9a39919da8db88c68ee1c41c01f6c26b88f12c
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
/*!
    \file    usbh_transc.c
    \brief   USB host mode transactions driver
 
    \version 2020-08-01, V3.0.0, firmware for GD32F30x
*/
 
/*
    Copyright (c) 2020, GigaDevice Semiconductor Inc.
 
    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 "drv_usb_hw.h"
#include "usbh_pipe.h"
#include "usbh_transc.h"
#include <string.h>
 
/* local function prototypes ('static') */
static usb_urb_state usbh_urb_wait (usbh_host *puhost, uint8_t pp_num, uint32_t wait_time);
static void usbh_setup_transc (usbh_host *puhost);
static void usbh_data_in_transc (usbh_host *puhost);
static void usbh_data_out_transc (usbh_host *puhost);
static void usbh_status_in_transc (usbh_host *puhost);
static void usbh_status_out_transc (usbh_host *puhost);
static uint32_t usbh_request_submit (usb_core_driver *pudev, uint8_t pp_num);
 
/*!
    \brief      send the setup packet to the USB device
    \param[in]  pudev: pointer to usb core instance
    \param[in]  buf: data buffer which will be sent to USB device
    \param[in]  pp_num: pipe number
    \param[out] none
    \retval     operation status
*/
usbh_status usbh_ctlsetup_send (usb_core_driver *pudev, uint8_t *buf, uint8_t pp_num)
{
    usb_pipe *pp = &pudev->host.pipe[pp_num];
 
    pp->DPID = PIPE_DPID_SETUP;
    pp->xfer_buf = buf;
    pp->xfer_len = USB_SETUP_PACKET_LEN;
 
    return (usbh_status)usbh_request_submit (pudev, pp_num);
}
 
/*!
    \brief      send a data packet to the USB device
    \param[in]  pudev: pointer to usb core instance
    \param[in]  buf: data buffer which will be sent to USB device
    \param[in]  pp_num: pipe number
    \param[in]  len: length of the data to be sent
    \param[out] none
    \retval     operation status
*/
usbh_status usbh_data_send (usb_core_driver *pudev, uint8_t *buf, uint8_t pp_num, uint16_t len)
{
    usb_pipe *pp = &pudev->host.pipe[pp_num];
 
    pp->xfer_buf = buf;
    pp->xfer_len = len;
 
    switch (pp->ep.type) {
    case USB_EPTYPE_CTRL:
        if (0U == len) {
            pp->data_toggle_out = 1U;
        }
 
        pp->DPID = PIPE_DPID[pp->data_toggle_out];
        break;
 
    case USB_EPTYPE_INTR:
        pp->DPID = PIPE_DPID[pp->data_toggle_out];
 
        pp->data_toggle_out ^= 1U;
        break;
 
    case USB_EPTYPE_BULK:
        pp->DPID = PIPE_DPID[pp->data_toggle_out];
        break;
 
    case USB_EPTYPE_ISOC:
        pp->DPID = PIPE_DPID[0];
        break;
 
    default:
        break;
    }
 
    usbh_request_submit (pudev, pp_num);
 
    return USBH_OK;
}
 
/*!
    \brief      receive a data packet from the USB device
    \param[in]  pudev: pointer to usb core instance
    \param[in]  buf: data buffer which will be received from USB device
    \param[in]  pp_num: pipe number
    \param[in]  len: length of the data to be received
    \param[out] none
    \retval     operation status
*/
usbh_status usbh_data_recev (usb_core_driver *pudev, uint8_t *buf, uint8_t pp_num, uint16_t len)
{
    usb_pipe *pp = &pudev->host.pipe[pp_num];
 
    pp->xfer_buf = buf;
    pp->xfer_len = len;
 
    switch (pp->ep.type) {
    case USB_EPTYPE_CTRL:
        pp->DPID = PIPE_DPID[1];
        break;
 
    case USB_EPTYPE_INTR:
        pp->DPID = PIPE_DPID[pp->data_toggle_in];
 
        /* Toggle DATA PID */
        pp->data_toggle_in ^= 1U;
        break;
 
    case USB_EPTYPE_BULK:
        pp->DPID = PIPE_DPID[pp->data_toggle_in];
        break;
 
    case USB_EPTYPE_ISOC:
        pp->DPID = PIPE_DPID[0];
        break;
 
    default:
        break;
    }
 
    usbh_request_submit (pudev, pp_num);
 
    return USBH_OK;
}
 
/*!
    \brief      USB control transfer handler
    \param[in]  puhost: pointer to USB host
    \param[out] none
    \retval     operation status
*/
usbh_status usbh_ctl_handler (usbh_host *puhost)
{
    usbh_status status = USBH_BUSY;
 
    switch (puhost->control.ctl_state) {
    case CTL_SETUP:
        usbh_setup_transc (puhost);
        break;
 
    case CTL_DATA_IN:
        usbh_data_in_transc (puhost);
        break;
 
    case CTL_DATA_OUT:
        usbh_data_out_transc (puhost);
        break;
 
    case CTL_STATUS_IN:
        usbh_status_in_transc (puhost);
        break;
 
    case CTL_STATUS_OUT:
        usbh_status_out_transc (puhost);
        break;
 
    case CTL_FINISH:
        puhost->control.ctl_state = CTL_IDLE;
 
        status = USBH_OK;
        break;
 
    case CTL_ERROR:
        if (++puhost->control.error_count <= USBH_MAX_ERROR_COUNT) {
            /* do the transmission again, starting from SETUP packet */
            puhost->control.ctl_state = CTL_SETUP;
        } else {
            status = USBH_FAIL;
        }
        break;
 
    default:
        break;
    }
 
    return status;
}
 
/*!
    \brief      wait for USB URB(USB request block) state
    \param[in]  puhost: pointer to USB host
    \param[in]  pp_num: pipe number
    \param[in]  wait_time: wait time
    \param[out] none
    \retval     USB URB state
*/
static usb_urb_state usbh_urb_wait (usbh_host *puhost, uint8_t pp_num, uint32_t wait_time)
{
    usb_urb_state urb_status = URB_IDLE;
 
    while (URB_DONE != (urb_status = usbh_urbstate_get((usb_core_driver *)(puhost->data), pp_num))) {
        if (URB_NOTREADY == urb_status) {
            break;
        } else if (URB_STALL == urb_status) {
            puhost->control.ctl_state = CTL_SETUP;
            break;
        } else if (URB_ERROR == urb_status) {
            puhost->control.ctl_state = CTL_ERROR;
            break;
        } else if ((wait_time > 0U) && ((usb_curframe_get((usb_core_driver *)(puhost->data))- puhost->control.timer) > wait_time)) {
            /* timeout for in transfer */
            puhost->control.ctl_state = CTL_ERROR;
            break;
        } else {
            /* no operation, just wait */
        }
    }
 
    return urb_status;
}
 
/*!
    \brief      USB setup transaction
    \param[in]  puhost: pointer to USB host
    \param[out] none
    \retval     none
*/
static void usbh_setup_transc (usbh_host *puhost)
{
    /* send a SETUP packet */
    usbh_ctlsetup_send ((usb_core_driver *)(puhost->data), 
                        puhost->control.setup.data, 
                        puhost->control.pipe_out_num);
 
    if (URB_DONE == usbh_urb_wait (puhost, puhost->control.pipe_out_num, 0U)) {
        uint8_t dir = (puhost->control.setup.req.bmRequestType & USB_TRX_MASK);
 
        if (puhost->control.setup.req.wLength) {
            if (USB_TRX_IN == dir) {
                puhost->control.ctl_state = CTL_DATA_IN;
            } else {
                puhost->control.ctl_state = CTL_DATA_OUT;
            }
        } else {
            if (USB_TRX_IN == dir) {
                puhost->control.ctl_state = CTL_STATUS_OUT;
            } else {
                puhost->control.ctl_state = CTL_STATUS_IN;
            }
        }
 
        /* set the delay timer to enable timeout for data stage completion */
        puhost->control.timer = (uint16_t)usb_curframe_get((usb_core_driver *)(puhost->data));
    }
}
 
/*!
    \brief      USB data IN transaction
    \param[in]  puhost: pointer to USB host
    \param[out] none
    \retval     none
*/
static void usbh_data_in_transc (usbh_host *puhost)
{
    usbh_data_recev ((usb_core_driver *)(puhost->data),
                     puhost->control.buf,
                     puhost->control.pipe_in_num,
                     puhost->control.ctl_len);
 
    if (URB_DONE == usbh_urb_wait (puhost, puhost->control.pipe_in_num, DATA_STAGE_TIMEOUT)) {
        puhost->control.ctl_state = CTL_STATUS_OUT;
 
        puhost->control.timer = (uint16_t)usb_curframe_get((usb_core_driver *)(puhost->data));
    }
}
 
/*!
    \brief      USB data OUT transaction
    \param[in]  puhost: pointer to USB host
    \param[out] none
    \retval     none
*/
static void usbh_data_out_transc (usbh_host *puhost)
{
    usbh_pipe_toggle_set((usb_core_driver *)(puhost->data), puhost->control.pipe_out_num, 1U);
 
    usbh_data_send ((usb_core_driver *)(puhost->data),
                    puhost->control.buf,
                    puhost->control.pipe_out_num,
                    puhost->control.ctl_len);
 
    if (URB_DONE == usbh_urb_wait (puhost, puhost->control.pipe_out_num, DATA_STAGE_TIMEOUT)) {
        puhost->control.ctl_state = CTL_STATUS_IN;
 
        puhost->control.timer = (uint16_t)usb_curframe_get((usb_core_driver *)(puhost->data));
    }
}
 
/*!
    \brief      USB status IN transaction
    \param[in]  puhost: pointer to USB host
    \param[out] none
    \retval     none
*/
static void usbh_status_in_transc (usbh_host *puhost)
{
    uint8_t pp_num = puhost->control.pipe_in_num;
 
    usbh_data_recev ((usb_core_driver *)(puhost->data), NULL, pp_num, 0U);
 
    if (URB_DONE == usbh_urb_wait (puhost, pp_num, NODATA_STAGE_TIMEOUT)) {
        puhost->control.ctl_state = CTL_FINISH;
    }
}
 
/*!
    \brief      USB status OUT transaction
    \param[in]  puhost: pointer to USB host
    \param[out] none
    \retval     none
*/
static void usbh_status_out_transc (usbh_host *puhost)
{
    uint8_t pp_num = puhost->control.pipe_out_num;
 
    usbh_data_send ((usb_core_driver *)(puhost->data), NULL, pp_num, 0U);
 
    if (URB_DONE == usbh_urb_wait (puhost, pp_num, NODATA_STAGE_TIMEOUT)) {
        puhost->control.ctl_state = CTL_FINISH;
    }
}
 
/*!
    \brief      prepare a pipe and start a transfer
    \param[in]  pudev: pointer to usb core instance
    \param[in]  pp_num: pipe number
    \param[out] none
    \retval     operation status
*/
static uint32_t usbh_request_submit (usb_core_driver *pudev, uint8_t pp_num) 
{
    pudev->host.pipe[pp_num].urb_state = URB_IDLE;
    pudev->host.pipe[pp_num].xfer_count = 0U;
 
    return (uint32_t)usb_pipe_xfer (pudev, pp_num);
}