
# The name of the project
PRG            = test
# Files owned by the user
OBJUSER        = test.o 
# Generated files that are required
OBJREQD        = lib/lib_iopins.o lib/lib_timerdef.o lib/lib_hardware.o lib/gen_clock.o lib/gen_speech.o lib/lib_timers.o 
# Generated files that are optional
OBJOPT         = lib/opt_DeviceName.o lib/opt_pcint.o lib/opt_scheduler.o lib/opt_TimerCompare.o lib/opt_TimerCapture.o lib/opt_TimerOverflow.o 

# The processor target
MCU_TARGET     = atmega640

# The processor speed
MCU_SPEED	   = 16000000

# The library where WebbotLib is installed
WEBBOT		   = ../../design_tools/libirary/webbotlib_v2

# The library to link against
WEBBOT_LIB = $(WEBBOT)/libWebbot-ATmega640.a


OPTIMIZE       = -O0
	

DEFS           = -gdwarf-2 -fpack-struct -fshort-enums  -funsigned-char -funsigned-bitfields -I"$(WEBBOT)" 
DEFS 		  +=  -MD -MP -MT $(*F).o -MF dep/$(@F).d


LIBS           = -L"$(WEBBOT)" -L"lib" -lWebbot-ATmega640  -lPost -lm -lc
	

# Define the tools to be used
CC             = avr-gcc
CPP            = avr-g++
OBJCOPY        = avr-objcopy
OBJDUMP        = avr-objdump
SIZE		   = avr-size
ARCHIVE		   = avr-ar

# Override is only needed by avr-lib build system.
# -Wall enable most warning errors
override CFLAGS        = -g -Wall -DF_CPU=${MCU_SPEED} -mmcu=$(MCU_TARGET) $(DEFS)  -std=gnu99 
override CPPFLAGS      = -g -Wall -DF_CPU=${MCU_SPEED} -mmcu=$(MCU_TARGET) $(DEFS) -fno-threadsafe-statics


# Use the minimal version of vfprintf
override LDFLAGS       = -Wl,-Map,$(PRG).map,-u,vfprintf -lprintf_min
	

# Default build target
all: $(PRG).elf lst text eeprom size

# How to create the elf file
$(PRG).elf: lib/libPost.a  $(WEBBOT_LIB) $(OBJUSER) $(OBJREQD)
	$(CC) -mmcu=$(MCU_TARGET) $(LDFLAGS) -o $@ $(OBJUSER) $(OBJREQD) $(LIBS)


# Create an archive for the optional items so they are only added if called
lib/libPost.a: $(OBJOPT)
	$(ARCHIVE) -r $@ $(OBJOPT)


# dependencies for compiling C files to O files

	
lib/lib_iopins.o: lib/lib_iopins.c
		$(CC) $(CFLAGS) -Os -c -o $@ lib/lib_iopins.c
	
lib/lib_timerdef.o: lib/lib_timerdef.c
		$(CC) $(CFLAGS) -Os -c -o $@ lib/lib_timerdef.c
	
lib/lib_hardware.o: lib/lib_hardware.c
		$(CC) $(CFLAGS) -Os -c -o $@ lib/lib_hardware.c
	
test.o: test.c
		$(CC) $(CFLAGS) $(OPTIMIZE) -c -o $@ test.c
	
lib/gen_clock.o: lib/gen_clock.c
		$(CC) $(CFLAGS) -Os -c -o $@ lib/gen_clock.c
	
lib/opt_DeviceName.o: lib/opt_DeviceName.c
		$(CC) $(CFLAGS) -Os -c -o $@ lib/opt_DeviceName.c
	
lib/gen_speech.o: lib/gen_speech.c
		$(CC) $(CFLAGS) -Os -c -o $@ lib/gen_speech.c
	
lib/opt_pcint.o: lib/opt_pcint.c
		$(CC) $(CFLAGS) -Os -c -o $@ lib/opt_pcint.c
	
lib/opt_scheduler.o: lib/opt_scheduler.c
		$(CC) $(CFLAGS) -Os -c -o $@ lib/opt_scheduler.c
	
lib/lib_timers.o: lib/lib_timers.c
		$(CC) $(CFLAGS) -Os -c -o $@ lib/lib_timers.c
	
lib/opt_TimerCompare.o: lib/opt_TimerCompare.c
		$(CC) $(CFLAGS) -Os -c -o $@ lib/opt_TimerCompare.c
	
lib/opt_TimerCapture.o: lib/opt_TimerCapture.c
		$(CC) $(CFLAGS) -Os -c -o $@ lib/opt_TimerCapture.c
	
lib/opt_TimerOverflow.o: lib/opt_TimerOverflow.c
		$(CC) $(CFLAGS) -Os -c -o $@ lib/opt_TimerOverflow.c

# dependencies for compiling CPP files to O files


size: $(PRG).elf
	$(SIZE) --format=avr --mcu=${MCU_TARGET} $(PRG).elf	

# Clean the compiled stuff but leave any codegen'd files in place
clean:
	rm -rf *.o dep/* lib/*.o lib/*.a $(PRG).elf example.txt  
	rm -rf $(PRG).lst $(PRG).map $(EXTRA_CLEAN_FILES)

# Clean out everything - including any codegen'd files
cleanall:
	rm -rf *.o dep/* $(PRG).elf example.txt hardware.h  
	rm -r lib
	rm -rf $(PRG).lst $(PRG).map $(EXTRA_CLEAN_FILES)

lst:  $(PRG).lst

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

# Rules for building the .text rom images
text: hex bin srec

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

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

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

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

# Rules for building the .eeprom rom images
eeprom: ehex ebin esrec

ehex:  $(PRG)_eeprom.hex
ebin:  $(PRG)_eeprom.bin
esrec: $(PRG)_eeprom.srec

%_eeprom.hex: %.elf
	$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ \
	|| { echo empty $@ not generated; exit 0; }

%_eeprom.srec: %.elf
	$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O srec $< $@ \
	|| { echo empty $@ not generated; exit 0; }

%_eeprom.bin: %.elf
	$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O binary $< $@ \
	|| { echo empty $@ not generated; exit 0; }

EXTRA_CLEAN_FILES       = $(PRG).hex $(PRG)_eeprom.hex *.bin *.srec

## Other dependencies
-include $(shell mkdir dep 2>/dev/null) $(wildcard dep/*)

	