STM32/ARM Cortex-M3 HOWTO: Development under Ubuntu (Debian)

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.

A good starting point to understand OpenOCD is to read their nice manual, it is available in either html or as a pdf. This will answer a lot of your questions.

So do not only open it, save it in a good place so it is close when you need it.

cd ~/stm32/
wget http://openocd.berlios.de/doc/pdf/openocd.pdf

Install

To install OpenOCD on Ubuntu 10.04 is very simple.

sudo apt-get install openocd

However since that version is kind of old, it could be a good idea to grab a newer version and build it your self. But please 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 git-core asciidoc \
    build-essential flex bison \
    libgmp3-dev libmpfr-dev autoconf \
    texinfo libncurses5-dev libexpat1 libexpat1-dev \
    tk tk8.4 tk8.4-dev

Then get and compile it.

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

Then do a quick check what version you have in your path.

which openocd

Since the default install for OpenOCD when you compile it your self is in /usr/local/, you should be something like this back.

/usr/local/bin/openocd

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.

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
Fatal error: Uncaught Error: Call to a member function link_ext() on null in /customers/9/b/5/fun-tech.se/httpd.www/stm32/class.page.php:62 Stack trace: #0 /customers/9/b/5/fun-tech.se/httpd.www/stm32/OpenOCD/index.php(298): page->getFoot() #1 {main} thrown in /customers/9/b/5/fun-tech.se/httpd.www/stm32/class.page.php on line 62