2023-04-04 01:31 PM
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
Solved! Go to Solution.
2023-04-06 09:32 AM
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;
}
2023-04-05 01:34 PM
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
2023-04-06 02:19 AM
Hi.
Thnak you!
I already saw this examples.
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.
2023-04-06 09:32 AM
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;
}
2023-04-06 01:32 PM
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.
2023-04-06 02:26 PM
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
2023-04-07 02:22 AM
Ok, its clear!
Thank you so much!