-
Notifications
You must be signed in to change notification settings - Fork 5
/
Makefile
193 lines (162 loc) · 6.53 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
184
185
186
187
188
189
190
191
192
193
# Project Name
PROJECT = ch32v307-lwip
# Path you your toolchain and openocd installation, leave empty if already in system PATH
TOOLCHAIN_ROOT := "/opt/wch/mounriver-studio-toolchain-riscv-gcc/bin/"
OPENOCD_ROOT := "/opt/wch/mounriver-studio-toolchain-openocd/bin/"
# Path to the WCH vendor codebase, make sure to update the submodule to get the code
VENDOR_ROOT = ./vendor/
FREERTOS_ROOT = ./src/lib/FreeRTOS/
FREERTOS_QUIRKS_ROOT = ./src/quirks/FreeRTOS/
LWIP_ROOT = ./src/lib/lwip/src/
LWIP_QUIRKS_ROOT = ./src/quirks/lwip/src/
###############################################################################
# Project specific
TARGET = $(PROJECT).elf
SRC_DIR = src/
CORE_DIR = $(VENDOR_ROOT)Core/
DEBUG_DIR = $(VENDOR_ROOT)Debug/
PERIPHERAL_DIR = $(VENDOR_ROOT)Peripheral/
STARTUP_DIR = $(VENDOR_ROOT)Startup/
# Toolchain
CC = $(TOOLCHAIN_ROOT)riscv-none-embed-gcc
DB = $(TOOLCHAIN_ROOT)riscv-none-embed-gdb
SIZE = $(TOOLCHAIN_ROOT)riscv-none-embed-size
# Project sources
SRC_FILES = $(wildcard $(SRC_DIR)*.c)
# FreeRTOS
SRC_FILES += $(FREERTOS_ROOT)croutine.c
SRC_FILES += $(FREERTOS_ROOT)event_groups.c
SRC_FILES += $(FREERTOS_ROOT)list.c
SRC_FILES += $(FREERTOS_ROOT)queue.c
SRC_FILES += $(FREERTOS_ROOT)stream_buffer.c
SRC_FILES += $(FREERTOS_ROOT)tasks.c
SRC_FILES += $(FREERTOS_ROOT)timers.c
SRC_FILES += $(FREERTOS_QUIRKS_ROOT)portable/GCC/RISC-V/port.c
ASM_FILES += $(FREERTOS_QUIRKS_ROOT)portable/GCC/RISC-V/portASM.S
SRC_FILES += $(FREERTOS_QUIRKS_ROOT)portable/MemMang/heap_4.c
# LwIP
SRC_FILES += $(LWIP_ROOT)core/def.c
SRC_FILES += $(LWIP_ROOT)core/inet_chksum.c
SRC_FILES += $(LWIP_ROOT)core/init.c
SRC_FILES += $(LWIP_ROOT)core/ip.c
SRC_FILES += $(LWIP_ROOT)core/mem.c
SRC_FILES += $(LWIP_ROOT)core/memp.c
SRC_FILES += $(LWIP_ROOT)core/netif.c
SRC_FILES += $(LWIP_ROOT)core/pbuf.c
SRC_FILES += $(LWIP_ROOT)core/raw.c
SRC_FILES += $(LWIP_ROOT)core/stats.c
SRC_FILES += $(LWIP_ROOT)core/sys.c
SRC_FILES += $(LWIP_ROOT)core/tcp_in.c
SRC_FILES += $(LWIP_ROOT)core/tcp_out.c
SRC_FILES += $(LWIP_ROOT)core/tcp.c
SRC_FILES += $(LWIP_ROOT)core/timeouts.c
SRC_FILES += $(LWIP_ROOT)core/udp.c
SRC_FILES += $(LWIP_ROOT)core/ipv4/acd.c
SRC_FILES += $(LWIP_ROOT)core/ipv4/dhcp.c
SRC_FILES += $(LWIP_ROOT)core/ipv4/etharp.c
SRC_FILES += $(LWIP_ROOT)core/ipv4/icmp.c
SRC_FILES += $(LWIP_ROOT)core/ipv4/ip4_addr.c
SRC_FILES += $(LWIP_ROOT)core/ipv4/ip4_frag.c
SRC_FILES += $(LWIP_ROOT)core/ipv4/ip4.c
SRC_FILES += $(LWIP_ROOT)netif/ethernet.c
SRC_FILES += $(LWIP_ROOT)api/api_lib.c
SRC_FILES += $(LWIP_ROOT)api/api_msg.c
SRC_FILES += $(LWIP_ROOT)api/err.c
SRC_FILES += $(LWIP_ROOT)api/if_api.c
SRC_FILES += $(LWIP_ROOT)api/netbuf.c
SRC_FILES += $(LWIP_ROOT)api/netdb.c
SRC_FILES += $(LWIP_ROOT)api/netifapi.c
SRC_FILES += $(LWIP_ROOT)api/sockets.c
SRC_FILES += $(LWIP_ROOT)api/tcpip.c
SRC_FILES += $(LWIP_ROOT)core/altcp.c
SRC_FILES += $(LWIP_ROOT)core/altcp_tcp.c
SRC_FILES += $(LWIP_ROOT)apps/http/httpd.c
SRC_FILES += $(LWIP_ROOT)apps/http/fs.c
SRC_FILES += $(LWIP_ROOT)../contrib/ports/freertos/sys_arch.c
# Project includes
INCLUDES = -I$(SRC_DIR)
INCLUDES += -I$(FREERTOS_ROOT)/include/
INCLUDES += -I$(FREERTOS_QUIRKS_ROOT)/portable/GCC/RISC-V/
INCLUDES += -I$(FREERTOS_QUIRKS_ROOT)/portable/GCC/RISC-V/chip_specific_extensions/RV32I_PFIC_no_extensions/
INCLUDES += -I$(LWIP_ROOT)
INCLUDES += -I$(LWIP_ROOT)../contrib/ports/freertos/include
INCLUDES += -I$(LWIP_ROOT)/include
INCLUDES += -I$(LWIP_QUIRKS_ROOT)
# Vendor sources:
ASM_FILES += $(STARTUP_DIR)startup_ch32v30x_D8C.S
SRC_FILES += $(CORE_DIR)core_riscv.c
SRC_FILES += $(DEBUG_DIR)debug.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_adc.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_bkp.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_can.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_crc.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_dac.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_dbgmcu.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_dma.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_dvp.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_eth.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_exti.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_flash.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_fsmc.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_gpio.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_i2c.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_iwdg.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_misc.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_opa.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_pwr.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_rcc.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_rng.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_rtc.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_sdio.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_spi.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_tim.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_usart.c
SRC_FILES += $(PERIPHERAL_DIR)src/ch32v30x_wwdg.c
# Vendor includes
INCLUDES += -I$(CORE_DIR)
INCLUDES += -I$(DEBUG_DIR)
INCLUDES += -I$(PERIPHERAL_DIR)inc/
# Vendor Link Script
LD_SCRIPT = $(VENDOR_ROOT)Ld/Link.ld
# Compiler Flags
CFLAGS = -march=rv32imacxw -mabi=ilp32 -msmall-data-limit=8 -mno-save-restore -O0
CFLAGS += -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -Wunused -Wuninitialized -g
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)"
CFLAGS += $(INCLUDES)
# Assembler Flags
ASMFLAGS = -x assembler-with-cpp
# Linker Flags
LFLAGS = -T $(LD_SCRIPT) -u _printf_float -nostartfiles -Xlinker --gc-sections -Wl,-Map,$(PROJECT).map --specs=nano.specs --specs=nosys.specs -static
###############################################################################
# This does an in-source build. An out-of-source build that places all object
# files into a build directory would be a better solution, but the goal was to
# keep this file very simple.
CXX_OBJS = $(SRC_FILES:.c=.o)
ASM_OBJS = $(ASM_FILES:.S=.o)
ALL_OBJS = $(ASM_OBJS) $(CXX_OBJS)
.PHONY: clean prog gdb-server_openocd gdb-client
all: $(TARGET) $(PROJECT).hex
# Compile
$(ASM_OBJS): %.o: %.S
@echo "[ASM CC] $@"
@$(CC) $(CFLAGS) $(ASMFLAGS) -c $< -o $@
$(CXX_OBJS): %.o: %.c
@echo "[CC] $@"
@$(CC) $(CFLAGS) -c $< -o $@
# Link
%.elf: $(ALL_OBJS)
@echo "[LD] $@"
@$(CC) $(CFLAGS) $(LFLAGS) $(ALL_OBJS) -o $@
@$(SIZE) $@
@@
%.hex: %.elf
@echo "[OBJCOPY] $@"
@$(TOOLCHAIN_ROOT)riscv-none-embed-objcopy -O ihex "$(PROJECT).elf" "$(PROJECT).hex"
# Clean
clean:
@rm -f $(ALL_OBJS) $(ALL_OBJS:o=d) $(TARGET) $(PROJECT).hex $(PROJECT).map
# Program
flash: $(TARGET)
sudo $(OPENOCD_ROOT)openocd -f $(OPENOCD_ROOT)wch-riscv.cfg -c init -c halt -c "program $(PROJECT).hex" -c "resume 0" -c exit
gdb:
sudo $(OPENOCD_ROOT)openocd -f $(OPENOCD_ROOT)wch-riscv.cfg