External Flash using SPI on nRF5340 DK

I want to use the external flash on the nRF5340 DK using SPI as the board I'm developing for will not have QSPI. I used the littlefs sample which has a lot of the settings I need, like those in the spi_flash sample:

CONFIG_STDOUT_CONSOLE=y
CONFIG_FLASH=y
CONFIG_SPI=y

Which gives me:

*** Booting nRF Connect SDK v3.5.99-ncs1-1 ***

mx25r6435f@0 SPI flash testing
==========================

Perform test on single sector
Test 1: Flash erase
Flash erase succeeded!

Test 2: Flash write
Attempting to write 4 bytes
Data read matches data written. Good!!

I see it's using external flash because of the flash_dev->name print.

Now if I add add CONFIG_SPI=y to the littlefs sample I get:

*** Booting nRF Connect SDK v3.5.99-ncs1-1 ***
Sample program to r/w files on littlefs
Area 5 at 0xf8000 on flash-controller@39000 for 32768 bytes
I: LittleFS version 2.5, disk version 2.0
I: FS at flash-controller@39000:0xf8000 is 8 0x1000-byte blocks with 512 cycle
I: sizes: rd 16 ; pr 16 ; ca 64 ; la 32
/lfs mount: 0
/lfs: bsize = 16 ; frsize = 4096 ; blocks = 8 ; bfree = 5

Listing dir /lfs ...
[FILE] boot_count (size = 1)
[FILE] pattern.bin (size = 547)
/lfs/boot_count read count:11 (bytes: 1)
/lfs/boot_count write new boot count 12: [wr:1]
------ FILE: /lfs/pattern.bin ------
0c 55 55 55 55 55 55 55 0d 55 55 55 55 55 55 55

