Skip to content

Commit ff120ff

Browse files
authored
Merge pull request #128 from adafruit/self-uf2
Major upgrade to bootloader to decouple SoftDevice and self upgrade with uf2
2 parents 79a6a0c + 2df81e6 commit ff120ff

File tree

79 files changed

+6656
-639
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+6656
-639
lines changed

.github/workflows/githubci.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ jobs:
2323

2424
- name: Install Toolchains
2525
run: |
26-
pip3 install adafruit-nrfutil uritemplate requests
27-
wget https://www.nordicsemi.com/api/sitecore/Products/DownloadPlatform --post-data=fileid=8F19D314130548209E75EFFADD9348DB -O cli-tools.tar
28-
tar -xv -f cli-tools.tar
29-
echo "::add-path::$GITHUB_WORKSPACE/mergehex"
26+
pip3 install adafruit-nrfutil uritemplate requests intelhex
3027
npm install --global xpm
3128
xpm install --global @xpack-dev-tools/arm-none-eabi-gcc@latest
3229
echo "::add-path::`echo $HOME/opt/xPacks/@xpack-dev-tools/arm-none-eabi-gcc/*/.content/bin`"

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ Module.symvers
5151
Mkfile.old
5252
dkms.conf
5353

54-
src/segger/Output
55-
_build-*/
54+
segger/Output
55+
_build/
5656
bin/
5757
*.emSession
5858
*.jlink
59+
.idea/
5960

6061
TAGS

.gitmodules

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
[submodule "tinyusb"]
1+
[submodule "lib/tinyusb"]
22
path = lib/tinyusb
33
url = https://github.com/hathach/tinyusb.git
44
[submodule "lib/nrfx"]
55
path = lib/nrfx
66
url = https://github.com/NordicSemiconductor/nrfx.git
7+
[submodule "lib/uf2"]
8+
path = lib/uf2
9+
url = https://github.com/microsoft/uf2.git
10+

Makefile

Lines changed: 80 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ SD_VERSION = 6.1.1
1717
SD_FILENAME = $(SD_NAME)_nrf52_$(SD_VERSION)
1818
SD_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

2325
GIT_VERSION = $(shell git describe --dirty --always --tags)
2426
GIT_SUBMODULE_VERSIONS = $(shell git submodule status | cut -d' ' -f3,4 | paste -s -d" " -)
@@ -93,6 +95,9 @@ endif
9395
# all files in src
9496
C_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
97102
C_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
164169
IPATH += src
170+
IPATH += src/boards
165171
IPATH += src/boards/$(BOARD)
166-
167172
IPATH += src/cmsis/include
168173
IPATH += src/usb
169-
IPATH += src/boards
170174
IPATH += $(TUSB_PATH)
171175

