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

📄 tcp_ip.h

📁 Analog公司的ADSP_BF532上面实现以太网接口的源码
💻 H
字号:
//##############################################################
//#
//# TCP_IP.H
//#
//# ADSP-21535 Embedded Web Server Project
//#
//# (c) ANALOG DEVICES 2002
//#     eDSP Division
//#     Stefan Hacker
//#     28-DEC-2002
//#
//# History
//#     16-APR-2003 HS  release 1.0
//#

#ifndef __tcp_ip_h__
#define __tcp_ip_h__

#include "ez_lan_types.h"

////////////////////////////
// EZ-LAN TCP/IP stack definitions
// prototypes
void TCP_State_Machine();

// Handlers for incoming frames
void TCP_Proc_EthBroadcast_Frame();
void TCP_Proc_EthIA_Frame();
void TCP_Proc_ICMP_Frame();
void TCP_Proc_TCP_Frame();

// fill TX-buffers
void TCP_Prep_ARP_REQUEST();
void TCP_Prep_ARP_ANSWER();
void TCP_Prep_ICMP_ECHO_REPLY();
void TCP_Prep_TCP_FRAME(WORD TCPCode);
void TCP_Prep_TCP_DATA_FRAME();

// general help functions
void TCP_StartRetryTimer();
void TCP_StartTimeWaitTimer();
void TCP_RestartTimer();
void TCP_StopTimer();
void TCP_HandleRetransmission();
void TCP_HandleTimeout();
WORD TCP_CalcChecksum(BYTE *Start, WORD Count, BYTE IsTCP);
void TCP_Copy_Frame(WORD length, BYTE *Frame);

// dedicated small supports
inline WORD swap_bytes(DWORD in);
inline void WriteDWBE(BYTE* Add, DWORD Data);
inline WORD get_cycles();

// API functions
void TCP_init(void);                              // setup state machine flags
void TCP_PassiveOpen(void);                       // listen for a connection
void TCP_ActiveOpen(void);                        // open connection
void TCP_Close(void);                             // close connection
void TCP_ReleaseRxBuffer(void);                   // indicate to discard rec'd packet
void TCP_TransmitTxBuffer(void);                  // initiate transfer after TxBuffer is filled

////////////////////////////
// EZ-LAN TCP/IP stack definitions

// Own MAC_IA Address
#define MAC_IA_1             00 // ''
#define MAC_IA_2             69 // 'E'
#define MAC_IA_3             90 // 'Z'
#define MAC_IA_4             76 // 'L'
#define MAC_IA_5             65 // 'A'
#define MAC_IA_6             79 // 'M'

#ifndef ADIIP
    // IP Addresses
    #define LOCAL_IP_1          192     // Local IP address
    #define LOCAL_IP_2          168
    #define LOCAL_IP_3            1
    #define LOCAL_IP_4           17

    #define GATEWAY_IP_1        192     // standard gateway
    #define GATEWAY_IP_2        168
    #define GATEWAY_IP_3          1
    #define GATEWAY_IP_4          1
   
    #define SUBNETMASK_1        255     // Sub Net Mask
    #define SUBNETMASK_2        255
    #define SUBNETMASK_3        255
    #define SUBNETMASK_4          0
#else
    // IP Addresses
    #define LOCAL_IP_1          192     // Local IP address
    #define LOCAL_IP_2          168
    #define LOCAL_IP_3            1
    #define LOCAL_IP_4           17

    #define GATEWAY_IP_1        192     // standard gateway
    #define GATEWAY_IP_2        168
    #define GATEWAY_IP_3          1
    #define GATEWAY_IP_4          1

    #define SUBNETMASK_1        255     // Sub Net Mask
    #define SUBNETMASK_2        255
    #define SUBNETMASK_3        255
    #define SUBNETMASK_4          0
#endif


// Timeouts and Buffer Sizes
#define TCP_RETRY_TIMEOUT     8     // wait max. 8 x 262ms for an ACK (about 2 sec.)
#define TCP_FIN_TIMEOUT       2     // max. time to wait for an ACK of a FIN
                                    //   before closing TCP state-machine (about 0.5 s)
#define TCP_MAX_RETRIES       4     // nr. of resendings before reset conn.
                                    //   total nr. of transmissions = MAX_RETRYS + 1

#define MAX_TCP_TX_SIZE    1024     // max. outgoing TCP data size (even!)
#define MAX_TCP_RX_SIZE    1400     // max. incoming TCP data size (even!)
                                        
