Blame view

Tangible/w5100.c 33.1 KB
67b73333   amoreau   Ajout du squelette
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
  /*
   * (c)COPYRIGHT
   * ALL RIGHT RESERVED
   *
   * FileName : w5100.c
   * Revision History :
   * ----------	-------		------------------------------------------------
   * 	Date			version	  	Description
   * ----------	-------  	------------------------------------------------
   * 01/25/2007	1.1			Bug is Fixed in the Indirect Mode 
   *							: Memory mapping error	
   * ----------	-------		------------------------------------------------
   * 01/08/2008	1.2			Modification of Socket Command Part
   *							: Check if the appropriately performed after writing Sn_CR
   *
   *							Modification of SPI Part
   *							: SPI code changed by adding 'spi.h'.
   *							: Change control type for SPI port from byte to bit.
   * ----------	-------		------------------------------------------------
   * 01/15/2008	1.3			Bug is Fixed in the pppinit() fuction.
   *							: do not clear interrupt value, so fixed.
   *
   *		                   			Modification of ISR
   *                   				: Do not exit ISR, if there is interrupt.
   * ----------	-------		------------------------------------------------
   * 03/21/2008	1.4			Modification of SetMR() function
   *                   				: Use IINCHIP_WRITE() function in Direct or SPI mode.
   * ----------	-------		------------------------------------------------
   */
  #include <stdio.h>
  #include <string.h>
  
  #include <avr/interrupt.h>
  // #include <avr/io.h> 
     
  #include "ethernet.h"
  #include "socket.h"
  #include "w5100.h"
  
  #ifdef __DEF_IINCHIP_PPP__
     #include "md5.h"
  #endif
  
  
  #if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_SPI_MODE__)
  #include "spi.h"		//+2007113[jhpark]
  #endif
  
  static uint8 I_STATUS[MAX_SOCK_NUM];
  static uint16 SMASK[MAX_SOCK_NUM]; /**< Variable for Tx buffer MASK in each channel */
  static uint16 RMASK[MAX_SOCK_NUM]; /**< Variable for Rx buffer MASK in each channel */
  static uint16 SSIZE[MAX_SOCK_NUM]; /**< Max Tx buffer size by each channel */
  static uint16 RSIZE[MAX_SOCK_NUM]; /**< Max Rx buffer size by each channel */
  static uint16 SBUFBASEADDRESS[MAX_SOCK_NUM]; /**< Tx buffer base address by each channel */
  static uint16 RBUFBASEADDRESS[MAX_SOCK_NUM]; /**< Rx buffer base address by each channel */
  
  uint8 getISR(uint8 s)
  {
  	return I_STATUS[s];
  }
  
  void putISR(uint8 s, uint8 val)
  {
     I_STATUS[s] = val;
  }
  
  uint16 getIINCHIP_RxMAX(uint8 s)
  {
     return RSIZE[s];
  }
  uint16 getIINCHIP_TxMAX(uint8 s)
  {
     return SSIZE[s];
  }
  uint16 getIINCHIP_RxMASK(uint8 s)
  {
     return RMASK[s];
  }
  uint16 getIINCHIP_TxMASK(uint8 s)
  {
     return SMASK[s];
  }
  uint16 getIINCHIP_RxBASE(uint8 s)
  {
     return RBUFBASEADDRESS[s];
  }
  uint16 getIINCHIP_TxBASE(uint8 s)
  {
     return SBUFBASEADDRESS[s];
  }
  
   /**
  @brief	This function writes the data into W5100 registers.
  */
  uint8 IINCHIP_WRITE(uint16 addr,uint8 data)
  {
  // DIRECT MODE I/F
  #if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_DIRECT_MODE__)
  	IINCHIP_ISR_DISABLE();
  	*((vuint8*)(addr)) = data;
  	IINCHIP_ISR_ENABLE();
  #elif(__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__)	/* INDIRECT MODE I/F */
  	   IINCHIP_ISR_DISABLE();
  	   *((vuint8*)IDM_AR0) = (uint8)((addr & 0xFF00) >> 8);
  	   *((vuint8*)IDM_AR1) = (uint8)(addr & 0x00FF);
  	   *((vuint8*)IDM_DR)  = data;
  	   IINCHIP_ISR_ENABLE();
  #elif (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_SPI_MODE__)
  	   IINCHIP_ISR_DISABLE();
  	IINCHIP_SpiInit();
  	
  	//SPI MODE I/F
  	IINCHIP_CSoff();                             // CS=0, SPI start
  
  	IINCHIP_SpiSendData(0xF0);
  	IINCHIP_SpiSendData((addr & 0xFF00) >> 8);
  	IINCHIP_SpiSendData(addr & 0x00FF);
  	IINCHIP_SpiSendData(data);
  
  	IINCHIP_CSon();    
  
  	   IINCHIP_ISR_ENABLE();   
  #else
  	#error "unknown bus type"
  #endif
  	return 1;
  }
  
  
  /**
  @brief	This function reads the value from W5100 registers.
  */
  uint8 IINCHIP_READ(uint16 addr)
  {
  	uint8 data;
  
  // DIRECT MODE I/F
  
  #if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_DIRECT_MODE__)
  	IINCHIP_ISR_DISABLE();
  	data = *((vuint8*)(addr));
  	IINCHIP_ISR_ENABLE();
  #elif(__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__)   
  	   IINCHIP_ISR_DISABLE();
  	   *((vuint8*)IDM_AR0) = (uint8)((addr & 0xFF00) >> 8);
  	   *((vuint8*)IDM_AR1) = (uint8)(addr & 0x00FF);
  	   data = *((vuint8*)IDM_DR);
  	   IINCHIP_ISR_ENABLE();
     
  #elif (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_SPI_MODE__)
  	   IINCHIP_ISR_DISABLE();
  	IINCHIP_SpiInit();	
  	IINCHIP_CSoff();                             // CS=0, SPI start
  
  	IINCHIP_SpiSendData(0x0F);
  	IINCHIP_SpiSendData((addr & 0xFF00) >> 8);
  	IINCHIP_SpiSendData(addr & 0x00FF);
  	
  	
  	IINCHIP_SpiSendData(0);
  	data = IINCHIP_SpiRecvData();
  
  	IINCHIP_CSon();                          	// SPI end
  	   IINCHIP_ISR_ENABLE();
  #else
  	#error "unknown bus type"
  #endif
  	return data;
  }
  
  
  /**
  @brief	This function writes into W5100 memory(Buffer)
  */ 
  uint16 wiz_write_buf(uint16 addr,uint8* buf,uint16 len)
  {
  #if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_DIRECT_MODE__)
  	IINCHIP_ISR_DISABLE();
     	memcpy((uint8 *)addr, buf, len);
  	IINCHIP_ISR_ENABLE();
  #elif (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__)
  	   uint16 idx = 0;
  	   IINCHIP_ISR_DISABLE();
  	   *((vuint8*)IDM_AR0) = (uint8)((addr & 0xFF00) >> 8);
  	   *((vuint8*)IDM_AR1) = (uint8)(addr & 0x00FF);
  	   for (idx = 0; idx < len ; idx++) *((vuint8*)IDM_DR) = buf[idx];
  	   IINCHIP_ISR_ENABLE();
  #elif (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_SPI_MODE__)
  	uint16 idx = 0;
  	
  	   IINCHIP_ISR_DISABLE();
  	IINCHIP_SpiInit();
        
  	//SPI MODE I/F
  	for(idx=0;idx<len;idx++)
  	   {
  		IINCHIP_CSoff();                             // CS=0, SPI start 
  
  		IINCHIP_SpiSendData(0xF0);
  		IINCHIP_SpiSendData(((addr+idx) & 0xFF00) >> 8);
  		IINCHIP_SpiSendData((addr+idx) & 0x00FF);
  		IINCHIP_SpiSendData(buf[idx]);
  
  		IINCHIP_CSon();                             // CS=0, SPI end 
  	   }
  
  	   IINCHIP_ISR_ENABLE();	   
  #else
  	#error "unknown bus type"
  #endif
  	return len;
  }
  
  
  /**
  @brief	This function reads into W5100 memory(Buffer)
  */ 
  uint16 wiz_read_buf(uint16 addr, uint8* buf,uint16 len)
  {
  #if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_DIRECT_MODE__)
  	IINCHIP_ISR_DISABLE();
  	memcpy(buf, (uint8 *)addr, len);
  	IINCHIP_ISR_ENABLE();
  #elif(__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__)
  	   uint16 idx = 0;
  	   IINCHIP_ISR_DISABLE();
  	   *((vuint8*)IDM_AR0) = (uint8)((addr & 0xFF00) >> 8);
  	   *((vuint8*)IDM_AR1) = (uint8)(addr & 0x00FF);
  	   for (idx = 0; idx < len ; idx++) buf[idx] = *((vuint8*)IDM_DR);
  	   IINCHIP_ISR_ENABLE();
  #elif (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_SPI_MODE__)
  	uint16 idx = 0;
  	   IINCHIP_ISR_DISABLE();
  
  	IINCHIP_SpiInit();
     
  	for (idx=0; idx<len; idx++)
        	   {
  		IINCHIP_CSoff();                             // CS=0, SPI start 
  
  		IINCHIP_SpiSendData(0x0F);
  		IINCHIP_SpiSendData(((addr+idx) & 0xFF00) >> 8);
  		IINCHIP_SpiSendData((addr+idx) & 0x00FF);
  
  
  		IINCHIP_SpiSendData(0);
  		buf[idx] = IINCHIP_SpiRecvData();
  
  		IINCHIP_CSon();                             // CS=0, SPI end 	   
  	   }
  
  	   IINCHIP_ISR_ENABLE();
  #else
  	#error "unknown bus type"
  #endif
  	return len;
  }
  
  
  /**
  @brief	Socket interrupt routine
  */ 
  #ifdef __DEF_IINCHIP_INT__
  ISR(INT4_vect)
  {
  	uint8 int_val;
  	IINCHIP_ISR_DISABLE();
  	int_val = IINCHIP_READ(IR);
  	
  	/* +200801[bj] process all of interupt */
     do {
     /*---*/
     
     	if (int_val & IR_CONFLICT)
     	{
     		printf("IP conflict : %.2x\r\n", int_val);
     	}
     	if (int_val & IR_UNREACH)
     	{
     		printf("INT Port Unreachable : %.2x\r\n", int_val);
     		printf("UIPR0 : %d.%d.%d.%d\r\n", IINCHIP_READ(UIPR0), IINCHIP_READ(UIPR0+1), IINCHIP_READ(UIPR0+2), IINCHIP_READ(UIPR0+3));
     		printf("UPORT0 : %.2x %.2x\r\n", IINCHIP_READ(UPORT0), IINCHIP_READ(UPORT0+1));
     	}
     
     	/* +200801[bj] interrupt clear */
     	IINCHIP_WRITE(IR, 0xf0); 
        /*---*/
     
     	if (int_val & IR_SOCK(0))
     	{
     	/* +-200801[bj] save interrupt value*/
     		I_STATUS[0] |= IINCHIP_READ(Sn_IR(0)); // can be come to over two times.
     		IINCHIP_WRITE(Sn_IR(0), I_STATUS[0]);
        /*---*/
     	}
     	if (int_val & IR_SOCK(1))
     	{
     	/* +-200801[bj] save interrupt value*/
     		I_STATUS[1] |= IINCHIP_READ(Sn_IR(1));
     		IINCHIP_WRITE(Sn_IR(1), I_STATUS[1]);
        /*---*/
     	}
     	if (int_val & IR_SOCK(2))
     	{
     	/* +-200801[bj] save interrupt value*/
     		I_STATUS[2] |= IINCHIP_READ(Sn_IR(2));
     		IINCHIP_WRITE(Sn_IR(2), I_STATUS[2]);
        /*---*/
     	}
     	if (int_val & IR_SOCK(3))
     	{
     	/* +-200801[bj] save interrupt value*/
     		I_STATUS[3] |= IINCHIP_READ(Sn_IR(3));
     		IINCHIP_WRITE(Sn_IR(3), I_STATUS[3]);
        /*---*/
     	}
     
     	/* +-200801[bj] re-read interrupt value*/
     	int_val = IINCHIP_READ(IR);
  
  	/* +200801[bj] if exist, contiue to process */
     } while (int_val != 0x00);
     /*---*/
  
  	IINCHIP_ISR_ENABLE();
  }
  #endif
  
  /**
  @brief	This function is for resetting of the iinchip. Initializes the iinchip to work in whether DIRECT or INDIRECT mode
  */ 
  void iinchip_init(void)
  {	
  	setMR( MR_RST );
  #if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__)
  	setMR( MR_IND | MR_AI );
  #ifdef __DEF_IINCHIP_DBG__	
  	printf("MR value is %d \r\n",IINCHIP_READ(MR));
  #endif	
  #endif
  }
  
  
  /**
  @brief	This function set the transmit & receive buffer size as per the channels is used
  
  Note for TMSR and RMSR bits are as follows\n
  bit 1-0 : memory size of channel #0 \n
  bit 3-2 : memory size of channel #1 \n
  bit 5-4 : memory size of channel #2 \n
  bit 7-6 : memory size of channel #3 \n\n
  Maximum memory size for Tx, Rx in the W5100 is 8K Bytes,\n
  In the range of 8KBytes, the memory size could be allocated dynamically by each channel.\n
  Be attentive to sum of memory size shouldn't exceed 8Kbytes\n
  and to data transmission and receiption from non-allocated channel may cause some problems.\n
  If the 8KBytes memory is already  assigned to centain channel, \n
  other 3 channels couldn't be used, for there's no available memory.\n
  If two 4KBytes memory are assigned to two each channels, \n
  other 2 channels couldn't be used, for there's no available memory.\n
  */ 
  void sysinit(
  	uint8 tx_size, 	/**< tx_size Tx memory size (00 - 1KByte, 01- 2KBtye, 10 - 4KByte, 11 - 8KByte) */
  	uint8 rx_size		/**< rx_size Rx memory size (00 - 1KByte, 01- 2KBtye, 10 - 4KByte, 11 - 8KByte) */
  	)
  {
  	int16 i;
  	int16 ssum,rsum;
  
  #ifdef __DEF_IINCHIP_DBG__
  	printf("sysinit()\r\n"); 
  #endif
  
  	ssum = 0;
  	rsum = 0;
  	
  	IINCHIP_WRITE(TMSR,tx_size); /* Set Tx memory size for each channel */
  	IINCHIP_WRITE(RMSR,rx_size);	 /* Set Rx memory size for each channel */
  
  	SBUFBASEADDRESS[0] = (uint16)(__DEF_IINCHIP_MAP_TXBUF__);		/* Set base address of Tx memory for channel #0 */
  	RBUFBASEADDRESS[0] = (uint16)(__DEF_IINCHIP_MAP_RXBUF__);		/* Set base address of Rx memory for channel #0 */
  
  #ifdef __DEF_IINCHIP_DBG__
  	printf("Channel : SEND MEM SIZE : RECV MEM SIZE\r\n");
  #endif
  
     for (i = 0 ; i < MAX_SOCK_NUM; i++)       // Set the size, masking and base address of Tx & Rx memory by each channel
  	{
  		SSIZE[i] = (int16)(0);
  		RSIZE[i] = (int16)(0);
  		if (ssum < 8192)
  		{
           switch((tx_size >> i*2) & 0x03)  // Set Tx memory size
  			{
  			case 0:
  				SSIZE[i] = (int16)(1024);
  				SMASK[i] = (uint16)(0x03FF);
  				break;
  			case 1:
  				SSIZE[i] = (int16)(2048);
  				SMASK[i] = (uint16)(0x07FF);
  				break;
  			case 2:
  				SSIZE[i] = (int16)(4096);
  				SMASK[i] = (uint16)(0x0FFF);
  				break;
  			case 3:
  				SSIZE[i] = (int16)(8192);
  				SMASK[i] = (uint16)(0x1FFF);
  				break;
  			}
  		}
  		if (rsum < 8192)
  		{
           switch((rx_size >> i*2) & 0x03)     // Set Rx memory size
  			{
  			case 0:
  				RSIZE[i] = (int16)(1024);
  				RMASK[i] = (uint16)(0x03FF);
  				break;
  			case 1:
  				RSIZE[i] = (int16)(2048);
  				RMASK[i] = (uint16)(0x07FF);
  				break;
  			case 2:
  				RSIZE[i] = (int16)(4096);
  				RMASK[i] = (uint16)(0x0FFF);
  				break;
  			case 3:
  				RSIZE[i] = (int16)(8192);
  				RMASK[i] = (uint16)(0x1FFF);
  				break;
  			}
  		}
  		ssum += SSIZE[i];
  		rsum += RSIZE[i];
  
        if (i != 0)             // Sets base address of Tx and Rx memory for channel #1,#2,#3
  		{
  			SBUFBASEADDRESS[i] = SBUFBASEADDRESS[i-1] + SSIZE[i-1];
  			RBUFBASEADDRESS[i] = RBUFBASEADDRESS[i-1] + RSIZE[i-1];
  		}
  #ifdef __DEF_IINCHIP_DBG__
  		printf("%d : %.4x : %.4x : %.4x : %.4x\r\n", i, (uint16)SBUFBASEADDRESS[i], (uint16)RBUFBASEADDRESS[i], SSIZE[i], RSIZE[i]);
  #endif
  	}
  }
  
  
  void setMR(uint8 val)
  {
  
  #if (__DEF_IINCHIP_BUS__ == __DEF_IINCHIP_INDIRECT_MODE__)
  	*((volatile uint8*)(MR)) = val;
  #else
  	/* 	DIRECT ACCESS	*/
  	IINCHIP_WRITE(MR,val);
  #endif	
  }
  
  
  /**
  @brief	This function sets up gateway IP address.
  */ 
  void setGAR(
  	uint8 * addr	/**< a pointer to a 4 -byte array responsible to set the Gateway IP address. */
  	)
  {
  	IINCHIP_WRITE((GAR0 + 0),addr[0]);
  	IINCHIP_WRITE((GAR0 + 1),addr[1]);
  	IINCHIP_WRITE((GAR0 + 2),addr[2]);
  	IINCHIP_WRITE((GAR0 + 3),addr[3]);
  }
  void getGWIP(uint8 * addr)
  {
  	addr[0] = IINCHIP_READ((GAR0 + 0));
  	addr[1] = IINCHIP_READ((GAR0 + 1));
  	addr[2] = IINCHIP_READ((GAR0 + 2));
  	addr[3] = IINCHIP_READ((GAR0 + 3));
  }
  
  
  /**
  @brief	It sets up SubnetMask address
  */ 
  void setSUBR(
  	uint8 * addr	/**< a pointer to a 4 -byte array responsible to set the SubnetMask address */
  	)
  {
  	IINCHIP_WRITE((SUBR0 + 0),addr[0]);
  	IINCHIP_WRITE((SUBR0 + 1),addr[1]);
  	IINCHIP_WRITE((SUBR0 + 2),addr[2]);
  	IINCHIP_WRITE((SUBR0 + 3),addr[3]);
  }
  
  
  /**
  @brief	This function sets up MAC address.
  */ 
  void setSHAR(
  	uint8 * addr	/**< a pointer to a 6 -byte array responsible to set the MAC address. */
  	)
  {
  	IINCHIP_WRITE((SHAR0 + 0),addr[0]);
  	IINCHIP_WRITE((SHAR0 + 1),addr[1]);
  	IINCHIP_WRITE((SHAR0 + 2),addr[2]);
  	IINCHIP_WRITE((SHAR0 + 3),addr[3]);
  	IINCHIP_WRITE((SHAR0 + 4),addr[4]);
  	IINCHIP_WRITE((SHAR0 + 5),addr[5]);
  }
  
  
  /**
  @brief	This function sets up Source IP address.
  */
  void setSIPR(
  	uint8 * addr	/**< a pointer to a 4 -byte array responsible to set the Source IP address. */
  	)
  {
  	IINCHIP_WRITE((SIPR0 + 0),addr[0]);
  	IINCHIP_WRITE((SIPR0 + 1),addr[1]);
  	IINCHIP_WRITE((SIPR0 + 2),addr[2]);
  	IINCHIP_WRITE((SIPR0 + 3),addr[3]);
  }
  
  
  /**
  @brief	This function gets Interrupt register in common register.
   */
  uint8 getIR( void )
  {
     return IINCHIP_READ(IR);
  }
  
  
  
  /**
  @brief	This function sets up Retransmission time.
  
  If there is no response from the peer or delay in response then retransmission 
  will be there as per RTR (Retry Time-value Register)setting
  */
  void setRTR(uint16 timeout)
  {
  	IINCHIP_WRITE(RTR0,(uint8)((timeout & 0xff00) >> 8));
  	IINCHIP_WRITE((RTR0 + 1),(uint8)(timeout & 0x00ff));
  }
  
  
  /**
  @brief	This function set the number of Retransmission.
  
  If there is no response from the peer or delay in response then recorded time 
  as per RTR & RCR register seeting then time out will occur.
  */
  void setRCR(uint8 retry)
  {
  	IINCHIP_WRITE(RCR,retry);
  }
  
  
  /**
  @brief	This function set the interrupt mask Enable/Disable appropriate Interrupt. ('1' : interrupt enable)
  
  If any bit in IMR is set as '0' then there is not interrupt signal though the bit is
  set in IR register.
  */
  void setIMR(uint8 mask)
  {
  	IINCHIP_WRITE(IMR,mask); // must be setted 0x10.
  }
  
  
  /**
  @brief	These below functions are used to get the Gateway, SubnetMask
  		and Source Hardware Address (MAC Address) and Source IP address
  */
  void getGAR(uint8 * addr)
  {
  	addr[0] = IINCHIP_READ(GAR0);
  	addr[1] = IINCHIP_READ(GAR0+1);
  	addr[2] = IINCHIP_READ(GAR0+2);
  	addr[3] = IINCHIP_READ(GAR0+3);
  }
  void getSUBR(uint8 * addr)
  {
  	addr[0] = IINCHIP_READ(SUBR0);
  	addr[1] = IINCHIP_READ(SUBR0+1);
  	addr[2] = IINCHIP_READ(SUBR0+2);
  	addr[3] = IINCHIP_READ(SUBR0+3);
  }
  void getSHAR(uint8 * addr)
  {
  	addr[0] = IINCHIP_READ(SHAR0);
  	addr[1] = IINCHIP_READ(SHAR0+1);
  	addr[2] = IINCHIP_READ(SHAR0+2);
  	addr[3] = IINCHIP_READ(SHAR0+3);
  	addr[4] = IINCHIP_READ(SHAR0+4);
  	addr[5] = IINCHIP_READ(SHAR0+5);
  }
  void getSIPR(uint8 * addr)
  {
  	addr[0] = IINCHIP_READ(SIPR0);
  	addr[1] = IINCHIP_READ(SIPR0+1);
  	addr[2] = IINCHIP_READ(SIPR0+2);
  	addr[3] = IINCHIP_READ(SIPR0+3);
  }
  
  
  /**
  @brief	These below functions are used to get the Destination Hardware Address (MAC Address), Destination IP address and Destination Port.
  */
  void getSn_DHAR(SOCKET s, uint8 * addr)
  {
  	addr[0] = IINCHIP_READ(Sn_DHAR0(s));
  	addr[1] = IINCHIP_READ(Sn_DHAR0(s)+1);
  	addr[2] = IINCHIP_READ(Sn_DHAR0(s)+2);
  	addr[3] = IINCHIP_READ(Sn_DHAR0(s)+3);
  	addr[4] = IINCHIP_READ(Sn_DHAR0(s)+4);
  	addr[5] = IINCHIP_READ(Sn_DHAR0(s)+5);
  }
  void setSn_DHAR(SOCKET s, uint8 * addr)
  {
  	IINCHIP_WRITE((Sn_DHAR0(s) + 0),addr[0]);
  	IINCHIP_WRITE((Sn_DHAR0(s) + 1),addr[1]);
  	IINCHIP_WRITE((Sn_DHAR0(s) + 2),addr[2]);
  	IINCHIP_WRITE((Sn_DHAR0(s) + 3),addr[3]);
  	IINCHIP_WRITE((Sn_DHAR0(s) + 4),addr[4]);
  	IINCHIP_WRITE((Sn_DHAR0(s) + 5),addr[5]);
  }
  void getSn_DIPR(SOCKET s, uint8 * addr)
  {
  	addr[0] = IINCHIP_READ(Sn_DIPR0(s));
  	addr[1] = IINCHIP_READ(Sn_DIPR0(s)+1);
  	addr[2] = IINCHIP_READ(Sn_DIPR0(s)+2);
  	addr[3] = IINCHIP_READ(Sn_DIPR0(s)+3);
  }
  void setSn_DIPR(SOCKET s, uint8 * addr)
  {
  	IINCHIP_WRITE((Sn_DIPR0(s) + 0),addr[0]);
  	IINCHIP_WRITE((Sn_DIPR0(s) + 1),addr[1]);
  	IINCHIP_WRITE((Sn_DIPR0(s) + 2),addr[2]);
  	IINCHIP_WRITE((Sn_DIPR0(s) + 3),addr[3]);
  }
  void getSn_DPORT(SOCKET s, uint8 * addr)
  {
  	addr[0] = IINCHIP_READ(Sn_DPORT0(s));
  	addr[1] = IINCHIP_READ(Sn_DPORT0(s)+1);
  }
  void setSn_DPORT(SOCKET s, uint8 * addr)
  {
  	IINCHIP_WRITE((Sn_DPORT0(s) + 0),addr[0]);
  	IINCHIP_WRITE((Sn_DPORT0(s) + 1),addr[1]);
  }
  
  
  /**
  @brief	This sets the maximum segment size of TCP in Active Mode), while in Passive Mode this is set by peer
  */
  void setSn_MSS(SOCKET s, uint16 Sn_MSSR0)
  {
  	IINCHIP_WRITE(Sn_MSSR0(s),(uint8)((Sn_MSSR0 & 0xff00) >> 8));
  	IINCHIP_WRITE((Sn_MSSR0(s) + 1),(uint8)(Sn_MSSR0 & 0x00ff));
  }
  
  void setSn_TTL(SOCKET s, uint8 ttl)
  {
     IINCHIP_WRITE(Sn_TTL(s), ttl);
  }
  
  
  /**
  @brief	These below function is used to setup the Protocol Field of IP Header when
  		executing the IP Layer RAW mode.
  */
  void setSn_PROTO(SOCKET s, uint8 proto)
  {
  	IINCHIP_WRITE(Sn_PROTO(s),proto);
  }
  
  
  /**
  @brief	get socket interrupt status
  
  These below functions are used to read the Interrupt & Soket Status register
  */
  uint8 getSn_IR(SOCKET s)
  {
     return IINCHIP_READ(Sn_IR(s));
  }
  
  
  /**
  @brief	 get socket status
  */
  uint8 getSn_SR(SOCKET s)
  {
     return IINCHIP_READ(Sn_SR(s));
  }
  
  
  /**
  @brief	get socket TX free buf size
  
  This gives free buffer size of transmit buffer. This is the data size that user can transmit.
  User shuold check this value first and control the size of transmitting data
  */
  uint16 getSn_TX_FSR(SOCKET s)
  {
  	uint16 val=0,val1=0;
  	do
  	{
  		val1 = IINCHIP_READ(Sn_TX_FSR0(s));
  		val1 = (val1 << 8) + IINCHIP_READ(Sn_TX_FSR0(s) + 1);
        if (val1 != 0)
  		{
     			val = IINCHIP_READ(Sn_TX_FSR0(s));
     			val = (val << 8) + IINCHIP_READ(Sn_TX_FSR0(s) + 1);
  		}
  	} while (val != val1);
     return val;
  }
  
  
  /**
  @brief	 get socket RX recv buf size
  
  This gives size of received data in receive buffer. 
  */
  uint16 getSn_RX_RSR(SOCKET s)
  {
  	uint16 val=0,val1=0;
  	do
  	{
  		val1 = IINCHIP_READ(Sn_RX_RSR0(s));
  		val1 = (val1 << 8) + IINCHIP_READ(Sn_RX_RSR0(s) + 1);
        if(val1 != 0)
  		{
     			val = IINCHIP_READ(Sn_RX_RSR0(s));
     			val = (val << 8) + IINCHIP_READ(Sn_RX_RSR0(s) + 1);
  		}
  	} while (val != val1);
     return val;
  }
  
  
  /**
  @brief	 This function is being called by send() and sendto() function also. 
  
  This function read the Tx write pointer register and after copy the data in buffer update the Tx write pointer
  register. User should read upper byte first and lower byte later to get proper value.
  */
  void send_data_processing(SOCKET s, uint8 *data, uint16 len)
  {
  	uint16 ptr;
  	ptr = IINCHIP_READ(Sn_TX_WR0(s));
  	ptr = ((ptr & 0x00ff) << 8) + IINCHIP_READ(Sn_TX_WR0(s) + 1);
  	write_data(s, data, (uint8 *)(ptr), len);
  	ptr += len;
  	IINCHIP_WRITE(Sn_TX_WR0(s),(uint8)((ptr & 0xff00) >> 8));
  	IINCHIP_WRITE((Sn_TX_WR0(s) + 1),(uint8)(ptr & 0x00ff));
  }
  
  
  /**
  @brief	This function is being called by recv() also.
  
  This function read the Rx read pointer register
  and after copy the data from receive buffer update the Rx write pointer register.
  User should read upper byte first and lower byte later to get proper value.
  */
  void recv_data_processing(SOCKET s, uint8 *data, uint16 len)
  {
  	uint16 ptr;
  	ptr = IINCHIP_READ(Sn_RX_RD0(s));
  	ptr = ((ptr & 0x00ff) << 8) + IINCHIP_READ(Sn_RX_RD0(s) + 1);
  #ifdef __DEF_IINCHIP_DBG__
  	printf("ISR_RX: rd_ptr : %.4x\r\n", ptr);
  #endif
  	read_data(s, (uint8 *)ptr, data, len); // read data
  	ptr += len;
  	IINCHIP_WRITE(Sn_RX_RD0(s),(uint8)((ptr & 0xff00) >> 8));
  	IINCHIP_WRITE((Sn_RX_RD0(s) + 1),(uint8)(ptr & 0x00ff));
  }
  
  
  /**
  @brief	for copy the data form application buffer to Transmite buffer of the chip.
  
  This function is being used for copy the data form application buffer to Transmite
  buffer of the chip. It calculate the actual physical address where one has to write
  the data in transmite buffer. Here also take care of the condition while it exceed
  the Tx memory uper-bound of socket.
  */
  void write_data(SOCKET s, vuint8 * src, vuint8 * dst, uint16 len)
  {
  	uint16 size;
  	uint16 dst_mask;
  	uint8 * dst_ptr;
  
  	dst_mask = (uint16)dst & getIINCHIP_TxMASK(s);
  	dst_ptr = (uint8 *)(getIINCHIP_TxBASE(s) + dst_mask);
  	
  	if (dst_mask + len > getIINCHIP_TxMAX(s)) 
  	{
  		size = getIINCHIP_TxMAX(s) - dst_mask;
  		wiz_write_buf((uint16)dst_ptr, (uint8*)src, size);
  		src += size;
  		size = len - size;
  		dst_ptr = (uint8 *)(getIINCHIP_TxBASE(s));
  		wiz_write_buf((uint16)dst_ptr, (uint8*)src, size);
  	} 
  	else
  	{
  		wiz_write_buf((uint16)dst_ptr, (uint8*)src, len);
  	}
  }
  
  
  /**
  @brief	This function is being used for copy the data form Receive buffer of the chip to application buffer.
  
  It calculate the actual physical address where one has to read
  the data from Receive buffer. Here also take care of the condition while it exceed
  the Rx memory uper-bound of socket.
  */
  void read_data(SOCKET s, vuint8 * src, vuint8 * dst, uint16 len)
  {
  	uint16 size;
  	uint16 src_mask;
  	uint8 * src_ptr;
  
  	src_mask = (uint16)src & getIINCHIP_RxMASK(s);
  	src_ptr = (uint8 *)(getIINCHIP_RxBASE(s) + src_mask);
  	
  	if( (src_mask + len) > getIINCHIP_RxMAX(s) ) 
  	{
  		size = getIINCHIP_RxMAX(s) - src_mask;
  		wiz_read_buf((uint16)src_ptr, (uint8*)dst,size);
  		dst += size;
  		size = len - size;
  		src_ptr = (uint8 *)(getIINCHIP_RxBASE(s));
  		wiz_read_buf((uint16)src_ptr, (uint8*) dst,size);
  	} 
  	else
  	{
  		wiz_read_buf((uint16)src_ptr, (uint8*) dst,len);
  	}
  }
  
  
  #ifdef __DEF_IINCHIP_PPP__
  #define PPP_OPTION_BUF_LEN 64
  
  uint8 pppinit_in(uint8 * id, uint8 idlen, uint8 * passwd, uint8 passwdlen);
  
  
  /**
  @brief	make PPPoE connection
  @return	1 => success to connect, 2 => Auth fail, 3 => timeout, 4 => Auth type not support
  
  */
  uint8 pppinit(uint8 * id, uint8 idlen, uint8 * passwd, uint8 passwdlen)
  {
  	uint8 ret;
  	uint8 isr;
  	
  	// PHASE0. W5100 PPPoE(ADSL) setup
  	// enable pppoe mode
  	printf("-- PHASE 0. W5100 PPPoE(ADSL) setup process --\r\n");
  	printf("\r\n");
  	IINCHIP_WRITE(MR,IINCHIP_READ(MR) | MR_PPPOE);
  
  	// open socket in pppoe mode
  	isr = IINCHIP_READ(Sn_IR(0));// first clear isr(0), W5100 at present time
  	IINCHIP_WRITE(Sn_IR(0),isr);
  	
  	IINCHIP_WRITE(PTIMER,200); // 5sec timeout
  	IINCHIP_WRITE(PMAGIC,0x01); // magic number
  	IINCHIP_WRITE(Sn_MR(0),Sn_MR_PPPOE);
  	IINCHIP_WRITE(Sn_CR(0),Sn_CR_OPEN);
  	
  	/* +20071122[chungs]:wait to process the command... */
  	while( IINCHIP_READ(Sn_CR(0)) ) 
  		;
  	/* ------- */
  	
  	ret = pppinit_in(id, idlen, passwd, passwdlen);
  
  	// close ppp connection socket
  	/* +200801 (hwkim) */
  	close(0);
  	/* ------- */
  	
  	return ret;
  }
  
  
  uint8 pppinit_in(uint8 * id, uint8 idlen, uint8 * passwd, uint8 passwdlen)
  {
  	uint8 loop_idx = 0;
  	uint8 isr = 0;
  	uint8 buf[PPP_OPTION_BUF_LEN];
  	uint16 len;
  	uint8 str[PPP_OPTION_BUF_LEN];
  	uint8 str_idx,dst_idx;
  
     // PHASE1. PPPoE Discovery
  	// start to connect pppoe connection
  	printf("-- PHASE 1. PPPoE Discovery process --");
  	printf(" ok\r\n");
  	printf("\r\n");
  	IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCON);
  	/* +20071122[chungs]:wait to process the command... */
  	while( IINCHIP_READ(Sn_CR(0)) ) 
  		;
  	/* ------- */
  	
  	wait_10ms(100);
  
  	loop_idx = 0;
  	//check whether PPPoE discovery end or not
  	while (!(IINCHIP_READ(Sn_IR(0)) & Sn_IR_PNEXT))
  	{
  		printf(".");
  		if (loop_idx++ == 10) // timeout
  		{
  			printf("timeout before LCP\r\n"); 
  			return 3;
  		}
  		wait_10ms(100);
  	}
  
     /* +200801[bj] clear interrupt value*/
     IINCHIP_WRITE(Sn_IR(0), 0xff);
     /*---*/
  
     // PHASE2. LCP process
  	printf("-- PHASE 2. LCP process --");
  		
  	// send LCP Request
  	{
  		// Magic number option
  		// option format (type value + length value + data)
  	   // write magic number value
  		buf[0] = 0x05; // type value
  		buf[1] = 0x06; // length value
  		buf[2] = 0x01; buf[3] = 0x01; buf[4] = 0x01; buf[5]= 0x01; // data
  		// for MRU option, 1492 0x05d4  
  		// buf[6] = 0x01; buf[7] = 0x04; buf[8] = 0x05; buf[9] = 0xD4;
  	}
  	send_data_processing(0, buf, 0x06);
  	IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCR); // send request 
  	/* +20071122[chungs]:wait to process the command... */
  	while( IINCHIP_READ(Sn_CR(0)) ) 
  		;
  	/* ------- */
  		
  	wait_10ms(100);
  
  	while (!((isr = IINCHIP_READ(Sn_IR(0))) & Sn_IR_PNEXT))
  	{
  		if (isr & Sn_IR_PRECV) // Not support option
  		{
     /* +200801[bj] clear interrupt value*/
           IINCHIP_WRITE(Sn_IR(0), Sn_IR_PRECV);
     /*---*/
  			len = getSn_RX_RSR(0);
  			if ( len > 0 )
  			{
  				recv_data_processing(0, str, len);
  				IINCHIP_WRITE(Sn_CR(0),Sn_CR_RECV);
  				/* +20071122[chungs]:wait to process the command... */
  				while( IINCHIP_READ(Sn_CR(0)) ) 
  					;
  				/* ------- */
  				
  				// for debug
  				//printf("LCP proc\r\n"); for (i = 0; i < len; i++) printf ("%02x ", str[i]); printf("\r\n");
  				// get option length
  				len = str[4]; len = ((len & 0x00ff) << 8) + str[5];
  				len += 2;
  				str_idx = 6; dst_idx = 0; // ppp header is 6 byte, so starts at 6.
  				do 
  				{
  					if ((str[str_idx] == 0x01) || (str[str_idx] == 0x02) || (str[str_idx] == 0x03) || (str[str_idx] == 0x05))
  					{
  						// skip as length of support option. str_idx+1 is option's length.
  						str_idx += str[str_idx+1];
  					}
  					else
  					{
  						// not support option , REJECT
  						memcpy((uint8 *)(buf+dst_idx), (uint8 *)(str+str_idx), str[str_idx+1]);
  						dst_idx += str[str_idx+1]; str_idx += str[str_idx+1];
  					}
  				} while (str_idx != len);
  	   			// for debug
  	   			// printf("LCP dst proc\r\n"); for (i = 0; i < dst_idx; i++) printf ("%02x ", dst[i]); printf("\r\n");
  	   
  	   			// send LCP REJECT packet
  	   			send_data_processing(0, buf, dst_idx);
  	   			IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCJ);
  				/* +20071122[chungs]:wait to process the command... */
  				while( IINCHIP_READ(Sn_CR(0)) ) 
  					;
  				/* ------- */
    			}
  		}
  		printf(".");
  		if (loop_idx++ == 10) // timeout
  		{
  			printf("timeout after LCP\r\n");
  			return 3;
  		}
  		wait_10ms(100);
  	}
  	printf(" ok\r\n");
  	printf("\r\n");
  
     /* +200801[bj] clear interrupt value*/
     IINCHIP_WRITE(Sn_IR(0), 0xff);
     /*---*/
  
  	printf("-- PHASE 3. PPPoE(ADSL) Authentication mode --\r\n");
  	printf("Authentication protocol : %.2x %.2x, ", IINCHIP_READ(PATR0), IINCHIP_READ(PATR0+1));
  
  	loop_idx = 0;
  	if (IINCHIP_READ(PATR0) == 0xc0 && IINCHIP_READ(PATR0+1) == 0x23)
  	{
  		printf("PAP\r\n"); // in case of adsl normally supports PAP.
  		// send authentication data
  		// copy (idlen + id + passwdlen + passwd)
  		buf[loop_idx] = idlen; loop_idx++;
  		memcpy((uint8 *)(buf+loop_idx), (uint8 *)(id), idlen); loop_idx += idlen;
  		buf[loop_idx] = passwdlen; loop_idx++;
  		memcpy((uint8 *)(buf+loop_idx), (uint8 *)(passwd), passwdlen); loop_idx += passwdlen;
  		send_data_processing(0, buf, loop_idx);
  		IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCR);
  		/* +20071122[chungs]:wait to process the command... */
  		while( IINCHIP_READ(Sn_CR(0)) ) 
  			;
  		/* ------- */
  		wait_10ms(100);
  	}	
  	else if (IINCHIP_READ(PATR0) == 0xc2 && IINCHIP_READ(PATR0+1) == 0x23)
  	{
  		uint8 chal_len;
  		md5_ctx context;
  		uint8  digest[16];
  
  		len = getSn_RX_RSR(0);
  		if ( len > 0 )
  		{
  			recv_data_processing(0, str, len);
  			IINCHIP_WRITE(Sn_CR(0),Sn_CR_RECV);
  			/* +20071122[chungs]:wait to process the command... */
  			while( IINCHIP_READ(Sn_CR(0)) ) 
  				;
  			/* ------- */
  #ifdef __DEF_IINCHIP_DBG__
  			printf("recv CHAP\r\n");
  			{
  				int16 i;
  				
  				for (i = 0; i < 32; i++) 
  					printf ("%02x ", str[i]);
  			}
  			printf("\r\n");
  #endif
  // str is C2 23 xx CHAL_ID xx xx CHAP_LEN CHAP_DATA
  // index  0  1  2  3       4  5  6        7 ...
  
  			memset(buf,0x00,64);
  			buf[loop_idx] = str[3]; loop_idx++; // chal_id
  			memcpy((uint8 *)(buf+loop_idx), (uint8 *)(passwd), passwdlen); loop_idx += passwdlen; //passwd
  			chal_len = str[6]; // chal_id
  			memcpy((uint8 *)(buf+loop_idx), (uint8 *)(str+7), chal_len); loop_idx += chal_len; //challenge
  			buf[loop_idx] = 0x80;
  #ifdef __DEF_IINCHIP_DBG__
  			printf("CHAP proc d1\r\n");
  			{
  				int16 i;
  				for (i = 0; i < 64; i++) 
  					printf ("%02x ", buf[i]);
  			}
  			printf("\r\n");
  #endif
  
  			md5_init(&context);
  			md5_update(&context, buf, loop_idx);
  			md5_final(digest, &context);
  
  #ifdef __DEF_IINCHIP_DBG__
  			printf("CHAP proc d1\r\n");
  			{
  				int16 i;				
  				for (i = 0; i < 16; i++) 
  					printf ("%02x", digest[i]);
  			}
  			printf("\r\n");
  #endif
  			loop_idx = 0;
  			buf[loop_idx] = 16; loop_idx++; // hash_len
  			memcpy((uint8 *)(buf+loop_idx), (uint8 *)(digest), 16); loop_idx += 16; // hashed value
  			memcpy((uint8 *)(buf+loop_idx), (uint8 *)(id), idlen); loop_idx += idlen; // id
  			send_data_processing(0, buf, loop_idx);
  			IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCR);
  			/* +20071122[chungs]:wait to process the command... */
  			while( IINCHIP_READ(Sn_CR(0)) ) 
  				;
  			/* ------- */
  			wait_10ms(100);
  		}
  	}
  	else
  	{
  		printf("Not support\r\n");
  #ifdef __DEF_IINCHIP_DBG__
  		printf("Not support PPP Auth type: %.2x%.2x\r\n",IINCHIP_READ(PATR0), IINCHIP_READ(PATR0+1));
  #endif
  		return 4;
  	}
  	printf("\r\n");
  
  	printf("-- Waiting for PPPoE server's admission --");
  	loop_idx = 0;
  	while (!((isr = IINCHIP_READ(Sn_IR(0))) & Sn_IR_PNEXT))
  	{
  		if (isr & Sn_IR_PFAIL)
  		{
     /* +200801[bj] clear interrupt value*/
     IINCHIP_WRITE(Sn_IR(0), 0xff);
     /*---*/
  			printf("failed\r\nReinput id, password..\r\n");
  			return 2;
  		}
  		printf(".");
  		if (loop_idx++ == 10) // timeout
  		{
     /* +200801[bj] clear interrupt value*/
     IINCHIP_WRITE(Sn_IR(0), 0xff);
     /*---*/
  			printf("timeout after PAP\r\n");
  			return 3;
  		}
  		wait_10ms(100);
  	}
     /* +200801[bj] clear interrupt value*/
     IINCHIP_WRITE(Sn_IR(0), 0xff);
     /*---*/
  	printf("ok\r\n");
  	printf("\r\n");
  	printf("-- PHASE 4. IPCP process --");
  	// IP Address
  	buf[0] = 0x03; buf[1] = 0x06; buf[2] = 0x00; buf[3] = 0x00; buf[4] = 0x00; buf[5] = 0x00;
  	send_data_processing(0, buf, 6);
  	IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCR);
  	/* +20071122[chungs]:wait to process the command... */
  	while( IINCHIP_READ(Sn_CR(0)) ) 
  		;
  	/* ------- */
  	wait_10ms(100);
  
  	loop_idx = 0;
  	while (1)
  	{
  		if (IINCHIP_READ(Sn_IR(0)) & Sn_IR_PRECV)
  		{
     /* +200801[bj] clear interrupt value*/
     IINCHIP_WRITE(Sn_IR(0), 0xff);
     /*---*/
  			len = getSn_RX_RSR(0);
  			if ( len > 0 )
  			{
  				recv_data_processing(0, str, len);
  				IINCHIP_WRITE(Sn_CR(0),Sn_CR_RECV);
  				/* +20071122[chungs]:wait to process the command... */
  				while( IINCHIP_READ(Sn_CR(0)) ) 
  					;
  				/* ------- */
  	   			//for debug
  	   			//printf("IPCP proc\r\n"); for (i = 0; i < len; i++) printf ("%02x ", str[i]); printf("\r\n");
  	   			str_idx = 6; dst_idx = 0;
  	   			if (str[2] == 0x03) // in case of NAK
  	   			{
  	   				do 
  	   				{
  	   					if (str[str_idx] == 0x03) // request only ip information
  	   					{
  	   						memcpy((uint8 *)(buf+dst_idx), (uint8 *)(str+str_idx), str[str_idx+1]);
  	   						dst_idx += str[str_idx+1]; str_idx += str[str_idx+1];
  	   					}
  	   					else
  	   					{
  	   						// skip byte
  	   						str_idx += str[str_idx+1];
  	   					}
  	   					// for debug
  	   					//printf("s: %d, d: %d, l: %d", str_idx, dst_idx, len);
  	   				} while (str_idx != len);
  	   				send_data_processing(0, buf, dst_idx);
  	   				IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCR); // send ipcp request
  	   				/* +20071122[chungs]:wait to process the command... */
  					while( IINCHIP_READ(Sn_CR(0)) ) 
  						;
  					/* ------- */
  	   				wait_10ms(100);
  	   				break;
  	   			}
  			}
  		}
  		printf(".");
  		if (loop_idx++ == 10) // timeout
  		{
  			printf("timeout after IPCP\r\n");
  			return 3;
  		}
  		wait_10ms(100);
  		send_data_processing(0, buf, 6);
  		IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCR); //ipcp re-request
  		/* +20071122[chungs]:wait to process the command... */
  		while( IINCHIP_READ(Sn_CR(0)) ) 
  			;
  		/* ------- */
  	}
  
  	loop_idx = 0;
  	while (!(IINCHIP_READ(Sn_IR(0)) & Sn_IR_PNEXT))
  	{
  		printf(".");
  		if (loop_idx++ == 10) // timeout
  		{
  			printf("timeout after IPCP NAK\r\n");
  			return 3;
  		}
  		wait_10ms(100);
  		IINCHIP_WRITE(Sn_CR(0),Sn_CR_PCR); // send ipcp request
  		/* +20071122[chungs]:wait to process the command... */
  		while( IINCHIP_READ(Sn_CR(0)) ) 
  			;
  		/* ------- */
  	}
     /* +200801[bj] clear interrupt value*/
     IINCHIP_WRITE(Sn_IR(0), 0xff);
     /*---*/
  	printf("ok\r\n");
  	printf("\r\n");
  	return 1;
  	// after this function, User must save the pppoe server's mac address and pppoe session id in current connection
  }
  
  
  /**
  @brief	terminate PPPoE connection
  */
  uint8 pppterm(uint8 * mac, uint8 * sessionid)
  {
  	uint16 i;
  	uint8 isr;
  #ifdef __DEF_IINCHIP_DBG__
  	printf("pppterm()\r\n");
  #endif
  	/* Set PPPoE bit in MR(Common Mode Register) : enable socket0 pppoe */
  	IINCHIP_WRITE(MR,IINCHIP_READ(MR) | MR_PPPOE);
  	
  	// write pppoe server's mac address and session id 
  	// must be setted these value.
  	for (i = 0; i < 6; i++) IINCHIP_WRITE((Sn_DHAR0(0)+i),mac[i]);
  	for (i = 0; i < 2; i++) IINCHIP_WRITE((Sn_DPORT0(0)+i),sessionid[i]);
  	isr = IINCHIP_READ(Sn_IR(0));
  	IINCHIP_WRITE(Sn_IR(0),isr);
  	
  	//open socket in pppoe mode
  	IINCHIP_WRITE(Sn_MR(0),Sn_MR_PPPOE);
  	IINCHIP_WRITE(Sn_CR(0),Sn_CR_OPEN);
  	/* +20071122[chungs]:wait to process the command... */
  	while( IINCHIP_READ(Sn_CR(0)) ) 
  		;
  	/* ------- */
  	wait_1us(1);
  	// close pppoe connection
  	IINCHIP_WRITE(Sn_CR(0),Sn_CR_PDISCON);
  	/* +20071122[chungs]:wait to process the command... */
  	while( IINCHIP_READ(Sn_CR(0)) ) 
  		;
  	/* ------- */
  	wait_10ms(100);
  	// close socket
  	/* +200801 (hwkim) */
  	close(0);
  	/* ------- */
  	
  
  #ifdef __DEF_IINCHIP_DBG__
  	printf("pppterm() end ..\r\n");
  #endif
  
  	return 1;
  }
  #endif