Resetting STM32 option bytes

Without too much thinking I wrote a code to Argon’s STM32 ARM processor that write protects its flash content. After a minute I encountered a problem that I was unable to program anything new to the chip as it protects writing into memory also with a debugging tool effectively bricking the device. It took an hour to figure out how to restore the chip option bytes that determine if the flash memory is protected or not. JLink’s small utility that is supposed to restore STM32 option bytes didn’t appear to work for write protection so alternative solution was necessary to avoid replacing the chip on PCB.

After some googling (found this useful forum post) and reading chip data sheets I figured out how to do it manually with JLink Commander tool. I’m posting JLink command list here as I’m sure there are people needing this info, too.

(unprotect option bytes)
w4 0x40023c08,0x08192A3B
g
halt
w4 0x40023c08,0x4C5D6E7F
g
halt

(restore non-write protected state. one could also reset other option bytes here such as read protection or brown out reset settings by modifying the next line)

w2 0x40023C16,0xffff
g
halt

(write option bytes)
w1 0x40023C14,0xef
g
halt

I’m not sure if g and haltĀ commands are necessary but used them just in case. This should work at least with STM32F2 series and probably also STM32F3 and STM32F4 chips too.

JLink Commander when unlocking STM32F2 write protection

JLink Commander screenshot after unlocking STM32F2 write protection

7 thoughts on “Resetting STM32 option bytes

  1. High, I messed up flash option bytes and was unable to program my STM32F103C8T6.

    And now what? The solution you provided worked like a charm! My chip is back on track once again! Thank you very very very much!

    P.S.: So I can confirm this solution is applicable to STM32F10x chips as well.

  2. Hi,

    I locked up my system with readout protection with level 1 and reterived with your information. The steps are usefull. Thank you,

    Sekar

  3. Thank you man! Great solution !
    But can you please confirm, If I want to disable read output protection, I will have to modify value in this address: 0x40023C16 , is that right ?

  4. I saw something like a few weeks before, but you did detailed study, and your post seems to be more compelling than others.

    I’m impressed by the arguments you supplied
    in addition to the manner of your post. I like when articles are both informative and interesting, when even dull details are
    presented in an interactive manner. Well, it is surely on your post.

Leave a Reply to Sekar Sonachalam Cancel reply

Your email address will not be published. Required fields are marked *

* Copy This Password *

* Type Or Paste Password Here *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>