Skip to content

Commit

Permalink
feat(usbmsc): Add is_writable function to the USBMSC class. (#9569)
Browse files Browse the repository at this point in the history
* (feat)usbmsc: Add is_writable function

Add is_writable function to the USBMSC class.
Allows USBMSC to be mounted in read-only mode.

* 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

* ci(pre-commit): Apply automatic fixes

---------

Co-authored-by: Rodrigo Garcia <[email protected]>
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed May 13, 2024
1 parent e8e251a commit a04fcee
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cores/esp32/USBMSC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions cores/esp32/USBMSC.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions cores/esp32/esp32-hal-tinyusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

/*
Expand Down
3 changes: 3 additions & 0 deletions libraries/USB/examples/USBMSC/USBMSC.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit a04fcee

Please sign in to comment.