Strange output from SD Memory

Discussion in 'Embedded Systems and Microcontrollers' started by spinnaker, Nov 20, 2011.

  1. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    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:

    [​IMG]



    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?
     
    Last edited: Nov 20, 2011
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    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.
     
  3. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    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.

    Code ( (Unknown Language)):
    1.  
    2. SDCTRL_deselect();
    3. sdspi_rxByte();
    4. SDCTRL_select();
    5. sdspi_rxByte();
    6. sdspi_txByte(0x40);
    7.  
    8.  
    9. while (1)
    10. {
    11.  
    12.     SDCTRL_deselect();
    13.     sdspi_rxByte();
    14.     SDCTRL_select();
    15.     sdspi_rxByte();
    16.     sdspi_txByte(0x49);
    17.     sdspi_rxByte();
    18.  
    19. }
    20.    
    21.  
    Is there a specific speed I should be transmitting at?
     
  4. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    The clock, data out and chip select all seem to be working.

    [​IMG]


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

    Here is the modified code used for the LA

    Code ( (Unknown Language)):
    1.  
    2. while (1)
    3. {
    4.  
    5.     //SDCTRL_deselect();
    6.     //sdspi_rxByte();
    7.     SDCTRL_select();
    8.     //sdspi_rxByte();
    9.     sdspi_txByte(0x49);
    10.     SDCTRL_deselect();
    11.     //sdspi_rxByte();
    12.  
    13. }
    14.  
     
  5. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    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,
     
  6. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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?
     
  7. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    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.

    [​IMG]


    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.

    [​IMG]
     
    Last edited: Nov 24, 2011
  8. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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?
     
  9. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    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: Nov 24, 2011
  10. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    I decieded to create a schematic

    [​IMG]
     
    Last edited: Nov 24, 2011
  11. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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.
     
  12. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    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.
     
  13. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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.
     
  14. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    I'm rewriting the SPI stuff with my own code. That way I know what the heck is going on.
     
  15. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Maybe start with This user written library and work from there?

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

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    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


    Code ( (Unknown Language)):
    1.  
    2.  unsigned char TempVar;
    3.   TempVar = SSPBUF;        // Clear BF
    4.   PIR1bits.SSPIF = 0;      // Clear interrupt flag
    5.   SSPBUF = 0x00;           // initiate bus cycle
    6.   //while ( !SSPSTATbits.BF );                  // wait until cycle complete
    7.   while(!PIR1bits.SSPIF);  // wait until cycle complete
    8.   TempVar = SSPBUF;
    9.   return ( TempVar);       // return with byte read
    10.  
    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?
     
  17. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    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.
     
  18. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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?
     
  19. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    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.
     
  20. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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?
     
Loading...