How to write doublewords to flash on an STM32F411RE
I could get HAL_FLASH_Program to write WORDS, as soon as I moved to FLASH_TYPEPROGRAM_DOUBLEWORD the last error returned is alwasy PGA | PGS flags in the SR. PGA = alignment, PGS = sequence. I referred to every internet article I could to figure out the problem.
AFAIK, I have done everything correctly. There seems to be a problem with trying to write to FLASH on a 32f411RE. Worked hard to figure it out, I think there is something wrong with the HAL libary, or at least how I am using it. Here is my code:
#define SECTOR 7
#define ADDRESS 0x08060000
uint32_t lastError = 0;
uint64_t valueToWrite = ~0ULL;
uint64_t valueToRead = 0;
HAL_StatusTypeDef status = HAL_ERROR;
valueToRead = * ( uint64_t * ) ADDRESS;
if ( valueToRead == ~0ULL )
{
FLASH_EraseInitTypeDef pEraseInit = { 0 };
pEraseInit.TypeErase = TYPEERASE_SECTORS;
pEraseInit.Sector = FLASH_SECTOR_7;
pEraseInit.NbSectors = 1;
pEraseInit.VoltageRange = FLASH_VOLTAGE_RANGE_3;
uint32_t SectorError = 0;
do
{
HAL_FLASH_Unlock();
status = HAL_FLASHEx_Erase(&pEraseInit, &SectorError);
HAL_FLASH_Lock();
valueToRead = * ( uint64_t * ) ADDRESS;
} while( (status !=HAL_OK) || (SectorError != ~0UL ) || ( valueToRead != ~0ULL ) );
HAL_FLASH_Unlock();
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, ADDRESS, valueToWrite );
HAL_FLASH_Lock();
lastError = 0;
if ( status != HAL_OK )
{
lastError = HAL_FLASH_GetError();
}
}