Skip to main content
Anonymous
Associate III
May 28, 2020 4:49 PM UTC
Question

Copy RAM to FLASH

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!

This topic has been closed for replies.

2 replies

TDK
May 28, 2020 5:02 PM UTC

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""."
Tesla DeLorean
Guru
May 28, 2020 5:37 PM UTC

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..