cancel
Showing results for 
Search instead for 
Did you mean: 

Trouble programming flash with HAL_FLASH_Program

TMalt
Associate II

I have troubles writing succesfully to flash on a STM32G07 chip.

The application is similar to this:

uint64_t data;

HAL_FLASH_Unlock();

HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, addr, data);

HAL_FLASH_Lock();

I started getting hardfault in lock because of error in programming. I found out that this was related to timeout on the wait for operation to finish inside program function. So I added a check that program returning HAL_OK before calling lock . But this just moved the problem.

The behavious is very strange.

I can see that PGAERR and PGSERR in the FLASH SR register are being set.

My data is aligned at a 64-bit boundry I have added check that both data and the address i program to will have addr & 0x07 == 0.

I suspect some other interrupt could interefere in some way. But adding __disable_irq(); does not help.

Data is programmed like 25-50% of the times rest I simply get no data programmed.

11 REPLIES 11

Hello,

Could you solve your problem ? I have a exactly same problem right now, even IWDG is not enabled.

ELeyl.1
Associate II

I could get HAL_FLASH_Program to write WORDS, as soon as I moved to doublewords the last error returned is PGA | PGS flags in the SR.

PGA = alignment

PGS = sequence

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 libary, or at least how I am using it. Here is my code:

 // STM32F411RE try to write doubleword into FLASH

 // The flashing works for WORDS but not DOUBLEWORDS

 #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();

}

 }