#define MAX_ETH_TX_SIZE      60     // 2nd buffer, used for ARP, ICMP, TCP (even!)
                                    //    enough to echo 32 byte via ICMP

#define DEFAULT_TTL          64     // TTL (Time To Live) sent with packets 
 
// Ethernet network layer definitions
#define ETH_DA_OFS            0     // Destination MAC Address Offset (48 bit)
#define ETH_SA_OFS            6     // Source MAC Address Offset (48 bit)
#define ETH_TYPE_OFS         12     // Ethernet Type field offset (16 bit)
#define ETH_DATA_OFS         14     // Frame Data Offset
#define ETH_HEADER_SIZE      14     // Number of Bytes in TCP Header

#define FRAME_ARP            0x0608 // frame types (stored in Type/Length field)
#define FRAME_IP             0x0008

// IPv4 layer definitions
#define IP_VER_IHL_TOS_OFS   ETH_DATA_OFS + 0    // Version, Header Length, Type of Service
#define IP_TOTAL_LENGTH_OFS  ETH_DATA_OFS + 2    // IP Frame's Total Length
#define IP_IDENT_OFS         ETH_DATA_OFS + 4    // Identifying Value
#define IP_FLAGS_FRAG_OFS    ETH_DATA_OFS + 6    // Flags and Fragment Offset
#define IP_TTL_PROT_OFS      ETH_DATA_OFS + 8    // Frame's Time to Live, Protocol
#define IP_HEAD_CHKSUM_OFS   ETH_DATA_OFS + 10   // IP Frame's Header Checksum
#define IP_SOURCE_OFS        ETH_DATA_OFS + 12   // Source Address (32 Bit)
#define IP_DESTINATION_OFS   ETH_DATA_OFS + 16   // Destination Address (32 Bit)
#define IP_DATA_OFS          ETH_DATA_OFS + 20   // Frame Data (if no options)
#define IP_HEADER_SIZE       20                  // w/o options

#define IP_VER_IHL           0x4500              // IPv4, Header Length = 5x32 bit
#define IP_TOS_D             0x0010              // TOS low delay
#define IP_TOS_T             0x0008              // TOS high throughput
#define IP_TOS_R             0x0004              // TOS high reliability

#define IP_FLAG_DONTFRAG     0x4000              // don't fragment IP frame
#define IP_FLAG_MOREFRAG     0x2000              // more fragments available
#define IP_FRAGOFS_MASK      0x1FFF              // indicates where this fragment belongs

#define PROT_ICMP            1                   // Internet Control Message Protocol
#define PROT_TCP             6                   // Transmission Control Protocol
#define PROT_UDP             17                  // User Datagram Protocol

// ARP definitions
#define ARP_HARDW_OFS        ETH_DATA_OFS + 0    // Hardware address type
#define ARP_PROT_OFS         ETH_DATA_OFS + 2    // Protocol
#define ARP_HLEN_PLEN_OFS    ETH_DATA_OFS + 4    // byte length of each hardw. / prot. address
#define ARP_OPCODE_OFS       ETH_DATA_OFS + 6    // Opcode
#define ARP_SENDER_HA_OFS    ETH_DATA_OFS + 8    // Hardw. address of sender of this packet
#define ARP_SENDER_IP_OFS    ETH_DATA_OFS + 14   // IP address of sender
#define ARP_TARGET_HA_OFS    ETH_DATA_OFS + 18   // Hardw. address of target of this packet
#define ARP_TARGET_IP_OFS    ETH_DATA_OFS + 24   // IP address of target
#define ARP_FRAME_SIZE       28

#define HARDW_ETH10          0x0100              // hardware-type Ethernet
#define IP_HLEN_PLEN         0x0406              // IP = 4 byte long, MAC = 6 byte long
#define OP_ARP_REQUEST       0x0100              // operations for ARP-frames
#define OP_ARP_ANSWER        0x0200

// ICMP definitions
#define ICMP_TYPE_CODE_OFS   IP_DATA_OFS + 0     // type of message
#define ICMP_CHKSUM_OFS      IP_DATA_OFS + 2     // checksum of ICMP-message (16 bit)
#define ICMP_DATA_OFS        IP_DATA_OFS + 4
#define ICMP_HEADER_SIZE     4

#define ICMP_ECHO            8                   // message is an echo request
#define ICMP_ECHO_REPLY      0                   // message is an echo reply

