# Makefile to build ARC sample code for GCC
#
# Larry Barello Dec, 2003
#
# Uses GCC tools (make, rm)
#
# Command line programmer, avrdude comes with WinAVR and the "bascom" programming cable
# comes with the ARC board.
#
	PROG	= avrdude.exe -c bascom
#
# H & L Fuse bytes select: 8mhz internal clock, 4.0v BOD enabled, JTAG disabled
#
	HFUSE	= 0xDF
	LFUSE	= 0x84
#
# Select your target CPU: atmega16
#
	GCCMCU	= atmega16
#
# Select one of the sample targets below
#
#	TARGET	= flashled
#	TARGET	= flashled_FSM
#	TARGET	= switch
#	TARGET  = ADCtoPWM
	TARGET  = minisumo      
#
# Enable StdIO support by removing comment mark.
# Enabling adds 2100 bytes to the ROM image

#	STDIO   = -DUSE_STDIO

#GCC Equates
	FORMAT	= coff-ext-avr
	GCCLIB	=
	GCCINC	= -I. -I$(AVR)/avr/inc
	LDFLAGS = -Wl,-lm,-Map=$(<:.o=.gcc),--cref,-v, -mmcu=$(GCCMCU)
	CFLAGS	= -Os -Wall -g -mmcu=$(GCCMCU) -fshort-enums $(STDIO)
	AFLAGS	= -c -mmcu=$(GCCMCU) -Wa,-gstabs -mmcu=$(GCCMCU) -I$(GCCINC)
	BIN     = avr-objcopy
	CC	= avr-gcc
	AS	= avr-gcc -x assembler-with-cpp
	RM	= rm -f
#
# Declare all source modules here
#
	SRC	= $(TARGET).c arclib_10.c
	ASRC	=

	GCCOBJ	= $(SRC:.c=.o) $(ASRC:.s=.o)

# GCC Rules

%o: %c
	$(CC) -c $< $(GCCINC) $(CFLAGS) -o $@

%s: %c
	$(CC) -S $< $(GCCINC) $(CFLAGS) -o $@

%o: %s
	$(AS) $(AFLAGS) $< -o $@

%elf: $(GCCOBJ)
	$(CC) $(GCCOBJ) $(GCCLIB) $(LDFLAGS) -o $@

%obj: %elf
	$(BIN) -O avrobj -R .eeprom $< $@

%cof: %elf
#	$(ELFCOF) loadelf $*elf mapfile $*map writecof $*cof
	avr-objcopy --debugging -O $(FORMAT) \
        --change-section-address .data-0x800000 \
        --change-section-address .bss-0x800000 \
        --change-section-address .noinit-0x800000 \
        $< $@

%hex: %elf
	$(BIN) -O ihex $< $@

%eep: %elf
	$(BIN) -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex $< $@

# Rules specific to my project (default to GCC)
# NB - first target is default when running make.

gcc:	$(TARGET).eep $(TARGET).hex $(TARGET).cof

all: 
	make TARGET=flashled
	make TARGET=flashled_FSM
	make TARGET=switch
	make TARGET=ADCtoPWM
	make TARGET=minisumo

clean:
	$(RM) *.o *.lst *.map *.cof *.gcc
	$(RM) *.rom *.eep *.hex
	$(RM) *.obj *.?_sym $(SRC:.c=.s)

# Command line programmer

burn.eep:	$(TARGET).eep
	$(PROG) -q -p $(GCCMCU) -U eeprom:w:$(TARGET).eep:i

burn.hex:	$(TARGET).hex
	$(PROG) -q -p $(GCCMCU) -U flash:w:$(TARGET).hex:i -U hfuse:w:$(HFUSE):m -U lfuse:w:$(LFUSE):m

burn:	burn.hex burn.eep

assembly: $(SRC:.c=.s)

flash_led.o:	makefile arc_10.h flash_led.c
arclib_10.o:	makefile arc_10.h arclib_10.c
minisumo.o:	makefile arc_10.h minisumo.c
flash_led_FSM.o:	makefile arc_10.h flash_led_FSM.c
ADCtoPWM.o:	makefile arc_10.h ADCtoPWM.c
