• AVR Freaks

SPI and MMC

Page: < 123 > Showing page 2 of 3
Author
jsluke
Starting Member
  • Total Posts : 69
  • Reward points : 0
  • Joined: 2010/11/05 09:24:51
  • Location: 0
  • Status: offline
Re:SPI and MMC 2011/01/17 02:52:58 (permalink)
0

Like the logic analyser - very neat!

At the moment, the issue is that there are two many variables - the fault could be:

  1. wiring.
  2. software.
  3. configuration (e.g. the hardware profile)
I've come up with another plan (number 2,746)!  I have a PIC18F Starter Kit so I can at least ensure the software is working and eliminate the wiring issue.  Watch this space.

Thanks.

James.
#21
jsluke
Starting Member
  • Total Posts : 69
  • Reward points : 0
  • Joined: 2010/11/05 09:24:51
  • Location: 0
  • Status: offline
Re:SPI and MMC 2011/01/17 14:00:50 (permalink)
0

Sorry, but I should have asked this question this morning.

What were the diagnostic steps you followed to understand the timing (& other SD Card behaviours)?

Thanks - any help would really be appreciated.

James.

#22
Jim Nickerson
User 452
  • Total Posts : 6187
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re:SPI and MMC 2011/01/18 08:55:20 (permalink)
0

This document http://alumni.cs.ucr.edu/~amitra/sdcard/Additional/sdcard_appnote_foust.pdf has some useful information in the section "Card Initialization".
This site has a logic trace of the init sequence http://www.esawdust.com/blog/serial/files/SPI_SD_MMC_ATMega128_AVR.html


There are many other documents on similar subjects.
Once I could monitor the pins to the SD card and knew what was required the problems were easier to resolve.
#23
jsluke
Starting Member
  • Total Posts : 69
  • Reward points : 0
  • Joined: 2010/11/05 09:24:51
  • Location: 0
  • Status: offline
Re:SPI and MMC 2011/01/18 10:54:24 (permalink)
0
Thanks Jim - for info I had found this web site whose author has a very similar story to mine => http://www.d.umn.edu/~cpr...ubRes/Hardware/SDcard/

Everything points to a wiring issue at the moment so I'm going to focus on that.

I'll let you know how it goes.

Thanks again.

James.
#24
jsluke
Starting Member
  • Total Posts : 69
  • Reward points : 0
  • Joined: 2010/11/05 09:24:51
  • Location: 0
  • Status: offline
Re:SPI and MMC 2011/01/25 14:02:01 (permalink)
0
Jim

I've set myself up with an Oscilloscope and a couple of probes and am now trying to work this problem from the ground up.  I'm working line by line through the code to figure out what is meant to happen and what is actually happening.

