Need to flash twice for successful flashing (LPC2368)

I am trying to flash lpc2368 (which has a 12MHz external oscillator, and 4MHz internal oscillator) with openocd-0.7.0. But it works only on second attempt. Here are the logs for successful flashing and failed flashing (Differences in the log output is shown in red fonts):

Failed:

Open On-Chip Debugger 0.5.0 (2011-08-09-23:26)

Licensed under GNU GPL v2

For bug reports, read

http://openocd.berlios.de/doc/doxygen/bugs.html

Info : only one transport option; autoselect ‘jtag’

init_targets

SETUP OPENOCD ---------------------------------------------

trst_and_srst separate srst_gates_jtag trst_push_pull srst_open_drain

adapter_nsrst_delay: 100

jtag_ntrst_delay: 100

500 kHz

debug_level: 2

dcc downloads are enabled

fast memory access is enabled

adapter_nsrst_delay: 200

jtag_ntrst_delay: 200

RESET DEVICE ----------------------------------------------

Info : clock speed 500 kHz

Info : JTAG tap: lpc2368.cpu tap/device found: 0x4f1f0f0f (mfg: 0x787, part: 0xf1f0, ver: 0x4)

Info : Embedded ICE version 7

Error: EmbeddedICE v7 handling might be broken

Info : lpc2368.cpu: hardware has 2 breakpoint/watchpoint units

target state: halted

target halted in ARM state due to debug-request, current mode: System

cpsr: 0x8000005f pc: 0x00006f84

Info : JTAG tap: lpc2368.cpu tap/device found: 0x4f1f0f0f (mfg: 0x787, part: 0xf1f0, ver: 0x4)

target state: halted

target halted in ARM state due to debug-request, current mode: System

cpsr: 0x8000005f pc: 0x00006fa0

requesting target halt and executing a soft reset

target state: halted

target halted in ARM state due to debug-request, current mode: Supervisor

cpsr: 0x800000d3 pc: 0x00000000

flash ‘lpc2000’ found at 0x00000000

FLASH INFO ------------------------------------------------

#0 : lpc2368.flash (lpc2000) at 0x00000000, size 0x0007e000, buswidth 0, chipwidth 0

UNPROTECT FLASH -------------------------------------------

cleared protection for sectors 0 through 27 on flash bank 0

ERASE FLASH -----------------------------------------------

Info : JTAG tap: lpc2368.cpu tap/device found: 0x4f1f0f0f (mfg: 0x787, part: 0xf1f0, ver: 0x4)

target state: halted

target halted in ARM state due to debug-request, current mode: System

cpsr: 0x8000005f pc: 0x00006fbc

erased sectors 0 through 27 on flash bank 0 in 0.078000s

successfully checked erase state

0: 0x00000000 (0x1000 4kB) erased

1: 0x00001000 (0x1000 4kB) erased

2: 0x00002000 (0x1000 4kB) erased

3: 0x00003000 (0x1000 4kB) erased

4: 0x00004000 (0x1000 4kB) erased

5: 0x00005000 (0x1000 4kB) erased

6: 0x00006000 (0x1000 4kB) erased

7: 0x00007000 (0x1000 4kB) erased

8: 0x00008000 (0x8000 32kB) erased

9: 0x00010000 (0x8000 32kB) erased

10: 0x00018000 (0x8000 32kB) erased

11: 0x00020000 (0x8000 32kB) erased

12: 0x00028000 (0x8000 32kB) erased

13: 0x00030000 (0x8000 32kB) erased

14: 0x00038000 (0x8000 32kB) erased

15: 0x00040000 (0x8000 32kB) erased

16: 0x00048000 (0x8000 32kB) erased

17: 0x00050000 (0x8000 32kB) erased

18: 0x00058000 (0x8000 32kB) erased

19: 0x00060000 (0x8000 32kB) erased

20: 0x00068000 (0x8000 32kB) erased

21: 0x00070000 (0x8000 32kB) erased

22: 0x00078000 (0x1000 4kB) erased

23: 0x00079000 (0x1000 4kB) erased

24: 0x0007a000 (0x1000 4kB) erased

25: 0x0007b000 (0x1000 4kB) erased

26: 0x0007c000 (0x1000 4kB) erased

27: 0x0007d000 (0x1000 4kB) erased

PROGRAM FLASH ---------------------------------------------

wrote 55304 bytes from file IMO-USB2.elf in 2.418004s (22.336 KiB/s)

VERIFY FLASH ----------------------------------------------

address 0x00000000 length 0x0000d6b8

address 0x0000d6b8 length 0x00000150

verified 55304 bytes in 0.000000s (inf KiB/s)

successfully checked erase state

0: 0x00000000 (0x1000 4kB) not erased

1: 0x00001000 (0x1000 4kB) not erased

2: 0x00002000 (0x1000 4kB) not erased

3: 0x00003000 (0x1000 4kB) not erased

4: 0x00004000 (0x1000 4kB) not erased

5: 0x00005000 (0x1000 4kB) not erased

6: 0x00006000 (0x1000 4kB) not erased

7: 0x00007000 (0x1000 4kB) not erased

8: 0x00008000 (0x8000 32kB) not erased

9: 0x00010000 (0x8000 32kB) erased

10: 0x00018000 (0x8000 32kB) erased

11: 0x00020000 (0x8000 32kB) erased

12: 0x00028000 (0x8000 32kB) erased

13: 0x00030000 (0x8000 32kB) erased

14: 0x00038000 (0x8000 32kB) erased

15: 0x00040000 (0x8000 32kB) erased

16: 0x00048000 (0x8000 32kB) erased

17: 0x00050000 (0x8000 32kB) erased

18: 0x00058000 (0x8000 32kB) erased

19: 0x00060000 (0x8000 32kB) erased

20: 0x00068000 (0x8000 32kB) erased

21: 0x00070000 (0x8000 32kB) erased

22: 0x00078000 (0x1000 4kB) erased

23: 0x00079000 (0x1000 4kB) erased

24: 0x0007a000 (0x1000 4kB) erased

25: 0x0007b000 (0x1000 4kB) erased

26: 0x0007c000 (0x1000 4kB) erased

27: 0x0007d000 (0x1000 4kB) erased

REBOOT DEVICE ---------------------------------------------

Info : JTAG tap: lpc2368.cpu tap/device found: 0x4f1f0f0f (mfg: 0x787, part: 0xf1f0, ver: 0x4)

target state: halted

target halted in ARM state due to breakpoint, current mode: Supervisor

cpsr: 0x40000053 pc: 0x40000070

requesting target halt and executing a soft reset

target state: halted

target halted in ARM state due to breakpoint, current mode: Supervisor

cpsr: 0x400000d3 pc: 0x00000000

shutdown command invoked

Passed:

Open On-Chip Debugger 0.5.0 (2011-08-09-23:26)

Licensed under GNU GPL v2

For bug reports, read

http://openocd.berlios.de/doc/doxygen/bugs.html

Info : only one transport option; autoselect ‘jtag’

init_targets

SETUP OPENOCD ---------------------------------------------

trst_and_srst separate srst_gates_jtag trst_push_pull srst_open_drain

adapter_nsrst_delay: 100

jtag_ntrst_delay: 100

500 kHz

debug_level: 2

dcc downloads are enabled

fast memory access is enabled

adapter_nsrst_delay: 200

jtag_ntrst_delay: 200

RESET DEVICE ----------------------------------------------

Info : clock speed 500 kHz

Info : JTAG tap: lpc2368.cpu tap/device found: 0x4f1f0f0f (mfg: 0x787, part: 0xf1f0, ver: 0x4)

Info : Embedded ICE version 7

Error: EmbeddedICE v7 handling might be broken

Info : lpc2368.cpu: hardware has 2 breakpoint/watchpoint units

target state: halted

target halted in Thumb state due to debug-request, current mode: Supervisor

cpsr: 0x200000f3 pc: 0x7fffe15a

Info : JTAG tap: lpc2368.cpu tap/device found: 0x4f1f0f0f (mfg: 0x787, part: 0xf1f0, ver: 0x4)

target state: halted

target halted in ARM state due to debug-request, current mode: Supervisor

cpsr: 0x200000d3 pc: 0x00000020

requesting target halt and executing a soft reset

target state: halted

target halted in ARM state due to debug-request, current mode: Supervisor

cpsr: 0x200000d3 pc: 0x00000000

flash ‘lpc2000’ found at 0x00000000

FLASH INFO ------------------------------------------------

#0 : lpc2368.flash (lpc2000) at 0x00000000, size 0x0007e000, buswidth 0, chipwidth 0

UNPROTECT FLASH -------------------------------------------

cleared protection for sectors 0 through 27 on flash bank 0

ERASE FLASH -----------------------------------------------

Info : JTAG tap: lpc2368.cpu tap/device found: 0x4f1f0f0f (mfg: 0x787, part: 0xf1f0, ver: 0x4)

target state: halted

target halted in ARM state due to debug-request, current mode: Supervisor

cpsr: 0x200000d3 pc: 0x0000004c

erased sectors 0 through 27 on flash bank 0 in 0.093600s

successfully checked erase state

# 0: 0x00000000 (0x1000 4kB) not erased

1: 0x00001000 (0x1000 4kB) erased

2: 0x00002000 (0x1000 4kB) erased

3: 0x00003000 (0x1000 4kB) erased

4: 0x00004000 (0x1000 4kB) erased

5: 0x00005000 (0x1000 4kB) erased

6: 0x00006000 (0x1000 4kB) erased

7: 0x00007000 (0x1000 4kB) erased

8: 0x00008000 (0x8000 32kB) erased

9: 0x00010000 (0x8000 32kB) erased

10: 0x00018000 (0x8000 32kB) erased

11: 0x00020000 (0x8000 32kB) erased

12: 0x00028000 (0x8000 32kB) erased

13: 0x00030000 (0x8000 32kB) erased

14: 0x00038000 (0x8000 32kB) erased

15: 0x00040000 (0x8000 32kB) erased

16: 0x00048000 (0x8000 32kB) erased

17: 0x00050000 (0x8000 32kB) erased

18: 0x00058000 (0x8000 32kB) erased

19: 0x00060000 (0x8000 32kB) erased

20: 0x00068000 (0x8000 32kB) erased

21: 0x00070000 (0x8000 32kB) erased

22: 0x00078000 (0x1000 4kB) erased

23: 0x00079000 (0x1000 4kB) erased

24: 0x0007a000 (0x1000 4kB) erased

25: 0x0007b000 (0x1000 4kB) erased

26: 0x0007c000 (0x1000 4kB) erased

27: 0x0007d000 (0x1000 4kB) erased

PROGRAM FLASH ---------------------------------------------

wrote 55304 bytes from file IMO-USB2.elf in 2.527204s (21.371 KiB/s)

VERIFY FLASH ----------------------------------------------

address 0x00000000 length 0x0000d6b8

address 0x0000d6b8 length 0x00000150

verified 55304 bytes in 0.000000s (inf KiB/s)

successfully checked erase state

0: 0x00000000 (0x1000 4kB) not erased

1: 0x00001000 (0x1000 4kB) not erased

2: 0x00002000 (0x1000 4kB) not erased

3: 0x00003000 (0x1000 4kB) not erased

4: 0x00004000 (0x1000 4kB) not erased

5: 0x00005000 (0x1000 4kB) not erased

6: 0x00006000 (0x1000 4kB) not erased

7: 0x00007000 (0x1000 4kB) not erased

8: 0x00008000 (0x8000 32kB) not erased

9: 0x00010000 (0x8000 32kB) erased

10: 0x00018000 (0x8000 32kB) erased

11: 0x00020000 (0x8000 32kB) erased

12: 0x00028000 (0x8000 32kB) erased

13: 0x00030000 (0x8000 32kB) erased

14: 0x00038000 (0x8000 32kB) erased

15: 0x00040000 (0x8000 32kB) erased

16: 0x00048000 (0x8000 32kB) erased

