►Reading and Writing a single sector

Another fundamental function is reading a single sector of data from the SD card. Since the sector size is fixed in SPI mode, we expect to read 512 bytes at a time. The following function will achieve this.

A. Reading Sector

BYTE SD_ReadSector( ULONG SectorNumber, BYTE *Buffer )
   BYTE c, i;
   WORD count;

   /* send block-read command... */
   SD_Command( CMD_READ_SINGLE_BLOCK, SectorNumber << 9 );
   c = SD_GetR1();
   i = SD_GetR1();
   count = 0xFFFF;
   /* wait for data token... */
   while( (i == 0xff) && --count)
      i = SD_GetR1();
   /* handle time out... */
   if(c || i != 0xFE)
      return( 1 );

   /* read the sector... */
   for( count=0; count<SD_DATA_SIZE; count++)       
      *Buffer++ = SPI_Byte(0xFF);
   /* ignore the checksum... */
   /* release the CS line... */

   return( 0 );

B. Writing Sector

BYTE SD_WriteSector( ULONG SectorNumber, BYTE *Buffer )
   BYTE i;
   WORD count;

   /* send block-write command... */
   SD_Command( CMD_WRITE_SINGLE_BLOCK, SectorNumber << 9 );
   i = SD_GetR1();

   /* send start block token... */
   SPI_Byte( 0xFE );

   /* write the sector... */
   for( count= 0; count< 512; count++ )
   /* ignore the checksum (dummy write)... */
   /* wait for response token... */
   while( SPI_Byte( 0xFF ) != 0xFF)
   /* these 8 clock cycles are critical for the card */
   /* to finish up whatever it's working on at the */
   /* time... (before CS is released!) */
   SPI_Byte( 0xFF );
   /* release the CS line... */
   SPI_Byte( 0xFF );
   return( 0 );

One final function

Finally, you will need one last function to emulate everything Chan’s library needs to talk to the SD card. This function is a simple flush function that is used to make sure the card is ready for the next command.

BYTE SD_WaitForReady()
   BYTE i;
   WORD j;

   SPI_Byte( 0xFF );
   j = 500;
      i = SPI_Byte( 0xFF );
      Delay( 1 );
   } while ((i != 0xFF) && --j);
   return( i );

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


Thu May 13 2021, 07:18 PM
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
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
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
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



Thu Jun 25 2020, 03:46 PM
Mon Jun 22 2020, 03:03 PM
Tue Jun 16 2020, 05:10 PM
Sat Jun 13 2020, 09:43 PM
Thu May 21 2020, 12:44 PM
Thu May 21 2020, 01:28 AM
Wed May 20 2020, 07:05 PM
Wed May 20 2020, 11:52 AM


Guests: 111, Members: 0 ...

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

Members: 38215
Newest member: Mirza123