cancel
Showing results for 
Search instead for 
Did you mean: 

Where is the UsbMassStorage Examples / Supt for stm32f4xx ? - Solved

jlchoobs
Associate II
Posted on April 18, 2012 at 06:58

I have an obvious question :

How do I implement SDCard as MassStorage on the Stm32f4Discovery Board ?

Why is there Examples of this for the stm32F103 but not for the stm32F4discovery ?

The wonderful 22examples for the Discovery are a lot less than the stm32f103 examples present.

It seems like it should be a simple modifying of code.

But, I find no examples or support for this anywhere at Stm or on the internet (besides my site).

Has Anybody tried this Project yet ?

#stm32f4discovery #stm3240g #epic-fail #msc
35 REPLIES 35
Posted on June 18, 2012 at 18:40

What happens if i ignore it and try to run.

You'll learn if it works or not, or if it clashes with your hardware. I chose to skip this step.

Can you please tell me where are these calls for LCD and LED. Also tell me what are those changes required.

No not really, I pruned the extraneous files from the project (ie all the stm324xg_eval_xxx files unrelated to SDIO SD). You could use ''Find in Files'' on LCD and LED references. You could fix the compiler errors as they occur.

It would take me longer to document it that it took to do. I could zip up my Yagarto project if it would help, but that is predicated on having a suitable build environment.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
danish_enchanting
Associate II
Posted on June 19, 2012 at 10:29

I have commented the LCD and LED calls and routines. The connection of microsd card with discovery kit are correct. the card is in working condition. I changed the PLL_M and HSE values as you described. but still nothing happens when i run the project. the device is not even detected. i am stuck dont know what to do next.

danish_enchanting
Associate II
Posted on June 19, 2012 at 19:49

I did some more tweeking which took me very close to my goal.

The usb mass storage is detected and in windows the system shows Removable Disk. But the SD card is inaccessible. Windows shows SD card as 0 bytes memory free, and 0 bytes available. I tried with two different cards and boht were functional. I do not know the cause of this behavior.

Posted on June 19, 2012 at 21:12

Yeah, well you'll need to break out a debugger or instrument it via the serial port.

I'd look specifically at what SD_GetCardInfo() does.

To take USB out of the equation, you could look at the SDIO example

STM32F4xx_DSP_StdPeriph_Lib_V1.0.1\Project\STM32F4xx_StdPeriph_Examples\SDIO\uSDCard\main.c

I was using 2GB SD cards with my STM32F2 testing. ie not SDHC

These cards do need the block size to be specified before SD_ReadBlock works properly, but the USB implementation does not us this function.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
danish_enchanting
Associate II
Posted on June 20, 2012 at 17:21

The usb card 8gb SDHC is now working perfectly (detect and write operations performed) .

However the data is not read properly. when i use the same card on other PC via a card reader without ST kit the data i wrote using ST kit is present. However i am unable to read it through ST kit.

I think its due to what you said define read block size. Please tell me in which file to define it.?

Posted on June 20, 2012 at 19:00

The non-SDHC need attention in SD_ReadBlock, and SD_WriteBlock, pretty sure the USB demo doesn't use these.

if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
{
BlockSize = 512;
ReadAddr /= 512;
}
else
{
// Non High Capacity cards need block size set first, per Frank
// http://blog.frankvh.com/2011/09/04/stm32f2xx-sdio-sd-card-interface/
/*!< Set Block Size for Card */
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize;
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;
SDIO_SendCommand(&SDIO_CmdInitStructure);
errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);
if (SD_OK != errorstatus)
{
return(errorstatus);
}
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
danish_enchanting
Associate II
Posted on June 21, 2012 at 17:40

ok let me sum up for you the situation at my end.

I am using keil MSC example in usb-device-examples directory.

The SD card is connected with discovery kit. The board is detected as mass storage device with 8GB memory. The card connected is also SDHC 8GB.

I am able to  write to full sdcard (i.e.8GB).  but when i try to read the card it fails. the data i write through STM KIT is present on the card (verified using other reader) but is not readable via STM kit.

I also embedded the code you gave yesterday but no success.

Plz help me

Posted on June 21, 2012 at 17:57

