Skip to main content
IPast.1
Associate
April 4, 2023
Solved

Problem with Flash write STM32G474RE

Dear colleagues,

I woild like to ask you help me with may issue below:

Why do I lose data when writing flash memory and an error occurs my code in the attachment.

Code seems good, but I am sure that I loose something.

In memory loosed letters "llo" from "Hello".

Thnak you.

With Best regards

Ivan

This topic has been closed for replies.
SStorBest answer
Senior
April 6, 2023

You have to increase your address pointer by 8 (not 4) for writing 64bit data and it has to be aligned on 64bit boundary always ((address & 0x00000007) == 0)

		if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address,data_union.data64[index_1]) == HAL_OK)
		{
			HAL_Delay(1);
			address = address + 4; //(FLASH_ROW_SIZE * sizeof(uint64_t));
			index_1 = index_1 + 1;
		}

6 replies

ST Technical Moderator
April 5, 2023

Hello @IPast.1​,

Welcome to the STM32 Community.

I advise you to refer to the Flash examples that will help you on the STM32CubeG4 MCU package:

STM32CubeG4/Projects/NUCLEO-G474RE/Examples/FLASH at master · GitHub

Imen

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Thanks
IPast.1
IPast.1Author
Associate
April 6, 2023

Hi.

Thnak you!

I already saw this examples.

https://github.com/STMicroelectronics/STM32CubeG4/blob/master/Projects/NUCLEO-G474RE/Examples/FLASH/FLASH_FastProgram/Src/main.c

Why in fast mode use this line:

Address = Address + (FLASH_ROW_SIZE*sizeof(uint64_t));

Why in FLASH_TYPEPROGRAM_FAST mode - is data written to addresses in 256 units , despite the fact that it can only be written page by page , and the difference between page addresses is 0x800 ? But at the same time this example working good.

SStorBest answer
Senior
April 6, 2023

You have to increase your address pointer by 8 (not 4) for writing 64bit data and it has to be aligned on 64bit boundary always ((address & 0x00000007) == 0)

		if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address,data_union.data64[index_1]) == HAL_OK)
		{
			HAL_Delay(1);
			address = address + 4; //(FLASH_ROW_SIZE * sizeof(uint64_t));
			index_1 = index_1 + 1;
		}

IPast.1
IPast.1Author
Associate
April 6, 2023

Thenk you for reply. Yes this is good advice , after spent some time under code construction with for loop and adress + 8 achieved a good result. But also I think that first I should align a structure size and bring it to a multiplicity of 32 bits. Please, give me advice how to use this statement in loop when I write ((address & 0x00000007) == 0) ?

With best regards,

Ivan.

Senior
April 6, 2023

Statement (address & 0x00000007) == 0) is only for correct alignment check of your address pointer if you use the 64bit write command FLASH_TYPEPROGRAM_DOUBLEWORD (8 Bytes)

If start of your flash data block is correct aligned you don't need this check because all multplied 64bit data are always aligned.

So you can link the start address of your union to page 40 in your example (see code below).

This allows access to aligned 64bit data64 and struct data in user_data parallel in variable flashdata_union.

typedef struct {
	uint32_t mfgData;
	uint8_t comData;
	uint8_t ID_Data;
	char name[100];
} myStruct;
 
typedef union {
	myStruct user_data;
	uint64_t data64[256];	// 8 x 256Bytes = 2kB (1 Page complete)
	//uint32_t data64[sizeof(myStruct) / sizeof(uint32_t)];
	//uint64_t data64[sizeof(myStruct) / sizeof(uint64_t)];
	//uint64_t data64[sizeof(myStruct) * 2];
} myUnion;
 
myUnion flashdata_union @ 0x08014000;	// use Page 40

IPast.1
IPast.1Author
Associate
April 7, 2023

Ok, its clear!

Thank you so much!