Top.Mail.Ru

USB Mass Storage device with STM32 microcontroller and SD-card.

Within series of articles devoted to STM32CubeMx we start discussing different USB modes. And today we'll realize USB Mass Storage Device class with SD-Card connected to the MCU. Thus, microcontroller STM32F10x acting as a card reader will be the result of this post 🙂

As I've mentioned at the beggining of the post, I'll use STM32F10x microcontroller. SD-card will be connected via SDIO interface. So, let's run STM32CubeMx and begin the configuring of MCU units! First of all, we should enable high-speed external oscillator, USB and SDIO units. Moreover, the proper USB mode should be selected. In this case it is Mass Storage Class mode:

Mass Storage Class

Please note that when we enable any microcontroller unit, STM32CubeMx automatically marks all involved pins. In addition, I'll use PA10 as USB_DISCONNECT_PIN - it is responsible for software connection and disconnection of USB. So, I'll configure this GPIO as "GPIO_Output".

General configuration is quite clear, and now we proceed to the "Clock configuration" tab. All the frequencies can be set there. While working with USB, the most important point of clock configuration is 48 MHz at USB clock:

STM32Cube clock settings

After that we can do some extra configuration. In order to do this let's open the "Configuration" tab and click on SDIO unit. As a result a new window will appear, where we would be able to set SDIO clock prescaler:

STM32Cube SDIO settings

In principle, after that all units are configured properly, so we can start the project generation process!

When the generation is finished, the project is ready for programming the MCU, but we have to make some modifications in the source files. Firstly, I'll reset the output state of USB_DISCONNECT_PIN:

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SDIO_SD_Init();
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_RESET);
MX_USB_DEVICE_Init();

As you see, Cube has already initialised SDIO and USB, but it doesn't take care about relations between these units. Thus, we should edit usbd_storage_if.c file, where all the functions needed for USB interface are located:

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

STMCubeMx doesn't fill this functions, so they are empty. Let's edit some of them in order to make our device working! Firstly, we should correct the block size:

#define BLOCK_SIZE                                             512

Secondly, STORAGE_GetCapacity_FS() function has to be modified. To get SD-card full size we can use HAL_SD_Get_CardInfo(). This function takes two arguments which are defined in the main.c file. So, they should be defined in usbd_storage_if.c file too:

extern SD_HandleTypeDef hsd;
extern HAL_SD_CardInfoTypedef SDCardInfo;

STORAGE_GetCapacity_FS() should return the block size and the number of blocks, and now we can calculate it. We should just divide the total size by the size of one block:

int8_t STORAGE_GetCapacity_FS (uint8_t lun, uint32_t *block_num, uint16_t *block_size)
{
	/* USER CODE BEGIN 3 */
	HAL_SD_Get_CardInfo(&hsd, &SDCardInfo);
	*block_num  = SDCardInfo.CardCapacity / BLOCK_SIZE;
	*block_size = BLOCK_SIZE;
	return (USBD_OK);
	/* USER CODE END 3 */ 
}

Thirdly, the read and write functions should be implemented:

/****************************************************************************************
* Function Name            : STORAGE_Read_FS
* Description              : 
* Input                    : None.
* Output                   : None.
* Return                   : None.
****************************************************************************************/
int8_t STORAGE_Read_FS(uint8_t lun, 
                       uint8_t *buf, 
                       uint32_t blk_addr,
                       uint16_t blk_len)
{
	/* USER CODE BEGIN 6 */
	HAL_SD_ReadBlocks(&hsd, (uint32_t*)buf, (uint64_t)(blk_addr * BLOCK_SIZE), BLOCK_SIZE, blk_len);

	return (USBD_OK);
	/* USER CODE END 6 */
}


/****************************************************************************************
* Function Name            : STORAGE_Write_FS
* Description              :
* Input                    : None.
* Output                   : None.
* Return                   : None.
****************************************************************************************/
int8_t STORAGE_Write_FS(uint8_t lun, 
                        uint8_t *buf, 
                        uint32_t blk_addr,
                        uint16_t blk_len)
{
	/* USER CODE BEGIN 7 */
	HAL_SD_WriteBlocks(&hsd, (uint32_t*)buf, (uint64_t)(blk_addr * BLOCK_SIZE), BLOCK_SIZE, blk_len);

	return (USBD_OK);
	/* USER CODE END 7 */
}

