msc_bootuf2_template.c 7.1 KB

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