cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_I2C_Mem_Read on SMT32L0 causes HardFault

Waller.George
Associate III

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

5 REPLIES 5

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

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
Up vote any posts that you find helpful, it shows what's working..

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

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
Up vote any posts that you find helpful, it shows what's working..
Waller.George
Associate III

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.