Skip to main content
arthhh
Associate II
May 11, 2022
Solved

Problems saving variables to FLASH in STM32G030F6P6

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)...

This topic has been closed for replies.
Tesla DeLorean
Tesla DeLoreanBest answer
Guru
May 11, 2022

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

2 replies

gbm
Super User
May 11, 2022

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

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
Tesla DeLorean
Tesla DeLoreanBest answer
Guru
May 11, 2022

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