# Strange output from SD Memory

#### spinnaker

Joined Oct 29, 2009
7,835
I am using SPI to communicate to SD Memory.

I am having issues reading the data with the PIC so I put my logic analyzer on it. My logic analyzer was not reading it.

So I put my scope on the output of the SD chip.

This is what I am getting:

I have nothing on the output of the SD chip other than the scope.

Both the PIC and SD chip are running at 3.3V.

What is causing this and how do I fix it?

#### Attachments

• 88.3 KB Views: 67
Last edited:

#### ErnieM

Joined Apr 24, 2011
8,181
I agree, this waveform is strange, it doesn't look "digital" though exactly what we are seeing here is unclear as the vertical & horizontal resolution is not defined, nor is ground.

I've make a few of these hookups work. The worst problem I had was the first time thru where I connected the SPI out of the PIC to SPI out of the SD, and the SPI in of the PIC to SPI in of the SD. Of course these should be the other way round, SPI out of PIC to SPI in of SD, SPI in of PIC to SPI out of SD.

Do you have a schematic?

Did you write your own code or are you using the Microchip library for this? I've found the MC libs to be a very handy working base.

#### spinnaker

Joined Oct 29, 2009
7,835
I agree, this waveform is strange, it doesn't look "digital" though exactly what we are seeing here is unclear as the vertical & horizontal resolution is not defined, nor is ground.

I've make a few of these hookups work. The worst problem I had was the first time thru where I connected the SPI out of the PIC to SPI out of the SD, and the SPI in of the PIC to SPI in of the SD. Of course these should be the other way round, SPI out of PIC to SPI in of SD, SPI in of PIC to SPI out of SD.

Do you have a schematic?

Did you write your own code or are you using the Microchip library for this? I've found the MC libs to be a very handy working base.
The tops of it are nice and square. That is a 1 V per division. 0V starts just above the 2nd division. 1ms per div horiz.

But I am getting nothing now! Not sure what happened. I am beginning to doubt if I was even measuring the output of the SD memory.

I made my own SD chip holder by soldering header pins to a micro SD holder. I decieded to solder on a decoupling cap. But I think I made some changes before that so I am not sure if I broke a connection inside the holder or not. I should have made sure things were working at least as above before making the change. Lessons learned.

I am using code based on Petit FF.

http://elm-chan.org/fsw/ff/00index_p.html

The Microchip library seemed awfully complicated but maybe I will take another look. This is that I am doing now.

Rich (BB code):
SDCTRL_deselect();
sdspi_rxByte();
SDCTRL_select();
sdspi_rxByte();
sdspi_txByte(0x40);

while (1)
{

SDCTRL_deselect();
sdspi_rxByte();
SDCTRL_select();
sdspi_rxByte();
sdspi_txByte(0x49);
sdspi_rxByte();

}
Is there a specific speed I should be transmitting at?

#### spinnaker

Joined Oct 29, 2009
7,835
The clock, data out and chip select all seem to be working.

I am sending a 0x49 on the bus and the LA confirms it.

Here is the modified code used for the LA

Rich (BB code):
while (1)
{

//SDCTRL_deselect();
//sdspi_rxByte();
SDCTRL_select();
//sdspi_rxByte();
sdspi_txByte(0x49);
SDCTRL_deselect();
//sdspi_rxByte();

}

#### Attachments

• 16.6 KB Views: 57

#### spinnaker

Joined Oct 29, 2009
7,835
Hah! Got my same strange waveform back. Apparently you have to send a command to the SD memory in a very specific way or it won't respond. I was just sending the command with no arguments so I did not send arguments.

The chip is now responding but again with the strange waveform. I will post an updated pic in a day or two,

#### thatoneguy

Joined Feb 19, 2009
6,359
It's every 2 mS, so I'd guess that it's releasing the line to low to check if something else is holding it high? The bottom edge doesn't lookneat and tidy, though. What brand of SD cards are you using?

#### spinnaker

Joined Oct 29, 2009
7,835
Well I figured it out. Sort of. Turns out the library code was not clearing the corresponding ANSELH bit for RB4 so the pin was still configured as an analog input pin.

So I set

ANSELHbits.ANS10 = 0;

This allowed me to read the pin as a bin input if I tied the pin high or low. I was also able to read 0xFF and 0x00 when I read the SPI input.

BUT if I connect the output of the SD memory chip direct to the SI pin on the pic then I get no output from the SD. Even after I disconnect the pin still no output from the SD. I only get output again after I cycle the power on the SD then the SD starts output again. But with my strange square wave.

This is 3v / division 1ms per division zero at center.

What would cause that strange waveform?

Why can't the SD drive the pic?

I am definitely getting data from the SD. Here is what the LA says.

#### Attachments

• 121.4 KB Views: 48
• 22.1 KB Views: 47
Last edited:

#### thatoneguy

Joined Feb 19, 2009
6,359
What is the bandwidth of your scope? The logic analyzer pulses are in the microsecond range, and could be appearing as the block on your scope, with the scope only showing the longer pause between bytes?

I'd think you would see "fuzz" under the square wave if that were the case on a 200Mhz+ scope, though, Unless you had the 10Mhz Noise Filter enabled on the scope, then the square wave would look like that, while the signal is actually what the logic analyzer is catching.

If the LA sees it, the PIC should be seeing it. So we are back to the problem of PIC and SPI? Is SPIBUF ever full after you clear it and reset BF?

#### spinnaker

