msc_storage_template.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbd_core.h"
  7. #include "usbd_msc.h"
  8. #ifdef __RT_THREAD_H__
  9. #define MSC_IN_EP 0x81
  10. #define MSC_OUT_EP 0x02
  11. #define USBD_VID 0xFFFF
  12. #define USBD_PID 0xFFFF
  13. #define USBD_MAX_POWER 100
  14. #define USBD_LANGID_STRING 1033
  15. #define USB_CONFIG_SIZE (9 + MSC_DESCRIPTOR_LEN)
  16. #ifdef CONFIG_USB_HS
  17. #define MSC_MAX_MPS 512
  18. #else
  19. #define MSC_MAX_MPS 64
  20. #endif
  21. const uint8_t msc_storage_descriptor[] = {
  22. USB_DEVICE_DESCRIPTOR_INIT(USB_1_1, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0200, 0x01),
  23. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  24. MSC_DESCRIPTOR_INIT(0x00, MSC_OUT_EP, MSC_IN_EP, MSC_MAX_MPS, 0x02),
  25. ///////////////////////////////////////
  26. /// string0 descriptor
  27. ///////////////////////////////////////
  28. USB_LANGID_INIT(USBD_LANGID_STRING),
  29. ///////////////////////////////////////
  30. /// string1 descriptor
  31. ///////////////////////////////////////
  32. 0x14, /* bLength */
  33. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  34. 'C', 0x00, /* wcChar0 */
  35. 'h', 0x00, /* wcChar1 */
  36. 'e', 0x00, /* wcChar2 */
  37. 'r', 0x00, /* wcChar3 */
  38. 'r', 0x00, /* wcChar4 */
  39. 'y', 0x00, /* wcChar5 */
  40. 'U', 0x00, /* wcChar6 */
  41. 'S', 0x00, /* wcChar7 */
  42. 'B', 0x00, /* wcChar8 */
  43. ///////////////////////////////////////
  44. /// string2 descriptor
  45. ///////////////////////////////////////
  46. 0x26, /* bLength */
  47. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  48. 'C', 0x00, /* wcChar0 */
  49. 'h', 0x00, /* wcChar1 */
  50. 'e', 0x00, /* wcChar2 */
  51. 'r', 0x00, /* wcChar3 */
  52. 'r', 0x00, /* wcChar4 */
  53. 'y', 0x00, /* wcChar5 */
  54. 'U', 0x00, /* wcChar6 */
  55. 'S', 0x00, /* wcChar7 */
  56. 'B', 0x00, /* wcChar8 */
  57. ' ', 0x00, /* wcChar9 */
  58. 'M', 0x00, /* wcChar10 */
  59. 'S', 0x00, /* wcChar11 */
  60. 'C', 0x00, /* wcChar12 */
  61. ' ', 0x00, /* wcChar13 */
  62. 'D', 0x00, /* wcChar14 */
  63. 'E', 0x00, /* wcChar15 */
  64. 'M', 0x00, /* wcChar16 */
  65. 'O', 0x00, /* wcChar17 */
  66. ///////////////////////////////////////
  67. /// string3 descriptor
  68. ///////////////////////////////////////
  69. 0x16, /* bLength */
  70. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  71. '2', 0x00, /* wcChar0 */
  72. '0', 0x00, /* wcChar1 */
  73. '2', 0x00, /* wcChar2 */
  74. '2', 0x00, /* wcChar3 */
  75. '1', 0x00, /* wcChar4 */
  76. '2', 0x00, /* wcChar5 */
  77. '3', 0x00, /* wcChar6 */
  78. '4', 0x00, /* wcChar7 */
  79. '5', 0x00, /* wcChar8 */
  80. '6', 0x00, /* wcChar9 */
  81. #ifdef CONFIG_USB_HS
  82. ///////////////////////////////////////
  83. /// device qualifier descriptor
  84. ///////////////////////////////////////
  85. 0x0a,
  86. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  87. 0x00,
  88. 0x02,
  89. 0x00,
  90. 0x00,
  91. 0x00,
  92. 0x40,
  93. 0x00,
  94. 0x00,
  95. #endif
  96. 0x00
  97. };
  98. struct usbd_interface intf0;
  99. /* assume the block device is 512M */
  100. #define BLOCK_DEV_NAME "sd0"
  101. #define BLOCK_SIZE 512U
  102. #define BLOCK_COUNT 0x1024U * 0x1024U
  103. static rt_device_t blk_dev = RT_NULL;
  104. static void usbd_event_handler(uint8_t busid, uint8_t event)
  105. {
  106. switch (event) {
  107. case USBD_EVENT_RESET:
  108. break;
  109. case USBD_EVENT_CONNECTED:
  110. break;
  111. case USBD_EVENT_DISCONNECTED:
  112. break;
  113. case USBD_EVENT_RESUME:
  114. break;
  115. case USBD_EVENT_SUSPEND:
  116. break;
  117. case USBD_EVENT_CONFIGURED:
  118. break;
  119. case USBD_EVENT_SET_REMOTE_WAKEUP:
  120. break;
  121. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  122. break;
  123. default:
  124. break;
  125. }
  126. }
  127. void usbd_msc_get_cap(uint8_t busid, uint8_t lun, uint32_t *block_num, uint32_t *block_size)
  128. {
  129. *block_num = BLOCK_COUNT;
  130. *block_size = BLOCK_SIZE;
  131. }
  132. int usbd_msc_sector_read(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length)
  133. {
  134. rt_device_read(blk_dev, sector, buffer, length / BLOCK_SIZE);
  135. return 0;
  136. }
  137. int usbd_msc_sector_write(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length)
  138. {
  139. rt_device_write(blk_dev, sector, buffer, length / BLOCK_SIZE);
  140. return 0;
  141. }
  142. void msc_storage_init(uint8_t busid, uintptr_t reg_base)
  143. {
  144. rt_err_t res;
  145. blk_dev = rt_device_find(BLOCK_DEV_NAME);
  146. RT_ASSERT(blk_dev);
  147. res = rt_device_open(blk_dev, RT_DEVICE_OFLAG_RDWR);
  148. RT_ASSERT(res == RT_EOK);
  149. usbd_desc_register(busid, msc_storage_descriptor);
  150. usbd_add_interface(busid, usbd_msc_init_intf(busid, &intf0, MSC_OUT_EP, MSC_IN_EP));
  151. usbd_initialize(busid, reg_base, usbd_event_handler);
  152. }
  153. #endif