LittleFS on external spi flash

I am trying to format an external flash chip with littlefs, but keep getting an MPU fault.  I can erase the chip, but not mount or format.  I have tried writing and reading to the chip using flash_area_read/write at different areas in the chip and it goes well.  I've read several posts here and tried them, but didn't work.

I'm using a small board from HolyIOT with the nrf52832 chip and a W25Q32JV Winbond chip using SPI.

nRF Connect SDK v2.6.1

Zephyr version: 3.5.99 (C:/ncs/v2.6.1/zephyr), build: v3.5.99-ncs1-1

VSCode v1.88.1

nRF Connect for VSCode v2024.3.25

// Copyright (c) 2024 Nordic Semiconductor ASA
// SPDX-License-Identifier: Apache-2.0

/dts-v1/;
//#include <nordic/nrf52832_ciaa.dtsi>
#include <nordic/nrf52832_qfaa.dtsi>

/ {
	model = "ArrowOne_V3";
	compatible = "denis,arrowone-v3";

	chosen {
		zephyr,sram = &sram0;
		zephyr,flash = &flash0;
	};

	cspins {
		compatible = "gpio-leds";
		csflash: csflash {
			label = "CS_FLASH";
			gpios = <&gpio0 12 0>;
		};
	};
};

&gpiote {
	status = "okay";
};

&gpio0 {
	status = "okay";
};

&pinctrl {
	spi2_default: spi2_default {
		group1 {
			psels = <NRF_PSEL(SPIM_SCK, 0, 21)>,
				<NRF_PSEL(SPIM_MOSI, 0, 18)>,
				<NRF_PSEL(SPIM_MISO, 0, 15)>;
		};
	};

	spi2_sleep: spi2_sleep {
		group1 {
			psels = <NRF_PSEL(SPIM_SCK, 0, 21)>,
				<NRF_PSEL(SPIM_MOSI, 0, 18)>,
				<NRF_PSEL(SPIM_MISO, 0, 15)>;
		};
	};
};

&spi2 {
	compatible = "nordic,nrf-spi";
	status = "okay";
	pinctrl-0 = <&spi2_default>;
	pinctrl-1 = <&spi2_sleep>;
	pinctrl-names = "default", "sleep";

	cs-gpios = <&gpio0 12 GPIO_ACTIVE_LOW>;

 	spiflash: spiflash@0 {
		compatible = "jedec,spi-nor";
		status = "okay";
		reg = <0>;
		spi-max-frequency = <1000000U>;
		jedec-id = [ef 40 16];
		t-enter-dpd=<3000>;
		t-exit-dpd=<3000>;
		size = <0x2000000>; //bits, not bytes!

		partitions {
			compatible="fixed-partitions";
			#address-cells = <1>;
			#size-cells = <1>;
	
			lfs1_part: partition@0 {
				label = "littlefs_storage";
				reg = <0x00000000 0x00400000>; //bytes
			};
		};
	
	};

};

/* These aliases are provided for compatibility with samples */
/ {
	aliases {
		csflash = &csflash;
		spiflash = &spiflash;
	};
};
	

&flash0 {
	partitions {
		compatible = "fixed-partitions";
		#address-cells = <1>;
		#size-cells = <1>;

		boot_partition: partition@0 {
			label = "mcuboot";
			reg = <0x0 0xc000>;
		};
		slot0_partition: partition@c000 {
			label = "image-0";
			reg = <0xc000 0x32000>;
		};
		slot1_partition: partition@3e000 {
			label = "image-1";
			reg = <0x3e000 0x32000>;
		};
		scratch_partition: partition@70000 {
			label = "image-scratch";
			reg = <0x70000 0xa000>;
		};
		storage_partition: partition@7a000 {
			label = "storage";
			reg = <0x7a000 0x6000>;
		};
	};
};

&ficr {
	status = "okay";
};

#math library?
CONFIG_NEWLIB_LIBC=y

#No external low frequency crystal
CONFIG_CLOCK_CONTROL_NRF_K32SRC_RC=y
CONFIG_CLOCK_CONTROL_NRF_K32SRC_XTAL=n
CONFIG_GPIO_AS_PINRESET=n


CONFIG_GPIO=y
CONFIG_GPIO_AS_PINRESET=n

# Make sure printk is printing to the UART console
CONFIG_CONSOLE=y

CONFIG_HEAP_MEM_POOL_SIZE=2048

CONFIG_FPU=y
CONFIG_CBPRINTF_FP_SUPPORT=y

# Config logger
CONFIG_LOG=y
CONFIG_USE_SEGGER_RTT=y
CONFIG_LOG_BACKEND_RTT=y
CONFIG_LOG_PRINTK=y
CONFIG_RTT_CONSOLE=y
CONFIG_UART_CONSOLE=n

#Flash NOR
CONFIG_SPI=y
CONFIG_SPI_NOR=y
CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE=4096
CONFIG_FLASH_JESD216_API=y

CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
CONFIG_FLASH_PAGE_LAYOUT=y
#CONFIG_NVS=y

#littlefs file system
CONFIG_FILE_SYSTEM=y
CONFIG_FILE_SYSTEM_MKFS=y
CONFIG_FILE_SYSTEM_LITTLEFS=y

