mmc_sd.c

Go to the documentation of this file.
00001 /*This file has been prepared for Doxygen automatic documentation generation.*/
00013 
00014 /* Copyright (c) 2007, Atmel Corporation All rights reserved.
00015  *
00016  * Redistribution and use in source and binary forms, with or without
00017  * modification, are permitted provided that the following conditions are met:
00018  *
00019  * 1. Redistributions of source code must retain the above copyright notice,
00020  * this list of conditions and the following disclaimer.
00021  *
00022  * 2. Redistributions in binary form must reproduce the above copyright notice,
00023  * this list of conditions and the following disclaimer in the documentation
00024  * and/or other materials provided with the distribution.
00025  *
00026  * 3. The name of ATMEL may not be used to endorse or promote products derived
00027  * from this software without specific prior written permission.
00028  *
00029  * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
00030  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00031  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
00032  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
00033  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00034  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00035  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00036  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00037  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00038  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00039  */
00040 
00041 
00042 /*_____ I N C L U D E S ____________________________________________________*/
00043 #include "config.h"                         /* system configuration */
00044 #include "conf_sdmmc.h"
00045 #include "lib_mcu/usb/usb_drv.h"            /* usb driver definition */
00046 #include "lib_mcu/spi/spi_drv.h"            /* spi driver definition */
00047 #include "mmc_sd.h"                         /* MMC SD definition */
00048 
00049 
00050 /*_____ D E F I N I T I O N ________________________________________________*/
00051 
00052 static   U32   gl_ptr_mem;                   // memory data pointer
00053 
00054 bit  mmc_sd_init_done = FALSE;
00055 U8   r1;
00056 U16  r2;
00057 
00058           U8   csd[16];                     // stores the Card Specific Data
00059 volatile  U32  capacity;                    // stores the capacity in bytes
00060 volatile  U32  mmc_sd_last_block_address;   // stores the address of the last block (sector)
00061           U16  erase_group_size;            // stores the number of blocks concerned by an erase command
00062           U8   card_type;                   // stores SD_CARD or MMC_CARD type card
00063 
00064 
00065 #if       (MMC_SD_RAM == ENABLED)
00066           U8   data_mem[513]; // data buffer
00067 #endif
00068 #if      (MMC_SD_READ_CID == ENABLED)
00069           U8   cid[16];
00070 #endif
00071 
00072 
00073 /*_____ D E C L A R A T I O N ______________________________________________*/
00074 
00085 void mmc_sd_spi_init(void)
00086 {
00087    Spi_select_master();
00088    Spi_set_mode(SPI_MODE_0);
00089    Spi_init_bus();
00090    Spi_set_rate(SPI_RATE_0);  // SCK freq == fosc/2.
00091    Spi_disable_ss();
00092    Spi_enable();
00093 }
00094 
00095 
00107 bit mmc_sd_init (void)
00108 {
00109   U16 retry;
00110 
00111   // INIT HARDWARE
00112   mmc_sd_spi_init();
00113 
00114   // RESET THE MEMORY CARD
00115   mmc_sd_init_done = FALSE;
00116   card_type = MMC_CARD;
00117   retry = 0;
00118   do
00119   {
00120     // reset card and go to SPI mode
00121     r1 = mmc_sd_send_command(MMC_GO_IDLE_STATE, 0);
00122     Spi_write_data(0xFF);            // write dummy byte
00123     // do retry counter
00124     retry++;
00125     if(retry > 100)
00126       return KO;
00127   }
00128   while(r1 != 0x01);   // check memory enters idle_state
00129 
00130   // IDENTIFICATION OF THE CARD TYPE (SD or MMC)
00131   // Both cards will accept CMD55 command but only the SD card will respond to ACMD41
00132   r1 = mmc_sd_send_command(SD_APP_CMD55,0);
00133   Spi_write_data(0xFF);  // write dummy byte
00134 
00135   r1 = mmc_sd_send_command(SD_SEND_OP_COND_ACMD, 0);
00136   Spi_write_data(0xFF);  // write dummy byte
00137 
00138   if ((r1&0xFE) == 0)   // ignore "in_idle_state" flag bit
00139   {
00140     card_type = SD_CARD;    // card has accepted the command, this is a SD card
00141   }
00142   else
00143   {
00144     card_type = MMC_CARD;   // card has not responded, this is a MMC card
00145     // reset card again
00146     retry = 0;
00147     do
00148     {
00149       // reset card again
00150       r1 = mmc_sd_send_command(MMC_GO_IDLE_STATE, 0);
00151       Spi_write_data(0xFF);            // write dummy byte
00152       // do retry counter
00153       retry++;
00154       if(retry > 100)
00155         return KO;
00156     }
00157     while(r1 != 0x01);   // check memory enters idle_state
00158   }
00159 
00160   // CONTINUE INTERNAL INITIALIZATION OF THE CARD
00161   // Continue sending CMD1 while memory card is in idle state
00162   retry = 0;
00163   do
00164   {
00165      // initializing card for operation
00166      r1 = mmc_sd_send_command(MMC_SEND_OP_COND, 0);
00167      Spi_write_data(0xFF);            // write dummy byte
00168      // do retry counter
00169      retry++;
00170      if(retry == 50000)    // measured approx. 500 on several cards
00171         return KO;
00172   }
00173   while (r1);
00174 
00175   // DISABLE CRC TO SIMPLIFY AND SPEED UP COMMUNICATIONS
00176   r1 = mmc_sd_send_command(MMC_CRC_ON_OFF, 0);  // disable CRC (should be already initialized on SPI init)
00177   Spi_write_data(0xFF);            // write dummy byte
00178 
00179   // SET BLOCK LENGTH TO 512 BYTES
00180   r1 = mmc_sd_send_command(MMC_SET_BLOCKLEN, 512);
00181   Spi_write_data(0xFF);            // write dummy byte
00182   if (r1 != 0x00)
00183     return KO;    // card unsupported if block length of 512b is not accepted
00184 
00185   // GET CARD SPECIFIC DATA
00186   if (KO ==  mmc_sd_get_csd(csd))
00187     return KO;
00188 
00189   // GET CARD CAPACITY and NUMBER OF SECTORS
00190   mmc_sd_get_capacity();
00191 
00192   // GET CARD IDENTIFICATION DATA IF REQUIRED
00193 #if (MMC_SD_READ_CID == ENABLED)
00194   if (KO ==  mmc_sd_get_cid(cid))
00195     return KO;
00196 #endif
00197 
00198   mmc_sd_init_done = TRUE;
00199 
00200   return(OK);
00201 }
00202 
00203 
00216 
00217 U8 mmc_sd_send_command(U8 command, U32 arg)
00218 {
00219   Mmc_sd_select();                    // select MMC_SD
00220   r1 = mmc_sd_command(command, arg);
00221   Mmc_sd_unselect();                  // unselect MMC_SD
00222   return r1;
00223 }
00224 
00237 U8 mmc_sd_command(U8 command, U32 arg)
00238 {
00239 U8 retry;
00240 
00241   Spi_write_data(0xFF);            // write dummy byte
00242   Spi_write_data(command | 0x40);  // send command
00243   Spi_write_data(arg>>24);         // send parameter
00244   Spi_write_data(arg>>16);
00245   Spi_write_data(arg>>8 );
00246   Spi_write_data(arg    );
00247   Spi_write_data(0x95);            // correct CRC for first command in SPI (CMD0)
00248                                   // after, the CRC is ignored
00249   // end command
00250   // wait for response
00251   // if more than 8 retries, card has timed-out and return the received 0xFF
00252   retry = 0;
00253   r1    = 0xFF;
00254   while((r1 = mmc_sd_send_and_read(0xFF)) == 0xFF)
00255   {
00256     retry++;
00257     if(retry > 10) break;
00258   }
00259   return r1;
00260 }
00261 
00262 
00263 
00275 U8 mmc_sd_send_and_read(U8 data_to_send)
00276 {
00277    Spi_write_data(data_to_send);
00278    return (Spi_read_data());
00279 }
00280 
00281 
00282 
00293 bit mmc_sd_get_csd(U8 *buffer)
00294 {
00295 U8 retry;
00296 
00297   // wait for MMC not busy
00298   if (KO == mmc_sd_wait_not_busy())
00299     return KO;
00300 
00301   Mmc_sd_select();                  // select MMC_SD
00302   // issue command
00303   r1 = mmc_sd_command(MMC_SEND_CSD, 0);
00304   // check for valid response
00305   if(r1 != 0x00)
00306   {
00307     Mmc_sd_unselect();     // unselect MMC_SD
00308     mmc_sd_init_done = FALSE;
00309     return KO;
00310   }
00311   // wait for block start
00312   retry = 0;
00313   while((r1 = mmc_sd_send_and_read(0xFF)) != MMC_STARTBLOCK_READ)
00314   {
00315     if (retry > 8)
00316     {
00317       Mmc_sd_unselect();     // unselect MMC_SD
00318       return KO;
00319     }
00320     retry++;
00321   }
00322   for (retry = 0; retry <16; retry++)
00323   {
00324     Spi_write_data(0xFF);
00325     buffer[retry] = Spi_read_data();
00326   }
00327   Spi_write_data(0xFF);   // load CRC (not used)
00328   Spi_write_data(0xFF);
00329   Spi_write_data(0xFF);   // give clock again to end transaction
00330   Mmc_sd_unselect();     // unselect MMC_SD
00331   return OK;
00332 }
00333 
00334 
00335 
00346 bit mmc_sd_get_cid(U8 *buffer)
00347 {
00348 U8 retry;
00349 
00350   // wait for MMC not busy
00351   if (KO == mmc_sd_wait_not_busy())
00352     return KO;
00353 
00354   Mmc_sd_select();                  // select MMC_SD
00355   // issue command
00356   r1 = mmc_sd_command(MMC_SEND_CID, 0);
00357   // check for valid response
00358   if(r1 != 0x00)
00359   {
00360     Mmc_sd_unselect();     // unselect MMC_SD
00361     mmc_sd_init_done = FALSE;
00362     return KO;
00363   }
00364   // wait for data block start
00365   retry = 0;
00366   while((r2 = mmc_sd_send_and_read(0xFF)) != MMC_STARTBLOCK_READ)
00367   {
00368     if (retry > 8)
00369     {
00370       Mmc_sd_unselect();     // unselect MMC_SD
00371       return KO;
00372     }
00373     retry++;
00374   }
00375   // store valid data block
00376   for (retry = 0; retry <16; retry++)
00377   {
00378     Spi_write_data(0xFF);
00379     buffer[retry] = Spi_read_data();
00380   }
00381   Spi_write_data(0xFF);   // load CRC (not used)
00382   Spi_write_data(0xFF);
00383   Spi_write_data(0xFF);   // give clock again to end transaction
00384   Mmc_sd_unselect();     // unselect MMC_SD
00385   return OK;
00386 }
00387 
00388 
00389 
00421 void mmc_sd_get_capacity(void)
00422 {
00423   U16 c_size;
00424   U8  c_size_mult;
00425   U8  read_bl_len;
00426   U8  erase_grp_size;
00427   U8  erase_grp_mult;
00428 
00429   // extract variables from CSD array
00430   c_size      = ((csd[6] & 0x03) << 10) + (csd[7] << 2) + ((csd[8] & 0xC0) >> 6);
00431   c_size_mult = ((csd[9] & 0x03) << 1) + ((csd[10] & 0x80) >> 7);
00432   read_bl_len = csd[5] & 0x0F;
00433   if (card_type == MMC_CARD)
00434   {
00435     erase_grp_size = ((csd[10] & 0x7C) >> 2);
00436     erase_grp_mult = ((csd[10] & 0x03) << 3) | ((csd[11] & 0xE0) >> 5);
00437   }
00438   else
00439   {
00440     erase_grp_size = ((csd[10] & 0x3F) << 1) + ((csd[11] & 0x80) >> 7);
00441     erase_grp_mult = 0;
00442   }
00443 
00444   // compute last block addr
00445   mmc_sd_last_block_address = ((U32)(c_size + 1) * (U32)((1 << (c_size_mult + 2)))) - 1;
00446   if (read_bl_len > 9)  // 9 means 2^9 = 512b
00447     mmc_sd_last_block_address <<= (read_bl_len - 9);
00448 
00449   // compute card capacity in bytes
00450   capacity = (1 << read_bl_len) * (mmc_sd_last_block_address + 1);
00451 
00452   // compute block group size for erase operation
00453   erase_group_size = (erase_grp_size + 1) * (erase_grp_mult + 1);
00454 }
00455 
00456 
00457 
00469 bit     mmc_sd_get_status(void)
00470 {
00471   U8 retry, spireg;
00472 
00473   // wait for MMC not busy
00474   if (KO == mmc_sd_wait_not_busy())
00475     return KO;
00476 
00477   Mmc_sd_select();       // select MMC_SD
00478 
00479   // send command
00480   Spi_write_data(MMC_SEND_STATUS | 0x40);  // send command
00481   Spi_write_data(0);                       // send parameter
00482   Spi_write_data(0);
00483   Spi_write_data(0);
00484   Spi_write_data(0);
00485   Spi_write_data(0x95);            // correct CRC for first command in SPI (CMD0)
00486                                   // after, the CRC is ignored
00487   // end command
00488   // wait for response
00489   // if more than 8 retries, card has timed-out and return the received 0xFF
00490   retry = 0;
00491   r2 = 0xFFFF;
00492   spireg = 0xFF;
00493   while((spireg = mmc_sd_send_and_read(0xFF)) == 0xFF)
00494   {
00495     retry++;
00496     if(retry > 10)
00497     {
00498       Mmc_sd_unselect();
00499       return KO;
00500     }
00501   }
00502   r2 = ((U16)(spireg) << 8) + mmc_sd_send_and_read(0xFF);    // first byte is MSb
00503 
00504   Spi_write_data(0xFF);   // give clock again to end transaction
00505   Mmc_sd_unselect();     // unselect MMC_SD
00506 
00507   return OK;
00508 }
00509 
00510 
00521 bit mmc_sd_wait_not_busy(void)
00522 {
00523   U16 retry;
00524 
00525   // Select the MMC_SD memory gl_ptr_mem points to
00526   Mmc_sd_select();
00527   retry = 0;
00528   while((r1 = mmc_sd_send_and_read(0xFF)) != 0xFF)
00529   {
00530     retry++;
00531     if (retry == 50000)
00532     {
00533       Mmc_sd_unselect();
00534       return KO;
00535     }
00536   }
00537   Mmc_sd_unselect();
00538   return OK;
00539 }
00540 
00541 
00542 
00556 bit mmc_sd_check_presence(void)
00557 {
00558   U16 retry;
00559 
00560   retry = 0;
00561   if (mmc_sd_init_done == FALSE)
00562   {
00563     // If memory is not initialized, try to initialize it (CMD0)
00564     // If no valid response, there is no card
00565     while ((r1 = mmc_sd_send_command(MMC_GO_IDLE_STATE, 0)) != 0x01)
00566     {
00567       Spi_write_data(0xFF);            // write dummy byte
00568       retry++;
00569       if (retry > 10)
00570         return KO;
00571     }
00572     return OK;
00573   }
00574   else
00575   {
00576     // If memory already initialized, send a CRC command (CMD59) (supported only if card is initialized)
00577     /*
00578     retry = 0;
00579     while (retry != 50000)
00580     {
00581       r1 = mmc_sd_send_command(MMC_CMD2,0);   // unsupported command in SPI mode
00582       Spi_write_data(0xFF);            // write dummy byte
00583       if (r1 != 0)    // memory must answer with an error code (with bit7=0)
00584       {
00585         if (r1 < 0x80)
00586           return OK;
00587         else
00588         {
00589           mmc_sd_init_done = FALSE;
00590           return KO;
00591         }
00592       }
00593       retry++;
00594     }
00595     */
00596     if ((r1 = mmc_sd_send_command(MMC_CRC_ON_OFF,0)) == 0x00)
00597       return OK;
00598     mmc_sd_init_done = FALSE;
00599     return KO;
00600   }
00601 
00602   return KO;
00603 }
00604 
00605 
00618 bit mmc_sd_mem_check(void)
00619 {
00620   if (mmc_sd_check_presence() == OK)
00621   {
00622     if (mmc_sd_init_done == FALSE)
00623     {
00624       mmc_sd_init();
00625     }
00626     if (mmc_sd_init_done == TRUE)
00627       return OK;
00628     else
00629       return KO;
00630   }
00631   return KO;
00632 }
00633 
00634 
00635 
00650 bit is_mmc_sd_write_pwd_locked(void)
00651 {
00652   if (card_type == MMC_CARD)
00653   {
00654     if (((csd[0] >> 2) & 0x0F) < 2) // lock feature is not present on the card since the MMC is v1.x released !
00655       return KO;
00656   }
00657   if (KO == mmc_sd_get_status())    // get STATUS response
00658     return KO;
00659   if ((r2&0x0001) != 0)             // check "card is locked" flag in R2 response
00660     return OK;
00661 
00662   return KO;
00663 }
00664 
00665 
00695 bit mmc_sd_lock_operation(U8 operation, U8 pwd_lg, U8 * pwd)
00696 {
00697   bit status = OK;
00698   U8 retry;
00699 
00700   // check parameters validity
00701   if ((operation != OP_FORCED_ERASE) && (pwd_lg == 0))  // password length must be > 0
00702     return KO;
00703 
00704   // wait card not busy
00705   if (mmc_sd_wait_not_busy() == KO)
00706     return KO;
00707 
00708   // set block length
00709   if (operation == OP_FORCED_ERASE)
00710     r1 = mmc_sd_send_command(MMC_SET_BLOCKLEN, 1);   // CMD
00711   else
00712     r1 = mmc_sd_send_command(MMC_SET_BLOCKLEN, pwd_lg+2);   // CMD + PWDSLEN + PWD
00713   Spi_write_data(0xFF);            // write dummy byte
00714   Spi_write_data(0xFF);            // write dummy byte
00715   Spi_write_data(0xFF);            // write dummy byte
00716   if (r1 != 0x00)
00717     return KO;
00718 
00719   // send the lock command to the card
00720   Mmc_sd_select();                // select MMC_SD
00721 
00722   // issue command
00723   r1 = mmc_sd_command(MMC_LOCK_UNLOCK, 0);
00724 
00725   // check for valid response
00726   if(r1 != 0x00)
00727   {
00728     status = KO;
00729   }
00730   // send dummy
00731   Spi_write_data(0xFF);   // give clock again to end transaction
00732 
00733   // send data start token
00734   Spi_write_data(MMC_STARTBLOCK_WRITE);
00735   // write data
00736   Spi_write_data(operation);
00737   if (operation != OP_FORCED_ERASE)
00738   {
00739     Spi_write_data(pwd_lg);
00740     for(retry=0 ; retry<pwd_lg ; retry++)
00741     {
00742       Spi_write_data(*(pwd+retry));
00743     }
00744   }
00745   Spi_write_data(0xFF);    // send CRC (field required but value ignored)
00746   Spi_write_data(0xFF);
00747 
00748   // check data response token
00749   retry = 0;
00750   r1 = mmc_sd_send_and_read(0xFF);
00751   if ((r1 & MMC_DR_MASK) != MMC_DR_ACCEPT)
00752     status = KO;
00753 
00754   Spi_write_data(0xFF);    // dummy byte
00755   Mmc_sd_unselect();
00756 
00757   // wait card not busy
00758   if (operation == OP_FORCED_ERASE)
00759     retry = 100;
00760   else
00761     retry = 10;
00762   while (mmc_sd_wait_not_busy() == KO)
00763   {
00764     retry--;
00765     if (retry == 0)
00766     {
00767       status = KO;
00768       break;
00769     }
00770   }
00771 
00772   // get and check status of the operation
00773   if (KO == mmc_sd_get_status())    // get STATUS response
00774     status = KO;
00775   if ((r2&0x0002) != 0)   // check "lock/unlock cmd failed" flag in R2 response
00776     status = KO;
00777 
00778   // set original block length
00779   r1 = mmc_sd_send_command(MMC_SET_BLOCKLEN, 512);
00780   Spi_write_data(0xFF);            // write dummy byte
00781   if (r1 != 0x00)
00782     status = KO;
00783 
00784   return status;
00785 }
00786 
00787 
00788 
00799 bit mmc_sd_read_open (U32 pos)
00800 {
00801   // Set the global memory ptr at a Byte address.
00802   gl_ptr_mem = pos << 9;        // gl_ptr_mem = pos * 512
00803 
00804   // wait for MMC not busy
00805   return mmc_sd_wait_not_busy();
00806 }
00807 
00808 
00818 void mmc_sd_read_close (void)
00819 {
00820 
00821 }
00822 
00823 
00838 bit mmc_sd_write_open (U32 pos)
00839 {
00840   // Set the global memory ptr at a Byte address.
00841   gl_ptr_mem = pos << 9;                    // gl_ptr_mem = pos * 512
00842 
00843   // wait for MMC not busy
00844   return mmc_sd_wait_not_busy();
00845 }
00846 
00847 
00858 void mmc_sd_write_close (void)
00859 {
00860 
00861 }
00862 
00863 
00864 
00865 #if (MMC_SD_USB == ENABLE)
00866 
00891 bit mmc_sd_read_sector(U16 nb_sector)
00892 {
00893   Byte i;
00894   U16  read_time_out;
00895 
00896   do
00897   {
00898     Mmc_sd_select();                  // select MMC_SD
00899     // issue command
00900     r1 = mmc_sd_command(MMC_READ_SINGLE_BLOCK, gl_ptr_mem);
00901     // check for valid response
00902     if(r1 != 0x00)
00903     {
00904        Mmc_sd_unselect();                  // unselect MMC_SD
00905        return KO;
00906     }
00907 
00908     // wait for token (may be a datablock start token OR a data error token !)
00909     read_time_out = 30000;
00910     while((r1 = mmc_sd_send_and_read(0xFF)) == 0xFF)
00911     {
00912        read_time_out--;
00913        if (read_time_out == 0)   // TIME-OUT
00914        {
00915          Mmc_sd_unselect();               // unselect MMC_SD
00916          return KO;
00917        }
00918     }
00919 
00920     // check token
00921     if (r1 != MMC_STARTBLOCK_READ)
00922     {
00923       Spi_write_data(0xFF);
00924       Mmc_sd_unselect();                  // unselect MMC_SD
00925       return KO;
00926     }
00927 
00928     //#
00929     //# Read 8x64b = 512b, put them in the USB FIFO IN.
00930     //#
00931     for (i = 8; i != 0; i--)
00932     {
00933        Disable_interrupt();    // Global disable.
00934 
00935        // Principle: send any Byte to get a Byte.
00936        // Spi_write_data(0): send any Byte + 1st step to clear the SPIF bit.
00937        // Spi_read_data(): get the Byte + final step to clear the SPIF bit.
00938        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00939        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00940        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00941        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00942        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00943        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00944        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00945        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00946        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00947        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00948        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00949        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00950        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00951        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00952        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00953        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00954        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00955        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00956        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00957        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00958        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00959        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00960        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00961        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00962        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00963        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00964        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00965        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00966        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00967        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00968        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00969        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00970        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00971        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00972        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00973        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00974        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00975        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00976        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00977        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00978        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00979        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00980        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00981        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00982        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00983        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00984        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00985        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00986        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00987        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00988        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00989        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00990        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00991        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00992        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00993        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00994        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00995        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00996        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00997        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00998        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
00999        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
01000        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
01001        Spi_write_data(0xFF); Usb_write_byte(Spi_read_data());
01002        Enable_interrupt();     // Global re-enable.
01003 
01004        //#
01005        //# Send the USB FIFO IN content to the USB Host.
01006        //#
01007        Usb_send_in();       // Send the FIFO IN content to the USB Host.
01008        // Wait until the tx is done so that we may write to the FIFO IN again.
01009        while(Is_usb_write_enabled()==FALSE)
01010        {
01011           if(!Is_usb_endpoint_enabled())
01012              return KO; // USB Reset
01013        }
01014     } // for (i = 8; i != 0; i--)
01015 
01016     gl_ptr_mem += 512;     // Update the memory pointer.
01017     nb_sector--;           // 1 more sector read
01018     // read 16-bit CRC
01019     Spi_write_data(0xFF);
01020     Spi_write_data(0xFF);
01021     // dummy bytes
01022     Spi_write_data(0xFF);
01023     Spi_write_data(0xFF);
01024     // release chip select
01025     Mmc_sd_unselect();                  // unselect MMC_SD
01026   }
01027   while (nb_sector != 0);
01028 
01029   return OK;   // Read done.
01030 }
01031 
01032 
01056 bit mmc_sd_write_sector (U16 nb_sector)
01057 {
01058 U8 i;
01059 
01060   do
01061   {
01062     // wait card not busy
01063     i=0;
01064     while (KO == mmc_sd_wait_not_busy())
01065     {
01066       i++;
01067       if (i == 10)
01068         return KO;
01069     }
01070 
01071     Mmc_sd_select();                  // select MMC_SD
01072     // issue command
01073     r1 = mmc_sd_command(MMC_WRITE_BLOCK, gl_ptr_mem);
01074     // check for valid response
01075     if(r1 != 0x00)
01076     {
01077       Mmc_sd_unselect();                  // unselect MMC_SD
01078       return KO;
01079     }
01080     // send dummy
01081     Spi_write_data(0xFF);   // give clock again to end transaction
01082     // send data start token
01083     Spi_write_data(MMC_STARTBLOCK_WRITE);
01084       // write data
01085   //#
01086   //# Write 8x64b = 512b from the USB FIFO OUT.
01087   //#
01088     for (i = 8; i != 0; i--)
01089     {
01090       // Wait end of rx in USB EPOUT.
01091       while(!Is_usb_read_enabled())
01092       {
01093          if(!Is_usb_endpoint_enabled())
01094            return KO; // USB Reset
01095       }
01096 
01097       Disable_interrupt();    // Global disable.
01098 
01099       // SPI write principle: send a Byte then clear the SPIF flag.
01100       // Spi_write_data(Usb_read_byte()): (.) Final step to clear the SPIF bit,
01101       //                                  (.) send a Byte read from USB,
01102       //                                  (.) 1st step to clear the SPIF bit.
01103       Spi_write_data(Usb_read_byte());
01104       Spi_write_data(Usb_read_byte());
01105       Spi_write_data(Usb_read_byte());
01106       Spi_write_data(Usb_read_byte());
01107       Spi_write_data(Usb_read_byte());
01108       Spi_write_data(Usb_read_byte());
01109       Spi_write_data(Usb_read_byte());
01110       Spi_write_data(Usb_read_byte());
01111       Spi_write_data(Usb_read_byte());
01112       Spi_write_data(Usb_read_byte());
01113       Spi_write_data(Usb_read_byte());
01114       Spi_write_data(Usb_read_byte());
01115       Spi_write_data(Usb_read_byte());
01116       Spi_write_data(Usb_read_byte());
01117       Spi_write_data(Usb_read_byte());
01118       Spi_write_data(Usb_read_byte());
01119       Spi_write_data(Usb_read_byte());
01120       Spi_write_data(Usb_read_byte());
01121       Spi_write_data(Usb_read_byte());
01122       Spi_write_data(Usb_read_byte());
01123       Spi_write_data(Usb_read_byte());
01124       Spi_write_data(Usb_read_byte());
01125       Spi_write_data(Usb_read_byte());
01126       Spi_write_data(Usb_read_byte());
01127       Spi_write_data(Usb_read_byte());
01128       Spi_write_data(Usb_read_byte());
01129       Spi_write_data(Usb_read_byte());
01130       Spi_write_data(Usb_read_byte());
01131       Spi_write_data(Usb_read_byte());
01132       Spi_write_data(Usb_read_byte());
01133       Spi_write_data(Usb_read_byte());
01134       Spi_write_data(Usb_read_byte());
01135       Spi_write_data(Usb_read_byte());
01136       Spi_write_data(Usb_read_byte());
01137       Spi_write_data(Usb_read_byte());
01138       Spi_write_data(Usb_read_byte());
01139       Spi_write_data(Usb_read_byte());
01140       Spi_write_data(Usb_read_byte());
01141       Spi_write_data(Usb_read_byte());
01142       Spi_write_data(Usb_read_byte());
01143       Spi_write_data(Usb_read_byte());
01144       Spi_write_data(Usb_read_byte());
01145       Spi_write_data(Usb_read_byte());
01146       Spi_write_data(Usb_read_byte());
01147       Spi_write_data(Usb_read_byte());
01148       Spi_write_data(Usb_read_byte());
01149       Spi_write_data(Usb_read_byte());
01150       Spi_write_data(Usb_read_byte());
01151       Spi_write_data(Usb_read_byte());
01152       Spi_write_data(Usb_read_byte());
01153       Spi_write_data(Usb_read_byte());
01154       Spi_write_data(Usb_read_byte());
01155       Spi_write_data(Usb_read_byte());
01156       Spi_write_data(Usb_read_byte());
01157       Spi_write_data(Usb_read_byte());
01158       Spi_write_data(Usb_read_byte());
01159       Spi_write_data(Usb_read_byte());
01160       Spi_write_data(Usb_read_byte());
01161       Spi_write_data(Usb_read_byte());
01162       Spi_write_data(Usb_read_byte());
01163       Spi_write_data(Usb_read_byte());
01164       Spi_write_data(Usb_read_byte());
01165       Spi_write_data(Usb_read_byte());
01166       Spi_write_data(Usb_read_byte());
01167       Spi_ack_write();        // Final step to clear the SPIF bit.
01168 
01169       Usb_ack_receive_out();  // USB EPOUT read acknowledgement.
01170 
01171       Enable_interrupt();     // Global re-enable.
01172     } // for (i = 8; i != 0; i--)
01173 
01174     Spi_write_data(0xFF);    // send dummy CRC
01175     Spi_write_data(0xFF);
01176 
01177     // read data response token
01178     Spi_write_data(0xFF);
01179     r1 = Spi_read_data();
01180     if( (r1&MMC_DR_MASK) != MMC_DR_ACCEPT)
01181     {
01182       Mmc_sd_unselect();                  // unselect MMC_SD
01183       return r1;
01184     }
01185 
01186     // send dummy byte
01187     Spi_write_data(0xFF);
01188 
01189     // release chip select
01190     Mmc_sd_unselect();                  // unselect MMC_SD
01191     gl_ptr_mem += 512;        // Update the memory pointer.
01192     nb_sector--;              // 1 more sector written
01193   }
01194   while (nb_sector != 0);
01195 
01196   // wait card not busy after last programming operation
01197   i=0;
01198   while (KO == mmc_sd_wait_not_busy())
01199   {
01200     i++;
01201     if (i == 10)
01202       return KO;
01203   }
01204 
01205   return OK;                  // Write done
01206 }
01207 #endif      // (MMC_SD_USB == ENABLE)
01208 
01209 /*
01234 bit mmc_sd_host_write_sector (U16 nb_sector)
01235 {
01236 
01237   return OK;                  // Write done
01238 }
01239 */
01240 
01241 /*
01267 bit mmc_sd_host_read_sector (U16 nb_sector)
01268 {
01269 
01270   return OK;   // Read done.
01271 }
01272 */
01273 
01274 
01275 
01295 bit mmc_sd_erase_sector_group(U32 adr_start, U32 adr_end)
01296 {
01297   U8 cmd;
01298 
01299   // wait for MMC not busy
01300   if (KO == mmc_sd_wait_not_busy())
01301     return KO;
01302 
01303   Mmc_sd_select();          // select MMC_SD
01304 
01305   // send address of 1st group
01306   if (card_type == MMC_CARD)
01307   { cmd = MMC_TAG_ERASE_GROUP_START; }
01308   else
01309   { cmd = SD_TAG_WR_ERASE_GROUP_START; }
01310   if ((r1 = mmc_sd_command(cmd,(adr_start << 9))) != 0)
01311   {
01312     Mmc_sd_unselect();
01313     return KO;
01314   }
01315   Spi_write_data(0xFF);
01316 
01317   // send address of last group
01318   if (card_type == MMC_CARD)
01319   { cmd = MMC_TAG_ERASE_GROUP_END; }
01320   else
01321   { cmd = SD_TAG_WR_ERASE_GROUP_END; }
01322   if ((r1 = mmc_sd_command(cmd,(adr_end << 9))) != 0)
01323   {
01324     Mmc_sd_unselect();
01325     return KO;
01326   }
01327   Spi_write_data(0xFF);
01328 
01329   // send erase command
01330   if ((r1 = mmc_sd_command(MMC_ERASE,0)) != 0)
01331   {
01332     Mmc_sd_unselect();
01333     return KO;
01334   }
01335   Spi_write_data(0xFF);
01336 
01337   Mmc_sd_unselect();
01338 
01339   return OK;
01340 }
01341 
01342 
01343 
01344 #if (MMC_SD_RAM == ENABLED)
01345 
01360 bit mmc_sd_read_sector_to_ram(U8 *ram)
01361 {
01362   U16  i;
01363   U16  read_time_out;
01364 
01365   // wait for MMC not busy
01366   if (KO == mmc_sd_wait_not_busy())
01367     return KO;
01368 
01369   Mmc_sd_select();          // select MMC_SD
01370   // issue command
01371   r1 = mmc_sd_command(MMC_READ_SINGLE_BLOCK, gl_ptr_mem);
01372 
01373   // check for valid response
01374   if (r1 != 0x00)
01375   {
01376     Mmc_sd_unselect();     // unselect MMC_SD
01377     return KO;
01378   }
01379 
01380   // wait for token (may be a datablock start token OR a data error token !)
01381   read_time_out = 30000;
01382   while((r1 = mmc_sd_send_and_read(0xFF)) == 0xFF)
01383   {
01384      read_time_out--;
01385      if (read_time_out == 0)   // TIME-OUT
01386      {
01387        Mmc_sd_unselect();               // unselect MMC_SD
01388        return KO;
01389      }
01390   }
01391 
01392   // check token
01393   if (r1 != MMC_STARTBLOCK_READ)
01394   {
01395     Spi_write_data(0xFF);
01396     Mmc_sd_unselect();                  // unselect MMC_SD
01397     return KO;
01398   }
01399 
01400   // store datablock
01401   Disable_interrupt();    // Global disable.
01402   for(i=0;i<MMC_SECTOR_SIZE;i++)
01403   {
01404     Spi_write_data(0xFF);
01405     *ram=Spi_read_data();
01406     ram++;
01407   }
01408   Enable_interrupt();     // Global re-enable.
01409   gl_ptr_mem += 512;     // Update the memory pointer.
01410 
01411   // load 16-bit CRC (ignored)
01412   Spi_write_data(0xFF);
01413   Spi_write_data(0xFF);
01414 
01415   // continue delivering some clock cycles
01416   Spi_write_data(0xFF);
01417   Spi_write_data(0xFF);
01418 
01419   // release chip select
01420   Mmc_sd_unselect();                  // unselect MMC_SD
01421 
01422   return OK;   // Read done.
01423 }
01424 
01425 
01426 
01443 bit mmc_sd_write_sector_from_ram(U8 *ram)
01444 {
01445   U16 i;
01446 
01447   // wait for MMC not busy
01448   if (KO == mmc_sd_wait_not_busy())
01449     return KO;
01450 
01451   Mmc_sd_select();                  // select MMC_SD
01452   // issue command
01453   r1 = mmc_sd_command(MMC_WRITE_BLOCK, gl_ptr_mem);
01454   // check for valid response
01455   if(r1 != 0x00)
01456   {
01457     Mmc_sd_unselect();
01458     return KO;
01459   }
01460   // send dummy
01461   Spi_write_data(0xFF);   // give clock again to end transaction
01462 
01463   // send data start token
01464   Spi_write_data(MMC_STARTBLOCK_WRITE);
01465   // write data
01466   for(i=0;i<MMC_SECTOR_SIZE;i++)
01467   {
01468     Spi_write_data(*ram);
01469     ram++;
01470   }
01471 
01472   Spi_write_data(0xFF);    // send CRC (field required but value ignored)
01473   Spi_write_data(0xFF);
01474 
01475   // read data response token
01476   r1 = mmc_sd_send_and_read(0xFF);
01477   if( (r1&MMC_DR_MASK) != MMC_DR_ACCEPT)
01478   {
01479      Spi_write_data(0xFF);    // send dummy bytes
01480      Spi_write_data(0xFF);
01481      Mmc_sd_unselect();
01482      return KO;
01483 //     return r1;             // return ERROR byte
01484   }
01485 
01486   Spi_write_data(0xFF);    // send dummy bytes
01487   Spi_write_data(0xFF);
01488 
01489   // release chip select
01490   Mmc_sd_unselect();                  // unselect MMC_SD
01491   gl_ptr_mem += 512;        // Update the memory pointer.
01492 
01493   // wait card not busy after last programming operation
01494   i=0;
01495   while (KO == mmc_sd_wait_not_busy())
01496   {
01497     i++;
01498     if (i == 10)
01499       return KO;
01500   }
01501 
01502   return OK;                  // Write done
01503 }
01504 
01505 #endif      // (MMC_SD_RAM == ENABLE)
01506 

Generated on Fri Oct 31 14:31:24 2008 for ATMEL by  doxygen 1.5.3