25 static uint8 DNS[4]={0};
27 static uint16 local_port = 0xC000;
43 DBGA(
"MR value is %02x", IINCHIP_READ(WIZC_MR));
51 DBG(
"device_mem_init()");
55 SBUFBASEADDRESS[0] = (uint16)(__DEF_IINCHIP_MAP_TXBUF__);
56 RBUFBASEADDRESS[0] = (uint16)(__DEF_IINCHIP_MAP_RXBUF__);
60 IINCHIP_WRITE((Sn_TXMEM_SIZE(i)),tx_size[i]);
61 IINCHIP_WRITE((Sn_RXMEM_SIZE(i)),rx_size[i]);
63 DBGA(
"Sn_TXMEM_SIZE = %d, Sn_RXMEM_SIZE = %d",
64 IINCHIP_READ(Sn_TXMEM_SIZE(i)), IINCHIP_READ(Sn_RXMEM_SIZE(i)));
66 SSIZE[i] = (int16)(0);
67 RSIZE[i] = (int16)(0);
70 if(tx_size[i]==1 || tx_size[i]==2 || tx_size[i]==4 ||
71 tx_size[i]==8 || tx_size[i]==16) mul = tx_size[i];
73 SSIZE[i] = 0x400 * mul;
74 SMASK[i] = 0x400 * mul - 1;
77 if(rx_size[i]==1 || rx_size[i]==2 || rx_size[i]==4 ||
78 rx_size[i]==8 || rx_size[i]==16) mul = rx_size[i];
80 RSIZE[i] = 0x400 * mul;
81 RMASK[i] = 0x400 * mul - 1;
88 SBUFBASEADDRESS[i] = SBUFBASEADDRESS[i-1] + SSIZE[i-1];
89 RBUFBASEADDRESS[i] = RBUFBASEADDRESS[i-1] + RSIZE[i-1];
91 DBGA(
"ch = %d, SSIZE = %d, RSIZE = %d",i,SSIZE[i],RSIZE[i]);
92 DBGA(
"SBUFBASEADDRESS = %d, RBUFBASEADDRESS = %d",
93 (uint16)SBUFBASEADDRESS[i],(uint16)RBUFBASEADDRESS[i]);
99 if(netinfo->
mac[0] != 0x00 || netinfo->
mac[1] != 0x00 || netinfo->
mac[2] != 0x00 ||
100 netinfo->
mac[3] != 0x00 || netinfo->
mac[4] != 0x00 || netinfo->
mac[5] != 0x00)
101 setSHAR(netinfo->
mac);
102 if(netinfo->
ip[0] != 0x00 || netinfo->
ip[1] != 0x00 || netinfo->
ip[2] != 0x00 ||
103 netinfo->
ip[3] != 0x00) setSIPR(netinfo->
ip);
104 if(netinfo->
sn[0] != 0x00 || netinfo->
sn[1] != 0x00 || netinfo->
sn[2] != 0x00 ||
105 netinfo->
sn[3] != 0x00) setSUBR(netinfo->
sn);
106 if(netinfo->
gw[0] != 0x00 || netinfo->
gw[1] != 0x00 || netinfo->
gw[2] != 0x00 ||
107 netinfo->
gw[3] != 0x00) setGAR(netinfo->
gw);
108 if(netinfo->
dns[0] != 0x00 || netinfo->
dns[1] != 0x00 || netinfo->
dns[2] != 0x00 ||
109 netinfo->
dns[3] != 0x00){
110 DNS[0] = netinfo->
dns[0];
111 DNS[1] = netinfo->
dns[1];
112 DNS[2] = netinfo->
dns[2];
113 DNS[3] = netinfo->
dns[3];
116 if(netinfo->
dhcp != 0) DHCP = netinfo->
dhcp;
121 uint8 zero[6] = {0,};
123 DBGA(
"Reset Address(%d)", member);
138 DNS[0] = DNS[1] = DNS[2] = DNS[3] = 0;
141 ERRA(
"wrong member value (%d)", member);
147 getSHAR(netinfo->
mac);
148 getSIPR(netinfo->
ip);
149 getSUBR(netinfo->
sn);
151 netinfo->
dns[0] = DNS[0];
152 netinfo->
dns[1] = DNS[1];
153 netinfo->
dns[2] = DNS[2];
154 netinfo->
dns[3] = DNS[3];
155 netinfo->
dhcp = DHCP;
161 getDPORT(s, dstport);
168 setRTR(option_value);
171 setRCR((uint8)(option_value&0x00FF));
174 setIMR((uint8)(option_value&0x00FF));
184 ERRA(
"wrong socket number(%d)", s);
201 if((IINCHIP_READ(Sn_MR(Sn_MR_TCP))&0x0F) != Sn_MR_TCP)
210 ERRA(
"wrong socket number(%d)", s);
218 case SOCK_IPRAW:
return 11;
219 case SOCK_MACRAW:
return 12;
220 case SOCK_PPPOE:
return 13;
223 if((IINCHIP_READ(Sn_MR(Sn_MR_UDP))&0x0F) != Sn_MR_UDP)
231 return getSn_TX_FSR(s);
236 return getSn_RX_RSR(s);
242 ERRA(
"wrong socket number(%d)", s);
247 if(local_port == 0xffff) local_port = 0xc000;
253 IINCHIP_WRITE(Sn_MR(s),Sn_MR_TCP);
254 IINCHIP_WRITE(Sn_PORT0(s),(uint8)((port & 0xff00) >> 8));
255 IINCHIP_WRITE((Sn_PORT0(s) + 1),(uint8)(port & 0x00ff));
256 IINCHIP_WRITE(Sn_CR(s),Sn_CR_OPEN);
257 while(IINCHIP_READ(Sn_CR(s)));
258 DBGA(
"Sn_SR = %.2x , Protocol = %.2x", IINCHIP_READ(Sn_SR(s)), IINCHIP_READ(Sn_MR(s)));
260 if (IINCHIP_READ(Sn_SR(s)) != SOCK_INIT) {
261 DBGA(
"wrong status(%d)", IINCHIP_READ(Sn_SR(s)));
264 IINCHIP_WRITE(Sn_CR(s),Sn_CR_LISTEN);
265 while(IINCHIP_READ(Sn_CR(s)));
277 if(ret !=
RET_OK)
return ret;
288 uint8 srcip[4], snmask[4];
291 ERRA(
"wrong socket number(%d)", s);
293 }
else if(dip == NULL) {
299 if(local_port == 0xffff) local_port = 0xc000;
305 IINCHIP_WRITE(Sn_MR(s),Sn_MR_TCP);
306 IINCHIP_WRITE(Sn_PORT0(s),(uint8)((sport & 0xff00) >> 8));
307 IINCHIP_WRITE((Sn_PORT0(s) + 1),(uint8)(sport & 0x00ff));
308 IINCHIP_WRITE(Sn_CR(s),Sn_CR_OPEN);
309 while(IINCHIP_READ(Sn_CR(s)) );
310 DBGA(
"Sn_SR = %.2x , Protocol = %.2x", IINCHIP_READ(Sn_SR(s)), IINCHIP_READ(Sn_MR(s)));
315 if( ((dip[0] == 0xFF) && (dip[1] == 0xFF) &&
316 (dip[2] == 0xFF) && (dip[3] == 0xFF)) ||
317 ((dip[0] == 0x00) && (dip[1] == 0x00) &&
318 (dip[2] == 0x00) && (dip[3] == 0x00)) || (sport == 0x00) )
320 DBG(
"invalid ip or port");
321 DBGA(
"SOCK(%d)-[%02x.%02x.%02x.%02x, %d]",s,
322 dip[0], dip[1], dip[2], dip[3] , sport);
325 else if( (srcip[0]==0 && srcip[1]==0 && srcip[2]==0 && srcip[3]==0) &&
326 (snmask[0]!=0 || snmask[1]!=0 || snmask[2]!=0 || snmask[3]!=0) )
328 DBG(
"Source IP is NULL while SN Mask is Not NULL");
333 IINCHIP_WRITE(Sn_DIPR0(s),dip[0]);
334 IINCHIP_WRITE((Sn_DIPR0(s) + 1),dip[1]);
335 IINCHIP_WRITE((Sn_DIPR0(s) + 2),dip[2]);
336 IINCHIP_WRITE((Sn_DIPR0(s) + 3),dip[3]);
337 IINCHIP_WRITE(Sn_DPORT0(s),(uint8)((dport & 0xff00) >> 8));
338 IINCHIP_WRITE((Sn_DPORT0(s) + 1),(uint8)(dport & 0x00ff));
339 IINCHIP_WRITE(Sn_CR(s),Sn_CR_CONNECT);
340 while (IINCHIP_READ(Sn_CR(s)) );
351 ERRA(
"wrong socket number(%d)", s);
355 socksr = IINCHIP_READ(Sn_SR(s));
356 if(socksr == SOCK_ESTABLISHED) {
358 }
else if(IINCHIP_READ(Sn_IR(s)) & Sn_IR_TIMEOUT) {
359 IINCHIP_WRITE(Sn_IR(s), (Sn_IR_TIMEOUT));
372 if(ret !=
RET_OK)
return ret;
386 ERRA(
"wrong socket number(%d)", s);
390 IINCHIP_WRITE(Sn_CR(s),Sn_CR_DISCON);
391 while(IINCHIP_READ(Sn_CR(s)));
393 status = getSn_SR(s);
402 #define TIMEOUT_CLOSE_WAIT 200
406 ERRA(
"wrong socket number(%d)", s);
410 status = getSn_SR(s);
411 if(status == SOCK_CLOSED)
goto END_OK;
415 IINCHIP_WRITE(Sn_CR(s),Sn_CR_CLOSE);
416 while(IINCHIP_READ(Sn_CR(s)));
419 IINCHIP_WRITE(Sn_IR(s), 0xFF);
425 #define TIMEOUT_CLOSE_WAIT 200
429 ERRA(
"wrong socket number(%d)", s);
433 status = getSn_SR(s);
434 if(status == SOCK_CLOSED)
goto END_OK;
435 if(status == SOCK_CLOSE_WAIT) {
436 IINCHIP_WRITE(Sn_CR(s),Sn_CR_CLOSE);
437 while(IINCHIP_READ(Sn_CR(s)));
441 IINCHIP_WRITE(Sn_IR(s), 0xFF);
445 int32
TCPSend(uint8 s,
const int8 *buf, uint16 len)
468 ERRA(
"wrong socket number(%d)", s);
470 }
else if(len == 0) {
475 status = getSn_SR(s);
481 init_windowfull_retry_cnt(s);
482 if(len > getIINCHIP_TxMAX(s)) len = getIINCHIP_TxMAX(s);
485 send_data_processing(s, (uint8*)buf, len);
487 txrd_checker[s] = IINCHIP_READ(Sn_TX_RD0(s));
488 txrd_checker[s] = (txrd_checker[s] << 8) + IINCHIP_READ(Sn_TX_RD0(s) + 1);
490 IINCHIP_WRITE(Sn_CR(s),Sn_CR_SEND);
491 while(IINCHIP_READ(Sn_CR(s)));
519 ERRA(
"wrong socket number(%d)", s);
523 status = getSn_SR(s);
529 status = incr_windowfull_retry_cnt(s);
535 txrd_checker[s] = IINCHIP_READ(Sn_TX_RD0(s));
536 txrd_checker[s] = (txrd_checker[s] << 8) + IINCHIP_READ(Sn_TX_RD0(s) + 1);
538 IINCHIP_WRITE(Sn_CR(s),Sn_CR_SEND);
539 while(IINCHIP_READ(Sn_CR(s)));
548 if(!(IINCHIP_READ(Sn_IR(s)) & Sn_IR_SEND_OK)) {
549 if(IINCHIP_READ(Sn_SR(s)) == SOCK_CLOSED) {
555 }
else IINCHIP_WRITE(Sn_IR(s), Sn_IR_SEND_OK);
557 txrd = IINCHIP_READ(Sn_TX_RD0(s));
558 txrd = (txrd << 8) + IINCHIP_READ(Sn_TX_RD0(s) + 1);
560 if(txrd > txrd_checker[s])
return txrd - txrd_checker[s];
561 else return (0xffff - txrd_checker[s]) + txrd + 1;
570 ERRA(
"wrong socket number(%d)", s);
572 }
else if(len == 0) {
579 status = getSn_SR(s);
585 if(len < RSR_len) RSR_len = len;
586 recv_data_processing(s, (uint8*)buf, RSR_len);
587 IINCHIP_WRITE(Sn_CR(s),Sn_CR_RECV);
588 while(IINCHIP_READ(Sn_CR(s)));
597 ERRA(
"wrong socket number(%d)", s);
602 if(local_port == 0xffff) local_port = 0xc000;
608 IINCHIP_WRITE(Sn_MR(s),Sn_MR_UDP);
609 IINCHIP_WRITE(Sn_PORT0(s),(uint8)((port & 0xff00) >> 8));
610 IINCHIP_WRITE((Sn_PORT0(s) + 1),(uint8)(port & 0x00ff));
611 IINCHIP_WRITE(Sn_CR(s),Sn_CR_OPEN);
612 while(IINCHIP_READ(Sn_CR(s)));
613 DBGA(
"Sn_SR = %.2x , Protocol = %.2x", IINCHIP_READ(Sn_SR(s)), IINCHIP_READ(Sn_MR(s)));
621 ERRA(
"wrong socket number(%d)", s);
625 IINCHIP_WRITE(Sn_CR(s),Sn_CR_CLOSE);
626 while(IINCHIP_READ(Sn_CR(s)));
627 IINCHIP_WRITE(Sn_IR(s), 0xFF);
632 int32
UDPSend(uint8 s,
const int8 *buf, uint16 len, uint8 *addr, uint16 port)
636 ret =
UDPSendNB(s, buf, len, addr, port);
637 if(ret <
RET_OK)
return ret;
642 if(ret ==
RET_OK)
return len;
647 int32
UDPSendNB(uint8 s,
const int8 *buf, uint16 len, uint8 *addr, uint16 port)
649 uint8 srcip[4], snmask[4], status = 0;
652 ERRA(
"wrong socket number(%d)", s);
654 }
else if(len == 0 || addr == NULL) {
655 if(len == 0)
ERR(
"Zero length");
656 else ERR(
"NULL Dst IP");
660 status = getSn_SR(s);
665 if (len > getIINCHIP_TxMAX(s)) len = getIINCHIP_TxMAX(s);
669 if((addr[0]==0x00 && addr[1]==0x00 && addr[2]==0x00 &&
670 addr[3]==0x00) || (port==0x00))
672 DBG(
"invalid ip or port");
673 DBGA(
"SOCK(%d)-[%02x.%02x.%02x.%02x, %d, %d]",s,
674 addr[0], addr[1], addr[2], addr[3] , port, len);
677 else if( (srcip[0]==0 && srcip[1]==0 && srcip[2]==0 && srcip[3]==0) &&
678 (snmask[0]!=0 || snmask[1]!=0 || snmask[2]!=0 || snmask[3]!=0) )
680 DBG(
"Source IP is NULL while SN Mask is Not NULL");
685 IINCHIP_WRITE(Sn_DIPR0(s),addr[0]);
686 IINCHIP_WRITE((Sn_DIPR0(s) + 1),addr[1]);
687 IINCHIP_WRITE((Sn_DIPR0(s) + 2),addr[2]);
688 IINCHIP_WRITE((Sn_DIPR0(s) + 3),addr[3]);
689 IINCHIP_WRITE(Sn_DPORT0(s),(uint8)((port & 0xff00) >> 8));
690 IINCHIP_WRITE((Sn_DPORT0(s) + 1),(uint8)(port & 0x00ff));
692 send_data_processing(s, (uint8*)buf, len);
694 IINCHIP_WRITE(Sn_CR(s),Sn_CR_SEND);
695 while(IINCHIP_READ(Sn_CR(s)));
703 uint8 ir = IINCHIP_READ(Sn_IR(s));
706 if(!(ir & Sn_IR_SEND_OK)) {
707 if(ir & Sn_IR_TIMEOUT) {
709 IINCHIP_WRITE(Sn_IR(s), (Sn_IR_SEND_OK | Sn_IR_TIMEOUT));
713 }
else IINCHIP_WRITE(Sn_IR(s), Sn_IR_SEND_OK);
718 int32
UDPRecv(uint8 s, int8 *buf, uint16 len, uint8 *addr, uint16 *port)
720 uint8 prebuf[8], status = 0;
721 uint16 tmp_len = 0, RSR_len = 0;
724 ERRA(
"wrong socket number(%d)", s);
726 }
else if(len == 0) {
731 status = getSn_SR(s);
738 DBGA(
"wrong data received (%d)", RSR_len);
739 recv_data_ignore(s, RSR_len);
740 IINCHIP_WRITE(Sn_CR(s),Sn_CR_RECV);
741 while(IINCHIP_READ(Sn_CR(s)));
745 recv_data_processing(s, prebuf, 8);
746 IINCHIP_WRITE(Sn_CR(s),Sn_CR_RECV);
756 *port = (*port << 8) + prebuf[5];
759 tmp_len = (tmp_len << 8) + prebuf[7];
760 while(IINCHIP_READ(Sn_CR(s)));
762 DBGA(
"UDP Recv - addr(%d.%d.%d.%d:%d), t(%d), R(%d)",
763 addr[0], addr[1], addr[2], addr[3], *port, tmp_len, RSR_len);
765 ERR(
"UDP Recv len Zero - remove rest all");
767 IINCHIP_WRITE(Sn_CR(s),Sn_CR_RECV);
768 while(IINCHIP_READ(Sn_CR(s)));
776 tmp_len = RSR_len - len;
778 DBGA(
"Recv buffer not enough - len(%d)", len);
781 switch (IINCHIP_READ(Sn_MR(s)) & 0x07)
784 recv_data_processing(s, (uint8*)buf, RSR_len);
785 IINCHIP_WRITE(Sn_CR(s),Sn_CR_RECV);
788 while(IINCHIP_READ(Sn_CR(s)));
789 DBG(
"Ignore rest data");
790 recv_data_ignore(s, tmp_len);
791 IINCHIP_WRITE(Sn_CR(s),Sn_CR_RECV);
792 while(IINCHIP_READ(Sn_CR(s)));
794 if(tmp_len)
DBGA(
"another rest data(%d)", tmp_len);
795 else DBG(
"No rest data");
803 while(IINCHIP_READ(Sn_CR(s)));