WIZlib Library API  ver 1.0
WIZlib Library API User Menual
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
w5200.c
Go to the documentation of this file.
1 
13 //#define FILE_LOG_SILENCE
14 #include "common/common.h"
15 //#include "device/w5200/w5200.h"
16 
17 //static uint8 I_STATUS[TOTAL_SOCK_NUM];
18 uint16 SMASK[TOTAL_SOCK_NUM]; //< Variable for Tx buffer MASK in each channel */
19 uint16 RMASK[TOTAL_SOCK_NUM]; //< Variable for Rx buffer MASK in each channel */
20 uint16 SSIZE[TOTAL_SOCK_NUM]; //< Max Tx buffer size by each channel */
21 uint16 RSIZE[TOTAL_SOCK_NUM]; //< Max Rx buffer size by each channel */
22 uint16 SBUFBASEADDRESS[TOTAL_SOCK_NUM]; //< Tx buffer base address by each channel */
23 uint16 RBUFBASEADDRESS[TOTAL_SOCK_NUM]; //< Rx buffer base address by each channel */
24 
25 uint8 windowfull_retry_cnt[TOTAL_SOCK_NUM];
26 
27 uint8 incr_windowfull_retry_cnt(uint8 s)
28 {
29  return ++windowfull_retry_cnt[s];
30 }
31 
32 void init_windowfull_retry_cnt(uint8 s)
33 {
34  windowfull_retry_cnt[s] = 0;
35 }
36 
37 //uint8 getISR(uint8 s)
38 //{
39 // return I_STATUS[s];
40 //}
41 //void putISR(uint8 s, uint8 val)
42 //{
43 // I_STATUS[s] = val;
44 //}
45 uint16 getIINCHIP_RxMAX(uint8 s)
46 {
47  return RSIZE[s];
48 }
49 uint16 getIINCHIP_TxMAX(uint8 s)
50 {
51  return SSIZE[s];
52 }
53 uint16 getIINCHIP_RxMASK(uint8 s)
54 {
55  return RMASK[s];
56 }
57 uint16 getIINCHIP_TxMASK(uint8 s)
58 {
59  return SMASK[s];
60 }
61 uint16 getIINCHIP_RxBASE(uint8 s)
62 {
63  return RBUFBASEADDRESS[s];
64 }
65 uint16 getIINCHIP_TxBASE(uint8 s)
66 {
67  return SBUFBASEADDRESS[s];
68 }
69 void IINCHIP_CSoff(void)
70 {
72 }
73 void IINCHIP_CSon(void)
74 {
76 }
77 uint8 IINCHIP_SpiSendData(uint8 dat)
78 {
79  return(wizspi_byte(WIZ_SPI1, dat));
80 }
81 
82 
83  /*
84 @brief This function writes the data into W5200 registers.
85 */
86 uint8 IINCHIP_WRITE(uint16 addr,uint8 data)
87 {
88  IINCHIP_ISR_DISABLE(); // Interrupt Service Routine Disable
89 
90  //SPI MODE I/F
91  IINCHIP_CSoff(); // CS=0, SPI start
92 
93  IINCHIP_SpiSendData((addr & 0xFF00) >> 8); // Address byte 1
94  IINCHIP_SpiSendData(addr & 0x00FF); // Address byte 2
95  IINCHIP_SpiSendData(0x80); // Data write command and Write data length 1
96  IINCHIP_SpiSendData(0x01); // Write data length 2
97  IINCHIP_SpiSendData(data); // Data write (write 1byte data)
98 
99  IINCHIP_CSon(); // CS=1, SPI end
100 
101  IINCHIP_ISR_ENABLE(); // Interrupt Service Routine Enable
102  return 1;
103 }
104 
105 /*
106 @brief This function reads the value from W5200 registers.
107 */
108 uint8 IINCHIP_READ(uint16 addr)
109 {
110  uint8 data;
111 
112  IINCHIP_ISR_DISABLE(); // Interrupt Service Routine Disable
113 
114  IINCHIP_CSoff(); // CS=0, SPI start
115 
116  IINCHIP_SpiSendData((addr & 0xFF00) >> 8); // Address byte 1
117  IINCHIP_SpiSendData(addr & 0x00FF); // Address byte 2
118  IINCHIP_SpiSendData(0x00); // Data read command and Read data length 1
119  IINCHIP_SpiSendData(0x01); // Read data length 2
120  data = IINCHIP_SpiSendData(0x00); // Data read (read 1byte data)
121 
122  IINCHIP_CSon(); // CS=1, SPI end
123 
124  IINCHIP_ISR_ENABLE(); // Interrupt Service Routine Enable
125  return data;
126 }
127 
128 /*
129 @brief This function writes into W5200 memory(Buffer)
130 */
131 uint16 IINCHIP_WRITE_BLOCK(uint16 addr,uint8* buf,uint16 len)
132 {
133  uint16 idx = 0;
134 
135  if(len == 0)
136  return 0;
137 
138  IINCHIP_ISR_DISABLE();
139 
140  //SPI MODE I/F
141  IINCHIP_CSoff(); // CS=0, SPI start
142 
143  IINCHIP_SpiSendData(((addr+idx) & 0xFF00) >> 8); // Address byte 1
144  IINCHIP_SpiSendData((addr+idx) & 0x00FF); // Address byte 2
145  IINCHIP_SpiSendData((0x80 | ((len & 0x7F00) >> 8))); // Data write command and Write data length 1
146  IINCHIP_SpiSendData((len & 0x00FF)); // Write data length 2
147  for(idx = 0; idx < len; idx++) // Write data in loop
148  {
149  IINCHIP_SpiSendData(buf[idx]);
150  }
151 
152  IINCHIP_CSon(); // CS=1, SPI end
153 
154  IINCHIP_ISR_ENABLE(); // Interrupt Service Routine Enable
155  return len;
156 }
157 
158 /*
159 @brief This function reads into W5200 memory(Buffer)
160 */
161 uint16 IINCHIP_READ_BLOCK(uint16 addr, uint8* buf,uint16 len)
162 {
163  uint16 idx = 0;
164 
165  IINCHIP_ISR_DISABLE(); // Interrupt Service Routine Disable
166 
167  IINCHIP_CSoff(); // CS=0, SPI start
168 
169  IINCHIP_SpiSendData(((addr+idx) & 0xFF00) >> 8); // Address byte 1
170  IINCHIP_SpiSendData((addr+idx) & 0x00FF); // Address byte 2
171  IINCHIP_SpiSendData((0x00 | ((len & 0x7F00) >> 8))); // Data read command
172  IINCHIP_SpiSendData((len & 0x00FF));
173 
174  for(idx = 0; idx < len; idx++) // Read data in loop
175  {
176  buf[idx] = IINCHIP_SpiSendData(0x00);
177 
178  }
179 
180  IINCHIP_CSon(); // CS=0, SPI end
181 
182  IINCHIP_ISR_ENABLE(); // Interrupt Service Routine Enable
183  return len;
184 }
185 
186 /*
187 @brief This function sets up gateway IP address.
188 @param addr a pointer to a 4 -byte array responsible to set the GW address.
189 */
190 void setGAR(uint8 *addr)
191 {
192  IINCHIP_WRITE((WIZC_GAR0),addr[0]);
193  IINCHIP_WRITE((WIZC_GAR1),addr[1]);
194  IINCHIP_WRITE((WIZC_GAR2),addr[2]);
195  IINCHIP_WRITE((WIZC_GAR3),addr[3]);
196 }
197 
198 /*
199 void getGWIP(uint8 * addr)
200 {
201  addr[0] = IINCHIP_READ((WIZC_GAR0));
202  addr[1] = IINCHIP_READ((WIZC_GAR1));
203  addr[2] = IINCHIP_READ((WIZC_GAR2));
204  addr[3] = IINCHIP_READ((WIZC_GAR3));
205 }
206 */
207 
208 /*
209 @brief It sets up SubnetMask address
210 @param addr a pointer to a 4 -byte array responsible to set the SubnetMask address
211 */
212 void setSUBR(uint8 *addr)
213 {
214  IINCHIP_WRITE((WIZC_SUBR0),addr[0]);
215  IINCHIP_WRITE((WIZC_SUBR1),addr[1]);
216  IINCHIP_WRITE((WIZC_SUBR2),addr[2]);
217  IINCHIP_WRITE((WIZC_SUBR3),addr[3]);
218 }
219 
220 /*
221 @brief This function sets up MAC address.
222 @param addr a pointer to a 6 -byte array responsible to set the MAC address.
223 */
224 void setSHAR(uint8 *addr)
225 {
226  IINCHIP_WRITE((WIZC_SHAR0),addr[0]);
227  IINCHIP_WRITE((WIZC_SHAR1),addr[1]);
228  IINCHIP_WRITE((WIZC_SHAR2),addr[2]);
229  IINCHIP_WRITE((WIZC_SHAR3),addr[3]);
230  IINCHIP_WRITE((WIZC_SHAR4),addr[4]);
231  IINCHIP_WRITE((WIZC_SHAR5),addr[5]);
232 }
233 
234 /*
235 @brief This function sets up Source IP address.
236 @param addr a pointer to a 4 -byte array responsible to set the Source IP address.
237 */
238 void setSIPR(uint8 *addr)
239 {
240  IINCHIP_WRITE((WIZC_SIPR0),addr[0]);
241  IINCHIP_WRITE((WIZC_SIPR1),addr[1]);
242  IINCHIP_WRITE((WIZC_SIPR2),addr[2]);
243  IINCHIP_WRITE((WIZC_SIPR3),addr[3]);
244 }
245 
246 /*
247 @brief This function sets up Source IP address.
248 */
249 void getGAR(uint8 *addr)
250 {
251  addr[0] = IINCHIP_READ(WIZC_GAR0);
252  addr[1] = IINCHIP_READ(WIZC_GAR1);
253  addr[2] = IINCHIP_READ(WIZC_GAR2);
254  addr[3] = IINCHIP_READ(WIZC_GAR3);
255 }
256 void getSUBR(uint8 *addr)
257 {
258  addr[0] = IINCHIP_READ(WIZC_SUBR0);
259  addr[1] = IINCHIP_READ(WIZC_SUBR1);
260  addr[2] = IINCHIP_READ(WIZC_SUBR2);
261  addr[3] = IINCHIP_READ(WIZC_SUBR3);
262 }
263 void getSHAR(uint8 *addr)
264 {
265  addr[0] = IINCHIP_READ(WIZC_SHAR0);
266  addr[1] = IINCHIP_READ(WIZC_SHAR1);
267  addr[2] = IINCHIP_READ(WIZC_SHAR2);
268  addr[3] = IINCHIP_READ(WIZC_SHAR3);
269  addr[4] = IINCHIP_READ(WIZC_SHAR4);
270  addr[5] = IINCHIP_READ(WIZC_SHAR5);
271 }
272 void getSIPR(uint8 *addr)
273 {
274  addr[0] = IINCHIP_READ(WIZC_SIPR0);
275  addr[1] = IINCHIP_READ(WIZC_SIPR1);
276  addr[2] = IINCHIP_READ(WIZC_SIPR2);
277  addr[3] = IINCHIP_READ(WIZC_SIPR3);
278 }
279 void getDIPR(uint8 s, uint8 *addr)
280 {
281  addr[0] = IINCHIP_READ(WIZ_SOCK_REG(s, WIZS_DIPR0));
282  addr[1] = IINCHIP_READ(WIZ_SOCK_REG(s, WIZS_DIPR1));
283  addr[2] = IINCHIP_READ(WIZ_SOCK_REG(s, WIZS_DIPR2));
284  addr[3] = IINCHIP_READ(WIZ_SOCK_REG(s, WIZS_DIPR3));
285 }
286 void getDPORT(uint8 s, uint16 *port)
287 {
288  *port = IINCHIP_READ(WIZ_SOCK_REG(s, WIZS_DPORT0)) << 8;
289  *port += IINCHIP_READ(WIZ_SOCK_REG(s, WIZS_DPORT1));
290 }
291 
292 void setMR(uint8 val)
293 {
294  IINCHIP_WRITE(WIZC_MR,val);
295 }
296 
297 /*
298 @brief This function gets Interrupt register in common register.
299  */
300 uint8 getIR(void)
301 {
302  return IINCHIP_READ(WIZC_IR);
303 }
304 
305 
306 /*
307  Retransmittion
308  */
309 
310 /*
311 @brief This function sets up Retransmission time.
312 
313 If there is no response from the peer or delay in response then retransmission
314 will be there as per RTR (Retry Time-value Register)setting
315 */
316 void setRTR(uint16 timeout)
317 {
318  IINCHIP_WRITE(WIZC_RTR0, (uint8)((timeout & 0xff00) >> 8));
319  IINCHIP_WRITE(WIZC_RTR1, (uint8)(timeout & 0x00ff));
320 }
321 
322 /*
323 @brief This function set the number of Retransmission.
324 
325 If there is no response from the peer or delay in response then recorded time
326 as per RTR & RCR register seeting then time out will occur.
327 */
328 void setRCR(uint8 retry)
329 {
330  IINCHIP_WRITE(WIZC_RCR,retry);
331 }
332 
333 
334 
335 
336 /*
337 @brief This function set the interrupt mask Enable/Disable appropriate Interrupt. ('1' : interrupt enable)
338 
339 If any bit in IMR is set as '0' then there is not interrupt signal though the bit is
340 set in IR register.
341 */
342 void setIMR(uint8 mask)
343 {
344  IINCHIP_WRITE(WIZC_IMR,mask); // must be setted 0x10.
345 }
346 
347 /*
348 @brief This sets the maximum segment size of TCP in Active Mode), while in Passive Mode this is set by peer
349 */
350 void setSn_MSS(uint8 s, uint16 Sn_MSSR0)
351 {
352  IINCHIP_WRITE(Sn_MSSR0(s),(uint8)((Sn_MSSR0 & 0xff00) >> 8));
353  IINCHIP_WRITE((Sn_MSSR0(s) + 1),(uint8)(Sn_MSSR0 & 0x00ff));
354 }
355 
356 void setSn_TTL(uint8 s, uint8 ttl)
357 {
358  IINCHIP_WRITE(Sn_TTL(s), ttl);
359 }
360 
361 
362 /*
363 @brief These below function is used to setup the Protocol Field of IP Header when
364  executing the IP Layer RAW mode.
365 */
366 void setSn_PROTO(uint8 s, uint8 proto)
367 {
368  IINCHIP_WRITE(Sn_PROTO(s),proto);
369 }
370 
371 
372 /*
373 @brief get socket interrupt status
374 
375 These below functions are used to read the Interrupt & Soket Status register
376 */
377 uint8 getSn_IR(uint8 s)
378 {
379  return IINCHIP_READ(Sn_IR(s));
380 }
381 
382 
383 /*
384 @brief get socket status
385 */
386 uint8 getSn_SR(uint8 s)
387 {
388  return IINCHIP_READ(Sn_SR(s));
389 }
390 
391 
392 /*
393 @brief get socket TX free buf size
394 
395 This gives free buffer size of transmit buffer. This is the data size that user can transmit.
396 User shuold check this value first and control the size of transmitting data
397 */
398 uint16 getSn_TX_FSR(uint8 s)
399 {
400  uint16 val=0,val1=0;
401  do
402  {
403  val1 = IINCHIP_READ(Sn_TX_FSR0(s));
404  val1 = (val1 << 8) + IINCHIP_READ(Sn_TX_FSR0(s) + 1);
405  if (val1 != 0)
406  {
407  val = IINCHIP_READ(Sn_TX_FSR0(s));
408  val = (val << 8) + IINCHIP_READ(Sn_TX_FSR0(s) + 1);
409  }
410  } while (val != val1);
411  return val;
412 }
413 
414 
415 /*
416 @brief get socket RX recv buf size
417 
418 This gives size of received data in receive buffer.
419 */
420 uint16 getSn_RX_RSR(uint8 s)
421 {
422  uint16 val=0,val1=0;
423  do
424  {
425  val1 = IINCHIP_READ(Sn_RX_RSR0(s));
426  val1 = (val1 << 8) + IINCHIP_READ(Sn_RX_RSR0(s) + 1);
427  if(val1 != 0)
428  {
429  val = IINCHIP_READ(Sn_RX_RSR0(s));
430  val = (val << 8) + IINCHIP_READ(Sn_RX_RSR0(s) + 1);
431  }
432  } while (val != val1);
433  return val;
434 }
435 
436 
437 /*
438 @brief This function is being called by send() and sendto() function also. for copy the data form application buffer to Transmite buffer of the chip.
439 
440 This function read the Tx write pointer register and after copy the data in buffer update the Tx write pointer
441 register. User should read upper byte first and lower byte later to get proper value.
442 And this function is being used for copy the data form application buffer to Transmite
443 buffer of the chip. It calculate the actual physical address where one has to write
444 the data in transmite buffer. Here also take care of the condition while it exceed
445 the Tx memory uper-bound of socket.
446 
447 */
448 void send_data_processing(uint8 s, uint8 *data, uint16 len)
449 {
450 
451  uint16 ptr;
452  uint16 size;
453  uint16 dst_mask;
454  uint8 * dst_ptr;
455 
456  ptr = IINCHIP_READ(Sn_TX_WR0(s));
457  ptr = (ptr << 8) + IINCHIP_READ(Sn_TX_WR0(s) + 1);
458 
459  dst_mask = (uint32)ptr & getIINCHIP_TxMASK(s);
460  dst_ptr = (uint8 *)(getIINCHIP_TxBASE(s) + dst_mask);
461 
462  if (dst_mask + len > getIINCHIP_TxMAX(s))
463  {
464  size = getIINCHIP_TxMAX(s) - dst_mask;
465  IINCHIP_WRITE_BLOCK((uint32)dst_ptr, (uint8*)data, size);
466  data += size;
467  size = len - size;
468  dst_ptr = (uint8 *)(getIINCHIP_TxBASE(s));
469  IINCHIP_WRITE_BLOCK((uint32)dst_ptr, (uint8*)data, size);
470  }
471  else
472  {
473  IINCHIP_WRITE_BLOCK((uint32)dst_ptr, (uint8*)data, len);
474  }
475 
476  ptr += len;
477 
478  IINCHIP_WRITE(Sn_TX_WR0(s),(uint8)((ptr & 0xff00) >> 8));
479  IINCHIP_WRITE((Sn_TX_WR0(s) + 1),(uint8)(ptr & 0x00ff));
480 
481 }
482 
483 
484 /*
485 @brief This function is being called by recv() also. This function is being used for copy the data form Receive buffer of the chip to application buffer.
486 
487 This function read the Rx read pointer register
488 and after copy the data from receive buffer update the Rx write pointer register.
489 User should read upper byte first and lower byte later to get proper value.
490 It calculate the actual physical address where one has to read
491 the data from Receive buffer. Here also take care of the condition while it exceed
492 the Rx memory uper-bound of socket.
493 */
494 void recv_data_processing(uint8 s, uint8 *data, uint16 len)
495 {
496  uint16 ptr;
497  uint16 size;
498  uint16 src_mask;
499  uint8 * src_ptr;
500 
501  ptr = IINCHIP_READ(Sn_RX_RD0(s));
502  ptr = ((ptr & 0x00ff) << 8) + IINCHIP_READ(Sn_RX_RD0(s) + 1);
503 
504  DBGA("ISR_RX: rd_ptr : %.4x", ptr);
505 
506  src_mask = (uint32)ptr & getIINCHIP_RxMASK(s);
507  src_ptr = (uint8 *)(getIINCHIP_RxBASE(s) + src_mask);
508 
509  if( (src_mask + len) > getIINCHIP_RxMAX(s) )
510  {
511  size = getIINCHIP_RxMAX(s) - src_mask;
512  IINCHIP_READ_BLOCK((uint32)src_ptr, (uint8*)data,size);
513  data += size;
514  size = len - size;
515  src_ptr = (uint8 *)(getIINCHIP_RxBASE(s));
516  IINCHIP_READ_BLOCK((uint32)src_ptr, (uint8*) data,size);
517  }
518  else
519  {
520  IINCHIP_READ_BLOCK((uint32)src_ptr, (uint8*) data,len);
521  }
522 
523  ptr += len;
524  IINCHIP_WRITE(Sn_RX_RD0(s),(uint8)((ptr & 0xff00) >> 8));
525  IINCHIP_WRITE((Sn_RX_RD0(s) + 1),(uint8)(ptr & 0x00ff));
526 }
527 
528 void recv_data_ignore(uint8 s, uint16 len)
529 {
530  uint16 ptr;
531 
532  ptr = IINCHIP_READ(Sn_RX_RD0(s));
533  ptr = ((ptr & 0x00ff) << 8) + IINCHIP_READ(Sn_RX_RD0(s) + 1);
534  ptr += len;
535  IINCHIP_WRITE(Sn_RX_RD0(s),(uint8)((ptr & 0xff00) >> 8));
536  IINCHIP_WRITE((Sn_RX_RD0(s) + 1),(uint8)(ptr & 0x00ff));
537 }
538 
539 
540 
541 
542 // ToDo: Check & Remove ???
543 
544 /*
545 @brief Output destination IP address of appropriate channel
546 @return 32bit destination address (Host Ordering)
547 */
548 uint32 GetDestAddr(
549  uint8 s //< Channel number which try to get destination IP Address */
550  )
551 {
552  uint32 addr=0;
553  int32 i = 0;
554  for(i=0; i < 4; i++)
555  {
556  addr <<=8;
557  addr += IINCHIP_READ(Sn_DIPR0(s)+i);
558  }
559  return addr;
560 }
561 
562 /*
563 @brief Output destination port number of appropriate channel
564 @return 16bit destination port number
565 */
566 uint32 GetDestPort(
567  uint8 s //< Channel number which try to get destination port */
568  )
569 {
570  uint16 port;
571  port = ((uint16) IINCHIP_READ(Sn_DPORT0(s))) & 0x00FF;
572  port <<= 8;
573  port += ((uint16) IINCHIP_READ(Sn_DPORT0(s)+1)) & 0x00FF;
574  return port;
575 }
576 
577 uint8 CheckDestInLocal(uint32 destip)
578 {
579  int32 i = 0;
580  uint8 * pdestip = (uint8*)&destip;
581  for(i =0; i < 4; i++)
582  {
583  if((pdestip[i] & IINCHIP_READ(WIZC_SUBR0+i)) != (IINCHIP_READ(WIZC_SIPR0+i) & IINCHIP_READ(WIZC_SUBR0+i)))
584  return 1; // Remote
585  }
586  return 0;
587 }
588 
589 /*
590 @brief Get handle of socket which status is same to 'status'
591 @return socket number
592 */
593 uint8 getSocket(
594  uint8 status, //< socket's status to be found */
595  uint8 start //< base of socket to be found */
596  )
597 {
598  uint8 i;
599  if(start > 3) start = 0;
600 
601  for(i = start; i < TOTAL_SOCK_NUM ; i++) if( getSn_SR(i)==status ) return i;
602  return TOTAL_SOCK_NUM;
603 }
604 
605 
606