Minimal STM32 CubeMX CMake project with ThreadX/USBX and SEGGER RTT functionality.
The application code located in Lib/Application/application.c
sets up ThreadX and USBX with a simple USB CDC ACM device.
It is recommended to install the STM32CubeCLT software suite in conjunction with the STM32CubeMX tool. The code can then be generated for CMake, Make, EWARM or STM32CubeIDE. I recommend using the CMake option with the STM32 VSCode Extension since I've set up tasks for building and debugging the code (flashing only via a PowerShell Script).
I personally used the STM32G431CoreBoard from WeActStudio, but you can use any board which has the USB peripheral connected to the USB pins. The LED is setup for this boards' LED on pin C6
.
There are two default application threads:
- The
Main Thread
which blinks the LED on the board and sends a message over RTT every second. - The
USB Thread
which waits until a message is received over USB CDC ACM and echoes it back via CDC and RTT.
The default stdio functions are redirected over USB CDC ACM. This means that you can use printf
and gets
and similar functions in your code and the output/input will be redirected over USB. The redirection is set up in Core/Src/main.c
.
Warning
If you send a message over CDC, make sure that the line is terminated with a newline character (\r\n
) as the USB functionality expects it.
There is a logging functionality via RTT available in Lib/Logging/logging.h
which is used in the application code. There are five log levels available:
LOG_LEVEL_NONE
: No loggingLOG_LEVEL_ERROR
: Only errorsLOG_LEVEL_WARNING
: Errors and warningsLOG_LEVEL_INFO
: Errors, warnings and infoLOG_LEVEL_DEBUG
: All logs
Tip
You have to use a SEGGER RTT viewer to see the messages sent over RTT. The default viewer is available in the SEGGER J-Link software. An alternative is via pyOCD.
Example output:
DEB thread_main_entry:17 Main Thread started
DEB thread_main_entry:23 Counter : 0
DEB thread_usb_entry:33 USB Thread started
DEB thread_main_entry:23 Counter : 1
DEB thread_main_entry:23 Counter : 2
INF thread_usb_entry:44 You typed: 'help'
DEB thread_main_entry:23 Counter : 3
DEB thread_main_entry:23 Counter : 4
DEB thread_main_entry:23 Counter : 5