Design Notes

STM32 UART Zephyr Incorrect Baudrate Setting

Say you configure STM32 UART on Zephyr for a normal baud rate: 9600

You print message and everything works ok:

	struct uart_config cfg;
	uart_config_get(uart_dev, &cfg);
	cfg.baudrate = 9600;
	uart_configure(uart_dev, &cfg);
	print_uart("HI");

Design Notes

1,000,000 / 104.48 us = 9571baud. Close enough to 9600

But then you need to lower baud rate even lower: 1000

	struct uart_config cfg;
	uart_config_get(uart_dev, &cfg);
	cfg.baudrate = 1000;
	uart_configure(uart_dev, &cfg);
	print_uart("HI");

Design Notes

1,000,000/181.12 = 5521

Oh no! That baud rate is not 1000 and its not reasonable number either.

Workaround

This is due to a bug in the stm32 uart driver. The clock going to the uart peripheral is too fast for this slow of baud rate.

Can slow down the clock by modifying the apb1-prescaler, because that is going to uart. Can be seen in dtsi file for your chip.

&rcc {
	clocks = <&pll>;
	clock-frequency = <DT_FREQ_M(80)>;
	ahb-prescaler = <1>;
	apb1-prescaler = <16>;
	apb2-prescaler = <1>;
};

Design Notes

1,000,000/181.12 = 998 baud. Much better.

I may try and submit a fix later - this was on zephyr zephyr-v3.2.0-4019