msc_ram_template.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. static const uint8_t device_descriptor[] = {
  21. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0200, 0x01)
  22. };
  23. static const uint8_t config_descriptor[] = {
  24. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  25. MSC_DESCRIPTOR_INIT(0x00, MSC_OUT_EP, MSC_IN_EP, MSC_MAX_MPS, 0x02)
  26. };
  27. static const uint8_t device_quality_descriptor[] = {
  28. ///////////////////////////////////////
  29. /// device qualifier descriptor
  30. ///////////////////////////////////////
  31. 0x0a,
  32. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  33. 0x00,
  34. 0x02,
  35. 0x00,
  36. 0x00,
  37. 0x00,
  38. 0x40,
  39. 0x00,
  40. 0x00,
  41. };
  42. static const char *string_descriptors[] = {
  43. (const char[]){ 0x09, 0x04 }, /* Langid */
  44. "CherryUSB", /* Manufacturer */
  45. "CherryUSB MSC DEMO", /* Product */
  46. "2022123456", /* Serial Number */
  47. };
  48. static const uint8_t *device_descriptor_callback(uint8_t speed)
  49. {
  50. return device_descriptor;
  51. }
  52. static const uint8_t *config_descriptor_callback(uint8_t speed)
  53. {
  54. return config_descriptor;
  55. }
  56. static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
  57. {
  58. return device_quality_descriptor;
  59. }
  60. static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
  61. {
  62. if (index > 3) {
  63. return NULL;
  64. }
  65. return string_descriptors[index];
  66. }
  67. const struct usb_descriptor msc_ram_descriptor = {
  68. .device_descriptor_callback = device_descriptor_callback,
  69. .config_descriptor_callback = config_descriptor_callback,
  70. .device_quality_descriptor_callback = device_quality_descriptor_callback,
  71. .string_descriptor_callback = string_descriptor_callback
  72. };
  73. static void usbd_event_handler(uint8_t busid, uint8_t event)
  74. {
  75. switch (event) {
  76. case USBD_EVENT_RESET:
  77. break;
  78. case USBD_EVENT_CONNECTED:
  79. break;
  80. case USBD_EVENT_DISCONNECTED:
  81. break;
  82. case USBD_EVENT_RESUME:
  83. break;
  84. case USBD_EVENT_SUSPEND:
  85. break;
  86. case USBD_EVENT_CONFIGURED:
  87. break;
  88. case USBD_EVENT_SET_REMOTE_WAKEUP:
  89. break;
  90. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  91. break;
  92. default:
  93. break;
  94. }
  95. }
  96. #if !defined(RT_CHERRYUSB_DEVICE_TEMPLATE_MSC_BLKDEV) && !defined(PKG_CHERRYUSB_DEVICE_TEMPLATE_MSC_BLKDEV)
  97. #define BLOCK_SIZE 512
  98. #define BLOCK_COUNT 10
  99. typedef struct
  100. {
  101. uint8_t BlockSpace[BLOCK_SIZE];
  102. } BLOCK_TYPE;
  103. BLOCK_TYPE mass_block[BLOCK_COUNT];
  104. void usbd_msc_get_cap(uint8_t busid, uint8_t lun, uint32_t *block_num, uint32_t *block_size)
  105. {
  106. *block_num = 1000; //Pretend having so many buffer,not has actually.
  107. *block_size = BLOCK_SIZE;
  108. }
  109. int usbd_msc_sector_read(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length)
  110. {
  111. if (sector < BLOCK_COUNT)
  112. memcpy(buffer, mass_block[sector].BlockSpace, length);
  113. return 0;
  114. }
  115. int usbd_msc_sector_write(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length)
  116. {
  117. if (sector < BLOCK_COUNT)
  118. memcpy(mass_block[sector].BlockSpace, buffer, length);
  119. return 0;
  120. }
  121. #else
  122. #include <rtthread.h>
  123. #include <rtdevice.h>
  124. #ifndef CONFIG_USBDEV_MSC_THREAD
  125. #error "Please enable CONFIG_USBDEV_MSC_THREAD, move msc read & write from isr to thread"
  126. #endif
  127. #ifndef CONFIG_USBDEV_MSC_BLOCK_DEV_NAME
  128. #define CONFIG_USBDEV_MSC_BLOCK_DEV_NAME "sd0"
  129. #endif
  130. static rt_device_t blk_dev = RT_NULL;
  131. struct rt_device_blk_geometry geometry = { 0 };
  132. void usbd_msc_get_cap(uint8_t busid, uint8_t lun, uint32_t *block_num, uint32_t *block_size)
  133. {
  134. rt_device_control(blk_dev, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
  135. *block_num = geometry.sector_count;
  136. *block_size = geometry.bytes_per_sector;
  137. }
  138. int usbd_msc_sector_read(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length)
  139. {
  140. rt_device_read(blk_dev, sector, buffer, length / geometry.bytes_per_sector);
  141. return 0;
  142. }
  143. int usbd_msc_sector_write(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length)
  144. {
  145. rt_device_write(blk_dev, sector, buffer, length / geometry.bytes_per_sector);
  146. return 0;
  147. }
  148. #endif
  149. static struct usbd_interface intf0;
  150. void msc_ram_init(uint8_t busid, uintptr_t reg_base)
  151. {
  152. #if defined(RT_CHERRYUSB_DEVICE_TEMPLATE_MSC_BLKDEV) || defined(PKG_CHERRYUSB_DEVICE_TEMPLATE_MSC_BLKDEV)
  153. rt_err_t res;
  154. blk_dev = rt_device_find(CONFIG_USBDEV_MSC_BLOCK_DEV_NAME);
  155. RT_ASSERT(blk_dev);
  156. res = rt_device_open(blk_dev, RT_DEVICE_OFLAG_RDWR);
  157. RT_ASSERT(res == RT_EOK);
  158. #endif
  159. usbd_desc_register(busid, &msc_ram_descriptor);
  160. usbd_add_interface(busid, usbd_msc_init_intf(busid, &intf0, MSC_OUT_EP, MSC_IN_EP));
  161. usbd_initialize(busid, reg_base, usbd_event_handler);
  162. }
  163. #if defined(CONFIG_USBDEV_MSC_POLLING)
  164. void msc_ram_polling(uint8_t busid)
  165. {
  166. usbd_msc_polling(busid);
  167. }
  168. #endif