2024-07-24 05:10 AM
Hi, I'm trying to store some information in flash, I'm attaching the program. The problem arises when I use HAL_FLASH_Program function. Inside the FLASH_WaitLastOperation function (called by HAL_FLASH_Program), the PGSERR+PGAERR bits are set after the write operation. Do you have any ideas about this.?
Thanks in advance
Solved! Go to Solution.
2024-07-25 12:59 AM - edited 2024-07-27 12:44 AM
It works now, but I don't know why it didn't work before
void qFlashWrite(struct SensorCalibrationValue aconfig[]/*uint32_t *data*/, uint32_t dataSize) {
static FLASH_EraseInitTypeDef EraseInitStruct;
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
uint32_t firstPage = _qGetPage(FLASH_USER_START_ADDR);
uint32_t nbOfPages = _qGetPage(FLASH_USER_END_ADDR) - firstPage + 1;
uint32_t pageError = 0;
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.Banks = FLASH_BANK_2;
EraseInitStruct.Page = firstPage;
EraseInitStruct.NbPages = nbOfPages;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &pageError) != HAL_OK) {
qDebugFatalError("Cancellazione fallita", pageError);
}
uint32_t bytesWrited = 0;
uint32_t startAddress = FLASH_USER_START_ADDR;
uint64_t dataChunk = 0;
uint32_t dataToWriteAtTime = sizeof(dataChunk);
void *data = aconfig;
// Avendo cancellato solo una pagina è predisposto per scrivere al massimo
// una pagina, esce prima quando raggiunge la quantità di dati da scrivere!!
while (startAddress < FLASH_USER_END_ADDR) {
memcpy(&dataChunk, data, dataToWriteAtTime);
data += dataToWriteAtTime;
//dataChunk = 0;
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, startAddress,
dataChunk) == HAL_OK) {
startAddress = startAddress + dataToWriteAtTime;
} else {
qDebugFatalError("Scrittura fallita", HAL_FLASH_GetError());
}
bytesWrited += dataToWriteAtTime;
if(bytesWrited >= dataSize) break;
}
HAL_FLASH_Lock();
}
2024-07-24 07:12 AM
The right call is:
for (uint32_t i = 0; i < sizeDataToWrite; i += 8) {
HAL_StatusTypeDef e = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,
ADDR_FLASH_PAGE_127 + i, dataToWrite[i]);
if (e != HAL_OK) {
uint32_t ee = HAL_FLASH_GetError();
qDebugFatalError("Errore in scrittura flash", ee);
}
}
I made a mistake in writing the post
2024-07-24 11:17 AM
You don't say which version of the Cube_G4xx library you are using, but may not matter. In my last G4xx project (a few years ago, CubeG4 version 1.2), I had to explicitly clear all error flags before every call to HAL_FLASH_Program(), something like this:
HAL_FLASH_Unlock();
for ( ..... ) {
__HAL_FLASH_CLEAR_FLAG( FLASH_FLAG_ALL_ERRORS );
sts = HAL_FLASH_Program( ... );
if ( sts != HAL_OK ) {
// report error
}
}
HAL_FLASH_Lock();
Not the most satisfying answer, but it worked.
2024-07-25 12:31 AM
thanks for the suggestion, unfortunately it doesn't work, I will rewrite the code in another way
2024-07-25 12:59 AM - edited 2024-07-27 12:44 AM
It works now, but I don't know why it didn't work before
void qFlashWrite(struct SensorCalibrationValue aconfig[]/*uint32_t *data*/, uint32_t dataSize) {
static FLASH_EraseInitTypeDef EraseInitStruct;
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
uint32_t firstPage = _qGetPage(FLASH_USER_START_ADDR);
uint32_t nbOfPages = _qGetPage(FLASH_USER_END_ADDR) - firstPage + 1;
uint32_t pageError = 0;
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.Banks = FLASH_BANK_2;
EraseInitStruct.Page = firstPage;
EraseInitStruct.NbPages = nbOfPages;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &pageError) != HAL_OK) {
qDebugFatalError("Cancellazione fallita", pageError);
}
uint32_t bytesWrited = 0;
uint32_t startAddress = FLASH_USER_START_ADDR;
uint64_t dataChunk = 0;
uint32_t dataToWriteAtTime = sizeof(dataChunk);
void *data = aconfig;
// Avendo cancellato solo una pagina è predisposto per scrivere al massimo
// una pagina, esce prima quando raggiunge la quantità di dati da scrivere!!
while (startAddress < FLASH_USER_END_ADDR) {
memcpy(&dataChunk, data, dataToWriteAtTime);
data += dataToWriteAtTime;
//dataChunk = 0;
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, startAddress,
dataChunk) == HAL_OK) {
startAddress = startAddress + dataToWriteAtTime;
} else {
qDebugFatalError("Scrittura fallita", HAL_FLASH_GetError());
}
bytesWrited += dataToWriteAtTime;
if(bytesWrited >= dataSize) break;
}
HAL_FLASH_Lock();
}