SD card interfacing with Q2686rd GSM module using SPI

hi sir,

Iam using Q2686RD Gsm module. iam interfacing SD card with Gsm module with SPI Protocal but write command and read commands not success for trying so many times.
the commands for write and read (24-write ,17-read)

can any one explain what is the problem behind this…
thank you in advance.

Hiya,

Show us some of your code. (and please use the ‘Code’ tags to format it nicely).

Ta, Dave

Have you verified that your hardware is working correctly - eg, using an oscilloscope?

Have you gone through the proper initialisation sequence to get the card into SPI mode?

It can be done:

antronics.co.uk/portfolio/gpstrack/

yes sir … i verified with oscilloscope sd card is working is properly and also i checked the spi bus initialization also correct.

some part of my code is

#include “adl_global.h”
#include “Sdcard.h”

unsigned char dummybyte[5]={0xFF,0xFF},response=0;
unsigned char buffer[512]=“I have the exact same SD-card, marked wit.I have another card that looks exactly the same apart from the number following SDC/2GB that works fine. I even ordered them together. Both work in an USB-reader that I havthese are uSD-cards SPI is optional, so for all I know the one that works has SPI while the other one doesn’t. I’m unfamiliar with the SD-protocol, so I wouldn’t know exactly when the SPI method fails.What kind of card are you using? If it’s not uSD then the problem must be something else.”;
char buffer2[600]={0};
int sdcard_Init(void)
{
unsigned char v=0;
Chipselect_Status(1);
for(v = 0; v < 20; v++)
{
adl_busWrite(HandleSPI,&AccessConfig,1,dummybyte);
}
Chipselect_Status(0);

 v = sdCommandAndResponse(0,1,0);  //SEND CMD0
 if(v == 1)
 {
	 TRACE (( 6, "CMD0 Success response is %d",v ));
	 adl_atSendResponse(ADL_AT_UNS,"CMD0 Success\r\n");
 }
 else
 {
	 TRACE (( 6, "CMD0 failed response is %d",v ));
	 adl_atSendResponse(ADL_AT_UNS,"CMD0 failed \r\n");
 }

 v = sdCommandAndResponse(8,1,0x000001AA);  //SEND CMD8  with 0x000001AA parameter
 if(v == 1)
 {
	 TRACE (( 6, "CMD8 Success response is %d",v ));
	 adl_atSendResponse(ADL_AT_UNS,"CMD8 Success \r\n");
 }
 else
 {
     TRACE (( 6, "CMD8 failed response is %d",v ));
     adl_atSendResponse(ADL_AT_UNS,"CMD8 failed  \r\n");
 }

 v = App_sdCommandAndResponse(41,0,0x40000000);  //SEND CMD41
 if(v == 0)
 {
	 TRACE (( 6, "ACMD41 Success response is %d",v ));
	 adl_atSendResponse(ADL_AT_UNS,"ACMD41 Success  \r\n");
 }
 else
 {
 	 TRACE (( 6, "ACMD41 failed response is %d",v ));
 	 adl_atSendResponse(ADL_AT_UNS,"ACMD41 failed  \r\n");
 }

return true;

}

void sdSendCommand(unsigned char cmd, unsigned long param)
{
unsigned char send[10]={0};
adl_busWrite(HandleSPI,&AccessConfig,1,dummybyte);
send[0] = cmd | 0x40;
send[1] = param >> 24;
send[2] = param >> 16;
send[3] = param >> 8;
send[4] = param;
if(cmd==0 || cmd==8)
send[5] = (sdCrc7(send, 5, 0) << 1) | 1;
else
send[5]=0xFF;
adl_busWrite(HandleSPI,&AccessConfig,6,send);
adl_busWrite(HandleSPI,&AccessConfig,1,dummybyte);
}

unsigned char sdCommandAndResponse(unsigned char cmd, unsigned char resp, unsigned long param)
{
unsigned char v=0, i = 0;
do
{
v=0;
Chipselect_Status(0);
sdSendCommand(cmd, param);
adl_busRead (HandleSPI, &AccessConfig, 1, &v );
Chipselect_Status(1);
adl_busWrite(HandleSPI,&AccessConfig,2,dummybyte);
}while((i++ < 255) && (v != resp));
return v;
}

