Ye Olde STM32F4 USART baud rate
I'm having the same trouble as many on here in that I can't persuade USART1 of my STM32F4 to come out with the correct baud rate. My transmitted data appears as garbage when received at 115,200 and if I attach a Saleae logic probe to the Tx pin it measures the emitted baud rate at 172,890 bits/s, so a factor of 1.5 too large. I know the external clock on my board is 12 MHz (it is printed on the can) and my clock configuration code to run at 168 MHz is therefore:
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
// Configure the main internal regulator output voltage
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
// Initialize the CPU, AHB and APB bus clocks
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 12;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
assert(false);
}
// Initialize the CPU, AHB and APB bus clocks
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 |
RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
assert(false);
}To configure the USART1 clock as 115,200 I'm calling LL_USART_Init() with the BaudRate field of the structure passed in set to 115200 and assuming that will just sort it out. I've tried different APB2CLKDivider values but the measured baud rate on the Tx pin remains unchanged, which implies that LL_USART_Init() is sorting it out but that something is wrong with the clock configuration code I have pasted in above.
Can anyone spot my mistake? Or is there somewhere else I should be looking for my mistake?