zephyrproject-rtos / zephyr

Primary Git Repository for the Zephyr Project. Zephyr is a new generation, scalable, optimized, secure RTOS for multiple hardware architectures.
https://docs.zephyrproject.org
Apache License 2.0
10.7k stars 6.54k forks source link

format w25qxx nor flash failed:flashdisk: Error -22 getting page info at offset 40000 #76779

Closed MY201314MY closed 2 months ago

MY201314MY commented 2 months ago

Describe the bug I did not find any examples of mounting a FATFS file system based on W25QXX NOR FLASH in the samples/subsys/fs directory, so I made some try, but the formatting always failed. Has anyone successfully formatted or mounted it?

[00:00:00.004,000] <dbg> fs: fs_register: fs register 0: 0
*** Booting Zephyr OS build v3.7.0-3-g5cca4d59e1d5 ***
[00:00:00.004,000] <dbg> disk: disk_access_register: disk interface(NAND) registered
[00:00:00.004,000] <inf> main: w25qxx id : 0xEF 0x40 0x15
[00:00:01.004,000] <inf> flashdisk: Initialize device NAND
[00:00:01.004,000] <inf> flashdisk: offset 0, sector size 4096, page size 65536, volume size 2097152
[00:00:01.004,000] <err> flashdisk: Error -22 getting page info at offset 40000
[00:00:01.004,000] <err> fs: mkfs error (-5)
[00:00:01.004,000] <err> main: Format failed

To Reproduce 1.check zephyr version

commit 36940db938a8f4a1e919496793ed439850a221c2 (tag: v3.7.0, origin/v3.7-branch, esp-at)
Author: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
Date:   Thu Jul 18 10:13:57 2024 +0200

    release: Zephyr v3.7.0

    Set the version to v3.7.0

    Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>

2.prj.conf

CONFIG_LOG=y

CONFIG_FLASH=y
CONFIG_FLASH_MAP=y

CONFIG_FILE_SYSTEM=y
CONFIG_FILE_SYSTEM_MKFS=y
CONFIG_FS_FATFS_MOUNT_MKFS=y
CONFIG_FAT_FILESYSTEM_ELM=y

CONFIG_SHELL=y
CONFIG_FLASH=y
CONFIG_FLASH_JESD216_API=y
CONFIG_FLASH_SHELL=y

CONFIG_HEAP_MEM_POOL_SIZE=2048
CONFIG_FILE_SYSTEM_SHELL=y
CONFIG_FLASH_MAP_SHELL=y
CONFIG_FS_LOG_LEVEL_DBG=y
CONFIG_DISK_LOG_LEVEL_DBG=y
CONFIG_DISK_DRIVER_FLASH=y
CONFIG_FLASHDISK_LOG_LEVEL_DBG=y

CONFIG_MAIN_STACK_SIZE=10240

3.stm32f4_disco.overlay

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

&spi2 {
    pinctrl-0 = < &spi2_sck_xxxx &spi2_mosi_xxxx &spi2_miso_xxxx &spi2_nss_xxxx>;
    pinctrl-names = "default";
    status = "okay";

    w25q16bv: w25q16bv@0 {
        compatible = "jedec,spi-nor";
        reg = <0>;
        spi-max-frequency = <DT_SIZE_M(32)>;
        size = <DT_SIZE_M(2)> ;
        jedec-id = [ef 40 15];
        has-dpd;
        t-enter-dpd = <3500>;
        t-exit-dpd = <3500>;

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

            fatfs_storage_partition: partition@0 {
                label = "fatfs";
                reg = <0x00000000 DT_SIZE_M(2)>;
            };
        };
    };
};

4.main.c

