00001 /*This file is prepared for Doxygen automatic documentation generation.*/ 00015 /* Copyright (c) 2009 Atmel Corporation. All rights reserved. 00016 * 00017 * Redistribution and use in source and binary forms, with or without 00018 * modification, are permitted provided that the following conditions are met: 00019 * 00020 * 1. Redistributions of source code must retain the above copyright notice, this 00021 * list of conditions and the following disclaimer. 00022 * 00023 * 2. Redistributions in binary form must reproduce the above copyright notice, 00024 * this list of conditions and the following disclaimer in the documentation 00025 * and/or other materials provided with the distribution. 00026 * 00027 * 3. The name of Atmel may not be used to endorse or promote products derived 00028 * from this software without specific prior written permission. 00029 * 00030 * 4. This software may only be redistributed and used in connection with an Atmel 00031 * AVR product. 00032 * 00033 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED 00034 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00035 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE 00036 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR 00037 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00038 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00039 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00040 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00041 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00042 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE 00043 * 00044 */ 00045 00046 #ifndef _SD_MMC_SPI_H_ 00047 #define _SD_MMC_SPI_H_ 00048 00049 00050 /*_____ I N C L U D E S ____________________________________________________*/ 00051 00052 #include "conf_access.h" 00053 00054 #if SD_MMC_SPI_MEM == DISABLE 00055 #error sd_mmc_spi.h is #included although SD_MMC_SPI_MEM is disabled 00056 #endif 00057 00058 00059 #include "compiler.h" 00060 #include "spi.h" 00061 00062 00063 /*_____ M A C R O S ________________________________________________________*/ 00064 00065 #ifndef MMC_SECTOR_SIZE 00066 #define MMC_SECTOR_SIZE 512 //default sector size is 512 bytes 00067 #endif 00068 00069 #define byte_adr_of_block(ad) (ad<<9) // returns the first byte address of a specified sector/block number (512bytes/block) 00070 00071 /*_____ D E F I N I T I O N ________________________________________________*/ 00072 00073 // Card identification 00074 #define MMC_CARD 0 00075 #define SD_CARD 1 00076 00077 // Lock operations 00078 #define OP_UNLOCK 0x00 00079 #define OP_LOCK 0x04 00080 #define OP_RESET_PWD 0x02 00081 #define OP_SET_PWD 0x01 00082 #define OP_FORCED_ERASE 0x08 00083 00084 // MMC commands (taken from MMC reference) 00085 #define MMC_GO_IDLE_STATE 0 00086 #define MMC_SEND_OP_COND 1 00087 #define MMC_CMD2 2 00088 #define MMC_SEND_CSD 9 00089 #define MMC_SEND_CID 10 00090 #define MMC_SEND_STATUS 13 00091 #define MMC_SET_BLOCKLEN 16 00092 #define MMC_READ_SINGLE_BLOCK 17 00093 #define MMC_WRITE_BLOCK 24 00094 #define MMC_PROGRAM_CSD 27 00095 #define MMC_SET_WRITE_PROT 28 00096 #define MMC_CLR_WRITE_PROT 29 00097 #define MMC_SEND_WRITE_PROT 30 00098 #define SD_TAG_WR_ERASE_GROUP_START 32 00099 #define SD_TAG_WR_ERASE_GROUP_END 33 00100 #define MMC_TAG_SECTOR_START 32 00101 #define MMC_TAG_SECTOR_END 33 00102 #define MMC_UNTAG_SECTOR 34 00103 #define MMC_TAG_ERASE_GROUP_START 35 00104 #define MMC_TAG_ERASE_GROUP_END 36 00105 #define MMC_UNTAG_ERASE_GROUP 37 00106 #define MMC_ERASE 38 00107 #define SD_SEND_OP_COND_ACMD 41 00108 #define MMC_LOCK_UNLOCK 42 00109 #define SD_APP_CMD55 55 00110 #define MMC_CRC_ON_OFF 59 00111 // R1 Response bit-defines 00112 #define MMC_R1_BUSY 0x80 00113 #define MMC_R1_PARAMETER 0x40 00114 #define MMC_R1_ADDRESS 0x20 00115 #define MMC_R1_ERASE_SEQ 0x10 00116 #define MMC_R1_COM_CRC 0x08 00117 #define MMC_R1_ILLEGAL_COM 0x04 00118 #define MMC_R1_ERASE_RESET 0x02 00119 #define MMC_R1_IDLE_STATE 0x01 00120 // Data Start tokens 00121 #define MMC_STARTBLOCK_READ 0xFE 00122 #define MMC_STARTBLOCK_WRITE 0xFE 00123 #define MMC_STARTBLOCK_MWRITE 0xFC 00124 // Data Stop tokens 00125 #define MMC_STOPTRAN_WRITE 0xFD 00126 // Data Error Token values 00127 #define MMC_DE_MASK 0x1F 00128 #define MMC_DE_ERROR 0x01 00129 #define MMC_DE_CC_ERROR 0x02 00130 #define MMC_DE_ECC_FAIL 0x04 00131 #define MMC_DE_OUT_OF_RANGE 0x04 00132 #define MMC_DE_CARD_LOCKED 0x04 00133 // Data Response Token values 00134 #define MMC_DR_MASK 0x1F 00135 #define MMC_DR_ACCEPT 0x05 00136 #define MMC_DR_REJECT_CRC 0x0B 00137 #define MMC_DR_REJECT_WRITE_ERROR 0x0D 00138 00139 00140 00141 /*_____ D E C L A R A T I O N ______________________________________________*/ 00142 00144 extern Bool sd_mmc_spi_internal_init(void); 00145 extern Bool sd_mmc_spi_init(spi_options_t spiOptions, unsigned int pba_hz); // initializes the SD/MMC card (reset, init, analyse) 00146 extern Bool sd_mmc_spi_check_presence(void); // check the presence of the card 00147 extern Bool sd_mmc_spi_mem_check(void); // check the presence of the card, and initialize if inserted 00148 extern Bool sd_mmc_spi_wait_not_busy (void); // wait for the card to be not busy (exits with timeout) 00149 extern Bool sd_mmc_spi_get_csd(U8 *); // stores the CSD of the card into csd[16] 00150 extern Bool sd_mmc_spi_get_cid(U8 *); // stores the CID of the card into cid[16] 00151 extern void sd_mmc_spi_get_capacity(void); // extract parameters from CSD and compute capacity, last block adress, erase group size 00152 extern Bool sd_mmc_spi_get_status(void); // read the status register of the card (R2 response) 00153 extern U8 sd_mmc_spi_send_and_read(U8); // send a byte on SPI and returns the received byte 00154 extern U8 sd_mmc_spi_send_command(U8, U32); // send a single command + argument (R1 response expected and returned), with memory select then unselect 00155 extern U8 sd_mmc_spi_command(U8, U32); // send a command + argument (R1 response expected and returned), without memory selct/unselect 00156 00158 extern Bool is_sd_mmc_spi_write_pwd_locked(void); // check if the lock protection on the card is featured and enabled 00159 extern Bool sd_mmc_spi_lock_operation(U8, U8, U8 *); // use this function to lock/unlock the card or modify password 00160 00162 extern Bool sd_mmc_spi_read_open (U32); // to call before first access to a random page 00163 extern void sd_mmc_spi_read_close (void); 00164 extern Bool sd_mmc_spi_write_open (U32); // to call before first access to a random page 00165 extern void sd_mmc_spi_write_close (void); 00166 00168 extern Bool sd_mmc_spi_write_sector (U16); // write a 512b sector from USB buffer 00169 extern Bool sd_mmc_spi_read_sector (U16); // reads a 512b sector to an USB buffer 00170 extern Bool sd_mmc_spi_read_multiple_sector(U16 nb_sector); 00171 extern Bool sd_mmc_spi_write_multiple_sector(U16 nb_sector); 00172 extern void sd_mmc_spi_read_multiple_sector_callback(const void *psector); 00173 extern void sd_mmc_spi_write_multiple_sector_callback(void *psector); 00174 00175 00176 /* 00178 bit sd_mmc_spi_host_write_sector (U16); 00179 bit sd_mmc_spi_host_read_sector (U16); 00180 */ 00181 00183 extern Bool sd_mmc_spi_read_sector_to_ram(void *ram); // reads a data block and send it to a buffer (512b) 00184 extern Bool sd_mmc_spi_write_sector_from_ram(const void *ram); // writes a data block from a buffer (512b) 00185 extern Bool sd_mmc_spi_erase_sector_group(U32, U32); // erase a group of sectors defined by start and end address (details in sd_mmc_spi.c) 00186 00187 00189 //Max reading size is block 00190 extern Bool sd_mmc_spi_read_open_PDCA (U32); // to call before first access to a random page 00191 extern void sd_mmc_spi_read_close_PDCA (void); // unselect the memory 00192 00193 00194 extern U8 csd[16]; // stores the Card Specific Data 00195 extern volatile U32 capacity; // stores the capacity in bytes 00196 extern volatile U32 sd_mmc_spi_last_block_address; 00197 extern U16 erase_group_size; 00198 extern U8 r1; 00199 extern U16 r2; 00200 extern U8 card_type; // stores SD_CARD or MMC_CARD type card 00201 extern Bool sd_mmc_spi_init_done; 00202 00203 00204 #endif // _SD_MMC_SPI_H_