/** *@brief TCP模式初始化 客户端 默认连接三次,三次连接不上,返回Fail *@param 无 *@return 无 */ u8 RJ45_1_TCP_ClientInit(void) { u8 i,j=0; RJ45_1_Write_Register(SIMR,1<<0); //允许SOCKET0产生中断 RJ45_1_Write_Register(Sn_IMR(0) ,Sn_IR_RECV|Sn_IR_DISCON|Sn_IR_CON); //设置中断屏蔽寄存器 CloseSocket_RJ45_1(); RJ45_1_Write_Register(Sn_MR(0),Sn_MR_TCP|Sn_MR_ND); //tcp模式,无延时 WriteTem[0]=RJ45_1_Loc_Potr/256; WriteTem[1]=RJ45_1_Loc_Potr%256; RJ45_1_Write_Buf(Sn_PORT0(0),WriteTem,2); //设置端口号 WriteTem[0]=RJ45_1_Dir_Port/256; WriteTem[1]=RJ45_1_Dir_Port%256; RJ45_1_Write_Buf(Sn_DPORT0(0),WriteTem,2); //设置目标服务器端口号 RJ45_1_Write_Buf(Sn_DIPR0(0),RJ45_1_DirIP,4); //目标服务器IP地址 RJ45_1_Write_Register(Sn_KPALVTR(0),1); //每5s自动检测一次连接状态 Init1: RJ45_1_Write_Register(Sn_CR(0),Sn_CR_OPEN); for(i=0;i<20;i++); while(RJ45_1_Read_Register(Sn_CR(0))) /*Wait to process the command*/ { for(i=0;i<20;i++); } if(RJ45_1_Read_Register(Sn_SR(0))!=SOCK_INIT) //检测网口开启状态 { j++; if(j<10) { CloseSocket_RJ45_1(); goto Init1; } else { CloseSocket_RJ45_1(); return Fail; } } RJ45_1_Write_Register(Sn_CR(0),Sn_CR_CONNECT); //开启连接 RJ45_1_Read_Buf(Sn_DIPR0(0),RJ45_1_DirIP,4); while(RJ45_1_Read_Register(Sn_CR(0))) /*Wait to process the command*/ { } while(RJ45_1_Read_Register(Sn_SR(0))!=SOCK_SYNSENT) { if(RJ45_1_Read_Register(Sn_SR(0))==SOCK_ESTABLISHED) { return Success; } if(RJ45_1_Read_Register(Sn_IR(0))& Sn_IR_TIMEOUT) { RJ45_1_Write_Register(Sn_IR(0),Sn_IR_TIMEOUT); return Fail; } } }
//TCP模式初始化 服务器 u8 RJ45_2_TCP_ServiceInit(void) { u8 i,j=0; RJ45_2_Write_Register(SIMR,1<<0); //允许SOCKET0产生中断 RJ45_2_Write_Register(Sn_IMR(0) ,Sn_IR_RECV|Sn_IR_TIMEOUT|Sn_IR_DISCON|Sn_IR_CON); //设置中断屏蔽寄存器 CloseSocket_RJ45_2(); RJ45_2_Write_Register(Sn_MR(0),Sn_MR_TCP|Sn_MR_ND); //tcp模式,无延时 WriteTem[0]=RJ45_2_Loc_Potr/256; WriteTem[1]=RJ45_2_Loc_Potr%256; RJ45_2_Write_Buf(Sn_PORT0(0),WriteTem,2); //设置端口号 RJ45_2_Write_Register(Sn_KPALVTR(0),1); //每5s自动检测一次连接状态 Init1: RJ45_2_Write_Register(Sn_CR(0),Sn_CR_OPEN); for(i=0;i<20;i++); while(RJ45_2_Read_Register(Sn_CR(0))) /*Wait to process the command*/ { for(i=0;i<20;i++); } if(RJ45_2_Read_Register(Sn_SR(0))!=SOCK_INIT) //检测网口开启状态 { j++; if(j<10) { CloseSocket_RJ45_2(); goto Init1; } else { CloseSocket_RJ45_2(); return Fail; } } RJ45_2_Write_Register(Sn_CR(0),Sn_CR_LISTEN); //开启监听状态 for(i=0;i<20;i++); while(RJ45_2_Read_Register(Sn_CR(0))) /*Wait to process the command*/ { for(i=0;i<20;i++); } return Success; }
void setSn_KPALVTR(SOCKET s, uint8 time) { uint16 keepalive=0; keepalive = (IINCHIP_READ(Sn_KPALVTR(s)) & 0x00FF) + ((uint16)time<<8); IINCHIP_WRITE(Sn_KPALVTR(s),keepalive); }
uint8 getSn_KPALVTR(SOCKET s) { return (uint8)(IINCHIP_READ(Sn_KPALVTR(s)) >> 8); }