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

📄 ata_8c-source.html

📁 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.4.2 --><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="dirs.html">Directories</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> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a></div><h1>ata.c</h1><a href="ata_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">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/pgmspace.h&gt;</span>00026 <span class="comment">//  #include &lt;stdio.h&gt;</span>00027 <span class="preprocessor">#endif</span>00028 <span class="preprocessor"></span><span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>00029 <span class="preprocessor">#include "<a class="code" href="timer_8h.html">timer.h</a>"</span>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="ata_8h.html">ata.h</a>"</span>00033 00034 <span class="comment">//#define DEBUG_ATA 1</span>00035 00036 <span class="comment">// global variables</span>00037 00038 <span class="comment">// drive information</span>00039 typeDriveInfo ataDriveInfo;00040 00041 00042 <span class="keywordtype">void</span> ataInit(<span class="keywordtype">void</span>)00043 {00044 00045 }00046 00047 <span class="keywordtype">void</span> ataDriveInit(<span class="keywordtype">void</span>)00048 {00049     u08 i;00050     <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* buffer = (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>*) SECTOR_BUFFER_ADDR;00051 00052     <span class="comment">// read drive identity</span>00053     <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"\r\nScanning IDE interface...\r\n"</span>);00054     <span class="comment">// Wait for drive to be ready</span>00055     ataStatusWait(ATA_SR_BSY, ATA_SR_BSY);00056     <span class="comment">// issue identify command</span>00057     ataWriteByte(ATA_REG_CMDSTATUS1, 0xEC);00058     <span class="comment">// wait for drive to request data transfer</span>00059     ataStatusWait(ATA_SR_DRQ, ATA_SR_DRQ);00060     <a class="code" href="group__timer.html#ga10">timerPause</a>(200);00061     <span class="comment">// read in the data</span>00062     ataReadDataBuffer(buffer, 512);00063 00064     <span class="comment">// set local drive info parameters</span>00065     ataDriveInfo.cylinders =        *( ((<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>*) buffer) + ATA_IDENT_CYLINDERS );00066     ataDriveInfo.heads =            *( ((<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>*) buffer) + ATA_IDENT_HEADS );00067     ataDriveInfo.sectors =          *( ((<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>*) buffer) + ATA_IDENT_SECTORS );00068     ataDriveInfo.LBAsupport =       *( ((<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>*) buffer) + ATA_IDENT_FIELDVALID );00069     ataDriveInfo.sizeinsectors =    *( (<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>*) (buffer + ATA_IDENT_LBASECTORS*2) );00070     <span class="comment">// copy model string</span>00071     <span class="keywordflow">for</span>(i=0; i&lt;40; i+=2)00072     {00073         <span class="comment">// correct for byte order</span>00074         ataDriveInfo.model[i  ] = buffer[(ATA_IDENT_MODEL*2) + i + 1];00075         ataDriveInfo.model[i+1] = buffer[(ATA_IDENT_MODEL*2) + i    ];00076     }00077     <span class="comment">// terminate string</span>00078     ataDriveInfo.model[40] = 0;00079 00080     <span class="comment">// process and print info</span>00081     <span class="keywordflow">if</span>(ataDriveInfo.LBAsupport)00082     {00083         <span class="comment">// LBA support</span>00084         rprintf(<span class="stringliteral">"Drive 0: %dMB "</span>, ataDriveInfo.sizeinsectors/(1000000/512) );00085         rprintf(<span class="stringliteral">"LBA mode -- MODEL: "</span>);00086     }00087     <span class="keywordflow">else</span>00088     {00089         <span class="comment">// CHS, no LBA support</span>00090         <span class="comment">// calculate drive size</span>00091         ataDriveInfo.sizeinsectors = (<span class="keywordtype">unsigned</span> long) ataDriveInfo.cylinders*00092                                                 ataDriveInfo.heads*ataDriveInfo.sectors;00093         rprintf(<span class="stringliteral">"Drive 0: %dMB "</span>, ataDriveInfo.sizeinsectors/(1000000/512) );00094         rprintf(<span class="stringliteral">"CHS mode C=%d H=%d S=%d -- MODEL: "</span>, ataDriveInfo.cylinders, ataDriveInfo.heads, ataDriveInfo.sectors );00095     }00096     <span class="comment">// print model information  </span>00097     <a class="code" href="group__rprintf.html#ga2">rprintfStr</a>(ataDriveInfo.model); <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>();00098 00099     <span class="comment">// initialize local disk parameters</span>00100     <span class="comment">//ataDriveInfo.cylinders = ATA_DISKPARM_CLYS;</span>00101     <span class="comment">//ataDriveInfo.heads = ATA_DISKPARM_HEADS;</span>00102     <span class="comment">//ataDriveInfo.sectors = ATA_DISKPARM_SECTORS;</span>00103 00104 }00105 00106 <span class="keywordtype">void</span> ataDiskErr(<span class="keywordtype">void</span>)00107 {00108     <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> b;00109 00110     b = ataReadByte(ATA_REG_ERROR); 00111     <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"ATA Error: "</span>); 00112     <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(b); 00113     <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>();00114 }00115 00116 <span class="keywordtype">void</span> ataSetDrivePowerMode(u08 DriveNo, u08 mode, u08 timeout)00117 {00118     <span class="comment">// select drive</span>00119     ataDriveSelect(DriveNo);00120     <span class="comment">// Wait for drive to be ready</span>00121     ataStatusWait(ATA_SR_BSY, ATA_SR_BSY);00122 00123     <span class="comment">// set mode</span>00124     <span class="keywordflow">switch</span>(mode)00125     {00126     <span class="keywordflow">case</span> ATA_DISKMODE_SPINDOWN:     ataWriteByte(ATA_REG_CMDSTATUS1, ATA_CMD_SPINDOWN); <span class="keywordflow">break</span>;00127     <span class="keywordflow">case</span> ATA_DISKMODE_SPINUP:       ataWriteByte(ATA_REG_CMDSTATUS1, ATA_CMD_SPINUP); <span class="keywordflow">break</span>;00128     <span class="keywordflow">case</span> ATA_DISKMODE_SETTIMEOUT:00129         ataWriteByte(ATA_REG_SECCOUNT, timeout);00130         ataWriteByte(ATA_REG_CMDSTATUS1, ATA_CMD_IDLE_5SU);00131         <span class="keywordflow">break</span>;00132     <span class="keywordflow">case</span> ATA_DISKMODE_SLEEP:        ataWriteByte(ATA_REG_CMDSTATUS1, ATA_CMD_SLEEP); <span class="keywordflow">break</span>;00133     <span class="keywordflow">default</span>:00134         <span class="keywordflow">break</span>;00135     }00136 }00137 00138 <span class="keywordtype">void</span> ataPrintSector( u08 *Buffer)00139 {00140     u08 i;00141     u16 j;00142     u08 *buf;00143     u08 s;00144 00145     buf = Buffer;00146     00147     <span class="comment">// print the low order address indicies</span>00148     <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"     00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F  0123456789ABCDEF\r\n"</span>);00149     <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"     -----------------------------------------------  ---- ASCII -----\r\n"</span>);00150     00151     <span class="comment">// print the data</span>00152     <span class="keywordflow">for</span>(j=0; j&lt;0x20; j++)00153     {00154         <span class="comment">// print the high order address index for this line</span>00155         <a class="code" href="group__rprintf.html#ga8">rprintfu16</a>(j&lt;&lt;4);00156         <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>);00157 00158         <span class="comment">// print the hex data</span>00159         <span class="keywordflow">for</span>(i=0; i&lt;0x10; i++)00160         {00161             <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(buf[(j&lt;&lt;4)+i]);00162             <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>);00163         }00164         00165         <span class="comment">// leave some space</span>00166         <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>);00167 00168         <span class="comment">// print the ascii data</span>00169         <span class="keywordflow">for</span>(i=0; i&lt;0x10; i++)00170         {00171             s = buf[(j&lt;&lt;4)+i]; 00172             <span class="comment">// make sure character is printable</span>00173             <span class="keywordflow">if</span>(s &gt;= 0x20)00174             {00175                 <a class="code" href="group__rprintf.html#ga1">rprintfChar</a>(s);00176             }00177             <span class="keywordflow">else</span>00178             {00179                 <a class="code" href="group__rprintf.html#ga1">rprintfChar</a>(0x20);00180             }00181 00182         }00183         <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>();00184     }00185 }00186 00187 <span class="keywordtype">void</span> ataReadDataBuffer(u08 *Buffer, u16 numBytes)00188 {00189     <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i;00190 00191     <span class="comment">//sbi(MCUCR, SRW);          // enable RAM waitstate</span>00192 00193     <span class="comment">// read data from drive</span>00194     <span class="keywordflow">for</span> (i=0; i&lt;(numBytes/16); i++)00195     {00196         <span class="comment">// optimize by reading 16 bytes in-line before looping</span>00197         *Buffer++ = *((<span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>*) ATA_REG_BASE + ATA_REG_DATAL);00198         *Buffer++ = *((<span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>*) ATA_REG_BASE + ATA_REG_DATAH);00199         *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 + -