msc_bootuf2_template.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. 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 UF2 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_bootuf2_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. static void usbd_event_handler(uint8_t busid, uint8_t event)
  75. {
  76. switch (event) {
  77. case USBD_EVENT_RESET:
  78. break;
  79. case USBD_EVENT_CONNECTED:
  80. break;
  81. case USBD_EVENT_DISCONNECTED:
  82. break;
  83. case USBD_EVENT_RESUME:
  84. break;
  85. case USBD_EVENT_SUSPEND:
  86. break;
  87. case USBD_EVENT_CONFIGURED:
  88. bootuf2_init();
  89. break;
  90. case USBD_EVENT_SET_REMOTE_WAKEUP:
  91. break;
  92. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  93. break;
  94. default:
  95. break;
  96. }
  97. }
  98. void usbd_msc_get_cap(uint8_t busid, uint8_t lun, uint32_t *block_num, uint32_t *block_size)
  99. {
  100. *block_num = bootuf2_get_sector_count();
  101. *block_size = bootuf2_get_sector_size();
  102. USB_LOG_INFO("sector count:%d, sector size:%d\n", (unsigned int)*block_num, (unsigned int)*block_size);
  103. }
  104. int usbd_msc_sector_read(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length)
  105. {
  106. boot2uf2_read_sector(sector, buffer, length / bootuf2_get_sector_size());
  107. return 0;
  108. }
  109. int usbd_msc_sector_write(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length)
  110. {
  111. bootuf2_write_sector(sector, buffer, length / bootuf2_get_sector_size());
  112. return 0;
  113. }
  114. static struct usbd_interface intf0;
  115. void msc_bootuf2_init(uint8_t busid, uintptr_t reg_base)
  116. {
  117. boot2uf2_flash_init();
  118. usbd_desc_register(busid, &msc_bootuf2_descriptor);
  119. usbd_add_interface(busid, usbd_msc_init_intf(busid, &intf0, MSC_OUT_EP, MSC_IN_EP));
  120. usbd_initialize(busid, reg_base, usbd_event_handler);
  121. }
  122. void boot2uf2_flash_init(void)
  123. {
  124. }
  125. int bootuf2_flash_write(uint32_t address, const uint8_t *data, size_t size)
  126. {
  127. USB_LOG_INFO("address:%08x, size:%d\n", (unsigned int)address, (unsigned int)size);
  128. return 0;
  129. }