### Modified to work for the ZB1

### The primary change is the AVR_FREQ constant which needs to
### be 12000000L (12MHz). In the CEE program the LED pin is
### changed to pin 7 (ATmega pin PD7)

### To compile the bootloader       type "Make"
### To burn the bootloader          type "Make isp"
### To test the usb-tiny for a '168 type "make isp-test-168"
### To test the usb-tiny for a '328 type "make isp-test-328"

# 30-Apr-2008 (* jcl *)

# Makefile for ATmegaBOOT
# E.Lins, 18.7.2005
# $Id$

# bootloader variables

# PROGRAM    is the base name of the C source file
# AVR_FREQ   XTAL frequency in Hz (12000000 for the dorkon-i)
# MCU_TARGET ATmega168
# TARGET     Bootloader Version      
# LDSECTION  

PROGRAM    = ATmegaBOOT_xx8
AVR_FREQ   = 12000000L
MCU_TARGET = atmega328p
TARGET     = zb1
LDSECTION  = 0x7800

# compiler and binutils variables and flags

AVR_DIR = /local/pub/avr-20081230/
BIN_DIR = $(AVR_DIR)bin/
CC      = $(BIN_DIR)avr-gcc
OBJCOPY = $(BIN_DIR)avr-objcopy
OBJDUMP = $(BIN_DIR)avr-objdump

CFLAGS  = -g -Wall -O2 -mmcu=$(MCU_TARGET) -DF_CPU=$(AVR_FREQ)

# -Wl pass option to linker
#    
#    -Map create map file 
#    --cref add cross reference

LDFLAGS = -Wl,--section-start=.text=$(LDSECTION) -Wl,-Map=$(PROGRAM)_$(TARGET).map,--cref
CFLAGS += '-DMAX_TIME_COUNT=F_CPU>>2' '-DNUM_LED_FLASHES=3' '-D__ZB1__'

# ISP parameters

# This is setup for the ladyada USBtiny.  With JP3 removed the USBtiny
# output buffer (IC2) is powered by the Vcc from the dorkon-i (3.3V).
# IC2 is a 74AHC125 quad buffer which will tolerate 5V levels when
# powered from a 3.3V supply. (* jcl *)

# the efuse should really be 0xf8; since, however, only the lower
# three bits of that byte are used on the atmega168, avrdude gets
# confused if you specify 1's for the higher bits, see:
# http://tinker.it/now/2007/02/24
# /the-tale-of-avrdude-atmega168-and-extended-bits-fuses/
#
# similarly, the lock bits should be 0xff instead of 0x3f (to
# unlock the bootloader section) and 0xcf instead of 0x0f (to
# lock it), but since the high two bits of the lock byte are
# unused, avrdude would get confused.

# avrdude options

#   -C <config_filename>
#   -c usbtiny
#   -p <partno>    part number specified as "id" in the config file
#   -P <port>      com port name which is USB for the usbtiny
#   -b <baudrate>  
#   -B <bitclock>  1uS for usbtiny

ISP      = $(BIN_DIR)avrdude
ISPCONF  = $(AVR_DIR)etc/avrdude-ladyada.conf
ISPTOOL  = usbtiny
ISPPORT  = usb
ISPSPEED = 28800
ISPOPT   = -c $(ISPTOOL) -C $(ISPCONF) -p m328p -P $(ISPPORT) -b $(ISPSPEED) -B 1

# The fuse settings are from the ladyada isp328 make target

HFUSE=0xDA
LFUSE=0xFF
ISPFUSES = -e -u 
ISPFUSES+= -U lock:w:0x3f:m  -U efuse:w:0x05:m 
ISPFUSES+= -U hfuse:w:$(HFUSE):m -U lfuse:w:$(LFUSE):m
ISPFLASH = -U flash:w:$(PROGRAM)_$(TARGET).hex -U lock:w:0x0f:m

hexs=$(PROGRAM)_$(TARGET).hex

all: $(hexs)

isp-test-168:
	$(ISP) -C $(ISPCONF) -c usbtiny -p m168 -b 115200

isp-test-328:
	$(ISP) -C $(ISPCONF) -c usbtiny -p m328p -b 115200

isp: $(PROGRAM)_$(TARGET).hex
	$(ISP) $(ISPOPT) $(ISPFUSES)
	$(ISP) $(ISPOPT) $(ISPFLASH)

%_$(TARGET).o: %.c
	$(CC) $(CFLAGS) $(LDFLAGS) -o $* $^ 

%_$(TARGET).elf: %.o
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $*.o 

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

clean:
	rm -rf $(PROGRAM)_$(TARGET).map
	rm -rf $(hexs)


