# ***************************************************************
# *     Makefile for Atmel AT91SAM7X256 - flash execution       *
# *                                                             *
# *                                                             *
# * James P Lynch  September 3, 2006                            *
#  sligthly changed by NSn   200700308                          *
# ***************************************************************

NAME   = TryOneMoreTime

# variables 
##BINDIRS = E:/Program/yagarto/bin/
CC      = arm-elf-gcc
LD      = arm-elf-ld -v
AR      = arm-elf-ar
AS      = arm-elf-as
CP      = arm-elf-objcopy
OD			= arm-elf-objdump

#EXTRA_LIBDIRS = E:/Program/yagarto/arm-elf/lib/
EXTRA_LIBDIRS = E:/Program/yagarto/lib/gcc/arm-elf/4.1.1/

CFLAGS  = -I./ -c -fno-common -O0 -g
AFLAGS  = -ahls -mapcs-32 -o crt.o
LFLAGS  =  -Map main.map -Tlinkconfig_RAM.cmd
CPFLAGS = --output-target=binary
ODFLAGS	= -x --syms

OBJECTS = crt.o	main.o timerisr.o timersetup.o isrsupport.o lowlevelinit.o blinker.o TWI_ctrl.o\
commands.o display.o HandleDDS.o menu.o buttons.o SPI_ctrl.o eepromhandler.o a2dmodule.o Smeter.o


# make target called by Eclipse (Project -> Clean ...)
clean:
	-rm $(OBJECTS) crt.lst main.lst main.out main.bin main.hex main.map main.dmp

         
#make target called by Eclipse  (Project -> Build Project)
#all: begin gccversion sizebefore build sizeafter finished end

all:  main.out
	@ echo "...copying"
	$(CP) $(CPFLAGS) main.out main.bin
	$(OD) $(ODFLAGS) main.out > main.dmp

main.out: $(OBJECTS) ./linkconfig_RAM.cmd
	@ echo "..linking"
#	$(LD) $(LFLAGS) -o main.out $(OBJECTS) -L%,$(EXTRA_LIBDIRS) -lgcc -lc
	$(LD) $(LFLAGS) -o main.out $(OBJECTS) -L%,$(EXTRA_LIBDIRS)libgcc.a -lc

crt.o: crt.s
	@ echo ".assembling crt.s"
	$(AS) $(AFLAGS) crt.s > crt.lst

main.o: main.c  AT91SAM7X256.h board.h
	@ echo ".compiling main.c"
	$(CC) $(CFLAGS) main.c
	
timerisr.o: timerisr.c  AT91SAM7X256.h board.h
	@ echo ".compiling timerisr.c"
	$(CC) $(CFLAGS) timerisr.c
	
lowlevelinit.o: lowlevelinit.c AT91SAM7X256.h board.h
	@ echo ".compiling lowlevelinit.c"
	$(CC) $(CFLAGS) lowlevelinit.c
	
timersetup.o: timersetup.c AT91SAM7X256.h board.h
	@ echo ".compiling timersetup.c"
	$(CC) $(CFLAGS) timersetup.c
	
isrsupport.o: isrsupport.c
	@ echo ".compiling isrsupport.c"
	$(CC) $(CFLAGS) isrsupport.c

blinker.o: blinker.c AT91SAM7X256.h board.h
	@ echo ".compiling blinker.c"
	$(CC) $(CFLAGS) blinker.c
	
commands.o: commands.c  AT91SAM7X256.h board.h
	@ echo ".compiling commands.c"
	$(CC) $(CFLAGS) commands.c
	
display.o: display.c  AT91SAM7X256.h board.h
	@ echo ".compiling display.c"
	$(CC) $(CFLAGS) display.c
	
HandleDDS.o: HandleDDS.c  AT91SAM7X256.h board.h
	@ echo ".compiling HandleDDS.c"
	$(CC) $(CFLAGS) HandleDDS.c
	
menu.o: menu.c  AT91SAM7X256.h board.h
	@ echo ".compiling menu.c"
	$(CC) $(CFLAGS) menu.c
	
