Skip to content

Commit 75f48a5

Browse files
authored
Merge pull request adafruit#1004 from adafruit/nrf52840_usbboot
Nrf52840 usbboot
2 parents 1c92c33 + 862ae2f commit 75f48a5

Some content is hidden

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

41 files changed

+22904
-10733
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*.dxf binary
1313
*.mpy binary
1414
*.deb binary
15+
*.zip binary
1516

1617
# These should also not be modified by git.
1718
tests/basics/string_cr_conversion.py -text

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*.bin
88
*.map
99
*.hex
10-
!ports/nrf/**/bootloader/*.hex
10+
!ports/nrf/**/bootloader/**/*.hex
1111
*.dis
1212
*.exe
1313

.gitmodules

+7-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
url = https://github.com/micropython/stm32lib
3838
branch = work-F4-1.13.1+F7-1.5.0+L4-1.3.0
3939
[submodule "atmel-samd/asf4"]
40-
path = ports/atmel-samd/asf4
41-
url = https://github.com/adafruit/asf4.git
42-
branch = circuitpython
40+
path = ports/atmel-samd/asf4
41+
url = https://github.com/adafruit/asf4.git
42+
branch = circuitpython
4343
[submodule "tools/usb_descriptor"]
4444
path = tools/usb_descriptor
4545
url = https://github.com/adafruit/usb_descriptor.git
@@ -73,3 +73,7 @@
7373
[submodule "ports/nrf/nrfx"]
7474
path = ports/nrf/nrfx
7575
url = https://github.com/NordicSemiconductor/nrfx.git
76+
[submodule "lib/tinyusb"]
77+
path = lib/tinyusb
78+
url = https://github.com/hathach/tinyusb.git
79+
branch = develop

conf.py

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
"ports/nrf/hal",
118118
"ports/nrf/modules",
119119
"ports/nrf/nrfx",
120+
"ports/nrf/usb",
120121
"ports/pic16bit",
121122
"ports/qemu-arm",
122123
"ports/stm32",

lib/nrfutil

lib/tinyusb

Submodule tinyusb added at a0849fe

ports/nrf/Makefile

