February 28, 2025

Black Pill boards - F411 USB hardware -- the Arduino_STM32 sources

As I have mentioned, I copied the USB sources for the F411 from the Arduino_STM32 project. I will just refer to this as the "duino" source from now on to avoid excess typing. I did a fair bit of work to prune it, and will continue to do so in an effort to make it easier to browse through.

It may seem like there is a lot of code in the "duino" source tree, but there really isn't. Take a look at this section extracted from the Hydra build:

cd usbF4; make
make[1]: Entering directory '/u1/Projects/STM32/Hydra/usbF4'
arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration -fno-builtin -DHYDRA -O -I. -o usb.o -c usb.c
arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration -fno-builtin -DHYDRA -O -I. -o misc.o -c vcp/misc.c
arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration -fno-builtin -DHYDRA -O -I. -o usb_bsp.o -c vcp/usb_bsp.c
arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration -fno-builtin -DHYDRA -O -I. -o usbd_cdc_vcp.o -c vcp/usbd_cdc_vcp.c
arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration -fno-builtin -DHYDRA -O -I. -o usbd_desc.o -c vcp/usbd_desc.c
arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration -fno-builtin -DHYDRA -O -I. -o usbd_usr.o -c vcp/usbd_usr.c
arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration -fno-builtin -DHYDRA -O -I. -o usb_core.o -c driver/usb_core.c
arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration -fno-builtin -DHYDRA -O -I. -o usb_dcd.o -c driver/usb_dcd.c
arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration -fno-builtin -DHYDRA -O -I. -o usb_dcd_int.o -c driver/usb_dcd_int.c
arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration -fno-builtin -DHYDRA -O -I. -o usbd_cdc_core.o -c library/usbd_cdc_core.c
arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration -fno-builtin -DHYDRA -O -I. -o usbd_core.o -c library/usbd_core.c
arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration -fno-builtin -DHYDRA -O -I. -o usbd_ioreq.o -c library/usbd_ioreq.c
arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration -fno-builtin -DHYDRA -O -I. -o usbd_req.o -c library/usbd_req.c
That is it. 13 files by my count. One top level file that I wrote, 5 files from the vcp directory, 3 files from the driver directory, and 4 files from the library directory. There are also include files. Many of them just contain ansi prototypes. In other words, this code base is not gigantic. Ignoring the header files, it is just over 7000 lines of code.

HS device support

Apparently there are some STM32 devices that support USB HS (480 Mbps). The F411 is not one of them.

The sources include support for OTG HS. Some extra registers are present in the OTG_HS controller, but for the most part it is the same (or we would have an entirely different driver). The STM32F407 and STM32F417 would be an example with two USB OTG ports, one FS and one HS.

And I have 3 Olimex boards with F407 and F405 chips. So I should not eradicate the HS support in this source tree in case I ever want to use it with them.

I have the Olimex STM32-P405 with a F405RGT6 chip. It has 1M flash, 196K sram, two OTG usb (one FS, one HS). I see conflicting information about ethernet - there is no connector on the board. The CPU runs at 196 Mhz.
I also have the Olimex STM32-P407 and an Olimex STM32-E407. Both have the F407ZGT6 chip. Like the F405, it has 1M flash, 196K sram, two OTG usb (one FS, one HS). It runs at the same 196 Mhz clock. These boards do have a connector for the ethernet.

There is also the STM32F429. Somewhere I have a 32F429IDISCOVERY board with a STM32F429ZI chip on it. This has 2M of flash, 256K of ram along with USB OTG. 180 Mhz CPU (Cortex M4). Interestingly, the TRM here is common for the F405, F407, and F429 and they all seem to have the same Synopsys OTG controller. The section in the TRM for USB OTG is still well over 100 pages.

Pruning

I have run into three sorts of things that are "distractions" in the code base: I have decided to just delete the boilerplate, keeping only useful comments and copyright information.

As for the HS and Host (HCD) code, I will ensure it is in ifdefs, adding USB_HS or USB_HOST myself as necessary. I will also prefix every line with "--" inside of those conditional blocks to keep myself from tripping over them. I have found these conditionals to be important:

USE_HOST_MODE

Feedback? Questions? Drop me a line!

Tom's Computer Info / tom@mmto.org