Пример #1
0
/**
*@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;
		}
	}
}
Пример #2
0
/**
@brief	This function established	the connection for the channel in passive (server) mode. This function waits for the request from the peer.
@return	1 for success else 0.
*/ 
uint8 listen(
	SOCKET s /**< the socket number */
	)
{
	uint8 ret;
#ifdef __DEF_IINCHIP_DBG__
	printf("listen()\r\n");
#endif
	if (wiz_read_byte(Sn_SR(s)) == SOCK_INIT)
	{
		wiz_write_byte(Sn_CR(s),Sn_CR_LISTEN);
		/* +20071122[chungs]:wait to process the command... */
		while( wiz_read_byte(Sn_CR(s)) ) 
			;
		/* ------- */
		ret = 1;
	}
	else
	{
		ret = 0;
#ifdef __DEF_IINCHIP_DBG__
	printf("Fail[invalid ip,port]\r\n");
#endif
	}
	return ret;
}
Пример #3
0
/**
@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;
}
Пример #4
0
// return values:
// 1 on success
// 0 otherwise
int EthernetDNSClass::_startDNSSession()
{
   (void)this->_closeDNSSession();
   
   int i;
   for (i = NUM_SOCKETS-1; i>=0; i--)
      if (SOCK_CLOSED == IINCHIP_READ(Sn_SR(i))) {
         if (socket(i, Sn_MR_UDP, DNS_CLIENT_PORT, 0) > 0) {
            this->_socket = i;
            break;
         }
      }

   if (this->_socket < 0)
      return 0;
	
	uint16 port = DNS_SERVER_PORT;

   for (i=0; i<4; i++)
      IINCHIP_WRITE((Sn_DIPR0(this->_socket) + i), this->_dnsData.serverIpAddr[i]);

   IINCHIP_WRITE(Sn_DPORT0(this->_socket), (uint8)((port & 0xff00) >> 8));
   IINCHIP_WRITE((Sn_DPORT0(this->_socket) + 1), (uint8)(port & 0x00ff));
   
   return 1;
}
Пример #5
0
uint16 igmpsend(SOCKET s, const uint8 * buf, uint16 len)
{
	uint8 status=0;
// uint8 isr=0;
	uint16 ret=0;
	
#ifdef __DEF_IINCHIP_DBG__
	printf("igmpsend()\r\n");
#endif
	if (len > IINCHIP_TxMAX) ret = IINCHIP_TxMAX; // check size not to exceed MAX size.
	else ret = len;

	if (ret == 0) 
	{
		;
#ifdef __DEF_IINCHIP_DBG__
	printf("%d Fail[%d]\r\n",len);
#endif
	}
	else
	{
		// copy data
		send_data_processing(s, (uint8 *)buf, ret);
		
/* +2008.01 bj */ 
#ifdef __DEF_IINCHIP_INT__
		while ( (getISR(s) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK ) 
#else
		while ( (wiz_read_byte(Sn_IR(s)) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK ) 
#endif
		{
			status = wiz_read_byte(Sn_SR(s));
#ifdef __DEF_IINCHIP_INT__
			if (getISR(s) & Sn_IR_TIMEOUT)
#else
			if (wiz_read_byte(Sn_IR(s)) & Sn_IR_TIMEOUT)
#endif
			{
#ifdef __DEF_IINCHIP_DBG__
				printf("igmpsend fail.\r\n");
#endif
				/* in case of igmp, if send fails, then socket closed */
				/* if you want change, remove this code. */
				close(s);
				/* ----- */
				
				return 0;
			}
		}

/* +2008.01 bj */ 
#ifdef __DEF_IINCHIP_INT__
		putISR(s, getISR(s) & (~Sn_IR_SEND_OK));
#else
		wiz_write_byte(Sn_IR(s), Sn_IR_SEND_OK);
#endif
	}
	return ret;
}
Пример #6
0
void loopback_udp(SOCKET s, uint16 port, uint8 xdata *data_buf, uint16 mode)
{
    switch (IINCHIP_READ(Sn_SR(s)))
    {
    case SOCK_UDP:
        loopback_recv(s);
        loopback_send(s);
        break;
    case SOCK_CLOSED:                                  // CLOSED
        close(s);                                       // close the SOCKET
        socket(s, Sn_MR_UDP, port, mode);                  // open the SOCKET with UDP mode
        printf("LOOPBACK_UDPStarted. CH : %bu\r\n", s);
        break;
    default:
        break;
    }
}
Пример #7
0
//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;
}
Пример #8
0
/**
@brief	This function used to send the data in TCP mode
@return	1 for success else 0.
*/ 
uint16 send(
	SOCKET s,		/**< the socket index */
	const uint8 * buf,	/**< a pointer to data */
	uint16 len		/**< the data size to be send */
	)
{
	uint8 status=0;
	uint16 ret=0;
	uint16 freesize=0;
#ifdef __DEF_IINCHIP_DBG__
	printf("send()\r\n");
#endif

	if (len > IINCHIP_TxMAX) ret = IINCHIP_TxMAX; // check size not to exceed MAX size.
	else ret = len;

	// if freebuf is available, start.
	do 
	{
		freesize = getSn_TX_FSR(s);
		status = wiz_read_byte(Sn_SR(s));
		if ((status != SOCK_ESTABLISHED) && (status != SOCK_CLOSE_WAIT))
		{
			ret = 0; 
			break;
		}
#ifdef __DEF_IINCHIP_DBG__
		printf("socket %d freesize(%d) empty or error\r\n", s, freesize);
#endif
	} while (freesize < ret);

		// copy data
	send_data_processing(s, (uint8 *)buf, ret);

	/* ------- */

/* +2008.01 bj */ 
#ifdef __DEF_IINCHIP_INT__
	while ( (getISR(s) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK ) 
#else
	while ( (wiz_read_byte(Sn_IR(s)) & Sn_IR_SEND_OK) != Sn_IR_SEND_OK ) 
#endif
	{
		/* m2008.01 [bj] : reduce code */
		if ( wiz_read_byte(Sn_SR(s)) == SOCK_CLOSED )
		{
#ifdef __DEF_IINCHIP_DBG__
			printf("SOCK_CLOSED.\r\n");
#endif
			close(s);
			return 0;
		}
	}
/* +2008.01 bj */ 
#ifdef __DEF_IINCHIP_INT__
	putISR(s, getISR(s) & (~Sn_IR_SEND_OK));
#else
	wiz_write_byte(Sn_IR(s), Sn_IR_SEND_OK);
#endif
	return ret;
}