Google Analytics

Tuesday, June 26, 2012

Banjo on - AVR Bootloader with USBtinyISP

I've been really struggling with something that just didn't make sense. Part of the reason is incomplete information on my part.  But part of that blame goes to those that are selling ISP programmers in not making this clear.

I'm going to put this here in the hopes that it helps someone, and also so I can refer back to it in the future!  However, this post is going to be subject to change as I learn more or need to correct it.


I built this Minimilast EvilMadScientist board for ISP programming, an ISP (In System Programmer).  Only it didn't work for me.  There wasn't a problem with the board; there was a problem with my understanding.

I had this device (USBtinyISP) to use to with the board above.

I never could get things to work...until I found out ONE THING...

When you buy an ATMega328P without a bootloader, by default it comes with the fuse set for using the internal crystal.  However, if you place a bootloader on this chip via an ISP such as the USBtinyISP above,   then it changes the fuse to use an external crystal....at least that's my understanding right now, and it appears to be right based on ability to now place a bootloader on a chip.


Burning a Bootloader
So, here's my understanding of how to place a bootloader onto a chip that doesn't have a bootloader.  It will use the AVRDUDE to load the data, and it will need to have an device driver installed for Windows (I don't know about Linux of Mac).  See this for details and a link, and scroll down to the Description section for info on this.

Now:

  1. Place the chip onto the device that will hold the chip, such as the Minimalist EMS board above.
  2. If it's a new chip and doesn't have a bootloader on it, then you can use the USBtinyISP to program it by connecting the USBtinyISP to the header on the board.
  3. Using the Arduino 1.0 IDE, select: Tools->Board->Arduino Duemilanove.  This is what I have to do on mine to get it to work....not sure why this matters; I could be wrong, since we aren't communicating to a board in this case.
  4. Again, on the Arduino 1.0 IDE, select: Tools->Programmer->USBtinyISP.
  5. Again, on the Arduino 1.0 IDE, select: Tools->Burn Bootloader.  If this doesn't work for you, then I can't help you yet as I don't know enough yet!  So have a look at this site and also anything else you can dig up via google.
If you've got a chip that you want to add code to it, and it has had a bootloader on it before, and for some reason it won't work anymore (e.g., gets a synch problem from the Arduino 1.0 IDE), then you may need to reload a bootloader.  However, if you need to do so, the fuse may have been set to use an external crystal.  So now, you are going to have to add an external crystal to the board, and then you will be able to push a bootloader onto it, and then use it in an Arduino board.  

NEEDS SOME MORE WORK

To transfer a file to a chip via the ISP and the USBtinyISP, you'll need to open a command prompt and use avrdude.  Note that this approach, not using the Arduino IDE, and directly using avrdude, means you are going to be transferring a HEX file.

How do you get a .hex file?  Well, it's actually produced each time by the Arduino 1.0 IDE.  You don't normally see it because it's placed into (at least on my computer!  Your's may differ!) C:\Users\\AppData\Local\Temp\.  (Note: you can probably change the Arduino 1.0 IDE preferences.txt to put it somewhere easier to get to).  (Note: AppData is a hidden directory, so you'll have to enable it in your file folders setting to even be able to find it! - one way to locate it is to do a file search for the file.)

On my sysem, doing the compile under Arduino 1.0 IDE for my Blinky2.ino produces a hex file named Blink2.cpp.hex, and places this into a new file folder (see the date/time stamp on the folder) in the AppData folder.  This is the file I'm going to transfer!

And here's how I'm going to transfer it to the chip.  If the chip has a bootloader on it, this is going to overlay it, so the bootloader won't work now on a Arduino.  So you just got 2K back from not having a bootloader.  However, if this chip had a bootloader on it at some point, then the fuse is set for external crystal, and won't run unless you either change the fuse setting (I'm not getting into that here - search google), or have a crystal on the target board.  It's also going to have to be there for the transfer to work!

Here's the command line (in a command window) that's going to do the transfer (make sure you've installed WinAVR per the link above with the driver info - if you get a 'command not found' error, then either your PATH isn't set up in your environment variables, or you haven't installed the drivers and WinAVR).  
        
         avrdude -p m328p -c usbtiny -U flash:w:Blink2.cpp.hex

No comments:

Post a Comment