Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Help initializing SD card using SPI, LPC2148

Status
Not open for further replies.

sknoogleplex

New Member
I'm using an LPC2148 and trying to interface it with a (non-HC) SD card. Specifically, I'm using this Olimex board.

The summary of my problem is that the card only responds with 0xff.

I've been using the demo code from **broken link removed**, **broken link removed**, and have been heavily reading NXP's Accessing SD/MMC card using SPI on LPC2000 along with manuals on SD. I can compile and run jcwren's code on my board, but either it doesn't work, or I'm not sure about exactly what commands will access the sd card. (A "file mount" and then "file ls" locks up the board using his code.) The SD card works fine with a PC card reader. I'm a grad student working on a project that I need to finish in a few months. I've been stuck on this same problem for about 20 hours and am at that point where I don't know what else to try.

Does anyone know of any reasons why I might only be getting 0xff responses or have references to any code that I could reasonably use from my linux (gcc etc.) environment, i.e. compile a .hex file, and upload it to my board to do _something_ with the sd card? (Reading or writing any byte anywhere, is fine.)


To clarify, here's what my code is doing (as far as I can tell):
-Setting pins 0.17, 0.18, and 0.19 to use the SSP (SCLK, MISO, and MOSI)
-Pulling pin 0.20, CS high.
-Setting the SPI clock to run at about 400 KHz
-Turning on SSP master mode
-Writing out 8 0xff bytes to SSPDR
-Waiting almost a second (in some tests)
-Reading 8 bytes out from SSPDR
-Pulling CS down. (Clearing pin 0.20)
-Waiting about another second (again, in some tests)
-Sending CMD0 (idle/reset) to the card (0x40, 0x00, 0x00, 0x00, 0x00, 0x95)
>>>Looping through the 6 bytes in order
>>>Make sure Tx buffer is not full and SPI is not busy
>>>Write the byte
>>>Wait until receive buffer has something
>>>Read receive buffer
-Waiting another second (in some tests)
-Loop forever
>>>Write an 0xff byte
>>>Read receive buffer
>>>(print the read value out - it's always 0xff)

My code is pretty ugly right now and all assembly (this is necessary eventually although I'd be extremely happy to have something working in c too - I just need to diagnose the problem), but if someone really wants to see it, I can clean it up and post a link.

Any help is _very_ appreciated. Thanks.

Ryan
 
Here's some code I'm using successfully on an Atmel chip..

Code:
	unsigned char Byte;
	unsigned int i;

    spiDisableDevices();
	/* start off with 80 bits of high data with card deselected */
	for(i=0;i<10;i++)
		Byte = SpiByte(0xFF);
		
	/* now send CMD0 - go to idle state, try up to 100 times */
	MMCCommand(0,0,0,0);  /* software reset command */

	if (MMCGet() != 1)
	  {
		  spiEnableDevice('s');
		  SpiByte(0xff);
		  return -1;  // MMC Not detected
	  }
	
	 
	/* send CMD1 until we get a 0 back, indicating card is done initializing */
	i =0xffff;    /* but only do it up to 1000 times */
	Byte = 1;
	
	while (Byte && i--){
	   MMCCommand(1,0,0,0);
	   Byte = MMCGet();
	   }
	
	spiEnableDevice('s');
	SpiByte(0xff); // Clear SPI
	
	if (Byte){
	  return -2;  // Init Fail
	  }
	  

	return 0;

At the very least, it gives you a comparison point. What clock speed is your SPI bus? I know at one point I was getting 0xFF back from everything I sent to my SD card. I think I had multiple problems, but part of it was that I had burned out my SD card ;) If yours works on the PC, though, then that's not your issue.

Edit: Nevermind, I see you are using 400kHz - that should be slow enough for the card.
 
Last edited:
is there any reason you are not using the NXP C code?

Dan

No, I just didn't see any NXP c code that looked appropriate. Did you have one in mind?

I did pretty much write my code to mimic their document on Accessing SD/MMC card using SPI on LPC2000. I've also used several other people's code as a reference, which I've essentially copied over in function. Since none of that is working, I'm getting to the point where I'd like to find something that I can actually compile and as is and run to make sure I'm not messing up some small detail somewhere on the way.

(My next plan is to basically copy all the code from the NXP document on Accessing SD/MMC card using SPI on LPC2000 over and try to make that work.)

Thanks.
 
It's a long shot as this could be many issues, but I was getting the same thing when my CPHASE was set incorrectly. I'd only get 0xFF responses. Data was clocking out on the wrong edge.
 
I see it now. I do have it set correctly.

Anyway, I did finally get some demo c code to work. :) So, I'm golden. Thanks all.
 
Status
Not open for further replies.

Latest threads

Back
Top