1
- # USB Composite library for STM32F1
1
+ # USB Composite library for Roger's Melbourne's STM32F1 core: https://github.com/rogerclarkmelbourne/Arduino_STM32/
2
2
3
3
## Protocols supported
4
4
5
5
- standard USB HID, with many built-in profiles, and customizable with more
6
6
7
7
- MIDI over USB
8
8
9
- - XBox360 controller (only controller-to-host is currently supported)
9
+ - XBox360 wired/wireless controllers
10
10
11
11
- Mass storage
12
12
13
+ - USB Audio (unidirectional, but both directions are supported)
14
+
13
15
## Basic concepts
14
16
15
17
Start with:
@@ -30,12 +32,16 @@ Plugin classes included in the library:
30
32
```
31
33
USBHID
32
34
USBMIDI
33
- USBXBox360
35
+ USBMultiXBox360<n> / USBXBox360 / USBXBox360W<n>
34
36
USBMassStorage
35
37
USBCompositeSerial
38
+ USBMultiSerial<n>
36
39
```
37
40
38
- To use them, you need to create instances of them. Currently, only one instance of each plugin class
41
+ ** NOTE:** Only one of USBMultiXBox360<n > / USBXBox360 / USBXBox360W<n > can be registered at a time:
42
+ they cannot be composited together.
43
+
44
+ To use the plugins, you need to create instances of them. NOTE: Only one instance of each plugin class
39
45
can be created.
40
46
41
47
If you want to make a simple (non-composite) USB device, you can create an instance of the plugin class
@@ -50,9 +56,15 @@ plugin2.registerComponent();
50
56
USBComposite.begin();
51
57
```
52
58
53
- Of course, you may need to do some further configuring of the plugins or the ` USBComposite ` device
59
+ Of course, you may need to do some further configuring of the plugins (e.g., if plugin1 is USBHID, then
60
+ you may want to do ` USBHID.setReportDescriptor(HID_KEYBOARD) ` ) or of the ` USBComposite ` device
54
61
before the ` USBComposite.begin() ` call.
55
62
63
+ After starting up USBComposite, it's a good idea to wait for it to become ready before sending any data:
64
+ ```
65
+ while(!USBComposite);
66
+ ```
67
+
56
68
Finally, there are a number of classes that implement particular protocols for the ` USBHID ` class plugin.
57
69
These are:
58
70
```
@@ -73,6 +85,19 @@ combinations will be supported by all operating systems.
73
85
I recommend calling ` USBComposite.setDeviceId(device) ` with a different device number for each combination
74
86
of plugins and profiles to prevent problems with cached configurations on the host computer.
75
87
88
+ ## Uploading with STM32duino bootloader
89
+
90
+ Normally, the STM32duino bootloader upload method in the Roger Melbourne STM32F1 core sends a command
91
+ to reset the board via the USB serial port, and thereby put it in bootloader mode, just prior to uploading.
92
+ If you have installed a sketch that includes a USB serial port in the composite device, this should still
93
+ work. But if the sketch you've installed doesn't include a USB serial port, then you need to manually activate
94
+ the bootloader mode next time you want to upload a sketch.
95
+
96
+ The bootloader mode is active for a short period after the board powers up or resets. So just initiate
97
+ the upload in the Arduino IDE as usual, but when "Searching for DFU device [ 1EAF:0003] " is displayed,
98
+ hit the reset button (or if the device is USB-powered, keep it unplugged from USB and plug it in when you
99
+ get this message).
100
+
76
101
## Simple USB device configuration
77
102
78
103
A simple USB device uses a single plugin. You need to create an instance of the plugin class,
@@ -83,21 +108,37 @@ to inject keyboard data, you should do:
83
108
USBHID HID; // create instance of USBHID plugin
84
109
HIDKeyboard Keyboard(HID); // create a profile
85
110
86
- HID.begin(HID_KEYBOARD );
111
+ HID.begin();
87
112
```
88
113
89
114
and then call ` Keyboard.print("TextToInject") ` to inject keyboard data. Some plugin configurations
90
115
may require further initialization code or further code that needs to be called inside the Arduino
91
116
` loop() ` function.
92
117
93
- See the ` BootKeyboard ` , ` midiout ` and ` x360 ` example code for this procedure.
118
+ See the ` BootKeyboard ` , ` midiout ` and ` x360 ` example code for variants on this procedure.
94
119
95
120
(Additionally, the ` USBHID ` plugin has a convenience ` begin() ` method that lets you include an
96
121
instance of a ` USBCompositeSerial ` plugin class, and that creates a composite HID-Serial device.)
97
122
98
123
However, if you want a USB device using more than one plugin, then you will NOT call the plugin's
99
124
` begin() ` method.
100
125
126
+ Note that a single HID plugin can support a device with multiple report profiles including a keyboard, several joysticks,
127
+ a mouse, etc.:
128
+ ```
129
+ USBHID HID; // create instance of USBHID plugin
130
+ HIDKeyboard Keyboard(HID); // create a profile
131
+ HIDJoystick Joystick1(HID); // create a profile
132
+ HIDJoystick Joystick2(HID); // create a profile
133
+ HIDMouse Mouse(HID); // create a profile
134
+
135
+ HID.begin();
136
+ ```
137
+
138
+ Each of the profiles (e.g., Joystick1) contributes a part of the HID report descriptor to USBHID which automatically stitches
139
+ them together and assigns report IDs. However, you can also make a single overarching custom HID report descriptor and include
140
+ it in the HID.begin() call. The ` softjoystick ` example does this.
141
+
101
142
## Memory limitations
102
143
103
144
There are 320 bytes of hardware buffer memory available after endpoint 0 is taken into account. The following
@@ -132,25 +173,32 @@ MIDI.setTXPacketSize(size);
132
173
```
133
174
The maximum and default packet size is 64. Smaller packet sizes have not been thoroughly tested and may slow things down. In
134
175
particular, for HID you should make sure your packet size is sufficient for your largest HID report. The CompositeSerial
135
- device also has a control channel whose 16 byte packet size is not adjustable.
176
+ device also has a control channel whose 16 byte packet size is not adjustable. Note that for reasons that I do not currently
177
+ understand, CompositeSerial RX packets must be a power of two in size.
136
178
137
- Note that in the above, RX and TX are from the point of view of the MCU, not the host (i.e., RX corresponds to USB Out and TX
179
+ Note also that in the above, RX and TX are from the point of view of the MCU, not the host (i.e., RX corresponds to USB Out and TX
138
180
to USB In).
139
181
140
182
## Endpoint limitations
141
183
142
- There is one bidirectional endpoint 0 that all endpoints share, and the hardware allows for seven more. Here are
143
- how many endpoints besides endpoint 0 are needed for each plugin:
184
+ There is one bidirectional endpoint 0 that all endpoints share, and the hardware allows for seven more in each direction,
185
+ but there are some complications in that the same endpoint number when used in different directions must have some
186
+ of the same parameters. The USBComposite library takes care of these complications when allocating endpoints, but if you
187
+ have too many plugins, you USBComposite.begin() will return ` false ` to indicate that you've used up too many.
188
+
189
+ This is pretty complicated, but a rule of thumb for having enough endpoints is to make sure that when you add up the
190
+ following contributions for the plugins you use, your total is at most seven.
191
+
192
+ * USB Serial: 2 (= 2 TX, 1 RX)
144
193
145
- * USB Serial: 3
194
+ * USB HID: 1 (= 1 TX)
146
195
147
- * USB HID : 1
196
+ * USB Mass Storage : 1 (= 1 TX, 1 RX)
148
197
149
- * USB Mass Storage: 2
198
+ * USB MIDI: 1 (= 1 TX, 1 RX)
150
199
151
- * USB MIDI: 2
200
+ * XBox360 Controller: 1 per controller (= 1 TX, 1 RX)
152
201
153
- * XBox360 Controller: 2
202
+ * USB Audio: 1 (= 1 TX or 1 RX depending on mode)
154
203
155
- When combining plugins, make sure the count of these endpoints does not exceed 7. For instance, USB Serial + USB Mass Storage +
156
- USB MIDI + USB HID adds up to 8, which is too much.
204
+ * USB Multi Serial: 2 per port (= 2 TX, 1 RX)
0 commit comments