cancel
Showing results for 
Search instead for 
Did you mean: 

PGERR writing user data option bytes STM32F103RE

okelly
Associate II
Posted on March 03, 2015 at 18:44

Hi all,

I am trying to read and write the STM32F103 user data bytes (Data0 and Data1 in Table 8 of STM32F10xxx PM0075 Programming manual).

I wrote this code to set Data0 to value 0x1B:

  FLASH_UnlockBank1();                                         // step 1

  flashStatus = FLASH_ProgramOptionByteData(0x1FFFF804, 0x1b); // step 2

  FLASH_LockBank1();                                           // step 3

and the result after step 2 is 

  flashStatus == FLASH_ERROR_PG

and the memory at 0x1FFFF804 has not changed.

Is there more that I should be doing to establish the proper context for executing step 2?

When I succeed, will the change to Data0 be immediately readable, or does the CPU need to go through a reset cycle for the updated option bytes to be visible?

At the assignment moment within FLASH_ProgramOptionByteData(), just before *address=data, we have FLASH_CR==OPTWRE|OPTPG and all the other control bits are zero.

Before the assignment, FLASH_SR==EOP; after the assignment, FLASH_SR==PGERR|EOP.

The FLASH_* functions are from stm32f10x_flash.c V3.6.1 .

Thank you for any advice or example code.

Owen

#flash #option-bytes
3 REPLIES 3
Posted on March 03, 2015 at 19:23

/* Unlock the Flash Program Erase controller */
FLASH_Unlock();
/* Clear All pending flags */
FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP|FLASH_FLAG_PGERR |FLASH_FLAG_WRPRTERR);
/* Erase all the option Bytes */
FLASHStatus = FLASH_EraseOptionBytes();
FLASHStatus = FLASH_ProgramOptionByteData(0x1FFFF804, 0x1B);
/* Generate System Reset to load the new option byte values */
NVIC_SystemReset();
or
/* Lock the Flash Program Erase controller */
FLASH_Lock();

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
okelly
Associate II
Posted on March 03, 2015 at 19:49

Thank you, that is incredibly helpful.

If I understand correctly then, in order to change Data0 and not affect other things, I must first read out DATA1, USER, RDP, WRP0, WRP1, WRP2, WRP3 and then write them back after the erase. Is that correct?

Owen

Posted on March 03, 2015 at 19:53

I haven't used STM32F1 parts actively for several years, but yes I believe you'd have to save whatever content you wish to preserve.

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