Arm Community
Arm Community
  • Site
  • User
  • Site
  • Search
  • User
  • Groups
    • Research Collaboration and Enablement
    • DesignStart
    • Education Hub
    • Innovation
    • Open Source Software and Platforms
  • Forums
    • AI and ML forum
    • Architectures and Processors forum
    • Arm Development Platforms forum
    • Arm Development Studio forum
    • Arm Virtual Hardware forum
    • Automotive forum
    • Compilers and Libraries forum
    • Graphics, Gaming, and VR forum
    • High Performance Computing (HPC) forum
    • Infrastructure Solutions forum
    • Internet of Things (IoT) forum
    • Keil forum
    • Morello Forum
    • Operating Systems forum
    • SoC Design and Simulation forum
    • 中文社区论区
  • Blogs
    • AI and ML blog
    • Announcements
    • Architectures and Processors blog
    • Automotive blog
    • Graphics, Gaming, and VR blog
    • High Performance Computing (HPC) blog
    • Infrastructure Solutions blog
    • Innovation blog
    • Internet of Things (IoT) blog
    • Operating Systems blog
    • Research Articles
    • SoC Design and Simulation blog
    • Tools, Software and IDEs blog
    • 中文社区博客
  • Support
    • Arm Support Services
    • Documentation
    • Downloads
    • Training
    • Arm Approved program
    • Arm Design Reviews
  • Community Help
  • More
  • Cancel
Arm Community blogs
Arm Community blogs
Embedded blog Unlocking the STM32F4 Discovery board with OpenOCD
  • Blogs
  • Mentions
  • Sub-Groups
  • Tags
  • Jump...
  • Cancel
More blogs in Arm Community blogs
  • AI and ML blog

  • Announcements

  • Architectures and Processors blog

  • Automotive blog

  • Embedded blog

  • Graphics, Gaming, and VR blog

  • High Performance Computing (HPC) blog

  • Infrastructure Solutions blog

  • Internet of Things (IoT) blog

  • Operating Systems blog

  • SoC Design and Simulation blog

  • Tools, Software and IDEs blog

Tags
  • STM32F4DISCOVERY
  • Tutorial
Actions
  • RSS
  • More
  • Cancel
Related blog posts
Related forum threads

Unlocking the STM32F4 Discovery board with OpenOCD

Jens Bauer
Jens Bauer
December 24, 2014

Update: This will only be needed if you're running OpenOCD from before Jan 16 2015 on Big Endian machines. The problem is fixed in later builds.

I'm not sure this applies to all STMicroelectronics STM32F4 Discovery boards, but it does to mine.

For a while, I haven't been able to flash-program the board, because my only available tool is OpenOCD; I have no other options. The reason I could not flash-program the board, is that the unlock procedure for the board in OpenOCD, does not remove write-protection in the OPTCR register.

Fortunately, this is no problem; OpenOCD is scriptable, so it's possible to make it work.

Let's get started

Let's assume we're running OpenOCD's server and we telnet into the server...

In our first terminal window, we start OpenOCD as a server:

$ openocd -f board/stm32f4discovery.cfg

We open a second terminal window and telnet into the OpenOCD server:

$ telnet localhost 4444

Start by issuing a halt command; this will stop program-execution on the discovery board.

We can either issue a 'halt' or a 'reset halt'; In this example I use 'reset halt':

> reset halt

Next, let's use the official unlock procedure that ships with OpenOCD:

> stm32f2x unlock 0

We will also need to unlock the OPTCR register. This is done by writing a special sequence of data into the OPTKEYR register.

Right after unlocking it, we want to change the value of the OPTCR register:

> mww 0x40023C08 0x08192A3B; mww 0x40023C08 0x4C5D6E7F; mww 0x40023C14 0x0fffaaed

We can now flash-program the board; it is recommended to use the .elf format:

> flash write_image erase led-blinker.elf

This can all be put in a script; For instance, my script resembles the following:

# file: my_stm32f4.cfg

proc myUnlock () {

  stm32f2x unlock 0

  mww 0x40023C08 0x08192A3B; mww 0x40023C08 0x4C5D6E7F

  mww 0x40023C14 0x0fffaaed

}

proc myFlash {file_to_flash} {

    init; sleep 200

    reset halt; wait_halt

    myUnlock ()

    flash write_image erase "$file_to_flash"

    reset run; sleep 10

    shutdown

}

proc myRun () {

    init; sleep 200

    reset halt; wait_halt

    reset run; sleep 10

    shutdown

}

So you can program the discovery board using the following command on the command-line:

$ openocd -f board/stm32f4discovery.cfg -f my_stm32f4.cfg -c "myFlash myFile.elf"

Special thanks go to John Frenz from the my.st.com forum, for helping me find this solution.

Update: You can actually write the OPTCR value, so it will not be changed back after a reset. Again, John Frenz pointed me in the right direction on how to do this:

> reset halt; mww 0x40023C08 0x08192A3B; mww 0x40023C08 0x4C5D6E7F; mdw 0x40023c14

> mww 0x40023C14 0x0fffaaec; sleep 200; mdw 0x40023c14; mww 0x40023C14 0x0fffaaef; sleep 200; mdw 0x40023c14; reset halt; mdw 0x40023c14

Note: In order to write the correct value, 0x0FFFAAED, it is necessary to first write the value with the low two bits cleared, then writing the value with bit 1 set will do the trick. The lock bit can also be set on the second write, but it will automatically be set after a reset anyway.

The 'sleep' commands are necessary, in order to avoid getting errors when reading the status register using 'mdw'.

Anonymous
  • karthik dharma
    Offline karthik dharma over 6 years ago

    hi jens,

             i am karthik, i am working on stm32f407 100 pin controller. in this i have interfaced tft lcd 4.3 inch with 480*272. i had stuck in transmitting an image through uart to tft. can you please help with any demo code.

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
  • Jens Bauer
    Offline Jens Bauer over 8 years ago

    Sometimes it's worth it to keep going and not give up.

    The STM32F407 is quite advanced. I'm currently looking into how to use the timers.

    As timers can be cascaded, this opens up a number of new possibilities; the timers also have good DMA possibilites.

    Because of the large Flash memory and the ART-accelerator, one can benefit a lot from making constant look-up tables - they would be just as fast as if using SRAM.

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
  • Alban Rampon
    Offline Alban Rampon over 8 years ago

    Congratulations on cracking it. I know you've been working hard on this one

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
Embedded blog
  • The flexible approach to adding Functional Safety to a CPU

    James Scobie
    James Scobie
    Find out more about Functional Safety with SoC designs and Software Test Libraries.
    • November 8, 2022
  • The importance of building functional safety into your design right from the start

    Madhusudan Rao
    Madhusudan Rao
    Currently, there are many processors that are not designed with functional safety standards in mind and the use of these can lead to lengthy and costly qualification processes for safety relevant applications…
    • November 8, 2022
  • Arm Safety Ready program: Building confidence into your application

    Madhusudan Rao
    Madhusudan Rao
    To demonstrate Arm’s commitment to functional safety, we announce the launch of our Safety Ready program.
    • November 8, 2022