cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L431RCT Hard Fault on SPI at HAL library

AKuan.1
Associate II

Hi,

I keep on getting hard fault on HAL_SPI_Transmit_IT.

I am trying to communicate to SX1276.

The circuit is designed by another experienced hardware engineer.

I use the similar code on STM32L431CBT without issue.

However, I face random hard fault on STM32L431RCT.

The problem only occurs after a few minutes of normal running.

The hard fault sometimes point to HAL_SPI_Transmit_IT, sometimes to HAL_SPI_TransmitReceive_IT and always point to the following line at HAL_SPI_Transmit_IT

if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)

0xE000ED28 = 00000000

0xE000ED2C = 00000000

0xE000ED30 = 01000000

0xE000ED34 = F8ED00E0

0xE000ED38 = F8ED00E0

0xE000ED3C = 00000000

I don't get the Mem Manage fault that mentioned in Joseph Yiu's book.

The problem code is in LORA.cpp as follows:

// SPI read register
uint8_t LORA::SPIReadReg(uint8_t addr)
{
	uint8_t data __attribute__((aligned (16)));
	uint8_t SendNull __attribute__((aligned (16)));
 
	SendNull = 0x00;
 
	HAL_GPIO_WritePin(LORA_NSS_GPIO_Port, LORA_NSS_Pin, GPIO_PIN_RESET);
 
	while(HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY);
	HAL_SPI_Transmit_IT(&hspi1, (uint8_t *)&addr, sizeof(uint8_t));
 
	// read register value
	while(HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY);
	HAL_SPI_TransmitReceive_IT (&hspi1, (uint8_t *)&SendNull, (uint8_t *)&data, sizeof(uint8_t));
 
	while(HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY);
	HAL_GPIO_WritePin(LORA_NSS_GPIO_Port, LORA_NSS_Pin, GPIO_PIN_SET);
 
	return((uint8_t)data);
}

Really need some help from experts here.

Attached is all the project related file.

3 REPLIES 3
TDK
Guru

Examine hspi and hspi->Instance for a valid pointer value at the time of the hard fault. If it's invalid, you likely have a stack overflow or array out of bound modification error.

If you feel a post has answered your question, please click "Accept as Solution".
AKuan.1
Associate II

They are valid values. And I had no idea what's wrong.

AKuan.1
Associate II

the problem solved, I added the rxbuffer array size to 256 which is max of sx1276 fifo.

Also push the device to sleep mode and reactivate it back to rx mode.

no more hardfault at the moment.