Skip to main content
Associate II
September 2, 2023
Question

Need Guidance on EEPROM Implementation in STM32L4R5ZI-P Microcontroller

  • September 2, 2023
  • 29 replies
  • 11280 views

Hello STMicroelectronics community,

I am currently working on a project that involves using EEPROM memory with an STM32 microcontroller, specifically [STM32L4R5ZI-P]. microcontroller that does not have internal EEPROM, so I need to use the Flash memory for EEPROM emulation. I've installed the xCube EEPROM package, which provides driver layers and specifies the memory address to use.

However, I'm currently facing an issue with not able write or read from Address[0x080FF000U] , while working with flash that is returning with an error = 160, i will be sharing the snippet for your reference.

with an error valuewith an error valueScreenshot (8).pngScreenshot (6).pngIntializationIntialization

HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
{
HAL_StatusTypeDef status;
uint32_t prog_bit = 0;

/* Process Locked */
__HAL_LOCK(&pFlash);

/* Check the parameters */
assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));

/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);

if(status == HAL_OK)
{
pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;

/* Deactivate the data cache if they are activated to avoid data misbehavior */
if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != 0U)
{
/* Disable data cache */
__HAL_FLASH_DATA_CACHE_DISABLE();
pFlash.CacheToReactivate = FLASH_CACHE_DCACHE_ENABLED;
}
else
{
pFlash.CacheToReactivate = FLASH_CACHE_DISABLED;
}

if(TypeProgram == FLASH_TYPEPROGRAM_DOUBLEWORD)
{
/* Program double-word (64-bit) at a specified address */
FLASH_Program_DoubleWord(Address, Data);
prog_bit = FLASH_CR_PG;
}
else if((TypeProgram == FLASH_TYPEPROGRAM_FAST) || (TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST))
{
/* Fast program a 32 row double-word (64-bit) at a specified address */
FLASH_Program_Fast(Address, (uint32_t)Data);

/* If it is the last row, the bit will be cleared at the end of the operation */
if(TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST)
{
prog_bit = FLASH_CR_FSTPG;
}
}
else
{
/* Nothing to do */
}

/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);

/* If the program operation is completed, disable the PG or FSTPG Bit */
if (prog_bit != 0U)
{
CLEAR_BIT(FLASH->CR, prog_bit);
}

/* Flush the caches to be sure of the data consistency */
FLASH_FlushCaches();
}

/* Process Unlocked */
__HAL_UNLOCK(&pFlash);

return status;
}

 

 


HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
{
/* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
Even if the FLASH operation fails, the BUSY flag will be reset and an error
flag will be set */

uint32_t tickstart = HAL_GetTick();
uint32_t error;

while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))
{
if(Timeout != HAL_MAX_DELAY)
{
if((HAL_GetTick() - tickstart) >= Timeout)
{
return HAL_TIMEOUT;
}
}
}

error = (FLASH->SR & FLASH_FLAG_SR_ERRORS);

if(error != 0u)
{
/*Save the error code*/
pFlash.ErrorCode |= error;

/* Clear error programming flags */
__HAL_FLASH_CLEAR_FLAG(error);

return HAL_ERROR;
}

/* Check FLASH End of Operation flag */
if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
{
/* Clear FLASH End of Operation pending bit */
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
}

/* If there is an error flag set */
return HAL_OK;
}

here i have provided the function also, where i am facing an error.

could please help me to resolve this issue.

can you please provide any reference to work with microcontroller which we are working.

Thank you.

 

This topic has been closed for replies.

29 replies

Issamos
Super User
September 2, 2023

Hello @yashaswini 

You can find some help taking a look to this post .

Best regards

II

 

Associate II
September 2, 2023

Hello,@lssamos

Thank you for your respose and information that you have provided.

TDK
September 2, 2023

You must write 64 bits at a time at addresses that are 64-bit aligned.

TDK_0-1693659653888.png

 

"If you feel a post has answered your question, please click ""Accept as Solution""."
Associate II
September 2, 2023

hello ,

while try to write at that time ,i am facing that error.The code which i mentioned in my post has HAL_FLASH_Program[ Program double word or fast program of a row at a specified address]. in that facing an issue,

 /* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);

Thank you.

Associate II
September 4, 2023

Hello STMicroelectronics community,

Can i get another reference check with this, EEPROM emulation on STM32L4R5ZI-P.

Could you please provide guidance or assistance in resolving this issue?

Associate II
September 9, 2023

hello,

I have tried this shrink the FLASH but still facing same issue.

Screenshot (14).png  

this way i have  made changes is this correct or need to change, help me to debug this issue.

Thank you,

Associate II
September 9, 2023

Hello ST Community,

Can anyone suggest a solution for this , I am stuck in this ,atleast if i am doing any mistake while writing open for your suggestion's

Can I get any reference for this microcontroller.

Thanks in Advance,.

 

Pavel A.
September 9, 2023

Documentation on this STM32 says that its flash page size can be 4 or 8 KB depending on some mode.

Are you in the right mode for 4KB page size as you assume?

 

Associate II
September 11, 2023

hello,

Yes, I am using 4KB as a size.

Total Flash size is of 2MB,in that for EEPROM Emulation Using 4KB ,as mentioned in datasheet.

Do I need to go through with any other conditions to check?

Thank you. 

Pavel A.
September 11, 2023

Can you find and run some FLASH example for your board first, to verify that erasing a sector and write work correctly? 

Pavel A.
September 13, 2023

@yashaswini  Does this occur with a simple example (such as some ST Cube FLASH example for a similar board), not the "eeprom emulation"?

 

Associate II
September 13, 2023

Yes, for simple example not for an eeprom emulation .Screenshot (16).png

I have attached a snippet of that for reference, in this I'm trying write data to flash in that erasestatus it returns error.

 

Associate II
September 13, 2023

Screenshot (17).png

TDK
September 26, 2023

I would recommend starting with an ST example that works and go from there. There are a number of basic errors with the code you've presented:

  • Page 1 almost certainly contains active code and erasing it will cause the program to malfunction.
  • Passing NULL as the error parameter is invalid.
  • pageAddress is no longer being used.

Here is one such example:

https://github.com/STMicroelectronics/STM32CubeL4/blob/4ca3922f320dbb2c4f3f9486f4a094f36125f31d/Projects/STM32L476G-EVAL/Examples/FLASH/FLASH_EraseProgram/readme.txt

 

"If you feel a post has answered your question, please click ""Accept as Solution""."
Associate II
September 27, 2023

Hello,

Thanks for the response and i will check with  the above reference.

Thank you

 

TDK
September 28, 2023

Perhaps go to the Documentation tab and look for documents there. Or look online for examples. Lots of resources out there if you look for them. Good luck.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Associate II
September 29, 2023

Hello,

Thanks for response, I will check with that.

Thank You