cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_UART_Init --> HAL_RCCEx_PeriphCLKConfig --> HardFault_Handler

Sergei2
Associate

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?

3 REPLIES 3
mƎALLEm
ST Employee

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 on "Accept as Solution" on the reply which solved your issue or answered your question.
Sergei2
Associate

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

How to debug a HardFault on an Arm® Cortex®-M STM32

 

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.