Skip to content

Commit f2b1aee

Browse files
authored
Merge pull request #2406 from tannewt/improve_usb_eject
Improve USB eject by resetting on replug
2 parents e9cb47f + 561fdfb commit f2b1aee

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

supervisor/shared/usb/usb.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,12 @@ void usb_background(void) {
8787

8888
// Invoked when device is mounted
8989
void tud_mount_cb(void) {
90+
usb_msc_mount();
9091
}
9192

9293
// Invoked when device is unmounted
9394
void tud_umount_cb(void) {
95+
usb_msc_umount();
9496
}
9597

9698
// Invoked when usb bus is suspended

supervisor/shared/usb/usb_msc_flash.c

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@
4141

4242
static bool ejected[1];
4343

44+
void usb_msc_mount(void) {
45+
// Reset the ejection tracking every time we're plugged into USB. This allows for us to battery
46+
// power the device, eject, unplug and plug it back in to get the drive.
47+
for (uint8_t i = 0; i < sizeof(ejected); i++) {
48+
ejected[i] = false;
49+
}
50+
}
51+
52+
void usb_msc_umount(void) {
53+
54+
}
55+
4456
// The root FS is always at the end of the list.
4557
static fs_user_mount_t* get_vfs(int lun) {
4658
// TODO(tannewt): Return the mount which matches the lun where 0 is the end
@@ -198,19 +210,34 @@ bool tud_msc_test_unit_ready_cb(uint8_t lun) {
198210
// - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage
199211
// - Start = 1 : active mode, if load_eject = 1 : load disk storage
200212
bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject) {
213+
if (lun > 1) {
214+
return false;
215+
}
216+
fs_user_mount_t* current_mount = get_vfs(lun);
217+
if (current_mount == NULL) {
218+
return false;
219+
}
201220
if (load_eject) {
202-
if (lun > 1) {
203-
return false;
204-
} else {
205-
fs_user_mount_t* current_mount = get_vfs(lun);
206-
if (current_mount == NULL) {
207-
return false;
208-
}
221+
if (!start) {
222+
// Eject but first flush.
209223
if (disk_ioctl(current_mount, CTRL_SYNC, NULL) != RES_OK) {
210224
return false;
211225
} else {
212226
ejected[lun] = true;
213227
}
228+
} else {
229+
// We can only load if it hasn't been ejected.
230+
return !ejected[lun];
231+
}
232+
} else {
233+
if (!start) {
234+
// Stop the unit but don't eject.
235+
if (disk_ioctl(current_mount, CTRL_SYNC, NULL) != RES_OK) {
236+
return false;
237+
}
238+
} else {
239+
// Start the unit, but only if not ejected.
240+
return !ejected[lun];
214241
}
215242
}
216243

supervisor/usb.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@ void init_usb_hardware(void);
4141
bool usb_enabled(void);
4242
void usb_init(void);
4343

44+
// Propagate plug/unplug events to the MSC logic.
45+
void usb_msc_mount(void);
46+
void usb_msc_umount(void);
47+
4448
#endif // MICROPY_INCLUDED_SUPERVISOR_USB_H

0 commit comments

Comments
 (0)