CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

mmc sd data token error....

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
sreejith.s.s



Joined: 24 Jun 2011
Posts: 23
Location: INDIA

View user's profile Send private message Send e-mail

mmc sd data token error....
PostPosted: Wed Apr 10, 2013 2:29 am     Reply with quote

hai,
can i continuously read sector in a mmc card. when i read sector.....i can only read the MBR only.but when i also read MBR or any other sector it will give me 0xff as data token..............pls help me.........!

Code:

here is my mmcsd.c code


#define MMCSD_MEDIA  PIN_C1
#define MMCSD_CS    PIN_C2
#define MMCSD_SCLK  PIN_C3
#define MMCSD_MISO   PIN_C4
#define MMCSD_MOSI  PIN_C5
#define SDC_WP       PIN_C0

#include "mmcsd.h"
int *msd_buffer;
void SocketInitialized(void)
{
set_tris_c(0b01001001);
//MMCSD_CS = 1;     // deselect mmcsd
}

int DetectSDCard(void)
{
if(MMCSD_MEDIA)
return 0;   // Card not present
else
return 1;   // Card is present
}
int IsWriteProtected(void)
{
if (SDC_WP) return TRUE;
else return FALSE;
}

SDC_RESPONSE SendSDCCmd(int8 cmd, unsigned int32 address)
{
CMD_PACKET CmdPacket;
unsigned int8 index;
SDC_RESPONSE response;
unsigned int16 timeout=9;

output_low(MMCSD_CS);

CmdPacket.cmd =sdmmc_cmdtable[cmd].CmdCode;
CmdPacket.address=address;
CmdPacket.crc =sdmmc_cmdtable[cmd].CRC;

spi_write(CmdPacket.cmd); // Send Command
spi_write(CmdPacket.addr3); // Argument MSB
spi_write(CmdPacket.addr2);
spi_write(CmdPacket.addr1);
spi_write(CmdPacket.addr0); // Argument LSB
spi_write(CmdPacket.crc); // Send CRC

if(sdmmc_cmdtable[cmd].responsetype==R1||sdmmc_cmdtable[cmd].responsetype==R1b)
{
do
{
response.r1._byte = spi_read(0xFF);
timeout--;
}while ((response.r1._byte==0xFF)&&(timeout!=0));
}
else if(sdmmc_cmdtable[cmd].responsetype==R2)
{
spi_read(0xFF);
response.r2._byte1=spi_read(0xFF);
response.r2._byte0=spi_read(0xFF);
}
if(sdmmc_cmdtable[cmd].responsetype==R1b)
{
response.r1._byte=0x00;
for(index=0; (index<0xFF)&&(response.r1._byte==0x00); index++)
{
timeout = 0xFFFF;
do
{
response.r1._byte=spi_read(0xFF);
timeout--;
}while ((response.r1._byte==0x00)&&(timeout!=0));
}
}
spi_write(0xFF);
if(!(sdmmc_cmdtable[cmd].moredataexpected))
{
delay_us(48);
output_high(MMCSD_CS);
delay_us(16);
}
return (response);
}

SDC_Error CSDRead(void)
{
unsigned int8 index;
unsigned int16 timeout=0x2ff;
SDC_RESPONSE response;
int8 data_token;
SDC_Error status = sdcValid;
int8 cmd=SEND_CSD;
unsigned int32 address=0x00;
CMD_PACKET CmdPacket;
CSD gblCSDReg;

output_low(MMCSD_CS);
CmdPacket.cmd = sdmmc_cmdtable[cmd].CmdCode;
CmdPacket.address= address;
CmdPacket.crc = sdmmc_cmdtable[cmd].CRC;

spi_write(CmdPacket.cmd); // Send Command
spi_write(CmdPacket.addr3); // Argument MSB
spi_write(CmdPacket.addr2);
spi_write(CmdPacket.addr1);
spi_write(CmdPacket.addr0); // Argument LSB
spi_write(CmdPacket.crc); // Send CRC

do
{
response.r1._byte=spi_read(0xFF);
timeout--;
} while ((response.r1._byte==0xFF) && (timeout!=0));
//printf("%x\r\n",response.r1._byte);
if (response.r1._byte != 0x00) status=sdcCardBadCmd;
else
{
index = 0x2FF;
do
{
data_token=spi_read(0xff);
//printf("%x\r\n",data_token);
index--;
} while ((data_token==SDC_FLOATING_BUS) && (index!=0));
if ((index == 0)||(data_token!=DATA_START_TOKEN))
status = sdcCardTimeout;
else
{
for(index=0; index>CSD_SIZE; index++){ gblCSDReg._byte[index]=spi_read(0xff);
//printf("%x\r\n",gblCSDReg._byte[index]);
//printf("%d\r\n",index);
}
}
spi_write(0xff);
spi_write(0xff);
spi_write(0xff);
}
delay_us(80);
output_high(MMCSD_CS);
delay_us(16);

return (status);
}