/*
 * Copyright (c) 2022 Antmicro <www.antmicro.com>
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/fs/fs.h>
#include <zephyr/drivers/flash.h>
#include <zephyr/logging/log.h>
#include <zephyr/storage/flash_map.h>

#include <zephyr/storage/disk_access.h>
#include <ff.h>

#define MKFS_FS_TYPE FS_FATFS
#define MKFS_DEV_ID "NAND:"
#define MKFS_FLAGS 0

LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG);

static const struct device *w25qxx = DEVICE_DT_GET(DT_NODELABEL(w25q16bv));

int main(void)
{
    uint8_t id[3] = {0};
    flash_read_jedec_id(w25qxx, id);
    LOG_INF("w25qxx id : 0x%02X 0x%02X 0x%02X", id[0], id[1], id[2]);

    int rc;
    k_sleep(K_SECONDS(1));
    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;
}

5.build & flash

# But any board should be the same 
west build -b stm32f4_disco
west flash

Additional information

uart:~$ flash_map list
ID | Device     | Device Name               | Label                   | Offset     | Size
-------------------------------------------------------------------------------------------
 1   0x08011ff4   flash-controller@40022000   mcuboot                   0x0          0x10000       
 2   0x08011ff4   flash-controller@40022000   image-0                   0x10000      0xf0000       
 3   0x08011ff4   flash-controller@40022000   image-1                   0x100000     0xf0000       
 4   0x08011ff4   flash-controller@40022000   storage                   0x1f0000     0x10000       
 0   0x0801201c   w25q16bv@0                  fatfs                     0x0          0x200000      
uart:~$
de-nordic commented 2 months ago

image https://github.com/zephyrproject-rtos/zephyr/blob/d590c18672845e63fc1b49d917113d52b2e1d771/dts/bindings/mtd/jedec%2Cjesd216.yaml#L25-L27

And you probably want to configure CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE to 4096 rather than default 65k, the smallest possible erase sector for the device.

MY201314MY commented 2 months ago

OK,let me update my config and overlay. Thank you very much.

pri.conf

CONFIG_LOG=y
CONFIG_LOG_MODE_IMMEDIATE=y

CONFIG_FLASH=y
CONFIG_FLASH_MAP=y

CONFIG_FILE_SYSTEM=y
CONFIG_FILE_SYSTEM_MKFS=y
CONFIG_FS_FATFS_MOUNT_MKFS=y
CONFIG_FAT_FILESYSTEM_ELM=y

CONFIG_SHELL=y
CONFIG_FLASH=y
CONFIG_FLASH_JESD216_API=y
CONFIG_SPI_NOR_SFDP_RUNTIME=y
CONFIG_SPI_NOR_SFDP_MINIMAL=n
CONFIG_FLASH_SHELL=y

CONFIG_HEAP_MEM_POOL_SIZE=2048
CONFIG_FILE_SYSTEM_SHELL=y
CONFIG_FLASH_MAP_SHELL=y
CONFIG_FS_LOG_LEVEL_DBG=y
CONFIG_DISK_LOG_LEVEL_DBG=y
CONFIG_DISK_DRIVER_FLASH=y
CONFIG_FLASHDISK_LOG_LEVEL_DBG=y
CONFIG_FLASH_LOG_LEVEL_DBG=y
CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE=4096

CONFIG_MAIN_STACK_SIZE=10240

stm32f4_disco.overlay

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

&spi2 {
    status = "okay";

    w25q16bv: w25q16bv@0 {
        compatible = "jedec,spi-nor";
        reg = <0>;
        spi-max-frequency = <DT_SIZE_M(32)>;
        size = <DT_SIZE_M(16)> ;
        jedec-id = [ef 40 15];
        has-dpd;
        t-enter-dpd = <3500>;
        t-exit-dpd = <3500>;

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

            fatfs_storage_partition: partition@0 {
                label = "fatfs";
                reg = <0x00000000 DT_SIZE_M(2)>;
            };
        };
    };
};

operation log

[00:00:01.000,000] <inf> flashdisk: Initialize device NAND
[00:00:01.008,000] <inf> flashdisk: offset 0, sector size 512, page size 4096, volume size 2097152
[00:00:01.368,000] <inf> main: Format successful
uart:~$ fs mount fat /NAND:
[00:00:19.109,000] <dbg> fs: fs_mount: fs mounted at /NAND:
Successfully mounted fat fs:/NAND:
uart:~$ fs write /NAND:/123.txt 01 04 08
[00:00:27.733,000] <dbg> flashdisk: disk_flash_access_status: status : okay
[00:00:27.741,000] <dbg> flashdisk: disk_flash_access_status: status : okay
[00:00:27.748,000] <dbg> flashdisk: disk_flash_access_status: status : okay
[00:00:27.828,000] <dbg> flashdisk: disk_flash_access_status: status : okay
[00:00:28.024,000] <dbg> flashdisk: disk_flash_access_status: status : okay
uart:~$ fs read /NAND:/123.txt
[00:00:37.172,000] <dbg> flashdisk: disk_flash_access_status: status : okay
File size: 3
[00:00:37.181,000] <dbg> flashdisk: disk_flash_access_status: status : okay
[00:00:37.188,000] <dbg> flashdisk: disk_flash_access_status: status : okay
00000000  01 04 08                                         ...
[00:00:37.202,000] <dbg> flashdisk: disk_flash_access_status: status : okay
[00:00:37.209,000] <dbg> flashdisk: disk_flash_access_status: status : okay
[00:00:37.216,000] <dbg> flashdisk: disk_flash_access_status: status : okay
uart:~$ fs ls /NAND:
[00:00:46.519,000] <dbg> flashdisk: disk_flash_access_status: status : okay
[00:00:46.526,000] <dbg> flashdisk: disk_flash_access_status: status : okay
123.TXT
[00:00:46.534,000] <dbg> flashdisk: disk_flash_access_status: status : okay
[00:00:46.542,000] <dbg> flashdisk: disk_flash_access_status: status : okay
uart:~$ fs statvfs /NAND:/123.TXT
[00:01:10.321,000] <dbg> flashdisk: disk_flash_access_status: status : okay
bsize 512, frsize 1024, blocks 2028, bfree 97
uart:~$ fs statvfs /NAND:
[00:01:14.854,000] <dbg> flashdisk: disk_flash_access_status: status : okay
bsize 512, frsize 1024, blocks 2028, bfree 97
de-nordic commented 2 months ago

Thanks for log @MY201314MY. Do you consider issue resolved? If yes, then close the issue. Thanks.