msc_ram_template.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. #define MSC_IN_EP 0x81
  9. #define MSC_OUT_EP 0x02
  10. #define USBD_VID 0xFFFF
  11. #define USBD_PID 0xFFFF
  12. #define USBD_MAX_POWER 100
  13. #define USBD_LANGID_STRING 1033
  14. #define USB_CONFIG_SIZE (9 + MSC_DESCRIPTOR_LEN)
  15. #ifdef CONFIG_USB_HS
  16. #define MSC_MAX_MPS 512
  17. #else
  18. #define MSC_MAX_MPS 64
  19. #endif
  20. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  21. static const uint8_t device_descriptor[] = {
  22. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0200, 0x01)
  23. };
  24. static const uint8_t config_descriptor[] = {
  25. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  26. MSC_DESCRIPTOR_INIT(0x00, MSC_OUT_EP, MSC_IN_EP, MSC_MAX_MPS, 0x02)
  27. };
  28. static const uint8_t device_quality_descriptor[] = {
  29. ///////////////////////////////////////
  30. /// device qualifier descriptor
  31. ///////////////////////////////////////
  32. 0x0a,
  33. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  34. 0x00,
  35. 0x02,
  36. 0x00,
  37. 0x00,
  38. 0x00,
  39. 0x40,
  40. 0x00,
  41. 0x00,
  42. };
  43. static const char *string_descriptors[] = {
  44. (const char[]){ 0x09, 0x04 }, /* Langid */
  45. "CherryUSB", /* Manufacturer */
  46. "CherryUSB MSC DEMO", /* Product */
  47. "2022123456", /* Serial Number */
  48. };
  49. static const uint8_t *device_descriptor_callback(uint8_t speed)
  50. {
  51. return device_descriptor;
  52. }
  53. static const uint8_t *config_descriptor_callback(uint8_t speed)
  54. {
  55. return config_descriptor;
  56. }
  57. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  58. {
  59. return device_quality_descriptor;
  60. }
  61. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  62. {
  63. if (index > 3) {
  64. return NULL;
  65. }
  66. return string_descriptors[index];
  67. }
  68. const struct usb_descriptor msc_ram_descriptor = {
  69. .device_descriptor_callback = device_descriptor_callback,
  70. .config_descriptor_callback = config_descriptor_callback,
  71. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  72. .string_descriptor_callback = string_descriptor_callback
  73. };
  74. #else
  75. const uint8_t msc_ram_descriptor[] = {
  76. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0200, 0x01),
  77. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  78. MSC_DESCRIPTOR_INIT(0x00, MSC_OUT_EP, MSC_IN_EP, MSC_MAX_MPS, 0x02),
  79. ///////////////////////////////////////
  80. /// string0 descriptor
  81. ///////////////////////////////////////
  82. USB_LANGID_INIT(USBD_LANGID_STRING),
  83. ///////////////////////////////////////
  84. /// string1 descriptor
  85. ///////////////////////////////////////
  86. 0x14, /* bLength */
  87. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  88. 'C', 0x00, /* wcChar0 */
  89. 'h', 0x00, /* wcChar1 */
  90. 'e', 0x00, /* wcChar2 */
  91. 'r', 0x00, /* wcChar3 */
  92. 'r', 0x00, /* wcChar4 */
  93. 'y', 0x00, /* wcChar5 */
  94. 'U', 0x00, /* wcChar6 */
  95. 'S', 0x00, /* wcChar7 */
  96. 'B', 0x00, /* wcChar8 */
  97. ///////////////////////////////////////
  98. /// string2 descriptor
  99. ///////////////////////////////////////
  100. 0x26, /* bLength */
  101. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  102. 'C', 0x00, /* wcChar0 */
  103. 'h', 0x00, /* wcChar1 */
  104. 'e', 0x00, /* wcChar2 */
  105. 'r', 0x00, /* wcChar3 */
  106. 'r', 0x00, /* wcChar4 */
  107. 'y', 0x00, /* wcChar5 */
  108. 'U', 0x00, /* wcChar6 */
  109. 'S', 0x00, /* wcChar7 */
  110. 'B', 0x00, /* wcChar8 */
  111. ' ', 0x00, /* wcChar9 */
  112. 'M', 0x00, /* wcChar10 */
  113. 'S', 0x00, /* wcChar11 */
  114. 'C', 0x00, /* wcChar12 */
  115. ' ', 0x00, /* wcChar13 */
  116. 'D', 0x00, /* wcChar14 */
  117. 'E', 0x00, /* wcChar15 */
  118. 'M', 0x00, /* wcChar16 */
  119. 'O', 0x00, /* wcChar17 */
  120. ///////////////////////////////////////
  121. /// string3 descriptor
  122. ///////////////////////////////////////
  123. 0x16, /* bLength */
  124. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  125. '2', 0x00, /* wcChar0 */
  126. '0', 0x00, /* wcChar1 */
  127. '2', 0x00, /* wcChar2 */
  128. '2', 0x00, /* wcChar3 */
  129. '1', 0x00, /* wcChar4 */
  130. '2', 0x00, /* wcChar5 */
  131. '3', 0x00, /* wcChar6 */
  132. '4', 0x00, /* wcChar7 */
  133. '5', 0x00, /* wcChar8 */
  134. '6', 0x00, /* wcChar9 */
  135. #ifdef CONFIG_USB_HS
  136. ///////////////////////////////////////
  137. /// device qualifier descriptor
  138. ///////////////////////////////////////
  139. 0x0a,
  140. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  141. 0x00,
  142. 0x02,
  143. 0x00,
  144. 0x00,
  145. 0x00,
  146. 0x40,
  147. 0x00,
  148. 0x00,
  149. #endif
  150. 0x00
  151. };
  152. #endif
  153. static void usbd_event_handler(uint8_t busid, uint8_t event)
  154. {
  155. switch (event) {
  156. case USBD_EVENT_RESET:
  157. break;
  158. case USBD_EVENT_CONNECTED:
  159. break;
  160. case USBD_EVENT_DISCONNECTED:
  161. break;
  162. case USBD_EVENT_RESUME:
  163. break;
  164. case USBD_EVENT_SUSPEND:
  165. break;
  166. case USBD_EVENT_CONFIGURED:
  167. break;
  168. case USBD_EVENT_SET_REMOTE_WAKEUP:
  169. break;
  170. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  171. break;
  172. default:
  173. break;
  174. }
  175. }
  176. #define BLOCK_SIZE 512
  177. #define BLOCK_COUNT 10
  178. typedef struct
  179. {
  180. uint8_t BlockSpace[BLOCK_SIZE];
  181. } BLOCK_TYPE;
  182. BLOCK_TYPE mass_block[BLOCK_COUNT];
  183. void usbd_msc_get_cap(uint8_t busid, uint8_t lun, uint32_t *block_num, uint32_t *block_size)
  184. {
  185. *block_num = 1000; //Pretend having so many buffer,not has actually.
  186. *block_size = BLOCK_SIZE;
  187. }
  188. int usbd_msc_sector_read(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length)
  189. {
  190. if (sector < BLOCK_COUNT)
  191. memcpy(buffer, mass_block[sector].BlockSpace, length);
  192. return 0;
  193. }
  194. int usbd_msc_sector_write(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length)
  195. {
  196. if (sector < BLOCK_COUNT)
  197. memcpy(mass_block[sector].BlockSpace, buffer, length);
  198. return 0;
  199. }
  200. static struct usbd_interface intf0;
  201. void msc_ram_init(uint8_t busid, uintptr_t reg_base)
  202. {
  203. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  204. usbd_desc_register(busid, &msc_ram_descriptor);
  205. #else
  206. usbd_desc_register(busid, msc_ram_descriptor);
  207. #endif
  208. usbd_add_interface(busid, usbd_msc_init_intf(busid, &intf0, MSC_OUT_EP, MSC_IN_EP));
  209. usbd_initialize(busid, reg_base, usbd_event_handler);
  210. }
  211. #if defined(CONFIG_USBDEV_MSC_POLLING)
  212. void msc_ram_polling(uint8_t busid)
  213. {
  214. usbd_msc_polling(busid);
  215. }
  216. #endif