cancel
Showing results for 
Search instead for 
Did you mean: 

Problems saving variables to FLASH in STM32G030F6P6

arthhh
Associate II

I've just bought a STM32G030F6P6 dev board and I'm testing its cababilities.

And one thing I'm having problems with is saving my data to the FLASH.

The first time it writes as espected in the FLASH, but not in the second, HAL_FLASH_Program returns HAL_ERROR.

#include "main.h"
 
uint32_t Address = 0x0800F800;
uint32_t buffer = 0x12345678;
uint32_t data1 = 0;
uint32_t data2 = 0;
HAL_StatusTypeDef status;
 
int main(void)
{
  HAL_Init();
 
  SystemClock_Config();
 
  data1 = *(__IO uint32_t *)Address;
  status = HAL_FLASH_Unlock();
  status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, buffer);
  status = HAL_FLASH_Lock();
  data2 = *(__IO uint32_t *)Address;
 
  while (1)
  {
  }
}
 
void SystemClock_Config(void)...

1 ACCEPTED SOLUTION

Accepted Solutions

>>The first time it writes as expected in the FLASH, but not in the second, HAL_FLASH_Program returns HAL_ERROR

You get ONE write per 64-bit line after an Erase, then you need to move onto the next.

Perhaps advance Address to the point where you read 0xFFFFFFFF, or zero, whatever the erased state is on the G0

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

View solution in original post

2 REPLIES 2
gbm
Lead III

Looks like you don't erase the Flash before programming, which is the first problem. Also, be careful with data alignment.

>>The first time it writes as expected in the FLASH, but not in the second, HAL_FLASH_Program returns HAL_ERROR

You get ONE write per 64-bit line after an Erase, then you need to move onto the next.

Perhaps advance Address to the point where you read 0xFFFFFFFF, or zero, whatever the erased state is on the G0

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