cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0 hardfault error with HAL Library

mvugs9
Associate II
Posted on October 09, 2015 at 15:34

Hello

We are currently switching from the STD library to the HAL library. Personally I'm not very pleased with that, but due to upcoming projects with F7 MCU we are mandatory to learn to use the HAL Library. We are designing a driver for the RFM69 Radio module. To set the RFM69 to our needs we need to send a lot of commands trough SPI. This is the point where strange things happen. Sometimes the Microcontroller sends the data right, the other times we get a hardfault error. This is the part where the problems begin:

void
Rfm69_Write_Reg(uint8_t address, uint8_t value) 
{
reg[0] = value;
reg[1] = address | 0x80;
HAL_SPI_Transmit(&hspi1, (uint8_t *)reg, 1, 1000);
}

The value 1 is because of the fact we set the SPI to 16 bit. The function Rfm69_write_Reg() is called multiple times at initialization of the RFM69 module:

void
Rfm69_Init(
void
)
{
uint8_t i;
Rfm69_Reset();
for
( i = 0; i < 
sizeof
(rfm69_base_config) / 2; i++)
{
Rfm69_Write_Reg(rfm69_base_config[i][0], rfm69_base_config[i][1]);
// check = Rfm69_Read_Reg(rfm69_base_config[i][0]);
}
}

But most of the times we get a Hardfault Handler. The strange thing is that when we uncomment the: check = Rfm69_Read_reg(rfm69_base_config[i][0]); line The program is working correctly. Does somebody sees the problem or has encountered the same problem? My sincerly #hal #spi #hardfault #m0
2 REPLIES 2
e1369008
Associate II
Posted on October 09, 2015 at 15:58

You are stating that the Rfm69_Read_Reg function causes the error and you didn't provide its source code.

Also, are you sure you must write then read, SPI is full duplex, you can write and read simultaneously.

mvugs9
Associate II
Posted on October 09, 2015 at 16:28

Hello,

Thank you for your reply. No the functionRfm69_Write_Reg() gives a problem. Only when I use theRfm69_Read_Reg() after theRfm69_Write_Reg() function the problem is gone. this is the implementation of theRfm69_Read_Reg():

uint8_t Rfm69_Read_Reg(uint8_t address)
{
reg[0] = 0;
reg[1] = address & 0x7F;
HAL_SPI_TransmitReceive(&hspi1, (uint8_t *)reg, &data, 1, 1000);
return

data;
}

I'm aware of the fact that SPI is full duplex. But at the moment I was busy with the low level functions for reading and writing registers. So this is more some kind of test for the functions. My sincerly,