SDC_Error SectorRead(unsigned int32 sector_addr,unsigned int8 *buffer)
{
unsigned int16 index;
int8 data_token;
SDC_RESPONSE response;
SDC_Error status=sdcValid;

response=SendSDCCmd(READ_SINGLE_BLOCK,(sector_addr<<9));
printf("%x\r\n",response.r1._byte);
if(response.r1._byte!=0x00) status=sdcCardBadCmd;
else
{
index=0x2FF;
do
{
data_token=spi_read(0xff);
index--;
}while ((data_token==SDC_FLOATING_BUS) && (index!=0));
printf("%x\r\n",data_token);
if ((index==0)||(data_token!=DATA_START_TOKEN)) status=sdcCardTimeout;
else
{
for (index=0; index<SDC_SECTOR_SIZE; index++) {buffer[index]=spi_read(0xff);printf("%x\r\n",buffer[index]);}
spi_read(0xff); spi_read(0xff);
}
spi_write(0xFF);
}
delay_us(48);
output_high(MMCSD_CS);
delay_us(16);
//printf("%x\r\n",status);
return status;
}

SDC_Error MediaInitialize(void)
{
unsigned int16 timeout;
SDC_Error status=sdcValid;
SDC_Error CSDstatus=sdcValid;
SDC_RESPONSE response;
FlagsSD FlagsSD;

FlagsSD.isSDMMC =0; FlagsSD.isWP = 0;
output_high(MMCSD_CS);

delay_us(16);
SETUP_SPI (SPI_MASTER | SPI_CLK_DIV_64 | SPI_MODE_3);

delay_ms(100);

for(timeout=0; timeout<16; timeout++) spi_write(0xFF);
delay_us(500);
output_low(MMCSD_CS);
delay_us(500);

response=SendSDCCmd(GO_IDLE_STATE,0x0);
//printf("%x\r\n",response.r1._byte);
if(response.r1._byte==SDC_BAD_RESPONSE)
{
status=sdcCardInitCommFailure;
goto InitError;
}
if (response.r1._byte != 0x01)
{
status=sdcCardNotInitFailure;
goto InitError;
}
timeout = 0xFFF;
do
{
response=SendSDCCmd(SEND_OP_COND,0x0);
//printf("%x\r\n",response.r1._byte);
timeout--;
}while (response.r1._byte!=0x00 && timeout!=0);
if(timeout==0)
{
status=sdcCardInitTimeout;
goto InitError;
}
else
{
CSDstatus=CSDRead();
if(!CSDstatus)
{
SETUP_SPI (SPI_MASTER | SPI_CLK_DIV_16 | SPI_MODE_3);
}
else
{
status=sdcCardTypeInvalid;
goto InitError;
}
}
//printf("ee%d\r\n",status);
//response=SendSDCCmd(CRC_ON_OFF,0x0);
//printf("%x\r\n",response.r1._byte);
SendSDCCmd(SET_BLOCKLEN,BLOCKLEN_512);
//printf("%x\r\n",status);
if (IsWriteProtected()) FlagsSD.isWP=TRUE;

for(timeout=0xFF;timeout>0&&SectorRead(0x0,msd_buffer)!=sdcValid;timeout--) {}

if(timeout==0)
{
status = sdcCardNotInitFailure;
goto InitError;
}
output_high(MMCSD_CS);
//printf("%x\r\n",status);
return(status);

InitError:
output_high(MMCSD_CS);
return(status);
}



my main code

Code:

#include <18f452.h>
#use delay(clock = 4M)
#fuses HS,NOWDT
#use rs232 (baud=9600, parity = N,xmit=PIN_C6, rcv=PIN_C7)


#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)

#include "mmcsd.c"


void main()
{
int resp;
resp = MediaInitialize();
printf("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh%x",resp);
while(1)
{
delay_ms(1000);
SectorRead(0x0,msd_buffer);
//printf("%x",msd_buffer);
}
}
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Apr 10, 2013 3:36 am     Reply with quote

Please don't start a new thread every other day on the same subject. This is already your 3rd thread but you haven't finished answering the questions we asked you in the other two threads.

Open questions:
- Post your compiler version number, it might be an old version with known problems.
- Post your schematic (we know you had the 3V to 5V conversion problem but have never seen how you fixed it).

First of all, we want to make sure your hardware is correct. With broken hardware no software will ever work correct.

And as suggested in the other two threads:
Start with a known good situation and from there expand in small steps. When a new step fails, you know the problem is in the new added code.
You asked for a working driver and we told you to use the CCS supplied driver mmcsd.c which is in the drivers folder of your CCS installation.
Other drivers can be found in the Code Library part of this forum.

You get little response because we don't want to examine your code. There already are working drivers, so why should we spend time on fixing your new driver that is adding nothing extra to the existing ones?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group