Skip to main content
Sany
Associate III
September 4, 2020
Solved

STM32L0xx hangs up by reading EEPROM

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...

This topic has been closed for replies.
Tesla DeLorean
Tesla DeLoreanBest answer
Guru
September 4, 2020

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.

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

3 replies

waclawek.jan
Super User
September 4, 2020

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

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
September 4, 2020

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.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Sany
SanyAuthor
Associate III
September 4, 2020

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 ;)