I've got another ticket open for a number of things I'm trying to do but just wanted to see if I could get external flash using spi and littlefs and I'm having trouble with this.

  • Hi,

     

    Have you followed the guide explained here?

    https://docs.nordicsemi.com/bundle/ncs-latest/page/nrf/scripts/partition_manager/partition_manager.html#external_flash_memory_partitions

     

    You will need to add a pm_static.yml file with the following (change sizes etc. to your liking):

    littlefs_storage:
      address: 0x0
      device: mx25r6435f
      region: external_flash
      size: 0x400000
    external_flash:
      address: 0x400000
      device: mx25r6435f
      region: external_flash
      size: 0x400000

     

    Then add your overlay:

    /delete-node/ &storage_partition;
    
    / {
    	chosen {
    		nordic,pm-ext-flash = &mx25r64;
    	};
    };
    
    &mx25r64 {
        status = "okay";
    	partitions {
    		compatible = "fixed-partitions";
    		#address-cells = <1>;
    		#size-cells = <1>;
    
    		storage_partition: partition@0 {
    			label = "storage";
    			reg = <0x00000000 0x00010000>;
    		};
    	};
    };

     

    And the corresponding config:

    CONFIG_SPI=y
    CONFIG_SPI_NOR=y
    CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE=4096
    CONFIG_FLASH=y
    CONFIG_PM_OVERRIDE_EXTERNAL_DRIVER_CHECK=y
    CONFIG_PM_PARTITION_REGION_LITTLEFS_EXTERNAL=y

     

    Then it should run towards ext flash:

    *** Booting nRF Connect SDK v2.8.0-a2386bfc8401 ***
    *** Using Zephyr OS v3.7.99-0bc3393fb112 ***
    Sample program to r/w files on littlefs
    Area 0 at 0x0 on mx25r6435f@1 for 4194304 bytes
    I: LittleFS version 2.9, disk version 2.1
    I: FS at mx25r6435f@1:0x0 is 1024 0x1000-byte blocks with 512 cycle
    I: sizes: rd 16 ; pr 16 ; ca 64 ; la 32
    E: WEST_TOPDIR/modules/fs/littlefs/lfs.c:1374: Corrupted dir pair at {0x1, 0x0}
    W: can't mount (LFS -84); formatting
    I: /lfs mounted
    /lfs mount: 0
    /lfs: bsize = 16 ; frsize = 4096 ; blocks = 1024 ; bfree = 1022
    
    Listing dir /lfs ...
    /lfs/boot_count read count:0 (bytes: 0)
    /lfs/boot_count write new boot count 1: [wr:1]
    I: Test file: /lfs/pattern.bin not found, create one!
    ...

     

    Kind regards,

    Håkon

  • Is this the same in nrfConnect 2.9 using sysbuild? Starting from scratch I use the Zephyr Hello World example and created a build configuration for the nrf5340 cpu app using SDK 2.9 and toolchain 2.9 and selecting sysbuild.


    I added:

    CONFIG_FLASH=y
    CONFIG_SPI=y

    Which built and flashed fine. I added:

    CONFIG_SPI_NOR=y
    and created an overlay with this:
    / {
    	chosen {
    		nordic,pm-ext-flash = &mx25r64;
    	};
    };

    when building I got this warning:


    warning: SPI_NOR (defined at soc/gaisler/gr716a/Kconfig.defconfig:18, drivers/flash/Kconfig.nor:6)
    was assigned the value 'y' but got the value 'n'. Check these unsatisfied dependencies: ((FLASH &&
    SOC_GR716A) || (DT_HAS_JEDEC_SPI_NOR_ENABLED && FLASH)) (=n). See
    docs.zephyrproject.org/.../kconfig.html and/or look up SPI_NOR in the
    menuconfig/guiconfig interface. The Application Development Primer, Setting Configuration Values,
    and Kconfig - Tips and Best Practices sections of the manual might be helpful too.

    I see in this post that:

    The external flash on the nRF52840 is by default configured to use QSPI and not SPI.

    Which is the same for the nRF5340 DK

    &qspi {
    	status = "okay";
    	pinctrl-0 = <&qspi_default>;
    	pinctrl-1 = <&qspi_sleep>;
    	pinctrl-names = "default", "sleep";
    	mx25r64: mx25r6435f@0 {
    		compatible = "nordic,qspi-nor";
    		reg = <0>;
    		/* MX25R64 supports only pp and pp4io */
    		writeoc = "pp4io";
    		/* MX25R64 supports all readoc options */
    		readoc = "read4io";
    		sck-frequency = <8000000>;
    		jedec-id = [c2 28 17];
    		sfdp-bfp = [
    			e5 20 f1 ff  ff ff ff 03  44 eb 08 6b  08 3b 04 bb
    			ee ff ff ff  ff ff 00 ff  ff ff 00 ff  0c 20 0f 52
    			10 d8 00 ff  23 72 f5 00  82 ed 04 cc  44 83 68 44
    			30 b0 30 b0  f7 c4 d5 5c  00 be 29 ff  f0 d0 ff ff
    		];
    		size = <67108864>;
    		has-dpd;
    		t-enter-dpd = <10000>;
    		t-exit-dpd = <35000>;
    	};
    };

    I read this post on "ncs external flash ota change qspi to spi" using the nRF52840dk which had sample code in the solution with this in the overlay:

    / {
    	chosen {
    		nordic,pm-ext-flash = &mx25r64test;
    	};
    };
    &qspi {
    	status = "disabled";
    };
    
    &spi3 {
    	status = "okay";
    	sck-pin = <19>;
    	mosi-pin = <20>;
    	miso-pin = <21>;
    	cs-gpios = <&gpio0 17 GPIO_ACTIVE_LOW>;
    	mx25r64test: mx25r6435ftest@0 {
    		compatible = "jedec,spi-nor";
    		reg = <0>;
    		spi-max-frequency = <8000000>;
    		label = "MX25R64TEST";
    		jedec-id = [c2 28 17];
    		sfdp-bfp = [
    			e5 20 f1 ff  ff ff ff 03  44 eb 08 6b  08 3b 04 bb
    			ee ff ff ff  ff ff 00 ff  ff ff 00 ff  0c 20 0f 52
    			10 d8 00 ff  23 72 f5 00  82 ed 04 cc  44 83 68 44
    			30 b0 30 b0  f7 c4 d5 5c  00 be 29 ff  f0 d0 ff ff
    		];
    		size = <67108864>;
    		has-dpd;
    		t-enter-dpd = <10000>;
    		t-exit-dpd = <35000>;
    	};
    };

    But it didn't work for nRF5340dk. How do I make it SPI? Is this done in the overlay? I see that it supports SPI:

    The nRF5340 DK has a 64 megabit external flash memory. The memory is a multi-I/O memory supporting both Serial Peripheral Interface (SPI) and Quad Serial Peripheral Interface (QSPI).

  • Hi,

     

    In addition to the application-wise setup, you need to setup sysbuild.conf with specific entries, in this case:

    SB_CONFIG_BOOTLOADER_MCUBOOT=y
    SB_CONFIG_PM_EXTERNAL_FLASH_MCUBOOT_SECONDARY=n
    SB_CONFIG_PM_OVERRIDE_EXTERNAL_DRIVER_CHECK=y

    and specify the partition in a pm_static.yml:

     

    littlefs_storage:
      address: 0x0
      device: mx25r6435f
      end_address: 0x800000
      region: external_flash
      size: 0x800000

     

    Since the nrf5340dk has the flash on the qspi peripheral, you need to delete it first:

    /delete-node/ &mx25r64;
    /delete-node/ &storage_partition;
    
    ..rest of file

     

    Here's an example for your convenience:

    littlefs_ext_flash_sysbuild_nrf5340dk.zip

     

    Kind regards,

    Håkon

Related