-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
249 lines (205 loc) · 7.78 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
VERSION ?= 0.0.0-unknown.0
DEBUG ?= true
DEBUG_FLAGS ?= dnsserver flashcfg html httpclient httpserver json ota rtc system tcpserver wifi \
gpio hspi uart \
api apiserver config core device
DEBUG_IP ?= # 192.168.0.1
DEBUG_PORT ?= 48879
DEBUG_UART_NO ?= 0
FLASH_MODE ?= qio
FLASH_FREQ ?= 40
FW_BASE_URL ?= http://provisioning.example.com
FW_BASE_OTA_PATH ?= /firmware/esphlg
# ---- configurable stuff ends here ---- #
SHELL = /bin/bash # other shells will probably fail
rwildcard = $(foreach d,$(wildcard $(1:=/*)),$(call rwildcard,$d,$2) $(filter $(subst *,%,$2),$d))
# parse version
VERSION_PARTS := $(subst -, ,$(subst ., ,$(VERSION)))
VERSION_MAJOR := $(word 1,$(VERSION_PARTS))
VERSION_MINOR := $(word 2,$(VERSION_PARTS))
VERSION_PATCH := $(word 3,$(VERSION_PARTS))
VERSION_TYPE := $(word 4,$(VERSION_PARTS))
VERSION_LABEL := $(word 5,$(VERSION_PARTS))
ifeq ($(VERSION_TYPE),)
VERSION_TYPE = stable
endif
ifneq ($(shell echo $(VERSION_LABEL) | grep -E "^g[a-f0-9]+$$"),)
VERSION_LABEL := $(VERSION_TYPE)
VERSION_TYPE = git
endif
ifeq ($(VERSION_LABEL),)
VERSION_LABEL = 0
endif
# flash parameters
CFLAGS += -DFW_VERSION=\"$(VERSION)\"
CFLAGS += -DFW_VERSION_MAJOR=$(VERSION_MAJOR)
CFLAGS += -DFW_VERSION_MINOR=$(VERSION_MINOR)
CFLAGS += -DFW_VERSION_PATCH=$(VERSION_PATCH)
CFLAGS += -DFW_VERSION_LABEL=$(VERSION_LABEL)
CFLAGS += -DFW_VERSION_TYPE=VERSION_TYPE_$(shell echo $(VERSION_TYPE) | tr a-z A-Z)
ifeq ($(FLASH_MODE),qio)
FLASH_MODE_INT = 0
else ifeq ($(FLASH_MODE),qout)
FLASH_MODE_INT = 1
else ifeq ($(FLASH_MODE),dio)
FLASH_MODE_INT = 2
else ifeq ($(FLASH_MODE),dout)
FLASH_MODE_INT = 3
endif
ifeq ($(FLASH_FREQ),20)
FLASH_CLK_DIV = 2
FLASH_SIZE_FREQ_HEX = 22
else ifeq ($(FLASH_FREQ),26)
FLASH_CLK_DIV = 1
FLASH_SIZE_FREQ_HEX = 21
else ifeq ($(FLASH_FREQ),40)
FLASH_CLK_DIV = 0
FLASH_SIZE_FREQ_HEX = 20
else ifeq ($(FLASH_FREQ),80)
FLASH_CLK_DIV = 15
FLASH_SIZE_FREQ_HEX = 2F
endif
FLASH_SIZE_MAP = 2 # 1024 (512 + 512)
FLASH_SIZE = 1024
FLASH_BOOT_ADDR=0x00000
FLASH_USR1_ADDR=0x01000
FLASH_CONFIG_ADDR = 0x7C000
FLASH_USR2_ADDR=0x81000
FLASH_INIT_DATA_ADDR=0xFC000
FLASH_SYS_PARAM_ADDR=0xFE000
# tools & commands
CC = xtensa-lx106-elf-gcc
AR = xtensa-lx106-elf-ar
LD = xtensa-lx106-elf-gcc
OC = xtensa-lx106-elf-objcopy
MD = mkdir -p
RM = rm -rf
GZ = gzip -c
DD = dd status=none
ESPTOOL ?= esptool
APPGEN ?= $(PWD)/gen_appbin.py
# ensure we have a toolchain
ifeq ($(shell which $(CC) 2>/dev/null),)
$(error "No $(CC) found in $(PATH). Please install the ESP8266 toolchain")
endif
# ensure we have an SDK
ifeq ($(SDK_BASE),)
$(error "Please set SDK_BASE to the installation dir of your ESP8266 non-OS SDK")
endif
# ensure the SDK has been fixed
ifeq ($(wildcard $(SDK_BASE)/.fixed),)
$(error "Please run 'builder/fix-sdk.sh $(SDK_BASE)' to apply required fixes")
endif
APP = esphlg
SRC_MAIN_DIR = src
SRC_ESPGOODIES_DIR = src/espgoodies
BUILD_DIR = build
INIT_DATA_FILES = esp_init_data_default.bin
INC = $(SRC_MAIN_DIR) $(SDK_BASE)/include
LIB = c gcc hal pp phy net80211 lwip wpa crypto upgrade m main
CFLAGS += -Wpointer-arith -Wall -Wl,-EL -fno-inline-functions -nostdlib -mlongcalls -mtext-section-literals \
-ffunction-sections -fdata-sections -mforce-l32 -Wmissing-prototypes -fno-builtin-printf \
-fno-guess-branch-probability -freorder-blocks-and-partition -fno-cse-follow-jumps \
-D__ets__ -DICACHE_FLASH -DUSE_OPTIMIZE_PRINTF -DFLASH_CONFIG_ADDR=$(FLASH_CONFIG_ADDR) -D_OTA \
-Os -O2 -std=c99
LDFLAGS = -nostdlib -Wl,--no-check-sections -u call_user_start -Wl,-static -L$(SDK_BASE)/lib -Wl,--gc-sections -Os -O2
ifneq ($(DEBUG),true)
CFLAGS += -Werror
endif
INC := $(addprefix -I,$(INC))
LIB := $(addprefix -l,$(LIB))
SRC_MAIN_FILES = $(call rwildcard,$(SRC_MAIN_DIR),*.c)
SRC_ESPGOODIES_FILES = $(call rwildcard,$(SRC_ESPGOODIES_DIR),*.c)
OBJ_FILES = $(SRC_MAIN_FILES:$(SRC_MAIN_DIR)/%.c=$(BUILD_DIR)/%.o) \
$(SRC_ESPGOODIES_FILES:$(SRC_MAIN_DIR)/%.c=$(BUILD_DIR)/%.o)
INIT_DATA_FILES := $(foreach f,$(INIT_DATA_FILES),$(SDK_BASE)/bin/$(f))
VPATH = $(SRC_MAIN_DIR)
ifeq ($(DEBUG), true)
CFLAGS += -D_DEBUG
CFLAGS += -D_DEBUG_UART_NO=$(DEBUG_UART_NO)
ifneq ($(DEBUG_IP),)
CFLAGS += -D_DEBUG_IP=\"$(DEBUG_IP)\"
CFLAGS += -D_DEBUG_PORT=$(DEBUG_PORT)
endif
endif
# define _DEBUG_<FLAG>
CFLAGS += $(addprefix -D_DEBUG_,$(shell echo $(DEBUG_FLAGS) | tr a-z A-Z))
# define init data hex content
INIT_DATA_HEX_DEF = $(foreach f,$(INIT_DATA_FILES),$(shell \
def_name=_$$(basename $(f) | sed 's/.bin/_hex/' | tr a-z A-Z); \
echo -D$${def_name}="\{$$(hexdump -ve '"0x%08X,"' $(f))\}"; \
))
CFLAGS += -DFW_BASE_URL=\"$(FW_BASE_URL)\"
CFLAGS += -DFW_BASE_OTA_PATH=\"$(FW_BASE_OTA_PATH)\"
ldscript = $(SDK_BASE)/ld/eagle.app.v6.new.$(FLASH_SIZE).app$(1).ld
V ?= $(VERBOSE)
ifeq ("$(V)","1")
Q :=
vecho := @true
else
Q := @
vecho := @echo
endif
.PHONY: dirs
.NOTPARALLEL: buildinfo
.PRECIOUS: $(BUILD_DIR)/%.html.gz build/$(APP)%.out
all: buildinfo dirs $(BUILD_DIR)/firmware.bin
dirs:
$(Q) mkdir -p $(BUILD_DIR)
buildinfo:
$(vecho) "---- configuration summary ----"
$(vecho) " *" SDK_BASE = $(SDK_BASE)
$(vecho) " *" VERSION = $(VERSION)
$(vecho) " *" DEBUG = $(DEBUG)
$(vecho) " *" DEBUG_UART_NO = $(DEBUG_UART_NO)
$(vecho) " *" DEBUG_IP = $(DEBUG_IP):$(DEBUG_PORT)
$(vecho) " *" DEBUG_FLAGS = $(DEBUG_FLAGS)
$(vecho) " *" FLASH_MODE = $(FLASH_MODE)
$(vecho) " *" FLASH_FREQ = $(FLASH_FREQ)
$(vecho) " *" FW_BASE_URL = "$(FW_BASE_URL)"
$(vecho) " *" FW_BASE_OTA_PATH = "$(FW_BASE_OTA_PATH)"
$(vecho) " *" CFLAGS = $(CFLAGS)
$(vecho) "-------------------------------"
$(BUILD_DIR)/%.o: %.c
$(vecho) "CC $<"
@$(MD) -p $(@D)
$(Q) $(CC) $(INC) $(CFLAGS) -c $< -o $@
$(BUILD_DIR)/espgoodies/initdata.o: espgoodies/initdata.c
$(vecho) "CC $<"
$(Q) $(CC) $(INC) $(CFLAGS) $(INIT_DATA_HEX_DEF) -c $< -o $@
$(BUILD_DIR)/$(APP).a: $(OBJ_FILES)
$(vecho) "AR $@"
$(Q) $(AR) cru $@ $^
$(BUILD_DIR)/$(APP)%.out:$(BUILD_DIR)/$(APP).a
$(vecho) "LD $@"
$(Q) $(LD) $(LDFLAGS) -T$(call ldscript,$*) -Wl,--start-group $(LIB) $^ -Wl,--end-group -o $@
$(BUILD_DIR)/%.html.gz: html/%.html
$(vecho) "GZ $@"
$(Q) sed 's/{{VERSION}}/$(VERSION)/g' $^ > $(BUILD_DIR)/$$(basename $^)
$(Q) $(GZ) $(BUILD_DIR)/$$(basename $^) > $@
$(BUILD_DIR)/user%.bin: $(BUILD_DIR)/$(APP)%.out $(BUILD_DIR)/index.html.gz
$(vecho) "FW $@"
$(Q) $(OC) --only-section .text -O binary $< $(BUILD_DIR)/eagle.app.v6.text.bin
$(Q) $(OC) --only-section .data -O binary $< $(BUILD_DIR)/eagle.app.v6.data.bin
$(Q) $(OC) --only-section .rodata -O binary $< $(BUILD_DIR)/eagle.app.v6.rodata.bin
$(Q) $(OC) --only-section .irom0.text -O binary $< $(BUILD_DIR)/eagle.app.v6.irom0text.bin
$(Q) cd $(BUILD_DIR) && \
$(APPGEN) $(APP)$*.out 2 $(FLASH_MODE_INT) $(FLASH_CLK_DIV) $(FLASH_SIZE_MAP) $* index.html.gz
$(Q) mv $(BUILD_DIR)/eagle.app.flash.bin $@
$(BUILD_DIR)/firmware.bin: $(BUILD_DIR)/user1.bin $(BUILD_DIR)/user2.bin
$(vecho) "FW $@"
$(Q) $(DD) if=/dev/zero of=$@ bs=1k count=$(FLASH_SIZE)
$(Q) $(DD) if=$(SDK_BASE)/bin/boot.bin of=$@ bs=1k \
seek=$$(($(FLASH_BOOT_ADDR) / 1024)) conv=notrunc
$(Q) $(DD) if=$(BUILD_DIR)/user1.bin of=$@ bs=1k \
seek=$$(($(FLASH_USR1_ADDR) / 1024)) conv=notrunc
$(Q) $(DD) if=$(BUILD_DIR)/user2.bin of=$@ bs=1k \
seek=$$(($(FLASH_USR2_ADDR) / 1024)) conv=notrunc
$(Q) $(DD) if=$(SDK_BASE)/bin/esp_init_data_default.bin of=$@ bs=1k \
seek=$$(($(FLASH_INIT_DATA_ADDR) / 1024)) conv=notrunc
$(Q) $(DD) if=$(SDK_BASE)/bin/blank.bin of=$@ bs=1k \
seek=$$(($(FLASH_SYS_PARAM_ADDR) / 1024)) conv=notrunc
$(Q) echo -en "\x$(FLASH_MODE_INT)" | $(DD) of=$@ bs=1 seek=2 conv=notrunc
$(Q) echo -en "\x$(FLASH_SIZE_FREQ_HEX)" | $(DD) of=$@ bs=1 seek=3 conv=notrunc
clean:
$(Q) $(RM) $(BUILD_DIR)