Detection of "Safely remove hardware" was used

Hello,

we use the RTX RTOS and USB MSD function to get access to an internal eMMC/SD card  that is connected to a Corex M7 CPU.

When the user does a "Safely remove hardware" on the PC: is it possible to detect this event in the microcontroller firmware?

When I do this for a standard USB-Stick, the device is removed and is not visible as a drive anymore.

If I do this for the drive of my embedded eMMC, Windows tells me that I can now safely remove the device, but the drive is still present and I can access it. So I assume that windows sends something like a "stop" command to the drive, and I need to do something in the firmware when I receive this. Is there a function that I can use to check that status?

Regards

Parents
  • Hello Gunnar, 

    you are correct.

    Windows sends SCSI command START STOP UNIT with LOEJ eject bit set to instruct the device to get ready for eject.
    After that Windows sends SCSI command TEST UNIT READY and when device is ready to eject it should respond with status FAIL.

    What you need to do to enable this functionality is:

    - implement callback USBD_MSC0_LUN_StartStop and register that after this function is called the device is ready for eject
    - implement callback USBD_MSC0_CheckMedia and if device is ready for eject return 0 (meaning media is not ready)

    Note: function USBD_MSC0_LUN_StartStop does not provide parameter for LOEJ bit but there is also start parameter which would be set to false.

    Note: there is also a command Prevent Allow Medium Removal before START STOP UNIT but there is no callback provided for that function in the USB Stack

    You can analyze code a bit if necessary, for example START STOP UNIT command handling is here https://github.com/ARM-software/MDK-Middleware/blob/e9d5313ea54b1b2f94a29ff5e06654674e7f0bb9/Components/USB/Source/usbd_lib_msc.c#L1191

Reply
  • Hello Gunnar, 

    you are correct.

    Windows sends SCSI command START STOP UNIT with LOEJ eject bit set to instruct the device to get ready for eject.
    After that Windows sends SCSI command TEST UNIT READY and when device is ready to eject it should respond with status FAIL.

    What you need to do to enable this functionality is:

    - implement callback USBD_MSC0_LUN_StartStop and register that after this function is called the device is ready for eject
    - implement callback USBD_MSC0_CheckMedia and if device is ready for eject return 0 (meaning media is not ready)

    Note: function USBD_MSC0_LUN_StartStop does not provide parameter for LOEJ bit but there is also start parameter which would be set to false.

    Note: there is also a command Prevent Allow Medium Removal before START STOP UNIT but there is no callback provided for that function in the USB Stack

    You can analyze code a bit if necessary, for example START STOP UNIT command handling is here https://github.com/ARM-software/MDK-Middleware/blob/e9d5313ea54b1b2f94a29ff5e06654674e7f0bb9/Components/USB/Source/usbd_lib_msc.c#L1191

Children
No data