jbsv
Posts: 5
Joined: Mon Apr 07, 2014 11:21 pm

Bit-banging SPI for SD access?

Mon Apr 07, 2014 11:35 pm

By looking at the schematics for the Raspberry Pi, I seen the SD card is connected to BCM2835 like this:

SCLK --- GPIO48
DI --- GPIO49
DO --- GPIO50
CS --- GPIO53


Would it be possible to access the SD card by bit-banging SPI using these GPIOs? or will the EMMC module make this impossible?

User avatar
joan
Posts: 15117
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Bit-banging SPI for SD access?

Tue Apr 08, 2014 7:27 am

Why not just try it and find out?

You will then add to the knowledge pool.

Remember all the programs reside on the SD card by default. You'll probably need to mount your file systems on a USB based disk or hold them in RAM.

PiGraham
Posts: 4208
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Bit-banging SPI for SD access?

Tue Apr 08, 2014 8:02 am

Why do you want to do this? What access to the SD card do you need that don't have via the OS?

[ETA] Doh! Just realised this is in Baremetal. :oops:
Last edited by PiGraham on Thu Apr 10, 2014 8:09 am, edited 1 time in total.

hldswrth
Posts: 108
Joined: Mon Sep 10, 2012 4:14 pm

Re: Bit-banging SPI for SD access?

Tue Apr 08, 2014 12:54 pm

or will the EMMC module make this impossible?
or unnecessary? Is there a specific reason why you want to bit-bang SPI? The Pi provides the EMMC registers to allow you to interact with the SD card more simply and efficiently.

dwelch67
Posts: 968
Joined: Sat May 26, 2012 5:32 pm

Re: Bit-banging SPI for SD access?

Tue Apr 08, 2014 6:32 pm

bit banging spi is super easy you should try it.

The alternate function map does have something there just says it is "internal" so I assume that is this eMMC IP and assume you need to switch to alternate function 0 to use it. If you want to try to use the emmc peripheral to talk to the sd card. some bare metal code for that would also be appreciated...

David

jbsv
Posts: 5
Joined: Mon Apr 07, 2014 11:21 pm

Re: Bit-banging SPI for SD access?

Thu Apr 10, 2014 7:54 am

PiGraham wrote:Why do you want to do this? What access to the SD card do you need that don't have via the OS?
There is currently no OS installed. Also, the Raspbery Pi seems to exist for the reason of learning, even though it's also useful for many other things, I use it most for learning.

jbsv
Posts: 5
Joined: Mon Apr 07, 2014 11:21 pm

Re: Bit-banging SPI for SD access?

Thu Apr 10, 2014 7:55 am

hldswrth wrote:or unnecessary? Is there a specific reason why you want to bit-bang SPI? The Pi provides the EMMC registers to allow you to interact with the SD card more simply and efficiently.
The FatFs project comes with a bit-banging control module that will work on the Raspberry Pi, if the underlying GPIO control functions are supplied. This could be a simple way to implement SD access for a bare metal application. However, it's probably better to use the EMMC registers as you are suggesting.

jbsv
Posts: 5
Joined: Mon Apr 07, 2014 11:21 pm

Re: Bit-banging SPI for SD access?

Thu Apr 10, 2014 7:56 am

I have now been trying this out as suggested, and my conclusion so far is that it's not possible to access the internal SD card by bit-banging SPI over the GPIOs. The main problem is that DO cannot be read. The initial test I made for finding out if communication was possible is like this:

1. Initialize CK (GPIO48) as output
2. Initialize DI (GPIO49) as output
3. Initialize DO (GPIO50) as input
4. Initialize CS (GPIO53) as output

5. Set DI (GPIO49) low

6. Read pin level of DO (GPIO50)

The default pull state on GPIOs connected to SD card is high, so after the above one would expect DO to be low, but it's high.

If I enable pull down control on GPIO50, the pin level will always be low. I do not understand why, because GPIO50 is configured as input and should read the level of DO on SD card, and not respond to changes on the pull control. Maybe the EMMC IP is somehow interfereing.

dwelch67
Posts: 968
Joined: Sat May 26, 2012 5:32 pm

Re: Bit-banging SPI for SD access?

Thu Apr 10, 2014 1:50 pm

pull controls are often for inputs, so one would expect the input to be affected...what happens when you disable both pull up and pull down?

jbsv
Posts: 5
Joined: Mon Apr 07, 2014 11:21 pm

Re: Bit-banging SPI for SD access?

Fri Apr 11, 2014 2:11 am

dwelch67 wrote:pull controls are often for inputs, so one would expect the input to be affected...what happens when you disable both pull up and pull down?
When pull-up/down is disabled, the pin level of GPIO50 is always high, and not affected by setting GPIO49 low or high. It seems like using the EMMC IP to talk to the internal SD card is a better route to take.

sibislaw
Posts: 5
Joined: Fri May 02, 2014 5:10 am

Re: Bit-banging SPI for SD access?