#partition manager settings
CONFIG_PM_EXTERNAL_FLASH_MCUBOOT_SECONDARY=n

#include <zephyr/kernel.h>

#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <soc.h>

#include <zephyr/fs/fs.h>
#include <zephyr/fs/littlefs.h>
#include <zephyr/storage/flash_map.h>
#include <zephyr/settings/settings.h>
#include <zephyr/logging/log.h>

FS_LITTLEFS_DECLARE_DEFAULT_CONFIG(flash_storage);
static struct fs_mount_t lfs_storage_mnt = {
	.type = FS_LITTLEFS,
	.fs_data = &flash_storage,
	.storage_dev = (void *)FIXED_PARTITION_ID(lfs1_part),
	.mnt_point = "/lfs",
};

#define MKFS_FS_TYPE FS_LITTLEFS
#define MKFS_DEV_ID FIXED_PARTITION_ID(lfs1_part)
#define MKFS_FLAGS 0

LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG);

int formatFS(void)
{
	int rc;

	rc = fs_mkfs(MKFS_FS_TYPE, (uintptr_t)MKFS_DEV_ID, NULL, MKFS_FLAGS);

	if (rc < 0) {
		LOG_ERR("Format failed");
		return 0;
	}

	LOG_INF("Format successful");
	return 0;
}

int eraseFlash()
{
	int rc;
	struct fs_mount_t *mp = &lfs_storage_mnt;
	unsigned int id = (uintptr_t)mp->storage_dev;
	const struct flash_area *pfa;

	rc = flash_area_open(id, &pfa);
	if (rc < 0)
	{
		printk("FAIL: unable to find flash area %u: %d\n", id, rc);
		return 1;
	}

	printk("Area %u at 0x%x for %u bytes\n", id, (unsigned int)pfa->fa_off, (unsigned int)pfa->fa_size);

	rc = flash_area_erase(pfa, 0, pfa->fa_size);
	printk("flash erase: %d\n", rc);

	flash_area_close(pfa);

	return 0;
}


int main(void)
{
	printk("\nerasing flash\n");
	eraseFlash();
	printk("Done.\n\n");

	printk("formating flash\n");
	formatFS();
	printk("Done.\n\n");

	printk("Starting main routine\n\n");

	while(1)
	{
		k_sleep(K_MSEC(50));
	}

	return 0;
}

SEGGER J-Link V7.80c - Real time terminal output
J-Link EDU Mini V1 compiled Sep 22 2022 14:55:40 V1.0, SN=801020502
Process: JLink.exe
[00:00:00.000,030] <wrn> spi_nor: Waiting until flash is ready
*** Booting nRF Connect SDK v3.5.99-ncs1-1 ***

erasing flash
Area 5 at 0x0 for 4194304 bytes
flash erase: 0
Done.

formating flash
[00:00:25.720,336] <inf> littlefs: LittleFS version 2.5, disk version 2.0
[00:00:25.721,282] <inf> littlefs: FS at spiflash@0:0x0 is 1024 0x1000-byte blocks with 512 cycle
[00:00:25.721,282] <inf> littlefs: sizes: rd 16 ; pr 16 ; ca 64 ; la 32
[00:00:25.721,588] <err> os: ***** MPU FAULT *****
[00:00:25.721,588] <err> os:   Stacking error (context area might be not valid)
[00:00:25.721,588] <err> os:   Data Access Violation
[00:00:25.721,618] <err> os:   MMFAR Address: 0x2000216c
[00:00:25.721,649] <err> os: r0/a1:  0xffdc5036  r1/a2:  0x90234921  r2/a3:  0xf7a0bb89
[00:00:25.721,649] <err> os: r3/a4:  0x8c2f424f r12/ip:  0xcbb6f52a r14/lr:  0xdce5aef8
[00:00:25.721,679] <err> os:  xpsr:  0xc92d0000
[00:00:25.721,679] <err> os: s[ 0]:  0x651fdac5  s[ 1]:  0x4bcb40c8  s[ 2]:  0x6cb4902d  s[ 3]:  0xea9b53c3
[00:00:25.721,710] <err> os: s[ 4]:  0xd7d6866c  s[ 5]:  0xc82cc575  s[ 6]:  0x774b11ad  s[ 7]:  0x116042b5
[00:00:25.721,710] <err> os: s[ 8]:  0xcb352f2f  s[ 9]:  0x8c9f08e0  s[10]:  0x6644993f  s[11]:  0xb07a1cd8
[00:00:25.721,740] <err> os: s[12]:  0xc77a3048  s[13]:  0xa48f49fa  s[14]:  0xf346350e  s[15]:  0x65346337
[00:00:25.721,740] <err> os: fpscr:  0xfc9fc82a
[00:00:25.721,740] <err> os: Faulting instruction address (r15/pc): 0x40f822a2
[00:00:25.721,801] <err> os: >>> ZEPHYR FATAL ERROR 2: Stack overflow on CPU 0
[00:00:25.721,832] <err> os: Current thread: 0x200009c0 (main)
[00:00:26.292,327] <err> fatal_error: Resetting system
*** Booting nRF Connect SDK v3.5.99-ncs1-1 ***

erasing flash
Area 5 at 0x0 for 4194304 bytes

Related