• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "usbd_core.h"
2 #include "usb_midi.h"
3 
4 #define MIDI_OUT_EP 0x02
5 #define MIDI_IN_EP  0x81
6 
7 #define USBD_VID           0x0d28
8 #define USBD_PID           0x0404
9 #define USBD_MAX_POWER     100
10 #define USBD_LANGID_STRING 1033
11 
12 #define USB_CONFIG_SIZE (9 + 9 + 9 + 9 + 7 + MIDI_SIZEOF_JACK_DESC + 9 + 5 + 9 + 5)
13 
14 #ifdef CONFIG_USB_HS
15 #define MIDI_EP_MPS 512
16 #else
17 #define MIDI_EP_MPS 64
18 #endif
19 
20 const uint8_t midi_descriptor[] = {
21     USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0100, 0x01),
22     USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
23     // Standard AC Interface Descriptor
24     0x09,
25     0x04,
26     0x00,
27     0x00,
28     0x00,
29     0x01,
30     0x01,
31     0x00,
32     0x00,
33     // Class-specific AC Interface Descriptor
34     0x09,
35     0x24,
36     0x01,
37     0x00,
38     0x01,
39     0x09,
40     0x00,
41     0x01,
42     0x01,
43     // MIDIStreaming Interface Descriptors
44     0x09,
45     0x04,
46     0x01,
47     0x00,
48     0x02,
49     0x01,
50     0x03,
51     0x00,
52     0x00,
53     // Class-Specific MS Interface Header Descriptor
54     0x07,
55     0x24,
56     0x01,
57     0x00,
58     0x01,
59     WBVAL(65),
60 
61     // MIDI_IN_JACK_DESCRIPTOR_INIT(MIDI_JACK_TYPE_EMBEDDED, 0x01),
62     // MIDI_IN_JACK_DESCRIPTOR_INIT(MIDI_JACK_TYPE_EXTERNAL, 0x02),
63     // MIDI_OUT_JACK_DESCRIPTOR_INIT(MIDI_JACK_TYPE_EMBEDDED, 0x03, 0x02),
64     // MIDI_OUT_JACK_DESCRIPTOR_INIT(MIDI_JACK_TYPE_EXTERNAL, 0x04, 0x01),
65     MIDI_JACK_DESCRIPTOR_INIT(0x01),
66     // OUT endpoint descriptor
67     0x09, 0x05, MIDI_OUT_EP, 0x02, WBVAL(MIDI_EP_MPS), 0x00, 0x00, 0x00,
68     0x05, 0x25, 0x01, 0x01, 0x01,
69 
70     // IN endpoint descriptor
71     0x09, 0x05, MIDI_IN_EP, 0x02, WBVAL(MIDI_EP_MPS), 0x00, 0x00, 0x00,
72     0x05, 0x25, 0x01, 0x01, 0x03,
73 
74     ///////////////////////////////////////
75     /// string0 descriptor
76     ///////////////////////////////////////
77     USB_LANGID_INIT(USBD_LANGID_STRING),
78     ///////////////////////////////////////
79     /// string1 descriptor
80     ///////////////////////////////////////
81     0x14,                       /* bLength */
82     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
83     'C', 0x00,                  /* wcChar0 */
84     'h', 0x00,                  /* wcChar1 */
85     'e', 0x00,                  /* wcChar2 */
86     'r', 0x00,                  /* wcChar3 */
87     'r', 0x00,                  /* wcChar4 */
88     'y', 0x00,                  /* wcChar5 */
89     'U', 0x00,                  /* wcChar6 */
90     'S', 0x00,                  /* wcChar7 */
91     'B', 0x00,                  /* wcChar8 */
92     ///////////////////////////////////////
93     /// string2 descriptor
94     ///////////////////////////////////////
95     0x28,                       /* bLength */
96     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
97     'C', 0x00,                  /* wcChar0 */
98     'h', 0x00,                  /* wcChar1 */
99     'e', 0x00,                  /* wcChar2 */
100     'r', 0x00,                  /* wcChar3 */
101     'r', 0x00,                  /* wcChar4 */
102     'y', 0x00,                  /* wcChar5 */
103     'U', 0x00,                  /* wcChar6 */
104     'S', 0x00,                  /* wcChar7 */
105     'B', 0x00,                  /* wcChar8 */
106     ' ', 0x00,                  /* wcChar9 */
107     'M', 0x00,                  /* wcChar10 */
108     'I', 0x00,                  /* wcChar11 */
109     'D', 0x00,                  /* wcChar12 */
110     'I', 0x00,                  /* wcChar13 */
111     ' ', 0x00,                  /* wcChar14 */
112     'D', 0x00,                  /* wcChar15 */
113     'E', 0x00,                  /* wcChar16 */
114     'M', 0x00,                  /* wcChar17 */
115     'O', 0x00,                  /* wcChar18 */
116     ///////////////////////////////////////
117     /// string3 descriptor
118     ///////////////////////////////////////
119     0x16,                       /* bLength */
120     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
121     '2', 0x00,                  /* wcChar0 */
122     '0', 0x00,                  /* wcChar1 */
123     '2', 0x00,                  /* wcChar2 */
124     '1', 0x00,                  /* wcChar3 */
125     '0', 0x00,                  /* wcChar4 */
126     '3', 0x00,                  /* wcChar5 */
127     '1', 0x00,                  /* wcChar6 */
128     '0', 0x00,                  /* wcChar7 */
129     '0', 0x00,                  /* wcChar8 */
130     '0', 0x00,                  /* wcChar9 */
131 #ifdef CONFIG_USB_HS
132     ///////////////////////////////////////
133     /// device qualifier descriptor
134     ///////////////////////////////////////
135     0x0a,
136     USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
137     0x00,
138     0x02,
139     0x02,
140     0x02,
141     0x01,
142     0x40,
143     0x01,
144     0x00,
145 #endif
146     0x00
147 };
148 
usbd_event_handler(uint8_t event)149 void usbd_event_handler(uint8_t event)
150 {
151     switch (event) {
152         case USBD_EVENT_RESET:
153             break;
154         case USBD_EVENT_CONNECTED:
155             break;
156         case USBD_EVENT_DISCONNECTED:
157             break;
158         case USBD_EVENT_RESUME:
159             break;
160         case USBD_EVENT_SUSPEND:
161             break;
162         case USBD_EVENT_CONFIGURED:
163             break;
164         case USBD_EVENT_SET_REMOTE_WAKEUP:
165             break;
166         case USBD_EVENT_CLR_REMOTE_WAKEUP:
167             break;
168 
169         default:
170             break;
171     }
172 }
173 
usbd_midi_bulk_out(uint8_t ep,uint32_t nbytes)174 void usbd_midi_bulk_out(uint8_t ep, uint32_t nbytes)
175 {
176 }
177 
usbd_midi_bulk_in(uint8_t ep,uint32_t nbytes)178 void usbd_midi_bulk_in(uint8_t ep, uint32_t nbytes)
179 {
180 }
181 
182 struct usbd_interface intf0;
183 struct usbd_interface intf1;
184 
185 struct usbd_endpoint midi_out_ep = {
186     .ep_addr = MIDI_OUT_EP,
187     .ep_cb = usbd_midi_bulk_out
188 };
189 
190 struct usbd_endpoint midi_in_ep = {
191     .ep_addr = MIDI_IN_EP,
192     .ep_cb = usbd_midi_bulk_in
193 };
194 
midi_init(void)195 void midi_init(void)
196 {
197     usbd_desc_register(midi_descriptor);
198     usbd_add_interface(&intf0);
199     usbd_add_interface(&intf1);
200     usbd_add_endpoint(&midi_out_ep);
201     usbd_add_endpoint(&midi_in_ep);
202 
203     usbd_initialize();
204 }