1
0

usbdevice.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifdef RT_USB_DEVICE_COMPOSITE
  19. const static char* ustring[] =
  20. {
  21. "Language",
  22. "RT-Thread Team.",
  23. "RTT Composite Device",
  24. "1.1.0",
  25. "Configuration",
  26. "Interface",
  27. };
  28. #endif
  29. #ifdef RT_USB_DEVICE_COMPOSITE
  30. static struct udevice_descriptor compsit_desc =
  31. {
  32. USB_DESC_LENGTH_DEVICE, //bLength;
  33. USB_DESC_TYPE_DEVICE, //type;
  34. USB_BCD_VERSION, //bcdUSB;
  35. USB_CLASS_MISC, //bDeviceClass;
  36. 0x02, //bDeviceSubClass;
  37. 0x01, //bDeviceProtocol;
  38. 0x40, //bMaxPacketSize0;
  39. _VENDOR_ID, //idVendor;
  40. _PRODUCT_ID, //idProduct;
  41. USB_BCD_DEVICE, //bcdDevice;
  42. USB_STRING_MANU_INDEX, //iManufacturer;
  43. USB_STRING_PRODUCT_INDEX, //iProduct;
  44. USB_STRING_SERIAL_INDEX, //iSerialNumber;
  45. USB_DYNAMIC, //bNumConfigurations;
  46. };
  47. #endif
  48. rt_err_t rt_usb_device_init(const char* udc_name)
  49. {
  50. rt_device_t udc;
  51. udevice_t udevice;
  52. uconfig_t cfg;
  53. uclass_t cls;
  54. RT_ASSERT(udc_name != RT_NULL);
  55. udc = rt_device_find(udc_name);
  56. if(udc == RT_NULL)
  57. {
  58. rt_kprintf("can't find usb device controller %s\n", udc_name);
  59. return -RT_ERROR;
  60. }
  61. /* create and startup usb device thread */
  62. rt_usbd_core_init();
  63. /* create a device object */
  64. udevice = rt_usbd_device_create();
  65. /* set usb controller driver to the device */
  66. rt_usbd_device_set_controller(udevice, (udcd_t)udc);
  67. /* create a configuration object */
  68. cfg = rt_usbd_config_create();
  69. #ifdef RT_USB_DEVICE_MSTORAGE
  70. /* create a mass storage class object */
  71. cls = rt_usbd_class_mstorage_create(udevice);
  72. /* add the class to the configuration */
  73. rt_usbd_config_add_class(cfg, cls);
  74. #endif
  75. #ifdef RT_USB_DEVICE_CDC
  76. /* create a cdc class object */
  77. cls = rt_usbd_class_cdc_create(udevice);
  78. /* add the class to the configuration */
  79. rt_usbd_config_add_class(cfg, cls);
  80. #endif
  81. #ifdef RT_USB_DEVICE_RNDIS
  82. /* create a rndis class object */
  83. cls = rt_usbd_class_rndis_create(udevice);
  84. /* add the class to the configuration */
  85. rt_usbd_config_add_class(cfg, cls);
  86. #endif
  87. /* set device descriptor to the device */
  88. #ifdef RT_USB_DEVICE_COMPOSITE
  89. rt_usbd_device_set_descriptor(udevice, &compsit_desc);
  90. rt_usbd_device_set_string(udevice, ustring);
  91. #else
  92. rt_usbd_device_set_descriptor(udevice, cls->dev_desc);
  93. #endif
  94. /* add the configuration to the device */
  95. rt_usbd_device_add_config(udevice, cfg);
  96. /* set default configuration to 1 */
  97. rt_usbd_set_config(udevice, 1);
  98. /* initialize usb device controller */
  99. rt_device_init(udc);
  100. return RT_EOK;
  101. }
  102. #endif