# ######################################################################
#    makefile for some assembly language example programs               #
#      for the LPC2378 ARM CPU, especially for the Olimex              #
#      LPC-P2378 development board.                                    #
#                                                                      #
#    You need to verify or edit the VARIABLES that depend on your      #
#    environment, then you would issue commands such as                #
#         make led1.bin                                                #
#         make led1.dl                                                 #
#         make led1.dls                                                #
#         make led1.jtag
#    to assemble the file led1.asm and then to download it with one or #
#    the other flash utility.                                          #
#                                                                      #
#    Note that targets such as led1.dl and led2.dls and led1.jtag      #
#    are fake targets that invoke one of the 3 downloaders.            #
#       for lpc21isp      use     make led1.dl                         #
#       for lpc_prog      use     make led1.dls                        #
#       for openOCD/JTAG  use    make led1.jtag                        #
#                                                                      #
#    If you use openOCD and a JTAG cable, watch for errors if you type #
#       make led1.jtag                                                 #
#    as you may need to give the command several times.  You can       #
#    always use openOCD directly by starting it in one terminal        #
#    and starting a telnet connection to it in another terminal then   #
#    typing the halt, flash erase, flash write, and resume commands    #
#    directly in the telnet terminal.  Using the makefile for this     #
#    (with the burn.sh script) is very convenient, though.             #
#                                                                      #
# ######################################################################

#  First, look over the section below named "VARIABLES that depend on
#  your environment" and edit them as appropriate.
#
#  To assemble and link one of the example programs, such as led1.asm,
#  type
#             make led1.bin

#  and then you can download it using the lpc_prog flash
#  utility, type
#             make led1.dls

#  or you can do both at once with
#             make led1.dls
#
#  Note that the lpc_prog flash utility (always) and the lpc21isp
#  flash utility (when given the -control option) will control the
#  serial port's DTR and RTS lines in an attempt reset the ARM 
#  while holding the bootloader request line low.  This works only
#  if your ARM board supports it.  For example, on the Olimex P2378
#  board, "make" the two jumpers ISP_E and RST_E if you wish this to
#  work.  On hardware that does not support this, you would need to
#  change jumpers and/or push reset buttons as appropriate in order
#  for the bootloading to work.
#  
# ######################################################################


# ######################################################################
#    Quick REFERENCE to the meaning of some automatic variables        #
# ######################################################################
#
#   Given a rule such as  %.s: %.asm  which says how to make
#   an assembly file suitable for passing to the GNU assembler 
#   (e.g. led1.s) from a preprocessable assembly source file 
#   (e.g. led1.asm),
#      $*  is the stem (e.g. led1)
#      $@  is the target file (e.g. led1.s)
#      $<  is the source file (e.g. led1.asm), i.e. the first prerequisite
#      $^  is the list of all the prerequisites
# ######################################################################


# ######################################################################
#    VARIABLES that depend on your environment                         #
# ######################################################################

# Set the following variables depending on the particular 
#   ARM chip, serial port, path to the assembler, etc. that
#   apply to your environment.
#       CPU    
#          the particular ARM chip on your development board,
#          for example, lpc2106 or lpc2378.  This is needed 
#          by the lpc_prog flash utility.  You can change this in
#          this makefile or you can call make from the command line
#          with the cpu, e.g.
#               make CPU=lpc2106 ...
#
#       CCKL
#          the CPU clock speed of your board in KHz.  This is used by
#          both the lpc_prog and the lpc21isp flash utilities.  Note 
#          that this is the external crystal speed when using the
#          LPC2106 but is always 14748 when using the LPC2378.
#
#       PORT
#          the full path of your PC's serial port, such as /dev/ttyS0
#          or COM1: for the first serial port.
#
#       TERMBAUD
#          the serial rate you wish lpc_prog's built-in mini terminal 
#          to use after downloading a program.  This has an effect
#          only if lpc_prog is given the -t option.
#
#       DLBAUD
#          the serial rate you wish to use for downloading.  Start with
#          115200 but try slower speeds if you have trouble (e.g.
#          57600, 38400, 19200, 9600, 4800, etc.).
#
#       BIN
#          the full path to the directory that contains the ARM assembler
#          (arm-elf-as) and other binutils utilities such as arm-elf-objdump.
#
#       PREASM 
#          the full (or relative) path to the preprocessor that
#          converts *.asm files into *.s files.  Normally the
#          executable file is preasm.lisp but you could write a
#          replacement in Python or sed or whatever if you prefer.
#          Its main purpose at the moment is to replace semicolons (my
#          preferred comment character) with at-signs (the GNU ARM
#          assembler's preferred comment character).
#
# ######################################################################

#CPU = lpc2106
CPU = lpc2378

# lpc2378 uses an internally generated clock of 14.748 MHz for bootloading
#  regardless of the crystal on the board.
CCLK = 14748

# lpc2106 on the Olimex board uses a 14.7456 MHz clock
#CCLK = 14746

PORT = /dev/ttyS1
TERMBAUD = 4800
DLBAUD = 115200

#BIN = /usr/local/arm-uclinux-tools2/bin
BIN = /usr/local/arm/bin

PREASM = ./preasm.lisp


# ######################################################################
#    other VARIABLES                                                   #
# ######################################################################
#
#       EQUATES
#          a list of the include files.  If you create additional include
#          files (such as equates for a different ARM chip), then add them
#          to this variable.
#
#       ASMFLAGS
#          flags passed to the assembler.
#
#       LNKFLAGS
#          flags passed to the linker.
#
#       DEMOFILES
#          a list of the files to be zip'd when you say 'make zip'
#
#       BINARIES
#          a list of the binary files to be zip'd when you say 'make zip'
#
# ######################################################################

EQUATES = equates-lpc23xx.s olimex-lpc2378-equates.s

ASMFLAGS = -mcpu=arm7tdmi -ahls -mapcs-32 -gstabs

LNKFLAGS =  -v -T lpc2xxx.ld -nostartfiles

DEMOFILES = readme makefile preasm.lisp burn.sh \
            lpc2378_wig.cfg .gdbinit lpc2xxx.ld \
            led1.asm led2.asm led3.asm led4.asm \
            ser1.asm ser2.asm ser3.asm

BINARIES = led1.bin led2.bin led3.bin led4.bin \
            ser1.bin ser2.bin ser3.bin

# ######################################################################
#                         end of VARIABLES
# ######################################################################


#  Following line prevents make from deleting these intermediate files
.PRECIOUS: %.o %.hex %.bin %.srec %.elf %.s

all: led1.bin 

clean:
	@ echo "...cleaning"
	rm -f *.o *.elf *.hex led*.s *.bin *.lst *.lnkh *.lnkt

zip: $(DEMOFILES)
	zip asm-demos-$(shell echo `date +%Y%m%d-%H%M`).zip $(DEMOFILES) $(BINARIES)

# e.g. 'make led1.o' to assemble led1.s into an object file
%.o: %.s $(EQUATES)
	$(BIN)/arm-elf-as -mcpu=arm7tdmi -mapcs-32 -gstabs -ahls=$*.lst  -o $@ $*.s


# e.g. 'make led1.dis' to produce a disassembly listing from led1.elf
%.dis: %.elf
	#$(BIN)/arm-elf-objdump  -d  $<  > $@
	$(BIN)/arm-elf-objdump  -d --source $<  > $@


# e.g. 'make led1.s' to preprocess led1.asm into led1.s
%.s: %.asm
	$(PREASM) $*.asm $@ 

%.hex: %.bin
	$(BIN)/arm-elf-objcopy --input-target binary  --output-target ihex  $<  $*.hex

%.srec: %.bin
	$(BIN)/arm-elf-objcopy --input-target binary  --output-target srec  $<  $*.srec

%.bin: %.elf
	$(BIN)/arm-elf-objcopy -O binary $<  $*.bin

%.elf: %.o
	@ echo "...linking $@"
	$(BIN)/arm-elf-ld $(LNKFLAGS) -o $@ $<

# e.g. 'make led1.dl' to use the lpc21isp flash utility to download led1.bin
%.dl: %.bin
	lpc21isp  -control -bin $*.bin  $(PORT) $(DLBAUD) $(CCLK)

# e.g. 'make led1.dls' to use the lpc_prog flash utility to download led1.srec
%.dls: %.srec
	lpc_prog -wvrt --device $(PORT) --part $(CPU) --khz $(CCLK) --baud $(DLBAUD) --terminal-baud $(TERMBAUD) $*.srec

# e.g. 'make led1.jtag' to use the openocd JTAG cable to download led1.bin
%.jtag: %.bin
	./burn.sh $*.bin

