ESP32, Mongoose OS and W25N01 VFS Driver trouble

jimhelios
Posts: 39
Joined: Sun Nov 10, 2019 2:35 pm

ESP32, Mongoose OS and W25N01 VFS Driver trouble

Postby jimhelios » Thu Mar 18, 2021 2:32 am

This post is about a problem with Mongoose OS, and I hope it's acceptable to post it here. There is a separate forum for Arduino, but not Mongoose. I have asked for assistance on their forum, and on Stack Overflow, but no luck. I realize this issue is probably more about Mongoose than the ESP32, but I'm hoping here are some Mongoose coders here.

I have an ESP32 board with a Winbond W25N01 (1Gbit) NAND flash chip connected via the SPI bus. I'm using Mongoose OS and their Winbond VFS driver. I'm using LittleFS on top of SPIFFS, because I need the robustness from LFS. The drivers allow the use of standard C file functions. I have an SD card on this board, and it works perfectly.

With this flash device, writing and reading mostly works, but after I open about 20 files I get an error opening more files, saying that the "superblock has become unwritable." I have written some Arduino code to minimally test and I believe the hardware is good, although it is on a breadboard for now.

The hardware this is running on is an Adafruit Huzzah32 (ESP32-WROOM-32) on a breadboard with a W25N01 on a breakout board, also on the breadboard.

I'm wondering if my configuration and code is correct and would appreciate comments. The code is not production ready but I believe, at least in the C code, there are no errors. I'm not sure about the mos.yml file.

Code: Select all

    #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)
    {
      static uint16_t ctr = 0;

      FILE *fp;
      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++;

    #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

Code: Select all

    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}

    # These comments are code taken from the Mongoose OS 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"]
      
      # Unless the ecc_chk is disabled, nothing works (which is a red flag)
      - ["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:

Code: Select all

    [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

jimhelios
Posts: 39
Joined: Sun Nov 10, 2019 2:35 pm

Re: ESP32, Mongoose OS and W25N01 VFS Driver trouble

Postby jimhelios » Sun Mar 21, 2021 3:35 am

I'm willing to pay someone to help if they can.

Who is online

Users browsing this forum: No registered users and 131 guests