We've just added the code which will read/write data from/to SD-card connected to SDIO interface.

Important note!

HAL_SD_ReadBlocks() and HAL_SD_WriteBlocks() functions' prototypes have been changed in the newest STM32CubeMx versions. So now we should rewrite USB callbacks in such a way:

/****************************************************************************************
* Function Name            : STORAGE_Read_FS
* Description              : 
* Input                    : None.
* Output                   : None.
* Return                   : None.
****************************************************************************************/
int8_t STORAGE_Read_FS(uint8_t lun, 
                       uint8_t *buf, 
                       uint32_t blk_addr,
                       uint16_t blk_len)
{
	/* USER CODE BEGIN 6 */
	HAL_SD_ReadBlocks(&hsd, buf, blk_addr, (uint32_t) blk_len, 10);

	return (USBD_OK);
	/* USER CODE END 6 */
}


/****************************************************************************************
* Function Name            : STORAGE_Write_FS
* Description              :
* Input                    : None.
* Output                   : None.
* Return                   : None.
****************************************************************************************/
int8_t STORAGE_Write_FS(uint8_t lun, 
                        uint8_t *buf, 
                        uint32_t blk_addr,
                        uint16_t blk_len)
{
	/* USER CODE BEGIN 7 */
	HAL_SD_WriteBlocks(&hsd, buf, blk_addr, (uint32_t) blk_len, 10);

	return (USBD_OK);
	/* USER CODE END 7 */
}


Where '10' is a timeout value for read/write operations.

Now the project is ready for compiling and programming the MCU! If we connect STM32 to the PC, we'll see a new USB Mass storage device in our system. Thus, we can create, edit and delete files on SD-card.

Well, I'll stop here, if you have any questions, you can add a comment on this post, I'll be very glad to help you!

If you have problems with USB you can try to increase Stack_size and Heap_size in startup_stm32xxxx.s/ Sometimes it helps 🙂

Подписаться
Уведомить о
guest

141 комментариев
Старые
Новые
Межтекстовые Отзывы
Посмотреть все комментарии
Kaf
Kaf
8 лет назад

Hi Aveal,
I try to do the MSC-FATfs connection on a custom board but fail at GetCapacity with getting zeros back. How does the underlying function know which SD card I wan to use? Is is correct to pass an empty/undefined hsd?

Thanks, Kaf

Kaf
Kaf
Ответ на комментарий  Aveal
8 лет назад

Any idea where it does that? MX_FATFS_Init() links the driver to the SDcard but doesn't seem to initialize anything. MX_SDIO_SD_Init() seems to initialize the low-lever stuff only.

What version of CubeMX are you actualy using? My CubeMX-generated usbd_storage_if.c looks a bit different anyway, block number and sizes are pre-defined, probably to have a default. Here's an excerpt:
[...]
#define STORAGE_LUN_NBR 1
#define STORAGE_BLK_NBR 0x10000
#define STORAGE_BLK_SIZ 0x200
[...]
int8_t STORAGE_GetCapacity_FS (uint8_t lun, uint32_t *block_num, uint16_t *block_size)
{
/* USER CODE BEGIN 3 */
*block_size = STORAGE_BLK_SIZ;
*block_num = STORAGE_BLK_NBR;
return (USBD_OK);
/* USER CODE END 3 */
}

FATFS
FATFS
Ответ на комментарий  Aveal
7 лет назад

what does mySize and myCount mean ? I have same problem : can't use fatfs with your tutorial

Germán
Germán
8 лет назад

Hi, Thank you very much for take the time to make this tutorial. I am working in a project in which i have to access to an eeprom instead of an sd. Could you bring me some help with how i have to configure the libraries?

Thank you.

Germán
Germán
Ответ на комментарий  Aveal
8 лет назад

Hi Aveal,

Thanks for your response. Did you get something like this working? I am also in trouble with the inicialization of the i2c with thehal libraries. I can not understand at all how i have to modify the function:

__weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(hi2c);
/* NOTE : This function should not be modified, when the callback is needed,
the HAL_I2C_MspInit could be implemented in the user file
*/
}

