Interfacing FAT file system into WinbondW25Q02 using SPI-nor driver

Greetings,

I have been trying to integrate file system into Winbond W25Q02 NOR storage, Initially I tested the hardware by interfacing SPI communication between nrf 5340 and winbond and I was able to read JEDEC id, write characters into various pages and read back from them, so hardware is NOT the problem. Now I saw that zephyr allows us to integrate file system and there is a library called SPI nor, which is heavily inspired by Winbond. Before jumping into my storage for testing file system, I tested a "Mass" sample which I found in nordic SDK. it follows QSPI protocol, and I was able to mount the mx25r64 storage, which is already present inside nrf dk. Now when I use the same code, with modifications for communication (instead of QSPI, I need SPI),I get the error saying JEDEC id 00 00 00  does not match my given id in the overlay i.e ef 70 22. I just edited prj.config and overlay file in the mass sample for this purpose and I am really not sure if I need to make any changes on main.c or spi-nor.c driver for this. 

This is my overlay file

/*
 * Copyright (c) 2020 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */
// / {
// 	aliases {
// 		spi-flash0 = &winbond;
// 	};
// };
/* Disable conflicting buttons and UART */
&button2 {
	status = "disabled";
};

&button3 {
	status = "disabled";
};

&gpio_fwd {
	status = "disabled";
};

&pinctrl {
		spi_master_default: spi_master_default {
				group1 {
						psels = <NRF_PSEL(SPIM_SCK, 0, 2)>,
										<NRF_PSEL(SPIM_MOSI, 0, 9)>,
										<NRF_PSEL(SPIM_MISO, 0, 3)>;
				};
		};

		spi_master_sleep: spi_master_sleep {
				group1 {
						psels = <NRF_PSEL(SPIM_SCK, 0, 2)>,
										<NRF_PSEL(SPIM_MOSI, 0, 9)>,
										<NRF_PSEL(SPIM_MISO, 0, 3)>;
						low-power-enable;
				};
		};
};

&spi4 {
		compatible = "nordic,nrf-spim";
		pinctrl-0 = <&spi_master_default>;
		pinctrl-1 = <&spi_master_sleep>;
		pinctrl-names = "default", "sleep";
		cs-gpios = <&gpio0 11 GPIO_ACTIVE_LOW>;	
		status = "okay";
		winbond:winbondw25q02@0 {
			compatible = "jedec,spi-nor";
			reg = <0>;
	//      irq-gpios = <&gpio0 21 0>;
			spi-max-frequency = <32000000>;
			//label = "WINBOND";
			jedec-id = [ ef 70 22 ];
			size = < 0x10000000 >;
			has-dpd;
			// 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
		 	// ];
		};
};

/delete-node/ &storage_partition;

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

		storage_partition: partition@0 {
			label = "storage";
			reg = <0x00000000 0x00020000>;
		};
	};
};

/ {
	msc_disk0 {
		compatible = "zephyr,flash-disk";
		partition = <&storage_partition>;
		disk-name = "NAND";
		cache-size = <4096>;
	};
};

This is my prj.config file

CONFIG_STDOUT_CONSOLE=y

#USB related configs
CONFIG_USB_DEVICE_STACK=y
CONFIG_USB_DEVICE_PRODUCT="Zephyr MSC sample"
CONFIG_USB_DEVICE_PID=0x0008
CONFIG_LOG=y
CONFIG_USB_DRIVER_LOG_LEVEL_ERR=y
CONFIG_USB_MASS_STORAGE=y
CONFIG_USB_DEVICE_LOG_LEVEL_ERR=y
CONFIG_USB_MASS_STORAGE_LOG_LEVEL_ERR=y
CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n
CONFIG_APP_MSC_STORAGE_FLASH_FATFS=y
CONFIG_MAIN_STACK_SIZE=2048
CONFIG_FILE_SYSTEM=y
CONFIG_FILE_SYSTEM_LITTLEFS=n
CONFIG_SPI=y
CONFIG_SPI_NOR=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE=4096
CONFIG_FLASH_JESD216_API=y
CONFIG_FLASH_LOG_LEVEL_DBG=y
CONFIG_SPI_LOG_LEVEL_DBG=y
CONFIG_SPI_NRFX=y
CONFIG_SPI_ASYNC=y
CONFIG_SPI_SLAVE=y


CONFIG_SPI=y
#CONFIG_SPI_4=y
CONFIG_SPI_NOR=y
CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE=4096
CONFIG_SPI_NOR_IDLE_IN_DPD=y

#FLASH Config ##############################################
CONFIG_DISK_ACCESS=y
CONFIG_FILE_SYSTEM=y
CONFIG_FAT_FILESYSTEM_ELM=y
CONFIG_FS_FATFS_LFN=y

CONFIG_SETTINGS_FS=y

CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_FLASH_LOG_LEVEL_ERR=y
CONFIG_MPU_ALLOW_FLASH_WRITE=y
CONFIG_DISK_DRIVER_FLASH=y

This is my terminal

*** Booting Zephyr OS build 28a3fca7da5c ***
[00:00:00.257,751] <err> flashdisk: Flash area 0 open error -19
[00:00:00.257,751] <err> usb_msc: Storage init ERROR !!!! - Aborting USB init
[00:00:00.257,781] <err> os: ***** BUS FAULT *****
[00:00:00.257,812] <err> os:   Precise data bus error
[00:00:00.257,812] <err> os:   BFAR Address: 0xef8008f3
[00:00:00.257,843] <err> os: r0/a1:  0x00000000  r1/a2:  0x00013384  r2/a3:  0x00000000
[00:00:00.257,843] <err> os: r3/a4:  0xef8008f3 r12/ip:  0xfffffffc r14/lr:  0x00000883
[00:00:00.257,873] <err> os:  xpsr:  0x69000000
[00:00:00.257,873] <err> os: Faulting instruction address (r15/pc): 0x0000f6c0
[00:00:00.257,904] <err> os: >>> ZEPHYR FATAL ERROR 25: Unknown error on CPU 0
[00:00:00.257,965] <err> os: Current thread: 0x20000b30 (unknown)
[00:00:00.257,110] <dbg> spi_nrfx_spim: spi_context_update_tx: tx buf/len (nil)/0
[00:00:00.257,110] <dbg> spi_nrfx_spim: finish_transaction: Transaction finished with status 0
--- 17 messages dropped ---
[00:00:00.257,141] <dbg> spi_nrfx_spim: spi_context_buffers_setup: tx_bufs 0x20005418 - rx_bufs 0x20005420 - 1
[00:00:00.257,171] <dbg> spi_nrfx_spim: spi_context_buffers_setup: current_tx 0x20005428 (2), current_rx 0x20005428 (2), tx buf/len 0x20005410/1, rx buf/len 0x20005410/1
[00:00:00.257,171] <dbg> spi_nrfx_spim: spi_context_update_tx: tx buf/len 0x20005478/3
[00:00:00.257,202] <dbg> spi_nrfx_spim: spi_context_update_rx: rx buf/len 0x20005478/3
[00:00:00.257,232] <dbg> spi_nrfx_spim: spi_context_update_tx: tx buf/len (nil)/0
[00:00:00.257,232] <dbg> spi_nrfx_spim: spi_context_update_rx: rx buf/len (nil)/0
[00:00:00.257,263] <dbg> spi_nrfx_spim: finish_transaction: Transaction finished with status 0
[00:00:00.257,293] <dbg> spi_nrfx_spim: spi_context_buffers_setup: tx_bufs 0x200053f8 - rx_bufs (nil) - 1
[00:00:00.257,293] <dbg> spi_nrfx_spim: spi_context_buffers_setup: current_tx 0x20005408 (1), current_rx (nil) (0), tx buf/len 0x200053f0/1, rx buf/len (nil)/0
[00:00:00.257,324] <dbg> spi_nrfx_spim: spi_context_update_tx: tx buf/len (nil)/0
[00:00:00.257,354] <dbg> spi_nrfx_spim: finish_transaction: Transaction finished with status 0
[00:00:00.257,385] <err> spi_nor: Device id 00 00 00 does not match config ef 70 22

file:///C:/Users/DELL/Desktop/datasheet/W25Q02JV_DTR_RevB_04192021-2329042.pdf

