Homebuilt Hardware

AVRDragon Linux Tutorial

Posted in Synth DIY by ryan on the December 8th, 2007

Atmel AVR Development in Ubuntu
with the AVRDragon and STK500
Part 1 - Hardware Setup and Interface Modes

This tutorial covers the initial setup of the AVRDragon in Ubuntu linux. Most of it is applicable in other Linux distributions. The goal is to show how to set the AVRDragon’s programming mode and connect through the USB port.

Required Hardware

For this tutorial, some hardware is required:

  • AVRDragon - A USB programmer/debugger adapter for AVR devices that have less than 32K flash. This is an inexpensive alternative to the JTAGICE mkii adapter. I paid $50 for it.
  • STK500 - This Atmel’s starter kit development board. I paid about $80 for some years ago. This board supports many of the AVR devices that have DIP packages.
  • ATtiny2313 - or another AVR device that is supported by the STK500, and the AVRDragon. It should also have debugWire support. Note: If a different device is used, some command strings will be slightly different.

To avoid purchasing the STK500, a simple circuit can be built around an AVR. The minimum circuit needs, a reset button + pull up resistor, and pin connections for the 6pin ISP connector. This is how I normally develop firmware once I have settled on the hardware design. Using the STK500 is ok for initial testing but may not be reasonable for in circuit debugging. This all depends on the complexity of the design; all of the AVR IO ports are brought out to headers on the STK500. The AVRDragon seems to be the best option for in circuit debugging. For about $300 a better version, the JTAGICE mkII, can be purchased. It supports the bigger AVR parts and also AVR32 parts. The dragon should be suitable for smaller devices.

Installing the tools

This tutorial requires avrdude. To install avrdude in Ubuntu, issue the following command in a terminal:

sudo apt-get install avrdude

Testing AVRDragon with the STK500

The dragon can be used with avrdude to program or debug a device. Insert the attiny2313 into red SCKT3300D3 socket on the STK500. The ISP header on the dragon should be connected to the red ISP header on the STK500. For an AVR other than the attiny2313, the correct socket and header are listed in the STK500 users manual. The RESET jumper on the STK500 should also be disconnected. <insert image of connections here> Power on the STK500, then connect a USB cable to the avrdagon. To verify the dragon is connected, run ‘lsusb’ in a terminal window. Verify the a device is listed as ‘Atmel Corp.’. To test the dragon run avrdude in the terminal like this:

sudo avrdude -c dragon_isp -P usb -p t2313 -F

If you are not using the attiny2313, remember to change the part option (-p t2313) to the correct value. See the avrdude documentation for a full list of command line options.

Userspace USB rules

By default access to the usb device is restricted to root. To allow all users to access the AVRDragon device create a file called: /etc/udev/rules.d/50-avrdragon.rules edit this file and add this line:

SUBSYSTEM=="usb", SYSFS{idVendor}=="03eb", SYSFS{idProduct}=="2107", GROUP="users", MODE="0666"

To verify vendor id and product id use the command: lsusb locate the line that shows Atmel Corp., the number preceding is of the format vvvv:pppp where vvvv is the vendor id and pppp is the product id. Before running avrdude again, restart udev like this: sudo /etc/init.d/udev restart now you should be able to access the dragon as a normal user.

avrdude -c dragon_isp -P usb -p t2313 -F

Switching from ISP mode to debugWire mode

The factory default interface is ISP but debugging is done in debugWire mode. To switch between ISP mode and debugWire mode, the DWEN fuse must be written. Check the data sheet for your device to find the proper fuse to write (I’m not sure if it is usually the same or not). For the attiny2313 the DWEN fuse is bit7 of the high fuse byte. The following is an example of switching between ISP and debugWire modes. note: a ‘programmed’ fuse is has a value of 0 and an unporgrammed fuse has a value of 1. Make sure not to accidentally change other fuse bits. If the reset enabled fuse is changed, the device will not be programmable except in high voltage programming mode (not covered here, and I don’t use it). The default hfuse value is: 0b11011111 = 0xdf . Bit7 is DWEN, so to program this fuse, a 0 is written to it. The new hfuse value is: 0b01011111 or 0×5f . The avrdude command string to write that fuse is:

avrdude -c dragon_isp -P usb -p t2313 -U hfuse:w:0x5f:m

debugWire mode can be verified by reading the device signature:

avrdude -c dragon_dw -P usb -p t2313 -F

To go back to ISP mode, first you must temporarily disable debugWire, then unprogram (write a 1) the DWEN fuse. avrdude can disable debugWire by issuing a command for the dragon_isp interface. as an example:

avrdude -c dragon_isp -P usb -p t2313 -F

avrdude initially fails, then tries to disable debugWire. Now issue this command:

avrdude -c dragon_isp -P usb -p t2313 -U hfuse:w:0xdf:m

Now the device is back in ISP mode. A final product should not be left in debugWire mode because it will leave some clocks turned on during sleep modes.

In part 2, we will build a C program with gcc-avr, download to flash, and debug the code.