-
Notifications
You must be signed in to change notification settings - Fork 8
/
Makefile
180 lines (146 loc) · 5.37 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
# Copyright (c) 2019-2020, Emil Renner Berthing
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
# OF SUCH DAMAGE.
MAKEFLAGS += rR
TARGET ?= code
# override these settings for your board/chip
HXTAL ?= 8000000
FLASH_SIZE ?= 128*1024
RAM_SIZE ?= 32*1024
# override these settings to your liking
CORECLOCK ?= 96000000
BOOTLOADER ?= 4*1024
O = build
HERE := $(dir $(lastword $(MAKEFILE_LIST)))
OS := $(if $(ComSpec),Windows,$(shell uname -s))
ARCH = rv32imac
ABI = ilp32
CODEMODEL = medlow
OPT = -Os -flto
ARCHFLAGS = -march=$(ARCH) -mabi=$(ABI) -mcmodel=$(CODEMODEL) -fno-pie
DEPENDS = -MMD -MP
WARNINGS = -Wall -Wextra -Wshadow -Wpointer-arith -Wformat=2 -Wformat-truncation=2 -Wundef -Wno-unused-parameter
CFLAGS = $(OPT) $(ARCHFLAGS) -ggdb -pipe $(DEPENDS) $(WARNINGS)
CFLAGS += -fno-common -ffunction-sections -fdata-sections
ASFLAGS = $(OPT) $(ARCHFLAGS) -ggdb -pipe $(DEPENDS) $(WARNINGS)
CPPFLAGS = $(if $(HXTAL),-DHXTAL=$(HXTAL) ,)-DCORECLOCK=$(CORECLOCK)
CPPFLAGS += -DBOOTLOADER=$(BOOTLOADER) -DFLASH_SIZE=$(FLASH_SIZE) -DRAM_SIZE=$(RAM_SIZE)
LDFLAGS = $(OPT) $(ARCHFLAGS) -static -Wl,-O1,--gc-sections,--relax,--build-id=none
LDSCRIPT = $(HERE)gd32vf103.ld
libs = eclic mtimer rcu gpio
vpath %.S $(HERE)
vpath %.c $(HERE)
ifeq ($(SPECS),)
CFLAGS += -ffreestanding -ftls-model=local-exec
CPPFLAGS += -I$(HERE)std -D_LIBC_LIMITS_H_
LDFLAGS += -nostdlib
LIBS += -lgcc
else ifeq ($(SPECS),picolibc)
else
LDFLAGS += -nostartfiles
endif
CPPFLAGS += -I$(HERE)include
CROSS_COMPILE = riscv64-unknown-elf-
CC = $(CROSS_COMPILE)gcc $(if $(SPECS),-specs=$(SPECS).specs ,)-std=gnu11
AS = $(CROSS_COMPILE)gcc $(if $(SPECS),-specs=$(SPECS).specs ,)-x assembler-with-cpp
OBJDUMP = $(CROSS_COMPILE)objdump
OBJCOPY = $(CROSS_COMPILE)objcopy
HEX = $(OBJCOPY) -O ihex
BIN = $(OBJCOPY) -O binary --strip-all
ifdef ComSpec
MKDIR_P = mkdir
RM_RF = rmdir /q /s
echo = @echo $1
size = @cmd /c 'for %I in ($1) do @echo. %~zI bytes'
else
MKDIR_P = mkdir -p
RM_RF = rm -rf
echo = @echo '$1'
ifeq ($(filter-out Linux MSYS%,$(OS)),)
size = @stat --printf ' %s bytes\n' $1
else
size = @stat -f ' %z bytes' $1
endif
endif
OPENOCD = openocd
STM32FLASH = stm32flash
STTY = stty
CAT = cat
SERIAL = /dev/ttyUSB0
DFU_UTIL = dfu-util
DFU_DEVICE = 1d50:613e
asm-objs := $(patsubst %.S,%.o,$(filter-out start.S,$(wildcard *.S)))
c-objs := $(patsubst %.c,%.o,$(filter-out start.c,$(wildcard *.c)))
objs = start.o
objs += $(patsubst %,lib-%.o,$(sort $(libs)))
objs += $(sort $(asm-objs) $(c-objs))
objects = $(addprefix $O/,$(objs))$(if $(SPECS),, $O/lib-std.o)
# use make V=1 to see raw commands or make -s for silence
ifeq ($V$(findstring s,$(word 1,$(MAKEFLAGS))),)
Q := @
else
echo =
size =
endif
.SECONDEXPANSION:
.PHONY: all release clean dump dfu romdfu uart cat
all: $O/$$(TARGET).bin
release: CPPFLAGS += -DNDEBUG
release: $O/$$(TARGET).bin
$O/lib-std.o: CFLAGS += -fno-builtin
$O/lib-%.o: $(HERE)lib/%.c $(MAKEFILE_LIST) | $O
$(call echo, CC $<)
$Q$(CC) -o $@ $(CFLAGS) $(CPPFLAGS) -c $<
$O/%.o: %.S $(MAKEFILE_LIST) | $O
$(call echo, AS $<)
$Q$(AS) -o $@ $(ASFLAGS) $(CPPFLAGS) -c $<
$O/%.o: %.c $(MAKEFILE_LIST) | $O
$(call echo, CC $<)
$Q$(CC) -o $@ $(CFLAGS) $(CPPFLAGS) -c $<
$O/$(TARGET).elf: $$(objects) $$(LDSCRIPT)
$(call echo, CCLD $@)
$Q$(CC) -o $@ $(LDFLAGS) -T$(LDSCRIPT) $(objects) $(LIBS)
$O/%.hex: $O/%.elf
$(call echo, HEX $@)
$Q$(HEX) $< $@
$O/%.bin: $O/%.elf
$(call echo, BIN $@)
$Q$(BIN) $< $@
$(call size,$@)
$O:
$(call echo, MKDIR $@)
$Q$(MKDIR_P) $@
clean:
$(call echo, RM $O)
$Q$(RM_RF) $O
dump: $O/$$(TARGET).elf
$(OBJDUMP) -x -d $< | $(PAGER)
dfu: $O/$$(TARGET).bin
$Q$(DFU_UTIL) -d $(DFU_DEVICE) -D $< -R
romdfu: $O/$$(TARGET).bin
$Q$(DFU_UTIL) -d 28e9:0189 -a 0 --dfuse-address 0x08000000:leave -D $<
cat: | $$(SERIAL)
$(STTY) -F$(SERIAL) raw -echo -hup cs8 -parenb -cstopb 115200
$(CAT) $(SERIAL)
-include $O/*.d