AVR-Template/makefile

67 lines
1.3 KiB
Makefile
Raw Normal View History

2024-03-23 19:36:08 +01:00
# AVR-GCC compiler
CC = avr-gcc
# Programmer (change it according to your programmer)
PROGRAMMER = usbasp
# MCU
MCU = atmega328p
QEMU_MACHINE_NAME = uno
# Compiler flags
CFLAGS = -std=c99 -Wall -Wno-array-bounds -mmcu=$(MCU) -DF_CPU=16000000UL -Os -g -ffunction-sections -fdata-sections
2024-03-23 19:36:08 +01:00
# Source files
SRCS = main.c
# Object files
OBJS = $(SRCS:.c=.o)
# Target file
TARGET = main
# Default target
all: $(TARGET).hex
# Compile C files
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
# Link object files
$(TARGET).elf: $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $(TARGET).elf -Wl,--gc-sections
2024-03-23 19:36:08 +01:00
# Convert ELF to HEX
$(TARGET).hex: $(TARGET).elf
avr-objcopy -O ihex -R .eeprom $(TARGET).elf $(TARGET).hex
# Flash the program
flash: $(TARGET).hex
avrdude -p $(MCU) -c $(PROGRAMMER) -U flash:w:$(TARGET).hex
# Run the program in QEMU
qemu: $(TARGET).elf
qemu-system-avr -machine $(QEMU_MACHINE_NAME) -bios $(TARGET).elf
.PHONY: serial
serial:
picocom -b 9600 /dev/ttyUSB0
# Check so that we can talk to the MCU
2024-03-23 19:36:08 +01:00
.PHONY: check
check:
avrdude -p $(MCU) -c $(PROGRAMMER)
# Generate symbol table and assembly
extra: $(TARGET).elf
avr-objdump -t $(TARGET).elf > $(TARGET).map
avr-objdump -S $(TARGET).elf > $(TARGET).asm
2024-03-23 19:36:08 +01:00
# Clean
.PHONY: clean
clean:
rm -f $(OBJS) $(TARGET).elf $(TARGET).hex $(TARGET).map $(TARGET).asm
2024-03-23 19:36:08 +01:00
size: main.hex
avr-size --mcu=$(MCU) main.hex