From ceed1f8acfbf66305c343fee95a8574942221b8b Mon Sep 17 00:00:00 2001 From: happycodefarm Date: Thu, 25 Apr 2024 13:37:59 +0200 Subject: [PATCH 1/3] (feat)usbmsc: Add is_writable function Add is_writable function to the USBMSC class. Allows USBMSC to be mounted in read-only mode. --- cores/esp32/USBMSC.cpp | 12 ++++++++++++ cores/esp32/USBMSC.h | 1 + cores/esp32/esp32-hal-tinyusb.c | 4 ++++ 3 files changed, 17 insertions(+) diff --git a/cores/esp32/USBMSC.cpp b/cores/esp32/USBMSC.cpp index 6d36117b886..eeaf3026535 100644 --- a/cores/esp32/USBMSC.cpp +++ b/cores/esp32/USBMSC.cpp @@ -33,6 +33,7 @@ extern "C" uint16_t tusb_msc_load_descriptor(uint8_t *dst, uint8_t *itf) { typedef struct { bool media_present; + bool is_writable; uint8_t vendor_id[8]; uint8_t product_id[16]; uint8_t product_rev[4]; @@ -179,11 +180,17 @@ int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void *buffer, u return resplen; } +bool tud_msc_is_writable_cb(uint8_t lun) { + log_v("[%u]: %u", lun, msc_luns[lun].is_writable); + return msc_luns[lun].is_writable; // RAM disk is always ready +} + USBMSC::USBMSC() { if (MSC_ACTIVE_LUN < MSC_MAX_LUN) { _lun = MSC_ACTIVE_LUN; MSC_ACTIVE_LUN++; msc_luns[_lun].media_present = false; + msc_luns[_lun].is_writable = true; msc_luns[_lun].vendor_id[0] = 0; msc_luns[_lun].product_id[0] = 0; msc_luns[_lun].product_rev[0] = 0; @@ -213,6 +220,7 @@ bool USBMSC::begin(uint32_t block_count, uint16_t block_size) { void USBMSC::end() { msc_luns[_lun].media_present = false; + msc_luns[_lun].is_writable = false; msc_luns[_lun].vendor_id[0] = 0; msc_luns[_lun].product_id[0] = 0; msc_luns[_lun].product_rev[0] = 0; @@ -247,6 +255,10 @@ void USBMSC::onWrite(msc_write_cb cb) { msc_luns[_lun].write = cb; } +void USBMSC::isWritable(bool is_writable) { + msc_luns[_lun].is_writable = is_writable; +} + void USBMSC::mediaPresent(bool media_present) { msc_luns[_lun].media_present = media_present; } diff --git a/cores/esp32/USBMSC.h b/cores/esp32/USBMSC.h index e9d41e0b7f3..454aca3520a 100644 --- a/cores/esp32/USBMSC.h +++ b/cores/esp32/USBMSC.h @@ -44,6 +44,7 @@ class USBMSC { void productID(const char *pid); //max 16 chars void productRevision(const char *ver); //max 4 chars void mediaPresent(bool media_present); + void isWritable(bool is_writable); void onStartStop(msc_start_stop_cb cb); void onRead(msc_read_cb cb); void onWrite(msc_write_cb cb); diff --git a/cores/esp32/esp32-hal-tinyusb.c b/cores/esp32/esp32-hal-tinyusb.c index d3b55bf907f..4b5afe76edc 100644 --- a/cores/esp32/esp32-hal-tinyusb.c +++ b/cores/esp32/esp32-hal-tinyusb.c @@ -390,6 +390,10 @@ __attribute__((weak)) int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint __attribute__((weak)) int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void *buffer, uint16_t bufsize) { return -1; } +__attribute__ ((weak)) bool tud_msc_is_writable_cb (uint8_t lun){ + return false; +} + #endif /* From aa62a2888b133da5841522cd2b7e59ac66b66384 Mon Sep 17 00:00:00 2001 From: happycodefarm Date: Sat, 27 Apr 2024 09:41:51 +0200 Subject: [PATCH 2/3] Update USBMSC.ino Changes to USB Mass Storage (MSC) example code in Arduino USB library * Added MSC.isWritable(true) line to set the disk as writable --- libraries/USB/examples/USBMSC/USBMSC.ino | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libraries/USB/examples/USBMSC/USBMSC.ino b/libraries/USB/examples/USBMSC/USBMSC.ino index 47c6084580d..5dd9a9a2173 100644 --- a/libraries/USB/examples/USBMSC/USBMSC.ino +++ b/libraries/USB/examples/USBMSC/USBMSC.ino @@ -152,7 +152,10 @@ void setup() { MSC.onStartStop(onStartStop); MSC.onRead(onRead); MSC.onWrite(onWrite); + MSC.mediaPresent(true); + MSC.isWritable(true); // true if writable, false if read-only + MSC.begin(DISK_SECTOR_COUNT, DISK_SECTOR_SIZE); USB.begin(); } From 5c183744c3db1d2867e2e865fef0b60a1ecae35e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Mon, 13 May 2024 12:20:53 +0000 Subject: [PATCH 3/3] ci(pre-commit): Apply automatic fixes --- cores/esp32/esp32-hal-tinyusb.c | 2 +- libraries/USB/examples/USBMSC/USBMSC.ino | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/esp32/esp32-hal-tinyusb.c b/cores/esp32/esp32-hal-tinyusb.c index 4b5afe76edc..bab17a3980f 100644 --- a/cores/esp32/esp32-hal-tinyusb.c +++ b/cores/esp32/esp32-hal-tinyusb.c @@ -390,7 +390,7 @@ __attribute__((weak)) int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint __attribute__((weak)) int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void *buffer, uint16_t bufsize) { return -1; } -__attribute__ ((weak)) bool tud_msc_is_writable_cb (uint8_t lun){ +__attribute__((weak)) bool tud_msc_is_writable_cb(uint8_t lun) { return false; } diff --git a/libraries/USB/examples/USBMSC/USBMSC.ino b/libraries/USB/examples/USBMSC/USBMSC.ino index 5dd9a9a2173..532691ba63d 100644 --- a/libraries/USB/examples/USBMSC/USBMSC.ino +++ b/libraries/USB/examples/USBMSC/USBMSC.ino @@ -154,7 +154,7 @@ void setup() { MSC.onWrite(onWrite); MSC.mediaPresent(true); - MSC.isWritable(true); // true if writable, false if read-only + MSC.isWritable(true); // true if writable, false if read-only MSC.begin(DISK_SECTOR_COUNT, DISK_SECTOR_SIZE); USB.begin();