#########  AVR Project Makefile Template   #########
######                                        ######
######    Copyright (C) 2003-2005,Pat Deegan, ######
######            Psychogenic Inc             ######
######          All Rights Reserved           ######
######                                        ######
###### You are free to use this code as part  ######
###### of your own applications provided      ######
###### you keep this copyright notice intact  ######
###### and acknowledge its authorship with    ######
###### the words:                             ######
######                                        ######
###### "Contains software by Pat Deegan of    ######
###### Psychogenic Inc (www.psychogenic.com)" ######
######                                        ######
###### If you use it as part of a web site    ######
###### please include a link to our site,     ######
###### http://electrons.psychogenic.com  or   ######
###### http://www.psychogenic.com             ######
######                                        ######
####################################################


##### 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
##### make clean
##### make listing  - added 10-11-05 wje generates disassembly of hex file
#####
##### 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=atmega16
MCU=atmega32

# id to use with programmer
# default: PROGRAMMER_MCU=$(MCU)
# In case the programer used, e.g avrdude, doesn't
# accept the same MCU name as avr-gcc (for example
# for ATmega8s, avr-gcc expects 'atmega8' and 
# avrdude requires 'm8')
#PROGRAMMER_MCU=m16
PROGRAMMER_MCU=m32

# Name of our project
# (use a single word, e.g. 'myproject')
PROJECTNAME=rotor

########## added VPATH 2-11-2014 - wje
VPATH=../CLEAR_VIEW/$(PROJECTNAME)

# 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=					\
	main.c				\
	uart_putchar.c		\
	uart_getchar.c		\
	init_rotor_ctl_io.c	\
	init_timer1.c		\
	timer_counter_int.c	\
	init_uart.c			\
	gpr.c				\
	calibrate.c			\
	initialize.c		\
	update_display.c	\
	manual.c			\
	rotor_position.c	\
	detect_rotor.c		\
	mode_priority.c		\
	rotor_bearing.c		\
	auto_track.c		\
	parse_rx_buffer.c	\
	novacomm1_parser.c	\
	Mac_Doppler_parser.c\
	rotor_pwr.c         \
	initialize_auto_mode.c \
	initialize_manual_mode.c \
	initialize_calibrate_mode.c \
	parse_tracking_data.c \
	lcd.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=0


#####      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=wje
#AVRDUDE_PROGRAMMERID=dt006
AVRDUDE_PROGRAMMERID=usbtiny

# port--serial or parallel port to which your 
# hardware programmer is attached
#
AVRDUDE_PORT=/dev/parport0


####################################################
#####                Config Done               #####
#####                                          #####
##### You shouldn't 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)  -B .1 -c $(AVRDUDE_PROGRAMMERID)   \
	 -p $(PROGRAMMER_MCU)  -e        \
	 -U flash:w:$(HEXROMTRG)

install: writeflash

$(DUMPTRG): $(TRG) 
	$(OBJDUMP) -S  $< > $@


$(TRG): $(OBJDEPS) 
	$(CC) $(LDFLAGS) -o $(TRG) $(OBJDEPS)

listing: $(PROJECTNAME.out)
	avr-objdump -S $(PROJECTNAME).out > $(PROJECTNAME).S

#### 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 $< -o $@


# object from C++ (.cc, .cpp, .C files)
.cc.o .cpp.o .C.o :
	$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@

# 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:
	@echo "Skipping eeprom Generation"
#   $(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                   #####
#### Clean this out and "avr-gcc -MM *.c >> Makefile" - to regenerate this
auto_track.o: auto_track.c sw_timers.h uart_getchar.h switches.h main.h \
 auto_track.h rotor.h
az_rotor_bearing.o: az_rotor_bearing.c rotor.h main.h
az_rotor_position.o: az_rotor_position.c gpr.h switches.h rotor.h main.h \
 sw_timers.h
calibrate.o: calibrate.c switches.h lcd.h rotor.h main.h sw_timers.h
detect_rotor.o: detect_rotor.c switches.h lcd.h rotor.h main.h \
 sw_timers.h
gpr.o: gpr.c gpr.h uart_putchar.h
initialize_auto_mode.o: initialize_auto_mode.c main.h auto_track.h \
 rotor.h uart_getchar.h
initialize.o: initialize.c lcd.h uart_getchar.h rotor.h main.h \
 sw_timers.h
initialize_calibrate_mode.o: initialize_calibrate_mode.c main.h \
 auto_track.h rotor.h uart_getchar.h
initialize_manual_mode.o: initialize_manual_mode.c main.h auto_track.h \
 rotor.h uart_getchar.h
init_timer1.o: init_timer1.c
init_uart.o: init_uart.c uart_putchar.h uart_getchar.h
lcd.o: lcd.c lcd.h
Mac_Doppler_parser.o: Mac_Doppler_parser.c uart_getchar.h switches.h \
 rotor.h main.h auto_track.h
manual.o: manual.c auto_track.h uart_getchar.h sw_timers.h switches.h \
 rotor.h main.h
mode_priority.o: mode_priority.c switches.h rotor.h main.h sw_timers.h \
 uart_getchar.h
novacomm1_parser.o: novacomm1_parser.c uart_getchar.h switches.h rotor.h \
 main.h auto_track.h
parse_rx_buffer.o: parse_rx_buffer.c uart_getchar.h switches.h rotor.h \
 main.h auto_track.h
parse_tracking_data.o: parse_tracking_data.c uart_getchar.h main.h
rotor_bearing.o: rotor_bearing.c rotor.h main.h
rotor_position.o: rotor_position.c gpr.h switches.h rotor.h main.h \
 sw_timers.h
rotor_pwr.o: rotor_pwr.c rotor.h main.h
uart_getchar.o: uart_getchar.c uart_getchar.h
uart_putchar.o: uart_putchar.c uart_putchar.h
update_display.o: update_display.c lcd.h auto_track.h rotor.h main.h
wip_calibrate.o: wip_calibrate.c switches.h lcd.h rotor.h main.h \
 sw_timers.h