Thanks again,

Germán
Germán
Ответ на комментарий  Aveal
8 лет назад

Are you sure? I guess that the cube utility inicialization rutines dont configure the i2c pins as open drain.

Germán
Germán
8 лет назад

Hi, i could build the read/write eeprom rutines. But i dont know what parameters of:

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

i have to pass to the read/write eeprom rutines. I understand that de minimal block size is 512 bytes but my eeprom buffer is 64 bytes, so that means that i have to do something in the middle. Could you please help with this?

Germán
Germán
Ответ на комментарий  Aveal
8 лет назад

ok, so it means that Buf can point between 1 and 512 bytes. Thats write?

How is organize the blk address? If i had 16 blocks of 512 bytes, the blk addresses that i receive from the function parameters would be 0,1,2,3,...,14,15 ?

Thanks for your time

Germán
Germán
Ответ на комментарий  Aveal
8 лет назад

I have an eeprom 24lc256. It has 32KBytes. There are 64 blocks of 512 bytes. That would be enough?

So, if in write funcion parameters i have: blk_add = 5 and blk_len = 3, it means that i have to write 1536 bytes ( 512 in block 5 , 512 in block 6 and 512 in block 7). It's that right?

Thanks for your time

Germán
Germán
Ответ на комментарий  Aveal
8 лет назад

thank you very much for your help!! i will be continue working with this.

Germán
Germán
8 лет назад

I have build this rutine:

int8_t STORAGE_Read_FS (uint8_t lun,
uint8_t *buf,
uint32_t blk_addr,
uint16_t blk_len)
{
/* USER CODE BEGIN 6 */

char y[100];

sprintf(&y[0],"%d", blk_addr);
LCD_Puts(0,0,&y[0]);

int i,j,k=0;
uint16_t a;
uint8_t aux;
a = blk_addr*512;

for(i=0;i<blk_len;i++)
{
for(j=0;j<512;j++)
{
eeRead(a,&aux,1);
buf[k]=aux;
a++;
k++;
}
}

return (USBD_OK);
/* USER CODE END 6 */
}

and other similar to write, the problem is that now, windows not recognize my device.

Mathew
Mathew
Ответ на комментарий  Aveal
8 лет назад

Hi, I have a problem with connecting device to computer. There is an info in device manager (on win10) that "request for the usb device descriptor failed". I'm using SDIO 1bit mode (which works fine, because I can read/write files using FatFs). Could this 1bit mode be a reason why USB doesn't work?
I've done rest of things like above.

ivoras
8 лет назад

Hi!

I'm new to STM32 and STM32cube and I'm using a STM32F103C MCU without the SDIO peripheral. The generated code doesn't contain diskio.c, only user_diskio.c with empty functions. I've searched for a SPI diskio drive and can only find ones for the STM32F4, which is not compatible with this smaller chip.

Any ideas how to do this with SPI?

Ali
Ali
7 лет назад

hi Aveal
tanx for your tutorial...
i do just like above in my program with stm32f407vgt6 .
but mass storage not recognized completely...
my cube version(newer) differ with your version and in usbd_storage_if.c BLOCK_SIZE not define...
i don't know what's the problem...Can you tell me?

teager300
teager300
Ответ на комментарий  Aveal
7 лет назад

Hello Ali,
Did you solve this problem?

ali
ali
Ответ на комментарий  teager300
7 лет назад

Hi
unfortunately not yet ... but i work in on it:)

Hesam
Hesam
Ответ на комментарий  ali
7 лет назад

Hi Ali
I have same problem , did you solve it ?

Khan
Khan
Ответ на комментарий  teager300
4 лет назад

Hi Ali, I have the same problem. Have you found any solution yet.

uyghur
7 лет назад

error 10.. this device not start

anh
anh
Ответ на комментарий  Aveal
7 лет назад

ok. I am going to try.

anh
anh
Ответ на комментарий  Aveal
7 лет назад

after increase Stack and Heap size. I have to format my SD card. what is solution for this problem?

uyghur
Ответ на комментарий  Aveal
7 лет назад

my files was not saving. help me please!!!

chicdoo
Ответ на комментарий  Aveal
7 лет назад

Thanks for ur advise ^^;

uyghur
7 лет назад