unsigned char App_sdCommandAndResponse(unsigned char cmd, unsigned char resp, unsigned long param)
{
unsigned char v=0, i=0;
do
{
v=0;
Chipselect_Status(0);
sdSendCommand(55, 0);
Chipselect_Status(1);
adl_busWrite(HandleSPI,&AccessConfig,2,dummybyte);

	 Chipselect_Status(0);
	 sdSendCommand(cmd, param);
	 Chipselect_Status(1);
	 adl_busRead (HandleSPI, &AccessConfig, 1, &v );
	 adl_busWrite(HandleSPI,&AccessConfig,2,dummybyte);
 }while((i++ < 255) && (v != resp));
 return v;

}

unsigned char sdCrc7(unsigned char* chr,unsigned char cnt,unsigned char crc)
{
unsigned char i, a;
unsigned char Data;
for(a = 0; a < cnt; a++)
{
Data = chr[a];
for(i = 0; i < 8; i++)
{
crc <<= 1;
if( (Data & 0x80) ^ (crc & 0x80) ) {crc ^= 0x09;}
Data <<= 1;
}
}
return crc & 0x7F;
}

unsigned char startwritecmnd=0xFE;
unsigned char SD_writeSingleBlock(unsigned long startBlock,unsigned char Wbuff[])
{
unsigned char v=0;
char buf[50]={0};
unsigned int count=0;
adl_atSendResponse(ADL_AT_UNS,“SDcard writing SingleBlock\r\n”);
v = sdCommandAndResponse(24,0,startBlock<<9); //SEND CMD24 write single block
if(v == 0)
{
TRACE (( 6, “CMD24 Success response is %d”,v ));
adl_atSendResponse(ADL_AT_UNS,“CMD24 Success\r\n”);
}
else
{
TRACE (( 6, “CMD24 failed response is %d”,v ));
adl_atSendResponse(ADL_AT_UNS,“CMD24 failed \r\n”);
}

Chipselect_Status(0);
adl_busWrite(HandleSPI,&AccessConfig,1,&startwritecmnd);   //start writing into sector
adl_busWrite(HandleSPI,&AccessConfig,500,Wbuff);
Chipselect_Status(1);

/* for(i=0; i<512; i++) //write 512 bytes data
{
adl_busWrite(HandleSPI,&AccessConfig,1,&Wbuff[i]);
}
*/ adl_busWrite(HandleSPI,&AccessConfig,2,dummybyte);
// adl_busWrite(HandleSPI,&AccessConfig,2,dummybyte);

adl_busRead (HandleSPI, &AccessConfig, 1, &response );
if( (response & 0x1f) != 0x05) //response=0xXXX0AAA1 ; AAA='010' - data accepted
{                              //AAA='101'-data rejected due to CRC error
	Chipselect_Status(1);     //AAA='110'-data rejected due to write error
	sprintf(buf,"ret resp is not 0x05, resp is %.2X\r\n",response);
	adl_atSendResponse(ADL_AT_UNS,buf);
	return response;
}

// Chipselect_Status(1);
adl_busWrite(HandleSPI,&AccessConfig,2,dummybyte);
// Chipselect_Status(0);
while(response!=0xfe)
{
adl_busRead (HandleSPI, &AccessConfig, 1, &response );
if(count++>0xfffe)
{
Chipselect_Status(1);
response=0;
adl_atSendResponse(ADL_AT_UNS,“in writing 0xFE failed \r\n”);
return 0;
}
}
// Chipselect_Status(1);
return 0;
}

unsigned char SD_readSingleBlock(unsigned long startBlock)
{
unsigned char v=0,count=0;
adl_atSendResponse(ADL_AT_UNS,“SDcard reading SingleBlock\r\n”);
v = sdCommandAndResponse(17,0,startBlock<<9); //SEND CMD17 read single block
if(v == 0)
{
TRACE (( 6, “CMD17 Success response is %d”,v ));
adl_atSendResponse(ADL_AT_UNS,“CMD17 Success \r\n”);
}
else
{
TRACE (( 6, “CMD17 failed response is %d”,v ));
adl_atSendResponse(ADL_AT_UNS,“CMD17 failed \r\n”);
}
Chipselect_Status(0);
while(response!=0xfe)
{
adl_busRead (HandleSPI, &AccessConfig, 1, &response );
if(count++>0xfe)
{
Chipselect_Status(1);
adl_atSendResponse(ADL_AT_UNS,“in reading 0xFE failed \r\n”);
return 0;
}
}

adl_busRead (HandleSPI, &AccessConfig, 500, buffer2 );
Chipselect_Status(1);
TRACE (( 6, buffer2));
adl_atSendResponse(ADL_AT_UNS,buffer2);
return 0;

}

Hiya,

It makes it easier to read…

Ta.