Line No. | Rev | Author | Line |
---|---|---|---|
1 | 6 | kaklik | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
2 | <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> |
||
3 | <title>Procyon AVRlib: net/rtl8019.c Source File</title> |
||
4 | <link href="dox.css" rel="stylesheet" type="text/css"> |
||
5 | </head><body> |
||
6 | <!-- Generated by Doxygen 1.4.2 --> |
||
7 | <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> | <a class="qindex" href="pages.html">Related Pages</a></div> |
||
8 | <div class="nav"> |
||
9 | <a class="el" href="dir_000001.html">net</a></div> |
||
10 | <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> |
||
11 | 00002 <span class="comment">//*****************************************************************************</span> |
||
12 | 00003 <span class="comment">//</span> |
||
13 | 00004 <span class="comment">// File Name : 'rtl8019.c'</span> |
||
14 | 00005 <span class="comment">// Title : Realtek RTL8019AS Ethernet Interface Driver</span> |
||
15 | 00006 <span class="comment">// Author : Pascal Stang</span> |
||
16 | 00007 <span class="comment">// Created : 7/6/2004</span> |
||
17 | 00008 <span class="comment">// Revised : 10/1/2005</span> |
||
18 | 00009 <span class="comment">// Version : 0.1</span> |
||
19 | 00010 <span class="comment">// Target MCU : Atmel AVR series</span> |
||
20 | 00011 <span class="comment">// Editor Tabs : 4</span> |
||
21 | 00012 <span class="comment">//</span> |
||
22 | 00013 <span class="comment">// Based in part on code by Louis Beaudoin (www.embedded-creations.com).</span> |
||
23 | 00014 <span class="comment">// Thanks to Adam Dunkels and Louis Beaudoin for providing the initial</span> |
||
24 | 00015 <span class="comment">// structure in which to write this driver.</span> |
||
25 | 00016 <span class="comment">//*****************************************************************************</span> |
||
26 | 00017 |
||
27 | 00018 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span> |
||
28 | 00019 <span class="preprocessor">#include "<a class="code" href="timer_8h.html">timer.h</a>"</span> |
||
29 | 00020 <span class="preprocessor">#include "<a class="code" href="rprintf_8h.html">rprintf.h</a>"</span> |
||
30 | 00021 |
||
31 | 00022 <span class="preprocessor">#include "<a class="code" href="rtl8019_8h.html">rtl8019.h</a>"</span> |
||
32 | 00023 |
||
33 | 00024 <span class="comment">// include configuration</span> |
||
34 | 00025 <span class="preprocessor">#include "<a class="code" href="rtl8019conf_8h.html">rtl8019conf.h</a>"</span> |
||
35 | 00026 |
||
36 | 00027 <span class="comment">// pointers to locations in the RTL8019 receive buffer</span> |
||
37 | 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> |
||
38 | 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> |
||
39 | 00030 |
||
40 | 00031 |
||
41 | <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>) |
||
42 | 00033 { |
||
43 | 00034 rtl8019Init(); |
||
44 | 00035 } |
||
45 | 00036 |
||
46 | <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) |
||
47 | 00038 { |
||
48 | 00039 rtl8019BeginPacketSend(len); |
||
49 | 00040 rtl8019SendPacketData(packet, len); |
||
50 | 00041 rtl8019EndPacketSend(); |
||
51 | 00042 } |
||
52 | 00043 |
||
53 | <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) |
||
54 | 00045 { |
||
55 | 00046 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> packetLength; |
||
56 | 00047 |
||
57 | 00048 packetLength = rtl8019BeginPacketRetreive(); |
||
58 | 00049 |
||
59 | 00050 <span class="comment">// if there's no packet or an error - exit without ending the operation</span> |
||
60 | 00051 <span class="keywordflow">if</span>( !packetLength ) |
||
61 | 00052 <span class="keywordflow">return</span> 0; |
||
62 | 00053 |
||
63 | 00054 <span class="comment">// drop anything too big for the buffer</span> |
||
64 | 00055 <span class="keywordflow">if</span>( packetLength > maxlen ) |
||
65 | 00056 { |
||
66 | 00057 rtl8019EndPacketRetreive(); |
||
67 | 00058 <span class="keywordflow">return</span> 0; |
||
68 | 00059 } |
||
69 | 00060 |
||
70 | 00061 <span class="comment">// copy the packet data into the packet buffer</span> |
||
71 | 00062 rtl8019RetreivePacketData( packet, packetLength ); |
||
72 | 00063 rtl8019EndPacketRetreive(); |
||
73 | 00064 |
||
74 | 00065 <span class="keywordflow">return</span> packetLength; |
||
75 | 00066 } |
||
76 | 00067 |
||
77 | 00068 <span class="keywordtype">void</span> nicGetMacAddress(u08* macaddr) |
||
78 | 00069 { |
||
79 | 00070 u08 tempCR; |
||
80 | 00071 <span class="comment">// switch register pages</span> |
||
81 | 00072 tempCR = rtl8019Read(CR); |
||
82 | 00073 rtl8019Write(CR,tempCR|PS0); |
||
83 | 00074 <span class="comment">// read MAC address registers</span> |
||
84 | 00075 *macaddr++ = rtl8019Read(PAR0); |
||
85 | 00076 *macaddr++ = rtl8019Read(PAR1); |
||
86 | 00077 *macaddr++ = rtl8019Read(PAR2); |
||
87 | 00078 *macaddr++ = rtl8019Read(PAR3); |
||
88 | 00079 *macaddr++ = rtl8019Read(PAR4); |
||
89 | 00080 *macaddr++ = rtl8019Read(PAR5); |
||
90 | 00081 <span class="comment">// switch register pages back</span> |
||
91 | 00082 rtl8019Write(CR,tempCR); |
||
92 | 00083 } |
||
93 | 00084 |
||
94 | 00085 <span class="keywordtype">void</span> nicSetMacAddress(u08* macaddr) |
||
95 | 00086 { |
||
96 | 00087 u08 tempCR; |
||
97 | 00088 <span class="comment">// switch register pages</span> |
||
98 | 00089 tempCR = rtl8019Read(CR); |
||
99 | 00090 rtl8019Write(CR,tempCR|PS0); |
||
100 | 00091 <span class="comment">// write MAC address registers</span> |
||
101 | 00092 rtl8019Write(PAR0, *macaddr++); |
||
102 | 00093 rtl8019Write(PAR1, *macaddr++); |
||
103 | 00094 rtl8019Write(PAR2, *macaddr++); |
||
104 | 00095 rtl8019Write(PAR3, *macaddr++); |
||
105 | 00096 rtl8019Write(PAR4, *macaddr++); |
||
106 | 00097 rtl8019Write(PAR5, *macaddr++); |
||
107 | 00098 <span class="comment">// switch register pages back</span> |
||
108 | 00099 rtl8019Write(CR,tempCR); |
||
109 | 00100 } |
||
110 | 00101 |
||
111 | <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>) |
||
112 | 00103 { |
||
113 | 00104 rtl8019RegDump(); |
||
114 | 00105 } |
||
115 | 00106 |
||
116 | 00107 |
||
117 | 00108 <span class="keywordtype">void</span> rtl8019SetupPorts(<span class="keywordtype">void</span>) |
||
118 | 00109 { |
||
119 | 00110 <span class="preprocessor">#if NIC_CONNECTION == MEMORY_MAPPED</span> |
||
120 | 00111 <span class="preprocessor"></span> <span class="comment">// enable external SRAM interface - no wait states</span> |
||
121 | 00112 sbi(MCUCR, SRE); |
||
122 | 00113 <span class="comment">// sbi(MCUCR, SRW10);</span> |
||
123 | 00114 <span class="comment">// sbi(XMCRA, SRW00);</span> |
||
124 | 00115 <span class="comment">// sbi(XMCRA, SRW01);</span> |
||
125 | 00116 <span class="comment">// sbi(XMCRA, SRW11);</span> |
||
126 | 00117 <span class="preprocessor">#else</span> |
||
127 | 00118 <span class="preprocessor"></span> <span class="comment">// make the address port output</span> |
||
128 | 00119 RTL8019_ADDRESS_DDR |= RTL8019_ADDRESS_MASK; |
||
129 | 00120 <span class="comment">// make the data port input with pull-ups</span> |
||
130 | 00121 RTL8019_DATA_PORT = 0xFF; |
||
131 | 00122 |
||
132 | 00123 <span class="comment">// initialize the control port read and write pins to de-asserted</span> |
||
133 | 00124 RTL8019_CONTROL_DDR |= (1<<RTL8019_CONTROL_READPIN); |
||
134 | 00125 RTL8019_CONTROL_DDR |= (1<<RTL8019_CONTROL_WRITEPIN); |
||
135 | 00126 <span class="comment">// set the read and write pins to output</span> |
||
136 | 00127 RTL8019_CONTROL_PORT |= (1<<RTL8019_CONTROL_READPIN); |
||
137 | 00128 RTL8019_CONTROL_PORT |= (1<<RTL8019_CONTROL_WRITEPIN); |
||
138 | 00129 <span class="preprocessor">#endif</span> |
||
139 | 00130 <span class="preprocessor"></span> <span class="comment">// set reset pin to output</span> |
||
140 | 00131 sbi(RTL8019_RESET_DDR, RTL8019_RESET_PIN); |
||
141 | 00132 } |
||
142 | 00133 |
||
143 | 00134 |
||
144 | 00135 <span class="preprocessor">#if NIC_CONNECTION == MEMORY_MAPPED</span> |
||
145 | 00136 <span class="preprocessor"></span><span class="keyword">inline</span> <span class="keywordtype">void</span> rtl8019Write(u08 address, u08 data) |
||
146 | 00137 { |
||
147 | 00138 *(<span class="keyword">volatile</span> u08*)(RTL8019_MEMORY_MAPPED_OFFSET + address) = data; |
||
148 | 00139 } |
||
149 | 00140 <span class="preprocessor">#else</span> |
||
150 | 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) |
||
151 | 00142 { |
||
152 | 00143 <span class="comment">// assert the address</span> |
||
153 | 00144 RTL8019_ADDRESS_PORT = address | (RTL8019_ADDRESS_PORT&~RTL8019_ADDRESS_MASK); |
||
154 | 00145 <span class="comment">// set data bus as output and place data on bus</span> |
||
155 | 00146 RTL8019_DATA_DDR = 0xFF; |
||
156 | 00147 RTL8019_DATA_PORT = data; |
||
157 | 00148 <span class="comment">// clock write pin</span> |
||
158 | 00149 cbi(RTL8019_CONTROL_PORT, RTL8019_CONTROL_WRITEPIN); |
||
159 | 00150 nop(); |
||
160 | 00151 nop(); |
||
161 | 00152 sbi(RTL8019_CONTROL_PORT, RTL8019_CONTROL_WRITEPIN); |
||
162 | 00153 <span class="comment">// set data port back to input with pullups enabled</span> |
||
163 | 00154 RTL8019_DATA_DDR = 0x00; |
||
164 | 00155 RTL8019_DATA_PORT = 0xFF; |
||
165 | 00156 } |
||
166 | 00157 <span class="preprocessor">#endif</span> |
||
167 | 00158 <span class="preprocessor"></span> |
||
168 | 00159 |
||
169 | 00160 <span class="preprocessor">#if NIC_CONNECTION == MEMORY_MAPPED</span> |
||
170 | 00161 <span class="preprocessor"></span><span class="keyword">inline</span> u08 ax88796Read(u08 address) |
||
171 | 00162 { |
||
172 | 00163 <span class="keywordflow">return</span> *(<span class="keyword">volatile</span> u08*)(RTL8019_MEMORY_MAPPED_OFFSET + address); |
||
173 | 00164 } |
||
174 | 00165 <span class="preprocessor">#else</span> |
||
175 | 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) |
||
176 | 00167 { |
||
177 | 00168 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> data; |
||
178 | 00169 |
||
179 | 00170 <span class="comment">// assert the address</span> |
||
180 | 00171 RTL8019_ADDRESS_PORT = address | (RTL8019_ADDRESS_PORT&~RTL8019_ADDRESS_MASK); |
||
181 | 00172 <span class="comment">// assert read</span> |
||
182 | 00173 cbi(RTL8019_CONTROL_PORT, RTL8019_CONTROL_READPIN); |
||
183 | 00174 nop(); |
||
184 | 00175 nop(); |
||
185 | 00176 <span class="comment">// read in the data</span> |
||
186 | 00177 data = RTL8019_DATA_PIN; |
||
187 | 00178 <span class="comment">// negate read</span> |
||
188 | 00179 sbi(RTL8019_CONTROL_PORT, RTL8019_CONTROL_READPIN); |
||
189 | 00180 <span class="comment">// return data</span> |
||
190 | 00181 <span class="keywordflow">return</span> data; |
||
191 | 00182 } |
||
192 | 00183 <span class="preprocessor">#endif </span> |
||
193 | 00184 <span class="preprocessor"></span> |
||
194 | 00185 |
||
195 | 00186 <span class="keywordtype">void</span> rtl8019Init(<span class="keywordtype">void</span>) |
||
196 | 00187 { |
||
197 | 00188 <span class="comment">// setup I/O ports</span> |
||
198 | 00189 rtl8019SetupPorts(); |
||
199 | 00190 |
||
200 | 00191 <span class="comment">// do a hard reset</span> |
||
201 | 00192 sbi(RTL8019_RESET_PORT, RTL8019_RESET_PIN); |
||
202 | 00193 delay_ms(10); |
||
203 | 00194 cbi(RTL8019_RESET_PORT, RTL8019_RESET_PIN); |
||
204 | 00195 |
||
205 | 00196 <span class="comment">// clear interrupt state</span> |
||
206 | 00197 rtl8019Write( ISR, rtl8019Read(ISR) ); |
||
207 | 00198 delay_ms(50); |
||
208 | 00199 |
||
209 | 00200 <span class="comment">// switch to page 3 to load config registers</span> |
||
210 | 00201 rtl8019Write(CR, (PS0|PS1|RD2|STOP)); |
||
211 | 00202 |
||
212 | 00203 <span class="comment">// disable EEPROM write protect of config registers</span> |
||
213 | 00204 rtl8019Write(RTL_EECR, (EEM1|EEM0)); |
||
214 | 00205 |
||
215 | 00206 <span class="comment">// set network type to 10 Base-T link test</span> |
||
216 | 00207 rtl8019Write(CONFIG2, 0x20); |
||
217 | 00208 |
||
218 | 00209 <span class="comment">// disable powerdown and sleep</span> |
||
219 | 00210 rtl8019Write(CONFIG3, 0); |
||
220 | 00211 delay_ms(255); |
||
221 | 00212 |
||
222 | 00213 <span class="comment">// reenable EEPROM write protect</span> |
||
223 | 00214 rtl8019Write(RTL_EECR, 0); |
||
224 | 00215 |
||
225 | 00216 <span class="comment">// go back to page 0, stop NIC, abort DMA</span> |
||
226 | 00217 rtl8019Write(CR, (RD2|STOP)); |
||
227 | 00218 delay_ms(2); <span class="comment">// wait for traffic to complete</span> |
||
228 | 00219 rtl8019Write(DCR, DCR_INIT); |
||
229 | 00220 rtl8019Write(RBCR0,0x00); |
||
230 | 00221 rtl8019Write(RBCR1,0x00); |
||
231 | 00222 rtl8019Write(RCR, AB); |
||
232 | 00223 rtl8019Write(TPSR, TXSTART_INIT); |
||
233 | 00224 rtl8019Write(TCR, LB0); |
||
234 | 00225 rtl8019Write(PSTART, RXSTART_INIT); |
||
235 | 00226 rtl8019Write(BNRY, RXSTART_INIT); |
||
236 | 00227 rtl8019Write(PSTOP, RXSTOP_INIT); |
||
237 | 00228 rtl8019Write(CR, (PS0|RD2|STOP)); <span class="comment">// switch to page 1</span> |
||
238 | 00229 delay_ms(2); |
||
239 | 00230 rtl8019Write(CPR, RXSTART_INIT); |
||
240 | 00231 |
||
241 | 00232 <span class="comment">// set MAC address</span> |
||
242 | 00233 rtl8019Write(PAR0, RTL8019_MAC0); |
||
243 | 00234 rtl8019Write(PAR1, RTL8019_MAC1); |
||
244 | 00235 rtl8019Write(PAR2, RTL8019_MAC2); |
||
245 | 00236 rtl8019Write(PAR3, RTL8019_MAC3); |
||
246 | 00237 rtl8019Write(PAR4, RTL8019_MAC4); |
||
247 | 00238 rtl8019Write(PAR5, RTL8019_MAC5); |
||
248 | 00239 |
||
249 | 00240 <span class="comment">// initialize sequence per NE2000 spec</span> |
||
250 | 00241 rtl8019Write(CR, (RD2|STOP)); |
||
251 | 00242 rtl8019Write(DCR, DCR_INIT); |
||
252 | 00243 rtl8019Write(CR, (RD2|START)); |
||
253 | 00244 rtl8019Write(ISR,0xFF); <span class="comment">// clear all interrupts</span> |
||
254 | 00245 rtl8019Write(IMR, IMR_INIT); |
||
255 | 00246 rtl8019Write(TCR, TCR_INIT); |
||
256 | 00247 |
||
257 | 00248 rtl8019Write(CR, (RD2|START)); <span class="comment">// start the NIC</span> |
||
258 | 00249 } |
||
259 | 00250 |
||
260 | 00251 |
||
261 | 00252 <span class="keywordtype">void</span> rtl8019BeginPacketSend(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> packetLength) |
||
262 | 00253 { |
||
263 | 00254 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> sendPacketLength; |
||
264 | 00255 sendPacketLength = (packetLength>=ETHERNET_MIN_PACKET_LENGTH)? |
||
265 | 00256 (packetLength):ETHERNET_MIN_PACKET_LENGTH; |
||
266 | 00257 |
||
267 | 00258 <span class="comment">//start the NIC</span> |
||
268 | 00259 rtl8019Write(CR, (RD2|START)); |
||
269 | 00260 |
||
270 | 00261 <span class="comment">// still transmitting a packet - wait for it to finish</span> |
||
271 | 00262 <span class="keywordflow">while</span>( rtl8019Read(CR) & TXP ); |
||
272 | 00263 |
||
273 | 00264 <span class="comment">// load beginning page for transmit buffer</span> |
||
274 | 00265 rtl8019Write(TPSR,TXSTART_INIT); |
||
275 | 00266 |
||
276 | 00267 <span class="comment">// set start address for remote DMA operation</span> |
||
277 | 00268 rtl8019Write(RSAR0,0x00); |
||
278 | 00269 rtl8019Write(RSAR1,0x40); |
||
279 | 00270 |
||
280 | 00271 <span class="comment">// clear the packet stored interrupt</span> |
||
281 | 00272 rtl8019Write(ISR,PTX); |
||
282 | 00273 |
||
283 | 00274 <span class="comment">// load data byte count for remote DMA</span> |
||
284 | 00275 rtl8019Write(RBCR0, (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>)(packetLength)); |
||
285 | 00276 rtl8019Write(RBCR1, (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>)(packetLength>>8)); |
||
286 | 00277 |
||
287 | 00278 rtl8019Write(TBCR0, (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>)(sendPacketLength)); |
||
288 | 00279 rtl8019Write(TBCR1, (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>)((sendPacketLength)>>8)); |
||
289 | 00280 |
||
290 | 00281 <span class="comment">// do remote write operation</span> |
||
291 | 00282 rtl8019Write(CR,(RD1|START)); |
||
292 | 00283 } |
||
293 | 00284 |
||
294 | 00285 |
||
295 | 00286 <span class="keywordtype">void</span> rtl8019SendPacketData(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *localBuffer, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length) |
||
296 | 00287 { |
||
297 | 00288 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i; |
||
298 | 00289 |
||
299 | 00290 <span class="comment">// write data to DMA port</span> |
||
300 | 00291 <span class="keywordflow">for</span>(i=0;i<length;i++) |
||
301 | 00292 rtl8019Write(RDMAPORT, localBuffer[i]); |
||
302 | 00293 } |
||
303 | 00294 |
||
304 | 00295 |
||
305 | 00296 <span class="keywordtype">void</span> rtl8019EndPacketSend(<span class="keywordtype">void</span>) |
||
306 | 00297 { |
||
307 | 00298 <span class="comment">//send the contents of the transmit buffer onto the network</span> |
||
308 | 00299 rtl8019Write(CR,(RD2|TXP)); |
||
309 | 00300 <span class="comment">// clear the remote DMA interrupt</span> |
||
310 | 00301 rtl8019Write(ISR, RDC); |
||
311 | 00302 } |
||
312 | 00303 |
||
313 | 00304 |
||
314 | 00305 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> rtl8019BeginPacketRetreive(<span class="keywordtype">void</span>) |
||
315 | 00306 { |
||
316 | 00307 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> i; |
||
317 | 00308 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> bnry; |
||
318 | 00309 |
||
319 | 00310 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> pageheader[4]; |
||
320 | 00311 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> rxlen; |
||
321 | 00312 |
||
322 | 00313 <span class="comment">// check for and handle an overflow</span> |
||
323 | 00314 rtl8019ProcessInterrupt(); |
||
324 | 00315 |
||
325 | 00316 <span class="comment">// read CPR from page 1</span> |
||
326 | 00317 rtl8019Write(CR,(PS0|RD2|START)); |
||
327 | 00318 i = rtl8019Read(CPR); |
||
328 | 00319 |
||
329 | 00320 <span class="comment">// return to page 0</span> |
||
330 | 00321 rtl8019Write(CR,(RD2|START)); |
||
331 | 00322 |
||
332 | 00323 <span class="comment">// read the boundary register - pointing to the beginning of the packet</span> |
||
333 | 00324 bnry = rtl8019Read(BNRY) ; |
||
334 | 00325 |
||
335 | 00326 <span class="comment">// return if there is no packet in the buffer</span> |
||
336 | 00327 <span class="keywordflow">if</span>( bnry == i ) |
||
337 | 00328 <span class="keywordflow">return</span> 0; |
||
338 | 00329 |
||
339 | 00330 <span class="comment">// clear the packet received interrupt flag</span> |
||
340 | 00331 rtl8019Write(ISR, PRX); |
||
341 | 00332 |
||
342 | 00333 <span class="comment">// if boundary pointer is invalid</span> |
||
343 | 00334 <span class="keywordflow">if</span>( (bnry >= RXSTOP_INIT) || (bnry < RXSTART_INIT) ) |
||
344 | 00335 { |
||
345 | 00336 <span class="comment">// reset the contents of the buffer and exit</span> |
||
346 | 00337 rtl8019Write(BNRY, RXSTART_INIT); |
||
347 | 00338 rtl8019Write(CR, (PS0|RD2|START)); |
||
348 | 00339 rtl8019Write(CPR, RXSTART_INIT); |
||
349 | 00340 rtl8019Write(CR, (RD2|START)); |
||
350 | 00341 <span class="keywordflow">return</span> 0; |
||
351 | 00342 } |
||
352 | 00343 |
||
353 | 00344 <span class="comment">// initiate DMA to transfer the RTL8019 packet header</span> |
||
354 | 00345 rtl8019Write(RBCR0, 4); |
||
355 | 00346 rtl8019Write(RBCR1, 0); |
||
356 | 00347 rtl8019Write(RSAR0, 0); |
||
357 | 00348 rtl8019Write(RSAR1, bnry); |
||
358 | 00349 rtl8019Write(CR, (RD0|START)); |
||
359 | 00350 <span class="comment">// transfer packet header</span> |
||
360 | 00351 <span class="keywordflow">for</span>(i=0;i<4;i++) |
||
361 | 00352 pageheader[i] = rtl8019Read(RDMAPORT); |
||
362 | 00353 <span class="comment">// end the DMA operation</span> |
||
363 | 00354 rtl8019Write(CR, (RD2|START)); |
||
364 | 00355 <span class="comment">// wait for remote DMA complete</span> |
||
365 | 00356 <span class="keywordflow">for</span>(i = 0; i < 20; i++) |
||
366 | 00357 <span class="keywordflow">if</span>(rtl8019Read(ISR) & RDC) |
||
367 | 00358 <span class="keywordflow">break</span>; |
||
368 | 00359 rtl8019Write(ISR, RDC); |
||
369 | 00360 |
||
370 | 00361 rxlen = (pageheader[PKTHEADER_PKTLENH]<<8) + pageheader[PKTHEADER_PKTLENL]; |
||
371 | 00362 NextPage = pageheader[PKTHEADER_NEXTPAGE]; |
||
372 | 00363 |
||
373 | 00364 CurrentRetreiveAddress = (bnry<<8) + 4; |
||
374 | 00365 |
||
375 | 00366 <span class="comment">// if the NextPage pointer is invalid, the packet is not ready yet - exit</span> |
||
376 | 00367 <span class="keywordflow">if</span>( (NextPage >= RXSTOP_INIT) || (NextPage < RXSTART_INIT) ) |
||
377 | 00368 <span class="keywordflow">return</span> 0; |
||
378 | 00369 |
||
379 | 00370 <span class="keywordflow">return</span> rxlen-4; |
||
380 | 00371 } |
||
381 | 00372 |
||
382 | 00373 |
||
383 | 00374 <span class="keywordtype">void</span> rtl8019RetreivePacketData(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> * localBuffer, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length) |
||
384 | 00375 { |
||
385 | 00376 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i; |
||
386 | 00377 |
||
387 | 00378 <span class="comment">// initiate DMA to transfer the data</span> |
||
388 | 00379 rtl8019Write(RBCR0, (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>)length); |
||
389 | 00380 rtl8019Write(RBCR1, (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>)(length>>8)); |
||
390 | 00381 rtl8019Write(RSAR0, (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>)CurrentRetreiveAddress); |
||
391 | 00382 rtl8019Write(RSAR1, (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>)(CurrentRetreiveAddress>>8)); |
||
392 | 00383 rtl8019Write(CR, (RD0|START)); |
||
393 | 00384 <span class="comment">// transfer packet data</span> |
||
394 | 00385 <span class="keywordflow">for</span>(i=0;i<length;i++) |
||
395 | 00386 localBuffer[i] = rtl8019Read(RDMAPORT); |
||
396 | 00387 <span class="comment">// end the DMA operation</span> |
||
397 | 00388 rtl8019Write(CR, (RD2|START)); |
||
398 | 00389 <span class="comment">// wait for remote DMA complete</span> |
||
399 | 00390 <span class="keywordflow">for</span>(i=0; i<20; i++) |
||
400 | 00391 <span class="keywordflow">if</span>(rtl8019Read(ISR) & RDC) |
||
401 | 00392 <span class="keywordflow">break</span>; |
||
402 | 00393 rtl8019Write(ISR, RDC); |
||
403 | 00394 <span class="comment">// keep track of current address</span> |
||
404 | 00395 CurrentRetreiveAddress += length; |
||
405 | 00396 <span class="keywordflow">if</span>( CurrentRetreiveAddress >= 0x6000 ) |
||
406 | 00397 CurrentRetreiveAddress = CurrentRetreiveAddress - (0x6000-0x4600) ; |
||
407 | 00398 } |
||
408 | 00399 |
||
409 | 00400 |
||
410 | 00401 <span class="keywordtype">void</span> rtl8019EndPacketRetreive(<span class="keywordtype">void</span>) |
||
411 | 00402 { |
||
412 | 00403 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> i; |
||
413 | 00404 |
||
414 | 00405 <span class="comment">// end the DMA operation</span> |
||
415 | 00406 rtl8019Write(CR, (RD2|START)); |
||
416 | 00407 <span class="comment">// wait for remote DMA complete</span> |
||
417 | 00408 <span class="keywordflow">for</span>(i=0; i<20; i++) |
||
418 | 00409 <span class="keywordflow">if</span>(rtl8019Read(ISR) & RDC) |
||
419 | 00410 <span class="keywordflow">break</span>; |
||
420 | 00411 rtl8019Write(ISR, RDC); |
||
421 | 00412 |
||
422 | 00413 <span class="comment">// set the boundary register to point to the start of the next packet</span> |
||
423 | 00414 rtl8019Write(BNRY, NextPage); |
||
424 | 00415 } |
||
425 | 00416 |
||
426 | 00417 |
||
427 | 00418 <span class="keywordtype">void</span> rtl8019ProcessInterrupt(<span class="keywordtype">void</span>) |
||
428 | 00419 { |
||
429 | 00420 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> byte = rtl8019Read(ISR); |
||
430 | 00421 |
||
431 | 00422 <span class="keywordflow">if</span>( byte & OVW ) |
||
432 | 00423 rtl8019ReceiveOverflowRecover(); |
||
433 | 00424 } |
||
434 | 00425 |
||
435 | 00426 <span class="keywordtype">void</span> rtl8019ReceiveOverflowRecover(<span class="keywordtype">void</span>) |
||
436 | 00427 { |
||
437 | 00428 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> data_L, resend; |
||
438 | 00429 |
||
439 | 00430 data_L = rtl8019Read(CR); |
||
440 | 00431 rtl8019Write(CR, 0x21); |
||
441 | 00432 delay_ms(2); |
||
442 | 00433 rtl8019Write(RBCR0, 0x00); |
||
443 | 00434 rtl8019Write(RBCR1, 0x00); |
||
444 | 00435 <span class="keywordflow">if</span>(!(data_L & 0x04)) |
||
445 | 00436 resend = 0; |
||
446 | 00437 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(data_L & 0x04) |
||
447 | 00438 { |
||
448 | 00439 data_L = rtl8019Read(ISR); |
||
449 | 00440 <span class="keywordflow">if</span>((data_L & 0x02) || (data_L & 0x08)) |
||
450 | 00441 resend = 0; |
||
451 | 00442 <span class="keywordflow">else</span> |
||
452 | 00443 resend = 1; |
||
453 | 00444 } |
||
454 | 00445 |
||
455 | 00446 rtl8019Write(TCR, 0x02); |
||
456 | 00447 rtl8019Write(CR, 0x22); |
||
457 | 00448 rtl8019Write(BNRY, RXSTART_INIT); |
||
458 | 00449 rtl8019Write(CR, 0x62); |
||
459 | 00450 rtl8019Write(CPR, RXSTART_INIT); |
||
460 | 00451 rtl8019Write(CR, 0x22); |
||
461 | 00452 rtl8019Write(ISR, 0x10); |
||
462 | 00453 rtl8019Write(TCR, TCR_INIT); |
||
463 | 00454 |
||
464 | 00455 <span class="keywordflow">if</span>(resend) |
||
465 | 00456 rtl8019Write(CR, 0x26); |
||
466 | 00457 |
||
467 | 00458 rtl8019Write(ISR, 0xFF); |
||
468 | 00459 } |
||
469 | 00460 |
||
470 | 00461 |
||
471 | 00462 <span class="keywordtype">void</span> rtl8019RegDump(<span class="keywordtype">void</span>) |
||
472 | 00463 { |
||
473 | 00464 <span class="comment">// unsigned char result;</span> |
||
474 | 00465 <span class="comment">// result = rtl8019Read(TR);</span> |
||
475 | 00466 |
||
476 | 00467 <span class="comment">// rprintf("Media State: ");</span> |
||
477 | 00468 <span class="comment">// if(!(result & AUTOD))</span> |
||
478 | 00469 <span class="comment">// rprintf("Autonegotiation\r\n");</span> |
||
479 | 00470 <span class="comment">// else if(result & RST_B)</span> |
||
480 | 00471 <span class="comment">// rprintf("PHY in Reset \r\n");</span> |
||
481 | 00472 <span class="comment">// else if(!(result & RST_10B))</span> |
||
482 | 00473 <span class="comment">// rprintf("10BASE-T \r\n");</span> |
||
483 | 00474 <span class="comment">// else if(!(result & RST_TXB))</span> |
||
484 | 00475 <span class="comment">// rprintf("100BASE-T \r\n");</span> |
||
485 | 00476 |
||
486 | 00477 <span class="comment">//rprintf("TR regsiter : %x\r\n",result);</span> |
||
487 | 00478 <span class="comment">//result = read_mii(0x10,0);</span> |
||
488 | 00479 <span class="comment">//rprintf("MII regsiter 0x10: %x\r\n",result);</span> |
||
489 | 00480 |
||
490 | 00481 rprintf(<span class="stringliteral">"Page0: CR BNRY PSR PST ISR TSR RSR MMR TR GPI\r\n"</span>); |
||
491 | 00482 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>); |
||
492 | 00483 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(rtl8019Read(CR)); |
||
493 | 00484 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>); |
||
494 | 00485 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(rtl8019Read(BNRY)); |
||
495 | 00486 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>); |
||
496 | 00487 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(rtl8019Read(PSTART)); |
||
497 | 00488 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>); |
||
498 | 00489 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(rtl8019Read(PSTOP)); |
||
499 | 00490 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>); |
||
500 | 00491 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(rtl8019Read(ISR)); |
||
501 | 00492 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>); |
||
502 | 00493 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(rtl8019Read(TSR)); |
||
503 | 00494 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>); |
||
504 | 00495 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(rtl8019Read(RSR)); |
||
505 | 00496 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>); |
||
506 | 00497 <span class="comment">// rprintfu08(rtl8019Read(MEMR));</span> |
||
507 | 00498 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>); |
||
508 | 00499 <span class="comment">// rprintfu08(rtl8019Read(TR));</span> |
||
509 | 00500 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>); |
||
510 | 00501 <span class="comment">// rprintfu08(rtl8019Read(GPI));</span> |
||
511 | 00502 <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>(); |
||
512 | 00503 |
||
513 | 00504 rtl8019Write(CR,rtl8019Read(CR)|PS0); |
||
514 | 00505 |
||
515 | 00506 rprintf(<span class="stringliteral">"Page1: CR PAR CPR\r\n"</span>); |
||
516 | 00507 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>); |
||
517 | 00508 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(rtl8019Read(CR)); |
||
518 | 00509 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>); |
||
519 | 00510 <a class="code" href="group__rprintf.html#ga1">rprintfChar</a>(rtl8019Read(PAR0)); |
||
520 | 00511 <a class="code" href="group__rprintf.html#ga1">rprintfChar</a>(rtl8019Read(PAR1)); |
||
521 | 00512 <a class="code" href="group__rprintf.html#ga1">rprintfChar</a>(rtl8019Read(PAR2)); |
||
522 | 00513 <a class="code" href="group__rprintf.html#ga1">rprintfChar</a>(rtl8019Read(PAR3)); |
||
523 | 00514 <a class="code" href="group__rprintf.html#ga1">rprintfChar</a>(rtl8019Read(PAR4)); |
||
524 | 00515 <a class="code" href="group__rprintf.html#ga1">rprintfChar</a>(rtl8019Read(PAR5)); |
||
525 | 00516 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>); |
||
526 | 00517 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(rtl8019Read(CPR)); |
||
527 | 00518 |
||
528 | 00519 rtl8019Write(CR,rtl8019Read(CR)&~PS0); |
||
529 | 00520 |
||
530 | 00521 delay_ms(25); |
||
531 | 00522 } |
||
532 | 00523 |
||
533 | </pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by |
||
534 | <a href="http://www.doxygen.org/index.html"> |
||
535 | <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address> |
||
536 | </body> |
||
537 | </html> |
Powered by WebSVN v2.8.3