Back to home

Attiny2313, AVRdude and Arduino & AVRisp

Today I've tested the avrdude, avrisp and arduino & avrisp toolchain (AAAA-toolchain) to program avr chips, especially the attiny2313 chip. Arduino is a very robust board used in various hardware projects. Using the arduino programming environment you can easily program the chip. With the AAAA-toolchain you can use your arduino board to program other AVR chips. Below I'll describe the steps necessary to program an attiny2313 chip to blink a led.

What you need

Installation

Arduino
Simply unpack the arduino zip file. Copy the arduino-0018 directory to a place you like, I used C:\Program Files\arduino-0018

Mega-isp
Unpack the zip which contains the avrisp.pde file. This is the Arduino project file which you need to burn to your Arduino.

WinAVR

  • Follow the installation wizard
  • Add the binary path to your system PATH variable. On windows, I used the default installation directory (C:\WinAVR-20100110\bin). By adding this to your PATH environment variable you're able to execute the "avrdude" command from any CMD window, in any path. Google for a description on how to set your PATH environment variable.

The hardware

Okay, when you've downloaded and installed all necessary files you're ready to create your circuit. But first some info, so you know what you're doing. Using the AAAA-toolchain you use the arduino to program another AVR chip. The arduino uses ICSP (in circuit serial programming) for this. Basically this means that you need to connect a couple of wires from the arduino to your AVR chip (attiny2313). We need to connect the standard MOSI, MISO, SCK, RST wires for ICSP. Use this wiring:

Programming wires

ArduinoAttiny2313
Pin 10 Pin 1 - RST
Pin 11Pin 17 - MOSI
Pin 12Pin 18 - MISO
Pin 13Pin 19 - SCK
Vcc (+5V)Pin 20 - Vcc
GndPin 10

Prepping the Arduino

Start the Arduino IDE and open the avrisp.pde file you downloaded before. Connect your Arduino, select the correct board and COM port and upload the avr-isp code to the arduino. I'll not explain this more thoroughly as it's basic arduino programming. When you've uploaded avrisp to the arduino you're ready to create the "hello world", blinking led application to test this avr-isp programming method.

Programming the attiny2313