buttons.o: buttons.c  AT91SAM7X256.h board.h
	@ echo ".compiling buttons.c"
	$(CC) $(CFLAGS) buttons.c
	
eepromhandler.o: eepromhandler.c  AT91SAM7X256.h board.h
	@ echo ".compiling eepromhandler.c"
	$(CC) $(CFLAGS) eepromhandler.c
	
Volume.o: Volume.c  AT91SAM7X256.h board.h
	@ echo ".compiling Volume.c"
	$(CC) $(CFLAGS) Volume.c
	
SPI_ctrl.o: SPI_ctrl.c  AT91SAM7X256.h board.h SPI_ctrl.h
	@ echo ".compiling SPI_ctrl.c"
	$(CC) $(CFLAGS) SPI_ctrl.c
	
TWI_ctrl.o: TWI_ctrl.c  AT91SAM7X256.h board.h TWI_ctrl.h
	@ echo ".compiling TWI_ctrl.c"
	$(CC) $(CFLAGS) TWI_ctrl.c
	
a2dmodule.o: a2dmodule.c  AT91SAM7X256.h board.h a2dmodule.h
	@ echo ".compiling a2dmodule.c"
	$(CC) $(CFLAGS) a2dmodule.c
	
Smeter.o: Smeter.c  AT91SAM7X256.h board.h Smeter.h
	@ echo ".compiling Smeter.c"
	$(CC) $(CFLAGS) Smeter.c
	
	
# **********************************************************************************************
#                            FLASH PROGRAMMING      (using OpenOCD and Amontec JTAGKey)
#
# Alternate make target for flash programming only
#
# You must create a special Eclipse make target (program) to run this part of the makefile 
# (Project -> Create Make Target...  then set the Target Name and Make Target to "program")
#
# OpenOCD is run in "batch" mode with a special configuration file and a script file containing
# the flash commands. When flash programming completes, OpenOCD terminates.
#
# Note that the make file below creates the script file of flash commands "on the fly"
#
# Programmers: Martin Thomas, Joseph M Dupre, James P Lynch
# **********************************************************************************************

# specify output filename here (must be *.bin file)
TARGET = main.bin

# specify the directory where openocd executable resides (openocd-ftd2xx.exe or openocd-pp.exe)
OPENOCD_DIR = E:/Program/openocd-2007re128/bin/

# specify OpenOCD executable (pp is for the wiggler, ftd2xx is for the USB debugger)
#OPENOCD = $(OPENOCD_DIR)openocd-pp.exe
OPENOCD = $(OPENOCD_DIR)openocd-ftd2xx.exe

# specify OpenOCD configuration file (pick the one for your device)
#OPENOCD_CFG = $(OPENOCD_DIR)AT91SAM7X256-wiggler-flash-program.cfg
#OPENOCD_CFG = $(OPENOCD_DIR)AT91SAM7X256-jtagkey-flash-program.cfg
OPENOCD_CFG = $(OPENOCD_DIR)at91sam7X256-armusbocd-flash-program.cfg

# specify the name and folder of the flash programming script file
OPENOCD_SCRIPT = c:\TEMP\temp.ocd

# program the AT91SAM7X256 internal flash memory
programChip: main.bin
	-rm $(OPENOCD_SCRIPT)
	@echo "Preparing OpenOCD script..."
	@cmd /c 'echo wait_halt > $(OPENOCD_SCRIPT)'
	@cmd /c 'echo armv4_5 core_state arm >> $(OPENOCD_SCRIPT)'
	@cmd /c 'echo flash write 0 $(TARGET) 0x0 >> $(OPENOCD_SCRIPT)'
	@cmd /c 'echo mww 0xfffffd08 0xa5000401 >> $(OPENOCD_SCRIPT)'
	@cmd /c 'echo reset >> $(OPENOCD_SCRIPT)'
	@cmd /c 'echo shutdown >> $(OPENOCD_SCRIPT)'
	@echo "Flash Programming with OpenOCD..."
	$(OPENOCD) -f $(OPENOCD_CFG)
	@echo "Flash Programming Finished."
	
