60 #define SOCK_ANY_PORT_NUM 0xC000
63 static uint16_t sock_io_mode = 0;
64 static uint16_t sock_is_sending = 0;
83 #define CHECK_SOCKNUM() \
85 if(sn > _WIZCHIP_SOCK_NUM_) return SOCKERR_SOCKNUM; \
88 #define CHECK_SOCKMODE(mode) \
90 if((getSn_MR(sn) & 0x0F) != mode) return SOCKERR_SOCKMODE; \
93 #define CHECK_SOCKINIT() \
95 if((getSn_SR(sn) != SOCK_INIT)) return SOCKERR_SOCKINIT; \
98 #define CHECK_SOCKDATA() \
100 if(len == 0) return SOCKERR_DATALEN; \
105 int8_t
socket(uint8_t sn, uint8_t protocol, uint16_t port, uint8_t flag)
124 #if ( _WIZCHIP_ < 5200 )
135 #if _WIZCHIP_ == 5200
145 #if _WIZCHIP_ == 5300
157 #if _WIZCHIP_ == 5500
170 #if _WIZCHIP_ == 5300
171 setSn_MR(sn, ((uint16_t)(protocol | (flag & 0xF0))) | (((uint16_t)(flag & 0x02)) << 7) );
173 setSn_MR(sn, (protocol | (flag & 0xF0)));
177 port = sock_any_port++;
184 sock_io_mode &= ~(1 <<sn);
187 sock_is_sending &= ~(1<<sn);
188 sock_remained_size[sn] = 0;
201 #if (_WIZCHIP_ == 5300)
206 uint8 destip[4] = {0, 0, 0, 1};
222 sendto(sn,destip,1,destip,0x3000);
231 sock_io_mode &= ~(1<<sn);
233 sock_is_sending &= ~(1<<sn);
234 sock_remained_size[sn] = 0;
259 int8_t
connect(uint8_t sn, uint8_t * addr, uint16_t port)
268 taddr = ((uint32_t)addr[0] & 0x000000FF);
269 taddr = (taddr << 8) + ((uint32_t)addr[1] & 0x000000FF);
270 taddr = (taddr << 8) + ((uint32_t)addr[2] & 0x000000FF);
271 taddr = (taddr << 8) + ((uint32_t)addr[3] & 0x000000FF);
281 if(sock_io_mode & (1<<sn))
return SOCK_BUSY;
306 sock_is_sending &= ~(1<<sn);
307 if(sock_io_mode & (1<<sn))
return SOCK_BUSY;
319 int32_t
send(uint8_t sn, uint8_t * buf, uint16_t len)
329 if( sock_is_sending & (1<<sn) )
337 #if _WIZCHIP_ == 5200
345 sock_is_sending &= ~(1<<sn);
355 if (len > freesize) len = freesize;
365 if( (sock_io_mode & (1<<sn)) && (len > freesize) )
return SOCK_BUSY;
366 if(len <= freesize)
break;
369 #if _WIZCHIP_ == 5200
373 #if _WIZCHIP_ == 5300
380 sock_is_sending |= (1 << sn);
387 int32_t
recv(uint8_t sn, uint8_t * buf, uint16_t len)
390 uint16_t recvsize = 0;
392 #if _WIZCHIP_ == 5300
402 if(recvsize < len) len = recvsize;
405 #if _WIZCHIP_ == 5300
407 if(sock_remained_size[sn] == 0)
419 if(recvsize != 0)
break;
432 if((sock_io_mode & (1<<sn)) && (recvsize == 0))
return SOCK_BUSY;
433 if(recvsize != 0)
break;
435 #if _WIZCHIP_ == 5300
440 #if _WIZCHIP_ == 5300
448 recvsize = (((uint16_t)head[1]) << 8) | ((uint16_t)head[0]);
450 recvsize = (((uint16_t)head[0]) << 8) | ((uint16_t)head[1]);
453 sock_remained_size[sn] = recvsize;
455 if(len > sock_remained_size[sn]) len = sock_remained_size[sn];
459 *buf = sock_remained_byte[sn];
463 sock_remained_size[sn] -= 1;
471 sock_remained_size[sn] -= recvsize;
472 if(sock_remained_size[sn] != 0)
478 if(
getSn_MR(sn) & Sn_MR_ALIGN) sock_remained_size[sn] = 0;
481 if(recvsize < len) len = recvsize;
492 int32_t
sendto(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t port)
495 uint16_t freesize = 0;
512 taddr = ((uint32_t)addr[0]) & 0x000000FF;
513 taddr = (taddr << 8) + ((uint32_t)addr[1] & 0x000000FF);
514 taddr = (taddr << 8) + ((uint32_t)addr[2] & 0x000000FF);
515 taddr = (taddr << 8) + ((uint32_t)addr[3] & 0x000000FF);
527 if (len > freesize) len = freesize;
532 if( (sock_io_mode & (1<<sn)) && (len > freesize) )
return SOCK_BUSY;
533 if(len <= freesize)
break;
537 #if _WIZCHIP_ < 5500 //M20150401 : for WIZCHIP Errata #4, #5 (ARP errata)
542 setSUBR((uint8_t*)
"\x00\x00\x00\x00");
548 #if _WIZCHIP_ == 5300
571 #if _WIZCHIP_ < 5500 //M20150401 : for WIZCHIP Errata #4, #5 (ARP errata)
572 if(taddr)
setSUBR((uint8_t*)&taddr);
578 #if _WIZCHIP_ < 5500 //M20150401 : for WIZCHIP Errata #4, #5 (ARP errata)
579 if(taddr)
setSUBR((uint8_t*)&taddr);
588 int32_t
recvfrom(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t *port)
591 #if _WIZCHIP_ == 5300
604 #if _WIZCHIP_ == 5300
613 #if ( _WIZCHIP_ < 5200 )
622 if(sock_remained_size[sn] == 0)
628 if( (sock_io_mode & (1<<sn)) && (pack_len == 0) )
return SOCK_BUSY;
629 if(pack_len != 0)
break;
637 if(sock_remained_size[sn] == 0)
644 #if _WIZCHIP_ == 5300
652 *port = (*port << 8) + head[4];
653 sock_remained_size[sn] = head[7];
654 sock_remained_size[sn] = (sock_remained_size[sn] << 8) + head[6];
664 *port = (*port << 8) + head[5];
665 sock_remained_size[sn] = head[6];
666 sock_remained_size[sn] = (sock_remained_size[sn] << 8) + head[7];
667 #if _WIZCHIP_ == 5300
672 if(len < sock_remained_size[sn]) pack_len = len;
673 else pack_len = sock_remained_size[sn];
676 #if _WIZCHIP_ == 5300
679 *buf++ = sock_remained_byte[sn];
681 sock_remained_size[sn] -= 1;
691 if(sock_remained_size[sn] == 0)
697 sock_remained_size[sn] = head[0];
698 sock_remained_size[sn] = (sock_remained_size[sn] <<8) + head[1];
699 if(sock_remained_size[sn] > 1514)
706 if(len < sock_remained_size[sn]) pack_len = len;
707 else pack_len = sock_remained_size[sn];
710 #if ( _WIZCHIP_ < 5200 )
712 if(sock_remained_size[sn] == 0)
721 sock_remained_size[sn] = head[4];
724 sock_remained_size[sn] = (sock_remained_size[sn] << 8) + head[5];
730 if(len < sock_remained_size[sn]) pack_len = len;
731 else pack_len = sock_remained_size[sn];
737 sock_remained_size[sn] = pack_len;
743 sock_remained_size[sn] -= pack_len;
746 if(sock_remained_size[sn] != 0)
749 #if _WIZCHIP_ == 5300
754 #if _WIZCHIP_ == 5300
760 return (int32_t)pack_len;
771 tmp = *((uint8_t*)arg);
779 *((uint8_t*)arg) = (uint8_t)((sock_io_mode >> sn) & 0x0001);
795 #if _WIZCHIP_ != 5100
832 #if _WIZCHIP_ != 5100
869 *(uint8_t*)arg =
getSn_MR(sn) & 0xF0;
905 *(uint16_t*)arg = sock_remained_size[sn];
Set/Get keep-alive auto transmission timer in TCP mode, Not supported in W5100, W5200.
Valid only in getsockopt. Get the socket status. Sn_SR, getSn_SR()
#define getSn_IR(sn)
Get Sn_IR register.
#define PACK_COMPLETED
In Non-TCP packet, It indicates to complete to receive a packet. (When W5300, This flag can be applie...
Set TTL. Sn_TTL ( setSn_TTL(), getSn_TTL() )
int8_t ctlsocket(uint8_t sn, ctlsock_type cstype, void *arg)
Control socket.
Valid only in getsockopt. Get the remained packet size in other then TCP mode.
#define SOCKERR_SOCKMODE
Invalid socket mode for socket operation.
void wiz_recv_ignore(uint8_t sn, uint16_t len)
It discard the received data in RX memory.
get the size of socket buffer allocated in TX memory
#define SOCK_UDP
UDP socket.
#define getSIPR(sipr)
Get SIPR.
uint16_t getSn_RX_RSR(uint8_t sn)
Get Sn_RX_RSR register.
#define SOCK_LISTEN
Listen state.
#define getSn_DIPR(sn, dipr)
Get Sn_DIPR register.
#define SOCKERR_ARG
Invalid argument.
#define setSn_MSSR(sn, mss)
Set Sn_MSSR register.
#define setSn_TTL(sn, ttl)
Set Sn_TTL register.
#define Sn_MR_PPPoE
PPPoE.
#define SOCK_OK
Result is OK about socket process.
#define PACK_FIFOBYTE
Valid only W5300, It indicate to have read already the Sn_RX_FIFOR.
#define SOCK_BUSY
Socket is busy on processing the operation. Valid only Non-block IO Mode.
#define SF_MULTI_ENABLE
In Sn_MR_UDP, Enable multicast mode.
#define setSn_IR(sn, ir)
Set Sn_IR register.
#define SOCKERR_SOCKOPT
Invalid socket option.
#define getSn_TTL(sn)
Get Sn_TTL register.
#define SF_TCP_NODELAY
In Sn_MR_TCP, Use to nodelayed ack.
#define getSn_TxMAX(sn)
Get the max TX buffer size of socket sn.
#define SOCKERR_TIMEOUT
Timeout occurred.
void wiz_recv_data(uint8_t sn, uint8_t *wizdata, uint16_t len)
It copies data to your buffer from internal RX memory.
#define setSn_MR(sn, mr)
Set Sn_MR register.
#define Sn_CR_LISTEN
Wait connection request in TCP mode(Server mode)
void wiz_send_data(uint8_t sn, uint8_t *wizdata, uint16_t len)
It copies data to internal TX memory.
#define Sn_CR_CONNECT
Send connection request in TCP mode(Client mode)
#define getSn_CR(sn)
Get Sn_CR register.
Valid only in setsockopt. Manually send keep-alive packet in TCP mode, Not supported in W5100...
int8_t getsockopt(uint8_t sn, sockopt_type sotype, void *arg)
get socket options
#define Sn_MR_MACRAW
MAC LAYER RAW SOCK.
uint16_t getSn_TX_FSR(uint8_t sn)
Get Sn_TX_FSR register.
#define Sn_CR_SEND_KEEP
Send keep alive message.
#define getSn_TX_RD(sn)
Get Sn_TX_RD register.
#define SOCK_MACRAW
MAC raw mode socket.
ctlsock_type
The type of ctlsocket().
#define SOCKFATAL_PACKLEN
Invalid packet length. Fatal Error.
#define Sn_CR_DISCON
Send closing request in TCP mode.
int8_t close(uint8_t sn)
Close a socket.
Set MSS. Sn_MSSR ( setSn_MSSR(), getSn_MSSR() )
#define PACK_FIRST
In Non-TCP packet, It indicates to start receiving a packet. (When W5300, This flag can be applied) ...
get the masked interrupt of socket. refer to sockint_kind, Not supported in W5100 ...
get the socket interrupt. refer to sockint_kind
#define Sn_CR_CLOSE
Close socket.
#define PACK_REMAINED
In Non-TCP packet, It indicates to remain a packet to be received. (When W5300, This flag can be appl...
#define getSn_RxMAX(sn)
Get the max RX buffer size of socket sn.
#define SF_IO_NONBLOCK
Socket nonblock io mode. It used parameter in socket().
#define SOCK_CLOSED
Closed.
#define SOCK_ANY_PORT_NUM
#define SOCKERR_SOCKCLOSED
Socket unexpectedly closed.
#define setSn_TX_WRSR(sn, txwrs)
Set Sn_TX_WRSR register.
#define setSUBR(subr)
Set SUBR.
int32_t recv(uint8_t sn, uint8_t *buf, uint16_t len)
Receive data from the connected peer.
Valid only in getsockopt. Get the free data size of Socekt TX buffer. Sn_TX_FSR, getSn_TX_FSR() ...
#define SOCKERR_SOCKINIT
Socket is not initialized or SIPR is Zero IP address when Sn_MR_TCP.
#define SF_UNI_BLOCK
In Sn_MR_UDP with SF_MULTI_ENABLE. Valid only in W5500.
#define getSn_MSSR(sn)
Get Sn_MSSR register.
#define setSn_IMR(sn, imr)
Set Sn_IMR register.
uint8_t sock_pack_info[_WIZCHIP_SOCK_NUM_]
#define getSn_DPORT(sn)
Get Sn_DPORT register.
Set the destination Port number. Sn_DPORT ( setSn_DPORT(), getSn_DPORT() )
#define Sn_IR_SENDOK
SEND_OK Interrupt.
clear the interrupt of socket with sockint_kind
#define getSn_SR(sn)
Get Sn_SR register.
#define SOCK_IO_BLOCK
Socket Block IO Mode in setsockopt().
Valid only in getsockopt(), For set flag of socket refer to flag in socket().
Set TOS. Sn_TOS ( setSn_TOS(), getSn_TOS() )
sockopt_type
The type of socket option in setsockopt() or getsockopt()
#define Sn_MR_IPRAW
IP LAYER RAW SOCK.
#define setSn_TOS(sn, tos)
Set Sn_TOS register.
int32_t sendto(uint8_t sn, uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t port)
Sends datagram to the peer with destination IP address and port number passed as parameter.
set socket IO mode with SOCK_IO_BLOCK or SOCK_IO_NONBLOCK
#define setSn_KPALVTR(sn, kpalvt)
Set Sn_KPALVTR register.
int8_t setsockopt(uint8_t sn, sockopt_type sotype, void *arg)
set socket options
#define SOCK_CLOSE_WAIT
Closing state.
int8_t socket(uint8_t sn, uint8_t protocol, uint16_t port, uint8_t flag)
Open a socket.
int8_t disconnect(uint8_t sn)
Try to disconnect a connection socket.
#define setSn_DIPR(sn, dipr)
Set Sn_DIPR register.
#define getSn_KPALVTR(sn)
Get Sn_KPALVTR register.
#define getSUBR(subr)
Get SUBR.
#define getSn_TOS(sn)
Get Sn_TOS register.
#define SOCKERR_SOCKSTATUS
Invalid socket status for socket operation.
int32_t recvfrom(uint8_t sn, uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t *port)
Receive datagram of UDP or MACRAW.
Valid only in getsockopt. Get the packet information as PACK_FIRST, PACK_REMAINED, and PACK_COMPLETED in other then TCP mode.
set the interrupt mask of socket with sockint_kind, Not supported in W5100
#define SOCKERR_IPINVALID
Invalid IP address.
#define CHECK_SOCKMODE(mode)
#define getSn_IMR(sn)
Get Sn_IMR register.
int8_t listen(uint8_t sn)
Listen to a connection request from a client.
#define Sn_MR_ALIGN
Alignment bit of Sn_MR.
#define Sn_CR_RECV
Update RX buffer pointer and receive data.
#define setSn_CR(sn, cr)
Set Sn_CR register.
get the size of socket buffer allocated in RX memory
#define SOCKERR_SOCKFLAG
Invalid socket flag.
#define getSn_MR(sn)
Get Sn_MR register.
#define _WIZCHIP_SOCK_NUM_
The count of independant socket of WIZCHIP.
#define setSn_PORT(sn, port)
Set Sn_PORT register.
Valid only in getsockopt. Get the received data size in socket RX buffer. Sn_RX_RSR, getSn_RX_RSR()
#define setSn_DPORT(sn, dport)
Set Sn_DPORT register.
#define Sn_CR_SEND
Update TX buffer pointer and send data.
#define SF_IGMP_VER2
In Sn_MR_UDP with SF_MULTI_ENABLE, Select IGMP version 2.
#define Sn_IR_TIMEOUT
TIMEOUT Interrupt.
int8_t connect(uint8_t sn, uint8_t *addr, uint16_t port)
Try to connect a server.
#define SOCK_IO_NONBLOCK
Socket Non-block IO Mode in setsockopt().
#define SOCKERR_PORTZERO
Port number is zero.
#define Sn_CR_OPEN
Initialize or open socket.
#define setSn_PORTR(sn, port)
Set Sn_PORTR register.
#define SOCK_ESTABLISHED
Success to connect.
int32_t send(uint8_t sn, uint8_t *buf, uint16_t len)
Send data to the connected peer in TCP socket.
Set the destination IP address. Sn_DIPR ( setSn_DIPR(), getSn_DIPR() )