Line No. | Rev | Author | Line |
---|---|---|---|
1 | 6 | kaklik | /*! \file mmc.h \brief MultiMedia and SD Flash Card Interface. */ |
2 | //***************************************************************************** |
||
3 | // |
||
4 | // File Name : 'mmc.h' |
||
5 | // Title : MultiMedia and SD Flash Card Interface |
||
6 | // Author : Pascal Stang - Copyright (C) 2004 |
||
7 | // Created : 2004.09.22 |
||
8 | // Revised : 2004.09.22 |
||
9 | // Version : 0.1 |
||
10 | // Target MCU : Atmel AVR Series |
||
11 | // Editor Tabs : 4 |
||
12 | // |
||
13 | /// \ingroup driver_hw |
||
14 | /// \defgroup mmc MultiMedia and SD Flash Card Interface (mmc.c) |
||
15 | /// \code #include "mmc.h" \endcode |
||
16 | /// \par Description |
||
17 | /// This library offers some simple functions which can be used |
||
18 | /// to read and write data on a MultiMedia or SecureDigital (SD) Flash |
||
19 | /// Card. Although MM and SD Cards are designed to operate with their own |
||
20 | /// special bus wiring and protocols, both types of cards also provide a |
||
21 | /// simple SPI-like interface mode which is exceptionally useful when |
||
22 | /// attempting to use the cards in embedded systems. |
||
23 | /// |
||
24 | /// \par Wiring |
||
25 | /// To work with this library, the card must be wired to the SPI port of |
||
26 | /// the Atmel microcontroller as described below. Typical cards can |
||
27 | /// operate at up to 25MHz maximum SPI clock rate (thus faster than most |
||
28 | /// AVR's maximum SPI clock rate). |
||
29 | /// <pre> |
||
30 | /// _________________ |
||
31 | /// / 1 2 3 4 5 6 78 | <- view of MMC/SD card looking at contacts |
||
32 | /// / 9 | Pins 8 and 9 are present only on SD cards |
||
33 | /// | MMC/SD Card | |
||
34 | /// | | |
||
35 | /// /\/\/\/\/\/\/\/\/\/\ |
||
36 | /// 1 - CS (chip select) - wire to any available I/O pin(*) |
||
37 | /// 2 - DIN (data in, card<-host) - wire to SPI MOSI pin |
||
38 | /// 3 - VSS (ground) - wire to ground |
||
39 | /// 4 - VDD (power, 3.3V only?) - wire to power (MIGHT BE 3.3V ONLY!) |
||
40 | /// 5 - SCLK (data clock) - wire to SPI SCK pin |
||
41 | /// 6 - VSS (ground) - wire to ground |
||
42 | /// 7 - DOUT (data out, card->host) - wire to SPI MISO pin |
||
43 | /// |
||
44 | /// (*) you must define this chip select I/O pin in mmcconf.h |
||
45 | /// </pre> |
||
46 | /// \note This code is currently below version 1.0, and therefore is considered |
||
47 | /// to be lacking in some functionality or documentation, or may not be fully |
||
48 | /// tested. Nonetheless, you can expect most functions to work. |
||
49 | // |
||
50 | // This code is distributed under the GNU Public License |
||
51 | // which can be found at http://www.gnu.org/licenses/gpl.txt |
||
52 | // |
||
53 | //***************************************************************************** |
||
54 | //@{ |
||
55 | |||
56 | #ifndef MMC_H |
||
57 | #define MMC_H |
||
58 | |||
59 | #include "global.h" |
||
60 | |||
61 | // constants/macros/typdefs |
||
62 | // MMC commands (taken from sandisk MMC reference) |
||
63 | #define MMC_GO_IDLE_STATE 0 ///< initialize card to SPI-type access |
||
64 | #define MMC_SEND_OP_COND 1 ///< set card operational mode |
||
65 | #define MMC_SEND_CSD 9 ///< get card's CSD |
||
66 | #define MMC_SEND_CID 10 ///< get card's CID |
||
67 | #define MMC_SEND_STATUS 13 |
||
68 | #define MMC_SET_BLOCKLEN 16 ///< Set number of bytes to transfer per block |
||
69 | #define MMC_READ_SINGLE_BLOCK 17 ///< read a block |
||
70 | #define MMC_WRITE_BLOCK 24 ///< write a block |
||
71 | #define MMC_PROGRAM_CSD 27 |
||
72 | #define MMC_SET_WRITE_PROT 28 |
||
73 | #define MMC_CLR_WRITE_PROT 29 |
||
74 | #define MMC_SEND_WRITE_PROT 30 |
||
75 | #define MMC_TAG_SECTOR_START 32 |
||
76 | #define MMC_TAG_SECTOR_END 33 |
||
77 | #define MMC_UNTAG_SECTOR 34 |
||
78 | #define MMC_TAG_ERASE_GROUP_START 35 ///< Sets beginning of erase group (mass erase) |
||
79 | #define MMC_TAG_ERARE_GROUP_END 36 ///< Sets end of erase group (mass erase) |
||
80 | #define MMC_UNTAG_ERASE_GROUP 37 ///< Untag (unset) erase group (mass erase) |
||
81 | #define MMC_ERASE 38 ///< Perform block/mass erase |
||
82 | #define MMC_CRC_ON_OFF 59 ///< Turns CRC check on/off |
||
83 | // R1 Response bit-defines |
||
84 | #define MMC_R1_BUSY 0x80 ///< R1 response: bit indicates card is busy |
||
85 | #define MMC_R1_PARAMETER 0x40 |
||
86 | #define MMC_R1_ADDRESS 0x20 |
||
87 | #define MMC_R1_ERASE_SEQ 0x10 |
||
88 | #define MMC_R1_COM_CRC 0x08 |
||
89 | #define MMC_R1_ILLEGAL_COM 0x04 |
||
90 | #define MMC_R1_ERASE_RESET 0x02 |
||
91 | #define MMC_R1_IDLE_STATE 0x01 |
||
92 | // Data Start tokens |
||
93 | #define MMC_STARTBLOCK_READ 0xFE ///< when received from card, indicates that a block of data will follow |
||
94 | #define MMC_STARTBLOCK_WRITE 0xFE ///< when sent to card, indicates that a block of data will follow |
||
95 | #define MMC_STARTBLOCK_MWRITE 0xFC |
||
96 | // Data Stop tokens |
||
97 | #define MMC_STOPTRAN_WRITE 0xFD |
||
98 | // Data Error Token values |
||
99 | #define MMC_DE_MASK 0x1F |
||
100 | #define MMC_DE_ERROR 0x01 |
||
101 | #define MMC_DE_CC_ERROR 0x02 |
||
102 | #define MMC_DE_ECC_FAIL 0x04 |
||
103 | #define MMC_DE_OUT_OF_RANGE 0x04 |
||
104 | #define MMC_DE_CARD_LOCKED 0x04 |
||
105 | // Data Response Token values |
||
106 | #define MMC_DR_MASK 0x1F |
||
107 | #define MMC_DR_ACCEPT 0x05 |
||
108 | #define MMC_DR_REJECT_CRC 0x0B |
||
109 | #define MMC_DR_REJECT_WRITE_ERROR 0x0D |
||
110 | |||
111 | // functions |
||
112 | |||
113 | //! Initialize AVR<->MMC hardware interface. |
||
114 | /// Prepares hardware for MMC access. |
||
115 | void mmcInit(void); |
||
116 | |||
117 | //! Initialize the card and prepare it for use. |
||
118 | /// Returns zero if successful. |
||
119 | u08 mmcReset(void); |
||
120 | |||
121 | //! Send card an MMC command. |
||
122 | /// Returns R1 result code. |
||
123 | u08 mmcSendCommand(u08 cmd, u32 arg); |
||
124 | |||
125 | //! Read 512-byte sector from card to buffer |
||
126 | /// Returns zero if successful. |
||
127 | u08 mmcRead(u32 sector, u08* buffer); |
||
128 | |||
129 | //! Write 512-byte sector from buffer to card |
||
130 | /// Returns zero if successful. |
||
131 | u08 mmcWrite(u32 sector, u08* buffer); |
||
132 | |||
133 | //! Internal command function. |
||
134 | /// Issues a generic MMC command as specified by cmd and arg. |
||
135 | u08 mmcCommand(u08 cmd, u32 arg); |
||
136 | |||
137 | #endif |
||
138 | //@} |
Powered by WebSVN v2.8.3