You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Added support for F4 and G4 boards.
Serial aliased to SerialTinyUSB.
USB initialisation, task polling, CDC flushing, and DFU bootloader entry added.
added .txt file with board info to paste to boards.txt
This port adds STM32F4 support to the [Adafruit TinyUSB Arduino Library](https://github.com/adafruit/Adafruit_TinyUSB_Arduino), enabling advanced USB device functionality (MIDI, HID, MSC, etc.) on STM32 microcontrollers.
3
+
> **Note:** An earlier version of this port is integrated into the official Adafruit TinyUSB library. I will submit the updated version once it is ready and hopefully get the changes made in the stm32 core files so that manual editing of boards.txt is not required.
4
+
5
+
This port adds STM32F1, STM32F4, and STM32G4 support to the [Adafruit TinyUSB Arduino Library](https://github.com/adafruit/Adafruit_TinyUSB_Arduino), enabling advanced USB device functionality (MIDI, HID, MSC, CDC, etc.) on STM32 microcontrollers.
6
+
7
+
## What's New
8
+
9
+
Whilst this port should still be considered experimental, it is now a lot more fleshed out than previous versions:
10
+
11
+
```cpp
12
+
// Previously required in setup():
13
+
if (!TinyUSBDevice.isInitialized()) { TinyUSBDevice.begin(0); }
14
+
if (TinyUSBDevice.mounted()) { TinyUSBDevice.detach(); delay(10); TinyUSBDevice.attach(); }
15
+
16
+
// Previously required in loop():
17
+
#ifdef TINYUSB_NEED_POLLING_TASK
18
+
TinyUSBDevice.task();
19
+
#endif
20
+
```
21
+
22
+
This version eliminates all of that. USB initialisation, task polling, CDC flushing, and DFU bootloader entry are all handled automatically — sketches work the same way as on officially supported cores like RP2040 and SAMD. `Serial` is now also automatically aliased to `SerialTinyUSB`, so no `#define` is needed in your sketch, `Serial` commands work like they should. And most importantly no need to press the BOOT and RESET buttons when uploading anymore!
23
+
24
+
Support has been added and tested for three STM32 families: **F1**, **F4**, and **G4**.
4
25
5
26
## Compatibility
6
27
7
28
### Tested and Working
8
-
- STM32F411 BlackPill (WeAct Studio)
29
+
| Board | MCU | Family |
30
+
|---|---|---|
31
+
| WeAct STM32F411 BlackPill | STM32F411 | F4 |
32
+
| STM32F103 BluePill | STM32F103 | F1 |
33
+
| WeAct STM32G431 | STM32G431 | G4 |
9
34
10
-
### Should Work (but untested)
11
-
- Other STM32F4 series (F401, F405, F407, F446, etc.) - uses same USB peripheral
12
-
- STM32F7 series - similar OTG_FS peripheral
35
+
### Should Work (untested)
36
+
- **STM32F4 series:** F401, F405, F407, F446 and other F4xx variants — same OTG_FS peripheral
37
+
- **STM32F1 series:** Other F103 variants — same USB peripheral
38
+
- **STM32G4 series:** G474, G491, G4A1 — same USB peripheral
13
39
14
-
### Won't Work Without Modification
15
-
- STM32F0, F1, F3 series - different USB peripheral architecture
16
-
- STM32L series - may need different configuration
17
-
- STM32H7 - may need clock configuration changes
40
+
### Not Supported (yet)
41
+
- STM32F0, F2, F3 — different USB peripheral architecture
42
+
- STM32L, STM32H7 — would need clock configuration changes
18
43
19
44
**If you test on other boards, please report your results!**
20
45
21
46
## Features
22
47
- ✅ USB MIDI
23
48
- ✅ USB CDC (Virtual Serial Port)
24
-
- ✅ USB HID (Keyboard/Mouse/Gamepad)
49
+
- ✅ USB HID (Keyboard / Mouse / Gamepad)
25
50
- ✅ USB MSC (Mass Storage)
26
51
- ✅ Multiple USB classes simultaneously
52
+
- ✅ No sketch boilerplate — USB initialises and polls automatically
53
+
- ✅ `Serial` works without any `#define` in your sketch
54
+
- ✅ Arduino IDE "touch 1200" DFU bootloader entry
55
+
56
+
## How It Works
57
+
58
+
The port implements the three hooks required by the STM32 Arduino core to integrate TinyUSB as a first-class citizen. `initVariant()` is called automatically before `setup()` and handles all USB hardware and stack initialisation. `HAL_IncTick()` is overridden to signal every 1ms SysTick tick, which `yield()` and `serialEventRun()` use to service the TinyUSB task in thread context — ensuring USB events are processed reliably regardless of what the sketch is doing. `TinyUSB_Port_EnterDFU()` handles the Arduino IDE's "touch 1200" auto-reset by disconnecting from USB cleanly, writing the STM32duino bootloader magic value to a backup register, and issuing a system reset.
Add these lines to your board configuration (example shown for Generic F4):
108
+
Find your board's section and add a TinyUSB USB menu entry. The board identifier prefix (e.g. `GenF4`, `GenF1`, `GenG4`) must match the one already used in that board's section. I have included the lines you need to add in `boards_txt_additions.txt` for ease of use. Examples for each family:
Close and reopen Arduino IDE for changes to take effect.
106
130
107
131
### Step 6: Select TinyUSB
108
-
In Arduino IDE, go to **Tools > USB** and select **"Adafruit TinyUSB"**
132
+
In Arduino IDE, go to **Tools > USB** and select **"Adafruit TinyUSB"**.
133
+
134
+
## Usage
109
135
110
-
## Usage Example
136
+
Sketches require no USB initialisation boilerplate. `Serial` works normally. Here is a minimal CDC echo example — the entire sketch is just application code:
111
137
112
-
Here's a simple USB MIDI example:
113
138
```cpp
114
-
#defineSerial SerialTinyUSB //Alias to allow Serial Communication throguh TinyUSB
> **Note:**`while (!TinyUSBDevice.mounted()) delay(1)` is entirely optional but useful if your sketch needs to send data immediately on startup. It works correctly because `tud_task()` is serviced inside `delay()`.
145
156
146
-
### Key Implementation Notes
147
-
- **VBUS Sensing:** Disabled to allow proper enumeration on bus-powered devices
- **Adafruit_TinyUSB_stm32.cpp** - Platform-specific USB initialization and IRQ handling
154
-
- **tusb_config_stm32.h** - TinyUSB configuration for STM32F4
159
+
-**`Adafruit_TinyUSB_stm32.cpp`** — Hardware initialisation, IRQ handlers, automatic task polling, and DFU bootloader entry for F1, F4, and G4 families
160
+
-**`tusb_config_stm32.h`** — TinyUSB configuration and class enable flags for STM32
155
161
156
162
## Known Limitations
157
-
- Only tested on STM32F411 (other F4 variants should work but are untested)
158
-
- Requires the official STM32duino core (other cores may not work)
159
-
- DFU bootloader entry not yet implemented
163
+
- Only the F1, F4, and G4 families are currently supported — see compatibility table above
164
+
- Requires the official STM32duino core — other STM32 cores are untested
165
+
- DFU bootloader entry requires that your board's upload method in Arduino board settings is configured to use STM32CubeProgrammer in DFU mode
160
166
161
167
## Troubleshooting
162
168
163
-
**Device not enumerating:**
164
-
- Verify you selected "Adafruit TinyUSB" from Tools > USB menu
165
-
- Check that USB D+ (PA12) and D- (PA11) pins are not being used for other purposes
166
-
- Try a different USB cable (some cables are charge-only)
169
+
**Device not enumerating / "USB device malfunctioned":**
170
+
- Verify you selected "Adafruit TinyUSB" from Tools > USB
171
+
- Check that USB D+ and D- pins on your board are not used for other purposes
172
+
- Try a different USB cable (or plugging directly into the PC if you are using a hub)
173
+
- On G4 boards, ensure no other USB stack (e.g. the STM32duino built-in CDC) is also enabled
167
174
168
175
**Compilation errors:**
169
-
- Make sure you installed the Adafruit TinyUSB library via Library Manager
170
-
- Restart Arduino IDE after making the modifications
171
-
- Verify file paths are correct
176
+
- Confirm the Adafruit TinyUSB library is installed via Library Manager
177
+
- Make sure USB support in board setting is set to `Adafruit TinyUSB`
178
+
- Restart Arduino IDE after making changes to `tusb_config.h` or `boards.txt`
179
+
- Verify the `stm32` folder is in the correct location under `ports/`
180
+
181
+
**"Touch 1200" / DFU upload not working:**
182
+
- Confirm your board's upload method is set to STM32CubeProgrammer (DFU)
183
+
- Check that the correct DFU drivers are installed on your system (use [Zadig](https://zadig.akeo.ie/) on Windows if needed)
172
184
173
185
## Contributing
174
-
Issues and pull requests welcome! If you test this on other STM32F4 boards, please report your results.
186
+
Issues and pull requests welcome! If you test this on other STM32 boards or families, please report your results.
175
187
176
-
## Credits
177
-
- Based on the [Adafruit TinyUSB Arduino Library](https://github.com/adafruit/Adafruit_TinyUSB_Arduino)
178
-
- Port structure inspired by existing CH32, SAMD, and RP2040 implementations
188
+
## Hardware Support Requests
189
+
190
+
Want support for a board or STM32 family not listed here? I'm happy to add it, but I can't afford to buy every STM32 dev board out there. If you send me the board (or cover the cost of it), I'll add support and test it properly.
191
+
192
+
Get in touch via Ko-fi — use the Commissions feature to describe what you need:
0 commit comments