lb
Posts: 261
Joined: Sat Jan 28, 2012 8:07 pm

Secondary SD/MMC interface

Wed Jun 20, 2012 12:47 am

I was bored, so I built a a simple secondary SPI-based SD/MMC interface. This was quite straight-forward, and quickly done with the help of the spi-gpio and mmc-spi drivers of the Linux kernel. The advanced bit-banging™ technology allows for speeds up to 180 KB/s.

Image

The old floppy drive edge connector makes for a nice and simple SD socket - the card fits tightly into it. I used the SPI pins on the header for the SPI interface, despite the fact that the actual SPI hardware support isn't used. The spi-gpio and mmc-spi drivers need to be configured in the kernel configuration. I used the 3.1.9 kernel, and here's a patch with my changes.

This is what the Linux kernel reports:
[ 5.818182] mmc_spi spi0.0: ASSUMING 3.2-3.4 V slot power
[ 5.827827] mmc_spi spi0.0: SD/MMC host mmc1, no DMA, no WP, no poweroff
[ 6.152809] mmc1: host does not support reading read-only switch. assuming write-enable.
[ 6.182565] mmc1: new SD card on SPI
[ 6.205942] mmcblk1: mmc1:0000 SD01G 972 MiB
Here's a simple speed test with hdparm -t:
/dev/mmcblk1:
Timing buffered disk reads: 2 MB in 11.50 seconds = 178.07 kB/sec
The hardware SPI support should make this a lot faster, maybe I'll try again with the 3.2 kernel.

lb
Posts: 261
Joined: Sat Jan 28, 2012 8:07 pm

Re: Secondary SD/MMC interface

Thu Jun 21, 2012 12:42 am

With the proper SPI driver in the 3.2.20 kernel I get a bit over 1 MB/s, without the extreme CPU load of spi-gpio. This might be acceptable for some use cases (e.g. an audio player).

User avatar
jbeale
Posts: 3491
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Secondary SD/MMC interface

Thu Jun 21, 2012 1:16 am

neat project, thanks for this post! 1 MB/sec is perfectly reasonable for a number of low to medium-speed data logging applications. I have one where an Arduino is just slightly too slow writing to a SD card direct via SPI, but this would be way more than enough.

martinayotte
Posts: 3
Joined: Fri Dec 07, 2012 9:28 pm

Re: Secondary SD/MMC interface

Fri Dec 07, 2012 9:32 pm

Hi Ib,

