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 screenshot after unlocking STM32F2 write protection