⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socket.lst

📁 W3100是WIZnet公司专门为以太网互联和嵌入式设备推出的硬件TCP/IP协议栈芯片
💻 LST
📖 第 1 页 / 共 5 页
字号:
 664          {
 665   1              I_STATUS[s] = 0;
 666   1              COMMAND(s) = CLISTEN;         // LISTEN
 667   1      }
 668          #endif
 669          
 670          /*
 671          ********************************************************************************
 672          *               Create random value for initial Seq# when establishing TCP connection
 673          *
 674          * Description : In this function, you can add some source codes to create random number for initial Seq#
 675          *     In real, TCP initial SEQ# should be random value. 
 676          *               (Currently, we're using static value in EVB/DK.)
 677          * Arguments   : s - channel number
 678          * Returns     : None
 679          * Note        : API Function
 680          ********************************************************************************
 681          */
 682          void initseqnum(SOCKET s)
 683          {
 684   1              int i;
 685   1      
 686   1              i = s;
 687   1      
 688   1              SEQ_NUM.lVal++;     // Designate initial seq#
 689   1                                  // If you have random number generation function, assign random number instead
             - of SEQ_NUM.lVal++.
 690   1      
 691   1              *TX_WR_PTR(s)       = SEQ_NUM.cVal[0];
 692   1              *(TX_WR_PTR(s) + 1) = SEQ_NUM.cVal[1];
 693   1              *(TX_WR_PTR(s) + 2) = SEQ_NUM.cVal[2];
 694   1              *(TX_WR_PTR(s) + 3) = SEQ_NUM.cVal[3];
 695   1              wait_1us(2);        // Wait until TX_WR_PRT has been written safely. ( Must have delay(1.6us) if next action
             - is to write 4byte-pointer register )
 696   1      
 697   1              *TX_RD_PTR(s)       = SEQ_NUM.cVal[0];
 698   1              *(TX_RD_PTR(s) + 1) = SEQ_NUM.cVal[1];
 699   1              *(TX_RD_PTR(s) + 2) = SEQ_NUM.cVal[2];
 700   1              *(TX_RD_PTR(s) + 3) = SEQ_NUM.cVal[3];
 701   1              wait_1us(2);        // Wait until TX_RD_PRT has been written safely.
 702   1      
 703   1              *TX_ACK_PTR(s)       = SEQ_NUM.cVal[0];
 704   1              *(TX_ACK_PTR(s) + 1) = SEQ_NUM.cVal[1];
 705   1              *(TX_ACK_PTR(s) + 2) = SEQ_NUM.cVal[2];
 706   1              *(TX_ACK_PTR(s) + 3) = SEQ_NUM.cVal[3];
C51 COMPILER V8.02   SOCKET                                                                10/17/2006 16:52:42 PAGE 13  

 707   1      }
 708          
 709          #ifdef  __TCP__
 710          /*
 711          ********************************************************************************
 712          *               Function for sending TCP data.
 713          *
 714          * Description : Function for sending TCP data and Composed of the send() and send_in() functions.
 715          *     The send() function is an application I/F function.
 716          *     It continues to call the send_in() function to complete the sending of the data up to the size of th
             -e data to be sent
 717          *     when the application is called.
 718          *     The send_in() function receives the return value (the size of the data sent), calculates the size of
             - the data to be sent,
 719          *     and calls the send_in() function again if there is any data left to be sent.
 720          * Arguments   : s   - channel number
 721          *               buf - Pointer pointing data to send
 722          *               len - data size to send
 723          * Returns     : Succeed: sent data size, Failed:  -1;
 724          * Note        : API Function
 725          ********************************************************************************
 726          */
 727          int send(SOCKET s, const u_char xdata* buf, u_int len)
 728          {
 729   1              int ptr, size;
 730   1      
 731   1              if (len <= 0) return (0);
 732   1              else 
 733   1              {
 734   2                      ptr = 0;
 735   2                      do {
 736   3                              size = send_in(s, buf + ptr, len);
 737   3                              if (size == -1) return -1;      // Error
 738   3                              len = len - size;
 739   3                              ptr += size;
 740   3                      } while ( len > 0);
 741   2              }
 742   1      
 743   1              return ptr;
 744   1      }
 745          
 746          /*
 747          ********************************************************************************
 748          *               Internal function for sending TCP data.
 749          *
 750          * Description : Called by the send() function for TCP transmission.
 751          *    It first calculates the free transmit buffer size
 752          *    and compares it with the size of the data to be transmitted to determine the transmission size.
 753          *    After calculating the data size, it copies data from TX_WR_PTR.
 754          *    It waits if there is a previous send command in process.
 755          *    When the send command is cleared, it updates the TX_WR_PTR up to the size to be transmitted and perfo
             -rms the send command.
 756          * Arguments   : s   - channel number
 757          *               buf - Pointer pointing data to send
 758          *               len - data size to send
 759          * Returns     : Succeeded: sent data size, Failed: -1
 760          * Note        : Internal Function
 761          ********************************************************************************
 762          */
 763          int send_in(SOCKET s, const u_char xdata * buf, u_int len)
 764          {
 765   1              u_char k;
C51 COMPILER V8.02   SOCKET                                                                10/17/2006 16:52:42 PAGE 14  

 766   1              int size;
 767   1              un_l2cval wr_ptr, ack_ptr;
 768   1              u_char * send_ptr;
 769   1      
 770   1      S_START:
 771   1              EX0 = 0;
 772   1              k = *SHADOW_TXWR_PTR(s);        // Must read the shadow register for reading 4byte pointer registers
 773   1              wait_1us(2);                    // wait for reading 4byte pointer registers safely
 774   1              wr_ptr.cVal[0] = *TX_WR_PTR(s);
 775   1              wr_ptr.cVal[1] = *(TX_WR_PTR(s) + 1);
 776   1              wr_ptr.cVal[2] = *(TX_WR_PTR(s) + 2);
 777   1              wr_ptr.cVal[3] = *(TX_WR_PTR(s) + 3);
 778   1      
 779   1              k = *SHADOW_TXACK_PTR(s);       // Must read the shadow register for reading 4byte pointer registers
 780   1              wait_1us(2);                    // wait for reading 4byte pointer registers safely
 781   1              ack_ptr.cVal[0] = *TX_ACK_PTR(s);
 782   1              ack_ptr.cVal[1] = *(TX_ACK_PTR(s) + 1);
 783   1              ack_ptr.cVal[2] = *(TX_ACK_PTR(s) + 2);
 784   1              ack_ptr.cVal[3] = *(TX_ACK_PTR(s) + 3);
 785   1              EX0 = 1;
 786   1      
 787   1              // Calculate send free buffer size
 788   1              if (wr_ptr.lVal >= ack_ptr.lVal) size = SSIZE[s] - (wr_ptr.lVal - ack_ptr.lVal);
 789   1              else size = SSIZE[s] - (0 - ack_ptr.lVal + wr_ptr.lVal);
 790   1      
 791   1              
 792   1              if (size > SSIZE[s])                                                            // Recalulate after some 
             -delay because of error in pointer caluation
 793   1              {
 794   2                      if (select(s, SEL_CONTROL) != SOCK_ESTABLISHED) return -1;              // Error
 795   2      
 796   2                      wait_1ms(1);
 797   2      #ifdef DEBUG
              //              printf("1 ");
              //              printf("%.8lx ", wr_ptr.lVal);
              //              printf("%.8lx\r\n", ack_ptr.lVal);
                              PutString("send_in() at S_START : ");PutHTOA(s); PutString(" : "); PutLTOA(wr_ptr.lVal) ; PutString(" : 
             -");PutLTOA(ack_ptr.lVal) ;PutStringLn("");
              #endif
 803   2                      goto S_START;
 804   2              }
 805   1      
 806   1              if (size == 0)                                                                  // Wait when previous sen
             -ding has not finished yet and there's no free buffer
 807   1              {
 808   2                      if (select(s, SEL_CONTROL) != SOCK_ESTABLISHED) return -1;              // Error
 809   2                      wait_1ms(1);
 810   2      #ifdef DEBUG
                              PutStringLn("send_in() at S_START : size == 0");
              #endif
 813   2                      goto S_START;
 814   2              }
 815   1              else if (size < len)    len = size;
 816   1      
 817   1              send_ptr = (u_char *)( SBUFBASEADDRESS[s] + (UINT)(wr_ptr.lVal & SMASK[s]));    // Calculate pointer to d
             -ata copy
 818   1              write_data(s, buf, send_ptr, len);                                              // data copy
 819   1      
 820   1              while (COMMAND(s) & CSEND)                                                      // Confirm send command
 821   1                      if (select(s, SEL_CONTROL) != SOCK_ESTABLISHED) return -1;                  // Error
 822   1      
 823   1              wr_ptr.lVal = wr_ptr.lVal + len;                                                // tx_wr_ptr update
C51 COMPILER V8.02   SOCKET                                                                10/17/2006 16:52:42 PAGE 15  

 824   1              *TX_WR_PTR(s) = wr_ptr.cVal[0];
 825   1              *(TX_WR_PTR(s) + 1) = wr_ptr.cVal[1];
 826   1              *(TX_WR_PTR(s) + 2) = wr_ptr.cVal[2];
 827   1              *(TX_WR_PTR(s) + 3) = wr_ptr.cVal[3];
 828   1      
 829   1              COMMAND(s) = CSEND;                                                             // SEND
 830   1      
 831   1          return      (len);
 832   1      }
 833          
 834          /*
 835          ********************************************************************************
 836          *               TCP data receiving function.
 837          *
 838          * Description : This function is for receiving TCP data.
 839          *     The recv() function is an application I/F function. It continues to wait for as much data as the app
             -lication wants to receive.
 840          * Arguments   : s   - channel number
 841          *               buf - Pointer where the data to be received is copied
 842          *               len - Size of the data to be received
 843          * Returns     : Succeeded: received data size, Failed: -1
 844          * Note        : API Fcuntion
 845          ********************************************************************************
 846          */
 847          int recv(SOCKET s, const u_char xdata* buf, u_int len)
 848          {
 849   1              u_char k;
 850   1              u_int size;
 851   1              un_l2cval wr_ptr, rd_ptr;
 852   1              u_char * recv_ptr;
 853   1      
 854   1      R_START:
 855   1              EX0 = 0;
 856   1              k = *SHADOW_RXWR_PTR(s);        // Must read the shadow register for reading 4byte pointer registers
 857   1              wait_1us(2);                    // wait for reading 4byte pointer registers safely
 858   1              wr_ptr.cVal[0] = *RX_WR_PTR(s);
 859   1              wr_ptr.cVal[1] = *(RX_WR_PTR(s) + 1);
 860   1              wr_ptr.cVal[2] = *(RX_WR_PTR(s) + 2);
 861   1              wr_ptr.cVal[3] = *(RX_WR_PTR(s) + 3);
 862   1      
 863   1              k = *SHADOW_RXRD_PTR(s);        // Must read the shadow register for reading 4byte pointer registers
 864   1              wait_1us(2);                    // wait for reading 4byte pointer registers safely
 865   1              rd_ptr.cVal[0] = *RX_RD_PTR(s);
 866   1              rd_ptr.cVal[1] = *(RX_RD_PTR(s) + 1);
 867   1              rd_ptr.cVal[2] = *(RX_RD_PTR(s) + 2);
 868   1              rd_ptr.cVal[3] = *(RX_RD_PTR(s) + 3);
 869   1              EX0 = 1;
 870   1      
 871   1              // calculate received data size
 872   1              if ( len <= 0 ) return (0);
 873   1              else if (wr_ptr.lVal >= rd_ptr.lVal) size = wr_ptr.lVal - rd_ptr.lVal;
 874   1              else size = 0 - rd_ptr.lVal + wr_ptr.lVal;
 875   1      
 876   1              
 877   1              if (size < len)                                                                 // Wait until receiving i
             -s done when received data size is less then len
 878   1              {
 879   2                      if (select(s, SEL_CONTROL) != SOCK_ESTABLISHED) return -1;              // Error
 880   2                      wait_1ms(1);  
 881   2      #ifdef DEBUG
              //              printf("size < len\r\n");
                              PutStringLn("size < len");
C51 COMPILER V8.02   SOCKET                                                                10/17/2006 16:52:42 PAGE 16  

              #endif
 885   2                      goto R_START;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -