cancel
Showing results for 
Search instead for 
Did you mean: 

SPI configuration problem on STM32F4 Discovery

cagibomber
Associate II
Posted on August 07, 2012 at 18:55

Hi everyone,

I am trying to send some bytes of data on a sd-card (8GB) using SPI.

I started with wiring the sd slot and configuring the spi but I have nothing, not even the clock...

I have followed what's explained in the file spi.h and seen other post about spi but I still don't see where I am wrong.

I'd really appreciate if you can help me with it ! Thank you.

#spi-sd-card-stm32-discovery
33 REPLIES 33
cagibomber
Associate II
Posted on August 09, 2012 at 19:19

Thanks,

I just check my wiring, it's correct.

Here is my main code :

int main(void){

SD_LowLevel_Init();

err = SD_Init(); // Returns OK

  

NVIC_Configuration(); /* Interrupt Config */

memset(&fs32, 0, sizeof(FATFS));

res = f_mount(0, &fs32); // Ret FR_OK

res = f_open(&fil, ''MESSAGE.TXT'', FA_CREATE_NEW); // Ret FR_DISK_ERR

  while(1){}

}

Since SD_Init returns OK, I guess the SD card initialization is alright. 

What do you mean by ''card reading code'' ?

Thanks again.

Posted on August 09, 2012 at 19:36

diskio.c disk_read() -> SD_ReadMultiBlocksFIXED() -> ... FOLLOW THE CODE

You'll need to instrument the code, or trace into it to understand WHY it fails in your situation.

Opening a new file for writing :

    res = f_open(&fil, ''LENGTH.TXT'', FA_CREATE_ALWAYS | FA_WRITE);

The attributes are probably important, so pay attention to them, and the examples provided on the FatFs website.

The code I've supplied tries to scan through an existing file MESSAGE.TXT, the exact reasoning and mechanics for that stem from the thread/conversation that went into forming the example. I do have the project in Keil, a lot better environment than GNU/GCC.

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

Thank you,

I've followed step by step to see where it goes wrong.

I've made my main very simple:

int main(void)

{

SD_LowLevel_Init();

err = SD_Init(); // Returns OK

NVIC_Configuration(); /* Interrupt Config */

memset(&fs32, 0, sizeof(FATFS));

res = f_mount(0, &fs32);

res = f_open(&fil, ''MESSAGE.TXT'', FA_CREATE_ALWAYS | FA_WRITE);

while(1){}

}

So the problem is:

If I make the main as simple, f_mount and f_open return FR_OK, but if there's no MESSAGE.txt file in the card, it still creates nothing, which it should...

Also, if I add:

if(res == FR_OK){

UINT Total = 0;

while(1){

BYTE Buffer[512];

UINT BytesRead;

res = f_read(&fil, Buffer, sizeof(Buffer), &BytesRead);

if(res != FR_OK){

break;

}

Total += BytesRead;

if(BytesRead < sizeof(Buffer)){

break;

}

}

res = f_close(&fil); // MESSAGE.TXT

}

If I fallow the code step by step, sometimes it will work (assuming I created  a MESSAGE.TXT file in the card). But sometimes it doesn't...

When it does not work, the first f_open (same as before) leads me to the 'BYTE check_fs ()' function in ff.c wich returns:

if (LD_WORD(&fs->win[BS_55AA]) != 0xAA55) /* Check record signature (always placed at offset 510 even if the sector size is >512) */

return 2;

And then gives me FR_NO_FILESYSTEM...

I don't understand what makes it not work when I add the rest of the main since it comes afterwards ! 

Do you have an idea of what causes the ''return 2'' ?

Thank you.

Posted on August 10, 2012 at 16:44

Getting garbled data back from the card is indicative of a connection issue, or a clock that is too fast, you could reduce the speed, ie SDIO_TRANSFER_CLK_DIV

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
cagibomber
Associate II
Posted on August 10, 2012 at 17:22

Ok, very useful ! I just forgot about the transfer speed. 

I have a class 6 card, so I just configured my clock at 6MHz which I verified on oscilloscope.

Is it normal that my clock is not square but more of a sinusoid ?

For my wiring,

I use this socket >> 

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

I plugged the 4 data lines(D0, IRQ, P9 and CS as D0, D1, D2 and D3 in SDIO), the CMD line (DI), VCC at 3.3v, COM and GND to the ground and CD on PC2 as Card Detection.

I don't think I need to plug WP so I left it unwired...

Thanks a lot !

Posted on August 10, 2012 at 17:51

Do you have the pull-up resistors?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
cagibomber
Associate II
Posted on August 10, 2012 at 18:33

Hardware you mean ? 

If so, no I don't.

Should I put some for the data lines ?

Posted on August 10, 2012 at 19:42

Yeah, the ones I told you about 6 or so posts ago on this thread.

And several other threads around the SDIO/SD Card topic within the last couple of weeks.

[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/STM32f4-Discovery SDIO problems&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=123]https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32Discovery%2FSTM32f4-Discovery%20SDIO%20problems&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F?tviews=123

Five pull-up on DATA and CMD lines, of the order 33K or 47K. Without them stuff fails.

0690X00000603EoQAI.jpg

See also README.TXT here on the MSC release

https://docs.google.com/open?id=0B7OY5pub_GfIaUozb1VsY3Flb1E

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
cagibomber
Associate II
Posted on August 10, 2012 at 20:48

Sorry about that, when you told me about the Pull Up resistor, I thought about the GPIO...

Thanks a lot, I'll try that tomorrow ! Your read me.txt is really helpful ! 

I thought the wiring couldn't be wrong since with my own code I manage to write in the Card and read it back with the stm32. I thought my problem was just FatFs !

Thanks again !!

cagibomber
Associate II
Posted on August 13, 2012 at 12:37

Good morning,

I've put the pull-up resistors and checked my wiring again, still nothing. I've read the post you linked

If I just try to open/create a file in the sd card, it returns FR_OK, if I try to write in it, it returns FR_DISK_ERR (which is logic since no file has been actually created if I check the sd card on my laptop). 

The clock frequency is 6MHz (for my class 6 SDC). The VDD is 3v...

I don't understand. Could it give FR_OK when creating file if it can't create it ?

Thank you.