📄 socket.c
字号:
setSn_CR(s,Sn_CR_SEND);
return ret;
}
uint32 recv(SOCKET s, uint8 * buf, uint32 len)
{
uint16 pack_size=0;
#ifdef __DEF_IINCHIP_DBG__
printf("%d : recv()\r\n",s);
#endif
if(IINCHIP_READ(Sn_MR(s)) & Sn_MR_ALIGN)
{
wiz_read_buf(s, buf, (uint32)len);
setSn_CR(s,Sn_CR_RECV);
return len;
}
wiz_read_buf(s,(uint8*)&pack_size,2); // extract the PACKET-INFO(DATA packet length)
if( (*(vint16*)MR) & MR_FS )
pack_size = ((((pack_size << 8 ) & 0xFF00)) | ((pack_size >> 8)& 0x00FF));
#ifdef __DEF_IINCHIP_DBG__
printf("%d:pack_size=%d\r\n",s,pack_size);
#endif
wiz_read_buf(s, buf, (uint32)pack_size); // copy data
setSn_CR(s,Sn_CR_RECV); // recv
/*
* \warning send a packet for updating window size. This code block must be only used when W5300 do only receiving data.
*/
// ------------------------
// WARNING CODE BLOCK START
// M_15052008 : Replace Sn_CR_SEND with Sn_CR_SEND_KEEP.
//if(!(getSn_IR(s) & Sn_IR_SENDOK))
//{
// setSn_TX_WRSR(s,0); // size = 0
// setSn_CR(s,Sn_CR_SEND); // send
// while(!(getSn_IR(s) & Sn_IR_SENDOK)); // wait SEND command completion
// setSn_IR(s,Sn_IR_SENDOK); // clear Sn_IR_SENDOK bit
//}
// M_04072008 : Replace Sn_CR_SEND_KEP with Sn_CR_SEND.
//if(getSn_RX_RSR(s) == 0) // send the window-update packet when the window size is full
//{
// uint8 keep_time = 0;
// if((keep_time=getSn_KPALVTR(s)) != 0x00) setSn_KPALVTR(s,0x00); // disables the auto-keep-alive-process
// setSn_CR(s,Sn_CR_SEND_KEEP); // send a keep-alive packet by command
// setSn_KPALVTR(s,keep_time); // restore the previous keep-alive-timer value
//}
#if 0
if(getSn_RX_RSR(s) == 0) // check if the window size is full or not
{ /* Sn_RX_RSR can be compared with another value instead of ‘0’,
according to the host performance of receiving data */
setSn_TX_WRSR(s,1); // size : 1 byte dummy size
IINCHIP_WRITE(Sn_TX_FIFOR(s),0x0000); // write dummy data into tx memory
setSn_CR(s,Sn_CR_SEND); // send
while(!(getSn_IR(s) & Sn_IR_SENDOK)); // wait SEND command completion
setSn_IR(s,Sn_IR_SENDOK); // clear Sn_IR_SENDOK bit
}
#endif
// WARNING CODE BLOCK END
// ----------------------
return (uint32)pack_size;
}
uint32 sendto(SOCKET s, uint8 * buf, uint32 len, uint8 * addr, uint16 port)
{
uint8 status=0;
uint8 isr=0;
uint32 ret=0;
#ifdef __DEF_IINCHIP_DBG__
printf("%d : sendto():%d.%d.%d.%d(%d), len=%d\r\n",s, addr[0], addr[1], addr[2], addr[3] , port, len);
#endif
if
(
((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
((port == 0x00)) ||(len == 0)
)
{
#ifdef __DEF_IINCHIP_DBG__
printf("%d : Fail[%d.%d.%d.%d, %.d, %d]\r\n",s, addr[0], addr[1], addr[2], addr[3] , port, len);
#endif
return 0;
}
if (len > getIINCHIP_TxMAX(s)) ret = getIINCHIP_TxMAX(s); // check size not to exceed MAX size.
else ret = len;
// set destination IP address
IINCHIP_WRITE(Sn_DIPR(s),(((uint16)addr[0])<<8) + (uint16) addr[1]);
IINCHIP_WRITE(Sn_DIPR2(s),(((uint16)addr[2])<<8) + (uint16) addr[3]);
// set destination port number
IINCHIP_WRITE(Sn_DPORTR(s),port);
wiz_write_buf(s, buf, ret); // copy data
// send
setSn_TX_WRSR(s,ret);
setSn_CR(s, Sn_CR_SEND);
while (!((isr = getSn_IR(s)) & Sn_IR_SENDOK)) // wait SEND command completion
{
status = getSn_SSR(s); // warning ---------------------------------------
if ((status == SOCK_CLOSED) || (isr & Sn_IR_TIMEOUT)) // Sn_IR_TIMEOUT causes the decrement of Sn_TX_FSR
{ // -----------------------------------------------
#ifdef __DEF_IINCHIP_DBG__
printf("%d: send fail.status=0x%02x,isr=%02x\r\n",status,isr);
#endif
setSn_IR(s,Sn_IR_TIMEOUT);
return 0;
}
}
setSn_IR(s, Sn_IR_SENDOK); // Clear Sn_IR_SENDOK
#ifdef __DEF_IINCHIP_DBG__
printf("%d : send()end\r\n",s);
#endif
return ret;
}
uint32 recvfrom(SOCKET s, uint8 * buf, uint32 len, uint8 * addr, uint16 *port)
{
uint16 head[4];
uint32 data_len=0;
uint16 crc[2];
#ifdef __DEF_IINCHIP_DBG__
printf("recvfrom()\r\n");
#endif
if ( len > 0 )
{
switch (IINCHIP_READ(Sn_MR(s)) & 0x07) // check the mode of s-th SOCKET
{ // -----------------------------
case Sn_MR_UDP : // UDP mode
wiz_read_buf(s, (uint8*)head, 8); // extract the PACKET-INFO
// read peer's IP address, port number.
if(*((vuint16*)MR) & MR_FS) // check FIFO swap bit
{
head[0] = ((((head[0] << 8 ) & 0xFF00)) | ((head[0] >> 8)& 0x00FF));
head[1] = ((((head[1] << 8 ) & 0xFF00)) | ((head[1] >> 8)& 0x00FF));
head[2] = ((((head[2] << 8 ) & 0xFF00)) | ((head[2] >> 8)& 0x00FF));
head[3] = ((((head[3] << 8 ) & 0xFF00)) | ((head[3] >> 8)& 0x00FF));
}
addr[0] = (uint8)(head[0] >> 8); // destination IP address
addr[1] = (uint8)head[0];
addr[2] = (uint8)(head[1]>>8);
addr[3] = (uint8)head[1];
*port = head[2]; // destination port number
data_len = (uint32)head[3]; // DATA packet length
#ifdef __DEF_IINCHIP_DBG__
printf("UDP msg arrived:%d(0x%04x)\r\n",data_len,data_len);
printf("source Port : %d\r\n", *port);
printf("source IP : %d.%d.%d.%d\r\n", addr[0], addr[1], addr[2], addr[3]);
#endif
wiz_read_buf(s, buf, data_len); // data copy.
break;
// -----------------------
case Sn_MR_IPRAW : // IPRAW mode
wiz_read_buf(s, (uint8*)head, 6); // extract the PACKET-INFO
if(*((vuint16*)MR) & MR_FS) // check FIFO swap bit
{
head[0] = ((((head[0] << 8 ) & 0xFF00)) | ((head[0] >> 8)& 0x00FF));
head[1] = ((((head[1] << 8 ) & 0xFF00)) | ((head[1] >> 8)& 0x00FF));
head[2] = ((((head[2] << 8 ) & 0xFF00)) | ((head[2] >> 8)& 0x00FF));
}
addr[0] = (uint8)(head[0] >> 8); // destination IP address
addr[1] = (uint8)head[0];
addr[2] = (uint8)(head[1]>>8);
addr[3] = (uint8)head[1];
data_len = (uint32)head[2]; // DATA packet length
#ifdef __DEF_IINCHIP_DBG__
printf("IP RAW msg arrived\r\n");
printf("source IP : %d.%d.%d.%d\r\n", addr[0], addr[1], addr[2], addr[3]);
#endif
wiz_read_buf(s, buf, data_len); // data copy.
break;
// -----------------------
case Sn_MR_MACRAW : // MACRAW mode
wiz_read_buf(s,(uint8*)head,2); // extract the PACKET-INFO
if(*((vuint16*)MR) & MR_FS) // check FIFO swap bit
head[0] = ((((head[0] << 8 ) & 0xFF00)) | ((head[0] >> 8)& 0x00FF));
data_len = (uint32)head[0]; // DATA packet length
wiz_read_buf(s, buf, data_len); // data copy.
wiz_read_buf(s,(uint8*)crc, 4); // extract CRC data and ignore it.
break;
default :
break;
}
setSn_CR(s,Sn_CR_RECV); // recv
}
#ifdef __DEF_IINCHIP_DBG__
printf("recvfrom() end ..\r\n");
#endif
return data_len;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -