Electronic – Program only runs when debugging in GDB – using Open OCD and Olimex arm-usb-ocd-h jtag to program at91sam3su

armarm7cortex-m3jtagopenocd

I'm trying to make my Atmel at91sam3u cortex-m3 arm chip make an LED blink. I have a Olimex ARM-USB-OCD-H jtag programmer, and I'm using Open OCD running on OS X to program my chip. I'm using the provided Open OCD startup scripts for the jtag programmer and my chip. I have no trouble compiling and linking the program, and I seem to be able to successfully load the program on the chip. However, the LED only blinks when I run the program via gdb. It works whether I step through it or run it continuously.

To make it work in gdb, I first run 'openocd' and then in another terminal (gdb feedback ommitted):

$ arm-none-eabi-gdb blinky/blinky.elf
(gdb) target remote :3333
(gdb) load
(gdb) cont

This works without any issues and the LED blinks. Ofcourse, when I exit gdb, the program is stopped, and the LED is left in whatever state it was at that time. If I click the chip's reset button the LED goes to a neutral (slightly on) state.

To try to simply program the chip, I run 'openocd' and then in another terminal:

$ telnet localhost 4444
> halt
target state: halted
target halted due to debug-request, current mod
xPSR: 0x81000000 pc: 0x00080107 msp: 0x20000598
> flash write_image blinky/blinky.elf
wrote 3044 bytes from file blinky/blinky.elf in 0.314594s (9.449 KiB/s)
> reset run
TAG tap: sam3.cpu tap/device found: 0x4ba00477 (mfg: 0x23b, part: 0xba00, ver: 0x4)

There are no errors, but the LED does not blink, it stays in a neutral state.

The linker script sets the program (.text) to be put in flash0 memory. Thus I've tried setting gpnvm bit 1 as that is the sram vs flash boot select bit according to the at91sam3 datasheet. This did not help, and it seemed to have prevented gdb form running properly.

Do I need to maybe flash the board with some sort of bootloader that can load programs from the flash0 bank? I assumed that the chip came with this.

I have not yet been able to load the .text into sram instead of flash0 as I've had trouble changing the linker script I have to do this.

Note:
The at91sam3u chip is on a custom pcb designed as part of a student design project. Thus, it is possible that a mistake was made during the pcb design… however, I would think that in that case it would not run properly in gdb either.

Thank you.

Best Answer

$ telnet localhost 4444
> halt
target state: halted
target halted due to debug-request, current mod
xPSR: 0x81000000 pc: 0x00080107 msp: 0x20000598
> flash write_image blinky/blinky.elf

You usually need reset init before a flash erase/write, halt may not be enough. In gdb it is monitor reset init.

The flash also needs to be erased before writing: flash write_image erase unlock blinky/blinky.elf