172176
# nrfx
@@ -196,17 +200,17 @@ IPATH += $(SDK_PATH)/drivers_nrf/delay
196200
IPATH += $(SD_PATH)/$(SD_FILENAME)_API/include
197201
IPATH += $(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
206208
ifeq ($(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
210214
endif
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
249254
CFLAGS += -DSOFTDEVICE_PRESENT
250255
CFLAGS += -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)"'
253258
CFLAGS += -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

290295
OBJECTS = $(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) $<

README.md

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,20 @@ This is a CDC/DFU/UF2 bootloader for nRF52 boards.
2020
- Particle Xenon
2121

2222
UF2 is an easy-to-use bootloader that appears as a flash drive. You can just copy `.uf2`-format
23-
application images to the flash drive to load new firmware.
24-
See https://github.com/Microsoft/uf2 and https://github.com/adafruit/uf2-samdx1
25-
for more information.
23+
application images to the flash drive to load new firmware. See https://github.com/Microsoft/uf2 and https://github.com/adafruit/uf2-samdx1 for more information.
2624

27-
[adafruit-nrfutil](https://github.com/adafruit/Adafruit_nRF52_nrfutil),
28-
a modified version of [Nordic nrfutil](https://github.com/NordicSemiconductor/pc-nrfutil),
29-
is required to perform DFU.
30-
Install `python3` if it is not installed already and run this command to install adafruit-nrfutil from PyPi:
25+
[adafruit-nrfutil](https://github.com/adafruit/Adafruit_nRF52_nrfutil), a modified version of [Nordic nrfutil](https://github.com/NordicSemiconductor/pc-nrfutil), is required to perform DFU. Install `python3` if it is not installed already and run this command to install adafruit-nrfutil from PyPi:
3126

3227
```
3328
$ pip3 install --user adafruit-nrfutil
3429
```
3530

36-
This repository depends on the following submodules:
37-
38-
- [tinyusb](https://github.com/hathach/tinyusb)
39-
- [nrfx](https://github.com/NordicSemiconductor/nrfx)
40-
41-
Note that `tinyusb` also includes `nrfx` as a submodule, so you need
42-
to initialize and update submodules with the `--recursive`` flag.
43-
4431
Clone this repo with following commands, or fork it for your own development
4532

4633
```
4734
git clone https://github.com/adafruit/Adafruit_nRF52_Bootloader
4835
cd Adafruit_nRF52_Bootloader
49-
git submodule update --init --recursive
36+
git submodule update --init
5037
```
5138

5239
## Features
@@ -63,7 +50,7 @@ There are two pins, `DFU` and `FRST` that bootloader will check upon reset/power
6350
- `Double Reset` Reset twice within 500 ms will enter DFU with UF2 and CDC support (only works with nRF52840)
6451
- `DFU = LOW` and `FRST = HIGH`: Enter bootloader with UF2 and CDC support
6552
- `DFU = LOW` and `FRST = LOW`: Enter bootloader with OTA, to upgrade with a mobile application such as Nordic nrfConnect/Toolbox
66-
- `DFU = HIGH` and `FRST = LOW`: Factory Reset mode: erase firmware application and its data
53+
- <s>`DFU = HIGH` and `FRST = LOW`: Factory Reset mode: erase firmware application and its data</s>
6754
- `DFU = HIGH` and `FRST = HIGH`: Go to application code if it is present, otherwise enter DFU with UF2
6855
- The `GPREGRET` register can also be set to force the bootloader can enter any of above modes (plus a CDC-only mode for Arduino).
6956
`GPREGRET` is set by the application before performing a soft reset.
@@ -92,6 +79,12 @@ To create a UF2 image from a .hex file:
9279
uf2conv.py firmware.hex -c -f 0xADA52840
9380
```
9481

82+
To create a UF2 image for bootloader from a .hex file using separated family of **0xd663823c**
83+
84+
```
85+
uf2conv.py bootloader.hex -c -f 0xd663823c
86+
```
87+
9588
## Burn & Upgrade with pre-built binaries
9689

9790
You can burn and/or upgrade the bootloader with either a J-link or DFU (serial) to a specific pre-built binary version
@@ -111,6 +104,7 @@ Prerequisites
111104

112105
- ARM GCC
113106
- Nordic's [nRF5x Command Line Tools](https://www.nordicsemi.com/Software-and-Tools/Development-Tools/nRF-Command-Line-Tools)
107+
- [Python IntelHex](https://pypi.org/project/IntelHex/)
114108

115109
To build:
116110

@@ -164,10 +158,17 @@ the variable `CROSS_COMPILE` as below:
164158
$ make CROSS_COMPILE=/opt/gcc-arm-none-eabi-9-2019-q4-major/bin/arm-none-eabi- BOARD=feather_nrf52832 all
165159
```
166160

167-
#### 2. `mergehex: No such file or directory`
161+
#### 2. `ModuleNotFoundError: No module named 'intelhex'`
162+
163+
Install python-intelhex with
164+
165+
```
166+
pip install intelhex
167+
```
168+
169+
170+
168171

169-
Make sure that `mergehex` is available from the command-line. This binary is
170-
part of Nordic's nRF5x Command Line Tools.
171172

172173
#### 3. `make: nrfjprog: No such file or directory`
173174

lib/SEGGER_RTT/License.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Important - Read carefully:
2+
3+
SEGGER RTT - Real Time Transfer for embedded targets
4+
5+
All rights reserved.
6+
7+
SEGGER strongly recommends to not make any changes
8+
to or modify the source code of this software in order to stay
9+
compatible with the RTT protocol and J-Link.
10+
11+
Redistribution and use in source and binary forms, with or
12+
without modification, are permitted provided that the following
13+
condition is met:
14+
15+
o Redistributions of source code must retain the above copyright
16+
notice, this condition and the following disclaimer.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19+
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20+
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR
23+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
25+
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26+
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
29+
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
30+
DAMAGE.
31+
32+
33+
(c) 2014 - 2016 SEGGER Microcontroller GmbH
34+
www.segger.com

lib/SEGGER_RTT/README.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
README.txt for the SEGGER RTT Implementation Pack.
2+
3+
Included files:
4+
===============
5+
Root Directory
6+
- Examples
7+
- Main_RTT_InputEchoApp.c - Sample application which echoes input on Channel 0.
8+
- Main_RTT_MenuApp.c - Sample application to demonstrate RTT bi-directional functionality.
9+
- Main_RTT_PrintfTest.c - Sample application to test RTT small printf implementation.
10+
- Main_RTT_SpeedTestApp.c - Sample application for measuring RTT performance. embOS needed.
11+
- RTT
12+
- SEGGER_RTT.c - The RTT implementation.
13+
- SEGGER_RTT.h - Header for RTT implementation.
14+
- SEGGER_RTT_Conf.h - Pre-processor configuration for the RTT implementation.
15+
- SEGGER_RTT_Printf.c - Simple implementation of printf to write formatted strings via RTT.
16+
- Syscalls
17+
- RTT_Syscalls_GCC.c - Low-level syscalls to retarget printf() to RTT with GCC / Newlib.
18+
- RTT_Syscalls_IAR.c - Low-level syscalls to retarget printf() to RTT with IAR compiler.
19+
- RTT_Syscalls_KEIL.c - Low-level syscalls to retarget printf() to RTT with KEIL/uVision compiler.
20+
- RTT_Syscalls_SES.c - Low-level syscalls to retarget printf() to RTT with SEGGER Embedded Studio.

0 commit comments

Comments
 (0)