#modprobe ppdev!!!
#on fox: uisp --erase --upload  --verify if=bohdalec.hex -dprog=dapafox -v=3 --hash=12
#do not do - crashes fox: termostat from fox: uisp --erase --upload  --verify if=termostat.hex -dprog=dasa  -dlpt=/dev/ttyUSB0 -v=3 --hash=12
#for 2313 use "x" version which specifies -dpart=$(MCU) and erase in one command


CC=avr-gcc                                                    
OBJCOPY=avr-objcopy                                           
OBJDUMP=avr-objdump

PROGPORT=/dev/parport0
PROGMETHOD=dapa
#PROGPORT=/dev/usb/lp0 nefunguje s PL2305

#PROGMETHOD=dapafox

#must be FTDI ttyusb, not PL2303
#PROGMETHOD=dasa
#PROGMETHOD=dasaneg
#PROGPORT=/dev/ttyUSB1
#PROGPORT=/dev/ttyS 1-3 nefunguje
#!!!nedavat do USB3 portu na novem laptopu - extremne pomale

#cf. /usr/local/etc/avrdude.conf
#this is much faster than going via kernel ttyUSB driver since it uses MPSSE access via libftdi
#old device, works only with rev1063 of avrdude
DUDEMETHOD=ft232r_dasaneg
#new device DOES NOT WORK with any avrdude I tried - at least with atmega128 it did not
#DUDEMETHOD=ft2232h_mpsse

#DEBUG=-g
OPTIMIZE= -D AVR_SOURCE -O4 -Os -fno-inline -mcall-prologues
############################
PROJECT=termometer
#PROJECT_AUX =  usblib/usbdrvasm.o
#for floating point (needed in e.g. wattmeter, pyrometer, meteo2, termometer_chata,svetlogaraz)
#LIB=-lm -Wl,-u,vfprintf -Wl,-u,vfscanf  -lprintf_flt -lscanf_flt
###############specify hardware
#XTAL=3686400L
#XTAL=7372800L
#XTAL=8000000L
#XTAL=9216000L
#XTAL=12500000L
XTAL=14745600L
#XTAL=16000000L
#XTAL=16500000L
#XTAL=16666666L
#XTAL=20000000L
#XTAL=24000000L
#MCU=at90s8535
#MCU=at90s2313
MCU=atmega8
#MCU=atmega168
#MCU=atmega16
#MCU=atmega162
#MCU=atmega32
#MCU=atmega328p
#MCU=atmega644
#MCU=atmega128
#MCU=attiny26
#MCU=attiny2313
#MCU=attiny13
#MCU=attiny25
#MCU=attiny45
#MCU=attiny85
#MCU=attiny861
#not supported by the C compiler: io1200.h iotn11.h iotn12.h iotn15.h iotn28.h
##########################
#path for linker scripts
#LDFLAGS = -L /usr/lib/binutils/avr/2.20 ... did not work
#ln -s /usr/lib/binutils/avr/2.22/ldscripts/ /usr/avr/lib WORKED

# optimize for size:                                          
CFLAGS= -mmcu=$(MCU) -DXTAL=$(XTAL) -D$(MCU) -DMCU=$(MCU) -Wstrict-prototypes   $(OPTIMIZE) $(DEBUG) -fomit-frame-pointer $(LDFLAGS)
#-------------------                                          
#specific projects from more than one source files
#-------------------                                          
program:  $(PROJECT).hex $(PROJECT).ee.hex

$(PROJECT).hex : $(PROJECT).out                               
	$(OBJCOPY) -R .eeprom -O ihex $(PROJECT).out  $(PROJECT).hex  

$(PROJECT).asm : $(PROJECT).out
	$(OBJDUMP) -d  $(PROJECT).out > $(PROJECT).asm

$(PROJECT).ee.hex : $(PROJECT).out
	$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0  -O ihex $(PROJECT).out  $(PROJECT).ee.hex

$(PROJECT).out : $(PROJECT).o  $(PROJECT_AUX)                           
	$(CC) $(CFLAGS) -o $(PROJECT).out -Wl,-Map,$(PROJECT).map $(PROJECT).o $(PROJECT_AUX) $(LIB)                                
#divny bug na lpn:
#	ln -s /usr/avr/lib/avr4/crt* .
#        ln -s /usr/avr/lib/avr5/crt* .


.c.o:
%.o : %.c
	$(CC) $(CFLAGS) -c $< -o $*.o                       

.S.o:
%.o : %.S
	$(CC) $(CFLAGS) -c $<  -o $*.o                

.s.o:
%.o : %.s
	$(CC) $(CFLAGS) -c $<  -o $*.o         

.c.s:
%.s : %.c
	$(CC) $(CFLAGS) -S $< -o $*.s

.c.ch:
%.ch : %.c
	$(CC) $(CFLAGS) -E $< -o $*.ch



# you need to erase first before loading the program.         
# load (program) the software into the flash:                


erase:
	uisp -dlpt=$(PROGPORT) --erase -dprog=$(PROGMETHOD) -v=3 

eerase:
	uisp -dlpt=$(PROGPORT) --segment=eeprom --erase -dprog=$(PROGMETHOD)  -v=3

dudeupload: $(PROJECT).hex
	avrdude -p $(MCU) -c $(DUDEMETHOD) -B 100 -v -e -U flash:w:$(PROJECT).hex:i
#avrdude -p atmega644 -c ft232r_dasaneg -v -e -U efuse:w:05:m`
#-B 100 can be omitted for the older ft232d programmer, with 2232 verification otherwise fails

dudeeeupload: $(PROJECT).ee.hex
	avrdude -p $(MCU) -c $(DUDEMETHOD) -B 100 -v -U eeprom:w:$(PROJECT).ee.hex:i

