Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] SD card interfacing, code init ok but not read write.

Status
Not open for further replies.

drbizzarow

Advanced Member level 1
Joined
May 24, 2006
Messages
414
Helped
25
Reputation
50
Reaction score
15
Trophy points
1,298
Activity points
3,662
Hi have done a code for 4gb sd card. which gives all ok status like after initialization of sd card, after sector write (512 bytes) and sector read (reading 512 bytes)command.although it gives ok status for all read command But on sector read sd card out puts always 0xFF. and nothing is saved on sd card.

here is my code, and after that i have copy the hex value i get during execution of my code. Please give your comments where i am doing wrong.



#include "reg_c51.h"
#include <intrins.h>
#include <stdio.h>
#include <string.h>
//////////////////////////////////
// PIN ASSINGMENTS //
//////////////////////////////////

//-------General Peraphirals
sbit SYS_LED = P0^4;
sbit MMC_SPIN = P1^2;
//-----MACROS
#define HIGH 1
#define LOW 0
#define SYS_LED_ON SYS_LED = LOW;
#define SYS_LED_OFF SYS_LED = HIGH;

#define SERIAL_PACKET_SIZE 20
#define BUFERS_SIZE SERIAL_PACKET_SIZE - 2
#define TIMER0_RELOAD_VALUE -0x2e //for 50ms time interval

#define MMC_SEL MMC_SPIN = 0;
#define MMC_DSEL MMC_SPIN = 1;

xdata char sector[512];

void serialTx(unsigned char chr);
//////////////////////////////////////////////
// GLOBAL VERIABLES DECLERATIONS //
//////////////////////////////////////////////
char SpiRcvDat;
char data_example=0x55;
char data_save;
bit transmit_completed= 0;
char x, rs232byt;
/*----------------------------------Interrupts--------------------------------------*/
/**
* FUNCTION_PURPOSE:interrupt
* FUNCTION_INPUTS: void
* FUNCTION_OUTPUTS: transmit_complete is software transfert flag
*/
void it_SPI(void) interrupt 9 /* interrupt address is 0x004B */
{
switch( SPSTA ) /* read and clear spi status register */
{
case 0x80:
SpiRcvDat=SPDAT; /* read receive data */
transmit_completed=1;/* set software flag */
break;
case 0x10:
serialTx('^'); /* put here for mode fault tasking */
break;
case 0x40:
serialTx('$');/* put here for overrun tasking */
break;
}
}

void serial_interrupt(void) interrupt 4 using 2 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~SERIAL ISR*/
{
if (RI)
{
rs232byt = SBUF;
RI = 0;
/*SPackRcv_FLAG = LOW;
SerialBuff[SerialBuffIndex] = SBUF;
RI = 0;

SerialBuffIndex++;
if(SerialBuffIndex >= SERIAL_PACKET_SIZE) SerialBuffIndex = 0; // incase of serial buffer overflow*/
}
}

/*------------------------------------------------------------------------------------*/
/*-------------------------------------UDF--------------------------------------------*/
/*------------------------------------------------------------------------------------*/
void delay(unsigned int times)
{
unsigned char i;
while(times-- > 0)
{
i = 255;
while(i-- > 0)
{
i = i;
}
}
}

/******************************************SPI Functions********************************/
void serialTx(unsigned char chr)
{
SBUF= chr;
while (!TI);
TI= 0;
}

char SpiTx(unsigned char txbyte)
{
SPDAT=txbyte; /* send data */
while(!(SPSTA & 0x80));/* wait end of transmition */
//serialTx(SPDAT);
return SPDAT;
}

void uart_puts (char *s) {
// loop until *s != NULL
while (*s) {
serialTx(*s);
s++;
}
}

/*char Command(char befF, unsigned int AdrH, unsigned int AdrL, char befH )
{ // sends a command to the MMC
SpiTx(0xFF);
SpiTx(befF);
SpiTx((char)(AdrH >> 8));
SpiTx((char)AdrH);
SpiTx((char)(AdrL >> 8));
SpiTx((char)AdrL);
SpiTx(befH);
SpiTx(0xFF);
return SpiTx(0xFF); // return the last received character
}*/
/*char Command(char befF, unsigned int AdrH, unsigned int AdrL, char befH )
{ // sends a command to the MMC
SpiTx(0xFF);
SpiTx(befF);
SpiTx((char)(AdrH >> 8));
SpiTx((char)AdrH);
SpiTx((char)(AdrL >> 8));
SpiTx((char)AdrL);
SpiTx(befH);
SpiTx(0xFF);
return SpiTx(0xFF); // return the last received character
}*/

char Command(char befF, unsigned long Adr, char befH )
{ // sends a command to the MMC.
_nop_();_nop_();
SpiTx(0xFF);
SpiTx(befF);
SpiTx((char)(Adr >> 24));
SpiTx((char)(Adr >> 16));
SpiTx((char)(Adr >> 8));
SpiTx((char)(Adr >> 0));
SpiTx(befH);
SpiTx(0xFF);
return SpiTx(0xFF); // return the last received character
_nop_();_nop_();
}

