Senior
March 20, 2026
Solved
STM32CubeMX does not generate UART clock init
- March 20, 2026
- 1 reply
- 145 views
Code generation of the attached .ioc file (copy of ST example file of similar name) does not generate clock init for UART4. This means the example application hard faults. I see no reason why the UART clock should not be initialised here and would save someone time debugging in the future!
In stm32mp2xx_hal.c:
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(huart->Instance==UART4)
{
/* USER CODE BEGIN UART4_MspInit 0 */
// TODO: Need this here! __HAL_RCC_UART4_CLK_ENABLE();
/* USER CODE END UART4_MspInit 0 */
/**UART4 GPIO Configuration
PH8 ------> UART4_RX
PH7 ------> UART4_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Alternate = GPIO_AF4_UART4;
HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF4_UART4;
HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
/* USER CODE BEGIN UART4_MspInit 1 */
/* USER CODE END UART4_MspInit 1 */
}
}
