cancel
Showing results for 
Search instead for 
Did you mean: 

Flash programming - want to write a few words without erasing an entire page first, STM32G03x

dwillis
Associate II

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

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

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.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

12 REPLIES 12

> 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

dwillis
Associate II

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)..

MM..1
Chief II

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.

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

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

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.

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?

TDK
Guru

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.

If you feel a post has answered your question, please click "Accept as Solution".

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.

Thanks, that corresponds to what I am seeing. Back to the drawing board!