October 30, 2023

Let's learn USB! -- RP2040 versus STM32F103

Which should we use?

First consider price. In October of 2023 on AliExpress I could order a lot of 5 STM32F103 "blue pill" boards for $5.43 and 3.44 shipping charges. $8.87 in US dollars. Round this up to $2 each. Dirt cheap! I already have 20 of these on hand. You will also want a ST-Link v2. I see these (believe it or not) on AliExpress for $0.99 with free shipping. I recommend buying a handfull of the STM32F103 devices and at leas two ST-Link devices so you have spares.

The Pi Pico with the RP2040 is a bit more expensive, but still ridiculously inexpensive. You can buy them right now (October, 2023) on Adafruit for $4. I suggest buying three since you will be paying shipping (perhaps more). This gives you one or more as a spare, and you can configure the other with picoprobe firmware which provides openocd access as well as a USB to serial interface on the same device. Here is my setup:

Either way the money is about the same (on the order of $20) to get started.

Note that with the STM32, the ST-Link is pretty much essential (although you could install a DFU boot loader and work with that). With the RP2040, a second device acting as a pico-probe is a luxury, you can get by nicely with nothing more than a single device and a USB cable.

Documentation

For me, this is the most important consideration in any kind of programming project. For the STM32F103 you get excellent documentation and RM0008 is the 1137 page reference manual for the chip. Pages 625-656 (section 23) cover the USB. For the RP2040 you get a 636 page reference manual, with pages 382-415 (section 4.1) covering the USB.

In short, the printed documentation for both is available and excellent.

Example code

For the RP2040, you can study the Bootrom source (the bootrom implements a mass storage device over USB). The SDK for the RP2040 uses TinyUSB for the USB subsystem, which is clean C code and reasonably understandable. The examples for the RP2040 include a number of USB examples that are tutorial in nature, so it is hard to ask for much more.

For the STM32F103, you have 3 options, one is terrible, the others are pretty good.

I give the link to libmaple as one project that uses the terrible USB source code distributed by ST. This awful code is part of "Cube" or whatever they call it. It is notorious for being one of the worst code disasters available anywhere. It does seem to work, so there is that for it.

The Papoon_usb is for the STM32F103 only and is nice code, but written in C++. I avoid C++ like the unclean plague, but I am willing to at least look at the code, but will probably refuse to compile or run it. His notes and comments are very much worth reading.

And again we have TinyUSB. All plain C code. And having the same code for the RP2040 and the STM32 means that what you learn for one carries over to the other.

USB hardware compared

The RP2040 provides for 32 endpoints (16 IN and 16 OUT).
The STM32 provides for 16 endpoints (either IN or OUT).

The RP2040 has 4096 bytes of dual ported SRAM set aside for USB (64 buffers of 64 bytes).
The STM32 has 512 bytes of RAM set aside for USB (8 buffers of 64 bytes)

The RP2040 uses IRQ5 for USB ctrl interrupts.
The STM32F103 is a "performance line" part in the STM32F1 lineup and provides 3 interrupts for USB. We have "hp" on IRQ 19, "lp" on IRQ 20 and "wk" on IRQ42. See elsewhere for an explanation of what these are.
Both provide an ARM NVIC controller.

A general comparison of the RP2040 and the STM32F103 is in order. Code is loaded into the RP2040 over USB by copying files to the mass storage device set up by the bootrom. Code for the STM32 is loaded using the ST-Link over the SWD lines. I typically use a command like this in my Makefile:

flash:  blink.elf
        openocd $(OCDCFG) -c "program blink.elf verify reset exit"
The RP2040 has a dual core ARM Cortex M0+ running at 133 Mhz with 264k of ram and 2M of flash.
The STM32F103 has a single core ARM Cortex M3 running at 72 Mhz with 20k of ram and 32 or 64k of flash
Feedback? Questions? Drop me a line!

Tom's Computer Info / tom@mmto.org