Sat May 03, 2014 10:58 am

The alternate function of this pins is called internal because its wired to internal mass media storage controller (SD card controller). Broadcom peripheals pdf covers that on page 65.

User avatar
joan
Posts: 15117
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Bit-banging SPI for SD access?

Sat May 03, 2014 11:21 am

sibislaw wrote:The alternate function of this pins is called internal because its wired to internal mass media storage controller (SD card controller). Broadcom peripheals pdf covers that on page 65.
That doesn't mean you can't change the gpio modes. It just means you can break the system if you are not careful.

sibislaw
Posts: 5
Joined: Fri May 02, 2014 5:10 am

Re: Bit-banging SPI for SD access?

Sat May 03, 2014 12:51 pm

Yes, according to documentation you can use his GPIO pins like others. And, i think you cannot break system. I studied electrical schematics and there is resistors on interface to SDCARD, so i think even if you will accidentaly set pin to high output, you wont burn electronics.

sibislaw
Posts: 5
Joined: Fri May 02, 2014 5:10 am

Re: Bit-banging SPI for SD access?

Sat May 03, 2014 1:05 pm

You can only broke GPIO47 sdcard detection, because it is wired to the ground when card is inserted.


EDIT: I had succesfully programmed GPIO47 for sd card insertion detecting so i suppose there is strong capability for use bit banging. Share your experiments.

JacobL
Posts: 76
Joined: Sun Apr 15, 2012 2:23 pm

Re: Bit-banging SPI for SD access?

Sat May 03, 2014 9:38 pm

First off, what is the exact reason for bitbanging SPI here? If it falls under something like "I want to see if I can/I want to learn something/for fun/etc" then we are in the baremetal forum, and those are very good reasons here. If it is just to get access to the SD card by any possible means, then I believe it will be easier to write a driver for the eMMC controller that is connected to the card.

If we assume that you are in the first group, the first thing I would recommend for a project like this is to get a USB based logic analyzer. If your code does not behave as you expect, this can both be because the command/data you sent has a problem, or it means that the data sent from the SD card is not read properly from your software. A logic analyzer tells you which one it is. I prefer the USB based ones because they let you save the trace to the PC memory, enabling very long traces. This one is cheap: http://www.dx.com/p/logic-analyzer-w-du ... ack-148945

About your question:

Why do you think that setting DI (GPIO49) low will result in the SD card pulling DO (GPIO50) low?

Did you set CS (GPIO53) low? Devices attached to SPI will normally not drive any output pins unless their CS (Chip Select) is low. That way multiple devices can be connected to the same SPI channel. This is also the reason for the requirement of pull-up resistors.

Are you actually shifting the clock with your data? The SD card cannot do anything unless you

Before you can actually get any response from the SD card, you need to send a number of clock pulses and a CMD0 while CS is low among other things. The sequence is described here: http://nerdclub-uk.blogspot.dk/2012/11/ ... -card.html and some more details here: http://www.dejazzer.com/ee379/lecture_n ... d_card.pdf

Note also that the initialisation sequence of an SD card must run at a maximum clock frequency of 400kHz. This means that there should be a minimum of 1.25us between each time you change the value of the clock pin. If you run faster than this, chances are that the initialisation will fail, and you will not see any response from the card. The logic analyzer allows measurement of this time.

If you get a logic analyzer on your system, then a good way to proceed would be to use it to develop your transmitting side first. Verify that you can send well-formed SD-SPI commands with CS low before you plug in the SD card and write code to decode the responses. That way you can break down the system a bit.

Edit: Note that SD cards use SPI mode 0. There are timing diagrams on page 2 in the PDF I link to showing what that means for the clock signal polarity, when to read MISO (Master In Slave Out) and when to shift data into MOSI (Master Out Slave In).

/Jacob

JacobL
Posts: 76
Joined: Sun Apr 15, 2012 2:23 pm

Re: Bit-banging SPI for SD access?

Sat May 03, 2014 10:04 pm

jbsv wrote:If I enable pull down control on GPIO50, the pin level will always be low. I do not understand why, because GPIO50 is configured as input and should read the level of DO on SD card, and not respond to changes on the pull control. Maybe the EMMC IP is somehow interfereing.
Let me add to the previous by answering this specifically: Unless you have actively selected and initialised the SD card, then the DO pin will actually be tristated. This means that whatever pull resistor you configure for that pin will be what you see on the GPIO initially. The requirement for SPI would be that this pin (normally called MISO) is pulled high, since the slave(s) will tristate the pin unless they are actively selected.

/Jacob

partis
Posts: 30
Joined: Sun Nov 13, 2011 9:54 pm
Location: North East England
Contact: Website

Re: Bit-banging SPI for SD access?

Sun Jun 22, 2014 9:17 am

Yup - you can :-)

I do SD access via SPI as it is part of my portable modular OS.

I have only found one card which does not appear to support SPI, and that is a 8GByte Lexar card.
Gary Partis

Return to “Bare metal, Assembly language”