cancel
Showing results for 
Search instead for 
Did you mean: 

Writing to the FLASH_OPTR to switch banks

Ethan-D
Visitor

I am designing a system that will utilize the dual bank feature of the STM32 devices to make a robust system for OTA updates. I am currently using a STM32U545 for my project and have questions about writing to the FLASH_OPTR to change the SWAP_BANK bit.

My setup has an external microcontroller connected to my STM32U545 that interfaces through UART and has control of the BOOT0 pin and the RST pin of the STM32 device. I was confused by the statement in the AN3155 pg. 20 when it talked about the Write Memory command and said "If the Write Memory command is issued to the option byte area, all bytes are erased before writing the new values, and at the end of the command the bootloader generates a system
reset to take into account the new configuration of the option bytes". What does it mean by all bytes are erased? 

My current plan was to use the read memory command and read the register FLASH_OPTR at 0x4002 2040 and then read bit 20 of that register(SWAP_BANK bit) then perform an operation on that bit to swap the banks. Then I would write the new value of the register using the Write Memory Command. My initial thought was that I only needed to read 4 bytes and then write 4 bytes, but after researching I am concerned if that would be problematic. Will it be a problem to only try to read/write the 4 bytes of the reigister?

 

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Super User

> What does it mean by all bytes are erased? 

Option bytes are in flash. It needs to erase the entire block of option bytes and then rewrite them all when any of them are modified because that is how flash operates. Only option bytes are erased, not user program memory.

The registers are an interface to the option bytes. The actual option bytes reside in flash.

You can only read/write 4 bytes of the register. No problems there. Provided you unlock them first. Option bytes still need to be loaded after you write to the registers.

See "7.4.2 Option-byte programming" in the reference manual for the correct procedure to modify option bytes.

TDK_0-1764963125738.png

 

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

View solution in original post

2 REPLIES 2
TDK
Super User

> What does it mean by all bytes are erased? 

Option bytes are in flash. It needs to erase the entire block of option bytes and then rewrite them all when any of them are modified because that is how flash operates. Only option bytes are erased, not user program memory.

The registers are an interface to the option bytes. The actual option bytes reside in flash.

You can only read/write 4 bytes of the register. No problems there. Provided you unlock them first. Option bytes still need to be loaded after you write to the registers.

See "7.4.2 Option-byte programming" in the reference manual for the correct procedure to modify option bytes.

TDK_0-1764963125738.png

 

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

Thanks.