1
\$\begingroup\$

I started my project of interfacing MicroSD Card with PIC32MX795F512L. To start with this first I made a program to test SPI protocol in which I did loopback test by shorting the SDO & SDI line. It was working fine. Then I moved ahead and write a good program to interface card but I even failed to initialize the card. So I though of purchasing some tools like saleae logic and MicroSD Sniffer. As this is my first time using the analyzer, what my understanding is to when we start our program, at the same time we have to start capturing the data in analyzer.

With this understanding, I tested a UART:

while(1)
{
  putsUART1("Hello");
  DelayMs(500);
}

I captured this in analyzer and everything was fine. Now I thought of capturing the SPI loopback test program but I am seeing some random data in analyzer. Following is the code for it:

void initSPI(void)
{
SPI1BRG = 71; 
SPI1CONbits.MSTEN = 1; //MasterEnable
SPI1CONbits.CKE = 1;  //CKE on. CKP OFF
SPI1CONbits.ON = 1;  //SPI Module ON
}

unsigned char writeSPI(unsigned int b)
{

    SPI1BUF=b; // write to buffer for TX
    while( !SPI1STATbits.SPIRBF); // wait transfer complete
    return(SPI1BUF);

}

and in main function, after calling initSPI, in while(1) I am sending data to SPI channel:

while(1)
{
 x = writeSPI(0x12);
}

I am sending 0x12. So in the analyzer MOSI(SDO) should show this value but its showing some random values. I have connected the pins correctly and SPI settings in the analyzer is according to my settings in the code. I am capturing the data for 1sec and speed at 24MS/s. Also I want to know that how can I check at what frequency SPI Protocol is running.? Any help.!

Here is the image of analyzer result Image of analyzer result

and analyzer settings for SPI analyzer settings for SPI

\$\endgroup\$
1
  • \$\begingroup\$ Well a little bit of hunting, I have found how to check frequency \$\endgroup\$ – user007 Feb 17 '16 at 7:09
1
\$\begingroup\$

Remember that a logic analyzer is nothing more than a fast clocked comparator. What do comparators do? They compare a voltage! With a reference voltage. This is then clocked and at the clock the channel was measured if it was high or lower than the reference voltage.

So, You have two things to adjust. The clock, and the reference voltage.

  1. Clock - How do you know your clock is fast enough? Keep sampling faster, if you see more and more "edges" then you know you need to run faster. This applies for an unclocked signal, for a clocked signal like spi, its even easier. Since you know the data comes in groups (usually 8 bits at a time), all you have to do is find 8 rising edges and 8 falling edges. If it looks like the edges are being poorly sampled (ie there are only 5 or less samples between clocks) you will want to turn up the speed of your sampling, so you get more samples per clock.

  2. Reference Voltage - You have to set the reference voltage at the correct level. Check your logic levels, if its 5V cmos, set it at 2V. 3.3V TTL = 1.25V (or thereabouts).

A caveat to this is all logic analyzers have limited memory (except for the new 3000 series from tektronix and keysight, 20M of memory will buy you a lot of time, like almost infinity). So you can't usually sample the at the fastest speed because there is a trade-off of sampling speed vs how much time your total samples last.

\$\endgroup\$
2
  • \$\begingroup\$ I think clock and voltage both are correct. Voltage on the pins is 3.3 and I am seeing the correct 8 clocks in analyzer with exact 250khz frequency. But still MOSI is not showing the correct data. When I send 0x05 it shows 0xB0 or some other value but not 0x05. Am I missing something.? \$\endgroup\$ – user007 Feb 17 '16 at 9:00
  • \$\begingroup\$ If you aren't seeing what you expect, then you might have an issue with your digital lines or your software. if its your software, I can't help you there, I'm allergic to software and I also think its good for people to do there own debugging so they can learn. If its the digital lines you are worried about, I would get an oscilloscope and see if you are getting clean crispy waveforms and no ringing runting or slow transitions. You have timing spec to meet on your sending and receiving IC, they each have transition levels where they detect an on or off condition. \$\endgroup\$ – Voltage Spike Feb 17 '16 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.