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

📄 rs232.c

📁 RTL8019以太网开发板资料
💻 C
📖 第 1 页 / 共 2 页
字号:
/************************************************************************
FileName:      RS232.c
Description:   RS232 protocol process  
Date:          2007 09 21                                                       
************************************************************************/
#include "Main.h"
#include "EEPROM.h"
#include "ip.h"
#include "arp.h"
#include "RTL8019.h"
#include "TCP.h"
/************************************************************************/
bit Rcv_flag;			   //串口接收到正确的标志位
bit Rcv_ok;					//串口接收过完成的标志位
bit Key_flag;					//按键标志位
WORK16 RcvDelayCount;		//按键延时记数器
WORK16 KeyDelayCount;		//按键延时记数器
UWORK16  RcvCount;                        			     // 接收记数器
UWORK16  RcvLen;                        			     // 接收长度 
xdata  UWORK8 Rcv_buf[UART_RCV_NUM];
xdata ST_UART_STATISTICS gstUARTStatistics;                      // 串口通信错误统计 
xdata WORK16   SystemTimer_buf[12];                 // 系统相对定时器
/*********************************************************************/
     /* 网口参数 */
xdata UWORK8  gucaNetPara[NET_PARA_LEN];
xdata UWORK8  gucOpenCount;                                      /* 3秒计数 */
xdata UWORK8  gucSecond;                                         /* 系统运行时间,秒 */
xdata UWORK8  gucMinute;                                         /* 系统运行时间,分钟 */
xdata UWORK8  gucHour;                                           /* 系统运行时间,小时 */
xdata UWORK16 gusDate;                                           /* 系统运行时间,天数 */
xdata UWORK16 gusStopRun;