int MMC_Init(void) { // init SPI
char i;
MMC_DSEL
delay(1);
for(i=0; i < 10; i++) SpiTx(0xFF); // send 10*8=80 clock pulses
//PORTB &= ~(1 << SPICS); // enable MMC
MMC_SEL
if (Command(0x40,0x00000000,0x95) != 0x01) goto mmcerror; // reset MMC
MMC_DSEL
delay(1);
MMC_SEL
st: // if there is no MMC, prg. loops here
if (Command(0x41,0x00000000,0xFF) !=0x00)
{
serialTx('x');
goto st;
}
serialTx('1');
MMC_DSEL
return 1;
mmcerror:
serialTx('0');
MMC_DSEL
return 0;
}

void fillram(void) { // fill RAM sector with ASCII characters
int i,c;
char mystring[18] = "Captain was here! ";
c = 0;
for (i=0;i<=512;i++) {
sector = mystring[c];
c++;
if (c > 17) { c = 0; }
}
}

int writeramtommc(unsigned long sectoradd) { // write RAM sector to MMC
int i, retry=0;
char c;
MMC_SEL
// 512 byte-write-mode
if (Command(0x58,sectoradd,0xFF) !=0) {
serialTx('e');//uart_puts("MMC: write error 1 ");
return 1;
}
//SpiTx(0xFF);
SpiTx(0xFF);
SpiTx(0xFE);
// write ram sectors to MMC
for (i=0;i<512;i++) {
SpiTx(sector);
//delay(300);
}
// at the end, send 2 dummy bytes
SpiTx(0xFF);
SpiTx(0xFF);

c = SpiTx(0xFF);
c &= 0x1F; // 0x1F = 0b.0001.1111;
serialTx('+');
serialTx(c);
if (c != 0x05) { // 0x05 = 0b.0000.0101
uart_puts("w");uart_puts("e");//uart_puts("MMC: write error 2 ");
MMC_DSEL
return 1;
}
// wait until MMC is not busy anymore
while(SpiTx(0xFF) != (char)0xFF);

// MMC_DSEL
// SpiTx(0xff); //just spend 8 clock cycle delay before reasserting the CS line
// MMC_SEL //re-asserting the CS line to verify if card is still busy
// while(! SpiTx(0x00)) //wait for SD card to complete writing and get idle
// if(retry++ > 0xfffe){MMC_DSEL; return 1;}
// MMC_DSEL

serialTx('+');
MMC_DSEL
return 0;
}


int sendmmc(unsigned long sectoradd) { // send 512 bytes from the MMC via the serial port
int i, ix;
unsigned char r1 = 0;
serialTx('=');
MMC_SEL
r1 = Command(0x51,sectoradd,0xFF);
serialTx(r1);
for (ix = 0; ix < 50000; ix++) {
if (r1 == 0x00) break;
r1 = Command(0x51,sector,0xFF);
r1 = SpiTx(0xFF);
}

if (r1 != 0x00) {
serialTx('R');serialTx('e');//uart_puts("MMC: read error 1 ");
return 1;
}
serialTx(':');
// wait for 0xFE - start of any transmission
// ATT: typecast (char)0xFE is a must!
MMC_DSEL
MMC_SEL
r1 = SpiTx(0xff);
while(r1 != (char)0xFE){r1 = SpiTx(0x00);serialTx(r1);};
for(i=0; i < 512; i++) {
//while(!(UCSRA & (1 << UDRE))); // wait for serial port
//UDR = SpiTx(0xFF); // send character
serialTx(SpiTx(0x00));
}
// at the end, send 2 dummy bytes
SpiTx(0xFF); // actually this returns the CRC/checksum byte
SpiTx(0xFF);
serialTx('$');
MMC_DSEL
return 0;
}


int main(void)
{
unsigned long addx;
/*--------Bismillah-------*/

/* setup Serial interrupt */
TMOD = TMOD | 0x20; //TIMER1 IN MOD2 (AUTO RELOAD)
TH1 = -3; //BAUD RATE = 9600bps @ 11.0592MHz
TR1 = 1; //RUN TIMER1 FOR SERIAL CLOCK
SCON = 0x52; //8BIT, 1STOPBIT, NO PARITY
RI = 0;
TI = 0;
ES = 1; //ENABLE SERIAL INT
/* setup timer 0 interrupt */
// TR0 = 0;
// TH0 = 0X4B;
// TL0 = 0xFD; // set timer period
// TMOD = TMOD | 0x01; // select mode 1
// TR0 = 1; // start timer 0
/* Ext0 interrupt Settings */
//IT1 = 1; //ext0 neg edge trigger
/*SPI Settings And Interrupt */
SPCON = 0x00;
SPCON = 0x10; /* Master mode */
MMC_DSEL// P1_1=1; /* enable master */
// SPCON |= 0x01; /* Fclk Periph/128 */
SPCON |= 0x80;//SPCON &= ~0x08; /* CPOL=0; transmit mode example */
// SPCON |= 0x04; /* CPHA=1; transmit mode example */
//IEN1 |= 0x04; /* enable spi interrupt */
SPCON |= 0x40; /* run spi */
EA=1; /* enable interrupts */
P0 = 0xff;

SYS_LED = 1;
delay(300);
MMC_Init();
fillram();
addx = 0x00000200;
writeramtommc(addx);
delay(100);
sendmmc(addx);
while(1) /* endless */
{
addx += 0x00000200;
SYS_LED = ~SYS_LED;
delay(100);
}
}


/////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
my code send bytes recives on serial terminal


78 78 78 78 31 2B 05 2B 3D 00 3A FE 55 AA 55 AA 55 AB 01 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 24

- - - Updated - - -

hex vales in red are special charterers for i deliberately send these values for debugging purpose.
in green what i read always from my sd card.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top