Skip to content

Commit d368611

Browse files
dhylandsdpgeorge
authored andcommitted
Proposed fix for USB Mass Storage.
1 parent c737086 commit d368611

File tree

3 files changed

+19
-44
lines changed

3 files changed

+19
-44
lines changed

stmhal/usbd_msc_storage.c

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@
4141

4242
// These are needed to support removal of the medium, so that the USB drive
4343
// can be unmounted, and won't be remounted automatically.
44-
static uint8_t flash_removed = 0;
44+
static uint8_t flash_started = 0;
4545

4646
#if MICROPY_HW_HAS_SDCARD
47-
static uint8_t sdcard_removed = 0;
47+
static uint8_t sdcard_started = 0;
4848
#endif
4949

5050
/******************************************************************************/
@@ -73,6 +73,7 @@ static const int8_t FLASH_STORAGE_Inquirydata[] = { // 36 bytes
7373
*/
7474
int8_t FLASH_STORAGE_Init(uint8_t lun) {
7575
storage_init();
76+
flash_started = 1;
7677
return 0;
7778
}
7879

@@ -95,10 +96,10 @@ int8_t FLASH_STORAGE_GetCapacity(uint8_t lun, uint32_t *block_num, uint16_t *blo
9596
* @retval Status
9697
*/
9798
int8_t FLASH_STORAGE_IsReady(uint8_t lun) {
98-
if (flash_removed) {
99-
return -1;
99+
if (flash_started) {
100+
return 0;
100101
}
101-
return 0;
102+
return -1;
102103
}
103104

104105
/**
@@ -111,8 +112,8 @@ int8_t FLASH_STORAGE_IsWriteProtected(uint8_t lun) {
111112
}
112113

113114
// Remove the lun
114-
int8_t FLASH_STORAGE_StopUnit(uint8_t lun) {
115-
flash_removed = 1;
115+
int8_t FLASH_STORAGE_StartStopUnit(uint8_t lun, uint8_t started) {
116+
flash_started = started;
116117
return 0;
117118
}
118119

@@ -176,7 +177,7 @@ const USBD_StorageTypeDef USBD_FLASH_STORAGE_fops = {
176177
FLASH_STORAGE_GetCapacity,
177178
FLASH_STORAGE_IsReady,
178179
FLASH_STORAGE_IsWriteProtected,
179-
FLASH_STORAGE_StopUnit,
180+
FLASH_STORAGE_StartStopUnit,
180181
FLASH_STORAGE_PreventAllowMediumRemoval,
181182
FLASH_STORAGE_Read,
182183
FLASH_STORAGE_Write,
@@ -228,7 +229,7 @@ int8_t SDCARD_STORAGE_Init(uint8_t lun) {
228229
if (!sdcard_power_on()) {
229230
return -1;
230231
}
231-
232+
sdcard_started = 1;
232233
return 0;
233234

234235
}
@@ -264,33 +265,10 @@ int8_t SDCARD_STORAGE_GetCapacity(uint8_t lun, uint32_t *block_num, uint16_t *bl
264265
* @retval Status
265266
*/
266267
int8_t SDCARD_STORAGE_IsReady(uint8_t lun) {
267-
if (sdcard_removed) {
268-
return -1;
268+
if (sdcard_started) {
269+
return 0;
269270
}
270-
/*
271-
#ifndef USE_STM3210C_EVAL
272-
273-
static int8_t last_status = 0;
274-
275-
if(last_status < 0)
276-
{
277-
SD_Init();
278-
last_status = 0;
279-
}
280-
281-
if(SD_GetStatus() != 0)
282-
{
283-
last_status = -1;
284-
return (-1);
285-
}
286-
#else
287-
if( SD_Init() != 0)
288-
{
289-
return (-1);
290-
}
291-
#endif
292-
*/
293-
return 0;
271+
return -1;
294272
}
295273

296274
/**
@@ -303,8 +281,8 @@ int8_t SDCARD_STORAGE_IsWriteProtected(uint8_t lun) {
303281
}
304282

305283
// Remove the lun
306-
int8_t SDCARD_STORAGE_StopUnit(uint8_t lun) {
307-
sdcard_removed = 1;
284+
int8_t SDCARD_STORAGE_StartStopUnit(uint8_t lun, uint8_t started) {
285+
sdcard_started = started;
308286
return 0;
309287
}
310288

@@ -356,7 +334,7 @@ const USBD_StorageTypeDef USBD_SDCARD_STORAGE_fops = {
356334
SDCARD_STORAGE_GetCapacity,
357335
SDCARD_STORAGE_IsReady,
358336
SDCARD_STORAGE_IsWriteProtected,
359-
SDCARD_STORAGE_StopUnit,
337+
SDCARD_STORAGE_StartStopUnit,
360338
SDCARD_STORAGE_PreventAllowMediumRemoval,
361339
SDCARD_STORAGE_Read,
362340
SDCARD_STORAGE_Write,

stmhal/usbdev/class/cdc_msc_hid/inc/usbd_cdc_msc_hid.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ typedef struct _USBD_STORAGE {
5858
int8_t (* GetCapacity) (uint8_t lun, uint32_t *block_num, uint16_t *block_size);
5959
int8_t (* IsReady) (uint8_t lun);
6060
int8_t (* IsWriteProtected) (uint8_t lun);
61-
int8_t (* StopUnit)(uint8_t lun);
61+
int8_t (* StartStopUnit)(uint8_t lun, uint8_t started);
6262
int8_t (* PreventAllowMediumRemoval)(uint8_t lun, uint8_t param0);
6363
int8_t (* Read) (uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
6464
int8_t (* Write)(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);

stmhal/usbdev/class/cdc_msc_hid/src/usbd_msc_scsi.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -450,13 +450,10 @@ static int8_t SCSI_StartStopUnit(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t
450450
hmsc->bot_data_length = 0;
451451

452452
// On Mac OS X, when the device is ejected a SCSI_START_STOP_UNIT command is sent.
453-
// params[1]==0 means stop, param[1]==1 seems to be something else (happens after the
454-
// device is plugged in and mounted for some time, probably a keep alive).
453+
// Bit 0 of params[4] is the START bit.
455454
// If we get a stop, we must really stop the device so that the Mac does not
456455
// automatically remount it.
457-
if (params[1] == 0) {
458-
((USBD_StorageTypeDef *)pdev->pUserData)->StopUnit(lun);
459-
}
456+
((USBD_StorageTypeDef *)pdev->pUserData)->StartStopUnit(lun, params[4] & 1);
460457

461458
return 0;
462459
}

0 commit comments

Comments
 (0)