cancel
Showing results for 
Search instead for 
Did you mean: 

SD Card + SPI interface + USB Device Mass Storage?

juansebastian16
Associate

Hi! I am trying to connect my code (SD in SPI mode) to USB Device Mass Storage. I have tested my SD with SPI interface writing and reading blocks and files with FATFS and it works without problems... But when I connect it with STORAGE_Write_FS and STORAGE_Read_FS the computer ask me to format the unit... The computer read properly the card capacity but only when it is going to format the unit. I don't know what to do to solve this.

I share the code lines below:

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

{

 /* USER CODE BEGIN 3 */ //// storage is a extern variable that reads the card capacity (this is OK)

 *block_num = storage / BLOCK_SIZE; //fre_sect/2 / BLOCK_SIZE;

 *block_size = BLOCK_SIZE;

 return (USBD_OK);

 /* USER CODE END 3 */

}

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

{

 /* USER CODE BEGIN 6 */

uint64_t br=0;

while(br<blk_len)

{

SD_Read_Block(buf, blk_addr);br++;

}

HAL_UART_Transmit(&huart2, (uint8_t*)"READ_USB", sizeof("READ_USB"),100);

 return (USBD_OK);

 /* USER CODE END 6 */

}

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

{

 /* USER CODE BEGIN 7 */

uint64_t bw=0;

while(bw<blk_len)

{

SD_Write_Block(buf, blk_addr);bw++;

}

HAL_UART_Transmit(&huart2, (uint8_t*)"WRITE_USB", sizeof("WRITE_USB"),100);

 return (USBD_OK);

 /* USER CODE END 7 */

}

3 REPLIES 3
ABatt.1
Senior

Hi juansebastian16,

I have same problem. Did you resolved it?

I think is a problem related to "STORAGE_Init_FS" but I don't find any ideas about thi function.

I'm tring to make a USB device Mass storage with an SDcard connected trough SPI.

My Hardware is a NUCEO-F401RE

It is the initialization function for the interface and card.

I would suggest that you test the SPI / SDCARD functionality externally from the USB MSC, either at the sector read/write level, or as integrated with FatFs

One the Nucleo side there is some code example within the AdaFruit SD/SPI shield, should be good for single sector reads. They might have fixed the multi-sector at this point.

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

HI,

I can access the SD Card with the FATFS library through both SPI and SDIO. I can write and read files on the SD correctly.

When I activate the USB, Windows correctly recognizes the device (enumeration and descriptors) but does not see any information about the me.

The problem is not the SD Card, but set the USB correctly.

Is there any example of usbd_storage_if.c both via SPI and SDIO?