Mbed littlefs mount failure on Seeed Xiao Sense with nrf52840

Hello I’m trying to mount the mbed littlefs to the qspi blockdevice on Seeed Xiao Sense (nrf52840).
I’m using Arduino IDE 2.0.4 on Windows. I installed “seeed nRF52 mbed-enabled Boards” through Arduino board manager. Then I updated the library files according to the commit attached here. So the qspi blockdevice will work on this board.

The qspi blockdevice functionality including read/prog/erase is OK, but when I was trying to mount the littlefs to the qspi blockdevice it failed. The format error code is -4005, which is “Erase command not on sector aligned addresses or exceeds device size” defined on line 32 of QSPIFBlockDevice.h. The mount error code is -138, which I didn’t find the definition. In addition, the qspi blockdevice read size is 4, program size is 4, erase size is 256.
I also tested the littlefs on heap blockdevice, and it worked just fine.
Attached below is my Arduino code. Thanks a lot!

#include “mbed.h”
#include “BlockDevice.h”
#include “QSPIFBlockDevice.h”
#include “LittleFileSystem.h”

#include “HeapBlockDevice.h”

//QSPIFBlockDevice bd(QSPI_FLASH1_IO0, QSPI_FLASH1_IO1, QSPI_FLASH1_IO2, QSPI_FLASH1_IO3, QSPI_FLASH1_SCK, QSPI_FLASH1_CSN, QSPIF_POLARITY_MODE_1, MBED_CONF_QSPIF_QSPI_FREQ);

//mbed::BlockDevice *bd = new mbed::HeapBlockDevice(2048, 1, 1, 512);
//heap blockdevice works fiine

mbed::BlockDevice *bd = new QSPIFBlockDevice(QSPI_FLASH1_IO0, QSPI_FLASH1_IO1, QSPI_FLASH1_IO2, QSPI_FLASH1_IO3, QSPI_FLASH1_SCK, QSPI_FLASH1_CSN, QSPIF_POLARITY_MODE_1, MBED_CONF_QSPIF_QSPI_FREQ);

mbed::LittleFileSystem fs(“fs”);

void setup() {
Serial.begin(115200);
while (!Serial);

Serial.println(“init the blockdevice…”);
int err = bd->init();
if(err) {
Serial.print(“init error. err code=”);
Serial.println(err);
}

Serial.print("bd read size = ");
Serial.println(bd->get_read_size());
Serial.print("bd program size = ");
Serial.println(bd->get_program_size());
Serial.print("bd erase size = ");
Serial.println(bd->get_erase_size());
Serial.print("bd erase value at addr 0 = ");
Serial.println(bd->get_erase_size(0));

int force_format = 1;
if (force_format) {
err = fs.format(bd);
if (err) {
Serial.print("format err = ");
Serial.println(err);
}
}

err = fs.mount(bd);
if (err) {
Serial.print("mount err = ");
Serial.println(err);
}

bd->deinit();
}

void loop() {
Serial.println(“testing done”);
delay(10000);
}

Arduino disabled the use of QSPIFBlockDevice. Instead you have to use FlashIAPBlockDevice.
https://os.mbed.com/docs/mbed-os/v6.16/apis/flashiapblockdevice.html

Here’s some code, which may help - this worked on a Raspberry Pi Pico but it has not been tested on a nRF52840.

#include "PluggableUSBMSD.h"
#include "FlashIAPBlockDevice.h"

static FlashIAPBlockDevice bd(XIP_BASE + 0x100000, 0x100000);
USBMSD MassStorage(&bd);

static FILE *f = nullptr;
// Note that file is written to the actual root directory as created when USBMSD is mounted
// File is not stored within a folder called root.
// File is only found when you remove usb and replace again
const char *fname = "/root/myfile.txt";


void USBMSD::begin()
{
  int err = getFileSystem().mount(&bd);
  if (err) {
   //String FN = getFileSystem().getName();
   Serial.println(" filesystem mount failed. Try to reformat device...");
    err = getFileSystem().reformat(&bd);
  }
  if (err) {
    Serial.println("Error: Unable to format/mount the device.");
    while(1);
  }
}


mbed::FATFileSystem &USBMSD::getFileSystem()
{
  static mbed::FATFileSystem fs("root");
  return fs;
}

void writeContents() {
    f = fopen(fname, "w+");
    if (f != nullptr) {
      fprintf(f, "Hello World\n");
      fflush(f);      
      fclose(f);
      Serial.println("Written to File");
    }
    else {
      Serial.println("File not found");
    }  
}

void readContents() {
    f = fopen(fname, "r");
    if (f != nullptr) {
      fclose(f);
      Serial.println("File found");
    }
    else {
      Serial.println("File not found");
    }  
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  MassStorage.begin();
  // If you do not want to wait for Serial Monitor to load then use a long delay here.
  // Found a delay helps if you want to capture the initial serial output.
  while(!Serial) {;;}
  //delay(1000);
  Serial.println("MassStorage mounted");
  Serial.flush();
  //writeContents();
  readContents();

}

void loop() {
  // put your main code here, to run repeatedly:
  
}

Thanks for your reply. But I don’t find anything about “Arduino disabled the use of QSPIFBlockDevice”. Actually the qspi chip on Seeed Xiao BLE sense works fine. I just cannot mount a file system on it.
The flashIAP on Xiao BLE sense with nrf52840 writes to the mcu’s internal flash, which is not intended for data storage based on what I read online.

Yes, sorry, I realised afterwards that the restriction probably just applies to Raspberry Pico because it uses external flash (uses QSPI too). Code should still work by changing FlashIAPBlockDevice with QSPIFBlockDevice