Skip to main content
VijayRakeshM
Associate III
January 28, 2019
Solved

EEPROM Write Goes to Hard Fault

Hi,

I'm trying save some data into EEPROM after successful storage of first byte for second byte it goes to hard fault irq. Can anyone help me out from this issue

void Save_data(float Data)
{
	char Buff[15];unsigned long Address=0x08080000;
	sprintf(Buff,"%f\n",Data);
	for(int i=0;i<15;i++)
	{
		HAL_FLASH_Unlock();
		if(Buff[i] == '\n')
		{
		HAL_FLASH_Program(FLASH_TYPEPROGRAMDATA_BYTE,Address++,'\0');
		break;
		}
		HAL_FLASH_Program(FLASH_TYPEPROGRAMDATA_BYTE,Address++,Buff[i]);
		HAL_FLASH_Lock();
	}
	
	return;
}

    This topic has been closed for replies.
    Tesla DeLorean
    Tesla DeLoreanBest answer
    Guru
    January 28, 2019

    The processors R7 register would be of particular interest here.

    Perhaps use the EEPROM functions

    #define FLASH_DisableRunPowerDown HAL_FLASHEx_DisableRunPowerDown

    #define HAL_DATA_EEPROMEx_Unlock  HAL_FLASHEx_DATAEEPROM_Unlock

    #define HAL_DATA_EEPROMEx_Lock    HAL_FLASHEx_DATAEEPROM_Lock

    #define HAL_DATA_EEPROMEx_Erase   HAL_FLASHEx_DATAEEPROM_Erase

    #define HAL_DATA_EEPROMEx_Program HAL_FLASHEx_DATAEEPROM_Program

    HAL_FLASHEx_DATAEEPROM_Unlock();

    HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_BYTE, EEPROM_START + addr, (uint32_t)data); // Iterate use WORD/HALFWORD as appropriate

    HAL_FLASHEx_DATAEEPROM_Lock();

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

    5 replies

    Tesla DeLorean
    Guru
    January 28, 2019

    What part are you using?

    Is the memory blank at the location you are writing?

    What instruction specifically is faulting, and what are the register settings?

    ​

    You can also pull the lock/unlock out of the loop but I don't think that is the issue.​

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

    Hi Clive,

    I'm using STM32L072CZY6TR, The Memory I'm trying to write has a value of 0x00 and while loading the address it's going to hard fault. I had tried to pull the Lock/unlock out of loop but the issue didn't resolved. I'm sharing the debug screen with register values.

    0690X000006DGFxQAO.png

    Tesla DeLorean
    Tesla DeLoreanBest answer
    Guru
    January 28, 2019

    The processors R7 register would be of particular interest here.

    Perhaps use the EEPROM functions

    #define FLASH_DisableRunPowerDown HAL_FLASHEx_DisableRunPowerDown

    #define HAL_DATA_EEPROMEx_Unlock  HAL_FLASHEx_DATAEEPROM_Unlock

    #define HAL_DATA_EEPROMEx_Lock    HAL_FLASHEx_DATAEEPROM_Lock

    #define HAL_DATA_EEPROMEx_Erase   HAL_FLASHEx_DATAEEPROM_Erase

    #define HAL_DATA_EEPROMEx_Program HAL_FLASHEx_DATAEEPROM_Program

    HAL_FLASHEx_DATAEEPROM_Unlock();

    HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_BYTE, EEPROM_START + addr, (uint32_t)data); // Iterate use WORD/HALFWORD as appropriate

    HAL_FLASHEx_DATAEEPROM_Lock();

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

    I had a similar issue with a project, changing the languages to C99 and C++03 fixed this