Joined Oct 29, 2009
7,835
What is the bandwidth of your scope? The logic analyzer pulses are in the microsecond range, and could be appearing as the block on your scope, with the scope only showing the longer pause between bytes?

I'd think you would see "fuzz" under the square wave if that were the case on a 200Mhz+ scope, though, Unless you had the 10Mhz Noise Filter enabled on the scope, then the square wave would look like that, while the signal is actually what the logic analyzer is catching.

If the LA sees it, the PIC should be seeing it. So we are back to the problem of PIC and SPI? Is SPIBUF ever full after you clear it and reset BF?
It's a Tech 475 A. 200MHZ. I was thinking it was the bandwith of the scope but my inputs look nice and crisp and they are similar pulse width. I'll take a closer look at that in a bit
.
As I mentioned above the ANSEL bit was not set right. The input was analog and not digital. But if I set the ANSEL bit then the port goes low and stays low until disconnected from the SD.

Disconnected it reads fine as an SPI input if I force high or low.

The SD spec mentions pullups so I added a 10K pullup on the output of the SD. Now instead of the SD staying low it stays high. And similar thing. I have to disconnect the output of the SD and reset the chip.

The good news is that the output of the SD now looks a lot cleaner for whatever reason.

Last edited:

#### spinnaker

Joined Oct 29, 2009
7,835
I decieded to create a schematic

#### Attachments

• 25.2 KB Views: 39
Last edited:

#### thatoneguy

Joined Feb 19, 2009
6,359
If you change it to transmit, what does the LA show when you send a query to the SD card?

What compiler are you using?

P.S. About time for a schematic! I see you went back to the k22 as a known quantity, this helps.

#### spinnaker

Joined Oct 29, 2009
7,835
If you change it to transmit, what does the LA show when you send a query to the SD card?

What compiler are you using?

P.S. About time for a schematic! I see you went back to the k22 as a known quantity, this helps.
I am using the MPLab c18 Compiler.

Here is an update. I went back and rewired everything. Now I am seeing transitions when the output of the SD is connected to the SDI of the PIC.

But I am still not reading data in the SSBUF. But if I tie that pin high or low, I can read that value on PORTB bit 5 (PORTB bit 5 is on the same pin as SDI). If I tie it high or low, I would expect to read a 0xFF if tied high and 0x00 if tied low. But I am always reading zero.

So it must be something to do with the setup of the SDI pin I guess.

#### thatoneguy

Joined Feb 19, 2009
6,359
Swap out SD Card for EEPROM or other device you know works, and try it without changing any code. Just see what the signals do then.

The Logic analyzer might be adding too much of a load, but I very much doubt it. Best to start from a known working solution and change only one thing at a time until it stops working, then you usually have found the problem.

#### spinnaker

Joined Oct 29, 2009
7,835
I'm rewriting the SPI stuff with my own code. That way I know what the heck is going on.

#### thatoneguy

Joined Feb 19, 2009
6,359

It's for boostC, but you'll get the idea as it is in C and asm.

#### spinnaker

Joined Oct 29, 2009
7,835

It's for boostC, but you'll get the idea as it is in C and asm.
I still think my problem comes way before using an SD Library.

I still need to know if I tie my SDI high or low, should I see a corresponding change in SSSSPBUF ?

Also here is how Microchip is reading the bus

Rich (BB code):
 unsigned char TempVar;
TempVar = SSPBUF;        // Clear BF
PIR1bits.SSPIF = 0;      // Clear interrupt flag
SSPBUF = 0x00;           // initiate bus cycle
//while ( !SSPSTATbits.BF );                  // wait until cycle complete
while(!PIR1bits.SSPIF);  // wait until cycle complete
TempVar = SSPBUF;
return ( TempVar);       // return with byte read
Notice the //Initiate bus cycle.

Why are the doing that? From what I have read about SPI, data is transmitted when it is sent out. So wouldn't my previous write accomplish this?

#### spinnaker

Joined Oct 29, 2009
7,835
Well I just found out you can do this. I have a 18F45K20. I tied SDI high and I was able to read a 0xFF on the SDI pin. Then I tied SDI low and I was able to read a 0.

So either my 18f14k22 is bad or there is something wrong with the MPLab library for the 18F14k20.

#### thatoneguy

Joined Feb 19, 2009
6,359
I thought you could read EEPROMS with that 45k20 without a problem using the Microchip libraries?

I'd suggest posting your question on the microchip forums. They are quick with answers and might know a trick for both your k22 and j53 that you are bumping into problems with.

Have you tried I2C devices?

#### spinnaker

Joined Oct 29, 2009
7,835
I thought you could read EEPROMS with that 45k20 without a problem using the Microchip libraries?

I'd suggest posting your question on the microchip forums. They are quick with answers and might know a trick for both your k22 and j53 that you are bumping into problems with.

Have you tried I2C devices?
I finally found it. The wrong pin was being initialized. I must have looked at that 100 times.

The PIC is now working properly. Now all I need to do is get the SD initialized. It seems to be starting too now. But gets stuck waiting for the SD's idle state,

I have posted a couple of questions on Microchip. Those guys are fairly now responsive.

#### thatoneguy

Joined Feb 19, 2009
6,359
Well, progress is progress...

Look at the MikroC lib code for SD Card routines, or the code I linked to above.

One or the other might have a tidbit of info not covered by official documentation.

Are you trying to talk to a SDHC (>2GB) card, or SD card?