Skip to content

Commit 5c97244

Browse files
USBMSD: Add media removal event check function
1 parent 2ba1cdd commit 5c97244

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
@@ -65,7 +65,7 @@ enum Status {
6565

6666
USBMSD::USBMSD(BlockDevice *bd, bool connect_blocking, uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
6767
: USBDevice(get_usb_phy(), vendor_id, product_id, product_release),
68-
_initialized(false), _in_task(&_queue), _out_task(&_queue), _reset_task(&_queue), _control_task(&_queue), _configure_task(&_queue), _bd(bd)
68+
_initialized(false), _media_removed(false), _in_task(&_queue), _out_task(&_queue), _reset_task(&_queue), _control_task(&_queue), _configure_task(&_queue), _bd(bd)
6969
{
7070
_init();
7171
if (connect_blocking) {
@@ -77,7 +77,7 @@ USBMSD::USBMSD(BlockDevice *bd, bool connect_blocking, uint16_t vendor_id, uint1
7777

7878
USBMSD::USBMSD(USBPhy *phy, BlockDevice *bd, uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
7979
: USBDevice(phy, vendor_id, product_id, product_release),
80-
_initialized(false), _in_task(&_queue), _out_task(&_queue), _reset_task(&_queue), _control_task(&_queue), _configure_task(&_queue), _bd(bd)
80+
_initialized(false), _media_removed(false), _in_task(&_queue), _out_task(&_queue), _reset_task(&_queue), _control_task(&_queue), _configure_task(&_queue), _bd(bd)
8181
{
8282
_init();
8383
}
@@ -160,6 +160,7 @@ bool USBMSD::connect()
160160
//connect the device
161161
USBDevice::connect();
162162
_initialized = true;
163+
_media_removed = false;
163164
_mutex.unlock();
164165
_mutex_init.unlock();
165166
return true;
@@ -212,7 +213,12 @@ void USBMSD::attach(mbed::Callback<void()> cb)
212213
unlock();
213214
}
214215

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

usb/device/USBMSD/USBMSD.h

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

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

127134
/*
@@ -204,6 +211,9 @@ class USBMSD: public USBDevice {
204211
// If this class has been initialized
205212
bool _initialized;
206213

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

0 commit comments

Comments
 (0)