So far (it isn't very far I admit), I have a very simple program:

void main(void)
{
    int i=0;

    i=GetSystemClock();
    FSInit();
}
Monitoring the pins, the only activity is that when I run the program CS goes high.

I can't find a clock pulse - it should be set up to be on pin 13 of my PIC 18F26J50 (RC2).  I've also checked all the other pins and nothing is showing a clock signal.

The call to GetSystemClock returns a value of 27,648.  I am running an external 12MHz oscillator with Osciallator configuration set to HS.

Any ideas whether I should be seeing a clock signal when calling FSInit()?

Thanks.

James.

#25
Jim Nickerson
User 452
  • Total Posts : 6187
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re:SPI and MMC 2011/01/26 08:51:53 (permalink)
0
If we are looking at the same FSInit() in FSIO.c it calls MDD_InitIO(); which is mapped to MDD_SDSPI_InitIO() in SD-SPI.c ( if you are using SPI which it looks like you are ) which floats the SD_CS line and then sets it high.
So it looks like the correct thing is happening, CS goes high.
The call to DISKmount() ends up calling MDD_SDSPI_MediaInitialize() which calls mSend8ClkCycles() ( WriteSPIM(0xFF); ) 10 times which should send 80 clocks.
If you are not seeing the 80 clocks then check in WriteSPIM(..) to see why not.
 If this is not one of the predefined configurations with the predefined hardware check your defines.
 
 
#26
jsluke
Starting Member
  • Total Posts : 69
  • Reward points : 0
  • Joined: 2010/11/05 09:24:51
  • Location: 0
  • Status: offline
Re:SPI and MMC 2011/01/26 13:24:15 (permalink)
0
Thanks Jim

WriteSPIManual is returning -1 at line 1108 =>

if ((SPIOUTPORT != SPIOUTLAT) || (SPICLOCKPORT != SPICLOCKLAT))
    return -1

so I believe this is a hardware configuration issue?

My hardware profile is =>

    /*******************************************************************/
    /******** MDD File System selection options ************************/
    /*******************************************************************/
    #define USE_PIC18

    #define USE_SD_INTERFACE_WITH_SPI

    // Macros for input and output TRIS bits
    #define INPUT_PIN   1
    #define OUTPUT_PIN  0

    #define TRIS_CARD_DETECT    TRISBbits.TRISB4    // Input
    #define CARD_DETECT         PORTBbits.RB4
   
    #define TRIS_WRITE_DETECT   TRISAbits.TRISA3    // Input
    #define WRITE_DETECT        PORTAbits.RA3

    // Chip Select Signal
    #define SD_CS               PORTCbits.RC6
    #define SD_CS_TRIS          TRISCbits.TRISC6
       
    // Card detect signal
    #define SD_CD               PORTBbits.RB4 //0 //PORTBbits.RB4
    #define SD_CD_TRIS          TRISBbits.TRISB4
       
    // Write protect signal
    #define SD_WE               PORTAbits.RA3
    #define SD_WE_TRIS          TRISAbits.TRISA3

    // Defines for the PIC18 Starter kit
    #define SPICLOCK            TRISCbits.TRISC2
    #define SPIIN               TRISCbits.TRISC7
    #define SPIOUT              TRISBbits.TRISB3

    // Latch pins for SCK/SDI/SDO lines
    #define SPICLOCKLAT         LATCbits.LATC2
    #define SPIINLAT            LATCbits.LATC7
    #define SPIOUTLAT           LATBbits.LATB3

    // Port pins for SCK/SDI/SDO lines
    #define SPICLOCKPORT        PORTCbits.RC2
    #define SPIINPORT           PORTCbits.RC7
    #define SPIOUTPORT          PORTBbits.RB3

    // Registers for the SPI module you want to use
    #define SPICON1             SSP2CON1
    #define SPISTAT             SSP2STAT
    #define SPIBUF              SSP2BUF
    #define SPISTAT_RBF         SSP2STATbits.BF
    #define SPICON1bits         SSP2CON1bits
    #define SPISTATbits         SSP2STATbits

    #define SPI_INTERRUPT_FLAG  PIR3bits.SSP2IF
    #define SPIENABLE           SSP2CON1bits.SSPEN

I have pin 13 (RC2) wired to CLK on the SD Card and pin 24 (RB3) wired to DI on the SD Card.

James.
#27
Jim Nickerson
User 452
  • Total Posts : 6187
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re:SPI and MMC 2011/01/27 09:21:05 (permalink)
0
jsluke
WriteSPIManual is returning -1 at line 1108 => 

if ((SPIOUTPORT != SPIOUTLAT) || (SPICLOCKPORT != SPICLOCKLAT))
   return -1


So which of the two conditions is causing the -1 return?
#28
jsluke
Starting Member
  • Total Posts : 69
  • Reward points : 0
  • Joined: 2010/11/05 09:24:51
  • Location: 0
  • Status: offline
Re:SPI and MMC 2011/01/27 09:46:20 (permalink)
0

It's (SPICLOCKPORT!=SPICLOCKLAT) that is returning -1.

When implementing the hardware profile, I simply copied the profile for the PIC18F46J50 - the only change I made was to the definitions of SPIIN, SPIINLAT and SPIINPORT which referenced pin D6 - I changed these to reference pin C7.

Thanks.

James.
#29
Jim Nickerson
User 452
  • Total Posts : 6187
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re:SPI and MMC 2011/01/27 11:55:44 (permalink)
0
There must be some other difference between 18F26J50 and 18F46J50.
#30
jsluke
Starting Member
  • Total Posts : 69
  • Reward points : 0
  • Joined: 2010/11/05 09:24:51
  • Location: 0
  • Status: offline
Re:SPI and MMC 2011/01/27 14:44:48 (permalink)
0

I understood that the two chips were identical in core, FLASH and RAM ( http://www.microchip.com/forums/tm.aspx?tree=true&m=545869 )

I'm going back to that thread for mode advice on porting from the 18F46J50 to the 18F26J50.

James.
#31
jsluke
Starting Member
  • Total Posts : 69
  • Reward points : 0
  • Joined: 2010/11/05 09:24:51
  • Location: 0
  • Status: offline
Re:SPI and MMC 2011/01/30 01:53:42 (permalink)
0
Still trying to work through this issue.

This may be a stupid question, but what exactly does (SPICLOCKPORT!=SPICLOCKLAT) mean?

Does it mean that the SPICLOCKPORT and SPICLOCKLAT are not referencing the same pin?  Do they reference something else where they could have a different value?  I know it's an obvious line of C++, however I'm trying to understand what it actually means on the hardware so that I can understand the problem.

Thanks.

James.
#32
Jim Nickerson
User 452
  • Total Posts : 6187
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re:SPI and MMC 2011/01/30 06:56:04 (permalink)
0
jsluke

Still trying to work through this issue.

This may be a stupid question, but what exactly does (SPICLOCKPORT!=SPICLOCKLAT) mean?

Does it mean that the SPICLOCKPORT and SPICLOCKLAT are not referencing the same pin?  Do they reference something else where they could have a different value?  I know it's an obvious line of C++, however I'm trying to understand what it actually means on the hardware so that I can understand the problem.

Thanks.

James.

SPICLOCKPORT does an input from RC3 in your case.
SPICLOCKLAT does an input from LATC3 in your case.
The code tests to see if they are equal.
Anything written to the latch port is written to the port.
I do not seem to find this line in  WriteSPIManual 

is returning -1 at line 1108 => 

if ((SPIOUTPORT != SPIOUTLAT) || (SPICLOCKPORT != SPICLOCKLAT)) 
    return -1 

What version of the stack and which project are you looking at?
WriteSPIManual manipulates the port bits directly ( bit bang ).
#33
jsluke
Starting Member
  • Total Posts : 69
  • Reward points : 0
  • Joined: 2010/11/05 09:24:51
  • Location: 0
  • Status: offline
Re:SPI and MMC 2011/01/30 13:08:59 (permalink)
0

I copied the "USB Device - Mass Storage - SD Card reader" project from the PIC18F Starter Kit Demos folder.

I've made a breakthrough today.  Whilst working through the data sheet I noticed that specific pins were designated as SPI input, output and clock.  I re-wired my circuit accordingly (and changed the Hardware Profile) so that:

  • SPICLK   =>  RB4 (pin 25)
  • SPIIN      => RB5 (pin 26)
  • SPIOUT   => RC7 (pin 18)
  • SD_CD    => RB3 (pin 24)
The code now makes it past the if ((SPIOUTPORT != SPIOUTLAT) || (SPICLOCKPORT != SPICLOCKLAT)) line.  I still get to a media cannot initialize error (line 1590 of SD_SPI.c) so I'm re-examining the wiring to see if I've missed something.

I do feel like we're making progress (just very slowly) [8)]

Thanks.

James.
#34
jsluke
Starting Member
  • Total Posts : 69
  • Reward points : 0
  • Joined: 2010/11/05 09:24:51
  • Location: 0
  • Status: offline
Re:SPI and MMC 2011/01/30 13:51:58 (permalink)
0
I should have included the following information in my last post.

The media initialization error occurs in the following code:

        //Media requires 80 clock cycles to startup [8 clocks/BYTE * 10 us]
        for(timeout=0; timeout<10; timeout++)
            WriteSPIManual(0xFF);
   
        SD_CS = 0;
       
        Delayms(1);
   
        // Send CMD0 to reset the media
        response = SendMMCCmdManual (GO_IDLE_STATE, 0x0);

        if ((response.r1._byte == MMC_BAD_RESPONSE) || ((response.r1._byte & 0xF7) != 0x01))
        {
            SD_CS = 1;                              // deselect the devices
            mediaInformation.errorCode = MEDIA_CANNOT_INITIALIZE;
            return &mediaInformation;
        }
The condition ((response.r1._byte & 0xF7)!=0x01) is true.

Thanks.

James.

#35
jsluke
Starting Member
  • Total Posts : 69
  • Reward points : 0
  • Joined: 2010/11/05 09:24:51
  • Location: 0
  • Status: offline
Re:SPI and MMC 2011/01/30 13:57:17 (permalink)
0

I am using version 1.2.2 of SD_SPI.c - I've attached the file.

Thanks.

James.
#36
Jim Nickerson
User 452
  • Total Posts : 6187
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re:SPI and MMC 2011/01/30 14:52:51 (permalink)
0
jsluke

I should have included the following information in my last post.

The media initialization error occurs in the following code:

        //Media requires 80 clock cycles to startup [8 clocks/BYTE * 10 us]
        for(timeout=0; timeout<10; timeout++)
           WriteSPIManual(0xFF);
  
       SD_CS = 0;
      
       Delayms(1);
  
       // Send CMD0 to reset the media
       response = SendMMCCmdManual (GO_IDLE_STATE, 0x0);

       if ((response.r1._byte == MMC_BAD_RESPONSE) || ((response.r1._byte & 0xF7) != 0x01))
       {
           SD_CS = 1;                              // deselect the devices
           mediaInformation.errorCode = MEDIA_CANNOT_INITIALIZE;
           return &mediaInformation;
       }
The condition ((response.r1._byte & 0xF7)!=0x01) is true.

Thanks.

James.

What is the value of response.r1._byte ?

Do you see data  I, O and clk on the scope ?

#37
jsluke
Starting Member
  • Total Posts : 69
  • Reward points : 0
  • Joined: 2010/11/05 09:24:51
  • Location: 0
  • Status: offline
Re:SPI and MMC 2011/01/30 16:30:23 (permalink)
0

response.r1._byte=0x00

DI on the SD Card (wired to RC7 - pin 18 on the chip) goes high when SPIOUTPUT = OUTPUT is executed (line 1106 in SD_SPI.c).  It then stays high throughout.

DO on the SD Card (wired to RB5 - pin 26) stays low throughout.

There is some (strange) clock activity on SPICLK (RB4 - pin 25) when the following lines execute =>

    //Media requires 80 clock cycles to startup [8 clocks/BYTE * 10 us]
    for(timeout=0; timeout<10; timeout++)
        mSend8ClkCycles();
I've attached a screenshot of the oscilloscope showing a brief sequence of 7 pulses after which there is no further activity.

Something I would like to check is whether any pull ups / downs are required - at present I have a 10K pull up resistor on the SC Card's CS pin.  Do I need anything else?

Thanks.

James.

Attached Image(s)

#38
Jim Nickerson
User 452
  • Total Posts : 6187
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re:SPI and MMC 2011/01/30 17:58:48 (permalink)
0

You are expecting response.r1._byte == 0x01 ( the card is in the Idle State )
You are getting no data from the card.
The card is not initializing.
Your scope can not keep up to the data rate. In WriteSPIManual() try setting MANUAL_SPI_CLOCK_VALUE temporarily to a larger value so you can see the pulses.
If you do not see the pulses as in the examples discussed earlier the card will not initialize.
I do not use a pull up on other than CS, and no pull downs.
#39
jsluke
Starting Member
  • Total Posts : 69
  • Reward points : 0
  • Joined: 2010/11/05 09:24:51
  • Location: 0
  • Status: offline
Re:SPI and MMC 2011/01/31 17:00:35 (permalink)
0

By increasing MANUAL_SPI_CLOCK_VALUE significantly to 1,000 I can capture a series of pulses (see attached scope output).  These pulses are generated by the code =>
// Media requires 80 clock cycles to startup (8 clocks/BYTE * 10 us)
for (timeout=0; timeout<10; timeout++)
    WriteSPIManual(0xFF)
A similar burst of pulses occurs when the following line of code is called =>
response=SendMMCCmdManual (GO_IDLE_STATE, 0x0);
DI on the SD Card remains high throughout.

Is there anything that I should check in the oscillator / clock configuration?

My circuit is wired with an external 12MHz oscillator wired across pins 9 and 10 and I declare the following parameters:
#pragma config PLLDIV = 3
#pragma config CPUDIV = OSC1
#pragma config OSC = HSPLL

#define CLOCK_FREQ 48000000
#define GetSystemClock CLOCK_FREQ
#define GetInstructionClock CLOCK_FREQ
Thanks.

James.

Attached Image(s)

#40
Page: < 123 > Showing page 2 of 3
Jump to:
© 2019 APG vNext Commercial Version 4.5