Skip to main content
Associate
May 8, 2026
Question

HAL_UART_Init --> HAL_RCCEx_PeriphCLKConfig --> HardFault_Handler

  • May 8, 2026
  • 2 replies
  • 155 views

MCU STM32H503KBU

internal clock  64 MHz

 

During a UART restart,
the HAL_UART_Init function calls
HAL_RCCEx_PeriphCLKConfig, which results in a HardFault_Handler

 

void UART_Set_BaudRate(UART_HandleTypeDef *huart, uint32_t BaudRate) 
{
	if (HAL_UART_DeInit(huart)!= HAL_OK)
	{
	Error_Handler();
	}

	huart->Instance = USART1;
	huart->Init.BaudRate = BaudRate;
	huart->Init.WordLength = UART_WORDLENGTH_8B;
	huart->Init.StopBits = UART_STOPBITS_1;
	huart->Init.Parity = UART_PARITY_NONE;
	huart->Init.Mode = UART_MODE_TX_RX;
	huart->Init.HwFlowCtl = UART_HWCONTROL_NONE;
	huart->Init.OverSampling = UART_OVERSAMPLING_16;
	huart->Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
	huart->Init.ClockPrescaler = UART_PRESCALER_DIV1;
	huart->AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

	if (HAL_UART_Init(huart) != HAL_OK)
	{
	Error_Handler();
	}

	if (HAL_UART_Receive_IT( OW.huart, &uart_temp, 1) != HAL_OK)
	 Error_Handler();
}

 

If you disable the HAL_UART_DeInit function, everything works.

 What could be the problem?

2 replies

mƎALLEm
Technical Moderator
May 8, 2026

Hello,

Not clear what do you mean by "During a UART restart".

+ in your code:

if (HAL_UART_Receive_IT( OW.huart, &uart_temp, 1) != HAL_OK)

What is that OW here?

You need to use the already used UART instance: huart

if (HAL_UART_Receive_IT(huart, &uart_temp, 1) != HAL_OK)

 

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
Sergei2Author
Associate
May 8, 2026

UART reboot is done to clear possible errors

As far as I understand, there's no direct function for this in HAL, so I use

HAL_UART_DeInit
HAL_UART_Init

about OW ,yes, it's my mistake. This is a global variable that is passed to this function.

OW.huart = &huart1;

 

But that's not a problem...
The first initialization goes fine

MX_USART1_UART_Init();

but repeating it after HAL_UART_DeInit causes an error

mƎALLEm
Technical Moderator
May 8, 2026
To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.