This is the link to winbond datasheet that I am using. Any help would be much appreciated!

  • Hi,

    The pins you've selected (P0.02 and P0.03) are among the few GPIOs are reserved for NFC. If you consult with the pin assignment in the product specification you can see which pins you can use for general purpose input that you can select yourself and which are reserved for others: https://docs.nordicsemi.com/bundle/ps_nrf5340/page/chapters/pin.html

    Try with for instance P0.07 and P0.08 instead (or any other available that you want to use)

    Kind regards,
    Andreas

  • Hi,

    Actually I am using Fanstel BT 40F which has nrf 5340 soc. I have tested that those two pins works fine as a GPIO. Like I mentioned above, I have done basic reading, writing and erasing using spi communication with this storage on those pins. It is the file system inplementation that I am not able to do.

    Regards,

    Barsha

  • Hi Barsha, 

    The suggestion still stands. The 5340SoC uses P0.02 and P0.03 for the NFC pins.

    Let me know if you see the same thing when changing the GPIO's and we'll continue from there :)

    Kind regards,
    Andreas

  • Hi AHaug,

    That was helpful thank you very much. However I used this 

    CONFIG_NFCT_PINS_AS_GPIOS=y
    in my prj config and now I am able to mount file system using same pin configurations since I have already given my PCB for printing using those pins. 
    Also, although I am able to mount the file system when I use partition size below 30 mB, it mounts but once I try to go beyond that it shows mount error. Could you help me with this?
    Here is my overlay
    /*
     * Copyright (c) 2020 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    // / {
    // 	aliases {
    // 		spi-flash0 = &winbond;
    // 	};
    // };
    /* Disable conflicting buttons and UART */
    &button2 {
    	status = "disabled";
    };
    
    &button3 {
    	status = "disabled";
    };
    
    &gpio_fwd {
    	status = "disabled";
    };
    
    &pinctrl {
    		spi_master_default: spi_master_default {
    				group1 {
    						psels = <NRF_PSEL(SPIM_SCK, 0, 2)>,
    										<NRF_PSEL(SPIM_MOSI, 0, 9)>,
    										<NRF_PSEL(SPIM_MISO, 0, 3)>;
    				};
    		};
    
    		spi_master_sleep: spi_master_sleep {
    				group1 {
    						psels = <NRF_PSEL(SPIM_SCK, 0, 2)>,
    										<NRF_PSEL(SPIM_MOSI, 0, 9)>,
    										<NRF_PSEL(SPIM_MISO, 0, 3)>;
    						low-power-enable;
    				};
    		};
    };
    
    &spi4 {
    		compatible = "nordic,nrf-spim";
    		pinctrl-0 = <&spi_master_default>;
    		pinctrl-1 = <&spi_master_sleep>;
    		pinctrl-names = "default", "sleep";
    		cs-gpios = <&gpio0 11 GPIO_ACTIVE_LOW>;	
    		status = "okay";
    		winbond:winbondw25q02@0 {
    			compatible = "jedec,spi-nor";
    			reg = <0>;
    	//      irq-gpios = <&gpio0 21 0>;
    			spi-max-frequency = <32000000>;
    			//label = "WINBOND";
    			jedec-id = [ ef 70 22 ];
    			size = < 268435456 >;
    			has-dpd;
    			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
    		 	];
    			 t-enter-dpd = <10000>;
    			 t-exit-dpd = <35000>;
    			 
    		};
    };
    
    /delete-node/ &storage_partition;
    
    &winbond {
    	partitions {
    		compatible = "fixed-partitions";
    		#address-cells = <1>;
    		#size-cells = <1>;
    
    		storage_partition: partition@0 {
    			label = "storage";
    			reg = <0x00000000 0x08000000>;  // 128 MB
    		};
    	};
    };
    
    / {
    	msc_disk0 {
    		compatible = "zephyr,flash-disk";
    		partition = <&storage_partition>;
    		disk-name = "NAND";
    		cache-size = <4096>;
    	};
    };
    
    
    
    Here is the error I am getting
    [00:00:00.273,406] <err> usb_msc: Storage init ERROR !!!! - Aborting USB init
    Area 0 at 0x0 on winbondw25q02@0 for 134217728 bytes
    [00:00:00.273,498] <inf> flashdisk: Initialize device NAND
    [00:00:00.273,529] <inf> flashdisk: offset 0, sector size 512, page size 4096, volume size 134217728
    [00:00:00.286,987] <err> flashdisk: Error -22 getting page info at offset 2000000
    [00:00:00.286,987] <err> fs: fs mount error (-5)
    [00:00:00.287,017] <err> main: Failed to mount filesystem
    [00:00:00.287,078] <inf> main: The device is put in USB mass storage mode.
    
    
    I tried partition size of 128mega byte here.
     
    Also,
    I am not able to see my storage as a USB device in my PC after i plug it in, I get this error and it reboots
    [00:00:00.256,744] <dbg> spi_nrfx_spim: spi_context_buffers_setup: tx_bufs 0x200053f8 - rx_bufs 0x20005400 - 1
    [00:00:00.256,805] <dbg> spi_nrfx_spim: spi_context_update_rx: rx buf/len 0x2000543f/1
    --- 2 messages dropped ---
    [00:00:00.256,835] <dbg> spi_nrfx_spim: spi_context_update_tx: tx buf/len (nil)/0
    [00:00:00.256,835] <dbg> spi_nrfx_spim: spi_context_update_rx: rx buf/len (nil)/0
    [00:00:00.256,866] <dbg> spi_nrfx_spim: finish_transaction: Transaction finished with status 0
    [00:00:00.256,866] <dbg> spi_nrfx_spim: spi_context_buffers_setup: tx_bufs 0x200053f8 - rx_bufs 0x20005400 - 1
    --- 15 messages dropped ---
    [00:00:00.274,444] <dbg> spi_nrfx_spim: spi_context_update_rx: rx buf/len 0x20000ca4/512
    [00:00:00.274,719] <dbg> spi_nrfx_spim: spi_context_update_tx: tx buf/len (nil)/0
    [00:00:00.274,749] <dbg> spi_nrfx_spim: spi_context_update_rx: rx buf/len (nil)/0
    [00:00:00.274,780] <dbg> spi_nrfx_spim: finish_transaction: Transaction finished with status 0
    Mount /NAND:: 0
    [00:00:00.324,951] <dbg> spi_nrfx_spim: spi_context_buffers_setup: tx_bufs 0x20005220 - rx_bufs 0x20005228 - 1
    [00:00:00.324,981] <dbg> spi_nrfx_spim: spi_context_buffers_setup: current_tx 0x20005230 (2), current_rx 0x20005230 (2), tx buf/len 0x20005218/4, rx buf/len 0x20005218/4
    [00:00:00.325,012] <dbg> spi_nrfx_spim: spi_context_update_tx: tx buf/len 0x20000ca4/512
    [00:00:00.325,012] <dbg> spi_nrfx_spim: spi_context_update_rx: rx buf/len 0x20000ca4/512
    [00:00:00.325,317] <dbg> spi_nrfx_spim: spi_context_update_tx: tx buf/len (nil)/0
    [00:00:00.325,317] <dbg> spi_nrfx_spim: spi_context_update_rx: rx buf/len (nil)/0
    [00:00:00.325,347] <dbg> spi_nrfx_spim: finish_transaction: Transaction finished with status 0
    /NAND:: bsize = 512 ; frsize = 512 ; blocks = 216 ; bfree = 216
    /NAND: opendir: 0
    [00:00:00.325,897] <dbg> spi_nrfx_spim: spi_context_buffers_setup: tx_bufs 0x20005138 - rx_bufs 0x20005140 - 1
    [00:00:00.325,927] <dbg> spi_nrfx_spim: spi_context_buffers_setup: current_tx 0x20005148 (2), current_rx 0x20005148 (2), tx buf/len 0x20005130/4, rx buf/len 0x20005130/4
    [00:00:00.325,958] <dbg> spi_nrfx_spim: spi_context_update_tx: tx buf/len 0x20000ca4/512
    [00:00:00.325,958] <dbg> spi_nrfx_spim: spi_context_update_rx: rx buf/len 0x20000ca4/512
    [00:00:00.326,263] <dbg> spi_nrfx_spim: spi_context_update_tx: tx buf/len (nil)/0
    [00:00:00.326,263] <dbg> spi_nrfx_spim: spi_context_update_rx: rx buf/len (nil)/0
    [00:00:00.326,293] <dbg> spi_nrfx_spim: finish_transaction: Transaction finished with status 0
    End of files
    [00:00:00.326,416] <inf> main: The device is put in USB mass storage mode.
    
    [00:01:45.822,265] <err> os: ***** USAGE FAULT *****
    [00:01:45.822,296] <err> os:   Stack overflow (context area not valid)
    [00:01:45.822,326] <err> os: r0/a1:  0x20003e20  r1/a2:  0x00000018  r2/a3:  0x00000000
    [00:01:45.822,326] <err> os: r3/a4:  0x20003ddc r12/ip:  0x00000000 r14/lr:  0x00001f09
    [00:01:45.822,326] <err> os:  xpsr:  0x61000200
    [00:01:45.822,357] <err> os: Faulting instruction address (r15/pc): 0x00000974
    [00:01:45.822,387] <err> os: >>> ZEPHYR FATAL ERROR 2: Stack overflow on CPU 0
    [00:01:45.822,418] <err> os: Current thread: 0x20000960 (unknown)
    
  • Barsa said:

    However I used this 

    CONFIG_NFCT_PINS_AS_GPIOS=y

    That's also a valid solution! :) 

    Barsa said:
    Could you help me with this?

    I'll have a look. Could you verify if this is the device you're trying to set up? https://www.winbond.com/hq/product/code-storage-flash-memory/serial-nor-flash/?__locale=en&partNo=W25Q02NW_DTR 

    Discussing this out loud with you: The size parameter in your spi instance is given in bytes represented as int and the Winbounds size is 2 Gigabits. Have you validated that you've entered the correct size?

    Moving on to fault: I see the error is on "flashdisk": Is this corresponding to the msc_disc0 nand disc-name that you've created in the partition? I'm curious to where the NAND-partition comes in on the SPI-nor type winbound device. Could you expand on this?

    Barsa said:
    I am not able to see my storage as a USB device in my PC after i plug it in, I get this error and it reboots

    You'll get the stack overflow errors whenever your stack sizes are too small (which I assume you also find obvious, but stating it nonetheless), but finding out which stack that's too small might vary from fault to fault. In this case I believe it is your main stack that might bee too small, which you set to 2k in your prj.conf i.e CONFIG_MAIN_STACK_SIZE=2048. Try to set it to 4k or 8k and see if this removes the stack overflow issue,

    Kind regards,
    Andreas

Related