cancel
Showing results for 
Search instead for 
Did you mean: 

ST32f4discovery+sdcard+fatfs problem

arnold
Associate II
Posted on June 30, 2012 at 23:43

Hello, 

i have a small problem i think.

With the help of this forum and google i created a project with CoIDE.

What it should do is reading the directory of an SDCARD.

The SDCARD part is working. but the fatfs is not working and i don't know what i'm doing wrong. 

I believe everything is oke. 

so i hope someone could help me. 

I attached the whole project. 

#sdcard-stm32f4-sdio-fatfs
48 REPLIES 48
arnold
Associate II
Posted on July 02, 2012 at 20:34

i'm going through the sd functions step by step. 

and when sending a cmd i get SD_CMD_RSP_TIMEOUT

i think this could be the problem.

what does this mean, is there a timing issue, if so how can i correct this. 

i connected the SDCard direct to the controller.

Posted on July 02, 2012 at 20:39

You need pull up resistors on the pins, review a schematic for the STM3240G, or other, eval boards.

A more considered diskio solution would be along the following lines.

..
/*-----------------------------------------------------------------------*/
/* Return Disk Status */
DSTATUS disk_status (
BYTE drv /* Physical drive nmuber (0..) */
)
{
DSTATUS stat = 0;
if (SD_Detect() != SD_PRESENT)
stat |= STA_NODISK;
// STA_NOTINIT - Subsystem not initailized
// STA_PROTECTED - Write protected, MMC/SD switch if available
return(stat);
}
/*-----------------------------------------------------------------------*/
/* Read Sector(s) */
DRESULT disk_read (
BYTE drv, /* Physical drive nmuber (0..) */
BYTE *buff, /* Data buffer to store read data */
DWORD sector, /* Sector address (LBA) */
BYTE count /* Number of sectors to read (1..255) */
)
{
SD_Error Status;
printf(''disk_read %d %p %10d %d
'',drv,buff,sector,count);
if (SD_Detect() != SD_PRESENT)
return(RES_NOTRDY);
if ((DWORD)buff & 3) // DMA Alignment failure, do single up to aligned buffer
{
DRESULT res = RES_OK;
DWORD scratch[BLOCK_SIZE / 4]; // Alignment assured, you'll need a sufficiently big stack
while(count--)
{
res = disk_read(drv, (void *)scratch, sector++, 1);
if (res != RES_OK)
break;
memcpy(buff, scratch, BLOCK_SIZE);
buff += BLOCK_SIZE;
}
return(res);
}
Status = SD_ReadMultiBlocksFIXED(buff, sector, BLOCK_SIZE, count); // 4GB Compliant
if (Status == SD_OK)
{
SDTransferState State;
Status = SD_WaitReadOperation(); // Check if the Transfer is finished
while((State = SD_GetStatus()) == SD_TRANSFER_BUSY); // BUSY, OK (DONE), ERROR (FAIL)
if (State == SD_TRANSFER_ERROR)
return(RES_ERROR);
else
return(RES_OK);
}
else
return(RES_ERROR);
}
..

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

0690X00000602lDQAQ.jpg
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
arnold
Associate II
Posted on July 02, 2012 at 21:01

i get this error when applying sd_selectdeselect so it has nothing to do with disk stuff.

i'm trying to get som resitors tommorow to put these pullups to the board.

Posted on July 02, 2012 at 21:17

disk_initialize() (by inference SD_Init) must succeed for any disk_read()'s to occur.

The SDCard and FsFat libraries work fine.

puts(''FsFAT Testing'');
memset(&fs32, 0, sizeof(FATFS));
res = f_mount(0, &fs32);
if (res != FR_OK)
printf(''res = %d f_mount
'', res);
res = f_open(&fil, ''MESSAGE.TXT'', FA_READ);
if (res != FR_OK)
printf(''res = %d f_open MESSAGE.TXT
'', res);
path = '''';
res = f_opendir(&dir, path);
if (res != FR_OK)
printf(''res = %d f_opendir
'', res);
if (res == FR_OK)
{
while(1)
{
char *fn;
res = f_readdir(&dir, &fno);
if (res != FR_OK)
printf(''res = %d f_readdir
'', res);
if ((res != FR_OK) || (fno.fname[0] == 0))
break;
#if _USE_LFN
fn = *fno.lfname ? fno.lfname : fno.fname;
#else
fn = fno.fname;
#endif
printf(''%c%c%c%c '',
((fno.fattrib & AM_DIR) ? 'D' : '-'),
((fno.fattrib & AM_RDO) ? 'R' : '-'),
((fno.fattrib & AM_SYS) ? 'S' : '-'),
((fno.fattrib & AM_HID) ? 'H' : '-') );
printf(''%10d '', fno.fsize);
printf(''%s/%s
'', path, fn);
}
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
arnold
Associate II
Posted on July 04, 2012 at 13:43

When searching the internet and looking at my sd card i count 9 connections.

should i use the connections as in the schematic below. Can i say that when i can read the size of the sd card the connections are right. i only need to put the pull up resistors.

Which i'm going to do tonight, after these pullups it should work.

0690X00000602dLQAQ.jpg
Posted on July 04, 2012 at 14:32

The rest of us are using MicroSD form factor cards.

The pin numbering on your picture is wrong. See

http://en.wikipedia.org/wiki/Secure_Digital

You will need to pay attention to the pin configuration of your specific socket.

SD SOCKET Naming and Connection

COM - COMMON - GND

WP - WRITE PROTECT - GPIO Optional

CD - CARD DETECT - GPIO - PC2 or whatever, external Pull-Up Optional use internal

P9 - DAT2 - PC10 47K PU

IRQ - DAT1 - PC9 47K PU

D0 - DAT0 - PC8 47K PU

GND

CLK - CLK - PC12

VCC - 3V

DI - CMD - PD2 47K PU

CS - DAT3 - PC11 47K PU

Generally breakout boards are easier to wire, and you can tack a resistor pack on the top side. Many have clear pin markings and function names.

http://www.sparkfun.com/products/204

http://www.ebay.com/itm/MicroSD-Micro-SD-MSD-Card-Breakout-Board-/120663990037

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
arnold
Associate II
Posted on July 04, 2012 at 15:57

Thanks,

indeed the numbering is wrong in my example.

But can you say when after a sd_init you read the Status info and get the correct card size all connections are right.

Posted on July 04, 2012 at 16:38

But can you say when after a sd_init you read the Status info and get the correct card size all connections are right.

You'd have to check to see where it switches from 1-bit 400 KHz mode to 4-bit 24 MHz mode, there is plenty of opportunity to fail.

I've pulled the FatFs code over, fixed the interface code, and have it working with FAT32 MicroSD cards.

If it's not getting to the disk_read() routines it's because other stuff is failing. Step through the code, identify where it fails, for the most part it's checking things very early on and bailing out quickly. If you are pulling data into local buffers make sure you have a sufficiently large stack, and make sure the SDIOCLK is in fact 48 MHz.

Confirm that you can read, and dump out, the partition table and boot sectors.

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

Now with the resistors in place, i'm unable to read the sdcard data.

I'm going to try it later again with a different sd card. 

I tripple checked my connection, but can't find what i did wrong.