Hi bro! My project was run. but i can't read or copy files to micro sd card. It was not storage.

FATFS
FATFS
Ответ на комментарий  Aveal
7 лет назад

Hi Aveal. This is my result and I want share for you to thank you https://community.st.com/message/146995-is-this-the-bug-of-stm32cubemx

FATFS
FATFS
Ответ на комментарий  Aveal
7 лет назад

I use sd card as your tutorial. using FATFS and after code was generated. I add
if (HAL_SD_Init(&hsd, &SDCardInfo) != SD_OK)
{
Error_Handler();
}
HAL_SD_WideBusOperation_Config(&hsd, SDIO_BUS_WIDE_4B);
in the last of function void MX_SDIO_SD_Init(void) {}
Code was run correctly

FATFS
FATFS
Ответ на комментарий  Aveal
7 лет назад

can you give me your facebook. I think I have to learn you 🙂

anh
anh
7 лет назад

thanks for your help. my code was run correctly

peiman
peiman
7 лет назад

Thanks for the tutorial, may I ask how much is the maximum data transfer in this configuration? I mean how much time will it take to copy a file to pc in this method.

Roxana
Roxana
7 лет назад

Hi,
Thanks for your helpful tutorial.
I use cubemx 4.15.1. I active SD card with fatfs and USB msc in device mode for stm32f429. and the tips which you mentioned I also did in the programs. Besides, when I connect the board to PC which recognize it as usb device; however, there is no new drivers or files in my computer to show SD card.

Roxana
Roxana
Ответ на комментарий  Aveal
7 лет назад

yes. In devices and printers we can see new device; but nothing in my computer as a new driver.

Roxana
Roxana
Ответ на комментарий  Aveal
7 лет назад

yes it works properly.

Roxana
Roxana
Ответ на комментарий  Aveal
7 лет назад

Thanks,
I have already sent it.

Roxana
Roxana
Ответ на комментарий  Aveal
7 лет назад

thanks it works

Hesam
Hesam
Ответ на комментарий  Aveal
7 лет назад

Hi
I have same problem , can you help me please ??
I use stm32f407 with sdio and usb FS.
thanks ..

rakesh rajbhar
rakesh rajbhar
7 лет назад

hello aveal,

I am evaluating the STM32f4 discovery board. I want to implement the USB mass storage functionality.I interfaced the SD card using the SDIO 1-bit mode.But, I want to share SD card's data in the PC use of USB Mass storage option.im able to read and write data to SD card using FATFS.but now i just want to implement USB part.

thanks
rakesh

rakesh rajbhar
rakesh rajbhar
Ответ на комментарий  Aveal
7 лет назад

thanks for your quick response aveal

i followed your instruction.but still my usb is not detecting by my PC.i generated code using cubemx 4.12.0.i configured SDIO for 1 bit mode.sd card's part was done by some one else. so i need to de USB part.so how to start with this i just did as you said.and i already have
#define STORAGE_LUN_NBR 1
#define STORAGE_BLK_NBR 0x10000
#define STORAGE_BLK_SIZ 0x200.

so should i again define BLOCK_SIZE?
and what should i pass in lun?

please help me.

thanks

rakesh rajbhar
rakesh rajbhar
Ответ на комментарий  Aveal
7 лет назад

hello sir i have tried all the solution given in this post but still my usb in not even getting detected by my PC.can we have usb mass storage functionality with sdio 1-bit?.i dont know what wrong with my code?

please help me.

rakesh rajbhar
rakesh rajbhar
Ответ на комментарий  Aveal
7 лет назад

and one more thing sir my device is shows as mass storage in device and printers,and in device manager also but not in my computer.so what shoul i do now?is there may be problem with code?

rakesh rajbhar
rakesh rajbhar
Ответ на комментарий  Aveal
7 лет назад

please help me sir if possible,i am stuck here.

rakesh rajbhar
rakesh rajbhar
Ответ на комментарий  Aveal
7 лет назад

yes
sd card works properly.and i followed your steps only for usb.can i mail you my code for your better understanding?

rakesh rajbhar
rakesh rajbhar
Ответ на комментарий  Aveal
7 лет назад

hello sir,

