📄 rtl8019.lst
字号:
227 1 WriteReg(RBCRH_WPAGE0,(unsigned char)((size>>8)&0x00ff));
228 1 WriteReg(RBCRL_WPAGE0,(unsigned char)size);
229 1 WriteReg(CR,(0x00 | CR_REMOTE_WRITE | CR_START_COMMAND));
230 1 for(Endp = buff + size; buff < Endp;)
231 1 {
232 2 WriteReg(REMOTE_DMA_PORT,*(buff++));
233 2 }
234 1 /* complete dma */
235 1 WriteReg(RBCRH_WPAGE0,0);
236 1 WriteReg(RBCRL_WPAGE0,0);
237 1 WriteReg(0x00,((PrePage&0xC0) | CR_ABORT_COMPLETE_DMA | CR_START_COMMAND));
238 1 //printf("RTL8019AS初始化完成!");
239 1 }
240
241 /* read rlt ram data to buffer */
C51 COMPILER V7.06 RTL8019 08/08/2007 10:09:40 PAGE 5
242 void RTLReadRam(unsigned int address,unsigned int size,unsigned char xdata * buff) reentrant
243 {
244 1 unsigned char xdata * Endp;
245 1 unsigned char PrePage; /* store page */
246 1
247 1 PrePage = ReadReg(CR);
248 1 RTLPage(0);
249 1 WriteReg(RSARH_WPAGE0,(unsigned char)((address>>8)&0x00ff));
250 1 WriteReg(RSARL_WPAGE0,(unsigned char)address);
251 1 WriteReg(RBCRH_WPAGE0,(unsigned char)((size>>8)&0x00ff));
252 1 WriteReg(RBCRL_WPAGE0,(unsigned char)size);
253 1 WriteReg(CR,(0x00 | CR_REMOTE_READ | CR_START_COMMAND));
254 1 for(Endp = buff + size; buff < Endp;)
255 1 {
256 2 *(buff++) = ReadReg(REMOTE_DMA_PORT);
257 2 }
258 1 /* complete dma */
259 1 WriteReg(RBCRH_WPAGE0,0);
260 1 WriteReg(RBCRL_WPAGE0,0);
261 1 WriteReg(CR,((PrePage&0xC0) | CR_ABORT_COMPLETE_DMA | CR_START_COMMAND));
262 1 }
263 /* call this function to send a packet by RTL8019. packet store in ram
264 starts at 'buffer' and its size is 'size'. 'size' should not large than
265 MAX_PACKET_SIZE or the excess data will be discard. */
266 unsigned char RTLSendPacket(unsigned char xdata * buffer,unsigned int size) reentrant
267 {
268 1 unsigned char StartPage;
269 1 unsigned char PrePage;
270 1
271 1 /* if send is already running */
272 1 if(InSending == TRUE)
273 1 return FALSE;
274 1 else
275 1 InSending = TRUE;
276 1 /* store page */
277 1 PrePage = ReadReg(CR);
278 1
279 1 /* check pakcet size */
280 1 if(size < MIN_PACKET_SIZE)
281 1 {
282 2 size = MIN_PACKET_SIZE;
283 2 }
284 1 else
285 1 {
286 2 if(size > MAX_PACKET_SIZE)
287 2 size = MAX_PACKET_SIZE;
288 2 }
289 1
290 1 /* write packet to ram */
291 1 if(LastSendStartPage == SEND_START_PAGE0)
292 1 {
293 2 StartPage = SEND_START_PAGE1;
294 2 LastSendStartPage = SEND_START_PAGE1;
295 2 }
296 1 else
297 1 {
298 2 StartPage = SEND_START_PAGE0;
299 2 LastSendStartPage = SEND_START_PAGE0;
300 2 }
301 1 RTLWriteRam((unsigned int)(((unsigned int)StartPage)<<8),size,buffer);
302 1
303 1 /* wait for last time trasmition to complete */
C51 COMPILER V7.06 RTL8019 08/08/2007 10:09:40 PAGE 6
304 1 while((ReadReg(CR) & CR_TXP) == CR_TXP);
305 1
306 1 /* write trasmit start page and size */
307 1 RTLPage(0);
308 1 WriteReg(TPSR_WPAGE0,StartPage); /* TPSR */
309 1 WriteReg(TBCRL_WPAGE0,(unsigned char)size);/*low */
310 1 WriteReg(TBCRH_WPAGE0,(unsigned char)((size>>8)&0x00ff)); /*high*/
311 1 WriteReg(CR,((PrePage&0xC0) | CR_ABORT_COMPLETE_DMA | CR_TXP | CR_START_COMMAND));
312 1
313 1 InSending = FALSE;
314 1 return TRUE;
315 1 }
316
317 /* call this function to receive a ethernet packet from RTL8019.
318 return value:
319 NULL: no packet can receive yet.
320 not NULL:
321 a address point to MemHead. This Head contain merory
322 Imformation(memory start address, memory end address ...) of
323 received packet. Memory is allocated by function 'MemAllocate(unsigned int size)'.
324 a example of struct SMemHead is:
325
326 struct SMemHead
327 {
328 unsigned char used; // if in using
329 unsigned char xdata *pStart; // the start address of memory
330 unsigned char xdata *pEnd;
331 };
332
333 You can use your own struct SMemHead and MemAllocat function in your project.
334 */
335 struct SMemHead xdata * RTLReceivePacket() reentrant
336 {
337 1 unsigned char curr,bnry;
338 1 unsigned int address;
339 1 unsigned int PacketSize;
340 1 struct SMemHead xdata *MemHead;
341 1
342 1 /* if send is running don't crrupt RTL register*/
343 1 if(InSending == TRUE)
344 1 return NULL;
345 1
346 1 MemHead = NULL;
347 1
348 1 RTLPage(1);
349 1 curr = ReadReg(CURR_RPAGE1);
350 1 RTLPage(0);
351 1
352 1 /* check if startpage exceed range becasue of unknow error */
353 1 if(StartPageOfPacket >= RECEIVE_STOP_PAGE || StartPageOfPacket < RECEIVE_START_PAGE)
354 1 {
355 2 /* use curr as the StartPageOfPacket in this case */
356 2 StartPageOfPacket = curr;
357 2 return NULL;
358 2 }
359 1
360 1 /* check if there is packets to read */
361 1 if(StartPageOfPacket == curr)
362 1 return NULL;
363 1
364 1 /*
365 1 * read a packet
C51 COMPILER V7.06 RTL8019 08/08/2007 10:09:40 PAGE 7
366 1 */
367 1
368 1 /* read packet head imformation */
369 1 address = ((unsigned int)StartPageOfPacket)<<8;
370 1 RTLReadRam(address,4,Head);
371 1
372 1 /* check rsr, if isn't a good packet no read */
373 1 if(Head[0] & RSR_RECEIVE_NO_ERROR)
374 1 {
375 2 /* this is a good packet */
376 2
377 2 /* packet size, sub 4 bytes, this 4 byte is MAC checksum */
378 2 PacketSize = ((unsigned int)Head[3])*256 + Head[2] - 4;
379 2
380 2 /* allocate buffer and read packet into buffer */
381 2 if((MemHead = MemAllocate(PacketSize)) != NULL)
382 2 {
383 3 /* if packet is put from bnry+1 to receive_stop_page and receive
384 3 start page to next packet startpage, that is if bnry+1 > next
385 3 packet start page and next start page != receive_start_page,
386 3 we need read by two times. the first time from bnry+1 to receive
387 3 _stop_page, the second time from receive start page to next packet
388 3 startpage.
389 3 */
390 3 address += 4;
391 3 if(StartPageOfPacket > Head[1] && Head[1] != RECEIVE_START_PAGE)
392 3 {
393 4 RTLReadRam(address,(unsigned int)((((unsigned int)RECEIVE_STOP_PAGE)<<8) - address),MemHead->pStart);
-/* read from rtl */
394 4 RTLReadRam((unsigned int)(((unsigned int)RECEIVE_START_PAGE)<<8),(unsigned int)(PacketSize - ((((unsig
-ned int)RECEIVE_STOP_PAGE)<<8) - address)),
395 4 MemHead->pStart + ((((unsigned int)RECEIVE_STOP_PAGE)<<8) - address)); /* read from rtl */
396 4 }
397 3 else
398 3 {
399 4 RTLReadRam(address,PacketSize,MemHead->pStart); /* read from rtl */
400 4 }
401 3
402 3 }
403 2 }
404 1
405 1 /* get next packet start page */
406 1 StartPageOfPacket = Head[1];
407 1
408 1 /* reset bnry */
409 1 bnry = StartPageOfPacket - 1;
410 1 if(bnry < RECEIVE_START_PAGE)
411 1 bnry = RECEIVE_STOP_PAGE - 1;
412 1 WriteReg(BNRY_WPAGE0,bnry);
413 1
414 1 return MemHead;
415 1 }
416
417 //ARP Test
418 unsigned char xdata TestPacket[42];
419 void RTL8019SendPacketTest()
420 {
421 1 /* send a arp request packet, src ip = 192.168.2.13, dest ip = 192.168.2.14,
422 1 if you config your computer's ip as 192.168.2.14, then after run this function,
423 1 your computer will receive the packet, and when you type "arp -a" in windows
424 1 command window(dos window) you will see a arp entry of 52 54 4c 30 2e 2f.
425 1 this indicate a packet has been successful sent
C51 COMPILER V7.06 RTL8019 08/08/2007 10:09:40 PAGE 8
426 1 */
427 1 /* this is a arp broad cast packet */
428 1 //Dest MAC
429 1 TestPacket[0] = 0xff;
430 1 TestPacket[1] = 0xff;
431 1 TestPacket[2] = 0xff;
432 1 TestPacket[3] = 0xff;
433 1 TestPacket[4] = 0xff;
434 1 TestPacket[5] = 0xff;
435 1 //Scr MAC
436 1 TestPacket[6] = 0x52;
437 1 TestPacket[7] = 0x54;
438 1 TestPacket[8] = 0x4c;
439 1 TestPacket[9] = 0x30;
440 1 TestPacket[10] = 0x2e;
441 1 TestPacket[11] = 0x2f;
442 1 //Type:arp
443 1 TestPacket[12] = 0x08;
444 1 TestPacket[13] = 0x06;
445 1
446 1 TestPacket[14] = 0x00;
447 1 TestPacket[15] = 0x01;
448 1 TestPacket[16] = 0x08;
449 1 TestPacket[17] = 0x00;
450 1 TestPacket[18] = 0x06;
451 1 TestPacket[19] = 0x04;
452 1 TestPacket[20] = 0x00;
453 1 TestPacket[21] = 0x01;
454 1
455 1 /* ARPPacket->IPDestAddr = DestIP:192.168.1.62;*/
456 1 TestPacket[22] = 0xc0;
457 1 TestPacket[23] = 0xa8;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -