cancel
Showing results for 
Search instead for 
Did you mean: 

How to store user data in the flash memory on STM32G0 using HAL

Sam Galam Robotics
Associate

Hi everyone,

I am working with a STM32G070 to control a small robot. I am currently coding a calibration function for the different motors of the robot and I would like to store the result of the calibration in the flash memory of the MCU, so that I do not have to redo the calibration process everytime I switch on the robot. Unfortunately I am missing something, but I can't achieve to store the data in the flash memory. I would be extremely grateful if someone could correct my code so that I can write (and read) in the flash memory. I use STM32CubeIDE for coding.

The functions I wrote are the following :

uint64_t flash_read(uint32_t address){

   return *((uint32_t*)address);

}

void flash_write(uint32_t address, uint64_t data){

   HAL_FLASH_Unlock();

   HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,address,data);

   HAL_FLASH_Lock();

}

void flash_page_erase(uint32_t page)

{

   HAL_FLASH_Unlock();

   uint32_t error = 0 ;

   FLASH_EraseInitTypeDef FLASH_EraseInitStruct =

       {

       .TypeErase = FLASH_TYPEERASE_PAGES,

      .Page = page,

      .NbPages = 1,

       };

   HAL_FLASHEx_Erase(&FLASH_EraseInitStruct,&error);

   HAL_FLASH_Lock();

}

Then in my main loop, I use the functions like this :

flash_page_erase(62) ;

flash_write(0x0801F060, (uint64_t)result) ;

Result is the value I am trying to store. During my tests, result = 81 (if you want me to do tests with other values, I will for sure).

To read the value I do the following :

uint64_t l = flash_read(0x0801F060) ;

When I read the value of l, the value is always equal to 1 instead of 81. I read the value with the Debugger of STM32CubeIDE.

Can someone correct me please ?

Many thanks guys !

1 REPLY 1
HMoens
Associate II

Hi, I want something similar. Did you figure out a solution?