Using AVR Dragon to burn bootloader

Hi! I have an AVR Dragon that I want to use as an ISP. I've hooked it up to an Arduino Uno, added the AVR Dragon to the programmers.txt list. I'm using a Mac by the way...

dragonisp.name=Dragon ISP
dragonisp.communication=usb
dragonisp.protocol=dragon_isp
dragonisp.program.protocol=dragon_isp
dragonisp.program.tool=avrdude
dragonisp.program.extra_params= -P usb

When I hit "burn bootloader" the AVR Dragon are flashing a few seconds before i get this error in Arduino IDE:

avrdude: usbdev_open(): did not find any USB device "usb"
Error while burning bootloader.

The weird thing is that the AVR dragon works fine in the Avrdude command line (v 5.11.1)!
sending this command:

avrdude -c dragon_isp -P usb -p atmega328p -U flash:w:main.hex:i

gives no error message, and the program are running on the mega328p (it blinks some LEDs)

How can I get the AVR dragon to work with Arduino IDE (1.6.5)?

Turn on verbose uploads, and see what command it's actually sending

This it the output from Arduino when I've turned on verbose upload:

programmers.txt

drgisp.name=AVR Dragon
drgisp.protocol=dragon_isp
drgisp.communication=usb
drgisp.program.extra_params= -P usb -v
/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avrdude -C/Applications/Arduino.app/Contents/Java/hardware/tools/avr/etc/avrdude.conf -v -patmega328p -cdragon_isp -P usb -v && sleep 5 -e -Ulock:w:0x3F:m -Uefuse:w:0x05:m -Uhfuse:w:0xDE:m -Ulfuse:w:0xFF:m 

avrdude: Version 6.0.1, compiled on Apr 14 2015 at 16:30:25
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "/Applications/Arduino.app/Contents/Java/hardware/tools/avr/etc/avrdude.conf"
         User configuration file is "/Users/hansi/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : usb
         Using Programmer              : dragon_isp
avrdude: stk500v2_dragon_isp_open()
avrdude: usbdev_open(): Found AVRDRAGON, serno: 00A200000F1A
avrdude: jtagmkII_getsync(): Sending sign-on command: 0x86 (26 bytes msg)
JTAG ICE mkII sign-on message:
Communications protocol version: 1
M_MCU:
  boot-loader FW version:        255
  firmware version:              1.00
  hardware version:              1
S_MCU:
  boot-loader FW version:        255
  firmware version:              1.02
  hardware version:              2
Serial number:                   00:a2:00:00:0f:1a
Device ID:                       AVRDRAGON
avrdude: jtagmkII_setparm()
avrdude: jtagmkII_setparm(): Sending set parameter command (parm 0x03, 1 bytes): 0x80 (1 bytes msg)
avrdude: jtagmkII_getsync(): Sending get sync command: 0x80 (1 bytes msg)
         AVR Part                      : ATmega328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : DRAGON_ISP
         Description     : Atmel AVR Dragon in ISP mode
avrdude: jtagmkII_getparm()
avrdude: jtagmkII_getparm(): Sending get parameter command (parm 0x06): 0x81 (3 bytes msg)
         Vtarget         : 4.7 V
         SCK period      : 1.00 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.15s

avrdude: Device signature = 0x1e950f
avrdude: stk500v2_jtagmkII_close()
avrdude: jtagmkII_close()
avrdude: jtagmkII_close(): Sending sign-off command: 0x80 (1 bytes msg)

avrdude done.  Thank you.

/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avrdude -C/Applications/Arduino.app/Contents/Java/hardware/tools/avr/etc/avrdude.conf -v -patmega328p -cdragon_isp -P usb -v && sleep 5 -Uflash:w:/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/bootloaders/optiboot/optiboot_atmega328.hex:i -Ulock:w:0x0F:m 

avrdude: Version 6.0.1, compiled on Apr 14 2015 at 16:30:25
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "/Applications/Arduino.app/Contents/Java/hardware/tools/avr/etc/avrdude.conf"
         User configuration file is "/Users/hansi/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : usb
         Using Programmer              : dragon_isp
avrdude: stk500v2_dragon_isp_open()
avrdude: usbdev_open(): did not find any USB device "usb"
Error while burning bootloader.

EDIT:
OOH, I found this known issue. Is there possible to add a delay, so that the USB device can be reset before writing the hex file?

See the issue https://github.com/arduino/Arduino/issues/2986 for more discussion(for AVRISP mkII but it's caused by the same problem). dmjlambert found a workaround here: Solved:Bootloader from Arudino IDE with AVRISPmkII fails - Installation & Troubleshooting - Arduino Forum that should make the dragon work. I also created a workaround with a slightly different approach here: GitHub - per1234/Arduino-AVRISPmkII-fix: An ugly workaround for AVRISP mkII not working with Arduino IDE 1.6.x that could be easily adapted to the dragon. If you're interested I'm happy to help out with that. The word is that adding the fixed avrdude to the Arduino IDE is in the works but this issue has been around so long I'm not holding my breath.

