📄 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> | <a class="qindex" href="pages.html">Related Pages</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 : 2006.06.12</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/interrupt.h></span> <span class="comment">// include interrupt support</span>00025 00026 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span> <span class="comment">// include our global settings</span>00027 <span class="preprocessor">#include "<a class="code" href="spi_8h.html">spi.h</a>"</span> <span class="comment">// include spi bus support</span>00028 00029 <span class="preprocessor">#include "<a class="code" href="rprintf_8h.html">rprintf.h</a>"</span>00030 00031 <span class="preprocessor">#include "<a class="code" href="mmc_8h.html">mmc.h</a>"</span>00032 00033 <span class="comment">// include project-specific hardware configuration</span>00034 <span class="preprocessor">#include "<a class="code" href="mmcconf_8h.html">mmcconf.h</a>"</span>00035 00036 <span class="comment">// Global variables</span>00037 00038 <span class="comment">// Functions</span>00039 <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>)00041 {00042 <span class="comment">// initialize SPI interface</span>00043 spiInit();00044 <span class="comment">// release chip select</span>00045 sbi(MMC_CS_DDR, MMC_CS_PIN);00046 sbi(MMC_CS_PORT,MMC_CS_PIN);00047 }00048 <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>)00050 {00051 u08 i;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 <span class="keywordflow">for</span>(i=0;i<10;i++) spiTransferByte(0xFF);00060 <span class="comment">// resetting card, go to SPI mode</span>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);00062 <span class="preprocessor"> #ifdef MMC_DEBUG</span>00063 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"MMC_GO_IDLE_STATE: R1=0x%x\r\n"</span>, r1);00064 <span class="preprocessor"> #endif</span>00065 <span class="preprocessor"></span> <span class="comment">// do retry counter</span>00066 retry++;00067 <span class="keywordflow">if</span>(retry>10) <span class="keywordflow">return</span> -1;00068 } <span class="keywordflow">while</span>(r1 != 0x01);00069 00070 <span class="comment">// TODO: check card parameters for voltage compliance</span>00071 <span class="comment">// before issuing initialize command</span>00072 00073 retry = 0;00074 <span class="keywordflow">do</span>00075 {00076 <span class="comment">// initializing card for operation</span>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);00078 <span class="preprocessor"> #ifdef MMC_DEBUG</span>00079 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"MMC_SEND_OP_COND: R1=0x%x\r\n"</span>, r1);00080 <span class="preprocessor"> #endif</span>00081 <span class="preprocessor"></span> <span class="comment">// do retry counter</span>00082 retry++;00083 <span class="keywordflow">if</span>(retry>100) <span class="keywordflow">return</span> -1;00084 } <span class="keywordflow">while</span>(r1);00085 00086 <span class="comment">// turn off CRC checking to simplify communication</span>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);00088 <span class="preprocessor"> #ifdef MMC_DEBUG</span>00089 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"MMC_CRC_ON_OFF: R1=0x%x\r\n"</span>, r1);00090 <span class="preprocessor"> #endif</span>00091 <span class="preprocessor"></span>00092 <span class="comment">// set block length to 512 bytes</span>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);00094 <span class="preprocessor"> #ifdef MMC_DEBUG</span>00095 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"MMC_SET_BLOCKLEN: R1=0x%x\r\n"</span>, r1);00096 <span class="preprocessor"> #endif</span>00097 <span class="preprocessor"></span>00098 <span class="comment">// return success</span>00099 <span class="keywordflow">return</span> 0;00100 }00101 <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)00103 {00104 u08 r1;00105 00106 <span class="comment">// assert chip select</span>00107 cbi(MMC_CS_PORT,MMC_CS_PIN);00108 <span class="comment">// issue the command</span>00109 r1 = <a class="code" href="group__mmc.html#ga5">mmcCommand</a>(cmd, arg);00110 <span class="comment">// release chip select</span>00111 sbi(MMC_CS_PORT,MMC_CS_PIN);00112 00113 <span class="keywordflow">return</span> r1;00114 }00115 <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)00117 {00118 u08 r1;00119 u16 i;00120 00121 <span class="comment">// assert chip select</span>00122 cbi(MMC_CS_PORT,MMC_CS_PIN);00123 <span class="comment">// issue command</span>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);00125 <span class="preprocessor"> #ifdef MMC_DEBUG</span>00126 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"MMC Read Block R1=0x%x\r\n"</span>, r1);00127 <span class="preprocessor"> #endif</span>00128 <span class="preprocessor"></span> <span class="comment">// check for valid response</span>00129 <span class="keywordflow">if</span>(r1 != 0x00)00130 <span class="keywordflow">return</span> r1;00131 <span class="comment">// wait for block start</span>00132 <span class="keywordflow">while</span>(spiTransferByte(0xFF) != <a class="code" href="group__mmc.html#ga34">MMC_STARTBLOCK_READ</a>);00133 <span class="comment">// read in data</span>00134 <span class="keywordflow">for</span>(i=0; i<0x200; i++)00135 {00136 *buffer++ = spiTransferByte(0xFF);00137 }00138 <span class="comment">// read 16-bit CRC</span>00139 spiTransferByte(0xFF);00140 spiTransferByte(0xFF);00141 <span class="comment">// release chip select</span>00142 sbi(MMC_CS_PORT,MMC_CS_PIN);00143 <span class="comment">// return success</span>00144 <span class="keywordflow">return</span> 0;00145 }00146 <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)00148 {00149 u08 r1;00150 u16 i;00151 00152 <span class="comment">// assert chip select</span>00153 cbi(MMC_CS_PORT,MMC_CS_PIN);00154 <span class="comment">// issue command</span>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);00156 <span class="preprocessor"> #ifdef MMC_DEBUG</span>00157 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"MMC Write Block R1=0x%x\r\n"</span>, r1);00158 <span class="preprocessor"> #endif</span>00159 <span class="preprocessor"></span> <span class="comment">// check for valid response</span>00160 <span class="keywordflow">if</span>(r1 != 0x00)00161 <span class="keywordflow">return</span> r1;00162 <span class="comment">// send dummy</span>00163 spiTransferByte(0xFF);00164 <span class="comment">// send data start token</span>00165 spiTransferByte(<a class="code" href="group__mmc.html#ga35">MMC_STARTBLOCK_WRITE</a>);00166 <span class="comment">// write data</span>00167 <span class="keywordflow">for</span>(i=0; i<0x200; i++)00168 {00169 spiTransferByte(*buffer++);00170 }00171 <span class="comment">// write 16-bit CRC (dummy values)</span>00172 spiTransferByte(0xFF);00173 spiTransferByte(0xFF);00174 <span class="comment">// read data response token</span>00175 r1 = spiTransferByte(0xFF);00176 <span class="keywordflow">if</span>( (r1&MMC_DR_MASK) != MMC_DR_ACCEPT)00177 <span class="keywordflow">return</span> r1;00178 <span class="preprocessor"> #ifdef MMC_DEBUG</span>00179 <span class="preprocessor"></span> rprintf(<span class="stringliteral">"Data Response Token=0x%x\r\n"</span>, r1);00180 <span class="preprocessor"> #endif</span>00181 <span class="preprocessor"></span> <span class="comment">// wait until card not busy</span>00182 <span class="keywordflow">while</span>(!spiTransferByte(0xFF));00183 <span class="comment">// release chip select</span>00184 sbi(MMC_CS_PORT,MMC_CS_PIN);00185 <span class="comment">// return success</span>00186 <span class="keywordflow">return</span> 0;00187 }00188 <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)00190 {00191 u08 r1;00192 u08 retry=0;00193 <span class="comment">// send command</span>00194 spiTransferByte(cmd | 0x40);00195 spiTransferByte(arg>>24);00196 spiTransferByte(arg>>16);00197 spiTransferByte(arg>>8);00198 spiTransferByte(arg);00199 spiTransferByte(0x95); <span class="comment">// crc valid only for MMC_GO_IDLE_STATE</span>00200 <span class="comment">// end command</span>00201 <span class="comment">// wait for response</span>00202 <span class="comment">// if more than 8 retries, card has timed-out</span>00203 <span class="comment">// return the received 0xFF</span>00204 <span class="keywordflow">while</span>((r1 = spiTransferByte(0xFF)) == 0xFF)00205 <span class="keywordflow">if</span>(retry++ > 8) <span class="keywordflow">break</span>;00206 <span class="comment">// return response</span>00207 <span class="keywordflow">return</span> r1;00208 }</pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:07 2006 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 + -