Skip to main content
Waller.George
Associate III
January 17, 2019
Question

HAL_I2C_Mem_Read on SMT32L0 causes HardFault

  • January 17, 2019
  • 5 replies
  • 1195 views

I call this function a very large amount of times through out my entire program however in just one spot I reliably get a Hard Fault when it's called.

When this line is executed, the fault occurs.

(*hi2c->pBuffPtr++) = hi2c->Instance->RXDR;

I called this function from other higher level functions which are also used a very high number of times in the program so memory allocation should not be a problem. Any one have can suggestions?

Thanks

George

This topic has been closed for replies.

5 replies

After Forever
Senior III
January 17, 2019

Check the value of hi2c->pBuffPtr, is it in its supposed range? Most likely you have a buffer overflow problem.

Tesla DeLorean
Guru
January 17, 2019

The CM0 is highly sensitive to unaligned memory access, ie 16-bit wide to an ODD address.

Check the alignment of the pointer, and special case the unaligned cases.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
After Forever
Senior III
January 17, 2019

hi2c->pBuffPtr is a pointer to uint8_t. Arent't 1-byte accesses always valid?

Tesla DeLorean
Guru
January 17, 2019

Perhaps, but the OP has already discounted using the wrong buffer/memory.

Sanity check the pointer involved, then we'd know.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Waller.George
Associate III
January 18, 2019

Thank for the replies.

The pData parameter passed to HAL_I2C_Mem_Read has the value 0x20001d88 however the value of hi2c->pBuffPtr is 0x0. How can this be?

In the function the pointer is tested:

 if ((pData == NULL) || (Size == 0U))
 {
 return HAL_ERROR;
 }

And then assigned:

hi2c->pBuffPtr = pData;

Any pointers (no pun intended) would be great, thanks.