# Name: Makefile
# Project: SPDM Control - Ferrari 308/512 Veglia Speedometer Replacement Board
# Copyright: (C) Adrian Le Hanne, info@dinoplex.org, www.dinoplex.org
# SPDX-License-Identifier: GPL-3.0-or-later
# Project v1.8

DEVICE      = attiny25
DUDEDEVICE  = ATTINY25
CLOCK       = 1000000
# -B 22 slows the ISP clock below F_CPU/4 (target runs at 1 MHz)
PROGRAMMER  = -c avrispmkII -P usb -B 22
OBJECTS     = main.o

# Usage:
#
# % make install
# this sets the correct fuses on the attiny25, then compiles the sources, creates a binary and flashes the binary
# 

# lfuse 0x62 = internal 8 MHz RC oscillator / 8 -> 1 MHz system clock
# (factory default; must match CLOCK above). hfuse/efuse = factory defaults.
FUSES = -U lfuse:w:0x62:m -U hfuse:w:0xdf:m -U efuse:w:0xff:m

AVRDUDE = avrdude $(PROGRAMMER) -p $(DUDEDEVICE)
COMPILE = avr-gcc -Wall -Os -DF_CPU=$(CLOCK) -mmcu=$(DEVICE)

# symbolic targets:
all:	main.hex

.c.o:
	$(COMPILE) -c $< -o $@

.c.s:
	$(COMPILE) -S $< -o $@

flash:	all
	$(AVRDUDE) -U flash:w:main.hex:i

fuse:
	$(AVRDUDE) $(FUSES)

install: fuse flash


clean:
	rm -f main.hex main.elf main.s $(OBJECTS)

main.elf: $(OBJECTS)
	$(COMPILE) -o main.elf $(OBJECTS)

main.hex: main.elf
	rm -f main.hex
	avr-objcopy -j .text -j .data -O ihex main.elf main.hex
	avr-size --format=avr --mcu=$(DEVICE) main.elf

disasm:	main.elf
	avr-objdump -d main.elf

cpp:
	$(COMPILE) -E main.c
