Wednesday, October 15, 2014

Xmega DFU Programming in Linux

I'm using elementary OS 0.2 and have an MT-DB-X4 development board from MattairTech with an ATxmega32A4U — so you may need to adjust the following commands to suit your particular setup.

Install dfu-programmer

First we need to install some prerequisites:

$> sudo aptitude install libusb-1.0-0 libusb-1.0-0-dev

Then, download and extract the source:

$> wget -qO-  "http://sourceforge.net/projects/dfu-programmer/files/dfu-programmer/0.7.0/dfu-programmer-0.7.0.tar.gz/download" | tar xvz

Now configure, make, make install:
#!/bin/bash

cd dfu-programmer-0.7.0
./configure --prefix=$HOME/.local  # This will install to your home
make                               # Compile
make install                       # Install

Now we can test it out. Plug in your device and run the following (change the device to match your chip):

$> sudo ~/.local/bin/dfu-programmer atxmega32a4u get
Bootloader Version: 0x04 (4)

Finally, we need to add ~/.local/bin to our PATH so we can just type dfu-programmer:

#!/bin/bash

# check to see if it's already in PATH
echo $PATH | grep -v "$HOME/\.local/bin" > /dev/null &&
cat << EOF >>~/.profile

# set PATH so it includes user's private .local/bin if it exists
if [ -d "\$HOME/.local/bin" ] ; then
    PATH="\$HOME/.local/bin:\$PATH"
fi
EOF

Remember, you need to logout and back in before the changes to your groups and .profile take effect.

Setup udev rules and permissions

When you first plug the device in, udev will only allow access to the root user by default. To change this we need to let udev know that we want to give access to the device to users in a certain group — plugdev in this case.

To start, check to see if you're already in the plugdev group:

$> groups
If not, you'll need to add yourself:
$> sudo usermod -a -G plugdev $USER

Next, we need to add a rules file to tell udev which group to add the device to. To get the device info, run lsusp to get the vendor and product IDs:

$> lsusb | grep Atmel
This should output something like the following:
Bus 005 Device 003: ID 03eb:2fe4 Atmel Corp.
Note the first and second parts of the ID

Now that we have the IDs, we can make the udev rule file and restart udev:

#!/bin/bash

sudo bash -c 'cat << EOF >/lib/udev/rules.d/12-atmel.rules
# atxmega32a4u with DFU Bootloader
SUBSYSTEM=="usb", ACTION=="add", ATTRS{idVendor}=="03eb", ATTRS{idProduct}=="2fe4", GROUP="plugdev"
EOF
service udev restart'

Now just unplug and replug your device and you should be able to access it without root:

$> dfu-programmer atxmega32a4u get
Bootloader Version: 0x04 (4)

No comments :