# The pusher firmware: bare-metal build for the ATmega328P (Arduino Nano), upload, and the host tests.
#   make            build bare/pusher.hex and print its size
#   make test       run the logic tests on this computer with gcc
#   make flash PORT=/dev/ttyUSB0            Nano with the new bootloader (115200 baud)
#   make flash-old PORT=/dev/ttyUSB0        Nano with the old bootloader (57600 baud, most clones)
# On Windows the port is COM3 or similar; on a Mac /dev/cu.usbserial-XXXX or /dev/cu.wchusbserialXXXX.

MCU   = atmega328p
F_CPU = 16000000UL
PORT ?= /dev/ttyUSB0
CC    = avr-gcc
CFLAGS = -mmcu=$(MCU) -DF_CPU=$(F_CPU) -Os -std=gnu99 -Wall -Wextra -ffunction-sections -fdata-sections
LDFLAGS = -mmcu=$(MCU) -Wl,--gc-sections
SRC = bare/main.c pusher/pusher_logic.c

all: bare/pusher.hex size

bare/pusher.elf: $(SRC) pusher/pusher_logic.h
	$(CC) $(CFLAGS) $(LDFLAGS) $(SRC) -o $@

bare/pusher.hex: bare/pusher.elf
	avr-objcopy -O ihex -R .eeprom $< $@

size: bare/pusher.elf
	avr-size --format=avr --mcu=$(MCU) $< 2>/dev/null || avr-size $<

flash: bare/pusher.hex
	avrdude -p $(MCU) -c arduino -P $(PORT) -b 115200 -U flash:w:$<:i

flash-old: bare/pusher.hex
	avrdude -p $(MCU) -c arduino -P $(PORT) -b 57600 -U flash:w:$<:i

test:
	gcc -std=c99 -Wall -Wextra -Werror -Ipusher pusher/pusher_logic.c test/test_pusher_logic.c -lm -o test/test_pusher
	./test/test_pusher

clean:
	rm -f bare/pusher.elf bare/pusher.hex test/test_pusher

.PHONY: all size flash flash-old test clean
