42 lines
929 B
Makefile

# Define the microcontroller type and clock frequency.
MCU = atmega328p
F_CPU = 16000000UL
# Define the project name (source and output files)
TARGET = main
SRC = $(TARGET).c
# Compiler and tools for AVR
CC = avr-gcc
CFLAGS = -mmcu=$(MCU) -DF_CPU=$(F_CPU)
LDFLAGS = -mmcu=$(MCU)
OBJCOPY = avr-objcopy
# avrdude settings: adjust the port (-P) to match your device
AVRDUDE = avrdude
AVRDUDE_FLAGS = -c arduino -p m328p -P /dev/ttyACM0 -b 115200 -D
# Output file names
ELF = $(TARGET).elf
HEX = $(TARGET).hex
# Default target: builds the HEX file
all: $(HEX)
# Compile source file to an ELF executable
$(ELF): $(SRC)
$(CC) $(CFLAGS) $(SRC) -Os -o $(ELF)
# Convert the ELF file to an Intel HEX file
$(HEX): $(ELF)
$(OBJCOPY) -O ihex -R .eeprom $(ELF) $(HEX)
# Flash the HEX file to the device with avrdude
flash: $(HEX)
$(AVRDUDE) $(AVRDUDE_FLAGS) -U flash:w:$(HEX):i
# Clean up build files
clean:
rm -f $(ELF) $(HEX)