i have observed one thing that after calling bellow function
void MX_USB_DEVICE_Init(void)
{
/* Init Device Library,Add Supported Class and Start the library*/
USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS);
USBD_RegisterClass(&hUsbDeviceFS, &USBD_MSC);
USBD_MSC_RegisterStorage(&hUsbDeviceFS, &USBD_Storage_Interface_fops_FS);
USBD_Start(&hUsbDeviceFS);
}
where USBD_MSC_RegisterStorage function is called but in this function argument &USBD_Storage_Interface_fops_FS is not calling the structure which is having function pointers.
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,
};
this is not called.

so where may be the problem sir?

rakesh rajbhar
rakesh rajbhar
Ответ на комментарий  Aveal
7 лет назад

hello sir,

i have already sent you a mail.
sir after increasing my stack and heap size i got the drive in computer drive,but it is asking for format every time i try to open it.and at the time of
formatting it is showing just 32 mb,in dick capacity.so what should i do now?please help me to solve this issue.

thanks,

rakesh rajbhar
rakesh rajbhar
7 лет назад

hello sir,

thanks a lot for your tutorial and your help,i got my usb mass storage working,but its taking too much time to detect the memory card.so how to solve this problem?

Sherry
Sherry
7 лет назад

Hi guy. In the stm32 cube mx 4.20 the genered code do not have HAL_SD_CardInfoTypedef and some function. will you fix your tutorial?

John_R
John_R
7 лет назад

Hello,
Anyone resolve a problem with formatting 32MB when USB is connected to PC ? I'm using STM32L496AG with FatFS and CubeMX4.20.
Can anyone help me with run the project, please i'm stuck 🙁

John_R
John_R
Ответ на комментарий  Aveal
7 лет назад

SDHC 8GB, i resolve that problem but if I try read and write via DMA, SDcard not working 🙁 Have you got any example code for SDIO DMA ? 🙂

John_R
John_R
Ответ на комментарий  Aveal
7 лет назад

No, I don't need USB with DMA. I just need only SD Card working with DMA, because my code not working 🙁

Ahmed
Ahmed
7 лет назад

Hi,
Thank You for your effort, I made the project and it works very well.
The problem now is the speed, how can I increase the speed of read/write, and I wounder why the speed is very low it's about 200KB/s write and 600KB/s read ?
Thanks

Ahmed
Ahmed
Ответ на комментарий  Aveal
7 лет назад

I wounder if I Use High-speed core will the speed much increase or it will be a little bit different ?

John_R
John_R
Ответ на комментарий  Ahmed
7 лет назад

I have the same problem, I try to transfer 1GB file and I got 350 KB/s during a
write and 500 KB/s during a read. But I think DMA will be increase that speed.

Ahmed
Ahmed
Ответ на комментарий  John_R
7 лет назад

The problem is that DMA is Memory -> peripheral, So how can I configure it to work with USB and SDIO ?

John_R
John_R
Ответ на комментарий  Aveal
7 лет назад

I increased data transfer to 1 MB/s by add 4096 data block to write but it's all,.
DMA not working for my project in new CubeMx - STM32L496 🙁 anyone can send me DMA SDIO example in new CubeMx ? Please 🙂
this is my mail: johnrray5@gmail.com

Steve
Steve
7 лет назад

Hello Aveal,

Thank you for this amazing tutorial! This is exactly what I was trying to do!

Quick question, would it be feasible to get the file name that is currently being accessed?
Again thank you so much!

Sercan
Sercan
7 лет назад

Hı Gays,
I am working STM32F103C8T6 , but, STM32F103C8T6 isn't SDIO,
otherwise, ı have worked SD card over SPI and ı have achieved.
but ı didnt work Mass storage class SD card with SPI ,

this page ; extern SD_HandleTypeDef hsd;
extern HAL_SD_CardInfoTypedef SDCardInfo; not for SPI because ıt is fall ,

What are you doing this situation?
Please help me 😉

sercan
sercan
Ответ на комментарий  Aveal
7 лет назад

Hı,
maybe you dont understand me , ı already made them but I didnt just make Mass storage class SD card with SPI , would you like to share example links ?

See you letter..

sercan
sercan
Ответ на комментарий  Aveal
7 лет назад

Hı ,
Please help me ,
I just rewrite STORAGE_Write_FS/STORAGE_Read_FS functions in order to make them work with my SPI SD-card functions.
but; ı dont make it,
ın order to SPI this problem is solution please,

sercan
sercan
Ответ на комментарий  Aveal
7 лет назад

hı,

My project send to mail ,
Best Regards,

thank you very much ,,,

hamid
hamid
7 лет назад

Hello;
High appreciate for your help and your great tutorial. I have already done everything according to your tutorial. when i connect my SD card to computer, my computer knows mass storage device and also shows in my computer but i can not format it. is it related to codes?
can you please help me and give me solution?
best regards
hamid

hamid
hamid
Ответ на комментарий  Aveal
7 лет назад

hi thanks for your answer, i am using sdio, i am using STM32F103RCTxxx, and MDK5. i write and configure all codes according to your tutorial , after loading on my mcu, and connect my board to computer, it shows the drive and even it shows the capacity of my sd card correct (8G) but hint to format for using driver, when i am going to format windows can not format the driver.
i will appreciate if u can help me. i am using windows 7.

hamid
hamid
Ответ на комментарий  Aveal
7 лет назад

hello again, I have tested again and it is working. but here is another problem. When i put my sd card inside card reader and program my MCU, when i disconnect my Jtag and connect my board to pc, windows ask me to format my sd card, when i am going to format my sd card, windows can not format it, i went to disk manger and i found it there is no file system for my sd card, that means i have a SD card raw file system. so i did this way and it works, i format my sd card with my mobile phone and erase my stm32f103 and program and verified it again, then i put sd card inside slot and it starts to work without asking for format, when i disconnect and connect it again, it asked for format again but i cancel format and after several second it open my sd card and all files been there. sorry for writing too long, but i will appreciate if u can help me and give me some help. is it related to SDIOCLK clock divide factor because i set it 3 according to your tutorial. also speed of read and write is a bit slow.
I hope you can help me because i spend to much time and it is not working properly .

Manh
Ответ на комментарий  hamid
7 лет назад

Hello Hamid, I have been struggling with this problem for several weeks.
Have been you success with this yet??? and how can you fix this.

Ali
Ali
7 лет назад

Hello Hesam
it solved...
the problem is heap size and stack size ...
put heap size=0x2000 and stack size=0x4000 and it works

sercan
sercan
7 лет назад

Again hı,

I am using stm32f103c8t6, ı am working about STM32F103C8T6 sd card(fatfs)+usb mass storage over spı

Please help me.........

AageM
AageM
7 лет назад

Hi folks,

I was trying to reproduce the project with STM32Cube V 4.22.0. I used the SDIO 1 bit and USB mass storage peripheral. Reading the SD capacity is going well but the read blocks function doesn't respond as expected. When I'd a look deeper into the function I found out that I got an RX_OVERRUN error. Does somebody has the same issue? I'm looking for a way to solve this, are there any suggestions?

Kind regards

AageM
AageM
Ответ на комментарий  AageM
7 лет назад

Got it, just initialized a fifo buffer. But know when I try to read the first block of the SD with the function "HAL_SD_ReadBlocks" I get different data as when I read the SD card with WinHex on the computer. Both ways shows me the fat signature in the 510'th and 511'th bit but the data isn't corresponding with each other. I search the whole SD card in WinHex to find the data which I got back from the "HAL_SD_ReadBlocks" function. But I wasn't able to find it with WinHex. It looks strange to me, am I missing something important? The USB device which appear in windows still asked to be formatted. I'm working with the STM32F413. Cheers!

AageM
AageM
7 лет назад

Alright guys,

Got it working. I'd to change the read function into
HAL_SD_ReadBlocks(&hsd, buf, blk_addr, blk_len, 10);

And the write function into
HAL_SD_WriteBlocks(&hsd, buf, blk_addr, blk_len, 10);

In combination with enlarging the heap and stack size and initialize the rx and tx buffers for the sdio it should work.

Good luck.

Marina Dioto
Marina Dioto
Ответ на комментарий  AageM
6 лет назад

Could you share how did you initialize the rx and tx buffers for the sdio, please?

Piotr O
6 лет назад

