diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index ca22e093c3..8db3508e47 100644 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -70,7 +70,7 @@ CFLAGS_MCU_m4 = $(CFLAGS_CORTEX_M) -mtune=cortex-m4 -mcpu=cortex-m4 -mfpu=fpv4-s CFLAGS_MCU_m0 = $(CFLAGS_CORTEX_M) --short-enums -mtune=cortex-m0 -mcpu=cortex-m0 -mfloat-abi=soft -fno-builtin -CFLAGS += $(CFLAGS_MCU_$(MCU_SERIES)) +CFLAGS += $(CFLAGS_MCU_$(MCU_SERIES)) CFLAGS += $(INC) -Wall -Werror -ansi -std=gnu99 -nostdlib $(COPT) $(NRF_DEFINES) $(CFLAGS_MOD) CFLAGS += -fno-strict-aliasing CFLAGS += -fstack-usage @@ -291,4 +291,3 @@ CFLAGS += -DMICROPY_MODULE_FROZEN_MPY endif include ../../py/mkrules.mk - diff --git a/ports/nrf/boards/feather52/README.md b/ports/nrf/boards/feather52/README.md new file mode 100644 index 0000000000..e05d899533 --- /dev/null +++ b/ports/nrf/boards/feather52/README.md @@ -0,0 +1,110 @@ +# Setup + +Before you can build, you will need to run the following commands once: + +``` +$ cd circuitpython +$ git submodule update --init +$ make -C mpy-cross +``` + +You then need to download the SD and Nordic SDK files: + +> This script relies on `wget`, which must be available from the command line. + +``` +$ cd ports/nrf +$ ./drivers/bluetooth/download_ble_stack.sh +``` + +# Building and flashing firmware images + +## Building CircuitPython + +#### REPL over UART (default settings) + +To build a CircuitPython binary with default settings for the +`feather52` target enter: + +``` +$ make BOARD=feather52 V=1 +``` + +#### REPL over BLE UART (AKA `NUS`) + +To build a CircuitPython binary with REPL over BLE UART, edit +`bluetooth_conf.h` with the following values (under +`#elif (BLUETOOTH_SD == 132)`): + +``` +#define MICROPY_PY_BLE (1) +#define MICROPY_PY_BLE_NUS (1) +#define BLUETOOTH_WEBBLUETOOTH_REPL (1) +``` + +Then build the CircuitPython binary, including `SD=s132` +to enable BLE support in the build process: + +``` +$ make BOARD=feather52 V=1 SD=s132 +``` + +## Flashing with `nrfutil` + +The Adafruit Bluefruit nRF52 Feather ships with a serial and OTA BLE bootloader +that can be used to flash firmware images over a simple serial connection, +using the on-board USB serial converter. + +These commands assume that you have already installed `nrfutil`, as described +in the [learning guide](https://learn.adafruit.com/bluefruit-nrf52-feather-learning-guide/arduino-bsp-setup) +for the Arduino variant of the board. + +### 1. **Update bootloader** to single-bank version + +The Adafruit nRF52 Feather ships, by default, with a **dual-bank** bootloader +that cuts the available flash memory in half in exchange for safer +OTA updates. + +Due to the size of CircuitPython, we must migrate this bootloader to a +**single-bank** version, doubling the amount of flash memory available to us. + +> These commands only need to be run once and will update the SoftDevice and +bootloader from the dual-bank version that ships on Arduino-based Adafruit +Feather52 boards to a single-bank CircuitPython compatible version: + +#### S132 v2.0.1 single-bank (recommended): + +By default s132 v2.0.1 is used when no `SOFTDEV_VERSION` field is passed in: + +``` +$ make BOARD=feather52 SERIAL=/dev/tty.SLAB_USBtoUART boot-flash +``` + +#### S132 v5.0.0 (BLE5, experimental): + +To enable BLE5 support and the latest S132 release, flash the v5.0.0 bootloader via: + +``` +$ make BOARD=feather52 SERIAL=/dev/tty.SLAB_USBtoUART SOFTDEV_VERSION=5.0.0 boot-flash +``` + +### 2. Generate a CircuitPython DFU .zip package and flash it over serial + +The following command will package and flash the CircuitPython binary using the +appropriate bootloader mentionned above. + +This command assumes you have already build a valid circuitpython +image, as described earlier in this readme. + +> The name of the serial port target will vary, depending on your OS. + +``` +$ make BOARD=feather52 SERIAL=/dev/tty.SLAB_USBtoUART dfu-gen dfu-flash +``` + +If you built your CircuitPython binary with **BLE UART** support you will +need to add the `SD=s132` flag as shown below: + +``` +$ make BOARD=feather52 SERIAL=/dev/tty.SLAB_USBtoUART SD=s132 dfu-gen dfu-flash +``` diff --git a/ports/nrf/boards/feather52/bootloader/feather52_bootloader_2.0.1_s132_single.zip b/ports/nrf/boards/feather52/bootloader/feather52_bootloader_2.0.1_s132_single.zip new file mode 100644 index 0000000000..14e3bf7d68 Binary files /dev/null and b/ports/nrf/boards/feather52/bootloader/feather52_bootloader_2.0.1_s132_single.zip differ diff --git a/ports/nrf/boards/feather52/bootloader/feather52_bootloader_5.0.0_s132_single.zip b/ports/nrf/boards/feather52/bootloader/feather52_bootloader_5.0.0_s132_single.zip new file mode 100644 index 0000000000..2f30a9fd4c Binary files /dev/null and b/ports/nrf/boards/feather52/bootloader/feather52_bootloader_5.0.0_s132_single.zip differ diff --git a/ports/nrf/boards/feather52/custom_nrf52832_dfu_app.ld b/ports/nrf/boards/feather52/custom_nrf52832_dfu_app_2.0.1.ld similarity index 50% rename from ports/nrf/boards/feather52/custom_nrf52832_dfu_app.ld rename to ports/nrf/boards/feather52/custom_nrf52832_dfu_app_2.0.1.ld index de737e1584..824b6becfe 100644 --- a/ports/nrf/boards/feather52/custom_nrf52832_dfu_app.ld +++ b/ports/nrf/boards/feather52/custom_nrf52832_dfu_app_2.0.1.ld @@ -1,20 +1,31 @@ /* GNU linker script for NRF52 w/ s132 2.0.1 SoftDevice + + MEMORY MAP + ------------------------------------------------------------------------ + START ADDR END ADDR SIZE DESCRIPTION + ---------- ---------- ------- ----------------------------------------- + 0x00074000..0x00080000 ( 48KB) Serial + OTA Bootloader + 0x0006D000..0x00073FFF ( 28KB) Private Config Data (Bonding, Keys, etc.) + 0x00055000..0x0006CFFF ( 96KB) User Filesystem + 0x0001C000..0x00054FFF (228KB) Application Code + 0x00001000..0x0001BFFF (108KB) SoftDevice + 0x00000000..0x00000FFF (4KB) Master Boot Record */ -/* Specify the memory areas */ +/* Specify the memory areas (S132 2.0.1) */ MEMORY { FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x080000 /* entire flash, 512 KiB */ FLASH_ISR (rx) : ORIGIN = 0x0001c000, LENGTH = 0x001000 /* sector 0, 4 KiB */ - FLASH_TEXT (rx) : ORIGIN = 0x0001c400, LENGTH = 0x026c00 /* 152 KiB - APP - ISR */ + FLASH_TEXT (rx) : ORIGIN = 0x0001d000, LENGTH = 0x038000 /* APP - ISR, 224 KiB */ RAM (xrw) : ORIGIN = 0x200039c0, LENGTH = 0x0c640 /* 49.5 KiB, give 8KiB headroom for softdevice */ } - + /* produce a link error if there is not this amount of RAM for these sections */ _minimum_stack_size = 2K; _minimum_heap_size = 16K; - + /* top end of the stack */ /*_stack_end = ORIGIN(RAM) + LENGTH(RAM);*/ @@ -24,4 +35,4 @@ _estack = ORIGIN(RAM) + LENGTH(RAM); _ram_end = ORIGIN(RAM) + LENGTH(RAM); _heap_end = 0x20007000; /* tunable */ -INCLUDE "boards/common.ld" +INCLUDE "boards/common.ld" \ No newline at end of file diff --git a/ports/nrf/boards/feather52/custom_nrf52832_dfu_app_5.0.0.ld b/ports/nrf/boards/feather52/custom_nrf52832_dfu_app_5.0.0.ld new file mode 100644 index 0000000000..e7a09303b9 --- /dev/null +++ b/ports/nrf/boards/feather52/custom_nrf52832_dfu_app_5.0.0.ld @@ -0,0 +1,38 @@ +/* + GNU linker script for NRF52 w/ s132 5.0.0 SoftDevice + + MEMORY MAP + ------------------------------------------------------------------------ + START ADDR END ADDR SIZE DESCRIPTION + ---------- ---------- ------- ----------------------------------------- + 0x00074000..0x00080000 ( 48KB) Serial + OTA Bootloader + 0x0006D000..0x00073FFF ( 28KB) Private Config Data (Bonding, Keys, etc.) + 0x00055000..0x0006CFFF ( 96KB) User Filesystem + 0x00023000..0x00054FFF (200KB) Application Code + 0x00001000..0x00022FFF (136KB) SoftDevice + 0x00000000..0x00000FFF (4KB) Master Boot Record +*/ + +/* Specify the memory areas (S132 5.0.0) */ +MEMORY +{ + FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x080000 /* entire flash, 512 KiB */ + FLASH_ISR (rx) : ORIGIN = 0x00023000, LENGTH = 0x001000 /* sector 0, 4 KiB */ + FLASH_TEXT (rx) : ORIGIN = 0x00024000, LENGTH = 0x030FFF /* APP - ISR, 200 KiB */ + RAM (xrw) : ORIGIN = 0x200039c0, LENGTH = 0x0c640 /* 49.5 KiB, give 8KiB headroom for softdevice */ +} + +/* produce a link error if there is not this amount of RAM for these sections */ +_minimum_stack_size = 2K; +_minimum_heap_size = 16K; + +/* top end of the stack */ + +/*_stack_end = ORIGIN(RAM) + LENGTH(RAM);*/ +_estack = ORIGIN(RAM) + LENGTH(RAM); + +/* RAM extents for the garbage collector */ +_ram_end = ORIGIN(RAM) + LENGTH(RAM); +_heap_end = 0x20007000; /* tunable */ + +INCLUDE "boards/common.ld" diff --git a/ports/nrf/boards/feather52/mpconfigboard.mk b/ports/nrf/boards/feather52/mpconfigboard.mk index ce8dcde30d..30f64ca43c 100644 --- a/ports/nrf/boards/feather52/mpconfigboard.mk +++ b/ports/nrf/boards/feather52/mpconfigboard.mk @@ -1,13 +1,15 @@ MCU_SERIES = m4 MCU_VARIANT = nrf52 MCU_SUB_VARIANT = nrf52832 -SOFTDEV_VERSION = 2.0.1 +SOFTDEV_VERSION ?= 2.0.1 -LD_FILE = boards/feather52/custom_nrf52832_dfu_app.ld +LD_FILE = boards/feather52/custom_nrf52832_dfu_app_$(SOFTDEV_VERSION).ld +BOOTLOADER_PKG = boards/feather52/bootloader/feather52_bootloader_$(SOFTDEV_VERSION)_s132_single.zip NRF_DEFINES += -DNRF52832_XXAA + check_defined = \ $(strip $(foreach 1,$1, \ $(call __check_defined,$1,$(strip $(value 2))))) @@ -15,11 +17,15 @@ __check_defined = \ $(if $(value $1),, \ $(error Undefined make flag: $1$(if $2, ($2)))) -.PHONY: dfu-gen dfu-flash +.PHONY: dfu-gen dfu-flash boot-flash dfu-gen: nrfutil dfu genpkg --dev-type 0x0052 --application $(BUILD)/$(OUTPUT_FILENAME).hex $(BUILD)/dfu-package.zip dfu-flash: @:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyUSB0) - sudo nrfutil dfu serial --package $(BUILD)/dfu-package.zip -p $(SERIAL) + nrfutil dfu serial --package $(BUILD)/dfu-package.zip -p $(SERIAL) -b 115200 + +boot-flash: + @:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyUSB0) + nrfutil dfu serial --package $(BOOTLOADER_PKG) -p $(SERIAL) -b 115200 diff --git a/ports/nrf/boards/feather52/mpconfigboard_s132.mk b/ports/nrf/boards/feather52/mpconfigboard_s132.mk index ce8dcde30d..315758de34 100644 --- a/ports/nrf/boards/feather52/mpconfigboard_s132.mk +++ b/ports/nrf/boards/feather52/mpconfigboard_s132.mk @@ -1,25 +1,4 @@ -MCU_SERIES = m4 -MCU_VARIANT = nrf52 -MCU_SUB_VARIANT = nrf52832 -SOFTDEV_VERSION = 2.0.1 - -LD_FILE = boards/feather52/custom_nrf52832_dfu_app.ld - -NRF_DEFINES += -DNRF52832_XXAA - - -check_defined = \ - $(strip $(foreach 1,$1, \ - $(call __check_defined,$1,$(strip $(value 2))))) -__check_defined = \ - $(if $(value $1),, \ - $(error Undefined make flag: $1$(if $2, ($2)))) - -.PHONY: dfu-gen dfu-flash - -dfu-gen: - nrfutil dfu genpkg --dev-type 0x0052 --application $(BUILD)/$(OUTPUT_FILENAME).hex $(BUILD)/dfu-package.zip - -dfu-flash: - @:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyUSB0) - sudo nrfutil dfu serial --package $(BUILD)/dfu-package.zip -p $(SERIAL) +# This file is a placeholder to enable building with 'SD=s132' flag +# The actual config data is stored in the file referenced below, regardless +# of whether S132 is actively used or not. +include boards/$(BOARD)/mpconfigboard.mk