17: 0x00050000 (0x8000 32kB) erased

18: 0x00058000 (0x8000 32kB) erased

19: 0x00060000 (0x8000 32kB) erased

20: 0x00068000 (0x8000 32kB) erased

21: 0x00070000 (0x8000 32kB) erased

22: 0x00078000 (0x1000 4kB) erased

23: 0x00079000 (0x1000 4kB) erased

24: 0x0007a000 (0x1000 4kB) erased

25: 0x0007b000 (0x1000 4kB) erased

26: 0x0007c000 (0x1000 4kB) erased

27: 0x0007d000 (0x1000 4kB) erased

REBOOT DEVICE ---------------------------------------------

Info : JTAG tap: lpc2368.cpu tap/device found: 0x4f1f0f0f (mfg: 0x787, part: 0xf1f0, ver: 0x4)

target state: halted

target halted in ARM state due to breakpoint, current mode: Supervisor

cpsr: 0x400000d3 pc: 0x40000070

requesting target halt and executing a soft reset

target state: halted

target halted in ARM state due to breakpoint, current mode: Supervisor

cpsr: 0x400000d3 pc: 0x00000000

shutdown command invoked

The config file is :

#
# Flash LPC2368 memory using openocd 
# and a FTDI FT2232-based JTAG-interface
#

source [find olimex-arm-usb-tiny.cfg]
source [find openocd-cpu-lpc2368.cfg]

proc init_targets {} {
    
    echo ""
    echo ">> SETUP OPENOCD ---------------------------------------------"
    # setup_lpc2368 <core_freq_khz> <adapter_freq_khz>
	setup_lpc2368 4000 500

    debug_level 2
    
    arm7_9 dcc_downloads enable
    arm7_9 fast_memory_access enable        
    
    jtag_nsrst_delay 200
    jtag_ntrst_delay 200
 
    
    echo ""
    echo ">> RESET DEVICE ----------------------------------------------"
    init
    halt 100
    reset
    sleep 10
    soft_reset_halt
    sleep 10    
    flash probe 0
    
    
    
    echo ""
    echo ">> FLASH INFO ------------------------------------------------"    
    flash banks
    
    
    echo ""
    echo ">> UNPROTECT FLASH -------------------------------------------"    
    sleep 10
    #flash protect num first last ('on'|'off')
    flash protect 0 0 last off
    
    
    echo ""
    echo ">> ERASE FLASH -----------------------------------------------"    
    sleep 10
	reset halt
    #flash erase_sector num first last
    flash erase_sector 0 0 last
    sleep 10
    flash erase_check 0

    
    echo ""
    echo ">> PROGRAM FLASH ---------------------------------------------"    
    sleep 10
	halt 100
    #flash write_image [erase] [unlock] filename [offset] [type]
    flash write_image IMO-USB2.elf
    
    
    echo ""
    echo ">> VERIFY FLASH ----------------------------------------------"
    sleep 10
    #test_image filename [address ['bin'|'ihex'|'elf']]
    test_image IMO-USB2.elf
    flash erase_check 0

    
    echo ""
    echo ">> REBOOT DEVICE ---------------------------------------------"
    init
    halt 100
    reset
    sleep 10
    soft_reset_halt
    sleep 10
    resume
    sleep 10    
    shutdown
	}

I cannot get what is the problem. Has anybody any idea?

Both outputs reports that the flash has been sucessfully programmed and verified.

How do you know that the first failed and the second succeded ?

mlu:
Both outputs reports that the flash has been sucessfully programmed and verified.

How do you know that the first failed and the second succeded ?

Because the microcontroller didn’t work. by the way, I tried to flash on another chip too, but sometimes it works and sometimes doesn’t. Also tried with latest verions of openOCD. No success.

I also have struggled with flashing LPC parts using openocd. You’ve already dug deeper and provided a lot more detail than I did. In [my case (different arm architecture version, different jtag interface, different version of openocd), resetting the chip between erase and flash seemed to be part of the secret sauce.](LPC1768 arm-usb-ocd openOCD flash - SparkFun Electronics Forum)