Feedback
Feedback

If you are having issues with the exercises, please create a ticket on DevZone: devzone.nordicsemi.com
Click or drag files to this area to upload. You can upload up to 2 files.

Exercise 3 – DFU with external flash

v2.9.0 – v2.7.0

In this exercise, we will learn how to add external flash to MCUboot and the application, allowing us to place mcuboot_secondary into external flash. This approach maximizes the use of internal flash for the application, effectively doubling the possible application size. You could also use the external flash to store the application’s data/storage. We will cover enabling QSPI or SPI drivers, configuring devicetree, and setting up Sysbuild to use external flash.

Some Nordic Semiconductor development kits (DK) use SPI for external flash, and some boards use QSPI for external flash. Here is a table that shows which boards support which features.

nRF52 DKnRF52840 DKnRF5340 DKnRF54L15 DKnRF91XX DKsnRF7002 DK
External flash❌✅ QSPI✅ QSPI✅ SPI✅ SPI✅ SPI

Documentation for Bootloader -> Using external flash memory partitions was not updated to Sysbuild at the time of writing this exercise, but that is where official documentation on this will be found.

Note that Nordic Semiconductor development kits (DK) may contain one of the following two types of memory on the kit.

External flash MX25R64                GD25WB256          
Manufacturer         Macronix             GigaDevice
Capacity             64 Mbit (8 MB)  256 Mbit (32 MB)
Interface            SPI/Quad SPISPI/Quad SPI
Used on Most Nordic DKs Only nRF91X1 DKs (nRF9161 DK and nRF9151 DK)

Important

An external flash is not available on the nRF9160 DK revisions 0.9.0 or older.

Exercise steps

Open the code base of the exercise by navigating to Create a new application in the nRF Connect for VS Code extension, select Copy a sample, and search for Lesson 9 – Exercise 3. Select the QSPI or SPI folder based on your Development Kit as outlined in the table above.

1. Enable QSPI or SPI drivers in Kconfig.

The application and MCUboot need drivers to use the external flash. Enable QSPI or SPI drivers in Kconfig. Again, select the QSPI or SPI tab based on your development kit.

1.1 Enable QSPI in application configuration: prj.conf:

# STEP 1.1 - Enable QSPI driver for Application
CONFIG_NORDIC_QSPI_NOR=y
Kconfig

1.2 Enable QSPI in MCUboot configuration: sysbuild/mcuboot.conf:

# STEP 1.2 - Enable QSPI driver for MCUboot
CONFIG_NORDIC_QSPI_NOR=y
Kconfig

1.1 Enable SPI in application configuration: prj.conf:

# Step 1.1 - Enable SPI driver for the application
CONFIG_GPIO=y
CONFIG_SPI=y
CONFIG_SPI_NOR=y
CONFIG_SPI_NOR_SFDP_DEVICETREE=y
CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE=4096
# QSPI drivers are enabled by defualt for some chips.
# Disable it explicitly to be sure QSPI is disabled.
CONFIG_NORDIC_QSPI_NOR=n 
Kconfig

1.2 Enable SPI in MCUboot configuration: sysbuild/mcuboot.conf:

# Step 1.2 - Enable SPI driver for MCUboot
CONFIG_GPIO=y
CONFIG_SPI=y
CONFIG_SPI_NOR=y
CONFIG_SPI_NOR_SFDP_DEVICETREE=y
CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE=4096
# QSPI drivers are enabled by defualt for some chips.
# Disable it explicitly to be sure QSPI is disabled.
CONFIG_NORDIC_QSPI_NOR=n 

# required by SPI driver
CONFIG_MULTITHREADING=y
Kconfig

2. Enable external flash to be used with the bootloader/DFU.

To enable external flash to be used with the bootloader/DFU, we need to configure Sysbuild, the application image, and the MCUboot image.

Let’s start with the application and MCUboot. Our DKs have either the mx25r64 external flash (All DKs except the nRF91x1 DKs) or the gd25wb256 external memory (nRF91x1 DKs). First, we will enable the external flash in DTS. At the same time, we will choose it as nordic,pm-ext-flash to tell the partition manager to use this as external flash.

More details on this can be found in External flash memory partitions.

2.1 Enable external flash in application.

Add the following in app.overlay

/* STEP 2.1 - Add external flash to application */
&mx25r64 {
	status = "okay";
};

/ {
	chosen {
		nordic,pm-ext-flash = &mx25r64;
	};
};
Devicetree
/* STEP 2.1 - Add external flash to application */
&gd25wb256 {
	status = "okay";
};
 
/ {
	chosen {
		nordic,pm-ext-flash = &gd25wb256;
	};
};
Devicetree

2.2 Enable external flash in MCUboot.

Add the following in sysbuild/mcuboot.overlay

/* STEP 2.2 - Add external flash to MCUboot */
&mx25r64 {
	status = "okay";
};

/ {
	chosen {
		nordic,pm-ext-flash = &mx25r64;
	};
};
Devicetree
/* STEP 2.2 - Add external flash to MCUboot */
&gd25wb256 {
	status = "okay";
};
 
/ {
	chosen {
		nordic,pm-ext-flash = &gd25wb256;
	};
};
Devicetree

2.3 Now, build the project, and observe that external flash is available in the partition manager using the Memory Report:

3. Configure Sysbuild to use external flash.

Next, we want to configure Sysbuild to use external flash for the mcuboot_secondary partition.

3.1 Configure MCUboot to use external flash.

Add the following lines in sysbuild.conf:

# STEP 3.1 - MCUboot should use external flash
SB_CONFIG_PM_EXTERNAL_FLASH_MCUBOOT_SECONDARY=y
Kconfig
# STEP 3.1 - MCUboot should use external flash
SB_CONFIG_PM_EXTERNAL_FLASH_MCUBOOT_SECONDARY=y
# STEP 3.1 - For SPI, we need to set this
SB_CONFIG_PM_OVERRIDE_EXTERNAL_DRIVER_CHECK=y
Kconfig

External flash drivers default to QSPI. For SPI, we need to override this with SB_CONFIG_PM_OVERRIDE_EXTERNAL_DRIVER_CHECK

3.2 Do a pristine build.

Observe that mcuboot_secondary is now placed in an external flash. Also, observe the increase in size of mcuboot_primary and mcuboot_secondary.

This demonstrates the benefit of using external flash to expand the non-volatile memory allocated to the application.

4. Increase the number of sectors used by MCUboot.

Lastly, we must increase the number of sectors used by MCUboot now, as the partitions are larger. See an explanation of image sectors at Using external flash memory partitions docs.

4.1 Set the following in sysbuild/mcuboot.conf:

# STEP 4.1 - Increase number of sectors
# This is 512 for the nRF54L15 but can be smaller for other chips
CONFIG_BOOT_MAX_IMG_SECTORS=512
Kconfig

5. Perform DFU on the DK to verify that the configuration is completed.

As always, we do not know if the configuration works until we have tried it.

5.1 Follow the steps in Exercise 1 – Steps 5.3-5.4 to perform DFU on the DK to verify that the configuration is complete.

v2.6.2 – v2.5.2

External flash is covered in Exercise 2 for v2.6.2 – v2.5.2

Register an account
Already have an account? Log in
(All fields are required unless specified optional)

  • 8 or more characters
  • Upper and lower case letters
  • At least one number or special character

Forgot your password?
Enter the email associated with your account, and we will send you a link to reset your password.