2022-02-25 02:46 AM
I want to program a double word (a calibration value) at the top of internal flash (STM32G03x) without erasing the entire page first.
I understand that only 1's can be set to 0, not vice versa. It may be assumed that the address in question already contains all F's and that once written, it doesn't need to be written again.
I am able to erase a page and write a double word without problems as in the code below, but if I want to write a single value without having erased immediately prior, I get a flash error =8 (Flash SR = Bit 3 PROGERR: Programming error)
The question is - is there any way to unlock the MCU in such a way it's OK to program the single double word ? (the option bits are set to no protection)
{
HAL_FLASH_Unlock();
// Erase top 2K page (64K device)
FLASH_EraseInitTypeDef eraseStruct =
{ .TypeErase = FLASH_TYPEERASE_PAGES, .Banks = FLASH_BANK_1, .Page = 31, .NbPages = 1 };
uint32_t error;
HAL_FLASHEx_Erase(&eraseStruct, &error); ///<< I don't want to do this!
if (error)
{
__BKPT(0);
}
uint32_t address = 0x800FF00;
uint64_t data = 0;
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address, data) != HAL_OK)
{
uint32_t err = HAL_FLASH_GetError();
__BKPT(0);
}
HAL_FLASH_Lock();
}
Solved! Go to Solution.
2022-02-25 05:02 PM
You can only program a double word at a time, and, due to the ECC, a double word can only be programmed once, even if that one time was all 0xFF's. You cannot knock 1s to 0s at will, you only have one shot.
PROGERR suggests the address was previously programmed after an erase.
2022-02-25 03:30 AM
> if I want to write a single value without having erased immediately prior, I get a flash error =8
What do you mean by "single value" here? To what address do you want to write? Is it an address of a doubleword which hasn't been written after erase?
JW
2022-02-25 04:06 AM
Yes, the code above shows a 64 bit word write at 0x800FF00, i.e properly aligned. This code works if the page is erased immediately beforehand. But I want to be able to write it without the Erase() call, because the Erase() call erases the entire page, and I just want to clear some bits from their FF state at a single location (at the minimum granularity possible, 64 bits)..
2022-02-25 04:17 AM
I mean this work , but not after flash program software load and debug. You need power off and clean start, or clear some flash flags before prog.
2022-02-25 06:03 AM
Journal writes over sections of memory you have not written post erasure. Align your data with the word width of the flash-lines
The ECC means you can't knock-down '1' bits repeatedly within the same words
2022-02-25 06:37 AM
I'm sorry, I don't understand your answer, my data is aligned and my code works provided the page is erased immediately before i try writing it.
2022-02-25 07:23 AM
Yes, I was hoping there might be a flag to set. Following your suggestion I tried setting the FLASH_ACR_PROGEMPTY flag in the ACR, to make it look as though the area is 'new' but that still gives me the PROGERR. Is there anyone who knows what it or they might be ? Or even if it is possible or not?
2022-02-25 05:02 PM
You can only program a double word at a time, and, due to the ECC, a double word can only be programmed once, even if that one time was all 0xFF's. You cannot knock 1s to 0s at will, you only have one shot.
PROGERR suggests the address was previously programmed after an erase.
2022-02-25 11:30 PM
I dont mean random flag set , you need debug HAL_FLASH_Program and locate line with error return. Here you see for example PGERR flag is set before start prog.
Then you need clear it before call HAL_FLASH_Program.
2022-02-28 03:10 AM
Thanks, that corresponds to what I am seeing. Back to the drawing board!