-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake.avr.mk
180 lines (151 loc) · 6.34 KB
/
make.avr.mk
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
# make.avr.mk
# ^^^^^^^^^^^^^^^^^
# Author : TychoJ
# Version : 0.1
#
# Inspired by : Edwin Boer
#
# File : make.avr.mk
# Contains : The make process for building/flashing/debuging of AVR microcontrollers
# The settings for this make file can be found in: Makefile
# MIT License
#
# Copyright (c) 20223 TychoJ
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# From stackoverflow https://stackoverflow.com/a/12959764/16373649
# Make does not offer a recursive wildcard function, so here's one:
rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))
# End from stackoverflow https://stackoverflow.com/a/12959764/16373649
# Generate a list of folders to be included
INCLUDELIST := $(foreach dir, $(EXTFOLDER), -I$(dir))
# Generate a list of source code files relative of the source folder
CLIST := $(call rwildcard,$(SRCFOLDER),*.c)
# Generate a list of library files
EXTLIST := $(foreach dir, $(EXTFOLDER), $(call rwildcard,$(dir),*.c))
# Generate a list of object files
OBJLIST := $(patsubst %.c, %.o, $(CLIST)) $(patsubst %.c, %.o, $(EXTLIST))
# Generate a list of object files in the object(sub)folder
OBJLIST2 := $(foreach file, $(OBJLIST), $(OBJFOLDER)$(file))
# Define all tools with standard settings
TOOLC = $(AVRFOLDER)avr-gcc $(CFLAGS) -Os -mmcu=$(MICROCONTROLLER)
TOOLCOPY = $(AVRFOLDER)avr-objcopy
TOOLSIZE = $(AVRFOLDER)avr-size --format=avr --mcu=$(MICROCONTROLLER)
TOOLDUMP = $(AVRFOLDER)avr-objdump
ifdef PORT
TOOLDUDE = $(DUDEFOLDER)avrdude -c $(PROGRAMMER) -p $(MICROCONTROLLER) -P $(PORT)
else
TOOLDUDE = $(DUDEFOLDER)avrdude -c $(PROGRAMMER) -p $(MICROCONTROLLER)
endif
# Public goals
# Goal: make help
help: _start _help1 _end
_start:
@echo
@echo "#################################"
@echo " > Start: make.avr.mk"
_help1:
@echo "_________________________________"
@echo " > Possible options:"
@echo " make := Show this help message."
@echo " make help := Show this help message."
@echo " make all := Compile and link the source code."
@echo " make flash := Compile and link the source code and write the compiled hex-file to"
@echo " the flash memory of the microcontroller"
@echo " make test := Test the connection between the programmer and the microcontroller."
@echo " make fuse := Write the fuse bytes to the mircrocontroller."
@echo " make disasm := Disassemble the code for debugging."
@echo " make clean := Delete all generated .hex, .elf, and .o files."
@echo " make distclean := Delete all generated .hex, .elf and .o files as wel as the $(BINFOLDER) and the $(OBJFOLDER) folders."
_end:
@echo
@echo "#################################"
@echo " > Finished :-)"
@echo
# Goal: make all
all: _start _all1 $(BINFOLDER)$(PROJECTNAME).hex _end
_all1:
@echo " > Goal: Compile and link the source code."
@echo " > Object folder: $(OBJFOLDER)"
@echo " > Binary folder: $(BINFOLDER)"
@mkdir -p $(OBJFOLDER)
@mkdir -p $(BINFOLDER)
# Goal: make flash
flash: _start _flash1 $(BINFOLDER)$(PROJECTNAME).hex _flash2 _end
_flash1:
@echo " > Goal: Compile and link the source code and write the compiled hex-file to the flash memory of the microcontroller."
@echo " > binaray folder: $(BINFOLDER)"
@mkdir -p $(BINFOLDER)
_flash2:
@echo "_________________________________"
@echo " > Start flash"
$(TOOLDUDE) -e -U flash:w:$(BINFOLDER)$(PROJECTNAME).hex
# Goal: make test
test: _start _test1 _end
_test1:
@echo " > Goal: Test the connection between the programmer and the microcontroller."
$(TOOLDUDE) -v
# Goal: make fuse
fuse: _start _fuse1 _end
_fuse1:
@echo " > Goal: Write the fuse bytes to the mircrocontroller."
$(TOOLDUDE) -U lfuse:w:$(FUSELOW):m -U hfuse:w:$(FUSEHIGH):m -U efuse:w:$(FUSEEXT):m
# Goal: make disasm
disasm: _start _disasm1 $(BINFOLDER)/$(PROJECTNAME).elf _disasm2 _end
_disasm1:
@echo " > Goal: Disassemble the code for debugging."
_disasm2: $(BINFOLDER)/$(PROJECTNAME).elf
$(TOOLDUMP) -d $(BINFOLDER)$(PROJECTNAME).elf
# Goal: make clean
clean: _start _clean _end
_clean:
@echo " > Goal: Delete all generated .hex, .elf, and .o files."
rm -f $(BINFOLDER)$(PROJECTNAME).hex
rm -f $(BINFOLDER)$(PROJECTNAME).elf
rm -f -r $(OBJFOLDER)$(SRCFOLDER)
distclean: _start _distclean _end
_distclean:
@echo " > Goal: Delete all generated .hex, .elf and .o files as wel as the $(BINFOLDER) and the $(OBJFOLDER) folders."
rm -f $(BINFOLDER)$(PROJECTNAME).hex
rm -f $(BINFOLDER)$(PROJECTNAME).elf
rm -f -r $(OBJFOLDER)$(SRCFOLDER)
rm -r $(OBJFOLDER)
rm -r $(BINFOLDER)
# Create specific file formats
$(BINFOLDER)$(PROJECTNAME).hex: $(BINFOLDER)$(PROJECTNAME).elf
@echo "_________________________________"
@echo " > Generate hex-file"
rm -f $(BINFOLDER)$(PROJECTNAME).hex
$(TOOLCOPY) -j .text -j .data -O ihex $(BINFOLDER)$(PROJECTNAME).elf $(BINFOLDER)$(PROJECTNAME).hex
@echo "_________________________________"
@echo " > Memory usage"
$(TOOLSIZE) $(BINFOLDER)$(PROJECTNAME).elf
$(BINFOLDER)$(PROJECTNAME).elf: _compiler $(OBJLIST)
@echo "_________________________________"
@echo " > Start linker"
$(TOOLC) -o $(BINFOLDER)$(PROJECTNAME).elf $(OBJLIST2)
_compiler:
@echo "OBJLIST: $(OBJLIST)"
@echo "OBJLIST2: $(OBJLIST2)"
@echo "_________________________________"
@echo " > Start compiler"
%.o: %.c
@mkdir -p $(OBJFOLDER)$(dir $@)
@echo "test"
$(TOOLC) $(INCLUDELIST) -c -o $(OBJFOLDER)$@ $<