cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with Flash write STM32G474RE

IPast.1
Associate II

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

1 ACCEPTED SOLUTION

Accepted Solutions
SStor
Senior

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;
		}

View solution in original post

6 REPLIES 6
Imen.D
ST Employee

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

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen

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.

SStor
Senior

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;
		}

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.

SStor
Senior

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
Associate II

Ok, its clear!

Thank you so much!