usbdevice.c 3.0 KB

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