/*********************************************************************
函数名:       void key_judge(void)
功能:         按键时间判断,如果是3秒中就初始化系统
输入:         None 
输出:         None
返回:         None
日期:         2004/02/09
*********************************************************************/
void key_judge(void)
{
    if(0 == Key)                           // 使用按钮对设置进行复位
    {
        if(++gucOpenCount > 3)                    // 按住3秒进行复位 
        {
            FlashInit();                      // 初始化FLASH 
            ResetSystem();
        }
    }
    else
    {
        gucOpenCount = 0;
    } 
} 
/*********************************************************************
函数名:       void runTimeStat(void) 
功能:         系统运行总时间统计
输入:         None 
输出:         None
返回:         None
日期:         2004/02/09
*********************************************************************/
void runTimeStat(void) 
{
    if(++gucSecond >= 60)                         // 是否到了1分钟 
    {
        gucSecond = 0;
        if(++gucMinute >= 60)                     // 是否到了1小时 
        {
            gucMinute = 0;
            if(++gucHour >= 24)                   // 是否到了一天 
            {
                gucHour = 0;
                gusDate++;                        // 天数加1 
            }
        }
    }
}
/*********************************************************************
函数名:       void judgeIPState(void)
功能:         判断IP状态,如果有冲突,就快闪
输入:         None 
输出:         None
返回:         None
日期:         2004/02/09
*********************************************************************/
void judgeIPState(void)
{
	if(IPOrMacConfiltFlag)
	{                                             // IP或MAC有冲突,运行灯快闪                
	    if(FALSE == StartConfiltLedFlag)
	    {
	        StartConfiltLedFlag = TRUE;
	    }
	}
	else
	{
	    if(StartConfiltLedFlag)
	    {
	        StartConfiltLedFlag = FALSE;
	    }
	    Led = ~Led;                           // 无冲突时1s闪烁 
	}
}
/*********************************************************************
函数名:       void judgeArpState(void)
功能:         判断ARP的状态
输入:         None 
输出:         None
返回:         None
日期:         2004/02/09
*********************************************************************/
void judgeArpState(void)
{ 
    switch(ArpPhases)
    {
        case ARP_FIRST_REQUEST:                   /* ARP第一次请求 */
            if(ArpCachePoniter < IpModeNum)
            {
                if(ArpCache_Buf[ArpCachePoniter].ucIPValible)    /* 是否为一个合法IP */
                {
                    if('f' == ArpCache_Buf[ArpCachePoniter].ucStatus)
                    {
                        StartTimer(4,10);         /* 还没有进行过ARP请求,每100ms进行一个IP */
                        ArpCache_Buf[ArpCachePoniter].ucStatus = 'p';
                        ArpStartRequest(&ArpCache_Buf[ArpCachePoniter].ucaArpIp[0]);                   /* ARP请求 */
                    }
                    else
                    {
                        ArpCachePoniter++;
                        StartTimer(7,2); /* 启动定时器进行下一个远程IP的ARP */
                    }
                }
                else
                {
                   
                    ArpCachePoniter++;          /* 非法的IP */
                    StartTimer(7,2);      /* 启动定时器进行下一个远程IP的ARP */
                }
            }
            else
            {
                StartTimer(7,400);
                ArpCachePoniter = 0;
                ArpPhases = ARP_SECOND_REQUEST; /* 没有ARP成功的IP需要再请求一次 */
            }
            break;

        case ARP_SECOND_REQUEST:                  /* 没有成功的IP,再次进行ARP */
            if(ArpCachePoniter < IpModeNum)
            {
                if('p' == ArpCache_Buf[ArpCachePoniter].ucStatus)
                {
                    StartTimer(4,10);             /* 第二次进行过ARP请求,每100ms进行一个IP */
                    ArpCache_Buf[ArpCachePoniter].ucStatus = 'f';    /* 如果ARP成功,在ARP中将置为"r",否则为下一次ARP做准备 */
                    ArpStartRequest(&ArpCache_Buf[ArpCachePoniter].ucaArpIp[0]);
                }
                else
                {
                    ArpCachePoniter++;
                    StartTimer(7,2);     /* 启动定时器进行下一个远程IP的ARP */
                }
            }
            else
            {
                StartTimer(7,2000);
                ArpCachePoniter = 0;
                ArpPhases = ARP_OLD;            /* ARP进入老化阶段 */
            }
            break;

        case ARP_OLD:                             /* ARP老化 */
            if(ArpCachePoniter < IpModeNum)
            {
                if('r' == ArpCache_Buf[ArpCachePoniter].ucStatus)
                {
                    StartTimer(6,100);            /* 发出请求后1秒没有响应说明目的网络或主机出现了问题.ARP成功后在ARP.C中关闭定时器6 */                   
                    ArpStartRequest(&ArpCache_Buf[ArpCachePoniter].ucaArpIp[0]);                   /* ARP请求 */
                }
                else
                {
                    ArpCachePoniter++;
                    StartTimer(7,2);     /* 启动定时器进行下一个远程IP的ARP */
                }
            }
            else
            {
                StartTimer(7,20);
                ArpCachePoniter = 0;
                ArpPhases = ARP_FIRST_REQUEST;  /* 没有ARP成功的或者已经老化IP可再次进行ARP */
            }
            break;

        default:
            ArpPhases = ARP_FIRST_REQUEST;
            break;
    }
}
/*********************************************************************
函数名:       void judgeArpState(void)
功能:         判断ARP的状态
输入:         None 
输出:         None
返回:         None
日期:         2004/02/09
*********************************************************************/
void judgeNetState(void)
{
	UWORK8   ucResult, ucValue;
	Page3();
    RTL8019asRead(RW3_CONFIG0,ucResult);
    Page0();
    if((R3_CONFIG0_JP | R3_CONFIG0_BNC) == (ucResult & 0x0f))
    {
        if(TRUE == EthernetLinkFlag)
        {
            EthernetLinkFlag = FALSE;
            for(ucValue = 0; ucValue < IpModeNum;ucValue++)
            {
                ArpCache_Buf[ucValue].ucStatus = 'f';
            }
            ARPFinishFlag = FALSE;
            
            IPOrMacConfiltFlag = FALSE;		    /* 没有LINK时应该立即停止快闪(如果正在快闪) */
            StopArpCounter();
        }
    }
    else
    {
        if(FALSE == EthernetLinkFlag)
        {
            EthernetLinkFlag = TRUE;
            IPConflitTestFlag = TRUE;
            
            ArpCachePoniter = 0;

            if(IPConflitTestFlag)                   /* 刚LINK上,需要进行IP冲突检测 */
            {
                StopArpCounter();
                ArpPhases = ARP_FIRST_REQUEST;
        /* ARP条件: 静态IP模式*/
                memcpy(&IPLocalAddress_buf[0],&NetParameter.IP_Local_buf[0],IP_LEN);
                memcpy(&SubNetMask_buf[0],&NetParameter.SubMask_buf[0],IP_LEN);
                memcpy(&GateWay_buf[0],&NetParameter.Gateway[0],IP_LEN);
                DelayMs(TL0 & 0x0f);              /* 取随机数避免2边同时发送,产生碰撞 */                                
                ArpStartRequest(&IPLocalAddress_buf[0]);    //发起ARP请求                                   
                IpOrRouter();                                    
            }
        }
    }
}
          
/*********************************************************************
函数名:       void SystemTimerProc()
功能:         相对定时器处理
输入:         None 
输出:         None
返回:         None
日期:         2004/02/09
*********************************************************************/
void SystemTimerProc(void)
{
    UWORK8   TimerType;			//定进类型 

⌨️ 快捷键说明

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