When you want to program your attiny2313, you need to understand that we use different steps to get the program-code on the chip. To program the attiny2313 you need to use "avrdude", which is also used by Arduino (under the hood, if I'm correct). AVRDude is able to put the program-code on the chip using the avrisp programming method. WinAVR is set of tools you need to program an attiny (or other avr) without Arduino, using avrdude. The steps you need to take are:

● write the application code in C
● compile the code for the attiny2313 chip using avr-gcc
● upload the compiled code (hex file) to your attiny2313 using avrdude

Application code
I just googled for a attiny2313 blinking led example that I'll use. Connect pin 12 of your attiny2313 to a led with a current limiting resistor. Copy and paste this code in a file named blinking_led_2313.c.

#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
 
int main(void) {
    DDRB |= 1<<PB0; /* set PB0 to output */
    while(1) {
        PORTB &= ~(1<<PB0); /* LED on */
        _delay_ms(100);
        PORTB |= 1<<PB0; /* LED off */
        _delay_ms(900);
    }
    return 0;
}

Compile the code to a hex file
Create a new file in the same directory of your blinking_led_2313.c file with the name "Makefile" and put this code in it (see below). You can easily reuse this makefile for other projects by changing the "PRJSRC" variable.

# See: http://electrons.psychogenic.com/modules/arms/view.php?w=art&idx=8&page=2
##### This Makefile will make compiling Atmel AVR 
##### micro controller projects simple with Linux 
##### or other Unix workstations and the AVR-GCC 
##### tools.
#####
##### It supports C, C++ and Assembly source files.
#####
##### Customize the values as indicated below and :
##### make
##### make disasm 
##### make stats 
##### make hex
##### make writeflash
##### make gdbinit
##### or make clean
#####
##### See the http://electrons.psychogenic.com/ 
##### website for detailed instructions
 
 
####################################################
#####                                          #####
#####              Configuration               #####
#####                                          #####
##### Customize the values in this section for #####
##### your project. MCU, PROJECTNAME and       #####
##### PRJSRC must be setup for all projects,   #####
##### the remaining variables are only         #####
##### relevant to those needing additional     #####
##### include dirs or libraries and those      #####
##### who wish to use the avrdude programmer   #####
#####                                          #####
##### See http://electrons.psychogenic.com/    #####
##### for further details.                     #####
#####                                          #####
####################################################
 
 
#####         Target Specific Details          #####
#####     Customize these for your project     #####
 
# Name of target controller 
# (e.g. 'at90s8515', see the available avr-gcc mmcu 
# options for possible values)
MCU=attiny2313
 
# Name of our project
# (use a single word, e.g. 'myproject')
PROJECTNAME=blinking_led_2313
 
# Source files
# List C/C++/Assembly source files:
# (list all files to compile, e.g. 'a.c b.cpp as.S'):
# Use .cc, .cpp or .C suffix for C++ files, use .S 
# (NOT .s !!!) for assembly source code files.
PRJSRC=blinking_led_2313.c
 
# additional includes (e.g. -I/path/to/mydir)
INC=
 
# libraries to link in (e.g. -lmylib)
LIBS=
 
# Optimization level, 
# use s (size opt), 1, 2, 3 or 0 (off)
OPTLEVEL=s
 
 
#####      AVR Dude 'writeflash' options       #####
#####  If you are using the avrdude program
#####  (http://www.bsdhome.com/avrdude/) to write
#####  to the MCU, you can set the following config
#####  options and use 'make writeflash' to program
#####  the device.
 
 
# programmer id--check the avrdude for complete list
# of available opts.  These should include stk500,
# avr910, avrisp, bsd, pony and more.  Set this to
# one of the valid "-c PROGRAMMER-ID" values 
# described in the avrdude info page.
# 
AVRDUDE_PROGRAMMERID=avrisp
 
# port--serial or parallel port to which your 
# hardware programmer is attached
#
AVRDUDE_PORT=com12
 
 
####################################################
#####                Config Done               #####
#####                                          #####
##### You should not need to edit anything      #####
##### below to use the makefile but may wish   #####
##### to override a few of the flags           #####
##### nonetheless                              #####
#####                                          #####
####################################################
 
 
##### Flags ####
 
# HEXFORMAT -- format for .hex file output
HEXFORMAT=ihex
 
# compiler
CFLAGS=-I. $(INC) -g -mmcu=$(MCU) -O$(OPTLEVEL) \
    -fpack-struct -fshort-enums             \
    -funsigned-bitfields -funsigned-char    \
    -Wall -Wstrict-prototypes               \
    -Wa,-ahlms=$(firstword                  \
    $(filter %.lst, $(<:.c=.lst)))
 
# c++ specific flags
CPPFLAGS=-fno-exceptions               \
    -Wa,-ahlms=$(firstword         \
    $(filter %.lst, $(<:.cpp=.lst))\
    $(filter %.lst, $(<:.cc=.lst)) \
    $(filter %.lst, $(<:.C=.lst)))
 
# assembler
ASMFLAGS =-I. $(INC) -mmcu=$(MCU)        \
    -x assembler-with-cpp            \
    -Wa,-gstabs,-ahlms=$(firstword   \
        $(<:.S=.lst) $(<.s=.lst))
 
 
# linker
LDFLAGS=-Wl,-Map,$(TRG).map -mmcu=$(MCU) \
    -lm $(LIBS)
 
##### executables ####
CC=avr-gcc
OBJCOPY=avr-objcopy
OBJDUMP=avr-objdump
SIZE=avr-size
AVRDUDE=avrdude
REMOVE=rm -f
 
##### automatic target names ####
TRG=$(PROJECTNAME).out
DUMPTRG=$(PROJECTNAME).s
 
HEXROMTRG=$(PROJECTNAME).hex 
HEXTRG=$(HEXROMTRG) $(PROJECTNAME).ee.hex
GDBINITFILE=gdbinit-$(PROJECTNAME)
 
# Define all object files.
 
# Start by splitting source files by type
#  C++
CPPFILES=$(filter %.cpp, $(PRJSRC))
CCFILES=$(filter %.cc, $(PRJSRC))
BIGCFILES=$(filter %.C, $(PRJSRC))
#  C
CFILES=$(filter %.c, $(PRJSRC))
#  Assembly
ASMFILES=$(filter %.S, $(PRJSRC))
 
 
# List all object files we need to create
OBJDEPS=$(CFILES:.c=.o)    \
    $(CPPFILES:.cpp=.o)\
    $(BIGCFILES:.C=.o) \
    $(CCFILES:.cc=.o)  \
    $(ASMFILES:.S=.o)
 
# Define all lst files.
LST=$(filter %.lst, $(OBJDEPS:.o=.lst))
 
# All the possible generated assembly 
# files (.s files)
GENASMFILES=$(filter %.s, $(OBJDEPS:.o=.s)) 
 
 
.SUFFIXES : .c .cc .cpp .C .o .out .s .S \
    .hex .ee.hex .h .hh .hpp
 
 
.PHONY: writeflash clean stats gdbinit stats
 
# Make targets:
# all, disasm, stats, hex, writeflash/install, clean
all: $(TRG)
 
disasm: $(DUMPTRG) stats
 
stats: $(TRG)
    $(OBJDUMP) -h $(TRG)
    $(SIZE) $(TRG) 
 
hex: $(HEXTRG)
 
 
writeflash: hex
    $(AVRDUDE) -c $(AVRDUDE_PROGRAMMERID)   \
     -p $(MCU) -P $(AVRDUDE_PORT) -e        \
     -b 19200 \
     -U flash:w:$(HEXROMTRG)
 
install: writeflash
 
$(DUMPTRG): $(TRG) 
    $(OBJDUMP) -S  $< > $@
 
 
$(TRG): $(OBJDEPS) 
    $(CC) $(LDFLAGS) -o $(TRG) $(OBJDEPS)
 
 
#### Generating assembly ####
# asm from C
%.s: %.c
    $(CC) -S $(CFLAGS) $< -o $@
 
# asm from (hand coded) asm
%.s: %.S
    $(CC) -S $(ASMFLAGS) $< > $@
 
 
# asm from C++
.cpp.s .cc.s .C.s :
    $(CC) -S $(CFLAGS) $(CPPFLAGS) $< -o $@
 
 
 
#### Generating object files ####
# object from C
.c.o: 
    $(CC) $(CFLAGS) -c $<
 
 
# object from C++ (.cc, .cpp, .C files)
.cc.o .cpp.o .C.o :
    $(CC) $(CFLAGS) $(CPPFLAGS) -c $<
 
# object from asm
.S.o :
    $(CC) $(ASMFLAGS) -c $< -o $@
 
 
#### Generating hex files ####
# hex files from elf
#####  Generating a gdb initialisation file    #####
.out.hex:
    $(OBJCOPY) -j .text                    \
        -j .data                       \
        -O $(HEXFORMAT) $< $@
 
.out.ee.hex:
    $(OBJCOPY) -j .eeprom                  \
        --change-section-lma .eeprom=0 \
        -O $(HEXFORMAT) $< $@
 
 
#####  Generating a gdb initialisation file    #####
##### Use by launching simulavr and avr-gdb:   #####
#####   avr-gdb -x gdbinit-myproject           #####
gdbinit: $(GDBINITFILE)
 
$(GDBINITFILE): $(TRG)
    @echo "file $(TRG)" > $(GDBINITFILE)
 
    @echo "target remote localhost:1212" \
                        >> $(GDBINITFILE)
 
    @echo "load"        >> $(GDBINITFILE) 
    @echo "break main"  >> $(GDBINITFILE)
    @echo "continue"    >> $(GDBINITFILE)
    @echo
    @echo "Use 'avr-gdb -x $(GDBINITFILE)'"
 
 
#### Cleanup ####
clean:
    $(REMOVE) $(TRG) $(TRG).map $(DUMPTRG)
    $(REMOVE) $(OBJDEPS)
    $(REMOVE) $(LST) $(GDBINITFILE)
    $(REMOVE) $(GENASMFILES)
    $(REMOVE) $(HEXTRG)
 
 
 
#####                    EOF                   #####

After you've created this Makefile in the same directory as your blinking_led_2313.c file, open a CMD window and go to that directory where the makefile is located. Then type make (but be sure you've closed the Arduino-IDE before doing so!!).

Upload the code to the chip

In the same CMD window type: make install and you should see a blinking LED in a couple of seconds.

Some helpfull links

http://dddanmar.net/
blinking led

Extra

MCU names for avr-gcc

avr2
at90s2313
at90s2323
at90s2333
at90s2343
attiny22
attiny26
at90s4414
at90s4433
at90s4434
at90s8515
at90c8534
at90s8535
at86rf401
attiny13
attiny2313
avr3
atmega103
atmega603
at43usb320
at43usb355
at76c711
avr4
atmega8
atmega48
atmega88
atmega8515
atmega8535
avr5
atmega16
atmega161
atmega162
atmega163
atmega165
atmega168
atmega169
atmega32
atmega323
atmega325
atmega3250
atmega64
atmega645
atmega6450
atmega128
at90can128
at94k
avr1
at90s1200
attiny11
attiny12
attiny15
attiny28