2020-09-04 04:33 AM
Hello!
I have these code to store/read data in the EEPROM of my STM32L031K6..
The write function works, the data is saved in flash correctly...
When i want to read the data back, i falling in the HardFault Handler.... :\
This is my Code:
eeprom.h
#define EEPROM_BASE_ADDRESS 0x08080000UL
HAL_StatusTypeDef writeEEPROMByte(uint32_t address, uint8_t data)
{
HAL_StatusTypeDef status;
uint32_t eepromAddress = address + EEPROM_BASE_ADDRESS;
HAL_FLASHEx_DATAEEPROM_Unlock();
status = HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_BYTE, eepromAddress, data);
HAL_FLASHEx_DATAEEPROM_Lock();
return status;
}
uint8_t readEEPROMByte(uint32_t address)
{
uint8_t data = 0;
uint32_t eepromAddress = address + EEPROM_BASE_ADDRESS;
data = *(__IO uint32_t *)eepromAddress;
return data;
}
i think, the convertion from uint32_t to uint8_t is wrong, but it works to write the data...
Solved! Go to Solution.
2020-09-04 04:43 AM
Don't think you understand casting properly
data = *(__IO uint8_t *)eepromAddress; // Read byte at address
The CM0 in the STM32L031 will not do unaligned 32-bit reads/writes. Odd addresses will fault, as will those spanning word boundaries.
2020-09-04 04:41 AM
Where do you have this from?
Why would you need to read an uint32_t, if you return only an uint8_t? In 'L0, an attempt to read a word from a non-word-aligned address leads to fault.
data = *(__IO uint8_t *)eepromAddress;
JW
2020-09-04 04:43 AM
Don't think you understand casting properly
data = *(__IO uint8_t *)eepromAddress; // Read byte at address
The CM0 in the STM32L031 will not do unaligned 32-bit reads/writes. Odd addresses will fault, as will those spanning word boundaries.
2020-09-04 04:58 AM
Hello,
Thanks clive, okay, i understand, i searched a solution to not write words, then i found this code on Github for the STM32L0xx (GitHub stm32EEPROM Library)
Now i see, the code is only tested for the STM32L1xx, i use this code without Problems on my STM32F4, but i forgot the problematic with 32-bit read/write for the CM0, i work first time with this ;)