stm32g0, openocd, and mecrisp stellaris - part 5

Andrew ClappAndrew Clapp wrote 11/24/2021 at 00:54 • 2 min read • Like

So just a short one today to describe briefly how to change option bytes.  The documentation does not seem to have examples, but I was finally able to stumble my way into getting it working by looking at the methods and steps of a few different articles about changing option bytes for other stm32 chips.

With the stm32g031, you must use the stm32l4x flash driver.  This may vary for your chip, so research that.  Since I found that out the long way round, I hope it saves you time.  Using my new friend openocd, I wanted to enable the BOR enable bit in the option bytes (change from 0xfffffeaa to 0xffffffaa).  It's a one bit change, how hard could it be?

Earlier I showed how you can read option bytes. You can verify they are set at the default. This command:

openocd -d0 -f read-opt-bytes-1 -c "stm32l4x option_read 0 0x20" -c "exit"

gives this result:

Option Register: <0x40022020> = 0xfffffeaa

And this matches the datasheet RM0444 p. 81.  Hooray!

I've tried every combination of option_write and option_load I can think of based on what others had got to work for them, and I was finally able to toggle one single bit in the option bytes. I had to use these two commands in order:

openocd -f read-opt-bytes-1 -c "stm32l4x option_write 0 0x20 0xffffffaa 0xffffffaa" -c "reset" -c "exit"

openocd -f read-opt-bytes-1 -c "stm32l4x option_load 0 0x20" -c "reset" -c "exit"

Then we have the new values: Option Register: <0x40022020> = 0xffffffaa

Recall from earlier, that I had made my own read-opt-bytes-1 config file for openocd.  It basically just sets some defaults, but more importantly, let's me specify exactly which st-linkv2 interface I want to use when there are more than one plugged in at one time.  This is done with the hla_serial line in the config file...

hla_serial "\x55\x3f\x66\x06\x48\x3f\x57\x54\x12\x27\x07\x3f"

It would be nice if openocd could read this style ID instead: 55FF66064887575412270787, but I'll take what I can get. 


... also, the BOR default settings did help with power ups when the supply voltage ramps up slowly.  If you have something like that, be sure to turn on the BOR EN bit.  :-)