dudeupload2: dudeupload dudeeeupload

dudedownload:
	avrdude -p $(MCU) -c $(DUDEMETHOD) -B 100 -v -U flash:r:$(PROJECT).hex:i

dudeeedownload:
	avrdude -p $(MCU) -c $(DUDEMETHOD) -B 100 -v -U eeprom:r:$(PROJECT).hex:i

upload2: erase eeupload upload0

upload: erase upload0

upload0: $(PROJECT).hex                                          
	uisp -dlpt=$(PROGPORT) --upload  --verify if=$(PROJECT).hex -dprog=$(PROGMETHOD)   -v=3 --hash=12   

uploadx: $(PROJECT).hex                                          
	uisp -dlpt=$(PROGPORT) --erase --upload --verify -dpart=$(MCU)  if=$(PROJECT).hex -dprog=$(PROGMETHOD)   -v=3 --hash=12      

eeupload: $(PROJECT).ee.hex
	uisp -dlpt=$(PROGPORT) --segment=eeprom --upload  --verify if=$(PROJECT).ee.hex -dprog=$(PROGMETHOD)   -v=3 --hash=12

eeuploadx: $(PROJECT).ee.hex
	uisp -dlpt=$(PROGPORT) --segment=eeprom --upload --verify  -dpart=$(MCU)  if=$(PROJECT).ee.hex -dprog=$(PROGMETHOD)   -v=3 --hash=12

download:
	uisp -dlpt=$(PROGPORT)   -dprog=$(PROGMETHOD)  -dpart=$(MCU)  -v=3  --download

eedownload:
	uisp -dlpt=$(PROGPORT)   -dprog=$(PROGMETHOD)  -dpart=$(MCU)  -v=3 --segment=eeprom  --download of=$(PROJECT).ee.hex


#prepare for external oscillator, high freq, crystal

fuse_attiny2313:
	uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_l=0xff
	
dude_fuse_attiny2313:
	avrdude -c $(PROGMETHOD) -p $(MCU) -U lfuse:w:0xff:m

dude_fuse_atmega:
	avrdude -p $(MCU) -c $(DUDEMETHOD) -v -e -U lfuse:w:0x2F:m
	avrdude -p $(MCU) -c $(DUDEMETHOD) -v -e -U hfuse:w:0xc9:m
#	avrdude -p $(MCU) -c $(DUDEMETHOD) -v -e -U efuse:w:0x05:m

dude_fuse_atmega_extclock:
	avrdude -p $(MCU) -c $(DUDEMETHOD) -v -e -U lfuse:w:0x20:m
	avrdude -p $(MCU) -c $(DUDEMETHOD) -v -e -U hfuse:w:0xd9:m
#	avrdude -p $(MCU) -c $(DUDEMETHOD) -v -e -U efuse:w:0x05:m

fuse_atmega:
	 uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_l=0x2f
	 uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_h=0xc9

fuse_atmega128:
	 uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4  --wr_fuse_l=0x2f
	 uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4  --wr_fuse_h=0xc9
	 uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4  --wr_fuse_e=0xff

fuse_atmega128_recover:
	 echo connect 100kHz rectangle 0-5V from signal generator to XTAL1 
	 uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4  -dt_sck=100 --wr_fuse_l=0x2f
	 uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4  -dt_sck=100 --wr_fuse_h=0xc9
	 uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4  -dt_sck=100 --wr_fuse_e=0xff

fuse_atmega_weak_clock_drive:
	 uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_l=0x2f
	 uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_h=0xf9

fuse_atmega644:
	 uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -v=4 --wr_fuse_l=0xd7
	 uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -v=4 --wr_fuse_h=0xd9
	 uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -v=4 --wr_fuse_e=0x04

fuse_atmega_extclock:
	 uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_l=0x20
	 uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_h=0xf9

fuse_atmega_no_eerase:
	 uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_l=0x2f
	 uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_h=0xf1

fuse_atmega_no_brownout:
	 uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_l=0xef
	 uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_h=0xc9

fuse_atmega_sut11:
	 uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_l=0x3f
	 uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_h=0xf9

fuse_atmega_jtag:
	uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_l=0x2f
	uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_h=0xb9

fuse_atmega_nojtag:
	uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_l=0x2f
	uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_h=0xf9

fuse_atmega_lowvolt:
	uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_l=0xaf
	uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_h=0xf9

fuse_atmega168:
	uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_l=0xff
	uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_h=0xdf

fuse_atmega328p:
	uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -v=4 --wr_fuse_l=0xff
	uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -v=4 --wr_fuse_h=0xdf

fuse_attiny_pll16mhz:
	uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_h=0xf7
	uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_l=0x61

fuse_attiny_rc8mhz:
	uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_h=0xf7
	uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_l=0xe4

fuse_attiny_crystal:
	uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_h=0xf7
	uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --wr_fuse_l=0xff

read_fuse:
	uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -dpart=$(MCU) -v=4 --rd_fuses

reset:
	uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)   -v=4 --rd_fuses 2>/dev/null >/dev/null

check:
	uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  --rd_fuses

lock_atmega:
	uisp -dlpt=$(PROGPORT) -dprog=$(PROGMETHOD)  -v=4 --wr_lock=0xfc

clean:                                                        
	rm -f *.o *.map *.out *.hex *.s  *.ee.hex   *.ch      *.asm           
#-------------------   
.PHONY : upload2x eeuploadx program upload upload0 download eeupload eedownload erase upload2

#avrdude -p m16 -P /dev/ttyUSB0 -c dasaneg -v -U eeprom:r:fi.hex:i ... slow
#avrdude -p atmega16 -c ft232r_dasaneg -v -U flash:w:pscontrol.out:e ... fast
