-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
executable file
·183 lines (152 loc) · 5.88 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#
# Author <insert your name here>
# Copyright (c) 2012 <insert your copyright message here>
# License: <insert your license reference here>
#
##########################################################
# Project Settings
##########################################################
PROJECT_NAME = main
OUTPUT_NAME = firmware
TARGET = atmega328
PROGRAMMER_TARGET = m328p
SYSTEM_CLOCK = 8000000L
FLASH_AFTER_COMPILE=FALSE
USE_BOOTLOADER=FALSE
##########################################################
# Compiler Options
##########################################################
OPTIMIZATION = s
##########################################################
# Programmer Options
##########################################################
PROGRAMMER = usbtiny
PORGRMAMER_PORT = usb
PROGRAMMER_OPTIONS =
BOOTLOADER = avrisp
BOOTLOADER_PORT = /dev/tty.usbserial-
BOOTLOADER_OPTIONS = -b 115200
##########################################################
# Project Directories
##########################################################
BUILD_DIR = ./build
DRIVERS_DIR = ./drivers
INCLUDES_DIR = ./includes
LIBRARIES_DIR = ./lib
SOURCES_DIR = ./src
##########################################################
# Includes
##########################################################
# C Files
C_SRCS = $(wildcard *.c)
C_SRCS += $(wildcard $(DRIVERS_DIR)/*.c)
C_SRCS += $(wildcard $(DRIVERS_DIR)/*/*.c)
C_SRCS += $(wildcard $(DRIVERS_DIR)/*/*/*.c)
C_SRCS += $(wildcard $(SOURCES_DIR)/*.c)
C_SRCS += $(wildcard $(SOURCES_DIR)/*/*.c)
C_SRCS += $(wildcard $(SOURCES_DIR)/*/*/*.c)
# C++ Files
CPP_SRCS = $(wildcard *.cpp)
CPP_SRCS += $(wildcard $(DRIVERS_DIR)/*.cpp)
CPP_SRCS += $(wildcard $(DRIVERS_DIR)/*/*.cpp)
CPP_SRCS += $(wildcard $(DRIVERS_DIR)/*/*/*.cpp)
CPP_SRCS += $(wildcard $(SOURCES_DIR)/*.cpp)
CPP_SRCS += $(wildcard $(SOURCES_DIR)/*/*.cpp)
CPP_SRCS += $(wildcard $(SOURCES_DIR)/*/*/*.cpp)
# ASM Files
AS_SRCS = $(wildcard *.S)
AS_SRCS += $(wildcard $(DRIVERS_DIR)/*.S)
AS_SRCS += $(wildcard $(DRIVERS_DIR)/*/*.S)
AS_SRCS += $(wildcard $(DRIVERS_DIR)/*/*/*.S)
AS_SRCS += $(wildcard $(SOURCES_DIR)/*.S)
AS_SRCS += $(wildcard $(SOURCES_DIR)/*/*.S)
AS_SRCS += $(wildcard $(SOURCES_DIR)/*/*/*.S)
# Header Files
INCLUDES = ./
INCLUDES += $(wildcard $(DRIVERS_DIR))
INCLUDES += $(wildcard $(INCLUDES_DIR))
INCLUDES += $(wildcard $(SOURCES_DIR))
# Generate Object List
OBJECTS = $(C_SRCS:.c=.o) $(CPP_SRCS:.cpp=.o) $(AS_SRCS:.S=.o)
LD_OBJECTS = $(wildcard *.a) $(wildcard $(LIBRARIES_DIR)*.a) $(wildcard $(LIBRARIES_DIR)*/*.a) $(wildcard $(LIBRARIES_DIR)*/*/*.a) $(wildcard $(LIBRARIES_DIR)*.o) $(wildcard $(LIBRARIES_DIR)*/*.o) $(wildcard $(LIBRARIES_DIR)*/*/*.o)
##########################################################
# Tools Path
##########################################################
AVRDUDE = avrdude
AS = avr-gcc
GCC = avr-gcc
GPP = avr-g++
LD = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
SIZE = avr-size
REMOVE = rm -f
##########################################################
# Compiler FLAGS
##########################################################
GCC_FLAGS = -g -mmcu=$(TARGET) -O$(OPTIMIZATION) -DF_CPU=$(SYSTEM_CLOCK) -fpack-struct -fshort-enums -funsigned-bitfields -funsigned-char -Wall -Wstrict-prototypes -std=c99
GCC_FLAGS += $(patsubst %,-I%,$(INCLUDES)) -I.
GPP_FLAGS = -g -mmcu=$(TARGET) -O$(OPTIMIZATION) -DF_CPU=$(SYSTEM_CLOCK) -fpack-struct -fshort-enums -funsigned-bitfields -funsigned-char -Wall -Wstrict-prototypes -fno-exceptions
GPP_FLAGS += $(patsubst %,-I%,$(INCLUDES)) -I.
AS_FLAGS = $(LISTING) -mmcu=$(TARGET) -DF_CPU=$(SYSTEM_CLOCK) -x assembler-with-cpp
AS_FLAGS += $(patsubst %,-I%,$(INCLUDES)) -I.
LD_FLAGS = -Wl,-Map,$(BUILD_DIR)/$(OUTPUT_NAME).map -mmcu=$(TARGET)
# Math Library
LD_FLAGS += -lm
# Minimalistic printf version
#LD_FLAGS += -Wl,-u,vfprintf -lprintf_min
# Floating point printf version (require math library above)
#LD_FLAGS += -Wl,-u,vfprintf -lprintf_flt
SIZE_FLAGS = -C --mcu=$(TARGET)
##########################################################
# Programmer FLAGS
##########################################################
PROGRAMMER_FLAGS = -p $(PROGRAMMER_TARGET) -c $(PROGRAMMER) -P $(PORGRMAMER_PORT) $(PROGRAMMER_OPTIONS)
BOOTLOADER_FLAGS = -p $(PROGRAMMER_TARGET) -c $(BOOTLOADER) -P $(BOOTLOADER_PORT) -F $(BOOTLOADER_OPTIONS)
ifeq ($(FLASH_AFTER_COMPILE),TRUE)
ifeq ($(USE_BOOTLOADER),TRUE)
all:: $(OUTPUT_NAME).hex flash_bootloader
else
all:: $(OUTPUT_NAME).hex flash_programmer
endif
else
all:: $(OUTPUT_NAME).hex
endif
$(OUTPUT_NAME).hex: directories $(OUTPUT_NAME).elf
$(OBJCOPY) -j .text -j .data -O ihex $(BUILD_DIR)/$(OUTPUT_NAME).elf $(BUILD_DIR)/$(OUTPUT_NAME).hex
$(SIZE) $(SIZE_FLAGS) $(BUILD_DIR)/$(OUTPUT_NAME).elf
$(OUTPUT_NAME).elf: $(OBJECTS)
$(LD) $(LD_FLAGS) $(LD_OBJECTS) $(OBJECTS) -o $(BUILD_DIR)/$(OUTPUT_NAME).elf
stats: $(OUTPUT_NAME).hex
$(SIZE) $(SIZE_FLAGS) $(BUILD_DIR)/$(OUTPUT_NAME).elf
clean:
$(REMOVE) $(OBJECTS)
$(REMOVE) $(BUILD_DIR)/$(OUTPUT_NAME).hex
$(REMOVE) $(BUILD_DIR)/$(OUTPUT_NAME).elf
$(REMOVE) $(BUILD_DIR)/$(OUTPUT_NAME).map
$(REMOVE) $(OBJECTS)
flash_programmer: $(OUTPUT_NAME).hex
$(AVRDUDE) $(PROGRAMMER_FLAGS) -U flash:w:$(BUILD_DIR)/$(OUTPUT_NAME).hex
flash_bootloader: $(OUTPUT_NAME).hex
$(AVRDUDE) $(BOOTLOADER_FLAGS) -U flash:w:$(BUILD_DIR)/$(OUTPUT_NAME).hex
##########################################################
# Default rules to create directories if they don't
# already exist
##########################################################
directories:
@mkdir -p $(BUILD_DIR)
@mkdir -p $(DRIVERS_DIR)
@mkdir -p $(INCLUDES_DIR)
@mkdir -p $(LIBRARIES_DIR)
@mkdir -p $(BUILD_DIR)
@mkdir -p $(SOURCES_DIR)
##########################################################
# Default rules to compile .c and .cpp file to .o
# and assemble .S files to .o
##########################################################
.c.o :
$(GCC) $(GCC_FLAGS) -c $< -o $(<:.c=.o)
.cpp.o :
$(GPP) $(GPP_FLAGS) -c $< -o $(<:.cpp=.o)
.S.o :
$(AS) $(AS_FLAGS) -c $< -o $(<:.S=.o)