+81-15
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
# Select the board to build for: if not given on the command line,
2-
# then default to feather52832.
3-
BOARD ?= feather52832
4-
ifeq ($(wildcard boards/$(BOARD)/.),)
5-
$(error Invalid BOARD specified)
1+
# Select the board to build for.
2+
ifeq ($(BOARD),)
3+
$(info You must provide a BOARD parameter with 'BOARD=')
4+
$(info Possible values are:)
5+
$(info $(sort $(subst /.,,$(subst boards/,,$(wildcard boards/*/.)))))
6+
$(error BOARD not defined)
7+
else
8+
ifeq ($(wildcard boards/$(BOARD)/.),)
9+
$(error Invalid BOARD specified)
10+
endif
611
endif
712

8-
# If SoftDevice is selected, try to use that one.
9-
# Default to SD132 (exact version can be set with SOFTDEV_VERSION)
10-
SD ?= s132
11-
SD_LOWER = $(shell echo $(SD) | tr '[:upper:]' '[:lower:]')
13+
include boards/$(BOARD)/mpconfigboard.mk
1214

13-
# TODO: Verify that it is a valid target.
15+
SD_LOWER = $(shell echo $(SD) | tr '[:upper:]' '[:lower:]')
1416

15-
# If the build directory is not given, make it reflect the board name
17+
# Build directory with SD
1618
BUILD ?= $(if $(SD),build-$(BOARD)-$(SD_LOWER),build-$(BOARD))
1719

1820
include ../../py/mkenv.mk
19-
include boards/$(BOARD)/mpconfigboard.mk
2021
-include mpconfigport.mk
2122

2223
ifneq ($(SD), )
@@ -52,6 +53,8 @@ INC += -I./nrfx/drivers/include
5253
INC += -I../../lib/mp-readline
5354
INC += -I./drivers/bluetooth
5455
INC += -I./drivers
56+
INC += -I../../lib/tinyusb/src
57+
INC += -I./usb
5558

5659
NRF_DEFINES += -DCONFIG_GPIO_AS_PINRESET
5760

@@ -87,6 +90,7 @@ LIBS += -L $(dir $(LIBC_FILE_NAME)) -lc
8790
LIBS += -L $(dir $(LIBGCC_FILE_NAME)) -lgcc
8891

8992
SRC_NRFX = $(addprefix nrfx/,\
93+
drivers/src/nrfx_power.c \
9094
drivers/src/nrfx_spim.c \
9195
drivers/src/nrfx_twim.c \
9296
drivers/src/nrfx_uart.c \
@@ -96,10 +100,13 @@ SRC_C += \
96100
mphalport.c \
97101
fatfs_port.c \
98102
tick.c \
103+
background.c \
104+
internal_flash.c \
99105
drivers/bluetooth/ble_drv.c \
100106
drivers/bluetooth/ble_uart.c \
101107
boards/$(BOARD)/board.c \
102108
nrfx/mdk/system_$(MCU_SUB_VARIANT).c \
109+
nrfx/hal/nrf_nvmc.c \
103110
device/$(MCU_VARIANT)/startup_$(MCU_SUB_VARIANT).c \
104111
lib/oofatfs/ff.c \
105112
lib/oofatfs/option/ccsbcs.c \
@@ -108,10 +115,25 @@ SRC_C += \
108115
lib/utils/context_manager_helpers.c \
109116
lib/utils/interrupt_char.c \
110117
lib/utils/pyexec.c \
118+
lib/utils/stdout_helpers.c \
111119
lib/libc/string0.c \
112120
lib/mp-readline/readline.c \
113-
internal_flash.c \
114121

122+
ifeq ($(MCU_SUB_VARIANT),nrf52840)
123+
124+
SRC_C += \
125+
usb/tusb_descriptors.c \
126+
usb/usb_msc_flash.c \
127+
lib/tinyusb/src/portable/nordic/nrf5x/dcd_nrf5x.c \
128+
lib/tinyusb/src/portable/nordic/nrf5x/hal_nrf5x.c \
129+
lib/tinyusb/src/common/tusb_fifo.c \
130+
lib/tinyusb/src/device/usbd.c \
131+
lib/tinyusb/src/device/usbd_desc.c \
132+
lib/tinyusb/src/class/msc/msc_device.c \
133+
lib/tinyusb/src/class/cdc/cdc_device.c \
134+
lib/tinyusb/src/tusb.c \
135+
136+
endif
115137

116138
DRIVERS_SRC_C += $(addprefix modules/,\
117139
ubluepy/modubluepy.c \
@@ -213,9 +235,9 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
213235
$(BUILD)/$(FATFS_DIR)/ff.o: COPT += -Os
214236
$(filter $(PY_BUILD)/../extmod/vfs_fat_%.o, $(PY_O)): COPT += -Os
215237

216-
.phony: all flash sd binary hex
238+
.phony: all flash sd binary hex bootloader
217239

218-
all: binary hex
240+
all: binary hex uf2
219241

220242
OUTPUT_FILENAME = firmware
221243

@@ -231,12 +253,18 @@ hex: $(BUILD)/$(OUTPUT_FILENAME).hex
231253
$(BUILD)/$(OUTPUT_FILENAME).hex: $(BUILD)/$(OUTPUT_FILENAME).elf
232254
$(OBJCOPY) -O ihex $< $@
233255

256+
#####################
257+
# Flash with debugger
258+
#####################
234259
FLASHER ?=
235260

236261
ifeq ($(FLASHER),)
237262

263+
# Also update to bootloader settting to validate application and skip checksum ( app valid = 0x0001, crc = 0x0000 )
238264
flash: $(BUILD)/$(OUTPUT_FILENAME).hex
239265
nrfjprog --program $< --sectorerase -f $(MCU_VARIANT)
266+
nrfjprog --erasepage $(BOOT_SETTING_ADDR) -f $(MCU_VARIANT)
267+
nrfjprog --memwr $(BOOT_SETTING_ADDR) --val 0x00000001 -f $(MCU_VARIANT)
240268
nrfjprog --reset -f $(MCU_VARIANT)
241269

242270
sd: $(BUILD)/$(OUTPUT_FILENAME).hex
@@ -245,6 +273,9 @@ sd: $(BUILD)/$(OUTPUT_FILENAME).hex
245273
nrfjprog --program $< --sectorerase -f $(MCU_VARIANT)
246274
nrfjprog --reset -f $(MCU_VARIANT)
247275

276+
bootloader:
277+
nrfjprog --program $(BOOT_FILE).hex -f nrf52 --chiperase --reset
278+
248279
else ifeq ($(FLASHER), pyocd)
249280

250281
flash: $(BUILD)/$(OUTPUT_FILENAME).hex
@@ -257,6 +288,41 @@ sd: $(BUILD)/$(OUTPUT_FILENAME).hex
257288

258289
endif
259290

291+
#####################
292+
# Flash with DFU
293+
#####################
294+
.phony: dfu-gen dfu-flash dfu-bootloader
295+
296+
ifeq ($(OS),Windows_NT)
297+
NRFUTIL = ../../lib/nrfutil/binaries/win32/nrfutil.exe
298+
else
299+
NRFUTIL = nrfutil
300+
endif
301+
302+
check_defined = \
303+
$(strip $(foreach 1,$1, \
304+
$(call __check_defined,$1,$(strip $(value 2)))))
305+
__check_defined = \
306+
$(if $(value $1),, \
307+
$(error Undefined make flag: $1$(if $2, ($2))))
308+
309+
dfu-gen: $(BUILD)/$(OUTPUT_FILENAME).hex
310+
$(NRFUTIL) dfu genpkg --sd-req 0xFFFE --dev-type 0x0052 --application $^ $(BUILD)/dfu-package.zip
311+
312+
dfu-flash: $(BUILD)/dfu-package.zip
313+
@:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyUSB0)
314+
$(NRFUTIL) --verbose dfu serial --package $^ -p $(SERIAL) -b 115200 --singlebank
315+
316+
dfu-bootloader:
317+
@:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyACM0)
318+
$(NRFUTIL) --verbose dfu serial --package $(BOOT_FILE).zip -p $(SERIAL) -b 115200
319+
320+
uf2: $(BUILD)/$(OUTPUT_FILENAME).hex
321+
$(ECHO) "Create $(OUTPUT_FILENAME).uf2"
322+
$(PYTHON2) $(TOP)/tools/uf2/utils/uf2conv.py -f 0xADA52840 -c -o "$(BUILD)/$(OUTPUT_FILENAME).uf2" $^
323+
324+
$(BUILD)/dfu-package.zip: dfu-gen
325+
260326
$(BUILD)/$(OUTPUT_FILENAME).elf: $(OBJ)
261327
$(ECHO) "LINK $@"
262328
$(Q)$(CC) $(LDFLAGS) -o $@ $(OBJ) -Wl,--start-group $(LIBS) -Wl,--end-group

ports/nrf/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ This is a port of CircuitPython to the Nordic Semiconductor nRF52 series of chip
2828
* nRF52840
2929
* [PCA10056](http://www.nordicsemi.com/eng/Products/nRF52840-Preview-DK)
3030

31+
## Board Specific Instructions
32+
33+
For board-specific instructions on building and flashing CircuitPython, see
34+
the following links:
35+
36+
> **NOTE**: These board specific readmes may be more up to date than the
37+
generic board-neutral documentation further down.
38+
39+
* Adafruit [Feather nRF52](boards/feather52/README.md): 512KB Flash, 64KB SRAM
40+
* Adafruit [Feather nRF52840](boards/feather52840/README.md): 1MB Flash, 256KB SRAM
41+
* Nordic PCA10056 see [Feather nRF52840](boards/feather52840/README.md)
42+
43+
For all other board targets, see the generic notes below.
44+
3145
## Compile and Flash
3246

3347
Prerequisite steps for building the nrf port:

ports/nrf/background.c

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "tusb.h"
28+
29+
void run_background_tasks(void) {
30+
#ifdef NRF52840_XXAA
31+
tusb_task();
32+
tud_cdc_flush();
33+
#endif
34+
}

ports/nrf/boards/feather52840/bluefruit_nrf52840_s140_6.0.0.ld renamed to ports/nrf/boards/bluefruit_nrf52840_s140_6.0.0.ld

+9-10
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
---------- ---------- ------- -----------------------------------------
88
0x000FF000..0x000FFFFF ( 4KB) Bootloader Settings
99
0x000FE000..0x000FEFFF ( 4KB) Master Boot Record Params
10-
0x000F4000..0x000FDFFF ( 40KB) Serial + OTA Bootloader
10+
0x000F4000..0x000FDFFF ( 40KB) Bootloader (UF2 + CDC + OTA)
1111

12-
0x000F3000..0x000F3FFF ( 4KB ) Private Config Data (Bonding, Keys, etc.)
13-
0x000F2000..0x000F2FFF ( 4KB ) User NVM data
14-
0x000B2000..0x000F1FFF (256KB) User Filesystem
12+
0x000ED000..0x000F3FFF (28KB ) Private Config Data (Bonding, Keys, etc.)
13+
0x000AD000..0x000ECFFF (256KB) User Filesystem
1514

16-
0x00025000..0x000B1FFF (564KB) Application Code (including ISR vector)
17-
0x00001000..0x00024FFF (144KB) SoftDevice
15+
0x00026000..0x000ACFFF (540KB) Application Code (including ISR vector)
16+
0x00001000..0x00025FFF (148KB) SoftDevice
1817
0x00000000..0x00000FFF (4KB) Master Boot Record
1918
*/
2019

@@ -23,9 +22,9 @@ MEMORY
2322
{
2423
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x100000
2524

26-
FLASH_ISR (rx) : ORIGIN = 0x00025000, LENGTH = 0x001000
27-
FLASH_TEXT (rx) : ORIGIN = 0x00026000, LENGTH = 0x08C000
28-
FLASH_FATFS (r) : ORIGIN = 0x000B2000, LENGTH = 0x040000
25+
FLASH_ISR (rx) : ORIGIN = 0x00026000, LENGTH = 0x001000
26+
FLASH_TEXT (rx) : ORIGIN = 0x00027000, LENGTH = 0x086000
27+
FLASH_FATFS (r) : ORIGIN = 0x000AD000, LENGTH = 0x040000
2928

3029
/* 0x2000000 - RAM:ORIGIN is reserved for Softdevice */
3130
RAM (xrw) : ORIGIN = 0x20004000, LENGTH = 0x20040000 - 0x20004000
@@ -42,6 +41,6 @@ _estack = ORIGIN(RAM) + LENGTH(RAM);
4241

4342
/* RAM extents for the garbage collector */
4443
_ram_end = ORIGIN(RAM) + LENGTH(RAM);
45-
_heap_end = 0x20007000; /* tunable */
44+
_heap_end = 0x20020000; /* tunable */
4645

4746
INCLUDE "boards/common.ld"
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,11 @@
11
MCU_SERIES = m4
22
MCU_VARIANT = nrf52
33
MCU_SUB_VARIANT = nrf52
4+
SD ?= s132
45
SOFTDEV_VERSION ?= 2.0.1
56

67
LD_FILE = boards/feather52832/custom_nrf52832_dfu_app_$(SOFTDEV_VERSION).ld
7-
BOOTLOADER_PKG = boards/feather52832/bootloader/feather52_bootloader_$(SOFTDEV_VERSION)_s132_single.zip
8+
BOOT_FILE = boards/feather52832/bootloader/feather52_bootloader_$(SOFTDEV_VERSION)_s132_single
89

10+
BOOT_SETTING_ADDR = 0x7F000
911
NRF_DEFINES += -DNRF52832_XXAA
10-
11-
ifeq ($(OS),Windows_NT)
12-
NRFUTIL = ../../lib/nrfutil/binaries/win32/nrfutil.exe
13-
else
14-
NRFUTIL = nrfutil
15-
endif
16-
17-
CFLAGS += -DADAFRUIT_FEATHER52
18-
19-
check_defined = \
20-
$(strip $(foreach 1,$1, \
21-
$(call __check_defined,$1,$(strip $(value 2)))))
22-
__check_defined = \
23-
$(if $(value $1),, \
24-
$(error Undefined make flag: $1$(if $2, ($2))))
25-
26-
.PHONY: dfu-gen dfu-flash boot-flash
27-
28-
dfu-gen:
29-
$(NRFUTIL) dfu genpkg --dev-type 0x0052 --application $(BUILD)/$(OUTPUT_FILENAME).hex $(BUILD)/dfu-package.zip
30-
31-
dfu-flash:
32-
@:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyUSB0)
33-
$(NRFUTIL) dfu serial --package $(BUILD)/dfu-package.zip -p $(SERIAL) -b 115200
34-
35-
boot-flash:
36-
@:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyUSB0)
37-
$(NRFUTIL) dfu serial --package $(BOOTLOADER_PKG) -p $(SERIAL) -b 115200

0 commit comments

Comments
 (0)