cancel
Showing results for 
Search instead for 
Did you mean: 

Read and Write operation in internal flash memory in STM32L5 using STM32CubeIDE

Nkhan.1
Associate II

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.

5 REPLIES 5
TDK
Guru

Are you erasing the relevant flash page before writing to it again? FLASH is not RAM.

Check the error code in pFlash.ErrorCode and go from there.

If you feel a post has answered your question, please click "Accept as Solution".

Hi,

Thank you for the reply.

In our application we need to store the 12 bytes data for 30 days at every 30 minutes (So total 16K memory = 8 pages) interval.  

1) From experiments we found that if the addresses in the same page are different then we can write on the flash without erasing it. Please correct us if this has some drawbacks here.

2) We are planning to 

  - Store the 2K memory in the RAM 

  - Store to flash only if the RAM buffer is full (Or device is planning to shutdown)

  - Erase the page to be written to flash

  - Increment the flash page address 

  - Clear the RAM buffer of 2K memory

 Please help us to conclude on the above approach.

3) If the device is shutting down how we can store that memory location such that after reboot we can write to the same page location? Please suggest what should be used here. RAM buffer of 2K memory

  Please help us to conclude on the above approach.

3) If the device is shutting down how we can store that memory location such that after reboot we can write to the same page location? Please suggest what should be used here.

I have to store the data at same address. And when writing to the same address it is showing the  FLASHIF_WRITING_ERROR with error code - 3.

QNGUY.1
Associate II

Can you check MCU address is 32bit or 64bit ?

I think usually STM32 MCU Address is 32bit and Data can be 64bit.

I see in your code such as Swap them.

void flash_write(uint32_t address, uint64_t data);

You can see the example in this X-CUBE-EEPROM for STM32L5xx MCU.

https://my.st.com/content/my_st_com/en/products/embedded-software/mcu-mpu-embedded-software/stm32-embedded-software/stm32cube-expansion-packages/x-cube-eeprom.license=1599698218214.product=X-CUBE-EEPROM.version=2.0.0.html

Yes.
If you feel a post has answered your question, please click "Accept as Solution".