What is the current status of your patches for getting a spi-mmc drivers ?
(btw, we can't find your patches in the post above, can you provide URL ?)

Is it now part of the current kernel ? If not, can you describe quickly what need to be done on top of std kernel sources ?

Thanks,

lb
Posts: 261
Joined: Sat Jan 28, 2012 8:07 pm

Re: Secondary SD/MMC interface

Wed Dec 12, 2012 12:51 pm

The drivers are all part of the kernel. You just need to patch the hardware configuration in arch/arm/mach-bcm2708/bcm2708.c. Sorry, I can't find the patch now, but it's quite straight-forward.

martinayotte
Posts: 3
Joined: Fri Dec 07, 2012 9:28 pm

Re: Secondary SD/MMC interface

Wed Dec 12, 2012 5:57 pm

Hi again Ib,

Looking at arch/arm/mach-bcm2708/bcm2708.c, for me, it doesn't seems obvious ...
Is it simply to add kernel configs for CONFIG_MMC_BCM2708 and/or CONFIG_MMC_SDHCI_BCM2708 ?

Thanks in advance,

User avatar
STrRedWolf
Posts: 24
Joined: Fri Oct 05, 2012 1:47 am

Re: Secondary SD/MMC interface

Mon Dec 17, 2012 3:57 am

Hey lb. I got the kernel recompiled here myself, but how are you plugging in the SD card via SPI? I have two wired up via Sparkfun MicroSD adapters, MISO to DO, MOSI to DI.

Trying to modprobe spi_mmc gets me nowhere -- the module pops in (even after unloading spidev) but doesn't show the two SD cards (one per chip select).

cetverooki
Posts: 22
Joined: Tue Mar 11, 2014 9:42 am

Re: Secondary SD/MMC interface

Fri Jul 04, 2014 8:21 am

@ lb

I need Your help with this modification. Please contact me on pm.

Raspi_developer
Posts: 8
Joined: Sun Aug 10, 2014 5:20 am

Re: Secondary SD/MMC interface

Sun Aug 10, 2014 3:00 pm

Hello all,
can anyone plz guide me how can i read write to external SD card using this updated Kernal?

papalukas
Posts: 8
Joined: Fri Jan 17, 2014 9:22 pm

Re: Secondary SD/MMC interface

Sat Dec 13, 2014 8:30 pm

Hi guys,

Has anyone manage to get this patch working? My experince is negative similar to yours. All the time the mmc1 device is created but most of the time /dev/mmcblk1 is not. Sometimes mmcblk1 is there and I can even read the data, but initialization and each access attempt causes tens to hundreds of access errors (code -110). I've tried various frequencies: 100k, 400k, 500k, 1M, 2.5M and two cards: 2G SDSC and 4G SDHC

Thanks,
Andrey

cetverooki
Posts: 22
Joined: Tue Mar 11, 2014 9:42 am

Re: Secondary SD/MMC interface

Sun Dec 14, 2014 12:05 pm

i have tried to compile kernel but failed. There is somewhere else on forum my post with links to changes. I'm not a good programmer so my changes could be wrong.

Finally due to lack of time I have abandoned this. This is still something that I would like to have

papalukas
Posts: 8
Joined: Fri Jan 17, 2014 9:22 pm

Re: Secondary SD/MMC interface

Sun Dec 14, 2014 1:29 pm

Actually I've started with your patch and it has compiled perfectly fine for me. I've applied it into the latest kernel BTW, you seem to be using something different. So you might want to retry. If you want I can send the patch to you or please let me know what problem do you have. Maybe I can help.

Indeed this does not seem to work for me, maybe the is problem with my cables or card slot (I'm using LC studio one). At the same time sometimes I get the data that means that at least I've wired the thing correctly

papalukas
Posts: 8
Joined: Fri Jan 17, 2014 9:22 pm

Re: Secondary SD/MMC interface

Mon Dec 15, 2014 8:40 am

mmmm, interesting. from my totally inexperienced view the card initialization sequence looks, say questionable:

Code: Select all

[size=85][    8.017874] mmc_spi spi0.0: mmc_spi:  clock to 400000 Hz, 0
[    8.036247] mmc_spi spi0.0:   mmc_spi: CMD52, resp R2/R5
[    8.036392] mmc_spi spi0.0: SD/MMC host mmc1, no WP, no poweroff
[    8.046138] mmc_spi spi0.0:   ... CMD52 response SPI_R2/R5: value -110
[    8.046222] mmc_spi spi0.0:   mmc_spi: CMD52, resp R2/R5
[    8.048382] mmc_spi spi0.0:   ... CMD52 response SPI_R2/R5: value -110
[    8.048430] mmc_spi spi0.0:   mmc_spi: CMD0, resp R1
[    8.049285] mmc_spi spi0.0:   ... CMD0 response SPI_R1: resp 0001 00000000
[    8.049350] mmc_spi spi0.0:   ... CMD0 response SPI_R1: value 0
[    8.050390] mmc_spi spi0.0:   mmc_spi: CMD8, resp R3/R4/R7
[    8.051414] mmc_spi spi0.0:   ... CMD8 response SPI_R3/R4/R: resp 0001 000001aa
[    8.051480] mmc_spi spi0.0:   ... CMD8 response SPI_R3/R4/R: value 0
[    8.051519] mmc_spi spi0.0:   mmc_spi: CMD5, resp R3/R4/R7
[    8.053872] mmc_spi spi0.0:   ... CMD5 response SPI_R3/R4/R: value -110
[    8.053912] mmc_spi spi0.0:   mmc_spi: CMD5, resp R3/R4/R7
[    8.054914] mmc_spi spi0.0:   ... CMD5 response SPI_R3/R4/R: resp 0005 ffffffff
[    8.054977] mmc_spi spi0.0:   ... CMD5 response SPI_R3/R4/R: value -38
[    8.055018] mmc_spi spi0.0:   mmc_spi: CMD55, resp R1
[    8.055892] mmc_spi spi0.0:   ... CMD55 response SPI_R1: resp 0001 00000000
[    8.056059] mmc_spi spi0.0:   ... CMD55 response SPI_R1: value 0
[    8.056130] mmc_spi spi0.0:   mmc_spi: CMD41, resp R1
[    8.058438] mmc_spi spi0.0:   ... CMD41 response SPI_R1: value -110
[    8.058517] mmc_spi spi0.0:   mmc_spi: CMD55, resp R1
[    8.060759] mmc_spi spi0.0:   ... CMD55 response SPI_R1: value -110
[    8.060807] mmc_spi spi0.0:   mmc_spi: CMD55, resp R1
[    8.061662] mmc_spi spi0.0:   ... CMD55 response SPI_R1: resp 0001 00000000
[    8.061725] mmc_spi spi0.0:   ... CMD55 response SPI_R1: value 0
[    8.061761] mmc_spi spi0.0:   mmc_spi: CMD41, resp R1
[    8.063986] mmc_spi spi0.0:   ... CMD41 response SPI_R1: value -110
[    8.064032] mmc_spi spi0.0:   mmc_spi: CMD55, resp R1
[    8.064897] mmc_spi spi0.0:   ... CMD55 response SPI_R1: resp 0001 00000000
[    8.064928] mmc_spi spi0.0:   ... CMD55 response SPI_R1: value 0
[    8.064995] mmc_spi spi0.0:   mmc_spi: CMD41, resp R1
[    8.067356] mmc_spi spi0.0:   ... CMD41 response SPI_R1: value -110
[    8.071765] mmc_spi spi0.0:   mmc_spi: CMD1, resp R1
[    8.076162] mmc_spi spi0.0:   ... CMD1 response SPI_R1: resp 0001 00000000
[    8.076229] mmc_spi spi0.0:   ... CMD1 response SPI_R1: value 0
[    8.076273] mmc_spi spi0.0:   mmc_spi: CMD58, resp R3/R4/R7
[    8.077307] mmc_spi spi0.0:   ... CMD58 response SPI_R3/R4/: resp 0001 80ff8000
[    8.077370] mmc_spi spi0.0:   ... CMD58 response SPI_R3/R4/: value 0
[    8.077410] mmc_spi spi0.0:   mmc_spi: CMD0, resp R1
[    8.079562] mmc_spi spi0.0:   ... CMD0 response SPI_R1: value -110
[    8.080637] mmc_spi spi0.0:   mmc_spi: CMD1, resp R1
[    8.082882] mmc_spi spi0.0:   ... CMD1 response SPI_R1: value -110
[    8.082919] mmc1: error -110 whilst initialising MMC card
[/size]
we have here a few commands which do not belong to SD cards, e.g. CMD5. It is for SDIO, but maybe it's Ok, as it does not look like we've specified anywhere that we're working with memory card. What's worse is the reponse and the sequence starting CMD1. As far as I've googled the driver shall wait for the Idle state to be clear after CMD1. That is "resp" shall become 0000. But apparently this is not happening and the rest of the sequence is screwed. I'll try to debug the driver a bit deeper

cetverooki
Posts: 22
Joined: Tue Mar 11, 2014 9:42 am

Re: Secondary SD/MMC interface

Mon Dec 15, 2014 10:19 pm

@papalukas if you can make this work please do so. I have very little time for this due to changes in family. If you can get this to work it would be great since i need to make copy of certain sd card that has failed communication using regular sdio protocol.

I have used source code and info from OP. I could make something wrong but I think that main problem is kernel version. OP was using older one then mine or yours. That would account for differences in initialization and my compile errors

papalukas
Posts: 8
Joined: Fri Jan 17, 2014 9:22 pm

Re: Secondary SD/MMC interface

Tue Dec 16, 2014 9:22 pm

@cetverooki
I've looked a bit more on the driver code and the docs and I cannot find what's wrong. It looks like my card is randomly failing to send the response. Maybe there is indeed a problem with wiring or defective LC Studio module (people say it happens). In the meanwhile, here's http://pastebin.com/gFcq4xWd the (full of commented out trash) patch which enables mmc_spi driver on latest 3.12.y RPi kernel (Suddenly found out that I don't know why I'm using 3.12.y, it just happened). Would be grateful if you could check whether this patch works for you, so at least I know if it's hardware or software problems. Please note that this patch disables the SPI chip select 0, so using this kernel you will not be able to use it from your apps. Instead it hard-winds it into mmc driver. So that you shall attach your SD's CS line to CS 0 (GPIO 8)

Andrey

cetverooki
Posts: 22
Joined: Tue Mar 11, 2014 9:42 am

Re: Secondary SD/MMC interface

Tue Dec 16, 2014 10:06 pm

@papalukas this will have to wait until January. Tomorrow I'm flying out of country and will come back in 20 days. When I get back I will try this.

Is there something about timing order for sdcards? what You had could be a timing problem.

papalukas
Posts: 8
Joined: Fri Jan 17, 2014 9:22 pm

Re: Secondary SD/MMC interface

Wed Dec 17, 2014 7:39 am

@cetverooki it does not look like. That moment card is working at 400 kHz. And the response shall be immediate by the spec. It can still carry the "idle" flag, but it should be there. Also the read code reads 16 more bytes than necessary (for other reasons) and none of these bytes has even a first byte of response. The only possible timing problem could be is with setting CS signal, which is important. I don't have oscilloscope so cannot verify it comes in time or not

papalukas
Posts: 8
Joined: Fri Jan 17, 2014 9:22 pm

Re: Secondary SD/MMC interface

Tue Dec 23, 2014 7:45 am

I'm out of options, I've:
- shorten CS to the ground permanently
- removed the pull-up resistors on the LCStudio board. Apparently MOSI in RPi has internal pull-down so having pull-up ended in having ~2V in inactive (no SPI command inprogress) state, I thought it might affect SD card somehow
- verified with oscilloscope that the all the voltages are correct, no shorts or leaks etc. Unfortunately oscilloscope has no memory so I cannot check the actual data on physical level
None of the above options have changed anything, I still get random (with about 50% chance probability) absences of responses from SD card. So it's not CS timing. Other than that if it were timing that means that SPI support is totally screwed in BCM chip or RPi kernel. Which I really doubt takes place

papalukas
Posts: 8
Joined: Fri Jan 17, 2014 9:22 pm

Re: Secondary SD/MMC interface

Mon Jan 26, 2015 6:54 am

Just in case if anybody curious - using logic analyzer I've verified that SD card receives all commands and gives a correct response. However for some reason the RPi does not receive roughly half of the responses, chosen in random way

msperl
Posts: 344
Joined: Thu Sep 20, 2012 3:40 pm

Re: Secondary SD/MMC interface

Sun Mar 22, 2015 10:19 pm

Did someone find a solution to this problem?

I remember that mmc_spi has been working 2 years ago, but with the last 3.18 with device-tree it does not work any longer. Driver works and loads, spi commands get sent, but it does not fully work - spi communication seems to work, but it stops at some point.

My relevant portion of the device-tree looks like this:

Code: Select all

&spi {
        sd1 {
                reg = <1>;
                status = "okay";
                compatible = "spi,mmc_spi";
                voltage-ranges = <3000 3500>;
                spi-max-frequency = <8000000>;
        };
};

msperl
Posts: 344
Joined: Thu Sep 20, 2012 3:40 pm

Re: Secondary SD/MMC interface

Sun Mar 22, 2015 10:36 pm

What I get in dmesg is:

Code: Select all

[  667.745791] mmc_spi spi0.1: SD/MMC host mmc1, no WP, no poweroff, cd polling
[  667.770415] mmc1: error -110 whilst initialising SD card
[  670.961894] mmc1: error -110 whilst initialising SD card

msperl
Posts: 344
Joined: Thu Sep 20, 2012 3:40 pm

Re: Secondary SD/MMC interface

Mon Mar 23, 2015 7:26 am

OK - i think I found my issue:
You need to add a pullup resistor for CS!

Note: If you have got 2 devices on a brakeout-board but you only connect one SPI device via CS you HAVE to HAVE pullup on both CS! This happened to me with a Adafruit 1.8"TFT+SD card adapter.

Martin

RBISWASX
Posts: 31
Joined: Thu Jan 09, 2014 5:30 pm

Re: Secondary SD/MMC interface

Sat Jul 04, 2015 5:40 am

Yes, here is Blog that i have written, about how to interface an External MICROSD Card Adapter with RaspberryPi. I have considered Elm Chan's generic FATFS Library for FATFS Api's and DISKFs API's, and using plain GPIOs to simulate the SPI signals of MISO, MOSI, CS and SCLK. Here is my blog below, in which i have covered it step by step...

http://blogsmayan.blogspot.in/p/interfa ... -card.html

gnumengor
Posts: 6
Joined: Mon Aug 06, 2012 6:38 pm

Re: Secondary SD/MMC interface

Sun Nov 01, 2015 10:03 am

Hi guys,

I successfully compiled the driver and it is loaded at startup. However, it gives error msg and I don't know why!!!

It's driving me crazy... I checked the connections to the SPI interface and those are correct...

Maybe you can give me a hint?

Code: Select all

[    5.790111] bcm2708_spi bcm2708_spi.0: master is unqueued, this is deprecated
[    5.916418] bcm2708_spi bcm2708_spi.0: SPI Controller at 0x20204000 (irq 80)
[    8.244537] random: nonblocking pool is initialized
[    8.267070] mmc_spi spi0.0: ASSUMING 3.2-3.4 V slot power
[   11.323591] mmc_spi spi0.0: SD/MMC host mmc1, no WP, no poweroff
[   11.337933] mmc_spi spi0.0: no support for card's volts
[   11.349840] mmc1: error -22 whilst initialising SDIO card
[   11.368322] mmc_spi spi0.0: no support for card's volts
[   11.383685] mmc1: error -22 whilst initialising SD card
[   11.400002] mmc_spi spi0.0: no support for card's volts
[   11.413698] mmc1: error -22 whilst initialising MMC card
Thanks :)

Return to “Interfacing (DSI, CSI, I2C, etc.)”