STM32/Cortex-M3 HOWTO: Development under Ubuntu.

OpenOCD / Open On-Chip Debugger

The Open On-Chip Debugger is a project that is aimed at arm-jtag flashing and debugging, and it has some quite nice features that I really like. And the supported jtags are often quite inexpensive, so there are a good choice for the hobbyist.

Install

To install OpenOCD on Ubuntu 10.04 is very simple.

sudo apt-get install openocd

If you have something older like Ubuntu 9.04 you need to compile it our self, since the version is to old. Also note that if the debian package work, there is no extra value to compile it your self.

Build OpenOCD

First install some stuff needed to build.

sudo apt-get install libftdi-dev libftdi1 libtool subversion git-core

Then get and compile it.

mkdir -p ~/stm32-tools
cd ~/stm32-tools
git clone git://openocd.git.sourceforge.net/gitroot/openocd/openocd OpenOCD
cd OpenOCD
git reset --hard v0.4.0-rc2
./bootstrap 
./configure --enable-maintainer-mode \
            --enable-ft2232_libftdi 
make $PARALLEL 
make install

Connect to the hardware

Then connect the ARM-USB-OCD jtag to your computer and check with dmesg that it is identified in a correct way, and you don't get any strange errors.

[55901.824010] usb 3-1: new full speed USB device using uhci_hcd and address 3
[55902.021206] usb 3-1: configuration #1 chosen from 1 choice
[55902.070759] USB Serial support registered for FTDI USB Serial Device
[55902.070850] usb 3-1: Ignoring serial port reserved for JTAG
[55902.070882] ftdi_sio 3-1:1.1: FTDI USB Serial Device converter detected
[55902.070902] usb 3-1: Detected FT2232C
[55902.070948] usb 3-1: FTDI USB Serial Device converter now attached to ttyUSB1
[55902.070959] usbcore: registered new interface driver ftdi_sio
[55902.070960] ftdi_sio: v1.4.3:USB FTDI Serial Converters Driver

And with the flywatter you will get something like this.

[89760.716880] usb 1-6.4: new full speed USB device using ehci_hcd and address 5
[89760.818440] usb 1-6.4: configuration #1 chosen from 1 choice
[89760.820264] ftdi_sio 1-6.4:1.0: FTDI USB Serial Device converter detected
[89760.820342] usb 1-6.4: Detected FT2232C
[89760.820350] usb 1-6.4: Number of endpoints 2
[89760.820357] usb 1-6.4: Endpoint 1 MaxPacketSize 64
[89760.820364] usb 1-6.4: Endpoint 2 MaxPacketSize 64
[89760.820370] usb 1-6.4: Setting MaxPacketSize 64
[89760.820683] usb 1-6.4: FTDI USB Serial Device converter now attached to ttyUSB0
[89760.822374] ftdi_sio 1-6.4:1.1: FTDI USB Serial Device converter detected
[89760.822452] usb 1-6.4: Detected FT2232C
[89760.822460] usb 1-6.4: Number of endpoints 2
[89760.822467] usb 1-6.4: Endpoint 1 MaxPacketSize 64
[89760.822474] usb 1-6.4: Endpoint 2 MaxPacketSize 64
[89760.822481] usb 1-6.4: Setting MaxPacketSize 64
[89760.824422] usb 1-6.4: FTDI USB Serial Device converter now attached to ttyUSB1

Then find the device id.

$> lsusb 
...
Bus 003 Device 009: ID 15ba:0003 Olimex Ltd. OpenOCD JTAG
...

Please note that with a Flyswatter-jtag you will get something more like this.

$> lsusb 
...
Bus 004 Device 005: ID 0403:6010 Future Technology Devices International, Ltd FT2232C Dual USB-UART/FIFO IC
...

Then we are going to add this device to udev so we don't have to use sudo to use it. First check that you are a member of the group plugdev (with commands like groups or id), then add the following to a udev rules file.

sudo vi /etc/udev/rules.d/45-ftdi2232-libftdi.rules

#Olimex
SYSFS{idProduct}=="0003", SYSFS{idVendor}=="15ba", MODE="666", GROUP="plugdev"
#Flyswatter
SYSFS{idProduct}=="6010", SYSFS{idVendor}=="0403", MODE="666", GROUP="plugdev"

If this works, you should be able to start the OpenOCD server without sudo.

Flash Olimex blinky example

