Skip to main content
Kleber Lima da Silva
Associate
January 10, 2017
Solved

Hard fault to read internal EEPROM - STM32L011

  • January 10, 2017
  • 3 replies
  • 1635 views
Posted on January 10, 2017 at 15:27

Hi,

I am getting a hard fault while trying to read from the internal EEPROM (STM32L011), function:

uint8_t Read_EEPROM_Byte(uint32_t address)

{

   uint8_t tmp = 0;

   address = address + 0x08080000;

   tmp = *(uint32_t*)address;

   return tmp;

}

How to solve this?

Thanks.

#stm32l0 #eeprom
This topic has been closed for replies.
Best answer by Tesla DeLorean
Posted on January 10, 2017 at 17:24

Pro tip: When debugging Hard Faults, Look at the address it is complaining about, print it out, or look at it in the debugger.

The Cortex-M0 does not support unaligned read/write operations, and will also fault if you touch memory out of bounds. Use appropriate casting.

uint8_t Read_EEPROM_Byte(uint32_t address)
{
 uint8_t tmp = 0;
 address = address + 0x08080000;
 tmp = *(uint8_t*)address; // READ AS A BYTE
 return tmp;
}

uint8_t Read_EEPROM_Byte(uint32_t address)
{
 return(*(uint8_t*)(address + 0x08080000));
}�?�?�?�?�?�?�?�?�?�?�?�?�?

3 replies

Jeroen3
Senior
January 10, 2017
Posted on January 10, 2017 at 16:29

You're most likely performing a unaligned memory access.

 tmp = *(uint32_t*)address;

You are now trying to read 4 bytes from address into tmp. (of which the 3 most significant bytes will be truncated)

However, if address is uneven, eg:

0x*1, it's an illegal operation.

Remember, you can only access words on a word aligned address.

0x08080000

halfword / word aligned

0x08080002 halfword aligned

0x08080004 word aligned

...

Byte reads are always allowed by the core.

Some peripherals also limit the types of read/write to their memory region. 

This doesn't apply to reading here. It does to writing.

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
January 10, 2017
Posted on January 10, 2017 at 17:24

Pro tip: When debugging Hard Faults, Look at the address it is complaining about, print it out, or look at it in the debugger.

The Cortex-M0 does not support unaligned read/write operations, and will also fault if you touch memory out of bounds. Use appropriate casting.

uint8_t Read_EEPROM_Byte(uint32_t address)
{
 uint8_t tmp = 0;
 address = address + 0x08080000;
 tmp = *(uint8_t*)address; // READ AS A BYTE
 return tmp;
}

uint8_t Read_EEPROM_Byte(uint32_t address)
{
 return(*(uint8_t*)(address + 0x08080000));
}�?�?�?�?�?�?�?�?�?�?�?�?�?

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Shay Eliyahu
Visitor II
December 11, 2017
Posted on December 11, 2017 at 11:08

Hello Clive One,

what about to Unlock the EEPROM and lock it back??

Tesla DeLorean
Guru
December 11, 2017
Posted on December 11, 2017 at 15:34

This post is from 11 months ago, a lot of the surrounding code is not shown, the cause of the fault in this case was the unaligned access. At the top level you might well need to lock/unlock the EEPROM, I'm sure the library has worked examples.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Kleber Lima da Silva
Associate
January 10, 2017
Posted on January 10, 2017 at 17:36

Thanks Jeroen3 and Clive One.

Solved!

Shay Eliyahu
Visitor II
December 11, 2017
Posted on December 11, 2017 at 11:09

Hello Kleber,

what about to Unlock the EEPROM and lock it back??