2017-01-10 06:27 AM
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 #eepromSolved! Go to Solution.
2017-01-10 08:24 AM
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));
}�?�?�?�?�?�?�?�?�?�?�?�?�?
2017-01-10 07:29 AM
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.
2017-01-10 08:24 AM
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));
}�?�?�?�?�?�?�?�?�?�?�?�?�?
2017-01-10 08:36 AM
Thanks Jeroen3 and Clive One.
Solved!
2017-12-11 03:08 AM
Hello Clive One,
what about to Unlock the EEPROM and lock it back??
2017-12-11 03:09 AM
Hello Kleber,
what about to Unlock the EEPROM and lock it back??
2017-12-11 07:34 AM
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.