File transfer over USB

Remyhx
Posts: 23
Joined: Thu Jun 17, 2021 8:22 pm

Re: File transfer over USB

Postby Remyhx » Tue Nov 01, 2022 8:50 am

ESP_igrr wrote:
Wed Oct 26, 2022 8:44 pm
Yes, it should be very similar to that example, except that you would call sdmmc_write_sectors instead of wear_levilling functions.
Hi ESP_iggr,

I'm stuck with the SD connection with TinyUSB. After usb_init() I mount and connect the SD card (first tested with the SD card sample if I can access) and initiate storage_mount_fat(). In that function I only use:

Code: Select all

s_fat_mounted = true;
s_base_path = base_path;
and disable the other code because of the already mounted SD card like the ESP example.

If I plug the ESP32-S3 with USB/SD-Card in my computer it initiates the callback functions and windows opens up the explorer. But it can't read the contents. Also the first read outs with sdmmc_read_sectors appear to be empty (formatted SD card), where I expected some MBR info at 0.

I changed the tud_msc_read10_cb as this:

Code: Select all

int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize)
{
    ESP_LOGI(TAG, "tud_msc_read10_cb() invoked, lun=%d, lba=%d, offset=%d, bufsize=%d", lun, lba, offset, bufsize);

    size_t addr = lba * storage_get_sector_size() + offset;
    
    ESP_LOGI(TAG,"Address: %d",addr);
    //esp_err_t err = storage_read_sector(addr, bufsize, buffer);
    esp_err_t err = sdmmc_read_sectors(card,buffer, addr, bufsize/storage_get_sector_size());
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "storage_read_sector failed: 0x%x", err);
        return 0;
    }
    return bufsize;
}

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

Re: File transfer over USB

Postby ESP_igrr » Tue Nov 01, 2022 11:12 pm

Could you check again the meaning of the arguments you are passing to sdmmc_read_sectors? The offset should be in sectors, but you seem to be calculating the value in bytes.
https://docs.espressif.com/projects/esp ... e_t6size_t

I think you need to read the sector given by lba argument an then use memcpy to copy the requested amount of data from the sector into the output buffer. (Optionally, you can cache the sector data between invocations of the callback, to avoid reading the same sector multiple times.)

Remyhx
Posts: 23
Joined: Thu Jun 17, 2021 8:22 pm

Re: File transfer over USB

Postby Remyhx » Wed Nov 02, 2022 2:43 pm

@ESP_iggr that was a little bit stupid of me :shock: :lol: , i changed it and voila it worked! Thank you!

With my SD or eMMC at 40mhz, 4-width com I can achieve 0.8 MB/s... Put MSC fifo size at 8192 (bigger not possible -> then it will automatically step down to 512). Do you think it could be faster?

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

Re: File transfer over USB

Postby ESP_igrr » Wed Nov 02, 2022 10:20 pm

I would probably try doing some basic performance optimization steps first: increase the CPU frequency, change compiler optimization mode to "performance", and so on (https://docs.espressif.com/projects/esp ... rall-speed)

You could also check if there is any caching you can do for read operations from the SD card. Typically reading larger buffers from SD card has higher throughput than reading just one sector at a time. If you can expect that the host will read several sectors sequentially, it might be more efficient to read them at once. You could also cache frequently accessed sectors, such as the FAT.

If you are using the ESP32-S3 you could also try to split USB and SD functions into separate tasks running on the two CPU cores. However, that will likely make the code more complex since you would have to synchronize access to shared resources.

User avatar
fasani
Posts: 197
Joined: Wed Jan 30, 2019 12:00 pm
Location: Barcelona
Contact:

Re: File transfer over USB

Postby fasani » Sun Oct 15, 2023 7:12 am

Did anyone succeed sending a file "modem style" via USB?

I tried and failed miserably so far :geek: . Checked the RZ / SZ ZModem Linux programs:

https://github.com/coderfordev/rzsz/blob/master/rz.c

And it seems at least complicated. So I guess would have to restrain to use SDCard also.
Just wanted to know if it will be possible to implement something like the SZ receive style via serial.
Writing documentation about my findings here
epdiy collaborator | http://fasani.de Fan of Espressif MCUs and electronic design

Who is online

Users browsing this forum: No registered users and 218 guests