Discussion in "Project Help" started by    prashants    Aug 4, 2007.
Sat Aug 04 2007, 08:15 pm
#1
hi ,
i want to interface SD card to ATmega 16/128 ,so i want help for how to interface the card & how i write the data to it , any file system is required for that card.
Sat Aug 04 2007, 08:47 pm
#2
you can use the AVRlib it got APIs to write and read in SD/MMC also IDE. you can download AVRlib from here...

AVRlib 2007-03-31

Documentation can be found here.. : Documentation


[ Edited Sat Aug 04 2007, 08:51 pm ]
Tags AVRlibC-Language Function Library for Atmel AVR ProcessoAVR Library
Thu Nov 01 2007, 09:32 am
#3
Hi, do you have similar files but instead for the 8051?
Thu Nov 01 2007, 12:56 pm
#4
no.. because only the recent S series of 8051 family (AT89S8252, AT89S8253) by atmel has SPI interface otherwise it was very rare in 8051 to find SPI on chip. So maybe in future someone will make it..

Here is a sample code for AVR actually.. but i will give a little info on how can u change it to 8051

/*************************************************************
 * MMC Init function
 *
 * - flushes card receive buffer
 * - selects card
 * - sends the reset command
 * - sends the initialization command, waits for card ready
 *************************************************************/
int MMCInit(void)
{
unsigned int i;
unsigned char Byte;

SPIDDR = SCLK + MOSI + CS + MMCPOWER;
SPIPORT = 0x00;
Delay_1ms(500);
SPIPORT |= MMCPOWER;
SPIPORT |= CS;
SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR1) | (1<<SPR0);    /* enable SPI as master, set clk divider */
Delay_1ms(250);

/* start off with 80 bits of high data with card deselected */
for(i=0;i<10;i++)
SpiByte(0xff);
SPIPORT &= ~CS;        /* select card */

/* now send CMD0 - go to idle state */
MMCCommand(0,0,0);
if (MMCGet() != 1)
   {
   SPIPORT |= CS;
   return -1;  // MMC Not detected
   }

/* send CMD1 until we get a 0 back, indicating card is done initializing */
i = 0xffff;
while ((SpiByte(0xff) != 0) && (--i))
     {
     MMCCommand(1,0,0);
     WDR();
     }
if (i == 0)
   {
   SPIPORT |= CS;
   return -2;  // Init Fail
   }

SPIPORT |= CS;
return 0;
}

/************************************************************
 * void MMCInfo(void)
 *
 * - gets and prints formatted CID and CSD info from card
 ************************************************************/
void MMCInfo(void)
{
int i;

MMCCommand(10,0,0);
if (MMCDataToken() != 0xfe) TxString("MMC: error during CID read\n\r\0");
else TxString("MMC: CID read\n\r\0");

// Skip 3 byte Manufacturer ID
SpiByte(0xff);
SpiByte(0xff);
SpiByte(0xff);

TxString("MMC: Product Name : ");
for (i=0;i<7;i++) TxChar(SpiByte(0xff));
TxString("\n\r\0");

for (i=0;i<9;i++) SpiByte(0xff); // Read 9 left byte

SPIPORT |= CS;
}

/************************************************************
 * int MMCReadSector(unsigned long lba, unsigned char * s)
 *
 * - reads a sector from the card (512 bytes)
 * - takes sector # as param
 ************************************************************/
int MMCReadSector(unsigned long lba, char *s)
{
unsigned int i;

MMCCommand(17,(lba>
>
7) & 0xffff, (lba<<9) & 0xffff);
if (MMCDataToken() != 0xfe)
   {
   SEI();
   return -1;
   }

for (i=0;i<512;i++)     /* read the sector */
   {
    *s++ = SpiByte(0xff);
   }
SpiByte(0xff);          /* checksum ->
 don't care about it for now */
SpiByte(0xff);       /* checksum ->
 don't care about it for now */
SPIPORT |= CS;
return 0;
}

/************************************************************
 * int MMCWriteSector(unsigned long lba, unsigned char * s)
 *
 * - reads a sector from the card (512 bytes)
 * - takes sector # as param
 ************************************************************/
int MMCWriteSector(unsigned long lba, char *s)
{
unsigned int i;

MMCCommand(24, (lba>
>
7)& 0xffff, (lba<<9)& 0xffff);
if (MMCGet() == 0xff) return -1;

SpiByte(0xfe);  // Send Start Byte

for (i=0;i<512;i++)       /* read the sector */
   {
    SpiByte(*s++);
   }
SpiByte(0xff);          /* checksum ->
 don't care about it for now */
SpiByte(0xff);       /* checksum ->
 don't care about it for now */
SpiByte(0xff);       /* Read "data response byte"                 */

i = 0xffff;
while ((SpiByte(0xff) == 0x00) && (--i)); /* wait for write finish */
if (i == 0) return -1; // Error

SPIPORT |= CS;
return 0;
}

