msc_ram_template.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "usbd_core.h"
  2. #include "usbd_msc.h"
  3. #define MSC_IN_EP 0x81
  4. #define MSC_OUT_EP 0x02
  5. #define USBD_VID 0xFFFF
  6. #define USBD_PID 0xFFFF
  7. #define USBD_MAX_POWER 100
  8. #define USBD_LANGID_STRING 1033
  9. #define USB_CONFIG_SIZE (9 + MSC_DESCRIPTOR_LEN)
  10. const uint8_t msc_ram_descriptor[] = {
  11. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0200, 0x01),
  12. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  13. MSC_DESCRIPTOR_INIT(0x00, MSC_OUT_EP, MSC_IN_EP, 0x02),
  14. ///////////////////////////////////////
  15. /// string0 descriptor
  16. ///////////////////////////////////////
  17. USB_LANGID_INIT(USBD_LANGID_STRING),
  18. ///////////////////////////////////////
  19. /// string1 descriptor
  20. ///////////////////////////////////////
  21. 0x14, /* bLength */
  22. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  23. 'C', 0x00, /* wcChar0 */
  24. 'h', 0x00, /* wcChar1 */
  25. 'e', 0x00, /* wcChar2 */
  26. 'r', 0x00, /* wcChar3 */
  27. 'r', 0x00, /* wcChar4 */
  28. 'y', 0x00, /* wcChar5 */
  29. 'U', 0x00, /* wcChar6 */
  30. 'S', 0x00, /* wcChar7 */
  31. 'B', 0x00, /* wcChar8 */
  32. ///////////////////////////////////////
  33. /// string2 descriptor
  34. ///////////////////////////////////////
  35. 0x26, /* bLength */
  36. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  37. 'C', 0x00, /* wcChar0 */
  38. 'h', 0x00, /* wcChar1 */
  39. 'e', 0x00, /* wcChar2 */
  40. 'r', 0x00, /* wcChar3 */
  41. 'r', 0x00, /* wcChar4 */
  42. 'y', 0x00, /* wcChar5 */
  43. 'U', 0x00, /* wcChar6 */
  44. 'S', 0x00, /* wcChar7 */
  45. 'B', 0x00, /* wcChar8 */
  46. ' ', 0x00, /* wcChar9 */
  47. 'M', 0x00, /* wcChar10 */
  48. 'S', 0x00, /* wcChar11 */
  49. 'C', 0x00, /* wcChar12 */
  50. ' ', 0x00, /* wcChar13 */
  51. 'D', 0x00, /* wcChar14 */
  52. 'E', 0x00, /* wcChar15 */
  53. 'M', 0x00, /* wcChar16 */
  54. 'O', 0x00, /* wcChar17 */
  55. ///////////////////////////////////////
  56. /// string3 descriptor
  57. ///////////////////////////////////////
  58. 0x16, /* bLength */
  59. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  60. '2', 0x00, /* wcChar0 */
  61. '0', 0x00, /* wcChar1 */
  62. '2', 0x00, /* wcChar2 */
  63. '2', 0x00, /* wcChar3 */
  64. '1', 0x00, /* wcChar4 */
  65. '2', 0x00, /* wcChar5 */
  66. '3', 0x00, /* wcChar6 */
  67. '4', 0x00, /* wcChar7 */
  68. '5', 0x00, /* wcChar8 */
  69. '6', 0x00, /* wcChar9 */
  70. #ifdef CONFIG_USB_HS
  71. ///////////////////////////////////////
  72. /// device qualifier descriptor
  73. ///////////////////////////////////////
  74. 0x0a,
  75. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  76. 0x00,
  77. 0x02,
  78. 0x00,
  79. 0x00,
  80. 0x00,
  81. 0x40,
  82. 0x01,
  83. 0x00,
  84. #endif
  85. 0x00
  86. };
  87. #define BLOCK_SIZE 512
  88. #define BLOCK_COUNT 10
  89. typedef struct
  90. {
  91. uint8_t BlockSpace[BLOCK_SIZE];
  92. } BLOCK_TYPE;
  93. BLOCK_TYPE mass_block[BLOCK_COUNT];
  94. void usbd_msc_get_cap(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
  95. {
  96. *block_num = 1000; //Pretend having so many buffer,not has actually.
  97. *block_size = BLOCK_SIZE;
  98. }
  99. int usbd_msc_sector_read(uint32_t sector, uint8_t *buffer, uint32_t length)
  100. {
  101. if (sector < 10)
  102. memcpy(buffer, mass_block[sector].BlockSpace, length);
  103. return 0;
  104. }
  105. int usbd_msc_sector_write(uint32_t sector, uint8_t *buffer, uint32_t length)
  106. {
  107. if (sector < 10)
  108. memcpy(mass_block[sector].BlockSpace, buffer, length);
  109. return 0;
  110. }
  111. /* function ------------------------------------------------------------------*/
  112. /**
  113. * @brief msc ram init
  114. * @pre none
  115. * @param[in] none
  116. * @retval none
  117. */
  118. void msc_ram_init(void)
  119. {
  120. usbd_desc_register(msc_ram_descriptor);
  121. usbd_msc_class_init(MSC_OUT_EP, MSC_IN_EP);
  122. usbd_initialize();
  123. }