I tried to add the extra parameters. Now I get a content mismatch error. The microcontroller is not dead, I'm still able to upload code with the Dragon. Is this the error you mentioned in the other post?

/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avrdude -C/Applications/Arduino.app/Contents/Java/hardware/tools/avr/etc/avrdude.conf -v -patmega16 -cdragon_isp -Pusb -Uflash:w:/Users/hansi/Documents/Arduino/hardware/DIP-40/avr/bootloaders/optiboot/atmega16/optiboot_atmega16_16MHz_external.hex:i -Ulock:w:0x0f:m -e -Ulock:w:0x3f:m -Uhfuse:w:0xdc:m -Ulfuse:w:0xbf:m 

avrdude: Version 6.0.1, compiled on Apr 14 2015 at 16:30:25
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "/Applications/Arduino.app/Contents/Java/hardware/tools/avr/etc/avrdude.conf"
         User configuration file is "/Users/hansi/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : usb
         Using Programmer              : dragon_isp
avrdude: usbdev_open(): Found AVRDRAGON, serno: 00A200000F1A
JTAG ICE mkII sign-on message:
Communications protocol version: 1
M_MCU:
  boot-loader FW version:        255
  firmware version:              1.00
  hardware version:              1
S_MCU:
  boot-loader FW version:        255
  firmware version:              1.02
  hardware version:              2
Serial number:                   00:a2:00:00:0f:1a
Device ID:                       AVRDRAGON
         AVR Part                      : ATmega16
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PA0
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom         4    10   128    0 no        512    4      0  9000  9000 0xff 0xff
           flash         33     6   128    0 yes     16384  128    128  4500  4500 0xff 0xff
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00
           calibration    0     0     0    0 no          4    0      0     0     0 0x00 0x00

         Programmer Type : DRAGON_ISP
         Description     : Atmel AVR Dragon in ISP mode
         Vtarget         : 4.9 V
         SCK period      : 1.00 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.15s

