SD Card over SPI

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
drgowen
Posts: 12
Joined: Tue Aug 27, 2013 3:28 pm

SD Card over SPI

Postby drgowen » Fri Aug 30, 2013 8:03 pm

Hi, could someone have a quick look over this code and tell me if I'm doing anything insanely wrong. It's a stm32f407 discovery board, with the sd card connected on PA15(NSS) and PB3,4,5 spi.

Code: Select all

------------structs
MMCDriver MMCD1;

/* Maximum speed SPI configuration (18MHz, CPHA=0, CPOL=0, MSb first).*/
static SPIConfig hs_spicfg = {NULL, GPIOA, 15, 0};

/* Low speed SPI configuration (281.250kHz, CPHA=0, CPOL=0, MSb first).*/
static SPIConfig ls_spicfg = {NULL, GPIOA, 15,
                              SPI_CR1_BR_2 | SPI_CR1_BR_1};

/* MMC/SD over SPI driver configuration.*/
static MMCConfig mmccfg = {&SPID1, &ls_spicfg, &hs_spicfg};

----in main-----------------------------------------------------------
  palSetPadMode(GPIOA, 4, PAL_MODE_UNCONNECTED);
    palSetPadMode(GPIOA, 5, PAL_MODE_UNCONNECTED);
    palSetPadMode(GPIOA, 6, PAL_MODE_UNCONNECTED);
    palSetPadMode(GPIOA, 7, PAL_MODE_UNCONNECTED);

  palSetPadMode(GPIOB, 3, PAL_MODE_ALTERNATE(5) | PAL_MODE_INPUT_PULLUP);
  palSetPadMode(GPIOB, 4, PAL_MODE_ALTERNATE(5) | PAL_MODE_INPUT_PULLUP);
  palSetPadMode(GPIOB, 5, PAL_MODE_ALTERNATE(5));
  palSetPadMode(GPIOA, 15, PAL_MODE_ALTERNATE(5) | PAL_MODE_INPUT_PULLUP);

  mmcObjectInit(&MMCD1);
  mmcStart(&MMCD1, &mmccfg);

   if (mmcConnect(&MMCD1)) {
      chprintf(chp, "SD CARD FAILED");
   } else {
      chprintf(chp, "SD CARD SUCCEEDED");
   }


I always get failed and can't work out why. I've tried external pullups, no pullups, etc, to no avail.
Best
Gareth

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: SD Card over SPI

Postby Giovanni » Fri Aug 30, 2013 8:45 pm

hi,

Probably because GPIOB3 is used for SWO and is not free but there could be a lot of other causes, long wires for example, also consider that internal pullups are very weak.

Giovanni

User avatar
Tectu
Posts: 1226
Joined: Thu May 10, 2012 9:50 am
Location: Switzerland
Contact:

Re: SD Card over SPI

Postby Tectu » Sat Aug 31, 2013 6:49 am

It's always a problem with the discovery kit because there are a lot of pin conflicts with the on-board hardware as Giovanni mentioned. Make sure that you check every pin in the schematics - sometimes people have to unsolder some stuff. However, in your case you should be able to just use a different pin.


~ Tectu

drgowen
Posts: 12
Joined: Tue Aug 27, 2013 3:28 pm

Re: SD Card over SPI

Postby drgowen » Sun Sep 01, 2013 10:44 am

Thanks for the tips chaps. I've remapped to PA4,5,6,7 but still having no luck. Tried putting 100k pull ups on the signal pins (what resistance should I be using and which pins need pull ups?) but still no luck .

Here's a photo of the setup - Giovanni mentioned long wires - could this be the problem ? My only alternative is to just order the prototype pcbs and hope they work.

out.jpg
out.jpg (173.29 KiB) Viewed 6790 times


Best
Gareth

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: SD Card over SPI

Postby Giovanni » Sun Sep 01, 2013 11:55 am

It already happened, try a search on the forum, long wires caused SD cards to not work.

Giovanni

colin
Posts: 149
Joined: Thu Dec 22, 2011 7:44 pm

Re: SD Card over SPI

Postby colin » Thu Sep 12, 2013 7:45 pm

If long wires are the problem, then you should be able to decrease the SPI clock frequency until it works, long wires are only a problem due to switching signals and with a low enough frequency it should work. That should at least help you diagnose the problem--whether it is just due to the long wires or something else.

pito
Posts: 199
Joined: Sun Nov 06, 2011 3:54 pm

Re: SD Card over SPI

Postby pito » Fri Sep 13, 2013 1:00 pm

Your cables seem to be quite long.. Long time back I did exhaustive experiments with SDIO here in the forum (STM32F4Disco), my wires were ~12cm long (jump wires). Also a decoupling cap (2x10u ceramics) soldered on the sdcard's socket (the same you have got).

As I can remember the biggest issue I had to mess with was with setting the proper board.h parameters (ie pin speeds, etc) for SDcard's SDIO. Otherwise it worked fine then. Also you may try the MP3 player demo, it uses an sdcard, but with SPI bus, I think. It works fine too :)

Here is the topic re SDIO, with some stuff re wire lengths:
viewtopic.php?f=3&t=519&hilit=pito

And the source code which worked:
viewtopic.php?f=3&t=519&hilit=pito&start=40#p4919
viewtopic.php?f=3&t=519&hilit=pito&start=60#p4970

And the MP3 demo:
viewtopic.php?f=4&t=351

rew
Posts: 380
Joined: Sat Jul 19, 2014 12:59 pm
Has thanked: 2 times
Been thanked: 13 times

Re: SD Card over SPI

Postby rew » Mon Feb 15, 2021 5:41 pm

For posterity....

Even with slow SPI speed, the long cables can be a problem. The SD cards are too small to house an on-card decoupling capacitor. So that should be just outside the card connector. So with these long wires, the SD card may suddenly draw too much power that the non-existant "close" capacitor would normally provide.... So the card browns out, hopefully resets and starts over.

SD cards, I understand often have their own internal ARM processor, so when that resets because of lack of power, you're a bit screwed...

So... Always add enough decoupling capacitors, as close as possible to the devices. Don't ask what reminded me today.


Return to “Development and Feedback”

Who is online

Users browsing this forum: No registered users and 16 guests