mmc_sd.h

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 
00055 
00056 #ifndef _MMC_SD_H_
00057 #define _MMC_SD_H_
00058 
00059 /*_____ I N C L U D E S ____________________________________________________*/
00060 
00061 #include "config.h"
00062 
00063 
00064 /*_____ M A C R O S ________________________________________________________*/
00065 
00066 #ifndef MMC_SECTOR_SIZE
00067 #define MMC_SECTOR_SIZE   512   //default sector size is 512 bytes
00068 #endif
00069 
00070 #define byte_adr_of_block(ad)   (ad<<9)   // returns the first byte address of a specified sector/block number (512bytes/block)
00071 
00072 /*_____ D E F I N I T I O N ________________________________________________*/
00073 
00074 // Card identification
00075 #define MMC_CARD                                0
00076 #define SD_CARD                                 1
00077 
00078 // Lock operations
00079 #define OP_UNLOCK                               0x00
00080 #define OP_LOCK                                 0x04
00081 #define OP_RESET_PWD                            0x02
00082 #define OP_SET_PWD                              0x01
00083 #define OP_FORCED_ERASE                         0x08
00084 
00085 // MMC commands (taken from sandisk MMC reference)
00086 #define MMC_GO_IDLE_STATE         0      
00087 #define MMC_SEND_OP_COND         1      
00088 #define MMC_CMD2                                2               
00089 #define MMC_SEND_CSD            9      
00090 #define MMC_SEND_CID            10      
00091 #define MMC_SEND_STATUS            13
00092 #define MMC_SET_BLOCKLEN         16      
00093 #define MMC_READ_SINGLE_BLOCK              17      
00094 #define MMC_WRITE_BLOCK            24      
00095 #define MMC_PROGRAM_CSD            27
00096 #define MMC_SET_WRITE_PROT         28
00097 #define MMC_CLR_WRITE_PROT         29
00098 #define MMC_SEND_WRITE_PROT         30
00099 #define SD_TAG_WR_ERASE_GROUP_START             32
00100 #define SD_TAG_WR_ERASE_GROUP_END               33
00101 #define MMC_TAG_SECTOR_START              32
00102 #define MMC_TAG_SECTOR_END         33
00103 #define MMC_UNTAG_SECTOR         34
00104 #define MMC_TAG_ERASE_GROUP_START            35      
00105 #define MMC_TAG_ERASE_GROUP_END              36      
00106 #define MMC_UNTAG_ERASE_GROUP              37      
00107 #define MMC_ERASE            38      
00108 #define SD_SEND_OP_COND_ACMD                    41              
00109 #define MMC_LOCK_UNLOCK                         42              
00110 #define SD_APP_CMD55                            55              
00111 #define MMC_CRC_ON_OFF            59      
00112 // R1 Response bit-defines
00113 #define MMC_R1_BUSY            0x80   
00114 #define MMC_R1_PARAMETER         0x40
00115 #define MMC_R1_ADDRESS            0x20
00116 #define MMC_R1_ERASE_SEQ         0x10
00117 #define MMC_R1_COM_CRC            0x08
00118 #define MMC_R1_ILLEGAL_COM         0x04
00119 #define MMC_R1_ERASE_RESET         0x02
00120 #define MMC_R1_IDLE_STATE         0x01
00121 // Data Start tokens
00122 #define MMC_STARTBLOCK_READ         0xFE   
00123 #define MMC_STARTBLOCK_WRITE              0xFE   
00124 #define MMC_STARTBLOCK_MWRITE              0xFC
00125 // Data Stop tokens
00126 #define MMC_STOPTRAN_WRITE         0xFD
00127 // Data Error Token values
00128 #define MMC_DE_MASK            0x1F
00129 #define MMC_DE_ERROR            0x01
00130 #define MMC_DE_CC_ERROR            0x02
00131 #define MMC_DE_ECC_FAIL            0x04
00132 #define MMC_DE_OUT_OF_RANGE         0x04
00133 #define MMC_DE_CARD_LOCKED         0x04
00134 // Data Response Token values
00135 #define MMC_DR_MASK            0x1F
00136 #define MMC_DR_ACCEPT            0x05
00137 #define MMC_DR_REJECT_CRC         0x0B
00138 #define MMC_DR_REJECT_WRITE_ERROR               0x0D
00139 
00140 
00141 
00142 /*_____ D E C L A R A T I O N ______________________________________________*/
00143 
00145 void    mmc_sd_spi_init(void);          // initializes the SPI bus and settings for SD/MMD card
00146 bit     mmc_sd_init (void);             // initializes the SD/MMC card (reset, init, analyse)
00147 bit     mmc_sd_check_presence(void);    // check the presence of the card
00148 bit     mmc_sd_mem_check(void);         // check the presence of the card, and initialize if inserted
00149 bit     mmc_sd_wait_not_busy (void);    // wait for the card to be not busy (exits with timeout)
00150 bit     mmc_sd_get_csd(U8 *);           // stores the CSD of the card into csd[16]
00151 bit     mmc_sd_get_cid(U8 *);           // stores the CID of the card into cid[16]
00152 void    mmc_sd_get_capacity(void);      // extract parameters from CSD and compute capacity, last block adress, erase group size
00153 bit     mmc_sd_get_status(void);        // read the status register of the card (R2 response)
00154 U8      mmc_sd_send_and_read(U8);       // send a byte on SPI and returns the received byte
00155 U8      mmc_sd_send_command(U8, U32);   // send a single command + argument (R1 response expected and returned), with memory select then unselect
00156 U8      mmc_sd_command(U8, U32);        // send a command + argument (R1 response expected and returned), without memory selct/unselect
00157 
00159 bit     is_mmc_sd_write_pwd_locked(void);     // check if the lock protection on the card is featured and enabled
00160 bit     mmc_sd_lock_operation(U8, U8, U8 *);  // use this function to lock/unlock the card or modify password
00161 
00163 bit     mmc_sd_read_open (U32);         // to call before first access to a random page
00164 void    mmc_sd_read_close (void);
00165 bit     mmc_sd_write_open (U32);        // to call before first access to a random page
00166 void    mmc_sd_write_close (void);
00167 
00169 bit     mmc_sd_write_sector (U16);      // write a 512b sector from USB buffer
00170 bit     mmc_sd_read_sector (U16);       // reads a 512b sector to an USB buffer
00171 
00172 /*
00174 bit     mmc_sd_host_write_sector (U16);
00175 bit     mmc_sd_host_read_sector (U16);
00176 */
00177 
00179 bit     mmc_sd_read_sector_to_ram(U8 *ram);     // reads a data block and send it to a buffer (512b)
00180 bit     mmc_sd_write_sector_from_ram(U8 *ram);  // writes a data block from a buffer (512b)
00181 bit     mmc_sd_erase_sector_group(U32, U32);    // erase a group of sectors defined by start and end address (details in mmc_sd.c)
00182 
00183 
00184 extern U8            csd[16];                    // stores the Card Specific Data
00185 extern volatile U32  capacity;                   // stores the capacity in bytes
00186 extern volatile U32  mmc_sd_last_block_address;
00187 extern  U16 erase_group_size;
00188 extern  U8  r1;
00189 extern  U16 r2;
00190 extern  U8   card_type;                   // stores SD_CARD or MMC_CARD type card
00191 extern  bit  mmc_sd_init_done;
00192 
00193 #endif  /* _MMC_SD_H_ */
00194 
00195 

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