cancel
Showing results for 
Search instead for 
Did you mean: 

SDIO 4 bit mode with STM32CubeMX

djhtml
Associate II

I would like to use SDIO with 4 bit mode with FatFS. I use STM32F429I Discovery board and I wired a micro SD card board to it. The SD card is a 16 GB SanDisk Ultra A1 card formatted to FAT32.

This is the micro SD card board: https://image.dhgate.com/0x0s/f2-albu-g4-M01-BC-F9-rBVaEVmcHSeAdh6CAAKj2N4sVnA447.jpg/3-3v-5v-micro-sd-tf-card-reader-module-spi.jpg all lines are pulled up to 3V with 10k res (even the CLK).

Version of STM32CubeMX is 4.26.1, firmware version is STM32Cube FW_F4 V1.21.0, I use Atollic TrueSTUDIO 9.0.0 and FatFS version is R0.12c.

If I generate SDIO with 1 bit mode with CubeMX it works fine, I can read, write the card.

When I generate SDIO with 4 bit mode it is not working. Function 'f_mount' is returning with error code: 13 which is 'FR_NO_FILESYSTEM'. But there is a working file system on the card, I can use it with PC and it works with 1 bit SDIO mode also.

This is the relevant part of the code (all other part is generated with CubeMX):

// ...
 
uint8_t myWrite[30] = "TEST STRING";
uint8_t myRead[30];
 
// ...
 
int main(void)
{
	
	// ...
	
	printf("Start!\r\n");
 
	FATFS myFAT;
	FIL myFile;
	UINT byteCount;
 
	printf("path: '%s'\r\n", SDPath);
 
	FRESULT fRet = 0;
 
	fRet = f_mount(&myFAT, SDPath, 1);
	if(fRet == FR_OK){
		f_open(&myFile, "test.txt", FA_WRITE | FA_CREATE_ALWAYS);
		f_write(&myFile, myWrite, 30, &byteCount);
		f_close(&myFile);
 
		f_open(&myFile, "test.txt", FA_READ);
		f_read(&myFile, myRead, 5, &byteCount);
		f_close(&myFile);
	}else{
		printf("mount fail :(\r\n");
		printf("err. code: %d\r\n", fRet);
	}
	
	// ...
	
	while (1)
	{
		// ...
	}
}

The same code works if I generate SDIO 1 bit mode with CubeMX, but I get the above mentioned 'FR_NO_FILESYSTEM' error with 4 bit SDIO mode.

Please help me how can I solve this.

Thank you in advance!

28 REPLIES 28

Hello. I have the same situation as you. Some cards do not write to SDIO in 4-bit mode. Have you solved this problem?

ANauz.1
Senior

I know this is an old post, but I you need a solution... I have the same problem, SDIO interface working in 1bit mode, and not in 4bits mode.

I have tried to use FreeRTOS-Plus-FAT. It include a stm32f4xx_hal_sd.c file. It is not the same as the one provided in STM32F4 firmware package. With this file, the initialisation of the SDIO interface works in 4bits mode.

So this is not an HW problem. It is a firmware problem, known for years now and still not corrected by ST!

Edit: There is also a workaround possible. First configure the SDIO in 1B mode, init SDIO and then configure it to 4B mode. Here is the solution detail: https://community.st.com/t5/stm32cubemx-mcus/sdio-interface-not-working-in-4bits-with-stm32f4-firmware/td-p/591776

I spent many hours fixing this issue, you can find a working solution, SDIO 4bit mode with DMA at 12Mhz with detailed instructions on how to set up everything here GitHub repo 

I suggest you carefully read the readme in the repo, everything is explained there.

Embedded_Engineer
Associate II

@djhtml @Tesla DeLorean @JCorn.2 @DOsbo @JGerd.1 @tizdipietro @Piranha @ANauz.1 @Sramo.1 


I am also facing the same problem, and I have not received any specific answers for it yet. However, my system is working fine with 1-bit SDIO. I am using the Discovery board of STM32F407VGT6 (DMSTF4BB - link). Additionally, I have noticed one more thing: even when using a 1-bit SDIO configuration, we still need to connect all four data buses (D0, D1, D2, D3) along with SDIO_cmd and SDIO_CK. Also, include the SDIO detect (GPIO input) on PB15 in my case.
20231127_122039.jpgapart from this I am also facing issue as " 

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 is my sample 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);

}

please go through my code (atteched below) and let me know what to do? and how to integrate USB and SD card simultaneously?


my observation: I took two STM32F407VGT6 microcontrollers and connect them the to same SDIO
source it(setup) is not working when both boards are powered on but when USB and FATFS works fine seperatly the problem is in two pins (PC10 and PC11) D2 & D3 please give me solution for the following:

  1. How do I configure USB_OTG_FS as a USB Device in STM32CubeMX or other development tools?
  2. 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?
  3. Are there any sample code snippets or project examples that demonstrate this integration?
  4. What should I be mindful of when managing the data transfer between the USB and SD card to avoid conflicts or data corruption?

 

Can you please specify !!

Hello,

I'm not familiar with USB. Can't help here. About the SDCard 1B/4B problem, here is a solution in another thread:

https://community.st.com/t5/stm32cubemx-mcus/sdio-interface-not-working-in-4bits-with-stm32f4-firmware/td-p/591776

 

Hi 

 

You'll find a detail explanation, with sample code here 

If you find something from that repo not working in your case, please use Git to add an Issue or a Pull Request

 

Embedded_Engineer
Associate II

I am still facing issues in my project. I want to use USB functionality, but I am unable to do so due to a problem with SDIO pins PC10 (SDIO_D2) and PC11 (SDIO_D3). I am trying to connect two STM32F407VGT6 microcontrollers with the SDIO pins of the same discovery board (DM-STF4BB). We have uploaded USB (device-Mass storage device) code in one microcontroller, but that code only works when PC10 and PC11 pins are not shorted/connected. It doesn't matter if the second board is powered or not; connecting these two pins will always create a problem. And I want design a PCB in which only single controller will be powered at a time please guide me what to do, we can not use shottkey diode in PC 10&11 and VCC  because of voltage loss, will MOSFET work ?

Consider the two red lines as PC10 and PC11 (D2 and D3 respectively), green lines as other SDIO lines with ground, and blue lines as VCC. Two squares represent STM32F4, and the rectangle below represents the SDIO device with an SD card.

code is uploaded in first STM32 on the left side

USB and FATFS functionality will only work in the following configuration

Embedded_Engineer_5-1701496006921.png

 

where as the USB will not even detect by any of the following configurations:


drawio.png