⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ata_8c-source.html

📁 ATMEL的AVR单片机库文件
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!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: ata.c Source File</title><link href="dox.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.3.6 --><div class="qindex"><a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data&nbsp;Structures</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Data&nbsp;Fields</a> | <a class="qindex" href="globals.html">Globals</a></div><h1>ata.c</h1><a href="ata_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <span class="comment">/*! \file ata.c \brief IDE-ATA hard disk interface driver. */</span>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name    : 'ata.c'</span>00005 <span class="comment">// Title        : IDE-ATA interface driver for hard disks</span>00006 <span class="comment">// Author       : Pascal Stang</span>00007 <span class="comment">// Date         : 11/22/2000</span>00008 <span class="comment">// Revised      : 4/19/2003</span>00009 <span class="comment">// Version      : 0.3</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="preprocessor">#ifndef WIN32</span>00023 <span class="preprocessor"></span><span class="preprocessor">    #include &lt;avr/io.h&gt;</span>00024 <span class="preprocessor">    #include &lt;avr/interrupt.h&gt;</span>00025 <span class="preprocessor">    #include &lt;avr/signal.h&gt;</span>00026 <span class="preprocessor">    #include &lt;avr/pgmspace.h&gt;</span>00027 <span class="comment">//  #include &lt;stdio.h&gt;</span>00028 <span class="preprocessor">#endif</span>00029 <span class="preprocessor"></span><span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>00030 <span class="preprocessor">#include "<a class="code" href="timer_8h.html">timer.h</a>"</span>00031 <span class="preprocessor">#include "<a class="code" href="rprintf_8h.html">rprintf.h</a>"</span>00032 00033 <span class="preprocessor">#include "<a class="code" href="ata_8h.html">ata.h</a>"</span>00034 00035 <span class="comment">//#define DEBUG_ATA 1</span>00036 00037 <span class="comment">// global variables</span>00038 00039 <span class="comment">// drive information</span>00040 typeDriveInfo ataDriveInfo;00041 00042 00043 <span class="keywordtype">void</span> ataInit(<span class="keywordtype">void</span>)00044 {00045 00046 }00047 00048 <span class="keywordtype">void</span> ataDriveInit(<span class="keywordtype">void</span>)00049 {00050     u08 i;00051     <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* buffer = (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>*) SECTOR_BUFFER_ADDR;00052 00053     <span class="comment">// read drive identity</span>00054     rprintfProgStrM(<span class="stringliteral">"\r\nScanning IDE interface...\r\n"</span>);00055     <span class="comment">// Wait for drive to be ready</span>00056     ataStatusWait(ATA_SR_BSY, ATA_SR_BSY);00057     <span class="comment">// issue identify command</span>00058     ataWriteByte(ATA_REG_CMDSTATUS1, 0xEC);00059     <span class="comment">// wait for drive to request data transfer</span>00060     ataStatusWait(ATA_SR_DRQ, ATA_SR_DRQ);00061     <a class="code" href="timer128_8h.html#a54">timerPause</a>(200);00062     <span class="comment">// read in the data</span>00063     ataReadDataBuffer(buffer, 512);00064 00065     <span class="comment">// set local drive info parameters</span>00066     ataDriveInfo.cylinders =        *( ((<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>*) buffer) + ATA_IDENT_CYLINDERS );00067     ataDriveInfo.heads =            *( ((<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>*) buffer) + ATA_IDENT_HEADS );00068     ataDriveInfo.sectors =          *( ((<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>*) buffer) + ATA_IDENT_SECTORS );00069     ataDriveInfo.LBAsupport =       *( ((<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>*) buffer) + ATA_IDENT_FIELDVALID );00070     ataDriveInfo.sizeinsectors =    *( (<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>*) (buffer + ATA_IDENT_LBASECTORS*2) );00071     <span class="comment">// copy model string</span>00072     <span class="keywordflow">for</span>(i=0; i&lt;40; i+=2)00073     {00074         <span class="comment">// correct for byte order</span>00075         ataDriveInfo.model[i  ] = buffer[(ATA_IDENT_MODEL*2) + i + 1];00076         ataDriveInfo.model[i+1] = buffer[(ATA_IDENT_MODEL*2) + i    ];00077     }00078     <span class="comment">// terminate string</span>00079     ataDriveInfo.model[40] = 0;00080 00081     <span class="comment">// process and print info</span>00082     <span class="keywordflow">if</span>(ataDriveInfo.LBAsupport)00083     {00084         <span class="comment">// LBA support</span>00085         rprintf(<span class="stringliteral">"Drive 0: %dMB "</span>, ataDriveInfo.sizeinsectors/(1000000/512) );00086         rprintf(<span class="stringliteral">"LBA mode -- MODEL: "</span>);00087     }00088     <span class="keywordflow">else</span>00089     {00090         <span class="comment">// CHS, no LBA support</span>00091         <span class="comment">// calculate drive size</span>00092         ataDriveInfo.sizeinsectors = (<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>) ataDriveInfo.cylinders*00093                                                 ataDriveInfo.heads*ataDriveInfo.sectors;00094         rprintf(<span class="stringliteral">"Drive 0: %dMB "</span>, ataDriveInfo.sizeinsectors/(1000000/512) );00095         rprintf(<span class="stringliteral">"CHS mode C=%d H=%d S=%d -- MODEL: "</span>, ataDriveInfo.cylinders, ataDriveInfo.heads, ataDriveInfo.sectors );00096     }00097     <span class="comment">// print model information  </span>00098     <a class="code" href="rprintf_8h.html#a8">rprintfStr</a>(ataDriveInfo.model); <a class="code" href="rprintf_8c.html#a9">rprintfCRLF</a>();00099 00100     <span class="comment">// initialize local disk parameters</span>00101     <span class="comment">//ataDriveInfo.cylinders = ATA_DISKPARM_CLYS;</span>00102     <span class="comment">//ataDriveInfo.heads = ATA_DISKPARM_HEADS;</span>00103     <span class="comment">//ataDriveInfo.sectors = ATA_DISKPARM_SECTORS;</span>00104 00105 }00106 00107 <span class="keywordtype">void</span> ataDiskErr(<span class="keywordtype">void</span>)00108 {00109     <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> b;00110 00111     b = ataReadByte(ATA_REG_ERROR); 00112     rprintfProgStrM(<span class="stringliteral">"ATA Error: "</span>); 00113     <a class="code" href="rprintf_8h.html#a13">rprintfu08</a>(b); 00114     <a class="code" href="rprintf_8c.html#a9">rprintfCRLF</a>();00115 }00116 00117 <span class="keywordtype">void</span> ataSetDrivePowerMode(u08 DriveNo, u08 mode, u08 timeout)00118 {00119     <span class="comment">// select drive</span>00120     ataDriveSelect(DriveNo);00121     <span class="comment">// Wait for drive to be ready</span>00122     ataStatusWait(ATA_SR_BSY, ATA_SR_BSY);00123 00124     <span class="comment">// set mode</span>00125     <span class="keywordflow">switch</span>(mode)00126     {00127     <span class="keywordflow">case</span> ATA_DISKMODE_SPINDOWN:     ataWriteByte(ATA_REG_CMDSTATUS1, ATA_CMD_SPINDOWN); <span class="keywordflow">break</span>;00128     <span class="keywordflow">case</span> ATA_DISKMODE_SPINUP:       ataWriteByte(ATA_REG_CMDSTATUS1, ATA_CMD_SPINUP); <span class="keywordflow">break</span>;00129     <span class="keywordflow">case</span> ATA_DISKMODE_SETTIMEOUT:00130         ataWriteByte(ATA_REG_SECCOUNT, timeout);00131         ataWriteByte(ATA_REG_CMDSTATUS1, ATA_CMD_IDLE_5SU);00132         <span class="keywordflow">break</span>;00133     <span class="keywordflow">case</span> ATA_DISKMODE_SLEEP:        ataWriteByte(ATA_REG_CMDSTATUS1, ATA_CMD_SLEEP); <span class="keywordflow">break</span>;00134     <span class="keywordflow">default</span>:00135         <span class="keywordflow">break</span>;00136     }00137 }00138 00139 <span class="keywordtype">void</span> ataPrintSector( u08 *Buffer)00140 {00141     u08 i;00142     u16 j;00143     u08 *buf;00144     u08 s;00145 00146     buf = Buffer;00147     00148     <span class="comment">// print the low order address indicies</span>00149     rprintfProgStrM(<span class="stringliteral">"     00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F  0123456789ABCDEF\r\n"</span>);00150     rprintfProgStrM(<span class="stringliteral">"     -----------------------------------------------  ---- ASCII -----\r\n"</span>);00151     00152     <span class="comment">// print the data</span>00153     <span class="keywordflow">for</span>(j=0; j&lt;0x20; j++)00154     {00155         <span class="comment">// print the high order address index for this line</span>00156         <a class="code" href="rprintf_8h.html#a14">rprintfu16</a>(j&lt;&lt;4);00157         rprintfProgStrM(<span class="stringliteral">" "</span>);00158 00159         <span class="comment">// print the hex data</span>00160         <span class="keywordflow">for</span>(i=0; i&lt;0x10; i++)00161         {00162             <a class="code" href="rprintf_8h.html#a13">rprintfu08</a>(buf[(j&lt;&lt;4)+i]);00163             rprintfProgStrM(<span class="stringliteral">" "</span>);00164         }00165         00166         <span class="comment">// leave some space</span>00167         rprintfProgStrM(<span class="stringliteral">" "</span>);00168 00169         <span class="comment">// print the ascii data</span>00170         <span class="keywordflow">for</span>(i=0; i&lt;0x10; i++)00171         {00172             s = buf[(j&lt;&lt;4)+i]; 00173             <span class="comment">// make sure character is printable</span>00174             <span class="keywordflow">if</span>(s &gt;= 0x20)00175             {00176                 <a class="code" href="rprintf_8h.html#a7">rprintfChar</a>(s);00177             }00178             <span class="keywordflow">else</span>00179             {00180                 <a class="code" href="rprintf_8h.html#a7">rprintfChar</a>(0x20);00181             }00182 00183         }00184         <a class="code" href="rprintf_8c.html#a9">rprintfCRLF</a>();00185     }00186 }00187 00188 <span class="keywordtype">void</span> ataReadDataBuffer(u08 *Buffer, u16 numBytes)00189 {00190     <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i;00191 00192     <span class="comment">//sbi(MCUCR, SRW);          // enable RAM waitstate</span>00193 00194     <span class="comment">// read data from drive</span>00195     <span class="keywordflow">for</span> (i=0; i&lt;(numBytes/16); i++)00196     {00197         <span class="comment">// optimize by reading 16 bytes in-line before looping</span>00198         *Buffer++ = *((<span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>*) ATA_REG_BASE + ATA_REG_DATAL);00199         *Buffer++ = *((<span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>*) ATA_REG_BASE + ATA_REG_DATAH);00200         *Buffer++ = *((<span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>*) ATA_REG_BASE + ATA_REG_DATAL);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -