User avatar
Arjan
Posts: 215
Joined: Sat Sep 08, 2012 1:59 pm

External SDCARD with FatFs module

Sun Feb 24, 2013 4:59 pm

The attached code (Eclipse project) is tested with SD card and micro SD card. Both 2GB.
It uses only 4 bit of GPIO port. No interrupt, no SPI port is used.
sd-card_rpi_SPI.JPG
sd-card_rpi_SPI.JPG (47.68 KiB) Viewed 6127 times
The attached code is based on FatFs - Generic FAT File System Module (http://elm-chan.org/fsw/ff/00index_e.html).
And the sample MMC bitbanging code is taken from the zip file : http://elm-chan.org/fsw/ff/ffsample.zip
The changes to the generic sample code (mmcbb.c) are shown below.

Code: Select all

#define  INIT_PORT()	init_port()			/* Initialize MMC control port (CS=H, CLK=L, DI=H, DO=in) */
#define 	DLY_US(n)	bcm2835_delayMicroseconds(n)		/* Delay n microseconds */

#define  CS_H()  bcm2835_gpio_set(RPI_GPIO_P1_26)	/* Set MMC CS "high" */
#define  CS_L()  bcm2835_gpio_clr(RPI_GPIO_P1_26)	/* Set MMC CS "low" */
#define  CK_H()  bcm2835_gpio_set(RPI_GPIO_P1_23)	/* Set MMC SCLK "high" */
#define  CK_L()  bcm2835_gpio_clr(RPI_GPIO_P1_23)	/* Set MMC SCLK "low" */
#define  DI_H()  bcm2835_gpio_set(RPI_GPIO_P1_19)	/* Set MMC DI "high" */
#define  DI_L()  bcm2835_gpio_clr(RPI_GPIO_P1_19)	/* Set MMC DI "low" */
#define  DO      bcm2835_gpio_lev(RPI_GPIO_P1_21)	/* Test for MMC DO ('H':true, 'L':false) */

Code: Select all

void init_port(void) {
	bcm2835_init();

	bcm2835_gpio_fsel(RPI_GPIO_P1_23, BCM2835_GPIO_FSEL_OUTP); // CLK
	bcm2835_gpio_clr(RPI_GPIO_P1_23);
	bcm2835_gpio_fsel(RPI_GPIO_P1_19, BCM2835_GPIO_FSEL_OUTP); // MOSI
	bcm2835_gpio_set(RPI_GPIO_P1_19);
	bcm2835_gpio_fsel(RPI_GPIO_P1_26, BCM2835_GPIO_FSEL_OUTP); // CE1
	bcm2835_gpio_set(RPI_GPIO_P1_26);
	bcm2835_gpio_fsel(RPI_GPIO_P1_21, BCM2835_GPIO_FSEL_INPT); // MISO
}
The bcm2835_* functions are from Mike's library : http://www.open.com.au/mikem/bcm2835/index.html

The data transfer rate will be several times slower than hardware SPI. I've just received a logic analyzer and I hope to build code for the SPI hardware soon.

For reference the SDCARD pinout :
sd-card-pinout.png
sd-card-pinout.png (58.5 KiB) Viewed 6127 times
Attachments
sdcard.zip
(48.75 KiB) Downloaded 501 times
Last edited by Arjan on Tue Apr 30, 2013 5:36 pm, edited 1 time in total.
http://www.raspberrypi-dmx.org/
Open Source DMX/RDM/MIDI/OSC/Art-Net/sACN solutions

mharizanov
Posts: 19
Joined: Thu May 31, 2012 12:57 pm

Re: External SDCARD with FatFs module

Sun Apr 28, 2013 4:57 pm

Hi,
that looks like interesting project, have you had further progress?

User avatar
Arjan
Posts: 215
Joined: Sat Sep 08, 2012 1:59 pm

Re: External SDCARD with FatFs module

Tue Apr 30, 2013 3:25 pm

Hi,

Yes, I have finished my new break-out board. See http://www.raspberrypi.org/phpBB3/viewt ... 92#p333992.

Furthermore, I am working on a bare-metal library for accessing the external SD card.
As I have already ported Mike's C library to bare-metal, it looks promising.

Regards, Arjan
Attachments
sch-v4.png
sch-v4.png (29.81 KiB) Viewed 5921 times
IMG_0237-1.jpg
IMG_0237-1.jpg (59.93 KiB) Viewed 5921 times
http://www.raspberrypi-dmx.org/
Open Source DMX/RDM/MIDI/OSC/Art-Net/sACN solutions

User avatar
Arjan
Posts: 215
Joined: Sat Sep 08, 2012 1:59 pm

Re: External SDCARD with FatFs module

Fri May 03, 2013 8:55 am

The source code {Linux} is now available from https://github.com/vanvught/OpenILDA/tree/master/sdcard

The source code {baremetal} is now available from https://github.com/vanvught/OpenILDA/tr ... card-mmcbb
Last edited by Arjan on Sun May 26, 2013 6:25 pm, edited 1 time in total.
http://www.raspberrypi-dmx.org/
Open Source DMX/RDM/MIDI/OSC/Art-Net/sACN solutions

mharizanov
Posts: 19
Joined: Thu May 31, 2012 12:57 pm

Re: External SDCARD with FatFs module

Fri May 03, 2013 9:45 am

Thanks,
will take a peek now

peterburk
Posts: 45
Joined: Tue Jan 29, 2013 12:16 am

Re: External SDCARD with FatFs module

Thu May 09, 2013 9:57 am

I'm very impressed! I'm working on making version 2 of my PiPod (http://peterburk.dyndns.org/pipod) after the first one was stolen, and I'm hoping to make a built-in SD card reader this time. The 2 times I've needed a mobile USB host have been to read photos from an SD card, using a USB SD card reader.

I'm concerned about transfer speed, though. If the GPIO operates at 1 Mb/s instead of 480 Mb/s USB 2, I think it could take a rather long time for 20 photos of 5 MB each to transfer.

Can you confirm or deny my worries? How long does your system take to transfer 100 MB?

Thanks!
Peter

vchristo
Posts: 7
Joined: Wed Aug 28, 2013 12:23 am

Re: External SDCARD with FatFs module

Thu Oct 17, 2013 9:02 pm

Hi friend.
First time thank you for this simple and functional solution.
I have tested and it works very fine, when it works alone (nothing more device on SPI bus)
but I found a trouble when I connect the ADC (mcp3004) on SPI bus as CS0.
if I execute the readout or write to sd card before initialize the ADC, I can read or write to card,
but the access to ADC gives wrong date acquisition.
If I read ADC first, I can not write to card.
I have forced CS1 and/or CS0 to high after finished access to sd card or ADC.
Have you any idea, can you help me?
thanks
Vitor

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

Re: External SDCARD with FatFs module

Fri Oct 18, 2013 8:21 am

One question: why don't you use the standard linux mmc_spi.c driver (over spi) which exposes the device as a normal block-device? You then can use the file system of your choice to format it... I have used this before and it worked quite will - if I remember correctly I was able to get up to 1MB/s (probably read) using a 20mhz clock rate on the spi bus.

If you fancy to use the non-spi ports, then you might try to get the linux spi-bitbang.c driver to work to create a second spi bus with the pins of your choice.

As for troubles with 2 spi devices on the same spi bus: maybe you are using the spi driver for a device that is active on CS-high -not CS-low- maybe because you use an optocoupler or similar.

If so, then I have to tell you that I believe there is a bug in the spi-bcm2708.c driver that would change back to default-polarity whenever a second device is used (hence effectively sending to both devices).

I have just found this yesterday during a code-review (but without means to verify it, as I am missing such a device)... No patch available yet...

Martin

chiruru
Posts: 17
Joined: Sun Apr 20, 2014 8:56 am

Re: External SDCARD with FatFs module

Wed Apr 23, 2014 6:52 am

Did you further more improvement?
I want to test about Second SD card for Raspberry Pi.

Please notify your situation for Second SD card
and How to use your Second SD card.
eg, How to connect HW, How to install SW, How to Mount on Linux...etc.

Thanks,,,,

miguel_trujano
Posts: 1
Joined: Sun May 10, 2015 9:13 pm

Re: External SDCARD with FatFs module

Sun May 10, 2015 10:38 pm

Hey im trying to read a second SDcard from the SPI pins of the raspberry pi B, so far i have try the first code from the ATTHACHMENTS, i havent been able to read anything from the SDcard, can somebody lead me in the right direcction, im kinda lost. i intend to read the raw SDcard without the directory i think it will be easier.
Thanks guys.

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

Re: External SDCARD with FatFs module

Thu May 14, 2015 10:49 am

Note that there is an issue introduced somewhere in 3.13 that produces hickups detecting the sd card when loading mmc-spi.

It was a simple patch similar to this:

Code: Select all

diff --git a/drivers/mmc/host/mmc_spi.c b/drivers/mmc/host/mmc_spi.c
index ae19d83..e21b8be 100644
--- a/drivers/mmc/host/mmc_spi.c
+++ b/drivers/mmc/host/mmc_spi.c
@@ -1386,8 +1386,7 @@ static int mmc_spi_probe(struct spi_device *spi)
        host->data = kmalloc(sizeof(*host->data), GFP_KERNEL);
        if (!host->data)
                goto fail_nobuf1;
-
-       if (spi->master->dev.parent->dma_mask) {
+       if (0) {
                struct device   *dev = spi->master->dev.parent;

                host->dma_dev = dev;
That made mmc_spi detect sd cards - with a corresponding device-tree...
dmesg shows:

Code: Select all

[ 8687.130221] mmc1: host does not support reading read-only switch, assuming write-enable
[ 8687.130276] mmc1: new SD card on SPI
[ 8687.141819] mmcblk1: mmc1:0000 SD2GB 1.86 GiB
[ 8687.154384]  mmcblk1: p1 p2
Then you can mount the mmc directly in the kernel:

Code: Select all

mount /dev/mmcblk1p1 /mnt

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

Re: External SDCARD with FatFs module

Thu May 14, 2015 10:55 am

the corresponding device tree looks like this:

Code: Select all

                sd1: sd@1 {
                                reg = <1>;
                                status = "okay";
                                compatible = "spi,mmc_spi";
                                voltage-ranges = <3200 3500>;
                                spi-max-frequency = <8000000>;
                        };
so all you need to do is write an overlay file...

Note that there is ONE catch that I have experienced:
make sure that mmc_spi is blacklisted in /etc/modprobe.d/raspi-blacklist.conf
otherwise the second sd-card may become the "main" boot-device resulting in headaches.
In case this happens: disconnect the sd card and you should be fine and you can boot the normal way...

oneinsect
Posts: 6
Joined: Sun Aug 02, 2015 7:52 pm

Re: External SDCARD with FatFs module

Sun Dec 20, 2015 7:33 pm

msperl wrote:Note that there is an issue introduced somewhere in 3.13 that produces hickups detecting the sd card when loading mmc-spi.

It was a simple patch similar to this:

Code: Select all

diff --git a/drivers/mmc/host/mmc_spi.c b/drivers/mmc/host/mmc_spi.c
index ae19d83..e21b8be 100644
--- a/drivers/mmc/host/mmc_spi.c
+++ b/drivers/mmc/host/mmc_spi.c
@@ -1386,8 +1386,7 @@ static int mmc_spi_probe(struct spi_device *spi)
        host->data = kmalloc(sizeof(*host->data), GFP_KERNEL);
        if (!host->data)
                goto fail_nobuf1;
-
-       if (spi->master->dev.parent->dma_mask) {
+       if (0) {
                struct device   *dev = spi->master->dev.parent;

                host->dma_dev = dev;
That made mmc_spi detect sd cards - with a corresponding device-tree...
dmesg shows:

Code: Select all

[ 8687.130221] mmc1: host does not support reading read-only switch, assuming write-enable
[ 8687.130276] mmc1: new SD card on SPI
[ 8687.141819] mmcblk1: mmc1:0000 SD2GB 1.86 GiB
[ 8687.154384]  mmcblk1: p1 p2
Then you can mount the mmc directly in the kernel:

Code: Select all

mount /dev/mmcblk1p1 /mnt
Hello Martin

Can you please tell me how to apply the above patch. I am running the latest raspbian 4.1.13 in my raspberry pi a+. My rpi a+ does not recognize the SPI SD Card.

Do i have to compile from source? There are NO dmesg or any logs anywhere that can show it recognizes the mmc1.

Thanking you
Rahul

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

Who is online

Users browsing this forum: piras77 and 15 guests