Now it is time to se if we can put some software on the card, so let's use the Olimex blinky program that we compiled earlier.

cd ~/stm32-example/projects/stm_h103/
wget http://fun-tech.se/stm32/OpenOCD/openocd.cfg
xterm -e "openocd -f openocd.cfg" &

Or if you have the Flyswatter-jtag, you should use another config file. (something like this).

wget http://fun-tech.se/stm32/OpenOCD/flyswatter.cfg
xterm -e "openocd -f flyswatter.cfg" &

Now let's get a second command window with the telnet session, so we can talk to the OpenOCD server we just started.

xterm -e "telnet localhost 4444" &

Here you should get a nice little prompt, from where you can manually control the jtag and flash the card.

reset halt
flash probe 0 
stm32x mass_erase 0
flash write_bank 0 main.bin 0
reset run 

If the card started to blink with the green led, you are successful...

openocd.cfg

Above you used a config file called openocd.cfg when you started openocd, what is that?

Filename: openocd.cfg

#daemon configuration
telnet_port 4444
gdb_port 3333

#interface
#cat /usr/lib/openocd/interface/olimex-arm-usb-ocd.cfg >> openocd.cfg
interface ft2232
ft2232_device_desc "Olimex OpenOCD JTAG"
ft2232_layout olimex-jtag
ft2232_vid_pid 0x15ba 0x0003

# target configuration
# cat /usr/lib/openocd/target/stm32.cfg >> openocd.cfg
# script for stm32

# jtag speed
jtag_khz 500

jtag_nsrst_delay 100
jtag_ntrst_delay 100

#use combined on interfaces or targets that can't set TRST/SRST separately
reset_config trst_and_srst

#jtag scan chain
#format L IRC IRCM IDCODE (Length, IR Capture, IR Capture Mask, IDCODE)
jtag_device 4 0x1 0xf 0xe
jtag_device 5 0x1 0x1 0x1e

target create target0 cortex_m3 -endian little -chain-position 0

[new_target_name] configure -work-area-virt 0 -work-area-phys 0x20000000 -work-area-size 16384 -work-area-backup 0

#flash bank str7x <base> <size> 0 0 <target#> <variant>
flash bank stm32x 0 0 0 0 0

# For more information about the configuration files, take a look at:
# openocd.texi

This file was created with information about 3 things, first there is some info about the daemon with things like what port numbers. Then we have info about the jtag, and that part is a copy from /usr/lib/openocd/interface/olimex-arm-usb-ocd.cfg. And finally some info about the target it self, and that part came from /usr/lib/openocd/target/stm32.cfg.

This also means that if you plan to use some other hardware, just change a little in the openocd.cfg file and you are probably ready to go. Say that you would like to play with the STM32-PerformanceStick, a safe bet would to look in /usr/lib/openocd/target/stm32stick.cfg and /usr/lib/openocd/interface/stm32-stick.cfg.

Please note that the version of openocd that comes with Ubuntu 9.10, complains about this file using depreciated syntax but it still works.

flyswatter.cfg

What happens if you use another jtag, like the Flyswatter?

You have already seem that you should use another config file, but please note that it is only the interface part that is different.

Filename: flyswatter.cfg

#daemon configuration
telnet_port 4444
gdb_port 3333

#interface
interface ft2232
ft2232_device_desc "Flyswatter"
ft2232_layout "flyswatter"
ft2232_vid_pid 0x0403 0x6010

# target configuration
# cat /usr/lib/openocd/target/stm32.cfg >> openocd.cfg
# script for stm32

# jtag speed
jtag_khz 500

jtag_nsrst_delay 100
jtag_ntrst_delay 100

#use combined on interfaces or targets that can't set TRST/SRST separately
reset_config trst_and_srst

#jtag scan chain
#format L IRC IRCM IDCODE (Length, IR Capture, IR Capture Mask, IDCODE)
jtag_device 4 0x1 0xf 0xe
jtag_device 5 0x1 0x1 0x1e

target create target0 cortex_m3 -endian little -chain-position 0

[new_target_name] configure -work-area-virt 0 -work-area-phys 0x20000000 -work-area-size 16384 -work-area-backup 0

#flash bank str7x <base> <size> 0 0 <target#> <variant>
flash bank stm32x 0 0 0 0 0

# For more information about the configuration files, take a look at:
# openocd.texi