VFS driver for Winbond W25N configuration trouble

Hello all, I have a board that currently supports an SD card successfully, but I have decided to replace the SD card with a Winbond W25N 1Gb NAND chip. I have wired the W25N to an Adafruit Huzzah32 (ESP32 WROOM) board and using some simple Arduino code I can write to and read from the flash chip.

I am now trying to make the W25N work with Mongoose and the provided VFS driver

https://mongoose-os.com/docs/mongoose-os/api/drivers/vfs-dev-w25xxx.md

and LFS, as described here (but with the Winbond driver):

https://mongoose-os.com/docs/mongoose-os/api/misc/fstab.md

The hardware is wired:

miso -> 19
mosi -> 18
clk -> 5
cs -> 21

I have tried many configurations, but what keeps happening is that the “fstab init failed” and so MGOS init failed: -32".

Ultimately I want a 1gb partition, but for now I can’t even make a 4MB partition for this chip.

I do notice in the last 5 lines of the output:

[Mar 15 22:25:32.546] esp32_spi_master.c:94   SPI3 init ok (MISO: 19, MOSI: 23, SCLK: 18; CS0/1/2: 5/-1/-1; native? yes)
[Mar 15 22:25:32.553] mgos_deps_init.c:72     Init fstab 1.0...
[Mar 15 22:25:32.558] mgos_vfs_dev_spi_fl:370 Failed to read JEDEC ID
[Mar 15 22:25:32.565] mgos_vfs_dev.c:66       Dev spi_flash {"cs": 21, "freq": 10000000} open failed: -7
[Mar 15 22:25:32.569] mgos_deps_init.c:74     fstab init failed
[Mar 15 22:25:32.573] mgos_freertos.c:184     MGOS init failed: -32

It thinks CS is 0/1/2 even though i specified 21.

I have stripped everything out of the code and mos.yml except what’s needed for this, and I’ll post the code and the output. If someone could tell me what I’m doing wrong I would really appreciate it. Thanks very much.

mos.yml:

author: mongoose-os
description: A Mongoose OS app skeleton
version: 1.0

libs_version: ${mos.version}
modules_version: ${mos.version}
mongoose_os_version: ${mos.version}

# Optional. List of tags for online search.
tags:
  - c

# List of files / directories with C sources. No slashes at the end of dir names.
sources:
  - src

# List of dirs. Files from these dirs will be copied to the device filesystem
filesystem:
  - fs

build_vars:
  ESP_IDF_EXTRA_PARTITION: spif0,data,spiffs,,256K

config_schema:
  - ["spi.enable", true]
  # Other SPI interface options go here.
  - ["sd_spi.pin_miso", 19]
  - ["sd_spi.pin_mosi", 18]
  - ["sd_spi.pin_clk", 5]
  - ["sd_spi.pin_cs", 21]

  - ["devtab.dev0.name", "spif0"]
  - ["devtab.dev0.type", "spi_flash"]
  - ["devtab.dev0.opts", '{"cs": 21, "freq": 10000000}']
  - ["fstab.fs0.dev", "spif0"]
  - ["fstab.fs0.type", "LFS"]

# I have tried it both with and without this line since I think these are auto detected
  - ["fstab.fs0.opts", '{"bs": 4096}']

  - ["fstab.fs0.path", "/spif0"]
  - ["fstab.fs0.create", true]

  - ["debug.level", 3]

libs:
  - origin: https://github.com/mongoose-os-libs/vfs-dev-w25xxx

  # These from fstab documentation
  - origin: https://github.com/mongoose-os-libs/fstab
  - origin: https://github.com/mongoose-os-libs/vfs-dev-spi-flash
  - origin: https://github.com/mongoose-os-libs/vfs-fs-lfs

# Used by the mos tool to catch mos binaries incompatible with this file format
manifest_version: 2017-09-29

Code:

#include "mgos.h"
#include "mgos_vfs_dev_w25xxx.h"

#define LED_PIN 13

static void timer_cb(void *arg)
{
#ifdef LED_PIN
  mgos_gpio_toggle(LED_PIN);
#endif
  (void) arg;
}

enum mgos_app_init_result mgos_app_init(void)
{
  mgos_gpio_set_mode(LED_PIN, MGOS_GPIO_MODE_OUTPUT);
  mgos_set_timer(1000, MGOS_TIMER_REPEAT, timer_cb, NULL);

  return MGOS_APP_INIT_SUCCESS;
}

Output:

