/** @brief This Socket function initialize the channel in perticular mode, and set the port and wait for W5100 done it. @return 1 for sucess else 0. */ uint8 socket( SOCKET s, /**< for socket number */ uint8 protocol, /**< for socket protocol */ uint16 port, /**< the source port for the socket */ uint8 flag /**< the option for the socket */ ) { uint8 ret; #ifdef __DEF_IINCHIP_DBG__ printf("socket()\r\n"); #endif if ((protocol == Sn_MR_TCP) || (protocol == Sn_MR_UDP) || (protocol == Sn_MR_IPRAW) || (protocol == Sn_MR_MACRAW) || (protocol == Sn_MR_PPPOE)) { close(s); if (!port) port = local_port++; wiz_write_byte(Sn_MR(s),protocol | flag); // if don't set the source port, set local_port number. wiz_write_word(Sn_PORT0(s),port); wiz_write_byte(Sn_CR(s),Sn_CR_OPEN); // run sockinit Sn_CR /* +20071122[chungs]:wait to process the command... */ while( wiz_read_byte(Sn_CR(s)) ) ; /* ------- */ ret = 1; } else { ret = 0; } #ifdef __DEF_IINCHIP_DBG__ printf("Sn_SR = %.2x , Protocol = %.2x\r\n", wiz_read_byte(Sn_SR(s)), wiz_read_byte(Sn_MR(s))); #endif return ret; }
/** *@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; } } }
/** @brief This Socket function open TCP server socket. @return 1 - success, 0 - fail. */ int8 TCPServerOpen(SOCKET s, uint16 port) { uint8 ret; #ifdef __DEF_IINCHIP_DBG__ printf("TCPServerOpen()\r\n"); #endif TCPClose(s); IINCHIP_WRITE(Sn_MR(s),Sn_MR_TCP); if (port != 0) { IINCHIP_WRITE(Sn_PORT0(s),(uint8)((port & 0xff00) >> 8)); IINCHIP_WRITE((Sn_PORT0(s) + 1),(uint8)(port & 0x00ff)); } else {
/** @brief This Socket function initialize the channel in perticular mode, and set the port and wait for W5200 done it. @return 1 for sucess else 0. */ uint8 socket(SOCKET s, uint8 protocol, uint16 port, uint8 flag) { uint8 ret; #ifdef __DEF_IINCHIP_DBG__ printf("socket()\r\n"); #endif if ((protocol == Sn_MR_TCP) || (protocol == Sn_MR_UDP) || (protocol == Sn_MR_IPRAW) || (protocol == Sn_MR_MACRAW) || (protocol == Sn_MR_PPPOE)) { close(s); IINCHIP_WRITE(Sn_MR(s),protocol | flag); if (port != 0) { IINCHIP_WRITE(Sn_PORT0(s),(uint8)((port & 0xff00) >> 8)); IINCHIP_WRITE((Sn_PORT0(s) + 1),(uint8)(port & 0x00ff)); } else {
/** @brief This Socket function initialize the channel in perticular mode, and set the port and wait for W5100 done it. @return 1 for sucess else 0. */ uint8 socket( SOCKET s, /**< for socket number */ uint8 protocol, /**< for socket protocol */ uint16 port, /**< the source port for the socket */ uint8 flag /**< the option for the socket */ ) { uint8 ret; #ifdef __DEF_IINCHIP_DBG__ printf("socket()\r\n"); #endif if ((protocol == Sn_MR_TCP) || (protocol == Sn_MR_UDP) || (protocol == Sn_MR_IPRAW) || (protocol == Sn_MR_MACRAW) || (protocol == Sn_MR_PPPOE)) { close(s); IINCHIP_WRITE(Sn_MR(s),protocol | flag); if (port != 0) { IINCHIP_WRITE(Sn_PORT0(s),(uint8)((port & 0xff00) >> 8)); IINCHIP_WRITE((Sn_PORT0(s) + 1),(uint8)(port & 0x00ff)); }
//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; }
/*----------- TCP Server Routine ------------*/ void ProcessTcpServer(SOCKET mSocket, uint16 port) { // if len is not volatile, it will overflow // and causes 'cstack' overstack error volatile uint16_t len; getSn = getSn_SR(mSocket); switch (getSn_SR(mSocket)) { case SOCK_ESTABLISHED: if(ch_status[mSocket] == 0) { printf("\r\n------------------------"); printf("\r\nSocket No. %d : Connected", mSocket); printf("\r\n - Peer IP : %d.%d.%d.%d", IINCHIP_READ(Sn_DIPR0(mSocket)+0), IINCHIP_READ(Sn_DIPR0(mSocket)+1), IINCHIP_READ(Sn_DIPR0(mSocket)+2), IINCHIP_READ(Sn_DIPR0(mSocket)+3)); printf("\r\n - Peer Port : %d", ( (uint16)(IINCHIP_READ(Sn_DPORT0(mSocket)+0)<<8) + (uint16)IINCHIP_READ(Sn_DPORT0(mSocket)+1)) ); printf("\r\n - Source Port : %d", IINCHIP_READ(Sn_PORT0(mSocket))); ch_status[mSocket] = 1; } /* check Rx data and set flags only when there's valid character recognized */ if ((len = getSn_RX_RSR(mSocket)) > 0) { /* if Rx data size is lager than TX_RX_MAX_BUF_SIZE */ /* the data size to read is MAX_BUF_SIZE. */ if (len > TX_RX_MAX_BUF_SIZE) len = TX_RX_MAX_BUF_SIZE; switch(mSocket) { case SOCK_ZERO : E1Flag = true; break; // EQ-DAQ-01 board case SOCK_ONE : E2Flag = true; break; // EQ-DAQ-02 board case SOCK_TWO : PCFlag = true; break; // PC Client default : break; } /* prevent from overflowing */ len = recv(mSocket, RX_BUF, len); } break; case SOCK_CLOSE_WAIT: E1Flag = false; // Reset E1 flag E2Flag = false; // Reset E2 flag PCFlag = false; // Reset PC flag printf("\r\nSocket No. %d : CLOSE_WAIT", mSocket); // disconnect and set status disconnect(mSocket); ch_status[mSocket] = 0; break; case SOCK_INIT: lis = listen(mSocket); break; case SOCK_CLOSED: //reinitialize the socket if(socket(mSocket, Sn_MR_TCP, port ,0x00) == 0) { printf("Fail to create socket."); } else { E1Flag = false; // Reset E1 flag E2Flag = false; // Reset E2 flag PCFlag = false; // Reset PC flag lis = listen(mSocket); } break; } }
/*----------- TCP Server Routine ------------*/ void ProcessTcpSever(SOCKET mSocket, uint16 port) { // if len is not volatile, it will overflow // and causes 'cstack' overstack error volatile uint16_t len; getSn = getSn_SR(mSocket); switch (getSn_SR(mSocket)) { case SOCK_ESTABLISHED: if(ch_status[mSocket] == 0) { printf("\r\n------------------------"); printf("\r\nSocket No. %d : Connected", mSocket); printf("\r\n - Peer IP : %d.%d.%d.%d", IINCHIP_READ(Sn_DIPR0(mSocket)+0), IINCHIP_READ(Sn_DIPR0(mSocket)+1), IINCHIP_READ(Sn_DIPR0(mSocket)+2), IINCHIP_READ(Sn_DIPR0(mSocket)+3)); printf("\r\n - Peer Port : %d", ( (uint16)(IINCHIP_READ(Sn_DPORT0(mSocket)+0)<<8) + (uint16)IINCHIP_READ(Sn_DPORT0(mSocket)+1)) ); printf("\r\n - Source Port : %d", IINCHIP_READ(Sn_PORT0(mSocket))); ch_status[mSocket] = 1; } /* check Rx data */ if ((len = getSn_RX_RSR(mSocket)) > 0) { /* if Rx data size is lager than TX_RX_MAX_BUF_SIZE */ /* the data size to read is MAX_BUF_SIZE. */ if (len > TX_RX_MAX_BUF_SIZE) len = TX_RX_MAX_BUF_SIZE; /* read the received data */ len = recv(mSocket, RX_BUF, len); RSR_len = len; if(mSocket == EQ_ONE) { } // start send those data SendFlag = True; /* send the received data */ //send(mSocket, RX_BUF, len, (bool)False); } /* send the received data */ //send(SOCK_TCPS, data_buf, len); break; case SOCK_CLOSE_WAIT: SendFlag = False; // Reset flag printf("\r\nSocket No. %d : CLOSE_WAIT", mSocket); // disconnect and set status disconnect(mSocket); ch_status[mSocket] = 0; break; case SOCK_INIT: lis = listen(mSocket); break; case SOCK_CLOSED: //reinitialize the socket if(socket(mSocket, Sn_MR_TCP, port ,0x00) == 0) { printf("Fail to create socket."); } else { SendFlag = False; // Reset flag lis = listen(mSocket); } break; } }