Julian Pass

Problems with SPI and SD Card

Discussion created by Julian Pass on Feb 27, 2008
Latest reply on Feb 11, 2009 by Olivier Lessard Fontaine
Hi,
 
I have a problem with the SPI Bus which should communicate with a SD Card.
If I send for example a 9(0x09) to the SD Card i dont get an answer. But the SD Card should answer something.
 
I think that the problem is caused by my init() function. Maybe someone here can have a look on the code an help me.
 
void vfnSPI_Init(void)

 
    DDRS_DDRS4 = 0; //Miso
    DDRS_DDRS5 = 1; //Mosi
    DDRS_DDRS6 = 1; //Sck
    DDRS_DDRS7 = 1; //CS
     
     
    // Chip Select auf High
    PTS_PTS7 =0;
   
    SPI0CR1_SPE = 1;    /* SPI module enabled */ 
    // SD - Karte an SPI0                
    SPI0CR2 = 0x00;
       
    (void)SPI0SR;       /* Read the status register and */
    (void)SPI0DR;       /* read the data register to clear the status flags*/
   
    /* Desired SPI baud rate = 12kHz   */   
    SPI0BR_SPPR = 0x07; 
    SPI0BR_SPR = 0x07;                                                            
   
    SPI0CR2_MODFEN = 1; /* Slave select output pin used by SPI module */ 
    SPI0CR1_CPHA = 1;   /* Data sampling at falling edges */
    SPI0CR1_SSOE = 1;   /* Slave select output enable */
    SPI0CR1_MSTR = 1;   /* SPI in Master mode */
   
    SPI0CR1_SPE = 1;    /* SPI module enabled */

    /* Desired SPI baud rate = 4MHz, baud rate divider = (40MHz/2.5MHz) = 16   */   
    SPI0BR_SPPR = 0x03;  /* Baud rate divider = (SPPR+1)*2^(SPR+1) = (4)*(2^2) */
    SPI0BR_SPR = 0x01;
   */
    
  }
 
void vfnSPI_Send_Byte(unsigned char u8DataByte)

   
    while (!SPI0SR_SPTEF)   /* Wait for empty data register */  
        ;
   
    SPI0DR = u8DataByte;    /* Load byte in data transmission register */
}
 
unsigned char u8SPI_Receive_Byte(void)
{
    while (SPI0SR_SPIF == 0)//(SPI0SR & SPI0SR_SPIF_MASK))  /* Wait for data in the receive buffer */
        ; 
                       
    return SPI0DR;     /* Return received data */
}
 

Outcomes