Skip to content
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

feat(usbmsc): Add is_writable function to the USBMSC class. #9569

Merged
merged 4 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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