mmc_sd.c

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

Generated on Wed Sep 23 09:17:02 2009 for ATMEL by  doxygen 1.5.3