usbdevice.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * File : usbdevice.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2012-10-02 Yi Qiu first version
  13. */
  14. #include <rtthread.h>
  15. #include <rtdevice.h>
  16. #include <rtservice.h>
  17. #ifdef RT_USING_USB_DEVICE
  18. const static char* ustring[] =
  19. {
  20. "Language",
  21. "RT-Thread Team.",
  22. "RT-Thread Device",
  23. "1.1.0",
  24. "Configuration",
  25. "Interface",
  26. };
  27. #ifdef RT_USB_DEVICE_COMPOSITE
  28. static struct udevice_descriptor compsit_desc =
  29. {
  30. USB_DESC_LENGTH_DEVICE, //bLength;
  31. USB_DESC_TYPE_DEVICE, //type;
  32. USB_BCD_VERSION, //bcdUSB;
  33. USB_CLASS_MISC, //bDeviceClass;
  34. 0x02, //bDeviceSubClass;
  35. 0x01, //bDeviceProtocol;
  36. 0x40, //bMaxPacketSize0;
  37. _VENDOR_ID, //idVendor;
  38. _PRODUCT_ID, //idProduct;
  39. USB_BCD_DEVICE, //bcdDevice;
  40. USB_STRING_MANU_INDEX, //iManufacturer;
  41. USB_STRING_PRODUCT_INDEX, //iProduct;
  42. USB_STRING_SERIAL_INDEX, //iSerialNumber;
  43. USB_DYNAMIC, //bNumConfigurations;
  44. };
  45. #endif
  46. rt_err_t rt_usb_device_init(const char* udc_name)
  47. {
  48. rt_device_t udc;
  49. udevice_t udevice;
  50. uconfig_t cfg;
  51. uclass_t cls;
  52. RT_ASSERT(udc_name != RT_NULL);
  53. udc = rt_device_find(udc_name);
  54. if(udc == RT_NULL)
  55. {
  56. rt_kprintf("can't find usb device controller %s\n", udc_name);
  57. return -RT_ERROR;
  58. }
  59. /* create and startup usb device thread */
  60. rt_usbd_core_init();
  61. /* create a device object */
  62. udevice = rt_usbd_device_create(ustring);
  63. /* set usb controller driver to the device */
  64. rt_usbd_device_set_controller(udevice, (udcd_t)udc);
  65. /* create a configuration object */
  66. cfg = rt_usbd_config_create();
  67. #ifdef RT_USB_DEVICE_MSTORAGE
  68. /* create a mass storage class object */
  69. cls = rt_usbd_class_mstorage_create(udevice);
  70. /* add the class to the configuration */
  71. rt_usbd_config_add_class(cfg, cls);
  72. #endif
  73. #ifdef RT_USB_DEVICE_CDC
  74. /* create a cdc class object */
  75. cls = rt_usbd_class_cdc_create(udevice);
  76. /* add the class to the configuration */
  77. rt_usbd_config_add_class(cfg, cls);
  78. #endif
  79. #ifdef RT_USB_DEVICE_RNDIS
  80. /* create a rndis class object */
  81. cls = rt_usbd_class_rndis_create(udevice);
  82. /* add the class to the configuration */
  83. rt_usbd_config_add_class(cfg, cls);
  84. #endif
  85. /* set device descriptor to the device */
  86. #ifdef RT_USB_DEVICE_COMPOSITE
  87. rt_usbd_device_set_descriptor(udevice, &compsit_desc);
  88. #else
  89. rt_usbd_device_set_descriptor(udevice, cls->dev_desc);
  90. #endif
  91. /* add the configuration to the device */
  92. rt_usbd_device_add_config(udevice, cfg);
  93. /* set default configuration to 1 */
  94. rt_usbd_set_config(udevice, 1);
  95. /* initialize usb device controller */
  96. rt_device_init(udc);
  97. return RT_EOK;
  98. }
  99. #endif