Skip to content

Commit

Permalink
Fix USB host not mounting USB drive sometimes
Browse files Browse the repository at this point in the history
After an unmountable drive is inserted and ejected, a valid drive won't mount
[no ci]
  • Loading branch information
danngreen committed Dec 22, 2023
1 parent d8f51b0 commit 6f22c10
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 13 additions & 1 deletion firmware/src/usb/msc_fatfs_ops.hh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "disk_ops.hh"
#include "usbh_conf.h"
#include "usbh_def.h"
#include "usbh_msc.h"

Expand All @@ -23,7 +24,18 @@ public:
}

DSTATUS initialize() override {
return (is_mounted_ && msc_isready()) ? 0 : STA_NODISK | STA_NOINIT;
auto ready = msc_isready();
if (ready) {
if (!is_mounted_)
USBH_UsrLog("MSC is ready, mounting\n");
is_mounted_ = true;
} else
USBH_ErrLog("MSC is not ready, cannot initialize\n");

DSTATUS status = is_mounted_ ? 0 : STA_NODISK;
status |= ready ? 0 : STA_NOINIT;

return status;
}

DRESULT read(uint8_t *dst, uint32_t sector_start, uint32_t num_sectors) override {
Expand Down
4 changes: 3 additions & 1 deletion firmware/src/usb/msc_host.hh
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ public:
}

void connect() {
// Do nothing: subsequent fileio operations will call mount_disk()
pr_trace("USB MSC connect(): calling mount_disk()\n");
msc.mount_disk();
}

void disconnect() {
pr_trace("USB MSC disconnect()\n");
msc.unmount_disk();
}

Expand Down

0 comments on commit 6f22c10

Please sign in to comment.