STM32F4 Discovery Flash and Debug not possible

I can`t get open ocd to flash my discovery board. I get various errors which are not really reproducible.

  1. Communication to device seems to be kind of unstable. There are huge variations in the time the board needs to answer.

  2. Unlocking the device (monitor stm32f2x unlock 0) only works half of the time.

  3. Usually even if the device is unlocked i can’t erase the flash.

  4. Monitor flash protect 0 0 11 off doesn’t work → monitor flash info 0 tells that the flash is still protected.

  5. Monitor flash info 0 sometimes shows 11 and sometimes 67 flash cells.

  6. Communication fails randomly → font getting read and no more answers.

Open OCD:

Open On-Chip Debugger 0.7.0 (2013-05-05-10:44)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.sourceforge.net/doc/doxygen/bugs.html
adapter speed: 1000 kHz
Info : This adapter doesn't support configurable speed
Info : STLINK v2 JTAG v17 API v2 SWIM v0 VID 0x0483 PID 0x3748
Info : Target voltage: 2.880000
Info : stm32f4x.cpu: hardware has 6 breakpoints, 4 watchpoints


------------------------------------------------------------------
GDB Script:

set tdesc filename gdb/workaround.xml
set remote hardware-breakpoint-limit 6
set remote hardware-watchpoint-limit 4
target remote localhost:3333


monitor halt
monitor poll

monitor stm32f2x unlock 0
monitor reset halt

monitor flash protect 0 0 11 off
monitor flash info 0
monitor flash erase_sector 0 0 11
monitor flash write_image bin/bareCortexM.elf
monitor flash protect 0 0 11 on
monitor soft_reset_halt

------------------------------------------------------------------
Output from GDB-Script:

source gdb/stm32f407vg.script
0x00000000 in ?? ()
target was in unknown state when halt was requested
target state: halted
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0xfffffffe msp: 0xfffffffc
background polling: on
TAP: stm32f4x.cpu (enabled)
target state: halted
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0xfffffffe msp: 0xfffffffc
Device Security Bit Set
timed out waiting for flash
stm32f2x failed to unlock device
target state: halted
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0xfffffffe msp: 0xfffffffc
Device Security Bit Set
cleared protection for sectors 0 through 11 on flash bank 0
Device Security Bit Set
#0 : stm32f2x at 0x08000000, size 0x00800000, buswidth 0, chipwidth 0
	#  0: 0x00000000 (0x4000 16kB) protected
	#  1: 0x00004000 (0x4000 16kB) protected
	#  2: 0x00008000 (0x4000 16kB) protected
	#  3: 0x0000c000 (0x4000 16kB) protected
	#  4: 0x00010000 (0x10000 64kB) protected
	#  5: 0x00020000 (0x20000 128kB) protected
	#  6: 0x00040000 (0x20000 128kB) protected
	#  7: 0x00060000 (0x20000 128kB) protected
	#  8: 0x00080000 (0x20000 128kB) protected
	#  9: 0x000a0000 (0x20000 128kB) protected
	# 10: 0x000c0000 (0x20000 128kB) protected
	# 11: 0x000e0000 (0x20000 128kB) protected
	# 12: 0x00100000 (0x20000 128kB) protected
	# 13: 0x00120000 (0x20000 128kB) protected
	# 14: 0x00140000 (0x20000 128kB) protected
	# 15: 0x00160000 (0x20000 128kB) protected
	# 16: 0x00180000 (0x20000 128kB) protected
	# 17: 0x001a0000 (0x20000 128kB) protected
	# 18: 0x001c0000 (0x20000 128kB) protected
	# 19: 0x001e0000 (0x20000 128kB) protected
	# 20: 0x00200000 (0x20000 128kB) protected
	# 21: 0x00220000 (0x20000 128kB) protected
	# 22: 0x00240000 (0x20000 128kB) protected
	# 23: 0x00260000 (0x20000 128kB) protected
	# 24: 0x00280000 (0x20000 128kB) protected
	# 25: 0x002a0000 (0x20000 128kB) protected
	# 26: 0x002c0000 (0x20000 128kB) protected
	# 27: 0x002e0000 (0x20000 128kB) protected
	# 28: 0x00300000 (0x20000 128kB) protected
	# 29: 0x00320000 (0x20000 128kB) protected
	# 30: 0x00340000 (0x20000 128kB) protected
	# 31: 0x00360000 (0x20000 128kB) protected
	# 32: 0x00380000 (0x20000 128kB) protected
	# 33: 0x003a0000 (0x20000 128kB) protected
	# 34: 0x003c0000 (0x20000 128kB) protected
	# 35: 0x003e0000 (0x20000 128kB) protected
	# 36: 0x00400000 (0x20000 128kB) protected
	# 37: 0x00420000 (0x20000 128kB) protected
	# 38: 0x00440000 (0x20000 128kB) protected
	# 39: 0x00460000 (0x20000 128kB) protected
	# 40: 0x00480000 (0x20000 128kB) protected
	# 41: 0x004a0000 (0x20000 128kB) protected
	# 42: 0x004c0000 (0x20000 128kB) protected
	# 43: 0x004e0000 (0x20000 128kB) protected
	# 44: 0x00500000 (0x20000 128kB) protected
	# 45: 0x00520000 (0x20000 128kB) protected
	# 46: 0x00540000 (0x20000 128kB) protected
	# 47: 0x00560000 (0x20000 128kB) protected
	# 48: 0x00580000 (0x20000 128kB) protected
	# 49: 0x005a0000 (0x20000 128kB) protected
	# 50: 0x005c0000 (0x20000 128kB) protected
	# 51: 0x005e0000 (0x20000 128kB) protected
	# 52: 0x00600000 (0x20000 128kB) protected
	# 53: 0x00620000 (0x20000 128kB) protected
	# 54: 0x00640000 (0x20000 128kB) protected
	# 55: 0x00660000 (0x20000 128kB) protected
	# 56: 0x00680000 (0x20000 128kB) protected
	# 57: 0x006a0000 (0x20000 128kB) protected
	# 58: 0x006c0000 (0x20000 128kB) protected
	# 59: 0x006e0000 (0x20000 128kB) protected
	# 60: 0x00700000 (0x20000 128kB) protected
	# 61: 0x00720000 (0x20000 128kB) protected
	# 62: 0x00740000 (0x20000 128kB) protected
	# 63: 0x00760000 (0x20000 128kB) protected
	# 64: 0x00780000 (0x20000 128kB) protected
	# 65: 0x007a0000 (0x20000 128kB) protected
	# 66: 0x007c0000 (0x20000 128kB) protected
	# 67: 0x007e0000 (0x20000 128kB) protected
stm32f4x - Rev: Z
stm32x device protected
failed erasing sectors 0 to 11
wrote 244 bytes from file bin/bareCortexM.elf in 0.032002s (7.446 KiB/s)
Device Security Bit Set
stm32x device protected
failed setting protection for areas 0 to 11
in procedure 'flash'
requesting target halt and executing a soft reset

Well all I can say is it is very stable for me so we need to look into why you are getting problems.

Firstly split up your cmds, any reason why your are performing a unlock/protect on each program cycle.

Sometimes a power cycle is required after an unlock, so do not perform an unlock unless really required. The same goes for protect

before programming you should use ‘reset init’ to make sure any reset scripts are called and the target is ready for programming.

you are using gdb so why not just use the gdb ‘load’ cmd ?

try to use the latest version of OpenOCD, from git master if possible.

‘soft_reset_halt’ is deprecated, use ‘reset halt’ instead.

Cheers

Spen

I tried to do what you proposed.

set tdesc filename gdb/workaround.xml
set remote hardware-breakpoint-limit 6
set remote hardware-watchpoint-limit 4
target remote localhost:3333

monitor reset init
monitor halt
monitor poll

monitor flash info 0

load bin/bareCortexM.elf
monitor reset halt

This gave me this result:

source gdb/my_gdb.script
0x00000000 in ?? ()
target state: halted
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0xfffffffe msp: 0xfffffffc
background polling: on
TAP: stm32f4x.cpu (enabled)
target state: halted
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0xfffffffe msp: 0xfffffffc
Device Security Bit Set
#0 : stm32f2x at 0x08000000, size 0x00800000, buswidth 0, chipwidth 0
	#  0: 0x00000000 (0x4000 16kB) not protected
	#  1: 0x00004000 (0x4000 16kB) not protected
	#  2: 0x00008000 (0x4000 16kB) not protected
	#  3: 0x0000c000 (0x4000 16kB) not protected
	#  4: 0x00010000 (0x10000 64kB) not protected
	#  5: 0x00020000 (0x20000 128kB) not protected
	#  6: 0x00040000 (0x20000 128kB) not protected
	#  7: 0x00060000 (0x20000 128kB) not protected
	#  8: 0x00080000 (0x20000 128kB) not protected
	#  9: 0x000a0000 (0x20000 128kB) not protected
	# 10: 0x000c0000 (0x20000 128kB) not protected
	# 11: 0x000e0000 (0x20000 128kB) not protected
	# 12: 0x00100000 (0x20000 128kB) protected
	# 13: 0x00120000 (0x20000 128kB) protected
	# 14: 0x00140000 (0x20000 128kB) protected
	# 15: 0x00160000 (0x20000 128kB) protected
	# 16: 0x00180000 (0x20000 128kB) protected
	# 17: 0x001a0000 (0x20000 128kB) protected
	# 18: 0x001c0000 (0x20000 128kB) protected
	# 19: 0x001e0000 (0x20000 128kB) protected
	# 20: 0x00200000 (0x20000 128kB) protected
	# 21: 0x00220000 (0x20000 128kB) protected
	# 22: 0x00240000 (0x20000 128kB) protected
	# 23: 0x00260000 (0x20000 128kB) protected
	# 24: 0x00280000 (0x20000 128kB) protected
	# 25: 0x002a0000 (0x20000 128kB) protected
	# 26: 0x002c0000 (0x20000 128kB) protected
	# 27: 0x002e0000 (0x20000 128kB) protected
	# 28: 0x00300000 (0x20000 128kB) protected
	# 29: 0x00320000 (0x20000 128kB) protected
	# 30: 0x00340000 (0x20000 128kB) protected
	# 31: 0x00360000 (0x20000 128kB) protected
	# 32: 0x00380000 (0x20000 128kB) not protected
	# 33: 0x003a0000 (0x20000 128kB) not protected
	# 34: 0x003c0000 (0x20000 128kB) not protected
	# 35: 0x003e0000 (0x20000 128kB) not protected
	# 36: 0x00400000 (0x20000 128kB) not protected
	# 37: 0x00420000 (0x20000 128kB) not protected
	# 38: 0x00440000 (0x20000 128kB) not protected
	# 39: 0x00460000 (0x20000 128kB) not protected
	# 40: 0x00480000 (0x20000 128kB) not protected
	# 41: 0x004a0000 (0x20000 128kB) not protected
	# 42: 0x004c0000 (0x20000 128kB) not protected
	# 43: 0x004e0000 (0x20000 128kB) not protected
	# 44: 0x00500000 (0x20000 128kB) protected
	# 45: 0x00520000 (0x20000 128kB) protected
	# 46: 0x00540000 (0x20000 128kB) protected
	# 47: 0x00560000 (0x20000 128kB) protected
	# 48: 0x00580000 (0x20000 128kB) protected
	# 49: 0x005a0000 (0x20000 128kB) protected
	# 50: 0x005c0000 (0x20000 128kB) protected
	# 51: 0x005e0000 (0x20000 128kB) protected
	# 52: 0x00600000 (0x20000 128kB) protected
	# 53: 0x00620000 (0x20000 128kB) protected
	# 54: 0x00640000 (0x20000 128kB) protected
	# 55: 0x00660000 (0x20000 128kB) protected
	# 56: 0x00680000 (0x20000 128kB) protected
	# 57: 0x006a0000 (0x20000 128kB) protected
	# 58: 0x006c0000 (0x20000 128kB) protected
	# 59: 0x006e0000 (0x20000 128kB) protected
	# 60: 0x00700000 (0x20000 128kB) protected
	# 61: 0x00720000 (0x20000 128kB) protected
	# 62: 0x00740000 (0x20000 128kB) protected
	# 63: 0x00760000 (0x20000 128kB) protected
	# 64: 0x00780000 (0x20000 128kB) not protected
	# 65: 0x007a0000 (0x20000 128kB) not protected
	# 66: 0x007c0000 (0x20000 128kB) not protected
	# 67: 0x007e0000 (0x20000 128kB) not protected
stm32f4x - Rev: Z
gdb/my_gdb.script:20: Error in sourced command file:
Error erasing flash with vFlashErase packet

I am new to open ocd and gdb so i am not sure if I used the load command in the right way. In case I use

monitor flash erase_sector 0 0 11
monitor flash write_image bin/bareCortexM.elf

instead of the load command i get:

source gdb/my_gdb.script
0x00000000 in ?? ()
target state: halted
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0xfffffffe msp: 0xfffffffc
background polling: on
TAP: stm32f4x.cpu (enabled)
target state: halted
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0xfffffffe msp: 0xfffffffc
Device Security Bit Set
#0 : stm32f2x at 0x08000000, size 0x00800000, buswidth 0, chipwidth 0
	#  0: 0x00000000 (0x4000 16kB) not protected
	#  1: 0x00004000 (0x4000 16kB) not protected
	#  2: 0x00008000 (0x4000 16kB) not protected
	#  3: 0x0000c000 (0x4000 16kB) not protected
	#  4: 0x00010000 (0x10000 64kB) not protected
	#  5: 0x00020000 (0x20000 128kB) not protected
	#  6: 0x00040000 (0x20000 128kB) not protected
	#  7: 0x00060000 (0x20000 128kB) not protected
	#  8: 0x00080000 (0x20000 128kB) not protected
	#  9: 0x000a0000 (0x20000 128kB) not protected
	# 10: 0x000c0000 (0x20000 128kB) not protected
	# 11: 0x000e0000 (0x20000 128kB) not protected
	# 12: 0x00100000 (0x20000 128kB) protected
	# 13: 0x00120000 (0x20000 128kB) protected
	# 14: 0x00140000 (0x20000 128kB) protected
	# 15: 0x00160000 (0x20000 128kB) protected
	# 16: 0x00180000 (0x20000 128kB) protected
	# 17: 0x001a0000 (0x20000 128kB) protected
	# 18: 0x001c0000 (0x20000 128kB) protected
	# 19: 0x001e0000 (0x20000 128kB) protected
	# 20: 0x00200000 (0x20000 128kB) protected
	# 21: 0x00220000 (0x20000 128kB) protected
	# 22: 0x00240000 (0x20000 128kB) protected
	# 23: 0x00260000 (0x20000 128kB) protected
	# 24: 0x00280000 (0x20000 128kB) protected
	# 25: 0x002a0000 (0x20000 128kB) protected
	# 26: 0x002c0000 (0x20000 128kB) protected
	# 27: 0x002e0000 (0x20000 128kB) protected
	# 28: 0x00300000 (0x20000 128kB) protected
	# 29: 0x00320000 (0x20000 128kB) protected
	# 30: 0x00340000 (0x20000 128kB) protected
	# 31: 0x00360000 (0x20000 128kB) protected
	# 32: 0x00380000 (0x20000 128kB) not protected
	# 33: 0x003a0000 (0x20000 128kB) not protected
	# 34: 0x003c0000 (0x20000 128kB) not protected
	# 35: 0x003e0000 (0x20000 128kB) not protected
	# 36: 0x00400000 (0x20000 128kB) not protected
	# 37: 0x00420000 (0x20000 128kB) not protected
	# 38: 0x00440000 (0x20000 128kB) not protected
	# 39: 0x00460000 (0x20000 128kB) not protected
	# 40: 0x00480000 (0x20000 128kB) not protected
	# 41: 0x004a0000 (0x20000 128kB) not protected
	# 42: 0x004c0000 (0x20000 128kB) not protected
	# 43: 0x004e0000 (0x20000 128kB) not protected
	# 44: 0x00500000 (0x20000 128kB) protected
	# 45: 0x00520000 (0x20000 128kB) protected
	# 46: 0x00540000 (0x20000 128kB) protected
	# 47: 0x00560000 (0x20000 128kB) protected
	# 48: 0x00580000 (0x20000 128kB) protected
	# 49: 0x005a0000 (0x20000 128kB) protected
	# 50: 0x005c0000 (0x20000 128kB) protected
	# 51: 0x005e0000 (0x20000 128kB) protected
	# 52: 0x00600000 (0x20000 128kB) protected
	# 53: 0x00620000 (0x20000 128kB) protected
	# 54: 0x00640000 (0x20000 128kB) protected
	# 55: 0x00660000 (0x20000 128kB) protected
	# 56: 0x00680000 (0x20000 128kB) protected
	# 57: 0x006a0000 (0x20000 128kB) protected
	# 58: 0x006c0000 (0x20000 128kB) protected
	# 59: 0x006e0000 (0x20000 128kB) protected
	# 60: 0x00700000 (0x20000 128kB) protected
	# 61: 0x00720000 (0x20000 128kB) protected
	# 62: 0x00740000 (0x20000 128kB) protected
	# 63: 0x00760000 (0x20000 128kB) protected
	# 64: 0x00780000 (0x20000 128kB) not protected
	# 65: 0x007a0000 (0x20000 128kB) not protected
	# 66: 0x007c0000 (0x20000 128kB) not protected
	# 67: 0x007e0000 (0x20000 128kB) not protected
stm32f4x - Rev: Z
stm32x device protected
failed erasing sectors 0 to 11
wrote 244 bytes from file bin/bareCortexM.elf in 0.031002s (7.686 KiB/s)
target state: halted
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0xfffffffe msp: 0xfffffffc

I also tried Keil to do the flashing but encouneterd similar errors when erasing flash.

Thanks for the help.

The important info is ‘Device Security Bit Set’, until you remove the protection nothing will work

stm32f2x unlock 0

perform a power cycle after the unlock cmd, just to be sure.

Cheers

Spen

I used unlock successfully,

monitor stm32f2x unlock 0
Device Security Bit Set
stm32f2x unlocked.

then disconnected the USB-cable. Reconnected the device and run the script again.

Sadly the output stays the same.

It seems that I found the solution to my problem. We used ST-Utility to take a look at the option bytes of the chip.

After changing back to default settings we got from another board, flashing and debugging started to work.

It seems that some other tutorial I tried messed with the option bytes.

It would be nice to know a way to do these settings using open ocd.

Thanks for the help,

Holger

ah ok you enable the hardware watchdog.

It is on the todo list to support this, unsure when it will be done.

Cheers

Spen