[Mar 15 22:25:31.853] rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
[Mar 15 22:25:31.858] configsip: 0, SPIWP:0xee
[Mar 15 22:25:31.858] clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
[Mar 15 22:25:31.864] mode:DIO, clock div:1
[Mar 15 22:25:31.870] load:0x3fff0030,len:4
[Mar 15 22:25:31.870] load:0x3fff0034,len:6868
[Mar 15 22:25:31.870] load:0x40078000,len:13428
[Mar 15 22:25:31.875] ho 0 tail 12 room 4
[Mar 15 22:25:31.875] load:0x40080400,len:5064
[Mar 15 22:25:31.879] entry 0x400806d4
[Mar 15 22:25:31.885] e[0;32mI (31) boot: ESP-IDF 4.2-r1 2nd stage bootloadere[0m
[Mar 15 22:25:31.885] e[0;32mI (31) boot: compile time 02:24:14e[0m
[Mar 15 22:25:31.890] e[0;32mI (31) boot: chip revision: 1e[0m
[Mar 15 22:25:31.896] e[0;32mI (34) boot_comm: chip revision: 1, min. bootloader chip revision: 0e[0m
[Mar 15 22:25:31.906] e[0;32mI (41) qio_mode: Enabling default flash chip QIOe[0m
[Mar 15 22:25:31.910] e[0;32mI (46) boot.esp32: SPI Speed      : 80MHze[0m
[Mar 15 22:25:31.921] e[0;32mI (51) boot.esp32: SPI Mode       : QIOe[0m
[Mar 15 22:25:31.921] e[0;32mI (55) boot.esp32: SPI Flash Size : 4MBe[0m
[Mar 15 22:25:31.933] e[0;32mI (60) boot: Enabling RNG early entropy source...e[0m
[Mar 15 22:25:31.933] e[0;32mI (65) boot: Partition Table:e[0m
[Mar 15 22:25:31.933] e[0;32mI (69) boot: ## Label            Usage          Type ST Offset   Lengthe[0m
[Mar 15 22:25:31.940] e[0;32mI (76) boot:  0 nvs              WiFi data        01 02 00009000 00004000 00000000e[0m
[Mar 15 22:25:31.950] e[0;32mI (84) boot:  1 otadata          OTA data         01 00 0000d000 00002000 00000000e[0m
[Mar 15 22:25:31.952] e[0;32mI (93) boot:  2 app_0            OTA app          00 10 00010000 00180000 00000000e[0m
[Mar 15 22:25:31.963] e[0;32mI (101) boot:  3 fs_0             FS               01 82 00190000 00040000 00000000e[0m
[Mar 15 22:25:31.968] e[0;32mI (109) boot:  4 app_1            OTA app          00 11 001d0000 00180000 00000000e[0m
[Mar 15 22:25:31.979] e[0;32mI (117) boot:  5 fs_1             FS               01 82 00350000 00040000 00000000e[0m
[Mar 15 22:25:31.985] e[0;32mI (126) boot:  6 spif0            FS               01 82 00390000 00040000 00000000e[0m
[Mar 15 22:25:31.996] e[0;32mI (134) boot: End of partition tablee[0m
[Mar 15 22:25:32.002] e[0;32mI (138) boot: OTA data 0: seq 0x00000001, st 0x10, CRC 0x157a2b85, valid? 1e[0m
[Mar 15 22:25:32.007] e[0;32mI (146) boot: OTA data 1: seq 0x00000000, st 0x00, CRC 0x00000000, valid? 0e[0m
[Mar 15 22:25:32.013] e[0;32mI (154) boot_comm: chip revision: 1, min. application chip revision: 0e[0m
[Mar 15 22:25:32.024] e[0;32mI (161) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x16e38 ( 93752) mape[0m
[Mar 15 22:25:32.050] e[0;32mI (197) esp_image: segment 1: paddr=0x00026e60 vaddr=0x3ffb0000 size=0x01afc (  6908) loade[0m
[Mar 15 22:25:32.057] e[0;32mI (200) esp_image: segment 2: paddr=0x00028964 vaddr=0x40080000 size=0x00400 (  1024) loade[0m
[Mar 15 22:25:32.067] e[0;32mI (204) esp_image: segment 3: paddr=0x00028d6c vaddr=0x40080400 size=0x072ac ( 29356) loade[0m
[Mar 15 22:25:32.072] e[0;32mI (223) esp_image: segment 4: paddr=0x00030020 vaddr=0x400d0020 size=0x5bcf0 (376048) mape[0m
[Mar 15 22:25:32.186] e[0;32mI (333) esp_image: segment 5: paddr=0x0008bd18 vaddr=0x400876ac size=0x02afc ( 11004) loade[0m
[Mar 15 22:25:32.197] e[0;32mI (343) boot: Loaded app from partition at offset 0x10000e[0m
[Mar 15 22:25:32.203] e[0;32mI (343) boot: Disabling RNG early entropy source...e[0m
[Mar 15 22:25:32.208] e[0;32mI (345) cpu_start: Pro cpu up.e[0m
[Mar 15 22:25:32.208] e[0;32mI (348) cpu_start: Application information:e[0m
[Mar 15 22:25:32.214] e[0;32mI (353) cpu_start: Project name:     winbond-w25n-teste[0m
[Mar 15 22:25:32.220] e[0;32mI (359) cpu_start: App version:      0be6836e[0m
[Mar 15 22:25:32.225] e[0;32mI (364) cpu_start: Compile time:     Mar 16 2021 02:24:14e[0m
[Mar 15 22:25:32.231] e[0;32mI (370) cpu_start: ELF file SHA256:  6e1e225c163c5e3a...e[0m
[Mar 15 22:25:32.240] e[0;32mI (376) cpu_start: ESP-IDF:          4.2-r1e[0m
[Mar 15 22:25:32.242] e[0;32mI (381) cpu_start: Single core modee[0m
[Mar 15 22:25:32.247] e[0;32mI (385) heap_init: Initializing. RAM available for dynamic allocation:e[0m
[Mar 15 22:25:32.253] e[0;32mI (392) heap_init: At 3FF80000 len 00002000 (8 KiB): RTCRAMe[0m
[Mar 15 22:25:32.258] e[0;32mI (398) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAMe[0m
[Mar 15 22:25:32.264] e[0;32mI (405) heap_init: At 3FFB5770 len 0002A890 (170 KiB): DRAMe[0m
[Mar 15 22:25:32.275] e[0;32mI (411) heap_init: At 3FFE0440 len 0001FBC0 (126 KiB): D/IRAMe[0m
[Mar 15 22:25:32.280] e[0;32mI (417) heap_init: At 40078000 len 00008000 (32 KiB): IRAMe[0m
[Mar 15 22:25:32.286] e[0;32mI (423) heap_init: At 4008A1A8 len 00015E58 (87 KiB): IRAMe[0m
[Mar 15 22:25:32.292] e[0;32mI (430) cpu_start: Pro cpu start user codee[0m
[Mar 15 22:25:32.301] e[0;32mI (446) spi_flash: detected chip: generice[0m
[Mar 15 22:25:32.301] e[0;32mI (447) spi_flash: flash io: qioe[0m
[Mar 15 22:25:32.306] e[0;32mI (447) cpu_start: Starting scheduler on PRO CPU.e[0m
[Mar 15 22:25:32.324] 
[Mar 15 22:25:32.324] 
[Mar 15 22:25:32.324] mgos_freertos.c:155     winbond-w25n-test 1.0 (20210316-022430)
[Mar 15 22:25:32.329] mgos_freertos.c:157     Mongoose OS 0be6836 (20210316-022430/g0be6836-master)
[Mar 15 22:25:32.335] mgos_freertos.c:161     CPU: 160 MHz, FreeRTOS 8.2.0, heap: 318368 total, 287376 free
[Mar 15 22:25:32.340] mgos_freertos.c:163     Newlib 3.0.0
[Mar 15 22:25:32.345] esp32_main.c:65         ESP-IDF 4.2-r1
[Mar 15 22:25:32.350] esp32_main.c:68         Boot partition: app_0; flash: 4M
[Mar 15 22:25:32.355] mongoose.c:15666        Mongoose 6.18, LwIP 2.1.3
[Mar 15 22:25:32.359] mongoose.c:4928         mbed TLS 2.16.6-cesanta1
[Mar 15 22:25:32.366] mgos_vfs_dev.c:73       fs_0: esp32part ({"label": "fs_0"}), size 262144
[Mar 15 22:25:32.372] mgos_vfs_dev.c:73       fs_1: esp32part ({"label": "fs_1"}), size 262144
[Mar 15 22:25:32.379] mgos_vfs_dev.c:73       spif0: esp32part ({"label": "spif0"}), size 262144
[Mar 15 22:25:32.386] mgos_vfs.c:174          /: SPIFFS @ fs_0, opts {"bs":4096,"ps":256,"es":4096}
[Mar 15 22:25:32.466] mgos_vfs.c:347          /: size 233681, used: 1004, free: 232677
[Mar 15 22:25:32.533] mgos_sys_config.c:368   MAC: 24:6f:28:1e:93:70
[Mar 15 22:25:32.536] mgos_sys_config.c:376   WDT: 30 seconds
[Mar 15 22:25:32.540] mgos_deps_init.c:72     Init spi 1.0...
[Mar 15 22:25:32.546] esp32_spi_master.c:94   SPI3 init ok (MISO: 19, MOSI: 23, SCLK: 18; CS0/1/2: 5/-1/-1; native? yes)
[Mar 15 22:25:32.553] mgos_deps_init.c:72     Init fstab 1.0...
[Mar 15 22:25:32.558] mgos_vfs_dev_spi_fl:370 Failed to read JEDEC ID
[Mar 15 22:25:32.565] mgos_vfs_dev.c:66       Dev spi_flash {"cs": 21, "freq": 10000000} open failed: -7
[Mar 15 22:25:32.569] mgos_deps_init.c:74     fstab init failed
[Mar 15 22:25:32.573] mgos_freertos.c:184     MGOS init failed: -32

The sd_spi parameters are specific for the sdlib which does not use the spi library.
You should use the spi library settings.

The cs parameter should be 0,1 or 2 as defined in mos.yml, e.g:

  - ["spi.miso_gpio", 19]
  - ["spi.mosi_gpio", 23]
  - ["spi.sclk_gpio", 18]
  - ["spi.cs0_gpio", 5]

Ug, that was a dumb error, thank you @nliviu

Unfortunately, I’m still seeing a failure:

[Mar 16 01:07:09.315] esp32_spi_master.c:94   SPI3 init ok (MISO: 19, MOSI: 18, SCLK: 5; CS0/1/2: 21/-1/-1; native? no)
[Mar 16 01:07:09.321] mgos_deps_init.c:72     Init fstab 1.0...
[Mar 16 01:07:09.326] mgos_vfs_dev_spi_fl:370 Failed to read JEDEC ID
[Mar 16 01:07:09.333] mgos_vfs_dev.c:66       Dev spi_flash {"cs": 21, "freq": 10000000} open failed: -7
[Mar 16 01:07:09.337] mgos_deps_init.c:74     fstab init failed
[Mar 16 01:07:09.341] mgos_freertos.c:184     MGOS init failed: -32
[Mar 16 01:07:09.846] mgos_event.c:135        ev MOS2 triggered 0 handlers
[Mar 16 01:07:09.850] mgos_vfs.c:1026         Unmounting filesystems
[Mar 16 01:07:09.853] mgos_vfs_dev.c:203      fs_0 refs 1
[Mar 16 01:07:09.857] mgos_vfs_dev.c:203      spif0 refs 0
[Mar 16 01:07:09.860] mgos_vfs_dev.c:203      fs_1 refs 0
[Mar 16 01:07:09.863] mgos_vfs_dev.c:203      fs_0 refs 0
[Mar 16 01:07:09.867] mgos_system.c:43        Restarting

Is the rest of my configuration correct? I did look through the code and could not find what -7 means. I’m also trying to turn the debug on in the Winbond driver code.

Thanks again

Actually I made some progress. I removed the vfs-dev-spi-flash library so that only the vfs-dev-w25xxx device driver lib was there:

libs:
  - origin: https://github.com/mongoose-os-libs/vfs-dev-w25xxx
  - origin: https://github.com/mongoose-os-libs/fstab
  - origin: https://github.com/mongoose-os-libs/vfs-fs-lfs

I then changed the device type from spi_flash to “w25xxx”:

  - ["devtab.dev0.name", "spif0"]
  - ["devtab.dev0.type", "w25xxx"]
  - ["devtab.dev0.opts", '{"cs": 21, "freq": 30000000, "mode": 0}']
  - ["fstab.fs0.dev", "spif0"]
  - ["fstab.fs0.type", "LFS"]
  - ["fstab.fs0.path", "/data"]
  - ["fstab.fs0.create", true]

But I still see:

[Mar 16 02:15:04.130] mgos_deps_init.c:68     Init spi 1.0...
[Mar 16 02:15:04.136] esp32_spi_master.c:94   SPI3 init ok (MISO: 19, MOSI: 18, SCLK: 5; CS0/1/2: 21/21/21; native? no)
[Mar 16 02:15:04.142] mgos_deps_init.c:68     Init fstab 1.0...
[Mar 16 02:15:04.147] mgos_vfs_dev_w25xxx:230 Failed to read JEDEC ID
[Mar 16 02:15:04.153] mgos_vfs_dev.c:66       Dev w25xxx {"cs": 21, "freq": 30000000, "mode": 0} open failed: -7
[Mar 16 02:15:04.159] mgos_deps_init.c:70     fstab init failed
[Mar 16 02:15:04.163] mgos_freertos.c:184     MGOS init failed: -32
[Mar 16 02:15:04.670] mgos_event.c:135        ev MOS2 triggered 0 handlers
[Mar 16 02:15:04.674] mgos_vfs.c:1026         Unmounting filesystems

It seems that the flash chip cannot be accessed via SPI. I just checked again, the Arduino library is working fine with the same hardware and wiring.

"cs": 0

not 21.

Oh!! Thanks again @nliviu that’s great! I’m much further along now, it’s definitely talking to the flash chip finally! I’m still getting a failure regarding LFS, I’ll work on that tomorrow. Perhaps it’s a mismatch on the block size?

[Mar 16 03:13:51.684] mongoose.c:4928         mbed TLS 2.16.6-cesanta1
[Mar 16 03:13:51.691] mgos_vfs_dev.c:73       fs_0: esp32part ({"label": "fs_0"}), size 262144
[Mar 16 03:13:51.697] mgos_vfs_dev.c:73       fs_1: esp32part ({"label": "fs_1"}), size 262144
[Mar 16 03:13:51.704] mgos_vfs.c:174          /: SPIFFS @ fs_0, opts {"bs":4096,"ps":256,"es":4096}
[Mar 16 03:13:52.151] mgos_vfs.c:347          /: size 233681, used: 1004, free: 232677
[Mar 16 03:13:52.360] mgos_sys_config.c:368   MAC: 24:6f:28:1e:93:70
[Mar 16 03:13:52.363] mgos_sys_config.c:376   WDT: 30 seconds
[Mar 16 03:13:52.367] mgos_deps_init.c:68     Init spi 1.0...
[Mar 16 03:13:52.374] esp32_spi_master.c:94   SPI3 init ok (MISO: 19, MOSI: 18, SCLK: 5; CS0/1/2: 21/-1/-1; native? no)
[Mar 16 03:13:52.380] mgos_deps_init.c:68     Init fstab 1.0...
[Mar 16 03:13:52.388] mgos_vfs_dev_w25xxx:270 Found W2501GxIG; size 128000 KiB; bb_reserve 24, num_bb 0
[Mar 16 03:13:52.394] mgos_vfs_dev.c:73       spif0: w25xxx ({"cs": 0, "freq": 10000000}), size 131072000
[Mar 16 03:13:52.400] mgos_vfs_dev.c:203      spif0 refs 1
[Mar 16 03:13:52.405] mgos_vfs.c:109          Create LFS (dev 0x3ffb7e80, opts )
[Mar 16 03:13:52.411] mgos_vfs_dev_w25xxx:540 0x3ffb7e80 erase 4096 @ 0x0 (pg 0 blk 0) -> -1
[Mar 16 03:13:52.418] mgos_vfs_lfs.c:286      size 131072000 rs 64 ps 64 bs 4096 => -1
[Mar 16 03:13:52.422] mgos_vfs.c:115          FS LFS : create failed
[Mar 16 03:13:52.425] mgos_vfs_dev.c:203      spif0 refs 1
[Mar 16 03:13:52.429] mgos_deps_init.c:70     fstab init failed
[Mar 16 03:13:52.433] mgos_freertos.c:184     MGOS init failed: -32
[Mar 16 03:13:52.938] mgos_event.c:135        ev MOS2 triggered 0 handlers
[Mar 16 03:13:52.942] mgos_vfs.c:1026         Unmounting filesystems
[Mar 16 03:13:52.945] mgos_vfs_dev.c:203      fs_0 refs 1
[Mar 16 03:13:52.949] mgos_vfs_dev.c:203      spif0 refs 0
[Mar 16 03:13:52.952] mgos_vfs_dev.c:203      fs_1 refs 0
[Mar 16 03:13:52.955] mgos_vfs_dev.c:203      fs_0 refs 0
[Mar 16 03:13:52.958] mgos_system.c:43        Restarting

Update!

I added:

  - ["fstab.fs0.dev", "spif0"]
  - ["fstab.fs0.type", "LFS"]
  - ["fstab.fs0.opts", '{"bs": 131072}'] # bs 1024*64  <<<----- Added line here for bs
  - ["fstab.fs0.path", "/data"]
  - ["fstab.fs0.create", true]

And now it appears to create a file system, but I noticed that the chip size seems much smaller than the 1Gbit it is, I think 128,000KiB is about 16MB??:

[Mar 16 03:29:36.539] mgos_vfs_dev_w25xxx:270 Found W2501GxIG; size 128000 KiB; bb_reserve 24, num_bb 0

And calling fopen() fails:

[Mar 16 03:29:35.834] mongoose.c:4928         mbed TLS 2.16.6-cesanta1
[Mar 16 03:29:35.841] mgos_vfs_dev.c:73       fs_0: esp32part ({"label": "fs_0"}), size 262144
[Mar 16 03:29:35.847] mgos_vfs_dev.c:73       fs_1: esp32part ({"label": "fs_1"}), size 262144
[Mar 16 03:29:35.854] mgos_vfs.c:174          /: SPIFFS @ fs_0, opts {"bs":4096,"ps":256,"es":4096}
[Mar 16 03:29:36.301] mgos_vfs.c:347          /: size 233681, used: 1004, free: 232677
[Mar 16 03:29:36.510] mgos_sys_config.c:368   MAC: 24:6f:28:1e:93:70
[Mar 16 03:29:36.513] mgos_sys_config.c:376   WDT: 30 seconds
[Mar 16 03:29:36.517] mgos_deps_init.c:68     Init spi 1.0...
[Mar 16 03:29:36.523] esp32_spi_master.c:94   SPI3 init ok (MISO: 19, MOSI: 18, SCLK: 5; CS0/1/2: 21/-1/-1; native? no)
[Mar 16 03:29:36.530] mgos_deps_init.c:68     Init fstab 1.0...
[Mar 16 03:29:36.539] mgos_vfs_dev_w25xxx:270 Found W2501GxIG; size 128000 KiB; bb_reserve 24, num_bb 0
[Mar 16 03:29:36.545] mgos_vfs_dev.c:73       spif0: w25xxx ({"cs": 0, "freq": 10000000}), size 131072000
[Mar 16 03:29:36.549] mgos_vfs_dev.c:203      spif0 refs 1
[Mar 16 03:29:36.555] mgos_vfs.c:109          Create LFS (dev 0x3ffb7e80, opts {"bs": 131072})
[Mar 16 03:29:36.567] mgos_vfs_lfs.c:286      size 131072000 rs 64 ps 64 bs 131072 => 0
[Mar 16 03:29:36.570] mgos_vfs_dev.c:203      spif0 refs 1
[Mar 16 03:29:36.576] mgos_vfs.c:174          /data: LFS @ spif0, opts {"bs": 131072}
[Mar 16 03:29:36.583] mgos_vfs_lfs.c:262      size 131072000 rs 64 ps 64 bs 131072 => 0
[Mar 16 03:29:36.589] mgos_vfs.c:283          /data -> /data pl 5 -> 2 0x3ffb7e44 (refs 1)
[Mar 16 03:29:36.596] mgos_vfs.c:347          /data: size 131072000, used: 262144, free: 130809856
[Mar 16 03:29:36.600] mgos_vfs_dev.c:203      spif0 refs 2

Here is the updated C code:

#include "mgos.h"
#include "mgos_vfs_dev_w25xxx.h"

#include <stdio.h>

#define W25XXX_DEBUG 1

#define LED_PIN 13

static void timer_cb(void *arg)
{
  FILE *fp;
  char c[] = "this is a test";
  char buffer[100];

  /* Open file for both reading and writing */
  fp = fopen("/data/file.txt", "w+");

  if(fp == NULL)
  {
    LOG(LL_ERROR, ("fopen() failed"));
    return;
  }

  /* Write data to the file */
  fwrite(c, strlen(c) + 1, 1, fp);

  /* Seek to the beginning of the file */
  fseek(fp, 0, SEEK_SET);

  /* Read and display data */
  fread(buffer, strlen(c)+1, 1, fp);
  LOG(LL_ERROR, ("Read: %s", buffer));
  fclose(fp);


#ifdef LED_PIN
  mgos_gpio_toggle(LED_PIN);
#endif
  (void) arg;
}

enum mgos_app_init_result mgos_app_init(void)
{
  mgos_gpio_set_mode(LED_PIN, MGOS_GPIO_MODE_OUTPUT);
  mgos_set_timer(1000, MGOS_TIMER_REPEAT, timer_cb, NULL);

  return MGOS_APP_INIT_SUCCESS;
}

Here is the error triggered by fopen()

[Mar 16 03:29:38.190] mgos_vfs.c:283          /data/file.txt -> /data/file.txt pl 5 -> 2 0x3ffb7e44 (refs 1)
[Mar 16 03:29:38.198] mgos_vfs_dev_w25xxx:395 Hard ECC error @ 0:64
[Mar 16 03:29:38.203] mgos_vfs_dev_w25xxx:433 0x3ffb7e80 read 64 @ 0x20040 -> -6
[Mar 16 03:29:38.208] mgos_vfs_dev_w25xxx:395 Hard ECC error @ 0:64
[Mar 16 03:29:38.213] mgos_vfs_dev_w25xxx:433 0x3ffb7e80 read 64 @ 0x20000 -> -6
[Mar 16 03:29:38.219] mgos_vfs.c:377          open /data/file.txt 0x602 0x1b6 => 0x3ffb7e44 file.txt -6 => -6 (refs 0)
[Mar 16 03:29:38.225] main.c:23               fopen() failed

1 Gbits = 128Mbytes = 128K Kbytes (128 x1024 x 1024 = 134217728) = 128 MB
131072000 bytes = 128000 Kbytes = 128000 x 1024 = 1048576000 bits = 1000 x 1024 x 1024 = 1000 Mb

I’m older than the ‘i’ suffix so feel free to add it to all units above.
I guess your chip is “1KMib” (power of 10 Kilo and power of 2 Mega); or “128KKiB” but don’t quote me.

Yeah @scaprile I don’t know what I was thinking when I posted that, thanks. Probably should have been sleeping. The reported size of the chip does match what it should be.

In related news I can get the LFS to initialize if I turn off the ECC check, (which makes me nervous):

  - ["devtab.dev0.name", "spif0"]
  - ["devtab.dev0.type", "w25xxx"]
  - ["devtab.dev0.opts", '{"cs": 0, "freq": 10000000, "ecc_chk": 0}'] # <<--- HERE

  - ["fstab.fs0.dev", "spif0"]
  - ["fstab.fs0.type", "LFS"]
  - ["fstab.fs0.opts", '{"bs": 131072}'] # bs 1024*64 # Fails without this line

I wrote a small timer that opens a file /data/file-?.txt (where ? is a number) and keep incrementing. I can then create 7 or 8 files before it fails with:

[Mar 16 15:37:17.896] mgos_vfs.c:283          /data/file-8.txt -> /data/file-8.txt pl 5 -> 2 0x3ffb7e4c (refs 1)
[Mar 16 15:37:17.907] mgos_vfs.c:377          open /data/file-8.txt 0x602 0x1b6 => 0x3ffb7e4c file-8.txt 0 => 512 (refs 1)
[Mar 16 15:37:17.916] mgos_vfs.c:536          fstat 512 => 0x3ffb7e4c:0 => 0 (size 0)
[Mar 16 15:37:17.921] mgos_vfs.c:476          write 512 15 => 0x3ffb7e4c:0 => 15
[Mar 16 15:37:17.926] mgos_vfs.c:564          lseek 512 0 0 => 0x3ffb7e4c:0 => 0
[Mar 16 15:37:17.931] main.c:68               Read: this is a test
[Mar 16 15:37:17.986] /data/fwbuild-volumes/latest/apps/winbond-w25n-test/esp32/build_contexts/build_ctx_274300798/deps/vfs-fs-lfs/littlefs/lfs.c:1691:warn: Superblock 0x1 has become unwritable
[Mar 16 15:37:17.998] mgos_vfs.c:410          close 512 => 0x3ffb7e4c:0 => -28 (refs 1)

If I don’t increment the number in the file name it will happily create the same file over and over. So, I don’t know if I’m running out of directory space or leaking some other resource.

#include "mgos.h"
#include "mgos_system.h"
#include "mgos_vfs_dev_w25xxx.h"

#include <stdio.h>

#define W25XXX_DEBUG 1

#define LED_PIN 13

static void timer_cb(void *arg)
{
  #if 1
  static uint16_t ctr = 0;

  FILE *fp;
  char c[] = "this is a test";
  char buffer[100];

  char fileName[50];
  sprintf(fileName, "/data/file-%d.txt", ctr++);

  LOG(LL_ERROR, ("***** File Name: %s\r\n", fileName));


  /* Open file for both reading and writing */
//  fp = fopen("/data/file.txt", "w+");
  fp = fopen(fileName, "w+");

  if(fp == NULL)
  {
    LOG(LL_ERROR, ("fopen() failed"));
    return;
  }

  /* Write data to the file */
  fwrite(c, strlen(c) + 1, 1, fp);

  /* Seek to the beginning of the file */
  fseek(fp, 0, SEEK_SET);

  /* Read and display data */
  fread(buffer, strlen(c)+1, 1, fp);
  LOG(LL_ERROR, ("Read: %s", buffer));
  fclose(fp);
  #endif

#ifdef LED_PIN
  mgos_gpio_toggle(LED_PIN);
#endif
  (void) arg;
}

enum mgos_app_init_result mgos_app_init(void)
{
  mgos_wdt_disable();

  mgos_gpio_set_mode(LED_PIN, MGOS_GPIO_MODE_OUTPUT);
  mgos_set_timer(5000, MGOS_TIMER_REPEAT, timer_cb, NULL);

  return MGOS_APP_INIT_SUCCESS;
}

I don’t have expertise in this area. I tend to think that if ECC fails (which I understand as Error Correcting Codes) then it is because of a reason. The two reasons that come to mind are a) not implemented, b) hardware issues. I would first try to lower the SPI speed as much as I can.
Unfortunately I can’t help any further.

Thanks for the suggestion, I agree, and it was the first thing I tried. The hardware works well with some Arduino code I wrote (just to test it). I’m nor trying to implement something using the LFS API directly, bypassing the VFS driver. If that all works well, then the driver (or my use of it) is likely the problem. I’ll keep posting updates.

Cheers!

Hi everyone, I have made progress with this issue but I’m still unable to open more than about 20 files. The good news is that it seems I can create, write to and read from files using the standard C functions like fopen() and such. I’ll paste the latest mos.yml and code below. If I’m doing something wrong that would be great to know. If I’m not that would be great to know as well, so I can decide how to proceed.

#include "mgos.h"
#include "mgos_system.h"
#include "mgos_vfs_dev_w25xxx.h"

#include <stdio.h>

#define W25XXX_DEBUG 1
#define LED_PIN 13

static void timer_cb(void *arg)
{
  #if 1
  static uint16_t ctr = 0;

  FILE *fp;
  char c[] = "this is a test";
  char buffer[100];
  char writeBuf[100];

  char fileName[50];
  sprintf(fileName, "/data/file-%d.txt", ctr);

  LOG(LL_ERROR, ("***** File Name: %s\r\n", fileName));

  /* Open file for both reading and writing */
  fp = fopen(fileName, "w+");

  if(fp == NULL)
  {
    LOG(LL_ERROR, ("fopen() failed"));
    return;
  }
  else
  {
    LOG(LL_ERROR, ("fopen() SUCCESS!!"));
  }

  /* Write data to the file */
  sprintf(writeBuf, "Here is some data for the file - %d", ctr);
  fwrite(writeBuf, strlen(writeBuf) + 1, 1, fp);

  /* Seek to the beginning of the file */
  fseek(fp, 0, SEEK_SET);

  /* Read and display data */
  fread(buffer, strlen(writeBuf)+1, 1, fp);
  LOG(LL_ERROR, ("Read: %s", buffer));
  fclose(fp);

  ctr++;
  #endif

#ifdef LED_PIN
  mgos_gpio_toggle(LED_PIN);
#endif
  (void) arg;
}

enum mgos_app_init_result mgos_app_init(void)
{
  mgos_wdt_disable();

  mgos_gpio_set_mode(LED_PIN, MGOS_GPIO_MODE_OUTPUT);
  mgos_set_timer(2000, MGOS_TIMER_REPEAT, timer_cb, NULL);

  return MGOS_APP_INIT_SUCCESS;
}

mos.yml

author: mongoose-os
description: A Mongoose OS app skeleton
version: 1.0

libs_version: ${mos.version}
modules_version: ${mos.version}
mongoose_os_version: ${mos.version}

# From the W25Nxxx driver code:
#define W25XXX_PAGE_SIZE 2048U
#define W25XXX_BLOCK_SIZE (64 * W25XXX_PAGE_SIZE)
#define W25XXX_DIE_SIZE (1024 * W25XXX_BLOCK_SIZE)

# Optional. List of tags for online search.
tags:
  - c

# List of files / directories with C sources. No slashes at the end of dir names.
sources:
  - src

# List of dirs. Files from these dirs will be copied to the device filesystem
filesystem:
  - fs

config_schema:
  - ["spi.enable", true]
  # Other SPI interface options go here.

  - ["spi.miso_gpio", 19]
  - ["spi.mosi_gpio", 18]
  - ["spi.sclk_gpio", 5]
  - ["spi.cs0_gpio", 21]

  - ["devtab.dev0.name", "spif0"]
  - ["devtab.dev0.type", "w25xxx"]
  - ["devtab.dev0.opts", '{"cs": 0, "freq": 10000000, "ecc_chk": 0}']

#  json_scanf(opts, strlen(opts),
#             "{cs: %d, freq: %d, mode: %d, "
#             "bb_reserve: %u, ecc_chk: %B, spi: %T}",
#             &cs_num, &spi_freq, &spi_mode, &bb_reserve, &ecc_chk,
#             &spi_cfg_json);

  - ["fstab.fs0.dev", "spif0"]
  - ["fstab.fs0.type", "LFS"]
  - ["fstab.fs0.opts", '{"bs": 131072}'] # bs 1024*64 # Fails without this line
  - ["fstab.fs0.path", "/data"]
  - ["fstab.fs0.create", true]

#   // block device configuration
#    .read_size = 16,
#    .prog_size = 16,
#    .block_size = 2048*64,
#    .block_count = 1024,
#    .lookahead = 2048,

  - ["debug.level", 3]

libs:
  - origin: https://github.com/mongoose-os-libs/vfs-dev-w25xxx
  - origin: https://github.com/mongoose-os-libs/fstab

# Used by the mos tool to catch mos binaries incompatible with this file format
manifest_version: 2017-09-29

Error:

[Mar 17 14:44:13.752] main.c:57               ***** File Name: /data/file-20.txt
[Mar 17 14:44:13.752] 
[Mar 17 14:44:13.758] mgos_vfs.c:283          /data/file-20.txt -> /data/file-20.txt pl 5 -> 2 0x3ffb7e4c (refs 1)
[Mar 17 14:44:14.055] /data/fwbuild-volumes/latest/apps/winbond-w25n-test/esp32/build_contexts/build_ctx_434460174/deps/vfs-fs-lfs/littlefs/lfs.c:1691:warn: Superblock 0x1 has become unwritable
[Mar 17 14:44:14.074] mgos_vfs.c:377          open /data/file-20.txt 0x602 0x1b6 => 0x3ffb7e4c file-20.txt -6 => -6 (refs 0)
[Mar 17 14:44:14.076] main.c:66               fopen() failed

The best ideea might be to open an issue here and describe what is not working for you.

Later edit: #define is a comment in mos.yml. Check the yaml file format.

[quote=“nliviu, post:13, topic:2048”]
The best ideea might be to open an issue here and describe what is not working for you.[/quote]

Okay thanks, I’ll open the issue there.

I don’t follow… I put the #defines from the driver C code just as notes for sizes, intended as a comment. It behaves the same with and without those comments.

There is no #define in YAML, that is C preprocessor, not YAML.
Lines starting with #are comments in YAML.
In order to add these

# From the W25Nxxx driver code:
#define W25XXX_PAGE_SIZE 2048U
#define W25XXX_BLOCK_SIZE (64 * W25XXX_PAGE_SIZE)
#define W25XXX_DIE_SIZE (1024 * W25XXX_BLOCK_SIZE)

to be passed to the compiler, as we seem to understand from your YAML file (mos.yml), you have to use the proper syntax as documented:

cdefs:
    # From the W25Nxxx driver code:
    W25XXX_PAGE_SIZE: 2048U
    W25XXX_BLOCK_SIZE: (64 * W25XXX_PAGE_SIZE)
    W25XXX_DIE_SIZE: (1024 * W25XXX_BLOCK_SIZE)

(that should work but I didn’t test)

Thanks @scaprile yup I realize things starting with a # in the mos.yml file are comments. Those #define lines were taken from the driver C code, and I put them into the mos.yml files as actual comments to remind myself of the block and page size.

I realize leaving that in there for this post caused a good bit of confusion, for which I apologize.

I don’t think doing what you presented will make any difference, but what the heck I’ll give it a try.

I do appreciate your help!