-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakefile
61 lines (42 loc) · 1.41 KB
/
makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Target name for final intel hex file
TARGET = ** Target Name **
# Directory Structure
SOURCEDIR = .
BUILDDIR = build
# Compiler definitions
CFLAGS = -g -Wall
DEVICE = atmega328p
# Programmer definitions
DEVICE_ID = m328p
PORT = /dev/ttyACM0
PROGRAMMER = avrisp
BAUDRATE = 19200
# Source and intermediary file paths
SOURCES := $(shell find -name '*.c')
OBJECTS = $(patsubst $(SOURCEDIR)/%.c, $(BUILDDIR)/%.o, $(SOURCES))
ELF = $(BUILDDIR)/$(TARGET).elf
HEX = $(BUILDDIR)/$(TARGET).hex
# Compile all sources to an intel hex file
compile:$(OBJECTS)
avr-gcc $(CFLAGS) -mrelax -Os -mmcu=$(DEVICE) $(OBJECTS) -o $(ELF)
avr-objcopy -j .text -j .data -O ihex $(ELF) $(HEX)
avr-size --format=avr --mcu=$(DEVICE) $(ELF)
$(BUILDDIR)/%.o: $(SOURCEDIR)/%.c | $(BUILDDIR)
avr-gcc $(CFLAGS) -ffunction-sections -fdata-sections -Os -mmcu=$(DEVICE) -c $< -o $@
$(BUILDDIR):
mkdir -p $@
# Flash the intel hex file to the specified avr microcontroller
flash: $(HEX)
avrdude -c $(PROGRAMMER) -P $(PORT) -b $(BAUDRATE) -p $(DEVICE_ID) -U flash:w:$(HEX)
# Remove build outputs
clean:
rm -f $(OBJECTS) $(ELF) $(HEX)
# Compile sources, flash to device, clean outputs
all: compile flash clean
.PHONY: all compile flash clean help
help:
@echo "Targets:"
@echo "all - Run all targets"
@echo "compile - Build and compile sources to intel hex"
@echo "flash - Flash intel hex to AVR microcontroller"
@echo "clean - Remove all build outputs"