Author Options:

How do you use makefiles with avrdude? Answered

I am new to avr and want to know if you can use avrdude with makefiles?


The forums are retiring in 2021 and are now closed for new topics and comments.

12 years ago

Yes. A good Makefile not only compiles the C code, it includes an option for transferring the hex file into the target chip (avrdude is usedfor the transfer only.)

Once the Makefile is written, it's invoked by entering "make" on the cmd line (shell.)

The make utility is quite complex. It's job is to decide which tasks (external programs) to execute, and which can be ignored. The Makefile is the configuration file for make.

So make is smart--it knows which source files you've changed since the last make session, and doesn't recompile things that are unchanged. This can save a huge amount of time when working on a large project.

But there's several things to know before creating a Makefile for AVR programming:

-- MCU (chip type: attiny13, atmega32, etc., etc.)
-- source file names (.c, .h)
-- output file name (project)
-- type of programmer hardware (avrdude won't work correctly otherwise)
-- optimization level

Here's a sample Makefile:

# Minimal Makefile for avr-gccPROJECT=myprogSOURCES=myprog.cCC=avr-gccOBJCOPY=avr-objcopyMMCU=attiny2313OPT=s CFLAGS=-mmcu=$(MMCU) -Wall -O$(OPT) $(PROJECT).hex: $(PROJECT).out  $(OBJCOPY) -j .text -O ihex $(PROJECT).out $(PROJECT).hex $(PROJECT).out: $(SOURCES)  $(CC) $(CFLAGS) -I./ -o $(PROJECT).out $(SOURCES) program: $(PROJECT).hex  avrdude -p t2313 -c dapa -e -U flash:w:$(PROJECT).hexclean:  rm -f $(PROJECT).out  rm -f $(PROJECT).hex

Enter "make" to build the C code.
Enter "make program" to upload hex file with avrdude.

It's been "filled out" with the following information:
-- MCU type = attiny2313
-- source file name = myprog.c
-- output file name = myprog
-- programmer hardware = dapa
--optimization = s (optimized for size)

You MUST customize this information for your setup, or it will not work. There is no one-size-fits-all Makefile.

It's a very minimal Makefile. It has only one dependency--the source file name. There are no include files, nor library files. I'm not guaranteeing it works, since I haven't tested it (it's simpler than the Makefiles I normally use...)

(Oh, and "Makefile" always starts with an uppercase 'M', or "make" cannot find it.)

Answer 12 years ago

Oh, I forgot. Enter "make clean" to delete the compiled output files (.out, .hex), as this is normal practice when distributing a source code project.