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

📄 rtl8019_8c-source.html

📁 avr cpu 库源代码 对avr单片机编程很有帮助
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!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: net/rtl8019.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><div class="nav"><a class="el" href="dir_000001.html">net</a></div><h1>rtl8019.c</h1><a href="rtl8019_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file rtl8019.c \brief Realtek RTL8019AS Ethernet Interface Driver. */</span>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name    : 'rtl8019.c'</span>00005 <span class="comment">// Title        : Realtek RTL8019AS Ethernet Interface Driver</span>00006 <span class="comment">// Author       : Pascal Stang</span>00007 <span class="comment">// Created      : 7/6/2004</span>00008 <span class="comment">// Revised      : 10/1/2005</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">// Based in part on code by Louis Beaudoin (www.embedded-creations.com).</span>00014 <span class="comment">// Thanks to Adam Dunkels and Louis Beaudoin for providing the initial</span>00015 <span class="comment">// structure in which to write this driver.</span>00016 <span class="comment">//*****************************************************************************</span>00017 00018 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>00019 <span class="preprocessor">#include "<a class="code" href="timer_8h.html">timer.h</a>"</span>00020 <span class="preprocessor">#include "<a class="code" href="rprintf_8h.html">rprintf.h</a>"</span>00021 00022 <span class="preprocessor">#include "<a class="code" href="rtl8019_8h.html">rtl8019.h</a>"</span>00023 00024 <span class="comment">// include configuration</span>00025 <span class="preprocessor">#include "<a class="code" href="rtl8019conf_8h.html">rtl8019conf.h</a>"</span>00026 00027 <span class="comment">// pointers to locations in the RTL8019 receive buffer</span>00028 <span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> NextPage;              <span class="comment">// page pointer to next Rx packet</span>00029 <span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> CurrentRetreiveAddress; <span class="comment">// DMA address for read Rx packet location</span>00030 00031 <a name="l00032"></a><a class="code" href="group__nic.html#ga0">00032</a> <span class="keywordtype">void</span> <a class="code" href="group__nic.html#ga0">nicInit</a>(<span class="keywordtype">void</span>)00033 {00034     rtl8019Init();00035 }00036 <a name="l00037"></a><a class="code" href="group__nic.html#ga1">00037</a> <span class="keywordtype">void</span> <a class="code" href="group__nic.html#ga1">nicSend</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> len, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* packet)00038 {00039     rtl8019BeginPacketSend(len);00040     rtl8019SendPacketData(packet, len);00041     rtl8019EndPacketSend();00042 }00043 <a name="l00044"></a><a class="code" href="group__nic.html#ga2">00044</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="group__nic.html#ga2">nicPoll</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> maxlen, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* packet)00045 {00046     <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> packetLength;00047     00048     packetLength = rtl8019BeginPacketRetreive();00049 00050     <span class="comment">// if there's no packet or an error - exit without ending the operation</span>00051     <span class="keywordflow">if</span>( !packetLength )00052         <span class="keywordflow">return</span> 0;00053 00054     <span class="comment">// drop anything too big for the buffer</span>00055     <span class="keywordflow">if</span>( packetLength &gt; maxlen )00056     {00057         rtl8019EndPacketRetreive();00058         <span class="keywordflow">return</span> 0;00059     }00060     00061     <span class="comment">// copy the packet data into the packet buffer</span>00062     rtl8019RetreivePacketData( packet, packetLength );00063     rtl8019EndPacketRetreive();00064         00065     <span class="keywordflow">return</span> packetLength;00066 }00067 00068 <span class="keywordtype">void</span> nicGetMacAddress(u08* macaddr)00069 {00070     u08 tempCR;00071     <span class="comment">// switch register pages</span>00072     tempCR = rtl8019Read(CR);00073     rtl8019Write(CR,tempCR|PS0);00074     <span class="comment">// read MAC address registers</span>00075     *macaddr++ = rtl8019Read(PAR0);00076     *macaddr++ = rtl8019Read(PAR1);00077     *macaddr++ = rtl8019Read(PAR2);00078     *macaddr++ = rtl8019Read(PAR3);00079     *macaddr++ = rtl8019Read(PAR4);00080     *macaddr++ = rtl8019Read(PAR5);00081     <span class="comment">// switch register pages back</span>00082     rtl8019Write(CR,tempCR);00083 }00084 00085 <span class="keywordtype">void</span> nicSetMacAddress(u08* macaddr)00086 {00087     u08 tempCR;00088     <span class="comment">// switch register pages</span>00089     tempCR = rtl8019Read(CR);00090     rtl8019Write(CR,tempCR|PS0);00091     <span class="comment">// write MAC address registers</span>00092     rtl8019Write(PAR0, *macaddr++);00093     rtl8019Write(PAR1, *macaddr++);00094     rtl8019Write(PAR2, *macaddr++);00095     rtl8019Write(PAR3, *macaddr++);00096     rtl8019Write(PAR4, *macaddr++);00097     rtl8019Write(PAR5, *macaddr++);00098     <span class="comment">// switch register pages back</span>00099     rtl8019Write(CR,tempCR);00100 }00101 <a name="l00102"></a><a class="code" href="group__nic.html#ga5">00102</a> <span class="keywordtype">void</span> <a class="code" href="group__nic.html#ga5">nicRegDump</a>(<span class="keywordtype">void</span>)00103 {00104     rtl8019RegDump();00105 }00106 00107 00108 <span class="keywordtype">void</span> rtl8019SetupPorts(<span class="keywordtype">void</span>)00109 {00110 <span class="preprocessor">#if NIC_CONNECTION == MEMORY_MAPPED</span>00111 <span class="preprocessor"></span>    <span class="comment">// enable external SRAM interface - no wait states</span>00112     sbi(MCUCR, SRE);00113 <span class="comment">//  sbi(MCUCR, SRW10);</span>00114 <span class="comment">//  sbi(XMCRA, SRW00);</span>00115 <span class="comment">//  sbi(XMCRA, SRW01);</span>00116 <span class="comment">//  sbi(XMCRA, SRW11);</span>00117 <span class="preprocessor">#else</span>00118 <span class="preprocessor"></span>    <span class="comment">// make the address port output</span>00119     RTL8019_ADDRESS_DDR |= RTL8019_ADDRESS_MASK;00120     <span class="comment">// make the data port input with pull-ups</span>00121     RTL8019_DATA_PORT = 0xFF;00122 00123     <span class="comment">// initialize the control port read and write pins to de-asserted</span>00124     RTL8019_CONTROL_DDR |= (1&lt;&lt;RTL8019_CONTROL_READPIN);00125     RTL8019_CONTROL_DDR |= (1&lt;&lt;RTL8019_CONTROL_WRITEPIN);00126     <span class="comment">// set the read and write pins to output</span>00127     RTL8019_CONTROL_PORT |= (1&lt;&lt;RTL8019_CONTROL_READPIN);00128     RTL8019_CONTROL_PORT |= (1&lt;&lt;RTL8019_CONTROL_WRITEPIN);00129 <span class="preprocessor">#endif</span>00130 <span class="preprocessor"></span>    <span class="comment">// set reset pin to output</span>00131     sbi(RTL8019_RESET_DDR, RTL8019_RESET_PIN);00132 }00133 00134 00135 <span class="preprocessor">#if NIC_CONNECTION == MEMORY_MAPPED</span>00136 <span class="preprocessor"></span><span class="keyword">inline</span> <span class="keywordtype">void</span> rtl8019Write(u08 address, u08 data)00137 {00138     *(<span class="keyword">volatile</span> u08*)(RTL8019_MEMORY_MAPPED_OFFSET + address) = data;00139 }00140 <span class="preprocessor">#else</span>00141 <span class="preprocessor"></span><span class="keywordtype">void</span> rtl8019Write(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> address, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> data)00142 {00143     <span class="comment">// assert the address</span>00144     RTL8019_ADDRESS_PORT = address | (RTL8019_ADDRESS_PORT&amp;~RTL8019_ADDRESS_MASK);00145     <span class="comment">// set data bus as output and place data on bus</span>00146     RTL8019_DATA_DDR = 0xFF;00147     RTL8019_DATA_PORT = data;00148     <span class="comment">// clock write pin</span>00149     cbi(RTL8019_CONTROL_PORT, RTL8019_CONTROL_WRITEPIN);00150     nop();00151     nop();00152     sbi(RTL8019_CONTROL_PORT, RTL8019_CONTROL_WRITEPIN);00153     <span class="comment">// set data port back to input with pullups enabled</span>00154     RTL8019_DATA_DDR = 0x00;00155     RTL8019_DATA_PORT = 0xFF;00156 }00157 <span class="preprocessor">#endif</span>00158 <span class="preprocessor"></span>00159 00160 <span class="preprocessor">#if NIC_CONNECTION == MEMORY_MAPPED</span>00161 <span class="preprocessor"></span><span class="keyword">inline</span> u08 ax88796Read(u08 address)00162 {00163     <span class="keywordflow">return</span> *(<span class="keyword">volatile</span> u08*)(RTL8019_MEMORY_MAPPED_OFFSET + address);00164 }00165 <span class="preprocessor">#else</span>00166 <span class="preprocessor"></span><span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> rtl8019Read(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> address)00167 {00168     <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> data;00169    00170     <span class="comment">// assert the address</span>00171     RTL8019_ADDRESS_PORT = address | (RTL8019_ADDRESS_PORT&amp;~RTL8019_ADDRESS_MASK);00172     <span class="comment">// assert read</span>00173     cbi(RTL8019_CONTROL_PORT, RTL8019_CONTROL_READPIN);00174     nop();00175     nop();00176     <span class="comment">// read in the data</span>00177     data = RTL8019_DATA_PIN;00178     <span class="comment">// negate read</span>00179     sbi(RTL8019_CONTROL_PORT, RTL8019_CONTROL_READPIN);00180     <span class="comment">// return data</span>00181     <span class="keywordflow">return</span> data;00182 }00183 <span class="preprocessor">#endif                       </span>00184 <span class="preprocessor"></span>00185 00186 <span class="keywordtype">void</span> rtl8019Init(<span class="keywordtype">void</span>)00187 {00188     <span class="comment">// setup I/O ports</span>00189     rtl8019SetupPorts();00190     00191     <span class="comment">// do a hard reset</span>00192     sbi(RTL8019_RESET_PORT, RTL8019_RESET_PIN);00193     delay_ms(10);00194     cbi(RTL8019_RESET_PORT, RTL8019_RESET_PIN);00195 00196     <span class="comment">// clear interrupt state</span>00197     rtl8019Write( ISR, rtl8019Read(ISR) );00198     delay_ms(50);00199 00200     <span class="comment">// switch to page 3 to load config registers</span>00201     rtl8019Write(CR, (PS0|PS1|RD2|STOP));00202 00203     <span class="comment">// disable EEPROM write protect of config registers</span>00204     rtl8019Write(RTL_EECR, (EEM1|EEM0));00205 00206     <span class="comment">// set network type to 10 Base-T link test</span>00207     rtl8019Write(CONFIG2, 0x20);00208 00209     <span class="comment">// disable powerdown and sleep</span>00210     rtl8019Write(CONFIG3, 0);00211     delay_ms(255);00212 00213     <span class="comment">// reenable EEPROM write protect</span>00214     rtl8019Write(RTL_EECR, 0);00215 00216     <span class="comment">// go back to page 0, stop NIC, abort DMA</span>00217     rtl8019Write(CR, (RD2|STOP));00218     delay_ms(2);                    <span class="comment">// wait for traffic to complete</span>00219     rtl8019Write(DCR, DCR_INIT);00220     rtl8019Write(RBCR0,0x00);00221     rtl8019Write(RBCR1,0x00);00222     rtl8019Write(RCR, AB);00223     rtl8019Write(TPSR, TXSTART_INIT);00224     rtl8019Write(TCR, LB0);00225     rtl8019Write(PSTART, RXSTART_INIT);00226     rtl8019Write(BNRY, RXSTART_INIT);00227     rtl8019Write(PSTOP, RXSTOP_INIT);00228     rtl8019Write(CR, (PS0|RD2|STOP));   <span class="comment">// switch to page 1</span>00229     delay_ms(2);00230     rtl8019Write(CPR, RXSTART_INIT);00231     00232     <span class="comment">// set MAC address</span>00233     rtl8019Write(PAR0, RTL8019_MAC0);00234     rtl8019Write(PAR1, RTL8019_MAC1);00235     rtl8019Write(PAR2, RTL8019_MAC2);00236     rtl8019Write(PAR3, RTL8019_MAC3);00237     rtl8019Write(PAR4, RTL8019_MAC4);00238     rtl8019Write(PAR5, RTL8019_MAC5);00239     00240     <span class="comment">// initialize sequence per NE2000 spec</span>00241     rtl8019Write(CR, (RD2|STOP));00242     rtl8019Write(DCR, DCR_INIT);00243     rtl8019Write(CR, (RD2|START));00244     rtl8019Write(ISR,0xFF);         <span class="comment">// clear all interrupts</span>00245     rtl8019Write(IMR, IMR_INIT);00246     rtl8019Write(TCR, TCR_INIT);00247     00248     rtl8019Write(CR, (RD2|START));  <span class="comment">// start the NIC</span>00249 }00250 00251 00252 <span class="keywordtype">void</span> rtl8019BeginPacketSend(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> packetLength)00253 {00254     <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> sendPacketLength;00255     sendPacketLength = (packetLength&gt;=ETHERNET_MIN_PACKET_LENGTH)?00256                         (packetLength):ETHERNET_MIN_PACKET_LENGTH;00257     00258     <span class="comment">//start the NIC</span>00259     rtl8019Write(CR, (RD2|START));00260     

⌨️ 快捷键说明

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