📄 main.lst
字号:
454 2 }
455 1 }
456
457 //-----------------------------------------------------------------------------
458 // CP220x_Send
459 //-----------------------------------------------------------------------------
460 //
461 // Return Value : None
462 // Parameters :
463 // 1) MACADDRESS* pDestAddr - destination MAC address.
464 // 2) unsigned char* buffer - address of payload.
465 // 3) unsigned int buffer_length - length of payload.
466 // 4) unsigned int packet_type - contents of Ethertype field.
467 //
468 // This function sends an IEEE 802.3 Ethernet packet using the CP220x.
469 // Upon entry, there should be valid data in array <buffer>.
470 //
471 // (8 bytes) 48-bit 48-bit 16-bit 0-1500 bytes
472 // ----------------------------------------------------------------------
473 // | Preamble| SFD | Dest |Source| Type/Length |Data Field | Pad | FCS |
474 // | | | Addr | Addr | Field | | | (CRC) |
475 // ----------------------------------------------------------------------
476 // supplied by | supplied by the MCU | supplied
477 // CP220x | (minimum 64 bytes) | by CP220x
478 //
479 //
480 //-----------------------------------------------------------------------------
481 void CP220x_Send( MACADDRESS* pDestAddr, unsigned char* buffer,
482 unsigned int buffer_length, unsigned int packet_type)
483 {
484 1
485 1 int i;
486 1 unsigned int ramaddr;
487 1
488 1 // Define Macro to increment the RAM address Pointer
489 1 #define INC_RAMADDR ramaddr++; \
C51 COMPILER V8.08 MAIN 11/04/2008 18:45:33 PAGE 9
490 1 RAMADDRH = (ramaddr >> 8);\
491 1 RAMADDRL = (ramaddr & 0x00FF);
492 1
493 1
494 1 // Step 1: Poll TXBUSY until it becomes 0x00
495 1 while(TXBUSY);
496 1
497 1 // Step 2: Set the TXSTARTH:TXSTARTL address to 0x0000
498 1 TXSTARTH = 0x00;
499 1 TXSTARTL = 0x00;
500 1
501 1
502 1 // Step 3: Load data into transmit buffer
503 1 // When the random access method is used, we do not need to check for
504 1 // aborted packets. This method will be slightly slower than the Autowrite
505 1 // method, however, it reduces code space requirements.
506 1
507 1 // Setup RAM Address Pointer To 0x0000
508 1 RAMADDRH = 0x00;
509 1 RAMADDRL = 0x00;
510 1 ramaddr = 0x0000;
511 1
512 1 // Step 3a: Load the destination address
513 1 for(i = 0; i < 6; i++){
514 2
515 2 RAMTXDATA = pDestAddr->Char[i];
516 2 INC_RAMADDR
517 2
518 2 }
519 1
520 1 // Step 3b: Load the source address
521 1 for(i = 0; i < 6; i++){
522 2 RAMTXDATA = MYMAC.Char[i];
523 2 INC_RAMADDR
524 2 }
525 1
526 1 // Step 3c: Load the Type/Length Field
527 1 RAMTXDATA = (packet_type >> 8);
528 1 INC_RAMADDR
529 1
530 1 RAMTXDATA = (packet_type & 0xFF);
531 1 INC_RAMADDR
532 1
533 1
534 1 // Step 3d: Load the packet payload
535 1 for(i = 0; i < buffer_length; i++){
536 2 RAMTXDATA = buffer[i];
537 2 INC_RAMADDR
538 2 }
539 1
540 1 // Step 3e: Pad short packets //将不足64字节长度的数据填充到64字节
541 1 while(ramaddr < 64){
542 2 RAMTXDATA = 0;
543 2 INC_RAMADDR
544 2 }
545 1
546 1 // Set the TXENDH:TXENDL address to <ramaddr - 1>
547 1 ramaddr--;
548 1 TXENDH = (ramaddr >> 8);
549 1 TXENDL = (ramaddr & 0x00FF);
550 1
551 1
C51 COMPILER V8.08 MAIN 11/04/2008 18:45:33 PAGE 10
552 1 // Step 4: Set the TXSTARTH:TXSTARTL address back to 0x0000
553 1 TXSTARTH = 0x00;
554 1 TXSTARTL = 0x00;
555 1
556 1 // Step 5: Write '1' to TXGO to begin transmission
557 1 TXCN = 0x01;
558 1
559 1 }
560
561
562 //-----------------------------------------------------------------------------
563 // CP220x_Receive
564 //-----------------------------------------------------------------------------
565 //
566 // This function reads the current packet from the CP220x receive buffer and
567 // copies it to the passed buffer. The data copied to the buffer includes the
568 // 14-byte Ethernet Header and the Data Field.
569 //
570 // Returns the number of bytes added to the buffer.
571 //
572 // (8 bytes) 48-bit 48-bit 16-bit 0-1500 bytes
573 // --------------------------------------------------------------------------
574 // | Preamble | SFD | Dest | Source | Type/Length | Data Field | Pad | FCS |
575 // | | | Addr | Addr | Field | | | (CRC) |
576 // --------------------------------------------------------------------------
577 // supplied by | supplied by the MCU | supplied by
578 // CP220x | | CP220x
579 //-----------------------------------------------------------------------------
580 unsigned int CP220x_Receive(unsigned char* buffer, unsigned int buffer_length)
581 {
582 1 bit rx_ok;
583 1 bit skip = 0;
584 1 UINT_struct cplen;
585 1 unsigned int i;
586 1
587 1 // Step 1: Check the RXOK bit to see if packet was received correctly
588 1 rx_ok = (CPINFOL & RXOK) && (CPINFOH & RXVALID);
589 1
590 1 // Step 2: If packet received correctly, read the length, otherwise, skip packet.
591 1 if(rx_ok){
592 2
593 2 // Read the packet length
594 2 cplen.Char[0] = CPLENH;
595 2 cplen.Char[1] = CPLENL;
596 2
597 2 } else {
598 2
599 2 // Set packet length to zero
600 2 cplen.Int = 0;
601 2
602 2 // Skip packet
603 2 skip = 1;
604 2 }
605 1
606 1 // Step 3: Read the entire packet from the buffer
607 1
608 1 // If packet will fit in the buffer
609 1 if(buffer_length >= cplen.Int){
610 2
611 2 // Copy entire packet
612 2 for(i = 0; i < cplen.Int; i++){
613 3 *buffer++ = RXAUTORD;
C51 COMPILER V8.08 MAIN 11/04/2008 18:45:33 PAGE 11
614 3 }
615 2
616 2 } else {
617 2
618 2 // Set packet length to zero
619 2 cplen.Int = 0;
620 2
621 2 // Skip packet
622 2 skip = 1;
623 2 }
624 1
625 1 // Step 4: Skip the packet, or clear the valid bit if the entire packet
626 1 // has been unloaded from the buffer.
627 1
628 1 if(skip)
629 1 {
630 2 RXCN |= 0x02; // Skip the packet
631 2 }
632 1
633 1 else
634 1 {
635 2 RXCN |= 0x04; // Clear the valid bit only
636 2 }
637 1
638 1 // If there are no more packets in the receive buffer, enable reception
639 1 if(TLBVALID == 0x00)
640 1 {
641 2 RXCN = 0x00;
642 2 }
643 1
644 1 // Return the number of bytes added to the buffer
645 1 return cplen.Int;
646 1 }
647
648 /********************************************************************/
649 //上位机与下位进行交互程序
650 /********************************************************************/
651 void LightONOFF(bit b)
652 {
653 1 if (b)
654 1 {
655 2 led=1;
656 2 }
657 1 else
658 1 {
659 2 led=0;
660 2 }
661 1
662 1
663 1 }
664 //-----------------------------------------------------------------------------
665 // End Of File
666 //-----------------------------------------------------------------------------
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 960 ----
CONSTANT SIZE = 97 ----
XDATA SIZE = 1788 ----
PDATA SIZE = ---- ----
DATA SIZE = 2 28
IDATA SIZE = 2 ----
C51 COMPILER V8.08 MAIN 11/04/2008 18:45:33 PAGE 12
BIT SIZE = ---- 3
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -