Skip to content

Commit 0cd9d24

Browse files
USBMSD: Add media removal event check function
1 parent 0e7f112 commit 0cd9d24

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

usb/device/USBMSD/USBMSD.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ enum Status {
6666

6767
USBMSD::USBMSD(BlockDevice *bd, bool connect_blocking, uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
6868
: USBDevice(get_usb_phy(), vendor_id, product_id, product_release),
69-
_initialized(false), _in_task(&_queue), _out_task(&_queue), _reset_task(&_queue), _control_task(&_queue), _configure_task(&_queue), _bd(bd)
69+
_initialized(false), _media_removed(false), _in_task(&_queue), _out_task(&_queue), _reset_task(&_queue), _control_task(&_queue), _configure_task(&_queue), _bd(bd)
7070
{
7171
_init();
7272
if (connect_blocking) {
@@ -78,7 +78,7 @@ USBMSD::USBMSD(BlockDevice *bd, bool connect_blocking, uint16_t vendor_id, uint1
7878

7979
USBMSD::USBMSD(USBPhy *phy, BlockDevice *bd, uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
8080
: USBDevice(phy, vendor_id, product_id, product_release),
81-
_initialized(false), _in_task(&_queue), _out_task(&_queue), _reset_task(&_queue), _control_task(&_queue), _configure_task(&_queue), _bd(bd)
81+
_initialized(false), _media_removed(false), _in_task(&_queue), _out_task(&_queue), _reset_task(&_queue), _control_task(&_queue), _configure_task(&_queue), _bd(bd)
8282
{
8383
_init();
8484
}
@@ -161,6 +161,7 @@ bool USBMSD::connect()
161161
//connect the device
162162
USBDevice::connect();
163163
_initialized = true;
164+
_media_removed = false;
164165
_mutex.unlock();
165166
_mutex_init.unlock();
166167
return true;
@@ -213,7 +214,12 @@ void USBMSD::attach(mbed::Callback<void()> cb)
213214
unlock();
214215
}
215216

216-
int USBMSD::disk_read(uint8_t *data, uint64_t block, uint8_t count)
217+
bool USBMSD::media_removed()
218+
{
219+
return _media_removed;
220+
}
221+
222+
int USBMSD::disk_read(uint8_t* data, uint64_t block, uint8_t count)
217223
{
218224
bd_addr_t addr = block * _bd->get_erase_size();
219225
bd_size_t size = count * _bd->get_erase_size();
@@ -816,6 +822,7 @@ void USBMSD::CBWDecode(uint8_t *buf, uint16_t size)
816822
case MEDIA_REMOVAL:
817823
_csw.Status = CSW_PASSED;
818824
sendCSW();
825+
_media_removed = true;
819826
break;
820827
default:
821828
fail();

usb/device/USBMSD/USBMSD.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ class USBMSD: public USBDevice {
123123
*/
124124
void attach(mbed::Callback<void()> cb);
125125

126+
/**
127+
* Check if MSD device was removed/unmounted on the host side.
128+
*
129+
* @returns true if device was removed/unmounted on the host side
130+
*/
131+
bool media_removed();
132+
126133
protected:
127134

128135
/*
@@ -205,6 +212,9 @@ class USBMSD: public USBDevice {
205212
// If this class has been initialized
206213
bool _initialized;
207214

215+
// If msd device has been unmounted by host
216+
volatile bool _media_removed;
217+
208218
//state of the bulk-only state machine
209219
Stage _stage;
210220

0 commit comments

Comments
 (0)