msc_ram_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. #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. const uint8_t msc_ram_descriptor[] = {
  21. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0200, 0x01),
  22. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  23. MSC_DESCRIPTOR_INIT(0x00, MSC_OUT_EP, MSC_IN_EP, MSC_MAX_MPS, 0x02),
  24. ///////////////////////////////////////
  25. /// string0 descriptor
  26. ///////////////////////////////////////
  27. USB_LANGID_INIT(USBD_LANGID_STRING),
  28. ///////////////////////////////////////
  29. /// string1 descriptor
  30. ///////////////////////////////////////
  31. 0x14, /* bLength */
  32. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  33. 'C', 0x00, /* wcChar0 */
  34. 'h', 0x00, /* wcChar1 */
  35. 'e', 0x00, /* wcChar2 */
  36. 'r', 0x00, /* wcChar3 */
  37. 'r', 0x00, /* wcChar4 */
  38. 'y', 0x00, /* wcChar5 */
  39. 'U', 0x00, /* wcChar6 */
  40. 'S', 0x00, /* wcChar7 */
  41. 'B', 0x00, /* wcChar8 */
  42. ///////////////////////////////////////
  43. /// string2 descriptor
  44. ///////////////////////////////////////
  45. 0x26, /* bLength */
  46. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  47. 'C', 0x00, /* wcChar0 */
  48. 'h', 0x00, /* wcChar1 */
  49. 'e', 0x00, /* wcChar2 */
  50. 'r', 0x00, /* wcChar3 */
  51. 'r', 0x00, /* wcChar4 */
  52. 'y', 0x00, /* wcChar5 */
  53. 'U', 0x00, /* wcChar6 */
  54. 'S', 0x00, /* wcChar7 */
  55. 'B', 0x00, /* wcChar8 */
  56. ' ', 0x00, /* wcChar9 */
  57. 'M', 0x00, /* wcChar10 */
  58. 'S', 0x00, /* wcChar11 */
  59. 'C', 0x00, /* wcChar12 */
  60. ' ', 0x00, /* wcChar13 */
  61. 'D', 0x00, /* wcChar14 */
  62. 'E', 0x00, /* wcChar15 */
  63. 'M', 0x00, /* wcChar16 */
  64. 'O', 0x00, /* wcChar17 */
  65. ///////////////////////////////////////
  66. /// string3 descriptor
  67. ///////////////////////////////////////
  68. 0x16, /* bLength */
  69. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  70. '2', 0x00, /* wcChar0 */
  71. '0', 0x00, /* wcChar1 */
  72. '2', 0x00, /* wcChar2 */
  73. '2', 0x00, /* wcChar3 */
  74. '1', 0x00, /* wcChar4 */
  75. '2', 0x00, /* wcChar5 */
  76. '3', 0x00, /* wcChar6 */
  77. '4', 0x00, /* wcChar7 */
  78. '5', 0x00, /* wcChar8 */
  79. '6', 0x00, /* wcChar9 */
  80. #ifdef CONFIG_USB_HS
  81. ///////////////////////////////////////
  82. /// device qualifier descriptor
  83. ///////////////////////////////////////
  84. 0x0a,
  85. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  86. 0x00,
  87. 0x02,
  88. 0x00,
  89. 0x00,
  90. 0x00,
  91. 0x40,
  92. 0x00,
  93. 0x00,
  94. #endif
  95. 0x00
  96. };
  97. static void usbd_event_handler(uint8_t busid, uint8_t event)
  98. {
  99. switch (event) {
  100. case USBD_EVENT_RESET:
  101. break;
  102. case USBD_EVENT_CONNECTED:
  103. break;
  104. case USBD_EVENT_DISCONNECTED:
  105. break;
  106. case USBD_EVENT_RESUME:
  107. break;
  108. case USBD_EVENT_SUSPEND:
  109. break;
  110. case USBD_EVENT_CONFIGURED:
  111. break;
  112. case USBD_EVENT_SET_REMOTE_WAKEUP:
  113. break;
  114. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  115. break;
  116. default:
  117. break;
  118. }
  119. }
  120. #define BLOCK_SIZE 512
  121. #define BLOCK_COUNT 10
  122. typedef struct
  123. {
  124. uint8_t BlockSpace[BLOCK_SIZE];
  125. } BLOCK_TYPE;
  126. BLOCK_TYPE mass_block[BLOCK_COUNT];
  127. void usbd_msc_get_cap(uint8_t busid, uint8_t lun, uint32_t *block_num, uint32_t *block_size)
  128. {
  129. *block_num = 1000; //Pretend having so many buffer,not has actually.
  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. if (sector < BLOCK_COUNT)
  135. memcpy(buffer, mass_block[sector].BlockSpace, length);
  136. return 0;
  137. }
  138. int usbd_msc_sector_write(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length)
  139. {
  140. if (sector < BLOCK_COUNT)
  141. memcpy(mass_block[sector].BlockSpace, buffer, length);
  142. return 0;
  143. }
  144. static struct usbd_interface intf0;
  145. void msc_ram_init(uint8_t busid, uintptr_t reg_base)
  146. {
  147. usbd_desc_register(busid, msc_ram_descriptor);
  148. usbd_add_interface(busid, usbd_msc_init_intf(busid, &intf0, MSC_OUT_EP, MSC_IN_EP));
  149. usbd_initialize(busid, reg_base, usbd_event_handler);
  150. }
  151. #if defined(CONFIG_USBDEV_MSC_POLLING)
  152. void msc_ram_polling(uint8_t busid)
  153. {
  154. usbd_msc_polling(busid);
  155. }
  156. #endif