hi,
I have working example with SDIO one wire and USB storage device (STM32F407VGT6 DISCOVERY, SD CARD 4Gb). I saved a few files by fatfs and that also work correctly. I have one problem: when I connect MCU to PC I have acces to files. I can copy file to PC, but I have problem with deleting files. When I choose all files and click delete, after reconnection USB to computer a 4 files apear. Every times I must to delete every one file by file. What am I doing wrong? Every steps were made by your tutorial with different name of function because I have latest version. Can you share with your experience with deleting multiple files from storage device on STM with SD CARD.
Kind regards

Marina Dioto
Marina Dioto
6 лет назад

Hello!!
I followed your tutorial and my PC recognizes the USB Mass Storage Device, but it shows a message saying that the location is not available "E:/ is not accessible. Incorrect function"
And I cannot access the files inside the card..
Can you help me, please?
Thank you a lot!!

Marina Dioto
Marina Dioto
Ответ на комментарий  Aveal
6 лет назад

My code was already with those modifications... Even with them, the files are not accessible..

Marina Dioto
Marina Dioto
Ответ на комментарий  Marina Dioto
6 лет назад

The error is on the HAL_ReadBlocks.. it gives a SDIO_FLAG_RXOVERR: Received FIFO overrun error
I don`t know what is causing it and what should I change.. could you help me?

Patryk
Patryk
Ответ на комментарий  Marina Dioto
6 лет назад

I had this problem. Just increase the stack. By defult cube generate stack size of 0x400. And one 0 (0x4000) and it should be ok.

Chris
Chris
Ответ на комментарий  Marina Dioto
4 лет назад

I had this problem and slowing down the clock helped, using the SDIO ClockDiv setting.

Unnati
Unnati
6 лет назад

Hello,
I followed your tutorial for implementing USB MASS STORAGE on STM32L053 with external data flash of 2 MB(page size of 256 bytes) for mass storage using SPI.
when i plug in the device into pc usb port, it gets detected immediately in device manager->USB SERIAL BUS CONTROLLERS-> USB MASS STORAGE DEVICE. but it takes around 1 minute to get detected in device manager->portable devices-> removable disk G:\ . why is it taking so long to get detected?
also when i try to check the properties of the disk, it shows 0 bytes capacity. why is it not showing any bytes in capacity?

i am looking for the ways to solve it.

kind regards

Unnati
Unnati
Ответ на комментарий  Aveal
6 лет назад

Hello!
Thanks for the help, i tried changing the method of reading flash and ended up with following:
when i plug in the device into pc usb port, it gets detected immediately in device manager->USB SERIAL BUS CONTROLLERS-> USB MASS STORAGE DEVICE and also device manager->portable devices-> removable disk G:\ .
but a window pops up asking "you need to format the disk G:\ before you can use it", then when we select the format option, another window pops up showing the capacity of disk, where it shows correct capacity as 1.95 MB, but further no format action takes place.
But when i check the properties of the removable disk G:\ in my computer, it shows 0 bytes capacity.

i don't understand where am i going wrong!! i have not yet implemented FATfs, can it be the reason?

Unnati
Unnati
Ответ на комментарий  Aveal
6 лет назад

I am trying to implement FatFs in my project. as of now i am not implementing USB, want test only FatFs first by creating file and storing it in SPI FLASH. my part of code for FatFs is as follows:

uint8_t retUSER; /* Return value for USER */
char USERPath[4]; /* USER logical drive path */
FATFS USERFatFS; /* File system object for USER logical drive */
FIL USERFile; /* File object for USER */

res = FATFS_LinkDriver(&USER_Driver, USERPath);
if(res == 0)
{
res = f_mount(&USERFatFS, (TCHAR const*)USERPath, 0);
if(res != FR_OK)
{
error_handler();
}
else
{ ....}
}
but an error of FR_NO_FILESYSTEM occurs when f_mount is executed.It can be due to no valid FAT volume on the drive or wrong low layer implementation.

how to implement FAT volume on external SPI FLASH? can you give some suggestions. i am not able to find anything relevent for SPI FLASH.

ofer
ofer
6 лет назад

Hi
My application needs to manage MSC profile of USB with SD-SARD communicating by sdio (4-bits).
When I push the USB into my computer i'm getting a new small window with the message "\:D The directory name is invalid".
Using the explorer I can see that there is a new drive "D". When I try to open it just by clicking I'm getting the same message ("The directory name is invalid").
When I open the device drivers I can see "USB mass storage Device" between the USB controllers.
I'm using Atollic true (version 9.0) as my IDE.
What can be the problem ?
Thanks.

mostafa
mostafa
6 лет назад

Hi
I use mass storage sample from STM32Cube_FW_F4_V1.21.0\Projects\STM324xG_EVAL\Applications\USB_Device\MSC_Standalone

When I format a partition with a NTFS system file and copy a large file to it, if device resets during the copy, the partition is corrupted and requests formatting. in FAT32 an extFAT i dont have this problem!
Please guide where is the problem?

Issa
Issa
6 лет назад

Hi,
I did the same steps mentioned on this post. My mcu is stm32f405 and i'm using SDIO 4 bits + USB full speed stack. I am able to see the device and read all the files (big files and small files) when i plug the usb connector of my board to the desktop. However, I am unable to write or delete files although when writing to the SD card it shows that write is complete. I am unable to do a quick format too...Has anyone encountered this issue?

Issa
Issa
Ответ на комментарий  Aveal
6 лет назад

yes I tried it with a 4 GB and an 8GB micro SD

Issa
Issa
Ответ на комментарий  Aveal
6 лет назад

Thank you very much Aveal for your help ... It works ...

Issa
Issa
Ответ на комментарий  Aveal
6 лет назад

the solution is to wait the writing process to be accomplished like below:

if (HAL_SD_WriteBlocks(&hsd, buf, blk_addr, (uint32_t) blk_len, 10) == HAL_OK)
{
/* Wait that writing process is completed or a timeout occurs */

timeout = HAL_GetTick();

while((HAL_GetTick() - timeout) < 30 * 1000)
{
if (BSP_SD_GetCardState() == 0)
{

break;
}
}
}

YFrickx
YFrickx
6 лет назад

Hi, what hardware configuration do you use? I am working on something similar with the stm32f446 but since this chip needs 3.3V as power and USB is 5V I am not sure how to connect everything

Parthasaradhi
Parthasaradhi
6 лет назад

Hi Aveal,
Thank you for the post. Though I got the USB working, I was getting disk error, cause I did not get the SD card data but put some sixed data.

Edmond
Edmond
5 лет назад

Hi Aveal,
Thank you for sharing your experience with us.
I follow the procedure as you explained. However, I cannot see the content of SD card by my PC using USB mass storage. I implement my project using stmcubemx version 4.21.0. The SD card works (meaning that I can create a .txt file and write something into it. When I put the SD card into adapter and connect to PC, I can also see its contents, as expected).

However, i can not see its contents using USB mass storage! I am really not sure whether the SD card can be truly initialized. Also dont know whether cubmx-generate-code (version 4.21.0) is reliable or must be modified?
Moreover, I cannot watch buf, blk_addr, blk_len in live watch to see what such arguments contain!

It really blocks me for a while! Would you please help me?
Infinite thanks in advance!
Edmond

Boer
Boer
4 лет назад

Can you show me the hardware you used?
is it just the chip and direct connection, do i need some resistor for current restrictions?

Pranjal Bhagat
4 лет назад

Hello Aveal, i just want to use extarnal flash instead of sd card???? Do you have any example code for me??? I want to use stm32f103cxx... i will make github repo. If it works, so anyone can access the code without wasting there valuable time on rewriting the whole code. Thank you.

Maniteja
Maniteja
3 лет назад

Hi, Thanks for your Tutorial.
I tried same as what you have done. But am Not able to detect my sd card using the board stm32f4-disc. Where I missed the flow . Can you just help me in this .

mugheesnawaz
3 лет назад

Can you please share the source code?

JellyCombMX
JellyCombMX
3 лет назад

Hi Aveal,
Great tutorial, thank you very much !
Unfortunatly I cant get the STORAGE_Read_FS/STORAGE_Write_FS functions to work. I am using SPI mode in order to access the SD Card. Everything works fine except the USB connection. I cant access the SD Card via USB.
Could you please help me out ?

141
0
Оставьте комментарий! Напишите, что думаете по поводу статьи.x