AVR-Playground/makefile

85 lines
1.8 KiB
Makefile
Raw Normal View History

2024-02-09 02:14:45 +01:00
# AVR-GCC compiler
CC = avr-gcc
# Programmer (change it according to your programmer)
PROGRAMMER = usbasp
# MCU
MCU = atmega328p
2024-02-10 14:37:38 +01:00
QEMU_MACHINE_NAME = uno
2024-02-09 02:14:45 +01:00
# Compiler flags
2024-03-23 04:28:03 +01:00
CFLAGS = -std=c2x -Wall -Wno-array-bounds -mmcu=$(MCU) -DF_CPU=16000000UL -O3
2024-02-09 02:14:45 +01:00
# Comment the following line to disable UART debugging (see uart.h)
CFLAGS += -DDEBUG_UART_ENABLED
2024-02-09 02:14:45 +01:00
# Source files
2024-03-23 21:18:52 +01:00
SRCS = $(wildcard *.c)
2024-02-09 02:14:45 +01:00
# Object files directory
OBJDIR = build
2024-02-09 02:14:45 +01:00
# Object files
OBJS = $(addprefix $(OBJDIR)/,$(SRCS:.c=.o))
2024-02-09 02:14:45 +01:00
# Target file
TARGET = main
# Default target
all: $(OBJDIR) $(TARGET).hex
2024-02-09 02:14:45 +01:00
# Compile C files
$(OBJDIR)/%.o: %.c | $(OBJDIR)
2024-02-09 02:14:45 +01:00
$(CC) $(CFLAGS) -c $< -o $@
# Link object files
$(OBJDIR)/$(TARGET).elf: $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $(OBJDIR)/$(TARGET).elf
avr-strip $(OBJDIR)/$(TARGET).elf
2024-02-09 02:14:45 +01:00
# Convert ELF to HEX
$(TARGET).hex: $(OBJDIR)/$(TARGET).elf
avr-objcopy -O ihex -R .eeprom $(OBJDIR)/$(TARGET).elf $(TARGET).hex
2024-02-09 02:14:45 +01:00
# Flash the program
flash: $(TARGET).hex
avrdude -p $(MCU) -c $(PROGRAMMER) -U flash:w:$(TARGET).hex
2024-02-10 14:37:38 +01:00
# Run the program in QEMU
2024-03-25 08:13:49 +01:00
qemu: $(OBJDIR)/$(TARGET).elf
qemu-system-avr --nographic -machine $(QEMU_MACHINE_NAME) -bios $(OBJDIR)/$(TARGET).elf
2024-02-10 14:37:38 +01:00
# View the generated assembly
asm: $(TARGET).hex
avr-objdump -S $(OBJDIR)/$(TARGET).elf
2024-03-23 04:28:03 +01:00
serial:
picocom -b 9600 /dev/ttyUSB0
# Create build directory
$(OBJDIR):
mkdir -p $(OBJDIR)
2024-02-09 02:14:45 +01:00
# Clean
clean:
rm -rf $(OBJDIR) $(TARGET).hex
2024-02-09 02:14:45 +01:00
size: $(TARGET).hex
avr-size --mcu=atmega328p $(TARGET).hex
2024-03-24 01:23:44 +01:00
# Reset target
reset:
avrdude -p $(MCU) -c $(PROGRAMMER) -E reset
read-flash:
avrdude -p $(MCU) -c $(PROGRAMMER) -U flash:r:flash.hex:i
read-eeprom:
avrdude -p $(MCU) -c $(PROGRAMMER) -U eeprom:r:eeprom.hex:i
read-fuses:
avrdude -p $(MCU) -c $(PROGRAMMER) -U lfuse:r:-:h -U hfuse:r:-:h -U efuse:r:-:h
2024-03-25 08:13:49 +01:00
.PHONY: all clean flash qemu asm serial reset read-flash read-eeprom size