# Makefile for generating Intel HEX files for the AVR MCU using avr-gcc and binutils
# By	-	Nandan Banerjee
#				December 24, 2011
#
# Change the MCU_TARGET to the AVR microcontroller you wish to generate the HEX file for.
# Change the PRG to the name of the C program file which you want to build.
# Set the optimizations in the OPTIMIZE macro.
# Comment the FILES_GENERATED macro if you want to keep the files generated.

PRG 			= main
OBJ 			= $(PRG).o
MCU_TARGET		= atmega32
OPTIMIZE       	= -Os
CC				= avr-gcc
CFLAGS			= -g $(OPTIMIZE) -mmcu=$(MCU_TARGET) 
OBJCOPY        	= avr-objcopy
OBJDUMP        	= avr-objdump
FILES_GENERATED	= $(PRG).elf $(PRG).bin $(OBJ) $(PRG).lst $(PRG).srec

all: $(PRG).elf $(PRG).lst text clean

#compile: $(PRG).c
#	$(CC) $(CFLAGS) -c $<

$(PRG).elf: $(OBJ)
	$(CC) $(CFLAGS) -o $@ $^

$(OBJ): $(PRG).c
	$(CC) $(CFLAGS) -c $<

$(PRG).lst: $(PRG).elf
	$(OBJDUMP) -h -S $< > $@

text: hex bin srec

hex:  $(PRG).hex
bin:  $(PRG).bin
srec: $(PRG).srec

$(PRG).hex: $(PRG).elf
	$(OBJCOPY) -j .text -j .data -O ihex $< $@

$(PRG).srec: $(PRG).elf
	$(OBJCOPY) -j .text -j .data -O srec $< $@

$(PRG).bin: $(PRG).elf
	$(OBJCOPY) -j .text -j .data -O binary $< $@

clean:
	rm -rf $(FILES_GENERATED)
