1 #include "usbd_core.h"
2 #include "usbd_msc.h"
3
4 #define MSC_IN_EP 0x81
5 #define MSC_OUT_EP 0x02
6
7 #define USBD_VID 0xFFFF
8 #define USBD_PID 0xFFFF
9 #define USBD_MAX_POWER 100
10 #define USBD_LANGID_STRING 1033
11
12 #define USB_CONFIG_SIZE (9 + MSC_DESCRIPTOR_LEN)
13
14 #ifdef CONFIG_USB_HS
15 #define MSC_MAX_MPS 512
16 #else
17 #define MSC_MAX_MPS 64
18 #endif
19
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 0x01,
93 0x00,
94 #endif
95 0x00
96 };
97
usbd_event_handler(uint8_t event)98 void usbd_event_handler(uint8_t event)
99 {
100 switch (event) {
101 case USBD_EVENT_RESET:
102 break;
103 case USBD_EVENT_CONNECTED:
104 break;
105 case USBD_EVENT_DISCONNECTED:
106 break;
107 case USBD_EVENT_RESUME:
108 break;
109 case USBD_EVENT_SUSPEND:
110 break;
111 case USBD_EVENT_CONFIGURED:
112 break;
113 case USBD_EVENT_SET_REMOTE_WAKEUP:
114 break;
115 case USBD_EVENT_CLR_REMOTE_WAKEUP:
116 break;
117
118 default:
119 break;
120 }
121 }
122
123 #define BLOCK_SIZE 512
124 #define BLOCK_COUNT 10
125
126 typedef struct
127 {
128 uint8_t BlockSpace[BLOCK_SIZE];
129 } BLOCK_TYPE;
130
131 BLOCK_TYPE mass_block[BLOCK_COUNT];
132
usbd_msc_get_cap(uint8_t lun,uint32_t * block_num,uint16_t * block_size)133 void usbd_msc_get_cap(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
134 {
135 *block_num = 1000; //Pretend having so many buffer,not has actually.
136 *block_size = BLOCK_SIZE;
137 }
usbd_msc_sector_read(uint32_t sector,uint8_t * buffer,uint32_t length)138 int usbd_msc_sector_read(uint32_t sector, uint8_t *buffer, uint32_t length)
139 {
140 if (sector < BLOCK_COUNT)
141 memcpy(buffer, mass_block[sector].BlockSpace, length);
142 return 0;
143 }
144
usbd_msc_sector_write(uint32_t sector,uint8_t * buffer,uint32_t length)145 int usbd_msc_sector_write(uint32_t sector, uint8_t *buffer, uint32_t length)
146 {
147 if (sector < BLOCK_COUNT)
148 memcpy(mass_block[sector].BlockSpace, buffer, length);
149 return 0;
150 }
151
152 struct usbd_interface intf0;
153
msc_ram_init(void)154 void msc_ram_init(void)
155 {
156 usbd_desc_register(msc_ram_descriptor);
157 usbd_add_interface(usbd_msc_init_intf(&intf0, MSC_OUT_EP, MSC_IN_EP));
158
159 usbd_initialize();
160 }
161