►Initializing SD/MMC card

A. Setting up the card for SPI Communication

Initializing the card, and setting it up for communication is the very first step. These are the most fundamental steps necessary before the card can be accessed. The CardType will be set as either a ‘0’, which is a MMC card, and cannot be used by the SPI interface, a ‘1’which is an SD card, version 1, or a ‘2’, which is an SD card, version 2

BYTE SD_Init()
{
   WORD CardStatus; // R2 value from status inquiry...
   WORD Count;      // local counter
   
   // Global CardType - b0:MMC, b1:SDv1, b2:SDv2

   /* initial speed is slow... */
   SPI_Init( kHz400 );
   
   /* disable SPI chip select... */
   SPI_DisableCS();

   /* fill send data with all ones - 80 bits long to   */
   /* establish link with SD card this fulfills the    */
   /* 74 clock cycle requirement...  */
   for(Count=0;Count<10;Count++)
      SPI_Byte( 0xFF );

   /* enable the card with the CS pin... */
   SPI_EnableCS();

   /* ************************************************ */
   /* SET SD CARD TO SPI MODE - IDLE STATE...          */
   /* ************************************************ */
   Count = 1000;     // one second...
   CardType = 0;
   
   /* wait for card to enter IDLE state... */
   do 
   {
      Delay(1);
      SD_Command( CMD_GO_IDLE_STATE, 0 );
   } while((SD_GetR1() != IDLE_STATE) && (--Count));  

   /* timeout if we never made it to IDLE state... */
   if( !Count )
      return( SD_TIME_OUT );

   /* ************************************************ */
   /* COMPLETE SD CARD INITIALIZATION                  */
   /* FIGURE OUT WHAT TYPE OF CARD IS INSTALLED...     */
   /* ************************************************ */
   Count = 2000;     // two seconds...
   
   /* Is card SDSC or MMC? */
   SD_Command( CMD_APP_CMD, 0 );
   SD_Command( ACMD_SEND_OP_COND, 0 );
   if( SD_GetR1() <= 1 )
   {
      CardType = 2;
   }
   else
   {
      CardType = 1;
   }
   
   /* wait for initialization to finish... */
   do
   {
      Delay(1);
      if( CardType == 2 )
      {
         SD_Command( CMD_APP_CMD, 0 );
         SD_Command( ACMD_SEND_OP_COND, 0 );
      }
      else
      {
         SD_Command( CMD_SEND_OP_COND, 0 );
      }
   } while(SD_GetR1() && --Count);

   if( !Count )
      return( SD_TIME_OUT );

   /* ************************************************ */
   /* QUERY CARD STATUS...                             */
   /* ************************************************ */
   SD_Command( CMD_SEND_STATUS, 0 );
   CardStatus = SD_GetR2();

   if( CardStatus )
      return( SD_ERROR );

   /* ************************************************ */
   /* SET BLOCK SIZE...                                */
   /* ************************************************ */
   SD_Command( CMD_SET_BLOCKLEN, 512 );
   if( SD_GetR1() )
   {
      CardType = 0;
      return( SD_ERROR );
   }

   /* ************************************************ */
   /* SWITCH TO HIGHEST SPI SPEED...                   */
   /* ************************************************ */
   SPI_Init( MHz10 );

   /* disable the card with the CS pin... */
   SPI_DisableCS();

   /* return OK... */
   return( 0 );
}



Tutorial Index
Interface to Chan’s Library of functions SD Card Initialization
Target development platform Reading and Writing a single sector
Setting up the SPI port during startup.A51 Working with diskio.c
Global type definitions and variables Pulling it all together
Basic SPI function Index Page

Conversation

Thu May 13 2021, 07:18 PM
Rufus
Pretty section of content. I just stumbled upon your weblog and in accession capital to assert that I get actually enjoyed account your blog posts. Any way I will be subscribing to your augment and even I achievement you access consistently quickly. https://mkfinstitute.com Trisha Trisha
Wed May 12 2021, 01:23 PM
Jed Roan
Now you can promote your site all over the internet for FREE: http://www3.listofsitesthatacceptfreeads.club
Wed May 12 2021, 08:20 AM
Edgardo
If some one wants to be updated with most recent technologies afterward he must be visit this web page and be up to date every day. annual credit report https://creditscoresetf.com
Wed May 12 2021, 06:11 AM
Laurinda
Great website. A lot of useful information here. I'm sending it to some pals ans also sharing in delicious. And of course, thanks on your effort! annual credit report https://creditscorecheckn.com
Wed May 12 2021, 05:37 AM
Joann
Does your site have a contact page? I'm having problems locating it but, I'd like to shoot you an email. I've got some recommendations for your blog you might be interested in hearing. Either way, great website and I look forward to seeing it improve over time. get credit report https://freecreditreportww.com

Downloads

Comments

Mirza123
Thu Jun 25 2020, 03:46 PM
eunicelove124
Mon Jun 22 2020, 03:03 PM
cerouno
Tue Jun 16 2020, 05:10 PM
Marce
Sat Jun 13 2020, 09:43 PM
Davidthils
Thu May 21 2020, 12:44 PM
Jakeror
Thu May 21 2020, 01:28 AM
motorCar
Wed May 20 2020, 07:05 PM
Vordrync
Wed May 20 2020, 11:52 AM

Online

Guests: 100, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 05:37 AM

Members: 38215
Newest member: Mirza123