📄 rtl8019_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: 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 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></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 : 8/22/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">//*****************************************************************************</span>00014 00015 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>00016 <span class="preprocessor">#include "<a class="code" href="timer_8h.html">timer.h</a>"</span>00017 <span class="preprocessor">#include "<a class="code" href="rprintf_8h.html">rprintf.h</a>"</span>00018 00019 <span class="preprocessor">#include "<a class="code" href="rtl8019_8h.html">rtl8019.h</a>"</span>00020 00021 <span class="comment">// include configuration</span>00022 <span class="preprocessor">#include "<a class="code" href="rtl8019conf_8h.html">rtl8019conf.h</a>"</span>00023 00024 <span class="comment">// pointers to locations in the RTL8019 receive buffer</span>00025 <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>00026 <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>00027 00028 <a name="l00029"></a><a class="code" href="group__nic.html#ga0">00029</a> <span class="keywordtype">void</span> <a class="code" href="group__nic.html#ga0">nicInit</a>(<span class="keywordtype">void</span>)00030 {00031 rtl8019Init();00032 }00033 <a name="l00034"></a><a class="code" href="group__nic.html#ga1">00034</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)00035 {00036 rtl8019BeginPacketSend(len);00037 rtl8019SendPacketData(packet, len);00038 rtl8019EndPacketSend();00039 }00040 <a name="l00041"></a><a class="code" href="group__nic.html#ga2">00041</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)00042 {00043 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> packetLength;00044 00045 packetLength = rtl8019BeginPacketRetreive();00046 00047 <span class="comment">// if there's no packet or an error - exit without ending the operation</span>00048 <span class="keywordflow">if</span>( !packetLength )00049 <span class="keywordflow">return</span> 0;00050 00051 <span class="comment">// drop anything too big for the buffer</span>00052 <span class="keywordflow">if</span>( packetLength > maxlen )00053 {00054 rtl8019EndPacketRetreive();00055 <span class="keywordflow">return</span> 0;00056 }00057 00058 <span class="comment">// copy the packet data into the packet buffer</span>00059 rtl8019RetreivePacketData( packet, packetLength );00060 rtl8019EndPacketRetreive();00061 00062 <span class="keywordflow">return</span> packetLength;00063 }00064 <a name="l00065"></a><a class="code" href="group__nic.html#ga3">00065</a> <span class="keywordtype">void</span> <a class="code" href="group__nic.html#ga3">nicGetMacAddress</a>(u08* macaddr)00066 {00067 u08 tempCR;00068 <span class="comment">// switch register pages</span>00069 tempCR = rtl8019Read(CR);00070 rtl8019Write(CR,tempCR|PS0);00071 <span class="comment">// read MAC address registers</span>00072 *macaddr++ = rtl8019Read(PAR0);00073 *macaddr++ = rtl8019Read(PAR1);00074 *macaddr++ = rtl8019Read(PAR2);00075 *macaddr++ = rtl8019Read(PAR3);00076 *macaddr++ = rtl8019Read(PAR4);00077 *macaddr++ = rtl8019Read(PAR5);00078 <span class="comment">// switch register pages back</span>00079 rtl8019Write(CR,tempCR);00080 }00081 <a name="l00082"></a><a class="code" href="group__nic.html#ga4">00082</a> <span class="keywordtype">void</span> <a class="code" href="group__nic.html#ga4">nicRegDump</a>(<span class="keywordtype">void</span>)00083 {00084 rtl8019RegDump();00085 }00086 00087 00088 <span class="keywordtype">void</span> rtl8019SetupPorts(<span class="keywordtype">void</span>)00089 {00090 <span class="preprocessor">#if NIC_CONNECTION == MEMORY_MAPPED</span>00091 <span class="preprocessor"></span> <span class="comment">// enable external SRAM interface - no wait states</span>00092 sbi(MCUCR, SRE);00093 <span class="comment">// sbi(MCUCR, SRW10);</span>00094 <span class="comment">// sbi(XMCRA, SRW00);</span>00095 <span class="comment">// sbi(XMCRA, SRW01);</span>00096 <span class="comment">// sbi(XMCRA, SRW11);</span>00097 <span class="preprocessor">#else</span>00098 <span class="preprocessor"></span> <span class="comment">// make the address port output</span>00099 RTL8019_ADDRESS_DDR = RTL8019_ADDRESS_MASK;00100 <span class="comment">// make the data port input with pull-ups</span>00101 RTL8019_DATA_PORT = 0xFF;00102 00103 <span class="comment">// initialize the control port read and write pins to de-asserted</span>00104 RTL8019_CONTROL_DDR |= (1<<RTL8019_CONTROL_READPIN);00105 RTL8019_CONTROL_DDR |= (1<<RTL8019_CONTROL_WRITEPIN);00106 <span class="comment">// set the read and write pins to output</span>00107 RTL8019_CONTROL_PORT |= (1<<RTL8019_CONTROL_READPIN);00108 RTL8019_CONTROL_PORT |= (1<<RTL8019_CONTROL_WRITEPIN);00109 <span class="preprocessor">#endif</span>00110 <span class="preprocessor"></span> <span class="comment">// set reset pin to output</span>00111 sbi(RTL8019_RESET_DDR, RTL8019_RESET_PIN);00112 }00113 00114 00115 <span class="preprocessor">#if NIC_CONNECTION == MEMORY_MAPPED</span>00116 <span class="preprocessor"></span><span class="keyword">inline</span> <span class="keywordtype">void</span> rtl8019Write(u08 address, u08 data)00117 {00118 *(<span class="keyword">volatile</span> u08*)(RTL8019_MEMORY_MAPPED_OFFSET + address) = data;00119 }00120 <span class="preprocessor">#else</span>00121 <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)00122 {00123 <span class="comment">// assert the address</span>00124 RTL8019_ADDRESS_PORT = address | (RTL8019_ADDRESS_PORT&~RTL8019_ADDRESS_MASK);00125 <span class="comment">// set data bus as output and place data on bus</span>00126 RTL8019_DATA_DDR = 0xFF;00127 RTL8019_DATA_PORT = data;00128 <span class="comment">// clock write pin</span>00129 cbi(RTL8019_CONTROL_PORT, RTL8019_CONTROL_WRITEPIN);00130 nop();00131 nop();00132 sbi(RTL8019_CONTROL_PORT, RTL8019_CONTROL_WRITEPIN);00133 <span class="comment">// set data port back to input with pullups enabled</span>00134 RTL8019_DATA_DDR = 0x00;00135 RTL8019_DATA_PORT = 0xFF;00136 }00137 <span class="preprocessor">#endif</span>00138 <span class="preprocessor"></span>00139 00140 <span class="preprocessor">#if NIC_CONNECTION == MEMORY_MAPPED</span>00141 <span class="preprocessor"></span><span class="keyword">inline</span> u08 ax88796Read(u08 address)00142 {00143 <span class="keywordflow">return</span> *(<span class="keyword">volatile</span> u08*)(RTL8019_MEMORY_MAPPED_OFFSET + address);00144 }00145 <span class="preprocessor">#else</span>00146 <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)00147 {00148 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> data;00149 00150 <span class="comment">// assert the address</span>00151 RTL8019_ADDRESS_PORT = address | (RTL8019_ADDRESS_PORT&~RTL8019_ADDRESS_MASK);00152 <span class="comment">// assert read</span>00153 cbi(RTL8019_CONTROL_PORT, RTL8019_CONTROL_READPIN);00154 nop();00155 nop();00156 <span class="comment">// read in the data</span>00157 data = RTL8019_DATA_PIN;00158 <span class="comment">// negate read</span>00159 sbi(RTL8019_CONTROL_PORT, RTL8019_CONTROL_READPIN);00160 <span class="comment">// return data</span>00161 <span class="keywordflow">return</span> data;00162 }00163 <span class="preprocessor">#endif </span>00164 <span class="preprocessor"></span>00165 00166 <span class="keywordtype">void</span> rtl8019Init(<span class="keywordtype">void</span>)00167 {00168 <span class="comment">// setup I/O ports</span>00169 rtl8019SetupPorts();00170 00171 <span class="comment">// do a hard reset</span>00172 sbi(RTL8019_RESET_PORT, RTL8019_RESET_PIN);00173 delay_ms(10);00174 cbi(RTL8019_RESET_PORT, RTL8019_RESET_PIN);00175 00176 <span class="comment">// clear interrupt state</span>00177 rtl8019Write( ISR, rtl8019Read(ISR) );00178 delay_ms(50);00179 00180 <span class="comment">// switch to page 3 to load config registers</span>00181 rtl8019Write(CR, (PS0|PS1|RD2|STOP));00182 00183 <span class="comment">// disable EEPROM write protect of config registers</span>00184 rtl8019Write(RTL_EECR, (EEM1|EEM0));00185 00186 <span class="comment">// set network type to 10 Base-T link test</span>00187 rtl8019Write(CONFIG2, 0x20);00188 00189 <span class="comment">// disable powerdown and sleep</span>00190 rtl8019Write(CONFIG3, 0);00191 delay_ms(255);00192 00193 <span class="comment">// reenable EEPROM write protect</span>00194 rtl8019Write(RTL_EECR, 0);00195 00196 <span class="comment">// go back to page 0, stop NIC, abort DMA</span>00197 rtl8019Write(CR, (RD2|STOP));00198 delay_ms(2); <span class="comment">// wait for traffic to complete</span>00199 rtl8019Write(DCR, DCR_INIT);00200 rtl8019Write(RBCR0,0x00);00201 rtl8019Write(RBCR1,0x00);00202 rtl8019Write(RCR, AB);00203 rtl8019Write(TPSR, TXSTART_INIT);00204 rtl8019Write(TCR, LB0);00205 rtl8019Write(PSTART, RXSTART_INIT);00206 rtl8019Write(BNRY, RXSTART_INIT);00207 rtl8019Write(PSTOP, RXSTOP_INIT);00208 rtl8019Write(CR, (PS0|RD2|STOP)); <span class="comment">// switch to page 1</span>00209 delay_ms(2);00210 rtl8019Write(CPR, RXSTART_INIT);00211 00212 <span class="comment">// set MAC address</span>00213 rtl8019Write(PAR0, RTL8019_MAC0);00214 rtl8019Write(PAR1, RTL8019_MAC1);00215 rtl8019Write(PAR2, RTL8019_MAC2);00216 rtl8019Write(PAR3, RTL8019_MAC3);00217 rtl8019Write(PAR4, RTL8019_MAC4);00218 rtl8019Write(PAR5, RTL8019_MAC5);00219 00220 <span class="comment">// initialize sequence per NE2000 spec</span>00221 rtl8019Write(CR, (RD2|STOP));00222 rtl8019Write(DCR, DCR_INIT);00223 rtl8019Write(CR, (RD2|START));00224 rtl8019Write(ISR,0xFF); <span class="comment">// clear all interrupts</span>00225 rtl8019Write(IMR, IMR_INIT);00226 rtl8019Write(TCR, TCR_INIT);00227 00228 rtl8019Write(CR, (RD2|START)); <span class="comment">// start the NIC</span>00229 }00230 00231 00232 <span class="keywordtype">void</span> rtl8019BeginPacketSend(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> packetLength)00233 {00234 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> sendPacketLength;00235 sendPacketLength = (packetLength>=ETHERNET_MIN_PACKET_LENGTH)?00236 (packetLength):ETHERNET_MIN_PACKET_LENGTH;00237 00238 <span class="comment">//start the NIC</span>00239 rtl8019Write(CR, (RD2|START));00240 00241 <span class="comment">// still transmitting a packet - wait for it to finish</span>00242 <span class="keywordflow">while</span>( rtl8019Read(CR) & TXP );00243 00244 <span class="comment">// load beginning page for transmit buffer</span>00245 rtl8019Write(TPSR,TXSTART_INIT);00246 00247 <span class="comment">// set start address for remote DMA operation</span>00248 rtl8019Write(RSAR0,0x00);00249 rtl8019Write(RSAR1,0x40);00250
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -