usbdevice.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #define RT_USB_DEVICE_CDC
  18. const static char* ustring[] =
  19. {
  20. "Language",
  21. "RT-Thread Team.",
  22. "UDISK",
  23. "12345678",
  24. "Config",
  25. "Interface",
  26. };
  27. rt_err_t rt_usb_device_init(const char* udc_name)
  28. {
  29. rt_device_t udc;
  30. udevice_t udevice;
  31. uconfig_t cfg;
  32. uclass_t cls;
  33. RT_ASSERT(udc_name != RT_NULL);
  34. udc = rt_device_find(udc_name);
  35. if(udc == RT_NULL)
  36. {
  37. rt_kprintf("can't find usb device controller %s\n", udc_name);
  38. return -RT_ERROR;
  39. }
  40. /* create and startup usb device thread */
  41. rt_usbd_core_init();
  42. /* create a device object */
  43. udevice = rt_usbd_device_create(ustring);
  44. /* set usb controller driver to the device */
  45. rt_usbd_device_set_controller(udevice, (udcd_t)udc);
  46. /* create a configuration object */
  47. cfg = rt_usbd_config_create();
  48. #if defined RT_USB_DEVICE_MASS_STORAGE
  49. /* create a mass storage class object */
  50. cls = rt_usbd_class_mass_storage_create(udevice);
  51. #elif defined RT_USB_DEVICE_CDC
  52. /* create a cdc class object */
  53. cls = rt_usbd_class_cdc_create(udevice);
  54. #else
  55. #error
  56. #endif
  57. /* set device descriptor to the device */
  58. rt_usbd_device_set_descriptor(udevice, cls->dev_desc);
  59. /* add the class to the configuration */
  60. rt_usbd_config_add_class(cfg, cls);
  61. /* add the configuration to the device */
  62. rt_usbd_device_add_config(udevice, cfg);
  63. /* set default configuration to 1 */
  64. rt_usbd_set_config(udevice, 1);
  65. /* initialize usb device controller */
  66. rt_device_init(udc);
  67. return RT_EOK;
  68. }