/************************************************************
 * unsigned char MMCGet(void)
 *
 * - pings the card until it gets a non-0xff value
 * - returns one byte of read info
 ************************************************************/
unsigned char MMCGet(void)
{
unsigned int i = 0xffff;
unsigned char Byte = 0xff;

while((Byte == 0xff) && (--i)) Byte = SpiByte(0xff);
return Byte;
}

/************************************************************
 * int MMCDataToken(void)
 *
 * - pings the card until it gets data token
 * - returns one byte of read info (data token)
 ************************************************************/
unsigned char MMCDataToken(void)
{
unsigned int i = 0xffff;
unsigned char Byte = 0xff;

while((Byte != 0xfe) && (--i)) Byte = SpiByte(0xff);
return Byte;
}

/************************************************************
 * void MMCCommand(unsigned char command, unsigned int px, unsigned int py)
 *
 * - send one byte of 0xff, then issue command + params + (fake) crc
 * - eat up the one command of nothing after the CRC
 ************************************************************/
void MMCCommand(unsigned char command, unsigned int px, unsigned int py)
{
SPIPORT &= ~CS;
SpiByte(0xff);
SpiByte(command | 0x40);
SpiByte((unsigned char)((px >
>
 8)&0x0ff)); /* high byte of param y */
SpiByte((unsigned char)(px & 0x00ff));     /* low byte of param y */
SpiByte((unsigned char)((py >
>
 8)&0x0ff)); /* high byte of param x */
SpiByte((unsigned char)(py & 0x00ff));     /* low byte of param x */
SpiByte(0x95);            /* correct CRC for first command in SPI          */
                          /* after that CRC is ignored, so no problem with */
                          /* always sending 0x95                           */
SpiByte(0xff);
}
/*****************************************************
 * Main SPI routine
 *  - transmits a byte and receives a byte simultaneously
 *  - received byte is returned
 *  - if you only want to read a byte, put a dummy 
 *    (say 0xff) in the transmit slot
 ****************************************************/
unsigned char SpiByte(unsigned char byte)
{
WDR();
SPDR = byte;               /* put byte to send in SPDR, which initiates xmit  */
while(!(SPSR & (1<<SPIF)));/* wait for completion */
return SPDR;               /* return with byte shifted in from slave */
}


If you can write the SpiByte routine for 8051 then you can use the above code to read and write to MMC. further there might be many more changes needed.. that you need to find out.

For more reference take a look at these documents
SD Memory Card Physical Layer Specification, version 1.01 (pdf)
SanDisk SD Card Product Manual v1.9 (pdf)
SanDisk SD Card Product Manual v2.2 (pdf)
Simplified SDIO Card Specification, version 1 (pdf)

also...
How programm SD Card :
http://hdf.ncsa.uiuc.edu/training/HDFtraining/tutorial/sd/sds.html
Link about socket to SD Card :
http://www.sparkfun.com/shop/index.php?shop=1&itemid=330

Project soft and schematic of connecting PIC to SD Card.
http://www.captain.at/electronics/pic-mmc/

MSP430 interfacing to SD card doc from Ti :
http://focus.ti.com/general/docs/lit/getliterature.tsp?literatureNumber=slaa281&fileType=pdf
Fri Aug 01 2008, 01:03 pm
#5
what does MMCCommand(1,0,0) mean?? :blush
Fri Aug 01 2008, 08:37 pm
#6
i will upload Hitachi specifications for MMC and SD soon in download section. Do check it back soon. you will come to know everything.
Sat Aug 09 2008, 11:58 pm
#7
thanks sir
Sun Aug 10 2008, 12:15 am
#8
i have just checked the download section. May i know in which section you upload the file? Thanks
Mon Aug 11 2008, 11:40 am
#9
please check the download section
Mon Aug 11 2008, 01:49 pm
#10
oh yea, i want to ask. Is MMC same to SD??

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Bobbyerilar
Thu Mar 28 2024, 08:08 am
pb58
Thu Mar 28 2024, 05:54 am
Clarazkafup
Thu Mar 28 2024, 02:24 am
Walterkic
Thu Mar 28 2024, 01:19 am
Davidusawn
Wed Mar 27 2024, 08:30 pm
Richardsop
Tue Mar 26 2024, 10:33 pm
Stevencog
Tue Mar 26 2024, 04:26 pm
Bernardwarge
Tue Mar 26 2024, 11:15 am