usbdevice.c 3.2 KB
Newer Older
M
Ming, Bai 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * File      : usbdevice.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2012, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
H
heyuanjie87 已提交
12
 * 2012-10-02     Yi Qiu       first version
M
Ming, Bai 已提交
13 14 15 16 17 18
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <rtservice.h>

H
heyuanjie87 已提交
19 20
#ifdef RT_USING_USB_DEVICE

21 22
#ifdef RT_USB_DEVICE_COMPOSITE
const static char* ustring[] =
M
Ming, Bai 已提交
23 24 25
{
    "Language",
    "RT-Thread Team.",
26
    "RTT Composite Device",
M
Ming, Bai 已提交
27 28 29 30
    "1.1.0",
    "Configuration",
    "Interface",
};
31
#endif
M
Ming, Bai 已提交
32 33 34 35 36 37 38 39 40 41 42

#ifdef RT_USB_DEVICE_COMPOSITE
static struct udevice_descriptor compsit_desc =
{
    USB_DESC_LENGTH_DEVICE,     //bLength;
    USB_DESC_TYPE_DEVICE,       //type;
    USB_BCD_VERSION,            //bcdUSB;
    USB_CLASS_MISC,             //bDeviceClass;
    0x02,                       //bDeviceSubClass;
    0x01,                       //bDeviceProtocol;
    0x40,                       //bMaxPacketSize0;
H
heyuanjie87 已提交
43 44
    _VENDOR_ID,                 //idVendor;
    _PRODUCT_ID,                //idProduct;
M
Ming, Bai 已提交
45 46 47 48
    USB_BCD_DEVICE,             //bcdDevice;
    USB_STRING_MANU_INDEX,      //iManufacturer;
    USB_STRING_PRODUCT_INDEX,   //iProduct;
    USB_STRING_SERIAL_INDEX,    //iSerialNumber;
49
    USB_DYNAMIC,                //bNumConfigurations;
M
Ming, Bai 已提交
50 51 52 53 54 55 56 57
};
#endif

rt_err_t rt_usb_device_init(const char* udc_name)
{
    rt_device_t udc;
    udevice_t udevice;
    uconfig_t cfg;
58
    uclass_t cls;
M
Ming, Bai 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72

    RT_ASSERT(udc_name != RT_NULL);

    udc = rt_device_find(udc_name);
    if(udc == RT_NULL)
    {
        rt_kprintf("can't find usb device controller %s\n", udc_name);
        return -RT_ERROR;
    }

    /* create and startup usb device thread */
    rt_usbd_core_init();

    /* create a device object */
73 74
    udevice = rt_usbd_device_create();

M
Ming, Bai 已提交
75 76 77 78 79 80 81 82
    /* set usb controller driver to the device */
    rt_usbd_device_set_controller(udevice, (udcd_t)udc);

    /* create a configuration object */
    cfg = rt_usbd_config_create();

#ifdef RT_USB_DEVICE_MSTORAGE
    /* create a mass storage class object */
83
    cls = rt_usbd_class_mstorage_create(udevice);
M
Ming, Bai 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

    /* add the class to the configuration */
    rt_usbd_config_add_class(cfg, cls);
#endif
#ifdef RT_USB_DEVICE_CDC
    /* create a cdc class object */
    cls = rt_usbd_class_cdc_create(udevice);

    /* add the class to the configuration */
    rt_usbd_config_add_class(cfg, cls);
#endif
#ifdef RT_USB_DEVICE_RNDIS
    /* create a rndis class object */
    cls = rt_usbd_class_rndis_create(udevice);

    /* add the class to the configuration */
    rt_usbd_config_add_class(cfg, cls);
#endif

    /* set device descriptor to the device */
#ifdef RT_USB_DEVICE_COMPOSITE
    rt_usbd_device_set_descriptor(udevice, &compsit_desc);
106
    rt_usbd_device_set_string(udevice, ustring);
M
Ming, Bai 已提交
107 108 109 110 111 112
#else
    rt_usbd_device_set_descriptor(udevice, cls->dev_desc);
#endif

    /* add the configuration to the device */
    rt_usbd_device_add_config(udevice, cfg);
113

M
Ming, Bai 已提交
114 115 116 117 118 119 120 121 122
    /* set default configuration to 1 */
    rt_usbd_set_config(udevice, 1);

    /* initialize usb device controller */
    rt_device_init(udc);

    return RT_EOK;
}

H
heyuanjie87 已提交
123
#endif