2021-06-04 02:52 AM
I've got the following structure to write to memory:
```
#define FLASH_CONFIG_START_ADDR ((uint32_t) 0x080E0000)
#define FLASH_CONFIG_END_ADDR ((uint32_t) 0x080E0040)
union NVRAM {
Config_t config;
uint8_t data[512];
} DevNVRAM;
```
I've used this code for writing to memory
```
uint32_t l_address = FLASH_CONFIG_START_ADDR;
uint8_t l_index = 0x00;
l_address = FLASH_CONFIG_START_ADDR;
HAL_FLASH_Unlock();
FLASH_Erase_Sector(FLASH_SECTOR_11, VOLTAGE_RANGE_1);
while (l_address < FLASH_CONFIG_END_ADDR)
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, l_address,
DevNVRAM.data[l_index]) == HAL_OK) {
l_index += 1;
l_address += 1;
}
HAL_FLASH_Lock();
```
and then I simply read from the same block of memory.
```
l_address = FLASH_CONFIG_START_ADDR;
l_index = 0x00;
while (l_address < FLASH_CONFIG_END_ADDR) {
DevNVRAM.data[l_index] = *(uint8_t*) l_address;
l_index += 1;
l_address += 1;
}
```
The problem is the following, after I have read the whole sector, data becomes different, in particular, it is filled with zeros. What is the problem with code?
P.S. How can I get first address of memory blovk in the sector without hard-coddding?
Solved! Go to Solution.
2021-06-05 08:12 AM
By default the FLASH memory is configured as write-through. Because of that cache cleaning is not necessary, but after erasing or programming still a cache invalidation needs to be done on the corresponding FLASH memory addresses. For data memory use SCB_InvalidateDCache_by_Addr() to invalidate D-cache.
2021-06-05 10:24 PM
I have tried EraseInitStruct.Sector = FLASH_SECTOR_11; but this didn't help.
2021-06-05 10:34 PM
I have added SCB_InvalidateDCache_by_Addr(FLASH_CONFIG_START_ADDR, FLASH_CONFIG_END_ADDR - FLASH_CONFIG_START_ADDR);
After Locking Flash. Still didn't fix the problem.
2021-06-06 02:22 AM
I actually think that it should work since this function is declared in stm32f7xx_hal_flash.c file
2021-06-06 03:04 AM
The problem was in address declaration. it is actually 0x81C000(viewd in st-link utility)