Micro SD Card programming


Hi this is srinat,


                  Ant body hlep me how to write read and how to read data from card to Host.

                 Here i am using the MSP430f2012 controller.


Thnks in advance,

srinath reddy.


  • There are lots of threads dealing with SD card access (micro SD card makes no differece) through SPI. Including pseudo-code and code snippets.
    Also, there is a TI applicaiton note about this (the threads are more advanced, btu the code that comes with this appnote has the basics)
    It only works up to 2GB size, however, since all cards with larger capacity have a different internal organization and usually do not support serial mode anymore.

  • In reply to Jens-Michael Gross:

    Hi Michael,


    Can you please send me any example codes for read from and write to Micro SD Card.

    It is useful to my project.


    Thanks and Regards

    Srinath Reddy



  • In reply to Andy Neil:



    SD card Block writing and Reading is happening but the problem is Reading the data sometimes it is showing error values.I dont know why it is happening.

    code for Block read is like this


                                sending the CMD24 command frame ( 48 bits )

                                Dummy byte

                                Response is 0x00

                                Dummy byte

                                reading  byte here i wait untill the data is 0xFE                  do

                                                                                                                                      {            Data = Spi_Rcv_Byte();

                                                                                                                                       }while ( Data  != 0xFE);

                               Dummy byte

                               After that Reading the 512 bytes.


              Here data is reading some time error data is reading so can u please tell me where i am doing the mistake.





  • In reply to srinath reddy:

    srinath reddy
    it is showing error values

    What, exactly, is showing error values?

    What error values, exactly, is it showing ?

    Have you looked-up what those error values mean ?


  • In reply to Andy Neil:

    Error Data is coming in different places like

    case 1)  suppose i am writing 0x79 to first block means first 512 bytes i am writing 0x79 data only.

    in reading it will read up to 26 bytes 0x79 after that it is reading 0xB3,0xB6,0xFF,0xFF...............up to 512 bytes it is reading 0xFF only

    case 2)  Writing 0x09 data to all 512 bytes.

                    Reading all the values 0x09 but in 32 byte location it is reading 0x01.

    case 3) Writing 0xDD to 512 bytes

                   Reading  up to 7 bytes is 0xDD and 8th and 9th bytes is 0xDC,0x09 after that again from 10th byte to 22nd byte it is reading properly (0xDD).23rd and 24th bytes is 0xC0,0x1D after that it is reading properly.


    Here i am not getting why this unexpected data values  are come into picture.Can you please tell me the reason and how to solve that problem.


  • In reply to srinath reddy:

    Hi srinath,

    You need to configure an USART port for syncronous operation (SPI) and then connect the card to this port.  May be you have a problem and you could need a pull-up resistor in this connection (you could observe this on the oscilloscope).  After this, try to send commands and read responses, the commands you can send to the card are specified in the memory datasheets. I can help you if you need more information.

    But before I could help you you need to start communicating with the card...

    Best regards



  • In reply to Arte8bits When technology becomes art:

    Hi ,

    Communication is happening properly means card initialization is ok.Writing also ok with proper responses.

    After that i send read command response is  0x00.After that i read the data until wait up to first data byte is 0xFE,This data also i am getting .

    After that reading 512 bytes of data here up to some locations reading properly after that garbage values is reading.


    This is the problem in my case.


  • In reply to srinath reddy:

    Hi srinath,

    Congratulations for your job! If you're communicating with the card, almost the half of your work is done.

    Garbage bytes... I remember I had the same problem, in my case it was because I was not working with the SD in protected mode, that means, using the CRCs.  So the problem appears because the SPI port is very sensible to interferences... I started to use the secure mode and the garbage bits dissappeared.

    Also I prevent you for another problem that may be you'll need to face.  When you're working writting blocks for long periods of time (several hours), the SD doesn't respond.  I found an application note of SanDisk in which they say that in these cases you need to restart the MMC... So you need to control Vcc of the SD card.

    I've just written in my blog a test about how to integrate SD cards in your electronic products, and I've included the functions prototype... Unfortunately it is in spanish but I hope it could be useful for you.  I'm ultimating the details of the next entry which will be about FAT file systems over the SD cards, and may be it could be also interesting for you.

    Best regards.



  • In reply to Arte8bits When technology becomes art:

    Arte8bits When technology becomes art
    I remember I had the same problem, in my case it was because I was not working with the SD in protected mode, that means, using the CRCs.

    The serial mode (SPI) is without CRC. the only command that requires a checksum is the intial command that enters the serial mode. It is identical for sertial and parallel mode and the CRC is always the same. After this point, all future commands still need a CRC byte but it is a don't care.

    Arte8bits When technology becomes art
    So the problem appears because the SPI port is very sensible to interferences.

    I cannot confirm this. None of my SD projects (three different, two with MSP) never had any problems with data loss. In fact, SPI is very stable and reliable.

    But there are other parameters which can cause instabilities. One of them is the supply voltage. During an SD card write, a peak current is drawn from Vcc. If Vcc isn't sufficiently buffered, it may happen that writing fails. However, most SD card implementations never check whether writing was completed successfully. It is just assumed. But SD card manufacturers are only humans too and the firmware in the card controller is not perfect, nor is the hardware.
    Another possible cause of problems is the clock speed. The original SD specs demand that the initialization must be done with <400kHz SPI clock speed. If the clock speed is higher, the card seems to work at first, but soem calibrations done at init ma have failed and further operation may be flakey.
    In my driver, I init the card with ~350kHz and then switch to up to 16MHz and it works fine.

    Using the secure mode of an SD card has severe drawbacks. And many card readers are not able to read the content. So it is only useful if you are the only one who writes and reads the card. But OTOH, in secur emode, the internal algorithms are different, so it might well be that this has caused the shift from flakey writes to reliable writes (hiding the real problem)

    You're right about the problem of not responding cards after some time. Many others have reported this too. Yet during my own tests, I initialized the card once only and it was logging data all day (and night) long, one sector every 15 seconds (one year storage per GB).
    So it seems to happen only after a certain time of continuously sleeping in standby. Accessing it every now and then seems to keep it alive.