forked from giuliof/AVR-makefile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makefile
155 lines (127 loc) · 4.34 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Name of the project
PROJ_NAME = TestSerial
######################################################################
# SOURCES #
######################################################################
## Directories ##
# This is where the source files are located,
# which are not in the current directory
SRC_DIR = ./src
# The header files we use are located here
INC_DIR = ./src
INC_DIR += .
BUILD_DIR = build
OUTPUT_DIR = output
## FILES ##
# c files
#~ SRCS = main.c
SRCS = TestSerial.cpp
# asm files
# S maiuscola! Invoca prima il compilatore gcc che interpreta macro e altro
ASRC = BasicSerial.S
# header files
# Specify here libraries! Makefile will check existance before launching
DEPS = BasicSerial.h
# Object files
# Automatically declares object file names
OBJS = $(patsubst %.c, $(BUILD_DIR)/%.o, $(filter %.c,$(SRCS)) )
OBJS += $(patsubst %.cpp, $(BUILD_DIR)/%.o, $(filter %.cpp,$(SRCS)) )
OBJS += $(patsubst %.s, $(BUILD_DIR)/%.s.o, $(filter %.s,$(ASRC)) )
OBJS += $(patsubst %.S, $(BUILD_DIR)/%.s.o, $(filter %.S,$(ASRC)) )
# Virtual Paths
# Tell make to look in that folder if it cannot find a source
# in the current directory
vpath %.c $(SRC_DIR)
vpath %.cpp $(SRC_DIR)
vpath %.S $(SRC_DIR)
vpath %.h $(INC_DIR)
######################################################################
# SETUP TOOLS #
######################################################################
# GCC/programming Tools
CC = avr-gcc
CXX = avr-g++
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
GDB = avr-gdb
AS = avr-as
SIZE = avr-size
# Microcontroller
MCU = attiny44
F_CPU = 8000000
### GCC options ###
## Compiler flags ##
# Do not run the linker
CFLAGS = -c
# Debug informations
CFLAGS += -g
# Auto optimisation
CFLAGS += -Os
# All warning messages
CFLAGS += -Wall
# Puts functions and data into its own section - remove thread-safe things
CFLAGS += -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics
# Microcontroller
CFLAGS += -mmcu=$(MCU)
# Clock speed
CFLAGS += -DF_CPU=$(F_CPU)L
# Header files
CFLAGS += $(addprefix -I,$(INC_DIR))
## CXX flags are the same as CC ones here!
CXXFLAGS = $(CFLAGS)
# Linker flags
LFLAGS = -mmcu=$(MCU)
LFLAGS += $(addprefix -I,$(INC_DIR))
######################################################################
# PROGRAMMING TOOLS #
######################################################################
# To match MCU with BOARD, see link
# http://www.nongnu.org/avr-libc/user-manual/using_tools.html
PROGRAMMER = usbasp
BOARD = t13
# verbose
PROGRAM_FLAGS = -v
# choose programmer
PROGRAM_FLAGS += -c $(PROGRAMMER)
# target board
PROGRAM_FLAGS += -p $(BOARD)
######################################################################
# TARGETS #
######################################################################
.PHONY: clean
all: $(OUTPUT_DIR)/$(PROJ_NAME).hex
# invokes CC compiler before assemblying
$(BUILD_DIR)/%.s.o : %.S $(DEPS)
@echo -e "\033[1;33m[Assembling ]\033[0m $^"
@mkdir -p ${BUILD_DIR}
$(CC) $(CFLAGS) $< -o $@
# pure asm
$(BUILD_DIR)/%.s.o : %.s $(DEPS)
@echo -e "\033[1;33m[Assembling ]\033[0m $^"
@mkdir -p ${BUILD_DIR}
$(CC) $(CFLAGS) $< -o $@
# .cxx files
$(BUILD_DIR)/%.o: %.cpp $(DEPS)
@echo -e "\033[1;33m[Compiling ]\033[0m $^"
@mkdir -p ${BUILD_DIR}
$(CXX) $(CXXFLAGS) $< -o $@
# .c files
$(BUILD_DIR)/%.o: %.c $(DEPS)
@echo -e "\033[1;33m[Compiling ]\033[0m $^"
@mkdir -p ${BUILD_DIR}
$(CC) $(CFLAGS) $< -o $@
$(OUTPUT_DIR)/$(PROJ_NAME).elf: $(OBJS)
@echo -e "\033[1;33m[Linking ]\033[0m $@"
@mkdir -p ${OUTPUT_DIR}
$(CC) $(LFLAGS) -o $@ $(foreach file, $^, $(file)) -lm
$(OBJDUMP) -h -S $@ > $(OUTPUT_DIR)/$(PROJ_NAME).lss
$(OUTPUT_DIR)/$(PROJ_NAME).hex: $(OUTPUT_DIR)/$(PROJ_NAME).elf
$(OBJCOPY) -O ihex -R .eeprom $^ $@
size: $(OUTPUT_DIR)/$(PROJ_NAME).elf
$(SIZE) -C --mcu=$(MCU) $(OUTPUT_DIR)/$(PROJ_NAME).elf
program:
avrdude $(PROGRAM_FLAGS) -U flash:w:$(OUTPUT_DIR)/$(PROJ_NAME).hex:i
clean:
@echo -e "\033[1;33m[Cleaning ]\033[0m"
@rm -f $(BUILD_DIR)/*
@rm -f $(OUTPUT_DIR)/*