📄 ata_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 ARMlib-LPC2100: 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 Page</a> | <a class="qindex" href="files.html">File List</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 : 4/2/2004</span>00008 <span class="comment">// Revised : 4/2/2004</span>00009 <span class="comment">// Version : 0.1</span>00010 <span class="comment">// Target MCU : ARMmini LPC210x</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">#include "<a class="code" href="global_8h.html">global.h</a>"</span>00023 <span class="preprocessor">#include "<a class="code" href="timer_8h.html">timer.h</a>"</span>00024 <span class="preprocessor">#include "<a class="code" href="rprintf_8h.html">rprintf.h</a>"</span>00025 00026 <span class="preprocessor">#include "<a class="code" href="ata_8h.html">ata.h</a>"</span>00027 00028 <span class="comment">// global variables</span>00029 00030 <span class="comment">// drive information</span>00031 typeDriveInfo ataDriveInfo;00032 00033 <span class="keywordtype">void</span> ataInit(<span class="keywordtype">void</span>)00034 {00035 00036 }00037 00038 <span class="keywordtype">void</span> ataDriveInit(<span class="keywordtype">void</span>)00039 {00040 u08 i;00041 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* buffer = (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>*)SECTOR_BUFFER_ADDR;00042 00043 <span class="comment">// read drive identity</span>00044 <a class="code" href="rprintf_8h.html#a8">rprintfStr</a>(<span class="stringliteral">"\r\nScanning IDE interface...\r\n"</span>);00045 <span class="comment">// Wait for drive to be ready</span>00046 ataStatusWait(ATA_SR_BSY, ATA_SR_BSY);00047 <span class="comment">// issue identify command</span>00048 ataWriteByte(ATA_REG_CMDSTATUS1, 0xEC);00049 <span class="comment">// wait for drive to request data transfer</span>00050 ataStatusWait(ATA_SR_DRQ, ATA_SR_DRQ);00051 timerPause(200);00052 <span class="comment">// read in the data</span>00053 ataReadDataBuffer(buffer, 512);00054 00055 <span class="comment">// set local drive info parameters</span>00056 ataDriveInfo.cylinders = *( ((<span class="keywordtype">unsigned</span> <span class="keywordtype">short</span>*) buffer) + ATA_IDENT_CYLINDERS );00057 ataDriveInfo.heads = *( ((<span class="keywordtype">unsigned</span> <span class="keywordtype">short</span>*) buffer) + ATA_IDENT_HEADS );00058 ataDriveInfo.sectors = *( ((<span class="keywordtype">unsigned</span> <span class="keywordtype">short</span>*) buffer) + ATA_IDENT_SECTORS );00059 ataDriveInfo.LBAsupport = *( ((<span class="keywordtype">unsigned</span> <span class="keywordtype">short</span>*) buffer) + ATA_IDENT_FIELDVALID );00060 ataDriveInfo.sizeinsectors = *( (<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>*) (buffer + ATA_IDENT_LBASECTORS*2) );00061 <span class="comment">// copy model string</span>00062 <span class="keywordflow">for</span>(i=0; i<40; i+=2)00063 {00064 <span class="comment">// correct for byte order</span>00065 ataDriveInfo.model[i ] = buffer[(ATA_IDENT_MODEL*2) + i + 1];00066 ataDriveInfo.model[i+1] = buffer[(ATA_IDENT_MODEL*2) + i ];00067 }00068 <span class="comment">// terminate string</span>00069 ataDriveInfo.model[40] = 0;00070 00071 <span class="comment">// process and print info</span>00072 <span class="keywordflow">if</span>(ataDriveInfo.LBAsupport)00073 {00074 <span class="comment">// LBA support</span>00075 rprintf(<span class="stringliteral">"Drive 0: %dMB "</span>, ataDriveInfo.sizeinsectors/(1000000/512) );00076 <a class="code" href="rprintf_8h.html#a8">rprintfStr</a>(<span class="stringliteral">"LBA mode -- MODEL: "</span>);00077 }00078 <span class="keywordflow">else</span>00079 {00080 <span class="comment">// CHS, no LBA support</span>00081 <span class="comment">// calculate drive size</span>00082 ataDriveInfo.sizeinsectors = (<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>) ataDriveInfo.cylinders*00083 ataDriveInfo.heads*ataDriveInfo.sectors;00084 rprintf(<span class="stringliteral">"Drive 0: %dMB "</span>, ataDriveInfo.sizeinsectors/(1000000/512) );00085 rprintf(<span class="stringliteral">"CHS mode C=%d H=%d S=%d -- MODEL: "</span>, ataDriveInfo.cylinders, ataDriveInfo.heads, ataDriveInfo.sectors );00086 }00087 <span class="comment">// print model information </span>00088 <a class="code" href="rprintf_8h.html#a8">rprintfStr</a>(ataDriveInfo.model); <a class="code" href="rprintf_8c.html#a10">rprintfCRLF</a>();00089 00090 <span class="comment">// initialize local disk parameters</span>00091 <span class="comment">//ataDriveInfo.cylinders = ATA_DISKPARM_CLYS;</span>00092 <span class="comment">//ataDriveInfo.heads = ATA_DISKPARM_HEADS;</span>00093 <span class="comment">//ataDriveInfo.sectors = ATA_DISKPARM_SECTORS;</span>00094 00095 }00096 00097 <span class="keywordtype">void</span> ataDiskErr(<span class="keywordtype">void</span>)00098 {00099 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> b;00100 00101 b = ataReadByte(ATA_REG_ERROR); 00102 <a class="code" href="rprintf_8h.html#a8">rprintfStr</a>(<span class="stringliteral">"ATA Error: "</span>); 00103 <a class="code" href="rprintf_8h.html#a13">rprintfu08</a>(b); 00104 <a class="code" href="rprintf_8c.html#a10">rprintfCRLF</a>();00105 }00106 00107 <span class="keywordtype">void</span> ataSetDrivePowerMode(u08 DriveNo, u08 mode, u08 timeout)00108 {00109 <span class="comment">// select drive</span>00110 ataDriveSelect(DriveNo);00111 <span class="comment">// Wait for drive to be ready</span>00112 ataStatusWait(ATA_SR_BSY, ATA_SR_BSY);00113 00114 <span class="comment">// set mode</span>00115 <span class="keywordflow">switch</span>(mode)00116 {00117 <span class="keywordflow">case</span> ATA_DISKMODE_SPINDOWN: ataWriteByte(ATA_REG_CMDSTATUS1, ATA_CMD_SPINDOWN); <span class="keywordflow">break</span>;00118 <span class="keywordflow">case</span> ATA_DISKMODE_SPINUP: ataWriteByte(ATA_REG_CMDSTATUS1, ATA_CMD_SPINUP); <span class="keywordflow">break</span>;00119 <span class="keywordflow">case</span> ATA_DISKMODE_SETTIMEOUT:00120 ataWriteByte(ATA_REG_SECCOUNT, timeout);00121 ataWriteByte(ATA_REG_CMDSTATUS1, ATA_CMD_IDLE_5SU);00122 <span class="keywordflow">break</span>;00123 <span class="keywordflow">case</span> ATA_DISKMODE_SLEEP: ataWriteByte(ATA_REG_CMDSTATUS1, ATA_CMD_SLEEP); <span class="keywordflow">break</span>;00124 <span class="keywordflow">default</span>:00125 <span class="keywordflow">break</span>;00126 }00127 }00128 00129 <span class="keywordtype">void</span> ataPrintSector( u08 *Buffer)00130 {00131 u08 i;00132 u16 j;00133 u08 *buf;00134 u08 s;00135 00136 buf = Buffer;00137 00138 <span class="comment">// print the low order address indicies</span>00139 <a class="code" href="rprintf_8h.html#a8">rprintfStr</a>(<span class="stringliteral">" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 0123456789ABCDEF\r\n"</span>);00140 <a class="code" href="rprintf_8h.html#a8">rprintfStr</a>(<span class="stringliteral">" ----------------------------------------------- ---- ASCII -----\r\n"</span>);00141 00142 <span class="comment">// print the data</span>00143 <span class="keywordflow">for</span>(j=0; j<0x20; j++)00144 {00145 <span class="comment">// print the high order address index for this line</span>00146 <a class="code" href="rprintf_8h.html#a14">rprintfu16</a>(j<<4);00147 <a class="code" href="rprintf_8h.html#a8">rprintfStr</a>(<span class="stringliteral">" "</span>);00148 00149 <span class="comment">// print the hex data</span>00150 <span class="keywordflow">for</span>(i=0; i<0x10; i++)00151 {00152 <a class="code" href="rprintf_8h.html#a13">rprintfu08</a>(buf[(j<<4)+i]);00153 <a class="code" href="rprintf_8h.html#a8">rprintfStr</a>(<span class="stringliteral">" "</span>);00154 }00155 00156 <span class="comment">// leave some space</span>00157 <a class="code" href="rprintf_8h.html#a8">rprintfStr</a>(<span class="stringliteral">" "</span>);00158 00159 <span class="comment">// print the ascii data</span>00160 <span class="keywordflow">for</span>(i=0; i<0x10; i++)00161 {00162 s = buf[(j<<4)+i]; 00163 <span class="comment">// make sure character is printable</span>00164 <span class="keywordflow">if</span>(s >= 0x20)00165 {00166 <a class="code" href="rprintf_8h.html#a7">rprintfChar</a>(s);00167 }00168 <span class="keywordflow">else</span>00169 {00170 <a class="code" href="rprintf_8h.html#a7">rprintfChar</a>(0x20);00171 }00172 00173 }00174 <a class="code" href="rprintf_8c.html#a10">rprintfCRLF</a>();00175 }00176 }00177 00178 <span class="keywordtype">void</span> ataReadDataBuffer(u08 *Buffer, u16 numBytes)00179 {00180 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i;00181 00182 <span class="comment">//sbi(MCUCR, SRW); // enable RAM waitstate</span>00183 00184 uint16_t* buf = (uint16_t*)Buffer;00185 00186 <span class="comment">// read data from drive</span>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -