2024-09-12 07:09 AM
Hello,
on an STM32F407 I am experiencing a Flash Erase/Write error. The odd things is that it happen only the first time after boot, the following times after the error I can write with no errors.
But, if I erase and write in the Init phase, before the FreeRTOS tasks start, it always work.
The write operation is performed by a state machine called from FREERTOS DefaultTask().
The first state Erase the flash, the second state write a structure stored on RAM.
I tried to change the size of the writed chunck (FLASH_TYPEPROGRAM_xxx) ant to use the HAL_FLASHEx_Erase() function but the problem persists.
I get the error from the write phase but FLASH_Erase_Sector() does not return a status so I think the error could be in the erase phase.
Any suggestion?
Thank you
#define FLASH_STORAGE_PAGE_SIZE 0x20000 //128kB
#define FLASH_STORAGE_ADDRESS 0x080E0000 //Punto all'inizio dell'ultima pagina in flash
#define FLASH_STORAGE_SECTOR FLASH_SECTOR_11
HAL_StatusTypeDef flash_erase(void)
{
HAL_StatusTypeDef res;
//Instantiate the FLASH_EraseInitTypeDef struct needed for the HAL_FLASHEx_Erase() function
res = HAL_FLASH_Unlock();
if (HAL_OK == res)
{
FLASH_Erase_Sector(FLASH_SECTOR_11, VOLTAGE_RANGE_3);
res |= HAL_FLASH_Lock();
}
return res;
}
HAL_StatusTypeDef storageSave(void)
{
uint32_t *pdata;
int sz = 0;
HAL_StatusTypeDef res;
res = flash_erase();
res |= HAL_FLASH_Unlock();
pdata = (uint32_t*)&m_ram_param;
m_ram_param.size = sizeof(PARAM_t);
uint32_t crc_val = HAL_CRC_Calculate(&hcrc, (uint32_t*)(&m_ram_param), (sizeof(PARAM_t)-4)/4);
m_ram_param.flashCRC = crc_val;
while (sz < sizeof(PARAM_t))
{
res |= HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, FLASH_STORAGE_ADDRESS + sz, (uint32_t)(*pdata));
pdata++;
sz += 4;
}
res |= HAL_FLASH_Lock();
return res;
}
2024-09-12 07:29 AM
What error do you get?
2024-09-12 07:58 AM
I get an HAL_ERROR from the first call of
HAL_FLASH_Program()
the calls that follows seems ok but data is not written correclty.
The error comes from FLASH_WaitForLastOperation() an the following flags are set:
Programming parallelism error: FLASH_FLAG_PGPERR
Programming sequence error: HAL_FLASH_ERROR_PGS