// TCP layer definitions
#define TCP_SRCPORT_OFS      IP_DATA_OFS + 0     // Source Port (16 bit)
#define TCP_DESTPORT_OFS     IP_DATA_OFS + 2     // Destination Port (16 bit)
#define TCP_SEQNR_OFS        IP_DATA_OFS + 4     // Sequence Number (32 bit)
#define TCP_ACKNR_OFS        IP_DATA_OFS + 8     // Acknowledge Number (32 bit)
#define TCP_DATA_CODE_OFS    IP_DATA_OFS + 12    // Data Offset and Control Bits (16 bit)
#define TCP_WINDOW_OFS       IP_DATA_OFS + 14    // Window Size (16 bit)
#define TCP_CHKSUM_OFS       IP_DATA_OFS + 16    // Checksum Field (16 bit)
#define TCP_URGENT_OFS       IP_DATA_OFS + 18    // Urgent Pointer (16 bit)
#define TCP_DATA_OFS         IP_DATA_OFS + 20    // Frame Data (if no options)
#define TCP_HEADER_SIZE      20                  // size w/o options

#define DATA_OFS_MASK        0xF000              // number of 32 bit words in the TCP Header

#define TCP_CODE_FIN         0x0001
#define TCP_CODE_SYN         0x0002
#define TCP_CODE_RST         0x0004
#define TCP_CODE_PSH         0x0008
#define TCP_CODE_ACK         0x0010
#define TCP_CODE_URG         0x0020

#define TCP_OPT_MSS          0x0204              // Type 2, Option Length 4 (Max. Segment Size)
#define TCP_OPT_MSS_SIZE     4

#define TCP_TX_FRAME1_SIZE   ETH_HEADER_SIZE + IP_HEADER_SIZE + TCP_HEADER_SIZE + MAX_TCP_TX_SIZE
#define TCP_TX_FRAME2_SIZE   ETH_HEADER_SIZE + MAX_ETH_TX_SIZE

// define some TCP standard-ports, useful for testing...
#define TCP_PORT_ECHO        7                   // echo
#define TCP_PORT_DISCARD     9                   // discard
#define TCP_PORT_DAYTIME     13                  // daytime
#define TCP_PORT_QOTD        17                  // quote of the day
#define TCP_PORT_CHARGEN     19                  // character generator
#define TCP_PORT_FTP         21                  // FTP
#define TCP_PORT_HTTP        80                  // word wide web HTTP

// constants
#define TCP_SEND_FRAME1                 0x01
#define TCP_SEND_FRAME2                 0x02

#define TCP_ACTIVE_OPEN                 0x01      // easyWEB shall initiate a connection
#define TCP_IP_ADDR_RESOLVED            0x02      // IP sucessfully resolved to MAC
#define TCP_TIMER_RUNNING               0x04
#define TCP_TIMER_TYPE_RETRY            0x08
#define TCP_CLOSE_REQUESTED             0x10


// API global vars and flags
#define TCP_SOCK_ACTIVE                 0x01      // state machine NOT closed
#define TCP_SOCK_CONNECTED              0x02      // user may send & receive data
#define TCP_SOCK_DATA_AVAILABLE         0x04      // new data available
#define TCP_SOCK_TX_BUF_RELEASED        0x08      // user may fill buffer

#define TCP_SOCK_ERROR_MASK             0xF0      // bit-mask to check for errors
#define TCP_SOCK_ERR_OK                 0x00      // no error
#define TCP_SOCK_ERR_ARP_TIMEOUT        0x10      // timeout waiting for an ARP-REPLY
#define TCP_SOCK_ERR_TCP_TIMEOUT        0x20      // timeout waiting for an ACK
#define TCP_SOCK_ERR_CONN_RESET         0x30      // connection was reset by the other TCP
#define TCP_SOCK_ERR_REMOTE             0x40      // remote TCP caused fatal error
#define TCP_SOCK_ERR_ETHERNET           0x50      // network interface error (timeout)

// API buffer-pointers
#define TCP_TX_BUF      ((BYTE*)TCP_TxFrame1 + ETH_HEADER_SIZE + IP_HEADER_SIZE + TCP_HEADER_SIZE)
#define TCP_RX_BUF      ((BYTE*)TCP_RxTCPBuffer) 

#endif //__tcp_ip_h__

⌨️ 快捷键说明

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