cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4: SD Card as the USB Mass Storage Drive

rory
Associate
Posted on December 15, 2014 at 22:02

I'm working on a project that has an on-board SD card and a USB port. I'd like to figure out how to connect the SD card to the USB as a mass storage device. Does anyone have any experience doing this and can provide some guidance?

I am currently using STM32Cube F4 firmware v1.3.0.

I have yet to find any example code that does this. Thank you!

#stm32f4-sdio-usb-msc #i-can''t-help-you-with-that
6 REPLIES 6
Posted on December 15, 2014 at 23:21

As for HAL/Cube, you are pretty much on your own there, you could fish around in the example directories it creates.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
rory
Associate
Posted on December 16, 2014 at 17:59

Wow. I don't appreciate the snark, Clive1. I have already searched in all the places you have searched. I'm aware of how to use the internet.

I was simply hoping someone had luck doing this with HAL/Cube and could point me in that direction. I understand there are lots of example out there but integrating it with my current HAL/Cube work looks messy.

bruce239955
Associate II
Posted on September 26, 2016 at 11:34

Clive1.

I've come across this post as I'm looking to implement a project with an STM32F2, where the USB is used as a MSC device, accessing SD card connected to the SDIO port.

I'm pretty new to embedded software development, so before I try and port this project, can you confirm that it would meet the desired objective, and also provide any pointers that would assist in the port.

I do have a project up an running using the STM32F207 board with an SD card attached which was generated using Cube/HAL, so in principle I do have a suitable development environment to work with.

Many thanks.

Posted on September 26, 2016 at 13:58

The F2 and F4 are very similar in this regard, just needs slower clocking, and not to compile with M4 instructions. My primary target has been an F205, and I've use the SPL to get USB MSC functionality against a MicroSD card slot.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Embedded_Engineer
Associate II
 

I am currently working on a project that involves integrating USB functionality for data transfer and data logging on an STM32F407VG micro-controller. Specifically, I want to log data files on an SD card and enable the user to download these logged files from the SD card via USB. Additionally, I'd like to allow users to copy files from their PC to the SD card via USB, all through the STM32F407VG using the SDIO communication protocol.

I'm facing a challenge in making the USB_OTG_FS (On-The-Go Full Speed USB) and the SD card (using FatFS) work together seamlessly. I would greatly appreciate your guidance and any insights you might have on how to achieve this integration.

Here are some specific questions I have:

 

  • How do I configure USB_OTG_FS as a USB Device in STM32CubeMX or other development tools? What are the essential initialization and setup steps for using the USB_OTG_FS as a USB Device alongside the FatFS library for SD card communication?
  • Are there any sample code snippets or project examples that demonstrate this integration?
  • What should I be mindful of when managing the data transfer between the USB and SD card to avoid conflicts or data corruption?
How do I configure USB_OTG_FS as a USB Device in STM32CubeMX or other development tools?
What are the essential initialization and setup steps for using the USB_OTG_FS as a USB Device alongside the FatFS library for SD card communication?
Are there any sample code snippets or project examples that demonstrate this integration?
What should I be mindful of when managing the data transfer between the USB and SD card to avoid conflicts or data corruption?

Here is my code:

#include "usbd_storage_if.h"

extern SD_HandleTypeDef hsd;

#define STORAGE_LUN_NBR 1
#define STORAGE_BLK_NBR 0x10000
#define STORAGE_BLK_SIZ 0x200

const int8_t STORAGE_Inquirydata_FS[] = {

0x00,
0x80,
0x02,
0x02,
(STANDARD_INQUIRY_DATA_LEN - 5),
0x00,
0x00,
0x00,
'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
'P', 'r', 'o', 'd', 'u', 'c', 't', ' ', /* Product : 16 Bytes */
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
'0', '.', '0' ,'1' /* Version : 4 Bytes */
};


static int8_t STORAGE_Init_FS(uint8_t lun);
static int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num, uint16_t *block_size);
static int8_t STORAGE_IsReady_FS(uint8_t lun);
static int8_t STORAGE_IsWriteProtected_FS(uint8_t lun);
static int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
static int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
static int8_t STORAGE_GetMaxLun_FS(void);

 

USBD_StorageTypeDef USBD_Storage_Interface_fops_FS =
{
    STORAGE_Init_FS,
    STORAGE_GetCapacity_FS,
    STORAGE_IsReady_FS,
    STORAGE_IsWriteProtected_FS,
    STORAGE_Read_FS,
    STORAGE_Write_FS,
    STORAGE_GetMaxLun_FS,
    (int8_t *)STORAGE_Inquirydata_FS
};

int8_t STORAGE_Init_FS(uint8_t lun)
{

    return (USBD_OK);

}


int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
{

    HAL_SD_CardInfoTypeDef info;
    int8_t ret = -1;

    HAL_SD_GetCardInfo(&hsd, &info);

    *block_num = info.LogBlockNbr - 1;
    *block_size = info.LogBlockSize;
    ret = 0;
    return ret;


}


int8_t STORAGE_IsReady_FS(uint8_t lun)
{

    return (USBD_OK);

}

int8_t STORAGE_IsWriteProtected_FS(uint8_t lun)
{

    return (USBD_OK);

}


int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{

    int8_t ret = -1;

    HAL_SD_ReadBlocks(&hsd, buf, blk_addr, blk_len, HAL_MAX_DELAY);

    /* Wait until SD card is ready to use for new operation */
    while (HAL_SD_GetCardState(&hsd) != HAL_SD_CARD_TRANSFER){}
    ret = 0;
    return ret;

}

int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{

    int8_t ret = -1;

    HAL_SD_WriteBlocks(&hsd, buf, blk_addr, blk_len, HAL_MAX_DELAY);

    while (HAL_SD_GetCardState(&hsd) != HAL_SD_CARD_TRANSFER){}
    ret = 0;
    return ret;

}

int8_t STORAGE_GetMaxLun_FS(void)
{

    return (STORAGE_LUN_NBR - 1);

}