summaryrefslogtreecommitdiff
path: root/bsp/at32/libraries/rt_drivers/drv_usbfsd.c
blob: 494db5dabc8c45226c97c62a04bf85558ddef3c8 (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
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-02-28     leo          first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include "usbd_int.h"
#include "drv_common.h"
#include "drv_usbfs.h"
#include "drv_config.h"

#if defined(BSP_USING_DEVICE_USBFS1) || defined(BSP_USING_DEVICE_USBFS2)

static struct at32_usbfs *p_usbfs_instance = NULL;
static struct ep_id endpoint_pool[] =
{
    {0x0,  USB_EP_ATTR_CONTROL,   USB_DIR_INOUT, 64, ID_ASSIGNED  },
    {0x1,  USB_EP_ATTR_BULK,      USB_DIR_IN,    64, ID_UNASSIGNED},
    {0x1,  USB_EP_ATTR_BULK,      USB_DIR_OUT,   64, ID_UNASSIGNED},
    {0x2,  USB_EP_ATTR_INT,       USB_DIR_IN,    64, ID_UNASSIGNED},
    {0x2,  USB_EP_ATTR_INT,       USB_DIR_OUT,   64, ID_UNASSIGNED},
    {0x3,  USB_EP_ATTR_ISOC,      USB_DIR_IN,    64, ID_UNASSIGNED},
    {0x3,  USB_EP_ATTR_ISOC,      USB_DIR_OUT,   64, ID_UNASSIGNED},
    {0xFF, USB_EP_ATTR_TYPE_MASK, USB_DIR_MASK,  0,  ID_ASSIGNED  },
};

enum
{
#ifdef BSP_USING_DEVICE_USBFS1
    USBFS1_INDEX,
#endif
#ifdef BSP_USING_DEVICE_USBFS2
    USBFS2_INDEX,
#endif
};

static struct at32_usbfs usbfsd_config[] = {
#ifdef BSP_USING_DEVICE_USBFS1
    USBFS1_CONFIG,
#endif
#ifdef BSP_USING_DEVICE_USBFS2
    USBFS2_CONFIG,
#endif
};

#ifdef BSP_USING_DEVICE_USBFS1
void OTGFS1_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    usbd_irq_handler(p_usbfs_instance->p_otg_core);

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif

#ifdef BSP_USING_DEVICE_USBFS2
void OTGFS2_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    usbd_irq_handler(p_usbfs_instance->p_otg_core);

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif

void usbd_reset_callback(usbd_core_type *udev)
{
    udcd_t udcd = (udcd_t)udev->pdata;
    rt_usbd_reset_handler(udcd);
}

void usbd_connectCallback(usbd_core_type *udev)
{
    udcd_t udcd = (udcd_t)udev->pdata;
    rt_usbd_connect_handler(udcd);
}

void usbd_disconnectCallback(usbd_core_type *udev)
{
    udcd_t udcd = (udcd_t)udev->pdata;
    rt_usbd_disconnect_handler(udcd);
}

void usbd_setup_phase_done_callback(usbd_core_type *udev)
{
    udcd_t udcd = (udcd_t)udev->pdata;
    rt_usbd_ep0_setup_handler(udcd, (struct urequest*)udev->setup_buffer);
}

void usbd_data_in_stage_callback(usbd_core_type *udev, uint32_t ept_num)
{
    udcd_t udcd = (udcd_t)udev->pdata;

    if (ept_num == 0)
    {
        rt_usbd_ep0_in_handler(udcd);
    }
    else
    {
        rt_usbd_ep_in_handler(udcd, 0x80 | ept_num, udev->ept_in[ept_num].trans_len);
    }
}

void usbd_sof_callback(usbd_core_type *udev)
{
    udcd_t udcd = (udcd_t)udev->pdata;
    rt_usbd_sof_handler(udcd);
}

void usbd_data_out_stage_callback(usbd_core_type *udev, uint32_t ept_num)
{
    udcd_t udcd = (udcd_t)udev->pdata;

    if (ept_num != 0)
    {
        rt_usbd_ep_out_handler(udcd, ept_num, udev->ept_out[ept_num].trans_len);
    }
    else
    {
        rt_usbd_ep0_out_handler(udcd, udev->ept_out[0].trans_len);
    }
}

static rt_err_t _ep_set_stall(rt_uint8_t address)
{
    usbd_set_stall(&p_usbfs_instance->p_otg_core->dev, address);
    return RT_EOK;
}

static rt_err_t _ep_clear_stall(rt_uint8_t address)
{
    usbd_clear_stall(&p_usbfs_instance->p_otg_core->dev, address);
    return RT_EOK;
}

static rt_err_t _set_address(rt_uint8_t address)
{
    usbd_set_device_addr(&p_usbfs_instance->p_otg_core->dev, address);
    return RT_EOK;
}

static rt_err_t _set_config(rt_uint8_t address)
{
    return RT_EOK;
}

static rt_err_t _ep_enable(uep_t ep)
{
    RT_ASSERT(ep != RT_NULL);
    RT_ASSERT(ep->ep_desc != RT_NULL);

    usbd_ept_open(&p_usbfs_instance->p_otg_core->dev, ep->ep_desc->bEndpointAddress, \
                  ep->ep_desc->bmAttributes, ep->ep_desc->wMaxPacketSize);
    return RT_EOK;
}

static rt_err_t _ep_disable(uep_t ep)
{
    RT_ASSERT(ep != RT_NULL);
    RT_ASSERT(ep->ep_desc != RT_NULL);

    usbd_ept_close(&p_usbfs_instance->p_otg_core->dev, ep->ep_desc->bEndpointAddress);
    return RT_EOK;
}

static rt_size_t _ep_read(rt_uint8_t address, void *buffer)
{
    rt_size_t size = 0;
    RT_ASSERT(buffer != RT_NULL);
    return size;
}

static rt_size_t _ep_read_prepare(rt_uint8_t address, void *buffer, rt_size_t size)
{
    usbd_ept_recv(&p_usbfs_instance->p_otg_core->dev, address, buffer, size);
    return size;
}

static rt_size_t _ep_write(rt_uint8_t address, void *buffer, rt_size_t size)
{
    usbd_ept_send(&p_usbfs_instance->p_otg_core->dev, address, buffer, size);
    return size;
}

static rt_err_t _ep0_send_status(void)
{
    usbd_ept_send(&p_usbfs_instance->p_otg_core->dev, 0x00, NULL, 0);
    return RT_EOK;
}

static rt_err_t _suspend(void)
{
    return RT_EOK;
}

static rt_err_t _wakeup(void)
{
    return RT_EOK;
}

static rt_err_t at32_dcd_init(rt_device_t device)
{
    /* usb gpio config */
    at32_msp_usb_init(device);

    /* enable otgfs irq */
    nvic_irq_enable(p_usbfs_instance->irqn, 2, 0);

    /* init usb */
    p_usbfs_instance->p_otg_core->usb_reg = usb_global_select_core(p_usbfs_instance->id);

    /* usb device core config */
    usb_core_config(p_usbfs_instance->p_otg_core, USB_FULL_SPEED_CORE_ID);

    if(p_usbfs_instance->p_otg_core->cfg.sof_out)
    {
      p_usbfs_instance->p_otg_core->usb_reg->gccfg_bit.sofouten = TRUE;
    }

    if(p_usbfs_instance->p_otg_core->cfg.vbusig)
    {
      p_usbfs_instance->p_otg_core->usb_reg->gccfg_bit.vbusig = TRUE;
    }

    /* usb device core init */
    usbd_core_init(&(p_usbfs_instance->p_otg_core->dev), p_usbfs_instance->p_otg_core->usb_reg,
                   USB_FULL_SPEED_CORE_ID);

    return RT_EOK;
}

const static struct udcd_ops _udc_ops =
{
    _set_address,
    _set_config,
    _ep_set_stall,
    _ep_clear_stall,
    _ep_enable,
    _ep_disable,
    _ep_read_prepare,
    _ep_read,
    _ep_write,
    _ep0_send_status,
    _suspend,
    _wakeup,
};

int at32_usbd_register(void)
{
    rt_size_t obj_num;
    rt_err_t result = 0;
    int index;

    obj_num = sizeof(usbfsd_config) / sizeof(struct at32_usbfs);

    for (index = 0; index < obj_num; index++) {
        udcd_t udcd = (udcd_t)rt_malloc(sizeof(struct udcd));
        if (udcd == RT_NULL)
        {
            rt_kprintf("uhcd malloc failed\r\n");
            return -RT_ERROR;
        }
        rt_memset((void *)udcd, 0, sizeof(struct udcd));

        otg_core_type *p_otg_core = (otg_core_type *)rt_malloc(sizeof(otg_core_type));
        if (p_otg_core == RT_NULL)
        {
            rt_kprintf("otg_core malloc failed\r\n");
            return -RT_ERROR;
        }
        rt_memset((void *)p_otg_core, 0, sizeof(otg_core_type));

        udcd->parent.type = RT_Device_Class_USBDevice;
        udcd->parent.init = at32_dcd_init;

        udcd->parent.user_data = &(p_otg_core->dev);
        udcd->ops = &_udc_ops;
        p_otg_core->dev.pdata = udcd;
        usbfsd_config[index].p_otg_core = p_otg_core;

        /* register endpoint infomation */
        udcd->ep_pool = endpoint_pool;
        udcd->ep0.id = &endpoint_pool[0];

        result = rt_device_register((rt_device_t)udcd, usbfsd_config[index].name, 0);
        RT_ASSERT(result == RT_EOK);

        p_usbfs_instance = &usbfsd_config[index];

        result = rt_usb_device_init();
        RT_ASSERT(result == RT_EOK);
    }

    return result;
}

INIT_DEVICE_EXPORT(at32_usbd_register);

#endif