#  MCU = at90can128
#  MCU = atmega328p
   MCU = atmega328pb   
 	LDFLAGS = -Wl,-Map=$(TRG).map,--section-start=.text=0x00000,--cref
#	LDFLAGS = -Wl,-Map=$(TRG).map,--section-start=.text=0x1E000,--cref

	CC		= avr-gcc
	AS		= avr-gcc -x assembler-with-cpp
	RM		= rm -f
	RN		= mv
	BIN		= avr-objcopy
	NM		= avr-nm
	INCDIR	= .
	FORMAT = ihex
	SRC	= $(TRG).c
	CPFLAGS	= -gdwarf-2 -Os -Wall -Wstrict-prototypes -Wa,-adhlns=$(<:.c=.lst)
	ASFLAGS = -Wa,-adhlns=$(<:.c=.lst),-gstabs 

	OBJ	= $(ASRC:.s=.o) $(SRC:.c=.o)
	CPFLAGS += -mmcu=$(MCU)
  CPFLAGS += -std=gnu99
	ASFLAGS += -mmcu=$(MCU)
	LDFLAGS += -mmcu=$(MCU)

all:	$(TRG).hex $(TRG).sym

#compile: instructions to create assembler and/or object files from C source
%o : %c
	$(CC) -c $(CPFLAGS) -I$(INCDIR) $< -o $@

%s : %c
	$(CC) -S $(CPFLAGS) -I$(INCDIR) $< -o $@

#assemble: instructions to create object file from assembler files
%o : %s
	$(AS) $(ASFLAGS) -I$(INCDIR) $< -o $@


#link: instructions to create elf output file from object files
%elf: $(OBJ)
	$(CC) $(OBJ) $(LIB) $(LDFLAGS) -o $@

#create avrobj file from elf output file
%obj: %elf
	$(BIN) -O avrobj $< $@

#create bin (ihex, srec) file from elf output file
%hex: %elf
	$(BIN) -O $(FORMAT) -R .eeprom $< $@
#	$(BIN) -j .eeprom --set-section-flags=.eeprom="alloc,load" -O $(FORMAT) $< $(@:.hex=.eep)

# Create a symbol table from ELF output file.
%sym: %elf
	$(NM) -n $< > $@

$(TRG).o : $(TRG).c
