Configure USART2 baud rate with the BRR register
Set USART2 to 115200 baud on an STM32F411 by writing 417 into BRR from a 48 MHz APB1 clock, then enable TE, RE and UE in CR1.
On an STM32F4 the baud rate is one divider in USART_BRR: with oversampling by 16, BRR = fck / baud, where the upper 12 bits are the integer part and the lower four the fraction. USART2 sits on APB1, which is 48 MHz in a 96 MHz STM32F411 setup, so 115200 baud gives 48000000 / 115200 = 416.67, which rounds to 417 (0x1A1).
#include "stm32f4xx.h"
void usart2_init(void)
{
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
(void)RCC->APB1ENR;
/* PA2 = TX, PA3 = RX, alternate function 7 */
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER2_Msk | GPIO_MODER_MODER3_Msk))
| GPIO_MODER_MODER2_1 | GPIO_MODER_MODER3_1;
GPIOA->AFR[0] = (GPIOA->AFR[0] & ~(0xFFu << 8)) | (7u << 8) | (7u << 12);
USART2->BRR = 417;
USART2->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE;
}The register writes happen in an order that keeps the line quiet: the pins are switched to the alternate function before the USART is enabled, and UE is set in the same write as TE and RE, so the first thing on the wire is a clean idle.
The actual rate is 48 MHz / 417, about 115,108 baud, an error of roughly 0.08 percent that any UART tolerates. A different APB1 clock gives a different BRR, which is why the next snippet computes it from the HAL. Default 8N1 framing needs no other register. The register names come from STM32CubeF4 V1.28 headers.