The ST-LINK on the board doubles as a USB serial adapter, wired to USART3. You find the pins in the manuals, switch on the clocks, work out the baud rate divider from the clock that feeds the USART, send single characters, and finally make printf use the port.
- Which USART and which two pins make up the virtual COM port, and how to confirm it in the user manual, the schematic and the datasheet
- How to switch a pin to an alternate function with
GPIOx_MODERandGPIOx_AFRH - How the USART kernel clock decides the baud rate and how to compute
USART_BRR - Send a character with
USART_ISRandUSART_TDR, and retargetprintfonto it
- STM32H750B-DK and a USB cable to CN14 (the ST-LINK connector)
- STM32CubeIDE installed
- A serial terminal on the PC (for example picocom, minicom or the terminal view in CubeIDE)
On this board
Pulled from the datasheet tables and the board schematic. Click a pin for everything the finder knows about it.
Find the serial port in the user manual
You want text to appear on your PC. The cable you already use to flash the board (the USB connector CN14) goes to the ST-LINK chip, and the user manual says that chip also offers a virtual COM port: "The USART3 serial interface is directly available as a virtual COM port of the PC". The settings it expects are:
- 115200 baud
- 8 data bits, no parity, 1 stop bit (called 8N1)
- no flow control
So the MCU side is USART3, and the PC side opens the new serial port that appears when the board is plugged in. No extra wires are needed.
Find the two pins
A USART needs two wires: TX (transmit) and RX (receive). The user manual's I/O assignment table (and the schematic) say which MCU pins carry the virtual COM port: net VCP_TX is PB10 and VCP_RX is PB11. The names are from the MCU's point of view: the MCU transmits on PB10.
A pin can only be handed to USART3 as an alternate function (AF), and the choice is fixed by the chip, so look it up in the datasheet's alternate function table: PB10 is AF7 = USART3_TX and PB11 is AF7 = USART3_RX. (The finder's pin pages show this table too.)
One thing to know: on an Arduino Uno, D0/D1 are the serial port to the PC. Not on this board. Here Arduino D1 is PB6 and D0 is PB7 (USART1), separate wires from the virtual COM port, so a shield on D0/D1 does not disturb your PC link. The Arduino sheet of the schematic prints "PB10/PB11" next to D1/D0, but the nets ARD_D1/ARD_D0 leave the MCU on PB6/PB7, and the user manual's Arduino table agrees: a good example of checking the net, not a text label.
Tip. Click VCP_TX in the schematic viewer to follow it to the ST-LINK part of the board.
Clocks and pins
Two new clock enables this time, both found in the RCC:
- GPIO port B:
GPIOBENinRCC_AHB4ENR, as for the LED. - USART3:
USART3ENinRCC_APB1LENR. USART3 sits on the APB1 bus, and the "L" means the low half of the register (a second one,RCC_APB1HENR, holds the other peripherals).
Then the pins. GPIOx_MODER value 10 means alternate function. Which function is chosen by GPIOx_AFRH (pins 8 to 15; AFRL is for pins 0 to 7): four bits per pin, and we write 7 for both PB10 and PB11. A pin in alternate-function mode is driven by the peripheral, so the speed and pull settings can stay at reset values.
#include "stm32h7xx.h" /* with STM32H750xx defined by the project */
static void uart3_pins_init(void)
{
/* clocks: GPIO port B and USART3 */
RCC->AHB4ENR |= RCC_AHB4ENR_GPIOBEN;
RCC->APB1LENR |= RCC_APB1LENR_USART3EN;
(void)RCC->APB1LENR; /* read back: lets the enables take effect */
/* PB10 and PB11: MODER = 10 (alternate function) */
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE10_Msk | GPIO_MODER_MODE11_Msk))
| (2U << GPIO_MODER_MODE10_Pos)
| (2U << GPIO_MODER_MODE11_Pos);
/* AFRH holds pins 8..15 (AFR[1]): both pins use AF7 = USART3 */
GPIOB->AFR[1] = (GPIOB->AFR[1] & ~(GPIO_AFRH_AFSEL10_Msk | GPIO_AFRH_AFSEL11_Msk))
| (7U << GPIO_AFRH_AFSEL10_Pos)
| (7U << GPIO_AFRH_AFSEL11_Pos);
}/* HAL calls this from HAL_UART_Init(); CubeIDE generates it in stm32h7xx_hal_msp.c */
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART3) {
__HAL_RCC_USART3_CLK_ENABLE(); /* = RCC_APB1LENR.USART3EN */
__HAL_RCC_GPIOB_CLK_ENABLE(); /* = RCC_AHB4ENR.GPIOBEN */
GPIO_InitTypeDef io = {0};
io.Pin = GPIO_PIN_10 | GPIO_PIN_11; /* PB10 = TX, PB11 = RX */
io.Mode = GPIO_MODE_AF_PP; /* MODER = 10, OTYPER = 0 */
io.Pull = GPIO_NOPULL;
io.Speed = GPIO_SPEED_FREQ_LOW;
io.Alternate = GPIO_AF7_USART3; /* AFRH = 7 for both pins */
HAL_GPIO_Init(GPIOB, &io);
}
}Configure USART3 and its baud rate
The USART has two clocks. The bus clock (the USART3EN you just set) lets the CPU read and write its registers. The kernel clock is the one that times every bit on the wire. Which clock feeds it is chosen by the field USART234578SEL in RCC_D2CCIP2R. Its reset value is 000, which selects rcc_pclk1, the APB1 bus clock.
What frequency is that right after reset? The chip starts on the 64 MHz internal oscillator (HSI), and the divider registers RCC_D1CFGR and RCC_D2CFGR reset to "divide by 1", so pclk1 is 64 MHz. The USART's own prescaler USART_PRESC also resets to "not divided".
With 16x oversampling (the reset setting) the formula is simple: BRR = kernel clock / baud rate. So 64 000 000 / 115 200 = 555.6, and we write 556. The real baud rate is then 64 000 000 / 556 = 115 108, which is 0.08 % off. The receiver tolerates a few percent (3.33 % for this frame format), so that is fine.
The reference manual's order is: set the word length and baud rate while the USART is off, then set UE (USART enable), then TE (transmitter enable). Frame format 8N1 is the reset state, so there is nothing to write for it.
If your project sets up the PLL (CubeIDE's generated SystemClock_Config does), pclk1 is no longer 64 MHz. Work out the new value, or in HAL code let HAL_UART_Init do it.
/* Kernel clock of USART3 after reset: HSI 64 MHz -> pclk1 (all prescalers 1).
Change this number if your project sets up the PLL. */
#define USART3_KER_CK 64000000U
#define BAUD 115200U
static void uart3_init(void)
{
uart3_pins_init(); /* from the previous step */
/* 8N1, 16x oversampling, no FIFO: all reset values, nothing to write.
BRR = 64 000 000 / 115 200 = 555.6 -> rounded to 556 */
USART3->BRR = (USART3_KER_CK + BAUD / 2U) / BAUD;
USART3->CR1 = USART_CR1_UE; /* USART on ... */
USART3->CR1 |= USART_CR1_TE | USART_CR1_RE; /* ... then transmitter and receiver */
}UART_HandleTypeDef huart3;
static void uart3_init(void)
{
huart3.Instance = USART3;
huart3.Init.BaudRate = 115200;
huart3.Init.WordLength = UART_WORDLENGTH_8B;
huart3.Init.StopBits = UART_STOPBITS_1;
huart3.Init.Parity = UART_PARITY_NONE;
huart3.Init.Mode = UART_MODE_TX_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart3.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
/* Calls HAL_UART_MspInit() above, reads the real kernel clock frequency
and writes USART_BRR, USART_CR1 and the rest. */
if (HAL_UART_Init(&huart3) != HAL_OK) {
Error_Handler(); /* generated by CubeIDE */
}
}Tip. In the CubeIDE .ioc editor the same settings are Connectivity > USART3 > Asynchronous, baud rate 115200. The generator writes the code above for you, and the clock tree view shows the real pclk1.
Send a character, then a string
To send a byte you write it into USART_TDR. The USART then shifts it out bit by bit, which at 115200 baud takes about 87 microseconds per byte (10 bits: start, 8 data, stop). You must not write the next byte before the register is free, or you overwrite the one still waiting.
The flag that says "TDR is free" is bit 7 of USART_ISR. It is called TXE when the FIFO is off (our case) and TXFNF when the FIFO is on; the CMSIS header uses one name, USART_ISR_TXE_TXFNF, for both. After the last byte, wait for TC (transmission complete): it means the last bit has really left the chip.
That is the whole transmit loop: wait for TXE, write TDR, repeat.
static void uart3_putc(char c)
{
while (!(USART3->ISR & USART_ISR_TXE_TXFNF)) { } /* wait: TDR is free */
USART3->TDR = (uint8_t)c; /* starts the transmission */
}
static void uart3_puts(const char *s)
{
while (*s) {
uart3_putc(*s++);
}
while (!(USART3->ISR & USART_ISR_TC)) { } /* last bit has left the chip */
}
/* in main(): */
uart3_init();
uart3_puts("Hello from the STM32H750!\r\n");#include <string.h>
/* in main(), after uart3_init() */
const char msg[] = "Hello from the STM32H750!\r\n";
/* blocks until all bytes are sent; HAL waits on TXE for every byte and on TC at the end */
HAL_UART_Transmit(&huart3, (const uint8_t *)msg, strlen(msg), 100); /* 100 ms timeout */Tip. Why \r\n and not just \n? A serial terminal does what it is told: \n moves down a line, \r returns to the start of the line. Without \r your text climbs diagonally to the right.
Make printf use the port
Sending strings by hand gets old fast. The C library's printf can format numbers for you, and you only have to tell it where the characters go. This is called retargeting printf.
This part is not in the ST documents; it comes from the toolchain. A CubeIDE project uses the newlib C library. printf ends up in a function _write, which in the generated syscalls.c calls __io_putchar(ch) once per character. That function is marked weak (a default that you may replace), so you simply define your own __io_putchar that sends the character on USART3.
I added one convenience: when printf gives a plain \n, we send \r first, so you can write normal C strings.
#include <stdio.h>
/* newlib calls this for every character printf produces */
int __io_putchar(int ch)
{
if (ch == '\n') {
uart3_putc('\r'); /* make "\n" start a new line at column 0 */
}
uart3_putc((char)ch);
return ch;
}
int main(void)
{
unsigned n = 0;
uart3_init();
for (;;) {
printf("hello from the H750, n = %u\n", n++); /* a trailing \n flushes the line */
for (volatile uint32_t i = 0; i < 4000000U; i++) { } /* crude delay: tune it */
}
}#include <stdio.h>
extern UART_HandleTypeDef huart3;
int __io_putchar(int ch)
{
uint8_t c = (uint8_t)ch;
if (c == '\n') {
uint8_t cr = '\r';
HAL_UART_Transmit(&huart3, &cr, 1, HAL_MAX_DELAY);
}
HAL_UART_Transmit(&huart3, &c, 1, HAL_MAX_DELAY);
return ch;
}
/* in main(), after HAL_Init(), SystemClock_Config() and uart3_init() */
unsigned n = 0;
while (1) {
printf("hello from the H750, n = %u\n", n++);
HAL_Delay(1000); /* 1 s, SysTick based */
}Tip. Printing floats with %f needs an extra option in CubeIDE: Project Properties > C/C++ Build > Settings > MCU Settings > tick "Use float with printf from newlib-nano". Without it the output is empty or blank.
Open the port on the PC and make it yours
Build, flash and run. On the PC a new serial device has appeared. On Linux it is usually /dev/ttyACM0 (look at dmesg right after plugging in; your user must be allowed to open it, which on Arch is the group uucp and on Debian or Ubuntu dialout). On Windows it is a COM port in Device Manager. The terminal must use 115200 baud, 8 data bits, no parity, 1 stop bit. A line should appear every second.
Then try:
- Receive: read bytes from
USART_RDRonce the flagRXNE(bit 5 ofUSART_ISRin the non-FIFO layout) is set, and echo them back. Typing in the terminal then types on the screen through the MCU. - Turn LD7 on when you type
1and off when you type0. - Change the clock (set up the PLL, or switch to HSI/2 with
HSIDIV) and see the text turn into garbage until you fixBRR.
ls /dev/ttyACM* # which device appeared?
picocom -b 115200 /dev/ttyACM0 # leave with Ctrl-A then Ctrl-X
# or: screen /dev/ttyACM0 115200 (leave with Ctrl-A then K)Common mistakes
- Garbage characters: the baud rate does not match. Usually the kernel clock is not the one you assumed (a PLL was set up) or the terminal speed is not 115200.
- Nothing appears and
printfhas no\n: newlib collects the text in a buffer and sends it at the end of the line. End lines with\nor callfflush(stdout). - Forgetting
USART3ENorGPIOBEN: the registers ignore your writes and the wire stays silent. - Writing
TDRwithout waiting for TXE: bytes are lost. Disabling the USART (or going to sleep) without waiting for TC cuts the last character off. - Setting
UEbeforeBRR: the reference manual says to configure first and enable after. Changing BRR during a transmission corrupts it. - Assuming Arduino D0/D1 are the PC serial port, as on an Uno. On this board they are PB7/PB6 (USART1); the virtual COM port is USART3 on PB10/PB11.
Check yourself
Try to answer before opening. If you get stuck, the pages linked above have the answer.
The kernel clock of USART3 is 100 MHz instead of 64 MHz. What USART_BRR value gives 115200 baud?
100 000 000 / 115 200 = 868.06, so BRR = 868 (16x oversampling). That is within 0.01 % of the target.
Why does uart3_putc wait for TXE before writing USART_TDR?
With the FIFO off, TDR holds exactly one byte. If you write while the previous byte is still waiting to move into the shift register, the old byte is overwritten and lost.
Both PB10 and PB11 use AF7. Why can the very same AF number work for TX and RX?
The AF number is chosen per pin, not per peripheral. The datasheet table lists, for each pin, which signal sits on which AF number. It happens that USART3_TX on PB10 and USART3_RX on PB11 are both on AF7; on other pins the numbers differ.
Reading list
Every page this guide sent you to, by document. Reading them in order is a good revision.
| UM2488 | UM2488 STM32H745I/H750B-DK Discovery kit user manual | p.15p.17p.20p.23 |
| MB1381 | MB1381 Discovery board schematic (H750XB variant) | sheet 3sheet 3 |
| DS12556 | STM32H750xB datasheet | p.53 |
| RM0433 | RM0433 reference manual (STM32H742/743/753/750) | p.338p.353p.393p.395p.415p.457p.461p.533p.540p.545p.2025p.2029p.2036p.2037p.2069p.2085p.2088p.2097p.2101p.2101p.2102 |
Search next
Each of these opens the finder with pins, registers, schematic hits and document pages for the term.