cancel
Showing results for 
Search instead for 
Did you mean: 

Copy RAM to FLASH

Anonymous
Associate II

Hello everyone!

I am trying to copy RAM data to FLASH. I am using STM32L412KB evaluation module. I have made a USB of my own. I am able to talk over the USB. It's a 34Kb mass storage device. I can make a .txt file and store it but, every time I connect the USB after power cycle, it has to be formatted as everything is in the RAM. Basically, I want my USB to use the FLASH storage instead of RAM. So, there's 2 things here that I have to figure out.

Firstly, I'll connect my USB to the computer, make a .txt file and save it. Now, I'd copy the entire RAM to FLASH through a memcpy or something. HOW?

Secondly, I'll want my USB to use Flash storage instead of RAM. With flash having all the RAM data, it'll now be able to retain data after power cycle. HOW?

Thank you!

2 REPLIES 2
TDK
Guru

In short, you need to:

  • unlock the flash
  • erase enough flash pages to store the data
  • copy the data over to those pages
  • (optionally, relock the flash)

Look at the HAL_FLASH functions.

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

The memory needs to be in an erased state, you get one shot of writing each "line"

Here writing memory on an L432, should be a close proxy, but surely the L4Cube libraries have examples

void FLASH_ProgramBuffer(uint32_t Address, uint32_t Size, uint8_t *Buffer)
{
  uint64_t *p = (uint64_t *)Buffer;
 
  /* Unlock the Flash to enable the flash control register access *************/
  HAL_FLASH_Unlock();
 
  while(Size)
  {
    if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, *p++) != HAL_OK)
    {
       // Fail Handler
      break;
    }
    Address += 8;
    Size -= min(8, Size);
  }
 
  /* Lock the Flash to disable the flash control register access (recommended
     to protect the FLASH memory against possible unwanted operation) *********/
  HAL_FLASH_Lock();
}

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