00001 /*! \file mmc.h \brief MultiMedia and SD Flash Card Interface. */ 00002 //***************************************************************************** 00003 // 00004 // File Name : 'mmc.h' 00005 // Title : MultiMedia and SD Flash Card Interface 00006 // Author : Pascal Stang - Copyright (C) 2004 00007 // Created : 2004.09.22 00008 // Revised : 2004.09.22 00009 // Version : 0.1 00010 // Target MCU : Atmel AVR Series 00011 // Editor Tabs : 4 00012 // 00013 /// \ingroup driver_hw 00014 /// \defgroup mmc MultiMedia and SD Flash Card Interface (mmc.c) 00015 /// \code #include "mmc.h" \endcode 00016 /// \par Description 00017 /// This library offers some simple functions which can be used 00018 /// to read and write data on a MultiMedia or SecureDigital (SD) Flash 00019 /// Card. Although MM and SD Cards are designed to operate with their own 00020 /// special bus wiring and protocols, both types of cards also provide a 00021 /// simple SPI-like interface mode which is exceptionally useful when 00022 /// attempting to use the cards in embedded systems. 00023 /// 00024 /// \par Wiring 00025 /// To work with this library, the card must be wired to the SPI port of 00026 /// the Atmel microcontroller as described below. Typical cards can 00027 /// operate at up to 25MHz maximum SPI clock rate (thus faster than most 00028 /// AVR's maximum SPI clock rate). 00029 /// <pre> 00030 /// _________________ 00031 /// / 1 2 3 4 5 6 78 | <- view of MMC/SD card looking at contacts 00032 /// / 9 | Pins 8 and 9 are present only on SD cards 00033 /// | MMC/SD Card | 00034 /// | | 00035 /// /\/\/\/\/\/\/\/\/\/\ 00036 /// 1 - CS (chip select) - wire to any available I/O pin(*) 00037 /// 2 - DIN (data in, card<-host) - wire to SPI MOSI pin 00038 /// 3 - VSS (ground) - wire to ground 00039 /// 4 - VDD (power, 3.3V only?) - wire to power (MIGHT BE 3.3V ONLY!) 00040 /// 5 - SCLK (data clock) - wire to SPI SCK pin 00041 /// 6 - VSS (ground) - wire to ground 00042 /// 7 - DOUT (data out, card->host) - wire to SPI MISO pin 00043 /// 00044 /// (*) you must define this chip select I/O pin in mmcconf.h 00045 /// </pre> 00046 /// \note This code is currently below version 1.0, and therefore is considered 00047 /// to be lacking in some functionality or documentation, or may not be fully 00048 /// tested. Nonetheless, you can expect most functions to work. 00049 // 00050 // This code is distributed under the GNU Public License 00051 // which can be found at http://www.gnu.org/licenses/gpl.txt 00052 // 00053 //***************************************************************************** 00054 //@{ 00055 00056 #ifndef MMC_H 00057 #define MMC_H 00058 00059 #include "global.h" 00060 00061 // constants/macros/typdefs 00062 // MMC commands (taken from sandisk MMC reference) 00063 #define MMC_GO_IDLE_STATE 0 ///< initialize card to SPI-type access 00064 #define MMC_SEND_OP_COND 1 ///< set card operational mode 00065 #define MMC_SEND_CSD 9 ///< get card's CSD 00066 #define MMC_SEND_CID 10 ///< get card's CID 00067 #define MMC_SEND_STATUS 13 00068 #define MMC_SET_BLOCKLEN 16 ///< Set number of bytes to transfer per block 00069 #define MMC_READ_SINGLE_BLOCK 17 ///< read a block 00070 #define MMC_WRITE_BLOCK 24 ///< write a block 00071 #define MMC_PROGRAM_CSD 27 00072 #define MMC_SET_WRITE_PROT 28 00073 #define MMC_CLR_WRITE_PROT 29 00074 #define MMC_SEND_WRITE_PROT 30 00075 #define MMC_TAG_SECTOR_START 32 00076 #define MMC_TAG_SECTOR_END 33 00077 #define MMC_UNTAG_SECTOR 34 00078 #define MMC_TAG_ERASE_GROUP_START 35 ///< Sets beginning of erase group (mass erase) 00079 #define MMC_TAG_ERARE_GROUP_END 36 ///< Sets end of erase group (mass erase) 00080 #define MMC_UNTAG_ERASE_GROUP 37 ///< Untag (unset) erase group (mass erase) 00081 #define MMC_ERASE 38 ///< Perform block/mass erase 00082 #define MMC_CRC_ON_OFF 59 ///< Turns CRC check on/off 00083 // R1 Response bit-defines 00084 #define MMC_R1_BUSY 0x80 ///< R1 response: bit indicates card is busy 00085 #define MMC_R1_PARAMETER 0x40 00086 #define MMC_R1_ADDRESS 0x20 00087 #define MMC_R1_ERASE_SEQ 0x10 00088 #define MMC_R1_COM_CRC 0x08 00089 #define MMC_R1_ILLEGAL_COM 0x04 00090 #define MMC_R1_ERASE_RESET 0x02 00091 #define MMC_R1_IDLE_STATE 0x01 00092 // Data Start tokens 00093 #define MMC_STARTBLOCK_READ 0xFE ///< when received from card, indicates that a block of data will follow 00094 #define MMC_STARTBLOCK_WRITE 0xFE ///< when sent to card, indicates that a block of data will follow 00095 #define MMC_STARTBLOCK_MWRITE 0xFC 00096 // Data Stop tokens 00097 #define MMC_STOPTRAN_WRITE 0xFD 00098 // Data Error Token values 00099 #define MMC_DE_MASK 0x1F 00100 #define MMC_DE_ERROR 0x01 00101 #define MMC_DE_CC_ERROR 0x02 00102 #define MMC_DE_ECC_FAIL 0x04 00103 #define MMC_DE_OUT_OF_RANGE 0x04 00104 #define MMC_DE_CARD_LOCKED 0x04 00105 // Data Response Token values 00106 #define MMC_DR_MASK 0x1F 00107 #define MMC_DR_ACCEPT 0x05 00108 #define MMC_DR_REJECT_CRC 0x0B 00109 #define MMC_DR_REJECT_WRITE_ERROR 0x0D 00110 00111 // functions 00112 00113 //! Initialize AVR<->MMC hardware interface. 00114 /// Prepares hardware for MMC access. 00115 void mmcInit(void); 00116 00117 //! Initialize the card and prepare it for use. 00118 /// Returns zero if successful. 00119 u08 mmcReset(void); 00120 00121 //! Send card an MMC command. 00122 /// Returns R1 result code. 00123 u08 mmcSendCommand(u08 cmd, u32 arg); 00124 00125 //! Read 512-byte sector from card to buffer 00126 /// Returns zero if successful. 00127 u08 mmcRead(u32 sector, u08* buffer); 00128 00129 //! Write 512-byte sector from buffer to card 00130 /// Returns zero if successful. 00131 u08 mmcWrite(u32 sector, u08* buffer); 00132 00133 //! Internal command function. 00134 /// Issues a generic MMC command as specified by cmd and arg. 00135 u08 mmcCommand(u08 cmd, u32 arg); 00136 00137 #endif 00138 //@}