avrdude: Device signature = 0x1e9403
avrdude: erasing chip
avrdude: reading input file "/Users/hansi/Documents/Arduino/hardware/DIP-40/avr/bootloaders/optiboot/atmega16/optiboot_atmega16_16MHz_external.hex"
avrdude: writing flash (16384 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 16384 bytes of flash written
avrdude: verifying flash memory against /Users/hansi/Documents/Arduino/hardware/DIP-40/avr/bootloaders/optiboot/atmega16/optiboot_atmega16_16MHz_external.hex:
avrdude: load data flash data from input file /Users/hansi/Documents/Arduino/hardware/DIP-40/avr/bootloaders/optiboot/atmega16/optiboot_atmega16_16MHz_external.hex:
avrdude: input file /Users/hansi/Documents/Arduino/hardware/DIP-40/avr/bootloaders/optiboot/atmega16/optiboot_atmega16_16MHz_external.hex contains 16384 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: 16384 bytes of flash verified
avrdude: reading input file "0x0f"
avrdude: writing lock (1 bytes):

Writing | ################################################## | 100% 0.15s

avrdude: 1 bytes of lock written
avrdude: verifying lock memory against 0x0f:
avrdude: load data lock data from input file 0x0f:
avrdude: input file 0x0f contains 1 bytes
avrdude: reading on-chip lock data:

Reading | ################################################## | 100% 0.06s

avrdude: verifying ...
avrdude: 1 bytes of lock verified
avrdude: reading input file "0x3f"
avrdude: writing lock (1 bytes):

Writing |  ***failed;  
################################################## | 100% 0.40s

avrdude: 1 bytes of lock written
avrdude: verifying lock memory against 0x3f:
avrdude: load data lock data from input file 0x3f:
avrdude: input file 0x3f contains 1 bytes
avrdude: reading on-chip lock data:

Reading | ################################################## | 100% 0.05s

avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x0000
         0x0f != 0x3f
avrdude: verification error; content mismatch

avrdude done.  Thank you.

Error while burning bootloader.

hansibull:
I tried to add the extra parameters.

I'm guessing you mean the dmjlambert workaround?

hansibull:
Is this the error you mentioned in the other post?

No, when I use the dmjlambert workaround with my mkII(or any of the other programmer I tested it with, but I don't have a dragon) I don't get any error at all, it works fine. With my workaround I still get the avrdude: usbdev_open(): did not find any USB device "usb" error but that's the only one. Your dragon is now successfully erasing, writing the bootloader and the lock_bits but then it fails on writing the unlock_bits. Does this happen every time you try?

That command like is a little wacky

/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avrdude -C/Applications/Arduino.app/Contents/Java/hardware/tools/avr/etc/avrdude.conf -v -patmega16 -cdragon_isp -Pusb -Uflash:w:/Users/hansi/Documents/Arduino/hardware/DIP-40/avr/bootloaders/optiboot/atmega16/optiboot_atmega16_16MHz_external.hex:i -Ulock:w:0x0f:m -e -Ulock:w:0x3f:m -Uhfuse:w:0xdc:m -Ulfuse:w:0xbf:m

setting the lock bits then a -e to erase the chip, then setting the lock bits again. And before all that writing the flash. Looks weird to me.

I suggest just use the IDE to burn the bootloader. If in preferences you turn on show verbose output during upload, you will see the 2 avrdude commands it tries to run. The first one it runs ok and the second one does not work. Paste the 2nd one onto the command line to finish the job. Then call it good.

this is the output from Arduino IDE with verbose uploads. Only one command are printed on the screen.

/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avrdude -C/Applications/Arduino.app/Contents/Java/hardware/tools/avr/etc/avrdude.conf -v -patmega328p -cdragon_isp -Pusb -Uflash:w:/Users/hansi/Documents/Arduino/hardware/Basic/avr/bootloaders/optiboot/optiboot_atmega328.hex:i -Ulock:w:0x0F:m -e -Ulock:w:0x3F:m -Uefuse:w:0x05:m -Uhfuse:w:0xde:m -Ulfuse:w:0xff:m 

avrdude: Version 6.0.1, compiled on Apr 14 2015 at 16:30:25
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "/Applications/Arduino.app/Contents/Java/hardware/tools/avr/etc/avrdude.conf"
         User configuration file is "/Users/hansi/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : usb
         Using Programmer              : dragon_isp
avrdude: usbdev_open(): Found AVRDRAGON, serno: 00A200000F1A
JTAG ICE mkII sign-on message:
Communications protocol version: 1
M_MCU:
  boot-loader FW version:        255
  firmware version:              1.00
  hardware version:              1
S_MCU:
  boot-loader FW version:        255
  firmware version:              1.02
  hardware version:              2
Serial number:                   00:a2:00:00:0f:1a
Device ID:                       AVRDRAGON
         AVR Part                      : ATmega328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : DRAGON_ISP
         Description     : Atmel AVR Dragon in ISP mode
         Vtarget         : 4.7 V
         SCK period      : 1.00 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.15s

avrdude: Device signature = 0x1e950f
avrdude: erasing chip
avrdude: reading input file "/Users/hansi/Documents/Arduino/hardware/Basic/avr/bootloaders/optiboot/optiboot_atmega328.hex"
avrdude: writing flash (32768 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 32768 bytes of flash written
avrdude: verifying flash memory against /Users/hansi/Documents/Arduino/hardware/Basic/avr/bootloaders/optiboot/optiboot_atmega328.hex:
avrdude: load data flash data from input file /Users/hansi/Documents/Arduino/hardware/Basic/avr/bootloaders/optiboot/optiboot_atmega328.hex:
avrdude: input file /Users/hansi/Documents/Arduino/hardware/Basic/avr/bootloaders/optiboot/optiboot_atmega328.hex contains 32768 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: 32768 bytes of flash verified
avrdude: reading input file "0x0F"
avrdude: writing lock (1 bytes):

Writing | ################################################## | 100% 0.15s

avrdude: 1 bytes of lock written
avrdude: verifying lock memory against 0x0F:
avrdude: load data lock data from input file 0x0F:
avrdude: input file 0x0F contains 1 bytes
avrdude: reading on-chip lock data:

Reading | ################################################## | 100% 0.05s

avrdude: verifying ...
avrdude: 1 bytes of lock verified
avrdude: reading input file "0x3F"
avrdude: writing lock (1 bytes):

Writing |  ***failed;  
################################################## | 100% 0.40s

avrdude: 1 bytes of lock written
avrdude: verifying lock memory against 0x3F:
avrdude: load data lock data from input file 0x3F:
avrdude: input file 0x3F contains 1 bytes
avrdude: reading on-chip lock data:

Reading | ################################################## | 100% 0.05s

avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x0000
         0x0f != 0x3f
avrdude: verification error; content mismatch

avrdude done.  Thank you.

Error while burning bootloader.

This is my programmer

drgisp.name=Dragon ISP
drgisp.protocol=dragon_isp
#drgisp.program.tool=avrdude
drgisp.communication=usb
drgisp.program.extra_params=-Pusb "-Uflash:w:{runtime.platform.path}/bootloaders/{bootloader.file}:i" -Ulock:w:{bootloader.lock_bits}:m

How can I get Arduino IDE to display the other command? I only get to see the first one..

Need to hear from pert on that, I don't know what sort of workaround you attempted to put in place. The IDE normally uses 2 avrdude commands to do burn bootloader. You would have to get it back into that normal state. Then just run the second command on the command line after using the IDE to attempt a burn bootloader.

Of course, it was the extra parameters in the programmers.txt file. Now I got two commands. The second command runs without any errors with avrdude.

So, I'm able to burn bootloader using the AVR dragon, but I need to use Avrdude externally to to the upload. Will there be a fix for the Arduino IDE, so these problems doesn't happend with the AVR dragon and the AVRISP MKII? I tested the latest hourly IDE build. it got the same Avrdude version as IDE 1.6.5

dmjlambert:
setting the lock bits then a -e to erase the chip, then setting the lock bits again. And before all that writing the flash. Looks weird to me.

I think avrdude is smart enough to do things in the right order even if the parameters are out of order. If you look at the output it is doing the erase first so it gets that right. I don't know what order the flash/lock bits and fuses are supposed to be done in but it works for me fine using every programmer I own.

I took a look at the avrdude command and found some issues that I think are causing hansibull's problem:

hansibull:
-patmega16

Are you burning the bootloader on an ATmega328P? If so it should be -patmega328p instead of -patmega16.

hansibull:
-Ulock:w:0x0f:m -e -Ulock:w:0x3f:m -Uhfuse:w:0xdc:m -Ulfuse:w:0xbf:m

it's missing

-Uefuse:w:0x05:m

Maybe you messed up the command when you did the edits for the workaround? Or maybe this is related to the atmega16 issue?

pert:
I'm guessing you mean the dmjlambert workaround?

hansibull:
This is my programmer

drgisp.name=Dragon ISP

drgisp.protocol=dragon_isp
#drgisp.program.tool=avrdude
drgisp.communication=usb
drgisp.program.extra_params=-Pusb "-Uflash:w:{runtime.platform.path}/bootloaders/{bootloader.file}:i" -Ulock:w:{bootloader.lock_bits}:m

Ok, looks like you used my workaround, not djmlambert's. If you want my assistance it would be helpful if you actually would answer my questions.

Why did you comment out the program.tool line?

Can you find the programmers.txt file that is associated with the board you're trying to burn the bootloader to? Please paste the tools.avrdude.erase.pattern line. It looks like the efuse parameter is missing from yours. It should be:

tools.avrdude.erase.pattern="{cmd.path}" "-C{config.path}" {erase.verbose} -p{build.mcu} -c{protocol} {program.extra_params} -e -Ulock:w:{bootloader.unlock_bits}:m -Uefuse:w:{bootloader.extended_fuses}:m -Uhfuse:w:{bootloader.high_fuses}:m -Ulfuse:w:{bootloader.low_fuses}:m

hansibull:
How can I get the AVR dragon to work with Arduino IDE (1.6.5)?

Simple answer is you have to fix avrdude or patch something else to work around the avrdude issue.

If you read the links to the open issues, you will see that I've been trying to get this fixed for 6+ years.
If you are not using windows, you can create a simple (1 line) wrapper script that adds a blind delay of a couple of seconds before executing avrdude.
That is probably one of the easiest work arounds that "just works".

The real issue is not that the dragon doesn't work with the IDE. It is an avrdude issue that crops up based on the way the IDE is using avrdude and the code in avrdude along with the dragon being very slow to power-up and initialize the USB with the PC host.
When not burning a bootloader it will work just fine with the IDE.
The problem is triggered from a combination of the IDE is using separate commands to set the fuses and burn the bootloader, avrdude screwing up the USB bus when it exits, and the dragon being slow to reconnect to the PC host after the USB is reset. As a result, when the second avrdude command is run, the dragon has not re-enumerated with the PC host yet and so avrdude does not see it. This is an avrdude problem and that is where it needs to be fixed.
It is a 6 line change to fix it in avrdude.

Also you might want to know that avrdude is not using the best communication method when talking to the Dragon.
It uses some low level ways of communicating, so as a result, it is not as fast at doing things as a cheap ISP programmer.
An under $10 USBasp will program bootloaders faster than the dragon.

While I have the dragon working for me since I have installed a programmer type for it that works across all my installed IDE versions (I have 19+ versions of the Arduino IDE installed) and I'm using a patched version of avrdude that has the back to back command issue fixed, I don't even bother to use my dragon for burning bootloaders anymore since my cheap USBasp device will burn a bootloader in under 2 seconds and it is supported "out of the box" by the IDE.

The only time I use the Dragon is if I want to do source level debugging or programming using debugwire.

--- bill