I'm not sure how much specific help I can render. I'm using a custom F2 platform with a MicroSD slot, rather than jury rigging an SDIO socket to an F4-Discovery. Others have suggest the power supply on the F4-Discovery may be inadequate to hang too much additional circuitry off of it, writing would be more problematic in terms of current draw. Consider if augmenting the supply would help. I'm not sure if the F4-Discovery powers off both the USB ST-LINK and USB A-B, or if that helps or not.

For practical purposes the F2 and F4 platforms should be very similar.

What I can do is grab some SDHC cards on my break, perhaps some 4, 8 and 16 GB versions, or combination there-of.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 21, 2012 at 20:28

Ok, some 4, 8 and 16 GB microSDHC cards in hand.

The 8 GB comes up and Windows wants to format it, suggests it can get 3.4 GB. I'm almost certain the example code assumes a byte offset using a 32-bit variable, which will clearly cause much chaos and hilarity with media >4,000,000,000 bytes.

Same card comes up using the built-in reader in my PC as ~8GB formatted FAT32. 7,939,817,472 bytes or some excuse for 8GB

This should be fixable with a little more thought. 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 21, 2012 at 20:37

From usbd_msc_scsi.c

/**
* @brief SCSI_Read10
* Process Read10 command
* @param lun: Logical unit number
* @param params: Command parameters
* @retval status
*/
static int8_t SCSI_Read10(uint8_t lun , uint8_t *params)
{
if(MSC_BOT_State == BOT_IDLE) /* Idle */
{
/* case 10 : Ho <> Di */
if ((MSC_BOT_cbw.bmFlags & 0x80) != 0x80)
{
SCSI_SenseCode(MSC_BOT_cbw.bLUN,
ILLEGAL_REQUEST,
INVALID_CDB);
return -1;
}
if(USBD_STORAGE_fops->IsReady(lun) !=0 )
{
SCSI_SenseCode(lun,
NOT_READY,
MEDIUM_NOT_PRESENT);
return -1;
}
SCSI_blk_addr = (params[2] << 
24
) | \
(params[3] << 16) | \
(params[4] << 8) | \
params[5];
SCSI_blk_len = (params[7] << 8) | \
params[8];
if( SCSI_CheckAddressRange(lun, SCSI_blk_addr, SCSI_blk_len) < 0)
{
return -1; /* error */
}
MSC_BOT_State
= 
BOT_DATA_IN
;

SCSI_blk_addr *= SCSI_blk_size; // << EPIC FAIL

SCSI_blk_len *= SCSI_blk_size;
/* cases 4,5 : Hi <> Dn */
if (MSC_BOT_cbw.dDataLength != SCSI_blk_len)
{
SCSI_SenseCode(MSC_BOT_cbw.bLUN,
ILLEGAL_REQUEST,
INVALID_CDB);
return -1;
}
}
MSC_BOT_DataLen = MSC_MEDIA_PACKET;
return SCSI_ProcessRead(lun);
}

SCSI_blk_addr is a uint32_t, so EPIC FAIL there. In usbd_storage_msd.c

/**
* @brief Read data from the medium
* @param lun : logical unit number
* @param buf : Pointer to the buffer to save data
* @param blk_addr : address of 1st block to be read
* @param blk_len : nmber of blocks to be read
* @retval Status
*/
int8_t STORAGE_Read (uint8_t lun,
uint8_t *buf,
uint32_t blk_addr,
uint16_t blk_len)
{
if( SD_ReadMultiBlocks (buf,
blk_addr * 512, // << EPIC FAIL
512,
blk_len) != 0)
{
return -1;
}
#ifndef USE_STM3210C_EVAL
SD_WaitReadOperation();
while (SD_GetStatus() != SD_TRANSFER_OK);
#endif
return 0;
}

/**
* @brief return medium capacity and block size
* @param lun : logical unit number
* @param block_num : number of physical block
* @param block_size : size of a physical block
* @retval Status
*/
int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_size)
{
#ifdef USE_STM3210C_EVAL
SD_CardInfo SDCardInfo;
SD_GetCardInfo(&SDCardInfo);
#else
if(SD_GetStatus() != 0 )
{
return (-1);
}
#endif
*block_size = 512;
*block_num = SDCardInfo.CardCapacity / 512; // << SOME MORE FAIL
return (0);
}

And obviously the WRITE side too. I'll get some patched files posted shortly.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..