usbdevice.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * File : usbdevice.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2012-10-02 Yi Qiu first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #include <rtservice.h>
  27. #ifdef RT_USING_USB_DEVICE
  28. #define USB_DEVICE_CONTROLLER_NAME "usbd"
  29. #ifdef RT_USB_DEVICE_COMPOSITE
  30. const static char* ustring[] =
  31. {
  32. "Language",
  33. "RT-Thread Team.",
  34. "RTT Composite Device",
  35. "320219198301",
  36. "Configuration",
  37. "Interface",
  38. };
  39. #endif
  40. #ifdef RT_USB_DEVICE_COMPOSITE
  41. static struct udevice_descriptor compsit_desc =
  42. {
  43. USB_DESC_LENGTH_DEVICE, //bLength;
  44. USB_DESC_TYPE_DEVICE, //type;
  45. USB_BCD_VERSION, //bcdUSB;
  46. USB_CLASS_MISC, //bDeviceClass;
  47. 0x02, //bDeviceSubClass;
  48. 0x01, //bDeviceProtocol;
  49. 0x40, //bMaxPacketSize0;
  50. _VENDOR_ID, //idVendor;
  51. _PRODUCT_ID, //idProduct;
  52. USB_BCD_DEVICE, //bcdDevice;
  53. USB_STRING_MANU_INDEX, //iManufacturer;
  54. USB_STRING_PRODUCT_INDEX, //iProduct;
  55. USB_STRING_SERIAL_INDEX, //iSerialNumber;
  56. USB_DYNAMIC, //bNumConfigurations;
  57. };
  58. #endif
  59. rt_err_t rt_usb_device_init(void)
  60. {
  61. rt_device_t udc;
  62. udevice_t udevice;
  63. uconfig_t cfg;
  64. ufunction_t func;
  65. /* create and startup usb device thread */
  66. rt_usbd_core_init();
  67. /* create a device object */
  68. udevice = rt_usbd_device_new();
  69. udc = rt_device_find(USB_DEVICE_CONTROLLER_NAME);
  70. if(udc == RT_NULL)
  71. {
  72. rt_kprintf("can't find usb device controller %s\n", USB_DEVICE_CONTROLLER_NAME);
  73. return -RT_ERROR;
  74. }
  75. /* set usb controller driver to the device */
  76. rt_usbd_device_set_controller(udevice, (udcd_t)udc);
  77. /* create a configuration object */
  78. cfg = rt_usbd_config_new();
  79. #ifdef RT_USB_DEVICE_MSTORAGE
  80. /* create a mass storage function object */
  81. func = rt_usbd_function_mstorage_create(udevice);
  82. /* add the function to the configuration */
  83. rt_usbd_config_add_function(cfg, func);
  84. #endif
  85. #ifdef RT_USB_DEVICE_CDC
  86. /* create a cdc function object */
  87. func = rt_usbd_function_cdc_create(udevice);
  88. /* add the function to the configuration */
  89. rt_usbd_config_add_function(cfg, func);
  90. #endif
  91. #ifdef RT_USB_DEVICE_RNDIS
  92. /* create a rndis function object */
  93. func = rt_usbd_function_rndis_create(udevice);
  94. /* add the function to the configuration */
  95. rt_usbd_config_add_function(cfg, func);
  96. #endif
  97. /* set device descriptor to the device */
  98. #ifdef RT_USB_DEVICE_COMPOSITE
  99. rt_usbd_device_set_descriptor(udevice, &compsit_desc);
  100. rt_usbd_device_set_string(udevice, ustring);
  101. #else
  102. rt_usbd_device_set_descriptor(udevice, func->dev_desc);
  103. #endif
  104. /* add the configuration to the device */
  105. rt_usbd_device_add_config(udevice, cfg);
  106. /* initialize usb device controller */
  107. rt_device_init(udc);
  108. /* set default configuration to 1 */
  109. rt_usbd_set_config(udevice, 1);
  110. return RT_EOK;
  111. }
  112. #endif