?? main.c
字號(hào):
///
// Copyright (c) 2003, Wolver Wang, MinShan Inc. R&D Center
// wolver@minshan-inc.com
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. All advertising materials mentioning features or use of this software
// must display the following acknowledgement:
// This product includes software developed by Wolver Wang.
// 4. The name of the author may not be used to endorse or promote
// products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
///
///
// Copyright (c) 2003, Wolver Wang, MinShan Inc. R&D Center
// wolver@minshan-inc.com
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. All advertising materials mentioning features or use of this software
// must display the following acknowledgement:
// This product includes software developed by Wolver Wang.
// 4. The name of the author may not be used to endorse or promote
// products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
///
#include "system.h"
#include "tcpip.h"
#include "drivers.h"
// 定義應(yīng)用:1 表示開(kāi)啟功能,0 表示關(guān)閉功能
#define cTCP_RS232 1 // TCP <-> RS232 的應(yīng)用,只用于服務(wù)模式
#define cTCP_ADAC 1 // TCP <-> Audio, 主要用于服務(wù),也可以用于客戶(hù)。要求高帶寬: > 912Kbit
// 分配本地用戶(hù)自定義服務(wù)模式應(yīng)用TCP端口號(hào),不能與知名端口相同!如:23, 80
// 注意:對(duì)不同的TCP事件使用不同的本地端口號(hào),有助于快速查找TCP事件而不需要判斷IP是否相同!
// 這樣做能使本地快速響應(yīng)。
#define cTCP_ListenPort_TEST 0x1000 // 4096
#if cTCP_RS232 == 1
#define cTCP_ListenPort_RS232 0x2000 // 8192
#endif
#if cTCP_ADAC == 0
#define cTCP_ListenPort_ADAC 0x3000 // 12288
#endif
// 客戶(hù)應(yīng)用模式的本地TCP端口號(hào)。不能與知名端口相同!如:23, 80
// 注意:對(duì)不同的TCP事件使用不同的本地端口號(hào)(包括:本地偵聽(tīng)端口),有助于快速查找TCP事件而
// 不需要判斷IP是否相同!這樣做能使本地快速響應(yīng)。
#if (cTCP_ADAC == 1) && (TCP_ACTIVE_OPEN == 1)
#define cTCP_ActivePort_ADAC 0x3001 // 12289
#endif
// 分配系統(tǒng)應(yīng)用臨時(shí)緩沖區(qū)(按 wrod 存儲(chǔ))
UINT16 guwAppBuf[cAppSizeMax];
//--------------------------------------------------------------------------------------
main(){
#if TCP_ACTIVE_OPEN == 1
UINT16 temp[2];
#endif
// 1. Hardware initialize: SPCE061A
SP_IO_INIT();
// 2. Open and Enable Hardware interrupt 2Hz and Clear WatchDog!
SP_OpenTime2();
// 3. Hardware initialize: RTL8019AS
RTL8019AS_RESET();
RTL8019AS_INIT();
// 4. vIP4 TCP/IP initialize
msip_Init();
// 5. We listen test port
msip_Listen(cTCP_ListenPort_TEST); // 用于偵聽(tīng)來(lái)自鏈路測(cè)試的TCP包
#if cTCP_RS232 == 1
SP_UART_INIT(C_UART_Baud_115200); // Hardware initialize: UART of SPCE061A
msip_Listen(cTCP_ListenPort_RS232); // 用于偵聽(tīng)來(lái)自RS232的TCP包
#endif
#if cTCP_ADAC == 1
// SP_ADAC_INIT(cSample_4096); // Open ADAC
// SP_ADAC_INIT(cSample_8192); // Open ADAC
// SP_ADAC_INIT(cSample_16384); // Open ADAC
// SP_ADAC_INIT(cSample_32768); // Open ADAC
// SP_CLOSE_FIQ(); // 關(guān)閉FIQ中斷,同時(shí)也禁止了ADAC
msip_Listen(cTCP_ListenPort_ADAC); // 用于偵聽(tīng)來(lái)自遠(yuǎn)端的Audio的TCP包
#endif
#if (cTCP_ADAC == 1) && (TCP_ACTIVE_OPEN == 1)
// for test audio, wo active link remote: 192.168.0.60
temp[0] = ((192<<8)|168);
temp[1] = ((0<<8)|30);
msip_Connect(cTCP_ActivePort_ADAC, temp, cTCP_ListenPort_ADAC);
#endif
// 6. We do TCP/IP Check Loop
loop:
// 接收新的以太包,并處理
if ((guwEthLen = ether_Receive()) != 0){
switch (cptEthHdrBuf->EthType){
case cEthType_Arp:
msip_Arp_In();
break;
case cEthType_Ip:
msip_Input();
}
}
// ARP表老化處理
if (guwMsg_Route & cM_ARP_TIME){
msip_Arp_Time();
}
// TCP事件輪詢(xún)
if (guwMsg_Route & cM_TCP_PERIODIC){
msip_Periodic();
}
goto loop;
}
// SUB-SUB-SUB-SUB-SUB-SUB-SUB-SUB-SUB-SUB-SUB-SUB-SUB-SUB-SUB-SUB-SUB-SUB-SUB-SUB-SUB
//--------------------------------------------------------------------------------------
//
// |--------------| |-----|----------|------| |------|--------|
// |工業(yè)設(shè)備|RS232| <---> |RS232|核心嵌入板|TCP/IP| <---> |TCP/IP|普通PC機(jī)|
// |--------------| |-----|----------|------| |------|--------|
//
//-------------------------------------------------------------------------------------
void userapp(){
switch (gptConn->LocalPort){
#if cTCP_RS232 == 1
case cTCP_ListenPort_RS232:
goto link_rs232;
#endif
#if cTCP_ADAC == 1 // ADAC 工作時(shí):由于雙向通訊,所以L(fǎng)isten和Active處理是一樣的!
case cTCP_ListenPort_ADAC:
goto link_adac_listen;
#endif
#if (cTCP_ADAC == 1) && (TCP_ACTIVE_OPEN == 1) // ADAC 工作時(shí):由于雙向通訊,所以L(fǎng)isten和Active處理是一樣的!
case cTCP_ActivePort_ADAC:
goto link_adac_active;
#endif
case cTCP_ListenPort_TEST:
goto test_net;
default:
return;
}
#if cTCP_RS232 == 1
link_rs232: // 與RS232透明傳輸通訊:本系統(tǒng)的一個(gè)應(yīng)用。
// 以下事件的過(guò)慮判斷并不按照事件發(fā)生的順序,是因?yàn)橛行┦录ǔV粫?huì)發(fā)生一次,
// 從而在大多數(shù)其它經(jīng)常發(fā)生的事件狀態(tài)下,減少對(duì)那些事件的過(guò)濾判斷,以提高速度!!!
if (msip_Poll() || msip_Acked()){ // 如果RS232有數(shù)據(jù)要發(fā)送,就轉(zhuǎn)發(fā)TCP數(shù)據(jù)段!
if (guwUartRxLen > 0) { // 根據(jù)guwUartRxLen判斷是否轉(zhuǎn)發(fā)RS232數(shù)據(jù)
MEMCPY(guwUartRxLen, guwUartRxBuf, cpTcpData);
guwEthLen = guwUartRxLen;
guwUartRxLen = 0;
gptConn->PollTime = 0; // 清除空閑時(shí)間記數(shù)
} else if (gptConn->PollTime++ > 3*cTCP_MAX_POLL){ // 太長(zhǎng)時(shí)間空閑(900秒),終止連接!
msip_Close();
}
return;
}
if (msip_NewData()){ // 收到TCP數(shù)據(jù)包,轉(zhuǎn)發(fā)給RS232
if (guwEthLen > 0){
SP_UART_TX(guwEthLen, cpTcpData);
guwEthLen = 0;
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -