This is my first post here, sorry if format or something is wrong. I succeed writing and reading flash memory of the STM32F4 Discovery following the advises of our colleges here and here (both posts explain the same way):
__attribute__((__section__(".user_data"))) const char userConfig[64];
[...]
void Write_Flash(uint8_t data)
{
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR );
FLASH_Erase_Sector(FLASH_SECTOR_6, VOLTAGE_RANGE_3);
HAL_FLASH_Program(TYPEPROGRAM_WORD, &userConfig[0], data);
HAL_FLASH_Lock();
}
[...]
dataSize=(sizeof dataBuffer) / (sizeof *dataBuffer);
for (i=0;i<dataSize;i++) {
dataBuffer[i]=i+1;
}
Write_Flash(dataBuffer[0]);
The above code works fine and writes 1 to userConfig[0]
. From this point, I have the following problem. I can only write one byte in the flash memory and don't know how to write more. I've tried to change the address of the HAL_FLASH_Program(TYPEPROGRAM_WORD, &userConfig[0], data);
but only works for &userConfig[0]
This is my attempt to write several bytes with no success:
__attribute__((__section__(".user_data"))) const char userConfig[64];
[...]
void Write_Flash(uint8_t data, uint8_t i)
{
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR );
FLASH_Erase_Sector(FLASH_SECTOR_6, VOLTAGE_RANGE_3);
HAL_FLASH_Program(TYPEPROGRAM_WORD, &userConfig[i], data);
HAL_FLASH_Lock();
}
[...]
dataSize=(sizeof dataBuffer) / (sizeof *dataBuffer);
for (i=0;i<dataSize;i++) {
dataBuffer[i]=i+1;
Write_Flash(dataBuffer[i],i);
}
Thanks in advance for your help.
Thanks to @phyloflash I got the answer. When you call
HAL_FLASH_Program
you specify the size of the data to be written and the address of the first byte of this data. In my case,HAL_FLASH_Program(TYPEPROGRAM_WORD, &userConfig[0], data);
a word means 4 bytes so the first 4 bytes ofuserConfig
were written. This also implies that parameter defined asuint8_t data
has to be consistent with the size of the data you are going to write, so it should beuint32_t data
.I've modified the code to take into account these considerations. Code below is proved and working: