# makefile, written by guido socher
MCU=atmega8
CC=avr-gcc
OBJCOPY=avr-objcopy
# optimize for size:
CFLAGS=-g -mmcu=$(MCU) -Wall -Wstrict-prototypes -Os -mcall-prologues
#-------------------
all: avrm8ledtest.hex
#-------------------
help: 
	@echo "Usage: make [all]|load|load_pre|rdfuses|wrfuse1mhz|wrfuse4mhz|wrfuse8mhz|wrfusecrystal"
	@echo "Warning: you will not be able to undo wrfusecrystal unless you connect an"
	@echo "         external crystal! uC is dead after wrfusecrystal if you do not"
	@echo "         have an external crystal."
#-------------------
avrm8ledtest.hex : avrm8ledtest.out 
	$(OBJCOPY) -R .eeprom -O ihex avrm8ledtest.out avrm8ledtest.hex 
avrm8ledtest.out : avrm8ledtest.o 
	$(CC) $(CFLAGS) -o avrm8ledtest.out -Wl,-Map,avrm8ledtest.map avrm8ledtest.o 
avrm8ledtest.o : avrm8ledtest.c 
	$(CC) $(CFLAGS) -Os -c avrm8ledtest.c
# you need to erase first before loading the program.
# load (program) the software into the eeprom:
load: avrm8ledtest.hex
	uisp -dlpt=/dev/parport0 --erase  -dprog=dapa
	uisp -dlpt=/dev/parport0 --upload if=avrm8ledtest.hex -dprog=dapa  -v=3 --hash=32
# here is a pre-compiled version in case you have trouble with
# your development environment
load_pre: avrm8ledtest_pre.hex
	uisp -dlpt=/dev/parport0 --erase  -dprog=dapa
	uisp -dlpt=/dev/parport0 --upload if=avrm8ledtest_pre.hex -dprog=dapa -dno-poll -v=3 --hash=32
#-------------------
# fuse byte settings:
#  Atmel AVR ATmega8 
#  Fuse Low Byte      = 0xe1 (1MHz internal), 0xe3 (4MHz internal), 0xe4 (8MHz internal)
#  Fuse High Byte     = 0xd9 
#  Factory default is 0xe1 for low byte and 0xd9 for high byte
# Check this with make rdfuses
rdfuses:
	uisp -dlpt=/dev/parport0 -dprog=dapa --rd_fuses
# use internal RC oscillator 1 Mhz
wrfuse1mhz:
	uisp -dlpt=/dev/parport0 -dprog=dapa --wr_fuse_l=0xe1
# use internal RC oscillator 4 Mhz
wrfuse4mhz:
	uisp -dlpt=/dev/parport0 -dprog=dapa --wr_fuse_l=0xe3
# use internal RC oscillator 8 Mhz
wrfuse8mhz:
	uisp -dlpt=/dev/parport0 -dprog=dapa --wr_fuse_l=0xe4
# use external 3-8 Mhz crystal
# Warning: you can not reset this to intenal unless you connect a crystal!!
wrfusecrystal:
	uisp -dlpt=/dev/parport0 -dprog=dapa --wr_fuse_l=0xee
#-------------------
clean:
	rm -f *.o *.map *.out *t.hex
#-------------------