Read and Write operation in internal flash memory in STM32L5 using STM32CubeIDE
Hello STM Team,
I am using stm32L5 and I am writing an application in which I have to store 8 byte data (On 8 byte aligned location) into internal flash memory in dual bank mode.
For storing the data I am facing issue while trying to write data at same address. We are getting the FLASHIF_WRITING_ERROR error when we are writing again on this location.
Code
#define DESIGNATED_ADDRESS 0x0803F800
uint32_t value = 50;
uint64_t addr = DESIGNATED_ADDRESS;
void flash()
{
uint32_t dest = 0;
if((FLASH_WRITE(addr, &value,2) != 0)) {
print("Error\n");
}
dest = FLASH_READ(addr);
print("data = %d", dest);
value++;
}
FLASH_WRITE(uint32_t destination, uint32_t *p_source, uint32_t length)
{
uint32_t status = FLASHIF_OK;
uint32_t i = 0;
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
/* DataLength must be a multiple of 64 bit */
for (i = 0; (i < length / 2) /*&& (destination <= (USER_FLASH_END_ADDRESS - 8))*/; i++)
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, destination, *((uint64_t *)(p_source + 2*i))) == HAL_OK)
{
if (*(uint64_t*)destination != *(uint64_t *)(p_source + 2*i))
{
/* Flash content doesn't match SRAM content */
status = FLASHIF_WRITINGCTRL_ERROR;
break;
}
/* Increment FLASH destination address */
destination += 8;
}
else
{
/* Error occurred while writing data in Flash memory */
status = FLASHIF_WRITING_ERROR;
break;
}
}
HAL_FLASH_Lock();
return status;
}
uint32_t FLASH_READ(uint32_t address)
{
return *(uint32_t*)address;
}
Queries:
1) Please let us know what changes we should make to make flash read/write sucessful in the above code.
