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