2015-03-03 09:44 AM
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 3and the result after step 2 is flashStatus == FLASH_ERROR_PGand 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-bytes2015-03-03 10:23 AM
/* 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();
2015-03-03 10:49 AM
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?Owen2015-03-03 10:53 AM
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.