1 #include "etherdev.h"
\r
3 void sleep(unsigned char);
\r
5 static xdata leds1 _at_ 0x0080;
\r
6 static xdata leds2 _at_ 0x00c0;
\r
8 unsigned char uip_buf[64] =
\r
10 //0x00, 0x0c, 0x6e, 0x37, 0x19, 0xbf, // MAC destino
\r
11 0x00, 0x80, 0xc7, 0x42, 0x8d, 0x27, // MAC destino
\r
12 0x00, 0x0c, 0x6e, 0x37, 0x19, 0xbe, // MAC fuente
\r
16 unsigned int uip_len = 14;
\r
18 /* ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
\r
19 º Private defines. º
\r
20 ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ */
\r
21 #define ETH_CPU_CLOCK ETH_CPU_XTAL / 12 // 8051 clock rate (X1 mode)
\r
23 // Delay routine timing parameters
\r
24 #define ETH_DELAY_CONST 9.114584e-5 // Delay routine constant
\r
25 #define ETH_DELAY_MULTPLR (unsigned char)(ETH_DELAY_CONST * ETH_CPU_CLOCK)
\r
27 // X1 CPU mode timing parameters
\r
28 #define ETH_T0_CLOCK ETH_CPU_XTAL / 12 // Timer 0 mode 1 clock rate
\r
29 #define ETH_T0_INT_RATE 24 // Timer 0 intrupt rate (Hz)
\r
30 #define ETH_T0_RELOAD 65536 - (ETH_T0_CLOCK / ETH_T0_INT_RATE)
\r
32 // Packet transmit & receive buffer configuration
\r
33 #define ETH_TX_PAGE_START 0x40 // 0x4000 Tx buffer is 6 * 256 = 1536 bytes
\r
34 #define ETH_RX_PAGE_START 0x46 // 0x4600 Rx buffer is 26 * 256 = 6656 bytes
\r
35 #define ETH_RX_PAGE_STOP 0x60 // 0x6000
\r
37 #define ETH_ADDR_PORT_MASK 0x1F // 00011111y
\r
38 #define ETH_DATA_PORT_MASK 0xFF // 11111111y
\r
40 #define ETH_MIN_PACKET_LEN 0x3C
\r
43 /* ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
\r
44 º Private Function Prototypes º
\r
45 ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ */
\r
46 static void etherdev_reg_write(unsigned char reg, unsigned char wr_data);
\r
47 static unsigned char etherdev_reg_read(unsigned char reg);
\r
48 static void etherdev_delay_ms(unsigned int count);
\r
49 static unsigned int etherdev_poll(void);
\r
50 //static void etherdev_timer0_isr(void) interrupt 1 using 1;
\r
53 /* ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
\r
54 º Private Macro Defines º
\r
55 ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ */
\r
56 // Manipulate PS1 & PS0 in CR to select RTL8019AS register page.
\r
57 #define ETHERDEV_SELECT_REG_PAGE(page) \
\r
60 etherdev_reg_write(CR, etherdev_reg_read(CR) & ~(PS1 | PS0)); \
\r
61 etherdev_reg_write(CR, etherdev_reg_read(CR) | (page << 6)); \
\r
65 /* ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
\r
66 º Global Variables º
\r
67 ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ */
\r
68 static unsigned char tick_count = 0;
\r
71 /* ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
\r
72 º Private Function Implementation º
\r
73 ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ */
\r
75 /* ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
\r
77 º etherdev_reg_write() º
\r
80 ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ */
\r
81 static void etherdev_reg_write(unsigned char reg, unsigned char wr_data)
\r
83 // Select register address.
\r
84 ETH_ADDR_PORT &= ~ETH_ADDR_PORT_MASK;
\r
85 ETH_ADDR_PORT |= reg;
\r
87 // Output register data to port.
\r
88 ETH_DATA_PORT = wr_data;
\r
90 // Clock register data into RTL8019AS.
\r
91 // IOR & IOW are both active low.
\r
97 // Set register data port as input again.
\r
98 ETH_DATA_PORT = ETH_DATA_PORT_MASK;
\r
104 /* ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
\r
106 º etherdev_reg_read() º
\r
109 ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ */
\r
110 static unsigned char etherdev_reg_read(unsigned char reg)
\r
112 unsigned char rd_data;
\r
114 // Select register address.
\r
115 ETH_ADDR_PORT &= ~ETH_ADDR_PORT_MASK;
\r
116 ETH_ADDR_PORT |= reg;
\r
118 // Enable register data output from RTL8019AS.
\r
122 // Read register data from port.
\r
123 rd_data = ETH_DATA_PORT;
\r
125 // Disable register data output from RTL8019AS.
\r
133 /* ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
\r
135 º etherdev_delay_ms() º
\r
137 º 1 to 255+ ms delay. º
\r
139 ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ */
\r
140 static void etherdev_delay_ms(unsigned int count)
\r
143 for(count *= ETH_DELAY_MULTPLR; count > 0; count--) continue;
\r
149 /* ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
\r
151 º etherdev_timer0_isr() º
\r
153 º This function is invoked each 1/24th of a second and updates a º
\r
154 º 1/24th of a second tick counter. º
\r
156 ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ */
\r
157 static void etherdev_timer0_isr(void) interrupt 1 using 1
\r
159 // Reload timer/ counter 0 for 24Hz periodic interrupt.
\r
160 TH0 = ETH_T0_RELOAD >> 8;
\r
161 TL0 = ETH_T0_RELOAD;
\r
163 // Increment 24ths of a second counter.
\r
170 /* ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
\r
171 º Public Function Implementation º
\r
172 ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ */
\r
174 /* ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
\r
176 º etherdev_init() º
\r
178 º Returns: 1 on success, 0 on failure. º
\r
179 º Refer to National Semiconductor DP8390 App Note 874, July 1993. º
\r
181 ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ */
\r
182 bit etherdev_init(void)
\r
184 // Set IOR & IOW as they're active low.
\r
189 // Set register data port as input.
\r
190 ETH_DATA_PORT = ETH_DATA_PORT_MASK;
\r
194 #endif /* ETH_DEBUG */
\r
196 // Configure RTL8019AS ethernet controller.
\r
198 // Keil startup code takes 4ms to execute (18.432MHz, X1 mode).
\r
199 // That leaves plenty of time for the RTL8019AS to read it's
\r
200 // configuration in from the 9346 EEPROM before we get here.
\r
202 // Select RTL8019AS register page 0.
\r
203 ETHERDEV_SELECT_REG_PAGE(0);
\r
205 // Check if RTL8019AS fully reset.
\r
206 if(!(etherdev_reg_read(ISR) & RST))
\r
214 // Select RTL8019AS register page 3.
\r
215 ETHERDEV_SELECT_REG_PAGE(3);
\r
217 // Temporarily disable config register write protection.
\r
218 etherdev_reg_write(_9346CR, EEM1 | EEM0);
\r
220 // Disable boot ROM & select 10BaseT with TP/CX auto-detect.
\r
221 etherdev_reg_write(CONFIG2, BSELB);
\r
223 // Select half-duplex, awake, power-up & LED_TX/ LED_RX/ LED_LNK behaviour.
\r
224 etherdev_reg_write(CONFIG3, LEDS0);
\r
226 // Re-enable config register write protection.
\r
227 etherdev_reg_write(_9346CR, 0x00);
\r
229 // Select RTL8019AS register page 0.
\r
230 ETHERDEV_SELECT_REG_PAGE(0);
\r
232 // Stop RTL8019AS, select page 0 and abort DMA operation.
\r
233 etherdev_reg_write(CR, RD2 | STP);
\r
235 // Initialise data configuration register.
\r
236 // FIFO threshold 8 bytes, no loopback, don't use auto send packet.
\r
237 etherdev_reg_write(DCR, FT1 | LS);
\r
239 // Reset remote byte count registers.
\r
240 etherdev_reg_write(RBCR0, 0x00);
\r
241 etherdev_reg_write(RBCR1, 0x00);
\r
243 // Receive configuration register to monitor mode.
\r
244 etherdev_reg_write(RCR, MON);
\r
246 // Initialise transmit configuration register to loopback internally.
\r
247 etherdev_reg_write(TCR, LB0);
\r
249 // Clear interrupt status register bits by writing 1 to each.
\r
250 etherdev_reg_write(ISR, 0xFF);
\r
252 // Mask all interrupts in mask register.
\r
253 etherdev_reg_write(IMR, 0x00);
\r
255 // Obtengo MAC de la placa
\r
256 //etherdev_reg_write(CR, 0x21);
\r
259 // Set transmit page start.
\r
260 etherdev_reg_write(TPSR, ETH_TX_PAGE_START);
\r
262 // Set receive buffer page start.
\r
263 etherdev_reg_write(PSTART, ETH_RX_PAGE_START);
\r
265 // Initialise last receive buffer read pointer.
\r
266 etherdev_reg_write(BNRY, ETH_RX_PAGE_START);
\r
268 // Set receive buffer page stop.
\r
269 etherdev_reg_write(PSTOP, ETH_RX_PAGE_STOP);
\r
271 // Select RTL8019AS register page 1.
\r
272 etherdev_reg_write(CR, RD2 | PS0 | STP);
\r
274 // Initialise current packet receive buffer page pointer
\r
275 etherdev_reg_write(CURR, ETH_RX_PAGE_START);
\r
277 // Set physical address
\r
278 etherdev_reg_write(PAR0, 0x00);
\r
279 etherdev_reg_write(PAR1, 0x0c);
\r
280 etherdev_reg_write(PAR2, 0x6e);
\r
281 etherdev_reg_write(PAR3, 0x37);
\r
282 etherdev_reg_write(PAR4, 0x19);
\r
283 etherdev_reg_write(PAR5, 0xbe);
\r
285 // Select RTL8019AS register page 0 and abort DMA operation.
\r
286 etherdev_reg_write(CR, RD2 | STP);
\r
288 // Restart RTL8019AS.
\r
289 etherdev_reg_write(CR, RD2 | STA);
\r
291 // Initialise transmit configuration register for normal operation.
\r
292 etherdev_reg_write(TCR, 0x00);
\r
294 // Receive configuration register to accept broadcast packets.
\r
295 etherdev_reg_write(RCR, AB);
\r
298 // Initialize Timer 0 to generate a periodic 24Hz interrupt.
\r
300 // Stop timer/ counter 0.
\r
303 // Set timer/ counter 0 as mode 1 16 bit timer.
\r
307 // Preload for 24Hz periodic interrupt.
\r
308 TH0 = ETH_T0_RELOAD >> 8;
\r
309 TL0 = ETH_T0_RELOAD;
\r
311 // Restart timer/ counter 0 running.
\r
314 // Enable timer/ counter 0 overflow interrupt.
\r
317 // Enable global interrupt.
\r
324 /* ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
\r
326 º etherdev_send() º
\r
328 º Send the packet in the uip_buf and uip_appdata buffers using the º
\r
329 º RTL8019AS ethernet card. º
\r
331 ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ */
\r
332 void etherdev_send(void)
\r
335 unsigned char *ptr;
\r
339 // Setup for DMA transfer from uip_buf & uip_appdata buffers to RTL8019AS.
\r
341 // Select RTL8019AS register page 0 and abort DMA operation.
\r
342 etherdev_reg_write(CR, RD2 | STA);
\r
345 i = etherdev_reg_read(CR);
\r
347 // Wait until pending transmit operation completes.
\r
348 while(etherdev_reg_read(CR) & TXP) continue;
\r
350 // Clear remote DMA complete interrupt status register bit.
\r
351 etherdev_reg_write(ISR, RDC);
\r
353 // Set remote DMA start address registers to indicate where to load packet.
\r
354 etherdev_reg_write(RSAR0, 0x00);
\r
355 etherdev_reg_write(RSAR1, ETH_TX_PAGE_START);
\r
357 // Set remote DMA byte count registers to indicate length of packet load.
\r
358 etherdev_reg_write(RBCR0, (unsigned char)(uip_len & 0xFF));
\r
359 etherdev_reg_write(RBCR1, (unsigned char)(uip_len >> 8));
\r
361 // Initiate DMA transfer of uip_buf & uip_appdata buffers to RTL8019AS.
\r
362 etherdev_reg_write(CR, RD1 | STA);
\r
364 // DMA transfer packet from uip_buf & uip_appdata to RTL8019AS local
\r
365 // transmit buffer memory.
\r
366 for(i = 0; i < uip_len; i++)
\r
368 etherdev_reg_write(RDMA, *ptr++);
\r
371 // Wait until remote DMA operation complete.
\r
373 leds2 = ~etherdev_reg_read(ISR);
\r
374 while(!(etherdev_reg_read(ISR) & RDC)) continue;
\r
376 // Abort/ complete DMA operation.
\r
377 etherdev_reg_write(CR, RD2 | STA);
\r
379 // Set transmit page start to indicate packet start.
\r
380 etherdev_reg_write(TPSR, ETH_TX_PAGE_START);
\r
382 // Ethernet packets must be > 60 bytes, otherwise are rejected as runts.
\r
383 if(uip_len < ETH_MIN_PACKET_LEN)
\r
385 uip_len = ETH_MIN_PACKET_LEN;
\r
388 // Set transmit byte count registers to indicate packet length.
\r
389 etherdev_reg_write(TBCR0, (unsigned char)(uip_len & 0xFF));
\r
390 etherdev_reg_write(TBCR1, (unsigned char)(uip_len >> 8));
\r
392 // Issue command for RTL8019AS to transmit packet from it's local buffer.
\r
393 etherdev_reg_write(CR, RD2 | TXP | STA);
\r
399 /* ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
\r
401 º etherdev_read() º
\r
403 º This function will read an entire IP packet into the uip_buf. º
\r
404 º If it must wait for more than 0.5 seconds, it will return with º
\r
405 º the return value 0. Otherwise, when a full packet has been read º
\r
406 º into the uip_buf buffer, the length of the packet is returned. º
\r
408 ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ */
\r
409 unsigned int etherdev_read(void)
\r
411 unsigned int bytes_read;
\r
413 /* tick_count threshold should be 12 for 0.5 sec bail-out
\r
414 One second (24) worked better for me, but socket recycling
\r
415 is then slower. I set UIP_TIME_WAIT_TIMEOUT 60 in uipopt.h
\r
416 to counter this. Retransmission timing etc. is affected also. */
\r
417 while ((!(bytes_read = etherdev_poll())) && (tick_count < 12)) continue;
\r
425 /* ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
\r
427 º etherdev_poll() º
\r
429 º Poll the RTL8019AS ethernet device for an available packet. º
\r
431 ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ */
\r
432 static unsigned int etherdev_poll(void)
\r
434 unsigned int len = 0;
\r
436 // Check if there is a packet in the rx buffer.
\r
437 if(etherdev_reg_read(ISR) & PRX)
\r
439 // Check if the rx buffer has overflowed.
\r
440 if(etherdev_reg_read(ISR) & OVW)
\r
442 bit retransmit = 0;
\r
444 // If the receive buffer ring has overflowed we dump the whole
\r
445 // thing and start over. There is no way of knowing whether the
\r
446 // data it contains is uncorrupted, or will cause us grief.
\r
448 // Stop RTL8019AS and abort DMA operation.
\r
449 etherdev_reg_write(CR, RD2 | STP);
\r
451 // Reset remote byte count registers.
\r
452 etherdev_reg_write(RBCR0, 0x00);
\r
453 etherdev_reg_write(RBCR1, 0x00);
\r
455 // Wait for controller to halt after any current tx completes.
\r
456 while(!(etherdev_reg_read(ISR) & RST)) continue;
\r
458 // Check whether currently transmitting a packet.
\r
459 if(etherdev_reg_read(CR) & TXP)
\r
461 // If neither a successful transmission nor a tx abort error
\r
462 // has occured, then flag current tx packet for resend.
\r
463 if(!((etherdev_reg_read(ISR) & PTX)
\r
464 || (etherdev_reg_read(ISR) & TXE)))
\r
470 // Set transmit configuration register to loopback internally.
\r
471 etherdev_reg_write(TCR, LB0);
\r
473 // Restart the RTL8019AS.
\r
474 etherdev_reg_write(CR, RD2 | STA);
\r
476 // Re-initialise last receive buffer read pointer.
\r
477 etherdev_reg_write(BNRY, ETH_RX_PAGE_START);
\r
479 // Select RTL8019AS register page 1.
\r
480 ETHERDEV_SELECT_REG_PAGE(1);
\r
482 // Re-initialise current packet receive buffer page pointer.
\r
483 etherdev_reg_write(CURR, ETH_RX_PAGE_START);
\r
485 // Select RTL8019AS register page 0.
\r
486 ETHERDEV_SELECT_REG_PAGE(0);
\r
488 // Clear rx buffer overflow & packet received interrupt flags.
\r
489 etherdev_reg_write(ISR, PRX | OVW);
\r
491 // Re-itialise transmit configuration reg for normal operation.
\r
492 etherdev_reg_write(TCR, 0x00);
\r
496 // Retransmit packet in RTL8019AS local tx buffer.
\r
497 etherdev_reg_write(CR, RD2 | TXP | STA);
\r
500 else // Rx buffer has not overflowed, so read a packet into uip_buf.
\r
503 unsigned char next_rx_packet;
\r
504 unsigned char current;
\r
506 // Retrieve packet header. (status, next_ptr, length_l, length_h)
\r
508 // Clear remote DMA complete interrupt status register bit.
\r
509 etherdev_reg_write(ISR, RDC);
\r
511 // Set remote DMA start address registers to packet header.
\r
512 etherdev_reg_write(RSAR0, 0x00);
\r
513 etherdev_reg_write(RSAR1, etherdev_reg_read(BNRY));
\r
515 // Set remote DMA byte count registers to packet header length.
\r
516 etherdev_reg_write(RBCR0, 0x04);
\r
517 etherdev_reg_write(RBCR1, 0x00);
\r
519 // Initiate DMA transfer of packet header.
\r
520 etherdev_reg_write(CR, RD0 | STA);
\r
522 // Drop packet status. We don't use it.
\r
523 etherdev_reg_read(RDMA);
\r
525 // Save next packet pointer.
\r
526 next_rx_packet = etherdev_reg_read(RDMA);
\r
528 // Retrieve packet data length and subtract CRC bytes.
\r
529 len = etherdev_reg_read(RDMA);
\r
530 len += etherdev_reg_read(RDMA) << 8;
\r
533 // Wait until remote DMA operation completes.
\r
534 while(!(etherdev_reg_read(ISR) & RDC)) continue;
\r
536 // Abort/ complete DMA operation.
\r
537 etherdev_reg_write(CR, RD2 | STA);
\r
540 // Retrieve packet data.
\r
542 // Check if incoming packet will fit into rx buffer.
\r
543 if(len <= sizeof(uip_buf))
\r
545 // Clear remote DMA complete interrupt status register bit.
\r
546 etherdev_reg_write(ISR, RDC);
\r
548 // Set remote DMA start address registers to packet data.
\r
549 etherdev_reg_write(RSAR0, 0x04);
\r
550 etherdev_reg_write(RSAR1, etherdev_reg_read(BNRY));
\r
552 // Set remote DMA byte count registers to packet data length.
\r
553 etherdev_reg_write(RBCR0, (unsigned char)(len & 0xFF));
\r
554 etherdev_reg_write(RBCR1, (unsigned char)(len >> 8));
\r
556 // Initiate DMA transfer of packet data.
\r
557 etherdev_reg_write(CR, RD0 | STA);
\r
559 // Read packet data directly into uip_buf.
\r
560 for(i = 0; i < len; i++)
\r
562 *(uip_buf + i) = etherdev_reg_read(RDMA);
\r
565 // Wait until remote DMA operation complete.
\r
566 while(!(etherdev_reg_read(ISR) & RDC)) continue;
\r
568 // Abort/ complete DMA operation.
\r
569 etherdev_reg_write(CR, RD2 | STA);
\r
574 // Incoming packet too big, so dump it.
\r
578 // Advance boundary pointer to next packet start.
\r
579 etherdev_reg_write(BNRY, next_rx_packet);
\r
581 // Select RTL8019AS register page 1.
\r
582 ETHERDEV_SELECT_REG_PAGE(1);
\r
584 // Retrieve current receive buffer page
\r
585 current = etherdev_reg_read(CURR);
\r
587 // Select RTL8019AS register page 0.
\r
588 ETHERDEV_SELECT_REG_PAGE(0);
\r
590 // Check if last packet has been removed from rx buffer.
\r
591 if(next_rx_packet == current)
\r
593 // Clear packet received interrupt flag.
\r
594 etherdev_reg_write(ISR, PRX);
\r