-
Notifications
You must be signed in to change notification settings - Fork 27
fix(tusb_msc): Cache sector metrics, enable storage buffering #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
4571365
to
92e6469
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for the PR! Description is perfect! Nice idea to use the resource of the same task, which runs tud_task()
.
I pointed some general things that affects the logic. Based on the that, may I ask you to try and remove one buffer? The logic will be much simpler: get write10 callback, memcpy data, usbd_defer_func write to storage.
As far as we don't use both buffers at the same time, this change could help us:
- simplify the logic
- decrease memory usage (twice)
- do not loose in speed comparing to your current results
92e6469
to
851d69d
Compare
83da0d2
to
22cf787
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@igi540 Overall LGTM.
Only 1 major question about adding the storage_buffer into tinyusb_msc_storage_handle struct. This way, we would have 1 handle for 1 storage instance
@igi540 and please check the CI fail: |
44c38b9
to
1b7b065
Compare
This is really weird because it fails on v5.0 v5.1 and v5.2. EDIT: Might be related to espressif/esp-idf#9691 |
@igi540 Not it is not related, as in this error the espressif dcd layer is used: dcd_esp32sx.c:356 We are not using it since v0.15. |
@roma-jam sorry, I didn't provide any details. Very similar code with the same race condition and by-zero-division is also in the new DCD layer. However, I can't confirm it is the same problem now. Will look into it tomorrow |
694c5a0
to
b0f400b
Compare
b3c8a1e
to
ad3272d
Compare
TinyUSB does not provide any locking means to protect the DCD variables. This can lead to race conditions when the user is trying to submit a transfer while the device is being disconnected. This can cause the device to be in an inconsistent state, leading to a crash or undefined behavior. This commit adds a spin-lock to protect the DCD variables during device disconnect. Closes espressif/esp-idf#9691 Also reported in espressif/esp-usb#131
@igi540 The problem with race condition is fixed espressif/tinyusb#55 Please update this PR, so we can merge it after the fix is released |
TinyUSB does not provide any locking means to protect the DCD variables. This can lead to race conditions when the user is trying to submit a transfer while the device is being disconnected. This can cause the device to be in an inconsistent state, leading to a crash or undefined behavior. This commit adds a spin-lock to protect the DCD variables during device disconnect. Closes espressif/esp-idf#9691 Also reported in espressif/esp-usb#131
TinyUSB does not provide any locking means to protect the DCD variables. This can lead to race conditions when the user is trying to submit a transfer while the device is being disconnected. This can cause the device to be in an inconsistent state, leading to a crash or undefined behavior. This commit adds a spin-lock to protect the DCD variables during device disconnect. Closes espressif/esp-idf#9691 Also reported in espressif/esp-usb#131
f0b2f30
to
342a9b8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The one thing that unleash the race condition - is immediate return from the tud_msc_write10_cb()
.
Maybe we could think of adding the test_app with emulated ram disk to:
- measure the raw speed of mscd driver speed
- test sudden disconnection in this case (as in this case reset comes sooner than we
prepare_cbw()
, so the reset flush thexfer->max_size
)
Otherwise, LGTM.
Please, don't forget to address all the rest notes before merge.
Cached `get_sector_size` and `get_sector_count` to improve performance and avoid redundant calls. Storage buffering mechanism is enabled. Closes espressif/esp-idf#11110
342a9b8
to
c30af29
Compare
TinyUSB does not provide any locking means to protect the DCD variables. This can lead to race conditions when the user is trying to submit a transfer while the device is being disconnected. This can cause the device to be in an inconsistent state, leading to a crash or undefined behavior. This commit adds a spin-lock to protect the DCD variables during device disconnect. Closes espressif/esp-idf#9691 Also reported in espressif/esp-usb#131
TinyUSB does not provide any locking means to protect the DCD variables. This can lead to race conditions when the user is trying to submit a transfer while the device is being disconnected. This can cause the device to be in an inconsistent state, leading to a crash or undefined behavior. This commit adds a spin-lock to protect the DCD variables during device disconnect. Closes espressif/esp-idf#9691 Also reported in espressif/esp-usb#131
π Optimize USB MSC Performance on ESP32-S3, ESP32-P4, and ESP32-S2
β Requirements
Why is this needed?
π Description
This PR improves USB Mass Storage (MSC) performance by:
get_sector_size
andget_sector_count
to reduce redundant queries and improve efficiency.π Observed Results
ESP32-S3
SD Card Performance
β Write speed improved significantly with 8192B FIFO (~27% increase).
β Read speed remains stable at ~0.9 MB/s.
SPI Flash Performance
β Minimal improvement for SPI Flash (limited by write speed).
ESP32-P4
SD Card Performance
β Larger FIFO sizes (32KB) significantly improved SD card write speed (~22% increase).
SPI Flash Performance
πΈ No major changes in SPI Flash performance on ESP32-P4.
ESP32-S2
SPI Flash Performance (Corrected Values)
πΈ Minimal improvements observed, similar to ESP32-S3 results.
β SPI Flash performance remains limited due to architectural constraints.
π« Note: SD card is currently not supported in MSC device mode for ESP32-S2.
π§ͺ Measurement Method
π© Why SPI Flash Performance is Limited?
The core limitation is that program execution and storage reside on the same flash chip:
SD cards, on the other hand, are not affected by this limitation, which is why they show significant improvements with optimized buffering.
π Related
π Conclusion
β SD card performance now approaches USB Full-Speed theoretical limit (~1 MB/s on ESP32-S3).
β Write speed improvements are significant for SD cards, especially with larger FIFO buffers (up to ~4.5 MB/s on ESP32-P4).
β SPI Flash remains speed-limited due to architectural constraints, with only minor improvements observed.
π This PR significantly enhances SD card performance and optimizes MSC efficiency across ESP32 targets!