ESP32-S2 USB MSC FATFS SPI-Flash

E_SCH.
Posts: 10
Joined: Mon Jul 19, 2021 11:16 am

ESP32-S2 USB MSC FATFS SPI-Flash

Postby E_SCH. » Thu Aug 05, 2021 11:55 am

I am using the ESP32-S2-Wrover.
I have tested the two examples "wear_levelling" (from ESP-IDF) and "cdc_msc_freertos" (from github: tinyusb/examples/device/cdc_msc/src/) and are running.
In the example "cdc_msc_freertos" the disk is unfortunately mapped in the RAM.
I would now like to create a partition with FATFS in the SPI flash that I can access via USB MSC.
I can generate the FATFS as in the "wear-leveling" example. However, I now have the problem of how do I access this FATFS block-oriented to read and write. The data between the USB host (Windows PC) and the USB device (ESP32-S2) are exchanged in blocks.
Does anyone have a tip on how I can do that?
Is there possibly a sample code for this.
Thank you for your support.

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

Re: ESP32-S2 USB MSC FATFS SPI-Flash

Postby ESP_igrr » Fri Aug 06, 2021 12:37 pm

Hi E_SCH,

There doesn't seem to be an example which combines wear levelling and USB MSC yet. Could you explain, what do you expect from such an example? I think concurrent access to the same partition from the app (via FATFS library) and from USB (via MSC) is tricky. You would need to decide somehow who is allowed to modify the partition at any given time — the application running on ESP, or the host. Allowing both to access the partition at the same time will likely result in data corruption.

chegewara
Posts: 2240
Joined: Wed Jun 14, 2017 9:00 pm

Re: ESP32-S2 USB MSC FATFS SPI-Flash

Postby chegewara » Fri Aug 06, 2021 1:53 pm

I think there is few problems with accessing raw data and wear leveling (at least i got such problems and quit that usage).
1. MSC read/write raw data, sector by sector
2. wear leveling is adding extra sectors and host OSs dont know how to deal with it
3. i didnt find API to read raw sectors to bridge between MSC and flash; i know we can use regular API to read/write partitions, but that excludes wear leveling i suppose

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

Re: ESP32-S2 USB MSC FATFS SPI-Flash

Postby ESP_igrr » Fri Aug 06, 2021 2:28 pm

chegewara wrote: 3. i didnt find API to read raw sectors to bridge between MSC and flash; i know we can use regular API to read/write partitions, but that excludes wear leveling i suppose
I think you can use wl_read, wl_write functions for this purpose. Basically, if the host asks for sector N, you don't read physical sector N, instead you call wl_read(handle, N * sector_size, ...) to read the logical sector that is managed by wear_levelling.
This way the host operations will go through the wear_levelling layer.

chegewara
Posts: 2240
Joined: Wed Jun 14, 2017 9:00 pm

Re: ESP32-S2 USB MSC FATFS SPI-Flash

Postby chegewara » Tue Aug 24, 2021 1:16 pm

ESP_igrr wrote: I think you can use wl_read, wl_write functions for this purpose. Basically, if the host asks for sector N, you don't read physical sector N, instead you call wl_read(handle, N * sector_size, ...) to read the logical sector that is managed by wear_levelling.
This way the host operations will go through the wear_levelling layer.
It seems to work more or less, and by more or less i mean FAT sector is read and recognized by ubuntu properly, but thats it.
System shows expected disk size etc. I can see also files that do not exist:
Screenshot from 2021-08-24 15-04-30.png
Screenshot from 2021-08-24 15-04-30.png (25.89 KiB) Viewed 4935 times
To mount VFS i am using this function:
https://docs.espressif.com/projects/esp ... l_handle_t

FFat partition and whole code are arduino.

chegewara
Posts: 2240
Joined: Wed Jun 14, 2017 9:00 pm

Re: ESP32-S2 USB MSC FATFS SPI-Flash

Postby chegewara » Sat Aug 28, 2021 7:54 pm

I found this code useful. No need to use low level API (like wl_read/wl_write):
https://github.com/espressif/esp-iot-so ... tusb_msc.c

Now i have only issue with WL sector size under arduino, which is 4096 bytes and tinyusb is configured to work with 512 bytes buffer.

Who is online

Users browsing this forum: No registered users and 275 guests