From c42dce080c14901ae86b0955e8a207e375f8d00d Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 14 Jul 2022 12:28:02 +0700 Subject: [PATCH 1/2] add debug linker when building with DEBUG=1 extend bootloader start to reserved app for debug build --- Makefile | 22 ++++- .../libraries/bootloader_dfu/dfu_types.h | 8 +- linker/nrf52833_debug.ld | 99 +++++++++++++++++++ linker/nrf52840_debug.ld | 99 +++++++++++++++++++ linker/nrf52_debug.ld | 89 +++++++++++++++++ 5 files changed, 311 insertions(+), 6 deletions(-) create mode 100644 linker/nrf52833_debug.ld create mode 100644 linker/nrf52840_debug.ld create mode 100644 linker/nrf52_debug.ld diff --git a/Makefile b/Makefile index f61922ed..50e18c8c 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,11 @@ SD_HEX = $(SD_PATH)/$(SD_FILENAME)_softdevice.hex MBR_HEX = lib/softdevice/mbr/hex/mbr_nrf52_2.4.1_mbr.hex # linker by MCU eg. nrf52840.ld -LD_FILE = linker/$(MCU_SUB_VARIANT).ld +ifeq ($(DEBUG), 1) + LD_FILE = linker/$(MCU_SUB_VARIANT)_debug.ld +else + LD_FILE = linker/$(MCU_SUB_VARIANT).ld +endif GIT_VERSION := $(shell git describe --dirty --always --tags) GIT_SUBMODULE_VERSIONS := $(shell git submodule status | cut -d" " -f3,4 | paste -s -d" " -) @@ -109,17 +113,17 @@ ifeq ($(MCU_SUB_VARIANT),nrf52) SD_NAME = s132 DFU_DEV_REV = 0xADAF CFLAGS += -DNRF52 -DNRF52832_XXAA -DS132 - CFLAGS += -DDFU_APP_DATA_RESERVED=7*4096 + DFU_APP_DATA_RESERVED=7*4096 else ifeq ($(MCU_SUB_VARIANT),nrf52833) SD_NAME = s140 DFU_DEV_REV = 52833 CFLAGS += -DNRF52833_XXAA -DS140 - CFLAGS += -DDFU_APP_DATA_RESERVED=7*4096 + DFU_APP_DATA_RESERVED=7*4096 else ifeq ($(MCU_SUB_VARIANT),nrf52840) SD_NAME = s140 DFU_DEV_REV = 52840 CFLAGS += -DNRF52840_XXAA -DS140 - CFLAGS += -DDFU_APP_DATA_RESERVED=10*4096 + DFU_APP_DATA_RESERVED=10*4096 else $(error Sub Variant $(MCU_SUB_VARIANT) is unknown) endif @@ -321,8 +325,18 @@ ifeq ($(DEBUG), 1) RTT_SRC = lib/SEGGER_RTT IPATH += $(RTT_SRC)/RTT C_SRC += $(RTT_SRC)/RTT/SEGGER_RTT.c + DFU_APP_DATA_RESERVED = 0 + + # expand bootloader address to 28KB of reserved app + ifeq ($(MCU_SUB_VARIANT),nrf52840) + CFLAGS += -DBOOTLOADER_REGION_START=0xED000 + else + CFLAGS += -DBOOTLOADER_REGION_START=0x6D000 + endif endif +CFLAGS += -DDFU_APP_DATA_RESERVED=$(DFU_APP_DATA_RESERVED) + #------------------------------------------------------------------------------ # Linker Flags #------------------------------------------------------------------------------ diff --git a/lib/sdk11/components/libraries/bootloader_dfu/dfu_types.h b/lib/sdk11/components/libraries/bootloader_dfu/dfu_types.h index 8da73309..4148e1c9 100644 --- a/lib/sdk11/components/libraries/bootloader_dfu/dfu_types.h +++ b/lib/sdk11/components/libraries/bootloader_dfu/dfu_types.h @@ -48,14 +48,18 @@ static inline bool is_sd_existed(void) #define CODE_PAGE_SIZE 0x1000 /**< Size of a flash codepage. Used for size of the reserved flash space in the bootloader region. Will be runtime checked against NRF_UICR->CODEPAGESIZE to ensure the region is correct. */ #if defined(NRF52832_XXAA) || defined(NRF52833_XXAA) - // Flash = 512 KB + // Flash = 512 KB, allow to be defined in debug mode + #ifndef BOOTLOADER_REGION_START #define BOOTLOADER_REGION_START 0x00074000 /**< This field should correspond to start address of the bootloader, found in UICR.RESERVED, 0x10001014, register. This value is used for sanity check, so the bootloader will fail immediately if this value differs from runtime value. The value is used to determine max application size for updating. */ + #endif #define BOOTLOADER_MBR_PARAMS_PAGE_ADDRESS 0x0007E000 /**< The field specifies the page location of the mbr params page address. */ #define BOOTLOADER_SETTINGS_ADDRESS 0x0007F000 /**< The field specifies the page location of the bootloader settings address. */ #elif defined(NRF52840_XXAA) - // Flash = 1024 KB + // Flash = 1024 KB, allow to be defined in debug mode + #ifndef BOOTLOADER_REGION_START #define BOOTLOADER_REGION_START 0x000F4000 /**< This field should correspond to start address of the bootloader, found in UICR.RESERVED, 0x10001014, register. This value is used for sanity check, so the bootloader will fail immediately if this value differs from runtime value. The value is used to determine max application size for updating. */ + #endif #define BOOTLOADER_MBR_PARAMS_PAGE_ADDRESS 0x000FE000 /**< The field specifies the page location of the mbr params page address. */ #define BOOTLOADER_SETTINGS_ADDRESS 0x000FF000 /**< The field specifies the page location of the bootloader settings address. */ diff --git a/linker/nrf52833_debug.ld b/linker/nrf52833_debug.ld new file mode 100644 index 00000000..ed8f3586 --- /dev/null +++ b/linker/nrf52833_debug.ld @@ -0,0 +1,99 @@ +/* Linker script to configure memory regions. */ + +SEARCH_DIR(.) +GROUP(-lgcc -lc -lnosys) + +MEMORY +{ + /** Flash start address for the bootloader. This setting will also be stored in UICR to allow the + * MBR to init the bootloader when starting the system. This value must correspond to + * BOOTLOADER_REGION_START found in dfu_types.h. The system is prevented from starting up if + * those values do not match. The check is performed in main.c, see + * APP_ERROR_CHECK_BOOL(*((uint32_t *)NRF_UICR_BOOT_START_ADDRESS) == BOOTLOADER_REGION_START); + */ + /* due to lack of flash for debug, we will use reserved app to extend bootloader size */ + FLASH (rx) : ORIGIN = 0x74000-28K, LENGTH = 0x7E000-0x74000-2K + 28K + + BOOTLOADER_CONFIG (r): ORIGIN = 0x7E000 - 2K, LENGTH = 2K + + /** Location of mbr params page in flash. */ + MBR_PARAMS_PAGE (rw) : ORIGIN = 0x0007E000, LENGTH = 0x1000 + + /** Location of bootloader setting in flash. */ + BOOTLOADER_SETTINGS (rw) : ORIGIN = 0x0007F000, LENGTH = 0x1000 + + + + /** RAM Region for bootloader. */ + /* Avoid conflict with NOINIT for OTA bond sharing */ + RAM (rwx) : ORIGIN = 0x20008000, LENGTH = 0x20020000-0x20008000 + + /* Location for double reset detection, no init */ + DBL_RESET (rwx) : ORIGIN = 0x20007F7C, LENGTH = 0x04 + + /** Location of non initialized RAM. Non initialized RAM is used for exchanging bond information + * from application to bootloader when using buttonless DFU OTA. */ + NOINIT (rwx) : ORIGIN = 0x20007F80, LENGTH = 0x80 + + + + /** Location in UICR where bootloader start address is stored. */ + UICR_BOOTLOADER (r) : ORIGIN = 0x10001014, LENGTH = 0x04 + + /** Location in UICR where mbr params page address is stored. */ + UICR_MBR_PARAM_PAGE(r) : ORIGIN = 0x10001018, LENGTH = 0x04 +} + +SECTIONS +{ + .fs_data_out ALIGN(4): + { + PROVIDE( __start_fs_data = .); + KEEP(*(fs_data)) + PROVIDE( __stop_fs_data = .); + } = 0 + + .bootloaderConfig : + { + KEEP(*(.bootloaderConfig)) + } > BOOTLOADER_CONFIG + + /* Place the bootloader settings page in flash. */ + .bootloaderSettings(NOLOAD) : + { + + } > BOOTLOADER_SETTINGS + + /* Write the bootloader address in UICR. */ + .uicrBootStartAddress : + { + KEEP(*(.uicrBootStartAddress)) + } > UICR_BOOTLOADER + + /* Place the mbr params page in flash. */ + .mbrParamsPage(NOLOAD) : + { + + } > MBR_PARAMS_PAGE + + /* Write the bootloader address in UICR. */ + .uicrMbrParamsPageAddress : + { + KEEP(*(.uicrMbrParamsPageAddress)) + } > UICR_MBR_PARAM_PAGE + + .dbl_reset(NOLOAD) : + { + + } > DBL_RESET + + /* No init RAM section in bootloader. Used for bond information exchange. */ + .noinit(NOLOAD) : + { + + } > NOINIT + + /* other placements follow here... */ +} + +INCLUDE "nrf_common.ld" diff --git a/linker/nrf52840_debug.ld b/linker/nrf52840_debug.ld new file mode 100644 index 00000000..324cad1d --- /dev/null +++ b/linker/nrf52840_debug.ld @@ -0,0 +1,99 @@ +/* Linker script to configure memory regions. */ + +SEARCH_DIR(.) +GROUP(-lgcc -lc -lnosys) + +MEMORY +{ + /** Flash start address for the bootloader. This setting will also be stored in UICR to allow the + * MBR to init the bootloader when starting the system. This value must correspond to + * BOOTLOADER_REGION_START found in dfu_types.h. The system is prevented from starting up if + * those values do not match. The check is performed in main.c, see + * APP_ERROR_CHECK_BOOL(*((uint32_t *)NRF_UICR_BOOT_START_ADDRESS) == BOOTLOADER_REGION_START); + */ + /* due to lack of flash for debug, we will use reserved app to extend bootloader size */ + FLASH (rx) : ORIGIN = 0xF4000-28K, LENGTH = 0xFE000-0xF4000 - 2K + 28K /* 38 KB */ + + BOOTLOADER_CONFIG (r): ORIGIN = 0xFE000 - 2K, LENGTH = 2K + + /** Location of mbr params page in flash. */ + MBR_PARAMS_PAGE (rw) : ORIGIN = 0xFE000, LENGTH = 0x1000 + + /** Location of bootloader setting in flash. */ + BOOTLOADER_SETTINGS (rw) : ORIGIN = 0xFF000, LENGTH = 0x1000 + + + + /** RAM Region for bootloader. */ + /* Avoid conflict with NOINIT for OTA bond sharing */ + RAM (rwx) : ORIGIN = 0x20008000, LENGTH = 0x20040000-0x20008000 + + /* Location for double reset detection, no init */ + DBL_RESET (rwx) : ORIGIN = 0x20007F7C, LENGTH = 0x04 + + /** Location of non initialized RAM. Non initialized RAM is used for exchanging bond information + * from application to bootloader when using buttonless DFU OTA. */ + NOINIT (rwx) : ORIGIN = 0x20007F80, LENGTH = 0x80 + + + + /** Location in UICR where bootloader start address is stored. */ + UICR_BOOTLOADER (r) : ORIGIN = 0x10001014, LENGTH = 0x04 + + /** Location in UICR where mbr params page address is stored. */ + UICR_MBR_PARAM_PAGE(r) : ORIGIN = 0x10001018, LENGTH = 0x04 +} + +SECTIONS +{ + .fs_data_out ALIGN(4): + { + PROVIDE( __start_fs_data = .); + KEEP(*(fs_data)) + PROVIDE( __stop_fs_data = .); + } = 0 + + .bootloaderConfig : + { + KEEP(*(.bootloaderConfig)) + } > BOOTLOADER_CONFIG + + /* Place the bootloader settings page in flash. */ + .bootloaderSettings(NOLOAD) : + { + + } > BOOTLOADER_SETTINGS + + /* Write the bootloader address in UICR. */ + .uicrBootStartAddress : + { + KEEP(*(.uicrBootStartAddress)) + } > UICR_BOOTLOADER + + /* Place the mbr params page in flash. */ + .mbrParamsPage(NOLOAD) : + { + + } > MBR_PARAMS_PAGE + + /* Write the bootloader address in UICR. */ + .uicrMbrParamsPageAddress : + { + KEEP(*(.uicrMbrParamsPageAddress)) + } > UICR_MBR_PARAM_PAGE + + .dbl_reset(NOLOAD) : + { + + } > DBL_RESET + + /* No init RAM section in bootloader. Used for bond information exchange. */ + .noinit(NOLOAD) : + { + + } > NOINIT + + /* other placements follow here... */ +} + +INCLUDE "nrf_common.ld" diff --git a/linker/nrf52_debug.ld b/linker/nrf52_debug.ld new file mode 100644 index 00000000..b04b36a7 --- /dev/null +++ b/linker/nrf52_debug.ld @@ -0,0 +1,89 @@ +/* Linker script to configure memory regions. */ + +SEARCH_DIR(.) +GROUP(-lgcc -lc -lnosys) + +MEMORY +{ + /** Flash start address for the bootloader. This setting will also be stored in UICR to allow the + * MBR to init the bootloader when starting the system. This value must correspond to + * BOOTLOADER_REGION_START found in dfu_types.h. The system is prevented from starting up if + * those values do not match. The check is performed in main.c, see + * APP_ERROR_CHECK_BOOL(*((uint32_t *)NRF_UICR_BOOT_START_ADDRESS) == BOOTLOADER_REGION_START); + */ + FLASH (rx) : ORIGIN = 0x74000-28K, LENGTH = 0x7E000-0x74000+28K /* 40 KB */ + + /** Location of mbr params page in flash. */ + MBR_PARAMS_PAGE (rw) : ORIGIN = 0x7E000, LENGTH = 0x1000 + + /** Location of bootloader setting in flash. */ + BOOTLOADER_SETTINGS (rw) : ORIGIN = 0x7F000, LENGTH = 0x1000 + + + + /** RAM Region for bootloader. */ + RAM (rwx) : ORIGIN = 0x20008000, LENGTH = 0x20010000-0x20008000 + + /* Location for double reset detection, no init */ + DBL_RESET (rwx) : ORIGIN = 0x20007F7C, LENGTH = 0x04 + + /** Location of non initialized RAM. Non initialized RAM is used for exchanging bond information + * from application to bootloader when using buttonless DFU OTA. */ + NOINIT (rwx) : ORIGIN = 0x20007F80, LENGTH = 0x80 + + + + /** Location in UICR where bootloader start address is stored. */ + UICR_BOOTLOADER (r) : ORIGIN = 0x10001014, LENGTH = 0x04 + + /** Location in UICR where mbr params page address is stored. */ + UICR_MBR_PARAM_PAGE(r) : ORIGIN = 0x10001018, LENGTH = 0x04 +} + +SECTIONS +{ + .fs_data_out ALIGN(4): + { + PROVIDE( __start_fs_data = .); + KEEP(*(fs_data)) + PROVIDE( __stop_fs_data = .); + } = 0 + + /* Place the bootloader settings page in flash. */ + .bootloaderSettings(NOLOAD) : + { + + } > BOOTLOADER_SETTINGS + + /* Write the bootloader address in UICR. */ + .uicrBootStartAddress : + { + KEEP(*(.uicrBootStartAddress)) + } > UICR_BOOTLOADER + + /* Place the mbr params page in flash. */ + .mbrParamsPage(NOLOAD) : + { + + } > MBR_PARAMS_PAGE + + /* Write the bootloader address in UICR. */ + .uicrMbrParamsPageAddress : + { + KEEP(*(.uicrMbrParamsPageAddress)) + } > UICR_MBR_PARAM_PAGE + + .dbl_reset(NOLOAD) : + { + + } > DBL_RESET + + /* No init RAM section in bootloader. Used for bond information exchange. */ + .noinit(NOLOAD) : + { + + } > NOINIT + /* other placements follow here... */ +} + +INCLUDE "nrf_common.ld" From 3af7f744b42a6e919a0cb07ed19c1e288ac48010 Mon Sep 17 00:00:00 2001 From: hathach Date: Sun, 16 Oct 2022 13:09:19 +0700 Subject: [PATCH 2/2] add temp flash-skip-crc target --- Makefile | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Makefile b/Makefile index 50e18c8c..3e35a1e0 100644 --- a/Makefile +++ b/Makefile @@ -490,6 +490,12 @@ dfu-flash: flash-dfu flash-dfu: $(BUILD)/$(MERGED_FILE).zip @:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyACM0) $(NRFUTIL) --verbose dfu serial --package $< -p $(SERIAL) -b 115200 --singlebank --touch 1200 + +# flash skip crc magic ( app valid = 0x0001, crc = 0x0000 ) +#flash-skip-crc: +# nrfjprog --memwr $(BOOT_SETTING_ADDR) --val 0x00000001 -f nrf52 +# nrfjprog --memwr 0xFF000 --val 0x00000001 -f nrf52 +# nrfjprog --memwr 0x7F000 --val 0x00000001 -f nrf52 #------------------- Debugging -------------------