This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

SPI and SD card

Hi everyone,
I found SD.c that works with SD card thorough SPI for LPC series. But I work with AT91SAM7x256 and I want to change the code to work with this series. I am working on USB and new to memories. This very important to me. I am so grateful for your help.

can anyone help me?

#include"lpc214x.h"
#include "PCF8833.h"

#define SD_Disable() IO0SET |= 1 << 20  //SD Chip Select Pin is P0.7
#define SD_Enable() IO0CLR |= 1 << 20
typedef unsigned char INT8U;
typedef unsigned short INT16U;
typedef unsigned int INT32U;

char CMD[]={0x40, 0x00, 0x00, 0x00, 0x00, 0x95};


INT8U SPI_RW(INT8U data)
{
        SSPDR = data;
        while((SSPSR & 0x01) == 0);
        return(SSPDR);
}

void spi_init (void)
{

   INT32U i;

   /* SSEL is GPIO, output set to high. */
   IODIR0  |= 1<<20;
   IOSET0   = 1<<20;
   /* SCK1, MISO1, MOSI1 are SSP pins. */
   PINSEL1  = (PINSEL1 & ~0x000003FC) | 0x000000A8;

   /* Enable SPI in Master Mode, CPOL=1, CPHA=1 (Clock low-active). */
   SSPCR0 = 0x00C7;
   SSPCR1 = 0x0002;
   SSPCPSR = 0xFE;

   /* Send SPI Command with card not selected at 400 KBit. */
   for (i = 0; i < 16; i++) {
      SPI_RW (0xFF);
   }

   /* Enable SSP auto select. */
}


Parents
  • SD_INIT()
    {
       INT8U retry, response;
       INT16U i;
       for(i=0;i<250;i++)
      __asm
      {
            NOP
      }
      for(i = 0; i < 0x0f; i++)
      {
            SPI_RW(0xff);
      }
      //Send Command CMD0 to SD/SD Card
      retry = 0;
      do
      { //retry 200 times to send CMD0 command
    
        response = Write_Command_SD(CMD);
        retry++;
      }
      while((response != 1) && (retry < 200));
    
       retry=0;
       do
       {
            CMD[0]=0x77;
            CMD[5]=0x55;
            Write_Command_SD(CMD);
            CMD[0]=0X69;
            response=Write_Command_SD(CMD);
                    retry++;
       }
       while((response!=0)&&(retry<200)) ;
    //  return(response);
    }
    
    
    
    void SD_WRITE(INT8U* Buffer,INT32U Number_Sector)
    {
       INT8U retry, response;
       retry=0;
      do
      {
          response=SD_write_sector(Number_Sector,Buffer);
              retry++;
       }
       while((response!=0x00)&&(retry<20))       ;
    }
    
    void SD_READ(INT8U *Buffer, INT16U Bytes,INT32U Number_Sector)
    {
       INT8U response,retry;
       INT8U CMD[]={0X51,0X00,0X00,0X00,0X00,0XFF};
       Number_Sector=Number_Sector<<9;
       CMD[1] = ((Number_Sector & 0xFF000000) >> 24 );
       CMD[2] = ((Number_Sector & 0x00FF0000) >> 16 );
       CMD[3] = ((Number_Sector & 0x0000FF00) >> 8 );
       CMD[4] = ((Number_Sector & 0x000000FF) >> 0) ;
       retry=0;
       do
       {
          response=SD_Read_Block(CMD,Buffer,Bytes);
              retry++;
       }
       while((response!=0)&&(retry<100));
    }
    

Reply
  • SD_INIT()
    {
       INT8U retry, response;
       INT16U i;
       for(i=0;i<250;i++)
      __asm
      {
            NOP
      }
      for(i = 0; i < 0x0f; i++)
      {
            SPI_RW(0xff);
      }
      //Send Command CMD0 to SD/SD Card
      retry = 0;
      do
      { //retry 200 times to send CMD0 command
    
        response = Write_Command_SD(CMD);
        retry++;
      }
      while((response != 1) && (retry < 200));
    
       retry=0;
       do
       {
            CMD[0]=0x77;
            CMD[5]=0x55;
            Write_Command_SD(CMD);
            CMD[0]=0X69;
            response=Write_Command_SD(CMD);
                    retry++;
       }
       while((response!=0)&&(retry<200)) ;
    //  return(response);
    }
    
    
    
    void SD_WRITE(INT8U* Buffer,INT32U Number_Sector)
    {
       INT8U retry, response;
       retry=0;
      do
      {
          response=SD_write_sector(Number_Sector,Buffer);
              retry++;
       }
       while((response!=0x00)&&(retry<20))       ;
    }
    
    void SD_READ(INT8U *Buffer, INT16U Bytes,INT32U Number_Sector)
    {
       INT8U response,retry;
       INT8U CMD[]={0X51,0X00,0X00,0X00,0X00,0XFF};
       Number_Sector=Number_Sector<<9;
       CMD[1] = ((Number_Sector & 0xFF000000) >> 24 );
       CMD[2] = ((Number_Sector & 0x00FF0000) >> 16 );
       CMD[3] = ((Number_Sector & 0x0000FF00) >> 8 );
       CMD[4] = ((Number_Sector & 0x000000FF) >> 0) ;
       retry=0;
       do
       {
          response=SD_Read_Block(CMD,Buffer,Bytes);
              retry++;
       }
       while((response!=0)&&(retry<100));
    }
    

Children