📄 mmc_8c-source.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>Procyon AVRlib: mmc.c Source File</title><link href="dox.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.4.2 --><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></div><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>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name : 'mmc.c'</span>00005 <span class="comment">// Title : MultiMedia and SD Flash Card Interface</span>00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2004</span>00007 <span class="comment">// Created : 2004.09.22</span>00008 <span class="comment">// Revised : 2004.09.22</span>00009 <span class="comment">// Version : 0.1</span>00010 <span class="comment">// Target MCU : Atmel AVR Series</span>00011 <span class="comment">// Editor Tabs : 4</span>00012 <span class="comment">//</span>00013 <span class="comment">// NOTE: This code is currently below version 1.0, and therefore is considered</span>00014 <span class="comment">// to be lacking in some functionality or documentation, or may not be fully</span>00015 <span class="comment">// tested. Nonetheless, you can expect most functions to work.</span>00016 <span class="comment">//</span>00017 <span class="comment">// This code is distributed under the GNU Public License</span>00018 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span>00019 <span class="comment">//</span>00020 <span class="comment">//*****************************************************************************</span>00021 00022 <span class="comment">//----- Include Files ---------------------------------------------------------</span>00023 <span class="preprocessor">#include <avr/io.h></span> <span class="comment">// include I/O definitions (port names, pin names, etc)</span>00024 <span class="preprocessor">#include <avr/signal.h></span> <span class="comment">// include "signal" names (interrupt names)</span>00025 <span class="preprocessor">#include <avr/interrupt.h></span> <span class="comment">// include interrupt support</span>00026 00027 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span> <span class="comment">// include our global settings</span>00028 <span class="preprocessor">#include "<a class="code" href="spi_8h.html">spi.h</a>"</span> <span class="comment">// include spi bus support</span>00029 00030 <span class="preprocessor">#include "<a class="code" href="rprintf_8h.html">rprintf.h</a>"</span>00031 00032 <span class="preprocessor">#include "<a class="code" href="mmc_8h.html">mmc.h</a>"</span>00033 00034 <span class="comment">// include project-specific hardware configuration</span>00035 <span class="preprocessor">#include "<a class="code" href="mmcconf_8h.html">mmcconf.h</a>"</span>00036 00037 <span class="comment">// Global variables</span>00038 00039 <span class="comment">// Functions</span>00040 <a name="l00041"></a><a class="code" href="group__mmc.html#ga0">00041</a> <span class="keywordtype">void</span> <a class="code" href="group__mmc.html#ga0">mmcInit</a>(<span class="keywordtype">void</span>)00042 {00043 <span class="comment">// initialize SPI interface</span>00044 spiInit();00045 <span class="comment">// release chip select</span>00046 sbi(MMC_CS_DDR, MMC_CS_PIN);00047 sbi(MMC_CS_PORT,MMC_CS_PIN);00048 }00049 <a name="l00050"></a><a class="code" href="group__mmc.html#ga1">00050</a> u08 <a class="code" href="group__mmc.html#ga1">mmcReset</a>(<span class="keywordtype">void</span>)00051 {00052 u08 retry;00053 u08 r1=0;00054 00055 retry = 0;00056 <span class="keywordflow">do</span>00057 {00058 <span class="comment">// send dummy bytes with CS high before accessing</span>00059 spiTransferByte(0xFF);00060 spiTransferByte(0xFF);00061 spiTransferByte(0xFF);00062 spiTransferByte(0xFF);00063 <span class="comment">// resetting card, go to SPI mode</span>00064 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);00065 <span class="preprocessor"> #ifdef MMC_DEBUG</span>00066 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"MMC_GO_IDLE_STATE: R1=0x%x\r\n"</span>, r1);00067 <span class="preprocessor"> #endif</span>00068 <span class="preprocessor"></span> <span class="comment">// do retry counter</span>00069 retry++;00070 <span class="keywordflow">if</span>(retry>10) <span class="keywordflow">return</span> -1;00071 } <span class="keywordflow">while</span>(r1 != 0x01);00072 00073 <span class="comment">// TODO: check card parameters for voltage compliance</span>00074 <span class="comment">// before issuing initialize command</span>00075 00076 retry = 0;00077 <span class="keywordflow">do</span>00078 {00079 <span class="comment">// initializing card for operation</span>00080 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);00081 <span class="preprocessor"> #ifdef MMC_DEBUG</span>00082 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"MMC_SEND_OP_COND: R1=0x%x\r\n"</span>, r1);00083 <span class="preprocessor"> #endif</span>00084 <span class="preprocessor"></span> <span class="comment">// do retry counter</span>00085 retry++;00086 <span class="keywordflow">if</span>(retry>100) <span class="keywordflow">return</span> -1;00087 } <span class="keywordflow">while</span>(r1);00088 00089 <span class="comment">// turn off CRC checking to simplify communication</span>00090 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);00091 <span class="preprocessor"> #ifdef MMC_DEBUG</span>00092 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"MMC_CRC_ON_OFF: R1=0x%x\r\n"</span>, r1);00093 <span class="preprocessor"> #endif</span>00094 <span class="preprocessor"></span>00095 <span class="comment">// set block length to 512 bytes</span>00096 r1 = <a class="code" href="group__mmc.html#ga2">mmcSendCommand</a>(<a class="code" href="group__mmc.html#ga11">MMC_SET_BLOCKLEN</a>, 512);00097 <span class="preprocessor"> #ifdef MMC_DEBUG</span>00098 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"MMC_SET_BLOCKLEN: R1=0x%x\r\n"</span>, r1);00099 <span class="preprocessor"> #endif</span>00100 <span class="preprocessor"></span>00101 <span class="comment">// return success</span>00102 <span class="keywordflow">return</span> 0;00103 }00104 <a name="l00105"></a><a class="code" href="group__mmc.html#ga2">00105</a> u08 <a class="code" href="group__mmc.html#ga2">mmcSendCommand</a>(u08 cmd, u32 arg)00106 {00107 u08 r1;00108 00109 <span class="comment">// assert chip select</span>00110 cbi(MMC_CS_PORT,MMC_CS_PIN);00111 <span class="comment">// issue the command</span>00112 r1 = <a class="code" href="group__mmc.html#ga5">mmcCommand</a>(cmd, arg);00113 <span class="comment">// release chip select</span>00114 sbi(MMC_CS_PORT,MMC_CS_PIN);00115 00116 <span class="keywordflow">return</span> r1;00117 }00118 <a name="l00119"></a><a class="code" href="group__mmc.html#ga3">00119</a> u08 <a class="code" href="group__mmc.html#ga3">mmcRead</a>(u32 sector, u08* buffer)00120 {00121 u08 r1;00122 u16 i;00123 00124 <span class="comment">// assert chip select</span>00125 cbi(MMC_CS_PORT,MMC_CS_PIN);00126 <span class="comment">// issue command</span>00127 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);00128 <span class="preprocessor"> #ifdef MMC_DEBUG</span>00129 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"MMC Read Block R1=0x%x\r\n"</span>, r1);00130 <span class="preprocessor"> #endif</span>00131 <span class="preprocessor"></span> <span class="comment">// check for valid response</span>00132 <span class="keywordflow">if</span>(r1 != 0x00)00133 <span class="keywordflow">return</span> r1;00134 <span class="comment">// wait for block start</span>00135 <span class="keywordflow">while</span>(spiTransferByte(0xFF) != <a class="code" href="group__mmc.html#ga34">MMC_STARTBLOCK_READ</a>);00136 <span class="comment">// read in data</span>00137 <span class="keywordflow">for</span>(i=0; i<0x200; i++)00138 {00139 *buffer++ = spiTransferByte(0xFF);00140 }00141 <span class="comment">// read 16-bit CRC</span>00142 spiTransferByte(0xFF);00143 spiTransferByte(0xFF);00144 <span class="comment">// release chip select</span>00145 sbi(MMC_CS_PORT,MMC_CS_PIN);00146 <span class="comment">// return success</span>00147 <span class="keywordflow">return</span> 0;00148 }00149 <a name="l00150"></a><a class="code" href="group__mmc.html#ga4">00150</a> u08 <a class="code" href="group__mmc.html#ga4">mmcWrite</a>(u32 sector, u08* buffer)00151 {00152 u08 r1;00153 u16 i;00154 00155 <span class="comment">// assert chip select</span>00156 cbi(MMC_CS_PORT,MMC_CS_PIN);00157 <span class="comment">// issue command</span>00158 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);00159 <span class="preprocessor"> #ifdef MMC_DEBUG</span>00160 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"MMC Write Block R1=0x%x\r\n"</span>, r1);00161 <span class="preprocessor"> #endif</span>00162 <span class="preprocessor"></span> <span class="comment">// check for valid response</span>00163 <span class="keywordflow">if</span>(r1 != 0x00)00164 <span class="keywordflow">return</span> r1;00165 <span class="comment">// send dummy</span>00166 spiTransferByte(0xFF);00167 <span class="comment">// send data start token</span>00168 spiTransferByte(<a class="code" href="group__mmc.html#ga35">MMC_STARTBLOCK_WRITE</a>);00169 <span class="comment">// write data</span>00170 <span class="keywordflow">for</span>(i=0; i<0x200; i++)00171 {00172 spiTransferByte(*buffer++);00173 }00174 <span class="comment">// write 16-bit CRC (dummy values)</span>00175 spiTransferByte(0xFF);00176 spiTransferByte(0xFF);00177 <span class="comment">// read data response token</span>00178 r1 = spiTransferByte(0xFF);00179 <span class="keywordflow">if</span>( (r1&MMC_DR_MASK) != MMC_DR_ACCEPT)00180 <span class="keywordflow">return</span> r1;00181 <span class="preprocessor"> #ifdef MMC_DEBUG</span>00182 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"Data Response Token=0x%x\r\n"</span>, r1);00183 <span class="preprocessor"> #endif</span>00184 <span class="preprocessor"></span> <span class="comment">// wait until card not busy</span>00185 <span class="keywordflow">while</span>(!spiTransferByte(0xFF));00186 <span class="comment">// release chip select</span>00187 sbi(MMC_CS_PORT,MMC_CS_PIN);00188 <span class="comment">// return success</span>00189 <span class="keywordflow">return</span> 0;00190 }00191 <a name="l00192"></a><a class="code" href="group__mmc.html#ga5">00192</a> u08 <a class="code" href="group__mmc.html#ga5">mmcCommand</a>(u08 cmd, u32 arg)00193 {00194 u08 r1;00195 u08 retry=0;00196 <span class="comment">// send command</span>00197 spiTransferByte(cmd | 0x40);00198 spiTransferByte(arg>>24);00199 spiTransferByte(arg>>16);00200 spiTransferByte(arg>>8);00201 spiTransferByte(arg);00202 spiTransferByte(0x95); <span class="comment">// crc valid only for MMC_GO_IDLE_STATE</span>00203 <span class="comment">// end command</span>00204 <span class="comment">// wait for response</span>00205 <span class="comment">// if more than 8 retries, card has timed-out</span>00206 <span class="comment">// return the received 0xFF</span>00207 <span class="keywordflow">while</span>((r1 = spiTransferByte(0xFF)) == 0xFF)00208 <span class="keywordflow">if</span>(retry++ > 8) <span class="keywordflow">break</span>;00209 <span class="comment">// return response</span>00210 <span class="keywordflow">return</span> r1;00211 }</pre></div><hr size="1"><address style="align: right;"><small>Generated on Mon Aug 22 04:29:27 2005 for Procyon AVRlib by <a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -