Line No. | Rev | Author | Line |
---|---|---|---|
1 | 6 | kaklik | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
2 | <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> |
||
3 | <title>Procyon AVRlib: mmc.c Source File</title> |
||
4 | <link href="dox.css" rel="stylesheet" type="text/css"> |
||
5 | </head><body> |
||
6 | <!-- Generated by Doxygen 1.4.2 --> |
||
7 | <div class="qindex"><a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related Pages</a></div> |
||
8 | <h1>mmc.c</h1><a href="mmc_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file mmc.c \brief MultiMedia and SD Flash Card Interface. */</span> |
||
9 | 00002 <span class="comment">//*****************************************************************************</span> |
||
10 | 00003 <span class="comment">//</span> |
||
11 | 00004 <span class="comment">// File Name : 'mmc.c'</span> |
||
12 | 00005 <span class="comment">// Title : MultiMedia and SD Flash Card Interface</span> |
||
13 | 00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2004</span> |
||
14 | 00007 <span class="comment">// Created : 2004.09.22</span> |
||
15 | 00008 <span class="comment">// Revised : 2006.06.12</span> |
||
16 | 00009 <span class="comment">// Version : 0.1</span> |
||
17 | 00010 <span class="comment">// Target MCU : Atmel AVR Series</span> |
||
18 | 00011 <span class="comment">// Editor Tabs : 4</span> |
||
19 | 00012 <span class="comment">//</span> |
||
20 | 00013 <span class="comment">// NOTE: This code is currently below version 1.0, and therefore is considered</span> |
||
21 | 00014 <span class="comment">// to be lacking in some functionality or documentation, or may not be fully</span> |
||
22 | 00015 <span class="comment">// tested. Nonetheless, you can expect most functions to work.</span> |
||
23 | 00016 <span class="comment">//</span> |
||
24 | 00017 <span class="comment">// This code is distributed under the GNU Public License</span> |
||
25 | 00018 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span> |
||
26 | 00019 <span class="comment">//</span> |
||
27 | 00020 <span class="comment">//*****************************************************************************</span> |
||
28 | 00021 |
||
29 | 00022 <span class="comment">//----- Include Files ---------------------------------------------------------</span> |
||
30 | 00023 <span class="preprocessor">#include <avr/io.h></span> <span class="comment">// include I/O definitions (port names, pin names, etc)</span> |
||
31 | 00024 <span class="preprocessor">#include <avr/interrupt.h></span> <span class="comment">// include interrupt support</span> |
||
32 | 00025 |
||
33 | 00026 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span> <span class="comment">// include our global settings</span> |
||
34 | 00027 <span class="preprocessor">#include "<a class="code" href="spi_8h.html">spi.h</a>"</span> <span class="comment">// include spi bus support</span> |
||
35 | 00028 |
||
36 | 00029 <span class="preprocessor">#include "<a class="code" href="rprintf_8h.html">rprintf.h</a>"</span> |
||
37 | 00030 |
||
38 | 00031 <span class="preprocessor">#include "<a class="code" href="mmc_8h.html">mmc.h</a>"</span> |
||
39 | 00032 |
||
40 | 00033 <span class="comment">// include project-specific hardware configuration</span> |
||
41 | 00034 <span class="preprocessor">#include "<a class="code" href="mmcconf_8h.html">mmcconf.h</a>"</span> |
||
42 | 00035 |
||
43 | 00036 <span class="comment">// Global variables</span> |
||
44 | 00037 |
||
45 | 00038 <span class="comment">// Functions</span> |
||
46 | 00039 |
||
47 | <a name="l00040"></a><a class="code" href="group__mmc.html#ga0">00040</a> <span class="keywordtype">void</span> <a class="code" href="group__mmc.html#ga0">mmcInit</a>(<span class="keywordtype">void</span>) |
||
48 | 00041 { |
||
49 | 00042 <span class="comment">// initialize SPI interface</span> |
||
50 | 00043 spiInit(); |
||
51 | 00044 <span class="comment">// release chip select</span> |
||
52 | 00045 sbi(MMC_CS_DDR, MMC_CS_PIN); |
||
53 | 00046 sbi(MMC_CS_PORT,MMC_CS_PIN); |
||
54 | 00047 } |
||
55 | 00048 |
||
56 | <a name="l00049"></a><a class="code" href="group__mmc.html#ga1">00049</a> u08 <a class="code" href="group__mmc.html#ga1">mmcReset</a>(<span class="keywordtype">void</span>) |
||
57 | 00050 { |
||
58 | 00051 u08 i; |
||
59 | 00052 u08 retry; |
||
60 | 00053 u08 r1=0; |
||
61 | 00054 |
||
62 | 00055 retry = 0; |
||
63 | 00056 <span class="keywordflow">do</span> |
||
64 | 00057 { |
||
65 | 00058 <span class="comment">// send dummy bytes with CS high before accessing</span> |
||
66 | 00059 <span class="keywordflow">for</span>(i=0;i<10;i++) spiTransferByte(0xFF); |
||
67 | 00060 <span class="comment">// resetting card, go to SPI mode</span> |
||
68 | 00061 r1 = <a class="code" href="group__mmc.html#ga2">mmcSendCommand</a>(<a class="code" href="group__mmc.html#ga6">MMC_GO_IDLE_STATE</a>, 0); |
||
69 | 00062 <span class="preprocessor"> #ifdef MMC_DEBUG</span> |
||
70 | 00063 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"MMC_GO_IDLE_STATE: R1=0x%x\r\n"</span>, r1); |
||
71 | 00064 <span class="preprocessor"> #endif</span> |
||
72 | 00065 <span class="preprocessor"></span> <span class="comment">// do retry counter</span> |
||
73 | 00066 retry++; |
||
74 | 00067 <span class="keywordflow">if</span>(retry>10) <span class="keywordflow">return</span> -1; |
||
75 | 00068 } <span class="keywordflow">while</span>(r1 != 0x01); |
||
76 | 00069 |
||
77 | 00070 <span class="comment">// TODO: check card parameters for voltage compliance</span> |
||
78 | 00071 <span class="comment">// before issuing initialize command</span> |
||
79 | 00072 |
||
80 | 00073 retry = 0; |
||
81 | 00074 <span class="keywordflow">do</span> |
||
82 | 00075 { |
||
83 | 00076 <span class="comment">// initializing card for operation</span> |
||
84 | 00077 r1 = <a class="code" href="group__mmc.html#ga2">mmcSendCommand</a>(<a class="code" href="group__mmc.html#ga7">MMC_SEND_OP_COND</a>, 0); |
||
85 | 00078 <span class="preprocessor"> #ifdef MMC_DEBUG</span> |
||
86 | 00079 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"MMC_SEND_OP_COND: R1=0x%x\r\n"</span>, r1); |
||
87 | 00080 <span class="preprocessor"> #endif</span> |
||
88 | 00081 <span class="preprocessor"></span> <span class="comment">// do retry counter</span> |
||
89 | 00082 retry++; |
||
90 | 00083 <span class="keywordflow">if</span>(retry>100) <span class="keywordflow">return</span> -1; |
||
91 | 00084 } <span class="keywordflow">while</span>(r1); |
||
92 | 00085 |
||
93 | 00086 <span class="comment">// turn off CRC checking to simplify communication</span> |
||
94 | 00087 r1 = <a class="code" href="group__mmc.html#ga2">mmcSendCommand</a>(<a class="code" href="group__mmc.html#ga25">MMC_CRC_ON_OFF</a>, 0); |
||
95 | 00088 <span class="preprocessor"> #ifdef MMC_DEBUG</span> |
||
96 | 00089 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"MMC_CRC_ON_OFF: R1=0x%x\r\n"</span>, r1); |
||
97 | 00090 <span class="preprocessor"> #endif</span> |
||
98 | 00091 <span class="preprocessor"></span> |
||
99 | 00092 <span class="comment">// set block length to 512 bytes</span> |
||
100 | 00093 r1 = <a class="code" href="group__mmc.html#ga2">mmcSendCommand</a>(<a class="code" href="group__mmc.html#ga11">MMC_SET_BLOCKLEN</a>, 512); |
||
101 | 00094 <span class="preprocessor"> #ifdef MMC_DEBUG</span> |
||
102 | 00095 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"MMC_SET_BLOCKLEN: R1=0x%x\r\n"</span>, r1); |
||
103 | 00096 <span class="preprocessor"> #endif</span> |
||
104 | 00097 <span class="preprocessor"></span> |
||
105 | 00098 <span class="comment">// return success</span> |
||
106 | 00099 <span class="keywordflow">return</span> 0; |
||
107 | 00100 } |
||
108 | 00101 |
||
109 | <a name="l00102"></a><a class="code" href="group__mmc.html#ga2">00102</a> u08 <a class="code" href="group__mmc.html#ga2">mmcSendCommand</a>(u08 cmd, u32 arg) |
||
110 | 00103 { |
||
111 | 00104 u08 r1; |
||
112 | 00105 |
||
113 | 00106 <span class="comment">// assert chip select</span> |
||
114 | 00107 cbi(MMC_CS_PORT,MMC_CS_PIN); |
||
115 | 00108 <span class="comment">// issue the command</span> |
||
116 | 00109 r1 = <a class="code" href="group__mmc.html#ga5">mmcCommand</a>(cmd, arg); |
||
117 | 00110 <span class="comment">// release chip select</span> |
||
118 | 00111 sbi(MMC_CS_PORT,MMC_CS_PIN); |
||
119 | 00112 |
||
120 | 00113 <span class="keywordflow">return</span> r1; |
||
121 | 00114 } |
||
122 | 00115 |
||
123 | <a name="l00116"></a><a class="code" href="group__mmc.html#ga3">00116</a> u08 <a class="code" href="group__mmc.html#ga3">mmcRead</a>(u32 sector, u08* buffer) |
||
124 | 00117 { |
||
125 | 00118 u08 r1; |
||
126 | 00119 u16 i; |
||
127 | 00120 |
||
128 | 00121 <span class="comment">// assert chip select</span> |
||
129 | 00122 cbi(MMC_CS_PORT,MMC_CS_PIN); |
||
130 | 00123 <span class="comment">// issue command</span> |
||
131 | 00124 r1 = <a class="code" href="group__mmc.html#ga5">mmcCommand</a>(<a class="code" href="group__mmc.html#ga12">MMC_READ_SINGLE_BLOCK</a>, sector<<9); |
||
132 | 00125 <span class="preprocessor"> #ifdef MMC_DEBUG</span> |
||
133 | 00126 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"MMC Read Block R1=0x%x\r\n"</span>, r1); |
||
134 | 00127 <span class="preprocessor"> #endif</span> |
||
135 | 00128 <span class="preprocessor"></span> <span class="comment">// check for valid response</span> |
||
136 | 00129 <span class="keywordflow">if</span>(r1 != 0x00) |
||
137 | 00130 <span class="keywordflow">return</span> r1; |
||
138 | 00131 <span class="comment">// wait for block start</span> |
||
139 | 00132 <span class="keywordflow">while</span>(spiTransferByte(0xFF) != <a class="code" href="group__mmc.html#ga34">MMC_STARTBLOCK_READ</a>); |
||
140 | 00133 <span class="comment">// read in data</span> |
||
141 | 00134 <span class="keywordflow">for</span>(i=0; i<0x200; i++) |
||
142 | 00135 { |
||
143 | 00136 *buffer++ = spiTransferByte(0xFF); |
||
144 | 00137 } |
||
145 | 00138 <span class="comment">// read 16-bit CRC</span> |
||
146 | 00139 spiTransferByte(0xFF); |
||
147 | 00140 spiTransferByte(0xFF); |
||
148 | 00141 <span class="comment">// release chip select</span> |
||
149 | 00142 sbi(MMC_CS_PORT,MMC_CS_PIN); |
||
150 | 00143 <span class="comment">// return success</span> |
||
151 | 00144 <span class="keywordflow">return</span> 0; |
||
152 | 00145 } |
||
153 | 00146 |
||
154 | <a name="l00147"></a><a class="code" href="group__mmc.html#ga4">00147</a> u08 <a class="code" href="group__mmc.html#ga4">mmcWrite</a>(u32 sector, u08* buffer) |
||
155 | 00148 { |
||
156 | 00149 u08 r1; |
||
157 | 00150 u16 i; |
||
158 | 00151 |
||
159 | 00152 <span class="comment">// assert chip select</span> |
||
160 | 00153 cbi(MMC_CS_PORT,MMC_CS_PIN); |
||
161 | 00154 <span class="comment">// issue command</span> |
||
162 | 00155 r1 = <a class="code" href="group__mmc.html#ga5">mmcCommand</a>(<a class="code" href="group__mmc.html#ga13">MMC_WRITE_BLOCK</a>, sector<<9); |
||
163 | 00156 <span class="preprocessor"> #ifdef MMC_DEBUG</span> |
||
164 | 00157 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"MMC Write Block R1=0x%x\r\n"</span>, r1); |
||
165 | 00158 <span class="preprocessor"> #endif</span> |
||
166 | 00159 <span class="preprocessor"></span> <span class="comment">// check for valid response</span> |
||
167 | 00160 <span class="keywordflow">if</span>(r1 != 0x00) |
||
168 | 00161 <span class="keywordflow">return</span> r1; |
||
169 | 00162 <span class="comment">// send dummy</span> |
||
170 | 00163 spiTransferByte(0xFF); |
||
171 | 00164 <span class="comment">// send data start token</span> |
||
172 | 00165 spiTransferByte(<a class="code" href="group__mmc.html#ga35">MMC_STARTBLOCK_WRITE</a>); |
||
173 | 00166 <span class="comment">// write data</span> |
||
174 | 00167 <span class="keywordflow">for</span>(i=0; i<0x200; i++) |
||
175 | 00168 { |
||
176 | 00169 spiTransferByte(*buffer++); |
||
177 | 00170 } |
||
178 | 00171 <span class="comment">// write 16-bit CRC (dummy values)</span> |
||
179 | 00172 spiTransferByte(0xFF); |
||
180 | 00173 spiTransferByte(0xFF); |
||
181 | 00174 <span class="comment">// read data response token</span> |
||
182 | 00175 r1 = spiTransferByte(0xFF); |
||
183 | 00176 <span class="keywordflow">if</span>( (r1&MMC_DR_MASK) != MMC_DR_ACCEPT) |
||
184 | 00177 <span class="keywordflow">return</span> r1; |
||
185 | 00178 <span class="preprocessor"> #ifdef MMC_DEBUG</span> |
||
186 | 00179 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"Data Response Token=0x%x\r\n"</span>, r1); |
||
187 | 00180 <span class="preprocessor"> #endif</span> |
||
188 | 00181 <span class="preprocessor"></span> <span class="comment">// wait until card not busy</span> |
||
189 | 00182 <span class="keywordflow">while</span>(!spiTransferByte(0xFF)); |
||
190 | 00183 <span class="comment">// release chip select</span> |
||
191 | 00184 sbi(MMC_CS_PORT,MMC_CS_PIN); |
||
192 | 00185 <span class="comment">// return success</span> |
||
193 | 00186 <span class="keywordflow">return</span> 0; |
||
194 | 00187 } |
||
195 | 00188 |
||
196 | <a name="l00189"></a><a class="code" href="group__mmc.html#ga5">00189</a> u08 <a class="code" href="group__mmc.html#ga5">mmcCommand</a>(u08 cmd, u32 arg) |
||
197 | 00190 { |
||
198 | 00191 u08 r1; |
||
199 | 00192 u08 retry=0; |
||
200 | 00193 <span class="comment">// send command</span> |
||
201 | 00194 spiTransferByte(cmd | 0x40); |
||
202 | 00195 spiTransferByte(arg>>24); |
||
203 | 00196 spiTransferByte(arg>>16); |
||
204 | 00197 spiTransferByte(arg>>8); |
||
205 | 00198 spiTransferByte(arg); |
||
206 | 00199 spiTransferByte(0x95); <span class="comment">// crc valid only for MMC_GO_IDLE_STATE</span> |
||
207 | 00200 <span class="comment">// end command</span> |
||
208 | 00201 <span class="comment">// wait for response</span> |
||
209 | 00202 <span class="comment">// if more than 8 retries, card has timed-out</span> |
||
210 | 00203 <span class="comment">// return the received 0xFF</span> |
||
211 | 00204 <span class="keywordflow">while</span>((r1 = spiTransferByte(0xFF)) == 0xFF) |
||
212 | 00205 <span class="keywordflow">if</span>(retry++ > 8) <span class="keywordflow">break</span>; |
||
213 | 00206 <span class="comment">// return response</span> |
||
214 | 00207 <span class="keywordflow">return</span> r1; |
||
215 | 00208 } |
||
216 | </pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by |
||
217 | <a href="http://www.doxygen.org/index.html"> |
||
218 | <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address> |
||
219 | </body> |
||
220 | </html> |
Powered by WebSVN v2.8.3