Electronic – How to detect a safe eject of mass storage device on STM32

stm32usbusb device

I use usb module of STM32F103C8T6 to be a mass storage device, which allows user to edit some config files in the project.

I want to exit mass storage mode as soon as the user eject the disk, I tried to inquire bDeviceState but its value only changes when I replug the device(it still remains "CONFIGURED" after I safe eject the device).

I want a reliable method to detect the safe eject event from computer.Thanks.

Best Answer

"Safe to eject" is strictly a host-side software change of state. It simply means that the host has finished writing any pending changes to the device and won't initiate any more.

There is no reason to provide any signaling to the device itself once this has happened, so there's no way to detect it there when you are emulating storage. You'll have to provide a side channel (i.e., a separate USB endpoint) and a custom host-side driver if you want to do this.