Serial Logging With STLINK
Programmer like the
STLINK-V3MINI
include a virtual COM port (VCP) that allows them to function as a serial-to-USB
adapter. When used with STM32-based microcontrollers like Blues Cygnet or
Blues Swan, you can use the STLINK's VCP_RX
and VCP_TX
pins for serial
logging.
Using the programmer for logging offers several advantages:
- Single USB connection: You don’t need a separate USB cable for serial logging. As long as the host is powered by other means, the same USB connection used to power the programmer also handles logs.
- Persistent serial connection: The serial link stays active even when the host is power-cycled, which is especially helpful for applications that frequently enter sleep mode.
- The STLINK's
VCP_RX
andVCP_TX
pins map to:PB10
andPB11
on Blues Cygnet.PG8
andPG7
on Blues Swan.
The STLINK's VCP_RX
and VCP_TX
pin provide UART interface. In general, to do
your debug logging through these pins you can treat them as a standard UART
interface.
The sections below show specific instructions you can follow when working with an STLINK for logging in Arduino and Zephyr.
Arduino
When using the STLINK on Arduino you can use the HardwareSerial
class
to establish a serial connection through the STLINK’s VCP_RX
and VCP_TX
pins.
HardwareSerial serialDebug(PIN_VCP_RX, PIN_VCP_TX);
Next, in setup()
, initialize the serial interface by calling its begin()
function with a baud rate:
serialDebug.begin(115200);
After calling begin()
to initialize the serial interface, it's often helpful to
wait for the connection to become available—especially when relying on tools like
a serial monitor that may need time to enumerate the device. The following code
waits up to 5 seconds for the serial interface to become ready:
serialDebug.begin(115200);
const size_t timeout_ms = 5000;
for (const size_t start_ms = millis(); !serialDebug && (millis() - start_ms) < timeout_ms;);
With the setup complete, you can use print()
and println()
as normal to log to
your serial monitor.
serialDebug.println("Testing...");
You can also use the STLINK’s serial connection as a way of
viewing Notecard debug logs
with the setDebugOutputStream()
method.
Notecard notecard;
notecard.setDebugOutputStream(serialDebug);
When put together, here's a good starting point for an Arduino sketch that uses an STLINK for viewing debug logs.
Notecard notecard;
HardwareSerial serialDebug(PIN_VCP_RX, PIN_VCP_TX);
void setup() {
serialDebug.begin(115200);
const size_t timeout_ms = 5000;
for (const size_t start_ms = millis(); !serialDebug && (millis() - start_ms) < timeout_ms;);
notecard.begin();
notecard.setDebugOutputStream(serialDebug);
}
Zephyr
The Zephyr board configuration for both Blues Swan and Blues Cygnet specifies
lpuart1
as the system console, which is connected to the STLINK’s virtual COM
port. This means any printk()
output or logging automatically appears over the
STLINK USB connection.