October 31, 2023

Let's learn USB! -- TinyUSB

TinyUSB includes support for the STM32F103 as well as quite a few other STM32 microcontrollers.

Here are the conclusions up front where they won't get lost. Forget it! TinyUSB is not a complete system, not even a complete library. It is sort of like having a transmission without an engine (and/or drive shaft). It might be great with the other pieces, but without them you won't get far.

In the case of the STM32F103, there is a driver of sorts (another nasty thing from ST) and it is incomplete. It seems to assume you are using it in the context of CMSIS and/or STM32CubeF1 and references include files that are not part of TinyUSB. It looks to me like TinyUSB took one drive file out of Cube, customized it so that it fits into TinyUSB, but expects you to also have the rest of Cube.

An update (as of 11-2024 from Valentijn Sessink -- thanks!).
To get the missing pieces, the trick is to run the "get-deps" command which will get the correct platform headers.

In the case of RP2040, I haven't looked all that deeply. I would just use the Pico SDK (which includes TinyUSB). Again, by itself TinyUSB only provides part of what you need, so just go to the SDK to get the whole potatoe.

Learning about TinyUSB

We begin by cloning TinyUSB. Then we start reading docs/reference/getting_started.rst

This seems to be done in the tradition of all USB documentation. They tell you about 30 percent of what you need to know. I am not sure just what it is. When people drop through the rabbit hole and start working with USB they seem unable to communicate with people who are not in their world.

A very nice example (they do give examples) is tinyusb/examples/device/cdc_dual_ports which implements a pair of serial ports that echo characters while changing their case.

The best bet may be to learn how to use TinyUSB in the context of the RP2040. There we have examples that seem to be set up to yield full working code. If we get that figured out, maybe we can tackle TinyUSB and the STM32F103.

Where is the STM32F103 driver?

There are still deep mysteries here. The closest thing to a driver I have found in the source is src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c -- but there are still unsolved questions. Where are base addresses like 0x40006000 for the PMA base address and 0x40005c00 for the device registers?

And alongside of that file is dcd_stm32_fsdev_pvt_st.h which has code like this:

#elif CFG_TUSB_MCU == OPT_MCU_STM32F1
  #include "stm32f1xx.h"
  #define PMA_LENGTH (512u)

This is all well and good, but where is stm32f1xx.h? I find this file on my system in STM32/ST/Cube/STM32CubeF1/Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f1xx.h which I just happen to have because I downloaded "Cube" back in 2017 or so, but that is sheer accident and it is certainly not part of TinyUSB.

Some searches relative to stm32f1xx.h lead me to this:

Yikes! Yet another USB stack for the STM32, but this one clearly says that is requires CMSIS V4 or CMSIS V5 -and- the stm32.h STM32 universal header.

The conclusion - Tiny USB is not a standalone library like I thought, but something (I'm not sure just what) that can be included as part of some other system (just what other system is unclear).

RP2040 bootrom and TinyUSB

I looked at the RP2040 bootrom briefly. They use files from TinyUSB, but they do not use TinyUSB in what I would call a normal way, or in a way that I can help me learn to get started.

Black pill and TinyUSB

Just in passing, I will note the following directories:

tinyusb/hw/bsp/stm32f4/boards/stm32f411blackpill
tinyusb/hw/bsp/stm32f4/boards/stm32f401blackpill
tinyusb/hw/bsp/stm32f4/boards/stm32f407blackvet
How about that? Two different F4 "blackpill" boards and something called a "blackvet"

Here is the black VET:

But we have now wandered entirely off topic.
Feedback? Questions? Drop me a line!

Tom's Computer Info / [email protected]