Flash programming - want to write a few words without erasing an entire page first, STM32G03x
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();
}