Sunday, August 24, 2014

Setting up the Toolchain for Atmel AVR programming in Windows

The basic setup for an open source toolchain will get you pretty far in programming Atmel AVR's that are supported by avr-gcc. If you want to program these AVR's directly using standard c code, you'll need a toolchain, plus some hardware.  Here are my notes for setting this up on a Windows 8 machine.

Hardware Needed:

  1. ATMega32A - PU (the PU package is breadboard friendly)
  2. An ISP / USB Flash Programmer -
    This is a USB device that allows you to send compiled hex files down from your computer to the chip. The device uses the SPI interface and  reset on the chip to load code onto the chip. I've been using my Arduino Mega for this.
  3. Some basic hardware: A Breadboard, an LED, and some wires for testing.

Software Needed:

  1. Notepad++  (You could also use Programmers Notepad that is included with the WinAVR suite or another IDE to edit and write your code).
  2. WinAVR - This is an open source suite of compiler tools that includes the compiler and AVRDUDE uploader software in one great package
    •  avr-gcc - this a compiler you can use to compile c  and c++ code and turn it into a hex file that can be run on an ATmel AVR 
    • AVRDUDE - This is the software tool that understands how to make your flash programmer upload your hex compiled code to your AVR chip.
  3. make file - these are text files that allow you to run all the tools in one easy step from from compiling your c code with avr-gcc to uploading it with AVRDUDE
  4. Code to upload and test with.


Step 1: Install WinAVR

  1. Download WinAVR and install it on your computer.
    You can download WinAVR from sourceforge by clicking here.
  2. Download Notepad++ and install it on your computer (or use another editor).
    You can download Notepad++ here


Step 2:  The Makefile

A make file is sort of like a script (or batch file) that can help you compile your code and then do other stuff too. In this case, it's going to compile your code and if your circuit connections are good, it's going to upload your code to the AVR chip! Sweet right!?! The easiest way to get started with make files is to start with one that already does most of what you want it and customize it for your needs.
Click here to download a Makefile and blink.c code to test with.

The make file contains only a few items that need to be changed. These are items specific to the type of programmer being used, the target avr chip, and the source code being compiled and uploaded.

Target AVR Flags:

MCU = atmega32    
F_CPU = 1000000
BAUD = 9600

MCU :   This tells avr-gcc what target to compile the code for.  In this case the target is an atmega32. To find out what other targets are supported just go to the command prompt and type:
  avr-gcc --target-help
F_CPU :   This tells avr-gcc the clock frequency in Hertz for the target AVR. This needs to be correct for anything timing related to work properly. Note: this does not set the clock frequency! It just tells avr-gcc what the frequency is. The specifics of the clock operations can be found by searching for "System Clock and Clock Options" in the ATMega32 Datasheet. It may be a bit daunting, but basically it tells you that the default clock source is set to 1 MHz.
BAUD : This is the baud rate used for serial communications between the avr and your computer. This just tells the avr what speed you want to talk at. The avr may support higher baud rates that are documented in the datasheet. 9600 is a safe default.

Source Code specifiers:

MAIN = blink.c
LOCAL_SOURCE = blink.h
EXTRA_SOURCE_DIR = ..\..\lib
EXTRA_SOURCE_FILES = myotherfile.c

MAIN:  The source file that contains your main function.
LOCAL_SOURCE:               Additional code files in the same directory as your main file.
EXTRA_SOURCE_DIR :      Specify another directory to look for source files.
EXTRA_SOURCE_FILES:   Specify additional source files to include.

Every Make file usually has some way of specifying these, although it often has different variables than the ones above. Check out the one make file template included with WinAVR to see one that is very different.

 Programmer Flags:

PROGRAMMER_TYPE = avrisp
PROGRAMMER_ARGS = -v -v -cstk500v1 -P\\.\COM4 -b19200

PROGRAMMER_TYPE :    This is the name that tells avrdude what programmer you are using. To find out what programmer options are supported in avrdude go to the command prompt and type:
avrdude -c ?
PROGRAMMER_ARGS :   These are all the other arguments that will be passed to avrdude. Depending on the type of programmer you use. To use the arduino as an ISP, I need to specify that the Arduino is on COM4, and that the ISP programmer code uses a baud rate of 19200. Also stk500v1 tells it to use the Atmel STK 500 version 1 and -v -v tells it to give verbose output.

STEP 3: Circuit Setup - Arduino as an ISP programmer

The circuit setup is the same as it was to program the ATTIN84A except that the pins are different on the ATMega32A.

Arduino Mega/ Uno Pins to ATMEGA32A Pins
MISO pin 50/ pin 12->pin 7 MISO
MOSI pin 51/ pin 11->pin 6 MOSI
CLK pin 52/ pin 13->pin 8 SCK
SS pin 53/ pin 10->pin 9 RESET
5V->pin 10 VCC
GND->pin 11 GND
RESET to Capacitor to->GND


STEP 4:  Program the Arduino as an ISP programmer

To turn the Arduino Mega into an AVR ISP you need to upload the  ArduinoISP sketch to your Arduino board.
  1. Using the USB cable, connect your Arduino Mega 2560 or Arduino Uno to your computer.
  2. Open the the Arduino IDE. 
  3. Under Tools -> Boards select the "Arduino Mega 2560" (or select "Arduino Uno" if you are using that instead) as the board.
  4. Under Tools -> Programmer select "AVR ISP"
  5. Select File -> Examples -> ArduinoISP to open the Arduino ISP sketch. 
  6. Compile the sketch and upload it to your Arduino Board. 
   Your Arduino is now ready to use as an in system programmer!

STEP 5: Run the Makefile 

Now we are ready to test it out. 
  1. Make sure the COM port in the make file  PROGRAMMER_ARGS matches the one the Arduino is connected to. (It is currently set to COM4 in the make file).
  2. Once the COM port is correct, go to the command line in the directory where the Makefile and blink.c are extracted.  On the command line type:
    make flash
   If it works you should see a lot of output with no errors. At the end it should say:
   avrdude done.  Thank you.
 
If you get an error, check your circuit and make sure the connections are correct. Also verify the COM port is correct,  and verify the baud rate matches the ArduinoISP sketch.

STEP 6: Test the blink code on the ATMega32 with an LED

If everything else worked above, connect an LED from Pin 1 (PB0) to ground.  And it blinks!

 

No comments:

Post a Comment