@@ -17,8 +17,10 @@ SD_VERSION = 6.1.1
1717SD_FILENAME = $(SD_NAME ) _nrf52_$(SD_VERSION )
1818SD_HEX = $(SD_PATH ) /$(SD_FILENAME ) _softdevice.hex
1919
20- # linker by MCU and SoftDevice eg. nrf52840_s140_v6.ld
21- LD_FILE = linker/$(MCU_SUB_VARIANT ) _$(SD_NAME ) _v$(word 1, $(subst ., ,$(SD_VERSION ) ) ) .ld
20+ MBR_HEX = lib/softdevice/mbr/hex/mbr_nrf52_2.4.1_mbr.hex
21+
22+ # linker by MCU eg. nrf52840.ld
23+ LD_FILE = linker/$(MCU_SUB_VARIANT ) .ld
2224
2325GIT_VERSION = $(shell git describe --dirty --always --tags)
2426GIT_SUBMODULE_VERSIONS = $(shell git submodule status | cut -d' ' -f3,4 | paste -s -d" " -)
9395# all files in src
9496C_SRC += $(wildcard src/* .c)
9597
98+ # all files in boards
99+ C_SRC += $(wildcard src/boards/* .c)
100+
96101# all sources files in specific board
97102C_SRC += $(wildcard src/boards/$(BOARD ) /* .c)
98103
@@ -162,11 +167,10 @@ ASM_SRC = $(NRFX_PATH)/mdk/gcc_startup_$(MCU_SUB_VARIANT).S
162167
163168# src
164169IPATH += src
170+ IPATH += src/boards
165171IPATH += src/boards/$(BOARD )
166-
167172IPATH += src/cmsis/include
168173IPATH += src/usb
169- IPATH += src/boards
170174IPATH += $(TUSB_PATH )
171175
172176# nrfx
@@ -196,17 +200,17 @@ IPATH += $(SDK_PATH)/drivers_nrf/delay
196200IPATH += $(SD_PATH ) /$(SD_FILENAME ) _API/include
197201IPATH += $(SD_PATH ) /$(SD_FILENAME ) _API/include/nrf52
198202
199- INC_PATHS = $(addprefix -I,$(IPATH ) )
200-
201203# ------------------------------------------------------------------------------
202204# Compiler Flags
203205# ------------------------------------------------------------------------------
204206
205- # Debugging/Optimization
207+ # Debug option use RTT for printf
206208ifeq ($(DEBUG ) , 1)
207- CFLAGS += -Og -ggdb
208- else
209- CFLAGS += -Os
209+ RTT_SRC = lib/SEGGER_RTT
210+
211+ CFLAGS += -ggdb -DCFG_DEBUG -DSEGGER_RTT_MODE_DEFAULT=SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL
212+ IPATH += $(RTT_SRC)/RTT
213+ C_SRC += $(RTT_SRC ) /RTT/SEGGER_RTT.c
210214endif
211215
212216# flags common to all targets
@@ -216,6 +220,7 @@ CFLAGS += \
216220 -mcpu=cortex-m4 \
217221 -mfloat-abi=hard \
218222 -mfpu=fpv4-sp-d16 \
223+ -Os \
219224 -ffunction-sections \
220225 -fdata-sections \
221226 -fno-builtin \
@@ -249,7 +254,7 @@ CFLAGS += -DCONFIG_NFCT_PINS_AS_GPIOS
249254CFLAGS += -DSOFTDEVICE_PRESENT
250255CFLAGS += -DDFU_APP_DATA_RESERVED=7*4096
251256
252- CFLAGS += -DUF2_VERSION='"$(GIT_VERSION ) $(GIT_SUBMODULE_VERSIONS ) $( SD_NAME ) $( SD_VERSION ) "'
257+ CFLAGS += -DUF2_VERSION='"$(GIT_VERSION ) $(GIT_SUBMODULE_VERSIONS ) "'
253258CFLAGS += -DBLEDIS_FW_VERSION='"$(GIT_VERSION ) $(SD_NAME ) $(SD_VERSION ) "'
254259
255260_VER = $(subst ., ,$(word 1, $(subst -, ,$(GIT_VERSION ) ) ) )
@@ -289,6 +294,8 @@ vpath %.S $(ASM_PATHS)
289294
290295OBJECTS = $(C_OBJECTS ) $(ASM_OBJECTS )
291296
297+ INC_PATHS = $(addprefix -I,$(IPATH ) )
298+
292299# ------------------------------------------------------------------------------
293300# BUILD TARGETS
294301# ------------------------------------------------------------------------------
@@ -306,40 +313,11 @@ endif
306313.PHONY : all clean flash dfu-flash sd gdbflash gdb
307314
308315# default target to build
309- all : $(BUILD ) /$(OUT_FILE ) -nosd.out $(BUILD ) /$(MERGED_FILE ) .hex
310-
311- # ------------------- Flash target -------------------
312-
313- check_defined = \
314- $(strip $(foreach 1,$1, \
315- $(call __check_defined,$1,$(strip $(value 2)))))
316- __check_defined = \
317- $(if $(value $1),, \
318- $(error Undefined make flag : $1$(if $2, ($2) )))
319-
320- # Flash the compiled
321- flash : $(BUILD ) /$(OUT_FILE ) -nosd.hex
322- @echo Flashing: $<
323- $(NRFJPROG ) --program $< --sectoranduicrerase -f nrf52 --reset
324-
325- dfu-flash : $(BUILD ) /$(MERGED_FILE ) .zip
326- @:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyACM0)
327- $(NRFUTIL ) --verbose dfu serial --package $< -p $(SERIAL ) -b 115200 --singlebank --touch 1200
328-
329- sd :
330- @echo Flashing: $(SD_HEX )
331- $(NRFJPROG ) --program $(SD_HEX ) -f nrf52 --chiperase --reset
332-
333- gdbflash : $(BUILD ) /$(MERGED_FILE ) .hex
334- @echo Flashing: $<
335- @$(GDB_BMP ) -nx --batch -ex ' load $<' -ex ' compare-sections' -ex ' kill'
336-
337- gdb : $(BUILD ) /$(OUT_FILE ) -nosd.out
338- $(GDB_BMP ) $<
316+ all : $(BUILD ) /$(OUT_FILE ) .out $(BUILD ) /$(OUT_FILE ) -nosd.hex $(BUILD ) /$(OUT_FILE ) -nosd.uf2 $(BUILD ) /$(MERGED_FILE ) .hex $(BUILD ) /$(MERGED_FILE ) .zip
339317
340318# ------------------- Compile rules -------------------
341319
342- # # Create build directories
320+ # Create build directories
343321$(BUILD ) :
344322 @$(MK ) $@
345323
@@ -357,26 +335,73 @@ $(BUILD)/%.o: %.S
357335 @$(CC ) -x assembler-with-cpp $(ASFLAGS ) $(INC_PATHS ) -c -o $@ $<
358336
359337# Link
360- $(BUILD ) /$(OUT_FILE ) -nosd .out : $(BUILD ) $(OBJECTS )
361- @echo LD $(OUT_FILE ) -nosd.out
338+ $(BUILD ) /$(OUT_FILE ) .out : $(BUILD ) $(OBJECTS )
339+ @echo LD $(notdir $@ )
362340 @$(CC ) -o $@ $(LDFLAGS ) $(OBJECTS ) -Wl,--start-group $(LIBS ) -Wl,--end-group
363341 @$(SIZE ) $@
364342
365343# ------------------- Binary generator -------------------
366344
367- # # Create binary . hex file from the .out file
368- $(BUILD ) /$(OUT_FILE ) -nosd .hex : $(BUILD ) /$(OUT_FILE ) -nosd .out
369- @echo CR $(OUT_FILE ) -nosd.hex
345+ # Create hex file (no sd, no mbr)
346+ $(BUILD ) /$(OUT_FILE ) .hex : $(BUILD ) /$(OUT_FILE ) .out
347+ @echo Create $(notdir $@ )
370348 @$(OBJCOPY ) -O ihex $< $@
371349
372- # merge bootloader and sd hex together
373- $(BUILD ) /$(MERGED_FILE ) .hex : $(BUILD ) /$(OUT_FILE ) -nosd .hex
374- @echo CR $(MERGED_FILE ) .hex
375- @mergehex -q -m $ < $(SD_HEX ) -o $@
350+ # Hex file with mbr (still no SD)
351+ $(BUILD ) /$(OUT_FILE ) -nosd .hex : $(BUILD ) /$(OUT_FILE ) .hex
352+ @echo Create $(notdir $@ )
353+ @python3 tools/hexmerge.py --overlap=replace -o $@ $ < $(MBR_HEX )
376354
377- # # Create pkg zip file for bootloader+SD combo to use with DFU Serial
378- .PHONY : genpkg
379- genpkg : $(BUILD ) /$(MERGED_FILE ) .zip
355+ # Bootolader only uf2
356+ $(BUILD ) /$(OUT_FILE ) -nosd.uf2 : $(BUILD ) /$(OUT_FILE ) -nosd.hex
357+ @echo Create $(notdir $@ )
358+ @python3 lib/uf2/utils/uf2conv.py -f 0xd663823c -c -o $@ $^
380359
381- $(BUILD ) /$(MERGED_FILE ) .zip : $(BUILD ) /$(OUT_FILE ) -nosd.hex
360+ # merge bootloader and sd hex together
361+ $(BUILD ) /$(MERGED_FILE ) .hex : $(BUILD ) /$(OUT_FILE ) .hex
362+ @echo Create $(notdir $@ )
363+ @python3 tools/hexmerge.py -o $@ $< $(SD_HEX )
364+
365+ # Create pkg zip file for bootloader+SD combo to use with DFU CDC
366+ $(BUILD ) /$(MERGED_FILE ) .zip : $(BUILD ) /$(OUT_FILE ) .hex
382367 @$(NRFUTIL ) dfu genpkg --dev-type 0x0052 --dev-revision $(DFU_DEV_REV ) --bootloader $< --softdevice $(SD_HEX ) $@
368+
369+ # ------------------- Flash target -------------------
370+
371+ check_defined = \
372+ $(strip $(foreach 1,$1, \
373+ $(call __check_defined,$1,$(strip $(value 2)))))
374+ __check_defined = \
375+ $(if $(value $1),, \
376+ $(error Undefined make flag : $1$(if $2, ($2) )))
377+
378+ # Flash the compiled
379+ flash : $(BUILD ) /$(OUT_FILE ) -nosd.hex
380+ @echo Flashing: $(notdir $< )
381+ $(NRFJPROG ) --program $< --sectoranduicrerase -f nrf52 --reset
382+
383+ # dfu using CDC interface
384+ dfu-flash : $(BUILD ) /$(MERGED_FILE ) .zip
385+ @:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyACM0)
386+ $(NRFUTIL ) --verbose dfu serial --package $< -p $(SERIAL ) -b 115200 --singlebank --touch 1200
387+
388+ erase :
389+ @echo Erasing flash
390+ $(NRFJPROG ) -f nrf52 --eraseall
391+
392+ # flash SD only
393+ sd :
394+ @echo Flashing: $(SD_HEX )
395+ $(NRFJPROG ) --program $(SD_HEX ) -f nrf52 --sectorerase --reset
396+
397+ # flash MBR only
398+ mbr :
399+ @echo Flashing: $(MBR_HEX )
400+ $(NRFJPROG ) --program $(MBR_HEX ) -f nrf52 --sectorerase --reset
401+
402+ gdbflash : $(BUILD ) /$(MERGED_FILE ) .hex
403+ @echo Flashing: $<
404+ @$(GDB_BMP ) -nx --batch -ex ' load $<' -ex ' compare-sections' -ex ' kill'
405+
406+ gdb : $(BUILD ) /$(OUT_FILE ) .out
407+ $(GDB_BMP ) $<
0 commit comments