T108: update gpio factory at

Change-Id: I20bd1b45522d11918fbf838691aefcdadbb9060a
diff --git a/mbtk/libmbtk_factory/mbtk_gpio.c b/mbtk/libmbtk_factory/mbtk_gpio.c
index cfcd1fb..e876195 100755
--- a/mbtk/libmbtk_factory/mbtk_gpio.c
+++ b/mbtk/libmbtk_factory/mbtk_gpio.c
@@ -1437,7 +1437,7 @@
     return n;
 }
 
-#elif defined(MBTK_PROJECT_T108)
+#elif 0//(MBTK_PROJECT_T108)
 int gpio_register_test_out(int port, int value)
 {
     int ret;
@@ -1513,7 +1513,7 @@
 
 int gpio_test_init_test_gpio_mode(void)
 {
-    int test_gpio[] = {
+    const int test_gpio[] = {
 		99,
 		117,
 		21,22,23,24,44,41,120,
@@ -1528,8 +1528,6 @@
     printf("[init]gpio_test_init_test_gpio_mode BEGIN\n");
 	
 	/* [1 all gpio set to low] */
-	//system("echo 118 > /sys/class/gpio/unexport");
-	//system("echo 118 > /sys/class/gpio/export");
 	gpio_register_set_func_0(118);
 	gpio_register_set_direction(118, 0);
 	system("echo in  > /sys/class/gpio/gpio118/direction");
@@ -1542,7 +1540,6 @@
 			//ret = gpio_register_get_value(test_gpio[i]);
 			//printf("[init]get gpio%d=%d\n", test_gpio[i], ret);
         }
-		//system("hwacc w 0xd401e1ac 0x1040");	//52
 
 		for(i = 0; i <= 35; i++){
 		  ret = gpio_register_get_value(118);
@@ -1567,42 +1564,244 @@
 
 int mbtk_at_gpio(void* arg)
 {
-    
 
-    int test_gpio[] = {
-		99,
-		117,
-		21,22,23,24,44,41,120,
-		8,127,46,59,58,57,56,55,48,19,34,33,35,36,49,
-		50,25,28,26,122,20,10,11,
-		39,40,37,38,
-		51,52,31,32,
-
+    const int test_gpio[] = {
+	  //GPIO	PIN		GPIO	PIN		GPIO	PIN		GPIO	PIN
+		99,		170,	117,	59,		21,		61,		22,		62,
+		23,		144,	24,		147,	44,		5,		41,		159,
+		120,	143,	8,		171,	127,	160,	46,		149,
+		59,		19,		58,		18,		57,		20,		56,		21,
+		55,		22,		48,		23,		19,		3,		34,		79,
+		33,		80,		35,		78,		36,		77,		49,		43,
+		50,		42,		25,		67,		28,		66,		26,		65,
+		122,	169,	20,		152,	10,		74,		11,		73,
+		39,		166,	40,		164,	37,		165,	38,		163,
+		51,		58,		52,		60,		31,		57,		32,		56,
 	};
 
     int i, n = 0, ret, total;
     int *fail_io = (int *)arg;
+	int try_count = 0;
     total = (sizeof(test_gpio)/sizeof(int));
-	printf("T108 Start test gpio V1.0, total gpio=%d\n", total);
+	printf("T108 Start test gpio V1.0, total gpio=%d\n", (total/2));
 	
-    /* [1 all gpio Init] */
-    gpio_test_init_test_gpio_mode();
+	for(try_count; try_count < 4; try_count++){
+	    n = 0;
+	    /* [1 all gpio Init] */
+	    gpio_test_init_test_gpio_mode();
 
-    /* [2 GPIOTEST] */
-	for(i = 0; i < total; i++){
-        ret = gpio_register_test_out(test_gpio[i], 0);
-        if(-1 == ret){
-            printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio[i]);
-            fail_io[n] = test_gpio[i];
-            n++;
-        }else{
-            //printf("############gpio [%d] test success############\n", test_gpio[i]);
-        }
+	    /* [2 GPIOTEST] */
+		for(i = 0; i < total; i = i + 2){
+	        ret = gpio_register_test_out(test_gpio[i], 0);
+	        if(-1 == ret){
+	            printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio[i]);
+				if(try_count != 3){
+					printf(" ---TEST FAILED! RETRY!--- \n");
+        			usleep(5000);
+					break;
+				}
+	            fail_io[n] = test_gpio[i+1];
+	            n++;
+	        }else{
+	            //printf("############gpio [%d] test success############\n", test_gpio[i]);
+	        }
+	    }
+	    if(0 == n){
+	    	break;
+		}
     }
     return n;
 }
 
 
+#elif (MBTK_PROJECT_T108)
+static int gpio_test(int port)
+{
+    int ret;
+    int i;
+    int valueh = 0;
+    int valuel = 1;
+    
+    //printf("Gpio port [%d] test start!\n", port);
+    ret = gpio_register_set_func_0(port);           
+    if(ret){
+        printf("gpio_port can't support!\n");
+        return -1;
+    }
+    gpio_register_set_direction(port, 1);           
+    //ret = gpio_register_get_value(port);
+    //printf("gpio default value is: %d\n", ret);
+    
+    
+    //[High]
+    for(i = 0; i <= 5; i++){
+        gpio_register_set_value(port, 1);
+        usleep(50);
+        //valueh = gpio_register_get_value(port);
+        //printf("set high? %d\n", valueh);
+        if(1 == valueh){
+            break;
+        }
+    }
+
+    for(i = 0; i <= 5; i++){
+        ret = gpio_register_get_value(118);  
+        if(1 == ret){
+
+            break;
+        }
+		usleep(3000);
+    }
+    //printf("count=%d \n", i);
+  	//printf("******gpio should is high: %d.******\n", ret);
+    if(1 != ret){
+        ret=-1;
+        goto exit;
+    }
+
+    //[Low]
+	usleep(200);
+	for(i = 0; i <= 9; i++){
+		gpio_register_set_value(port, 0);              
+		usleep(50);
+		//valuel = gpio_register_get_value(port);
+		//printf("set low? %d\n", valuel);
+		if(0 == valuel){
+			break;
+		}
+	}
+
+    for(i = 0; i <= 5; i++){
+        ret = gpio_register_get_value(118);  
+        if(0 == ret){
+        
+            break;
+        }
+        usleep(3000);
+    }
+    //printf("count=%d \n", i);
+    //printf("******gpio should is low: %d.******\n", ret);
+    if(0 != ret){
+        ret=-1;
+        goto exit;
+    }
+
+exit:
+    gpio_register_set_value(port, 0);
+    gpio_register_set_direction(port, 0);  
+    return ret;
+    
+}
+
+static int gpio_test_init_test_gpio_mode(void)
+{
+	const int test_gpio[] = {
+      99,8,117,21,22,23,24,41,120,19,123,
+      58,59,57,56,55,48,125,127,36,35,34,
+      33,54,47,53,46,50,49,11,10,26,28,25,
+      27,32,31,51,52,39,40,37,38,
+      44,45
+	};
+	
+    int i, j, ret, total, n = 0;
+    total = (sizeof(test_gpio)/sizeof(int));
+    //printf("[init]gpio_test_init_test_gpio_mode BEGIN\n");
+	
+	/* [1 all gpio set to low] */
+	gpio_register_set_func_0(118);
+	gpio_register_set_direction(118, 0);
+	
+    for(j = 0; j < 5 ; j++){
+        for(i = 0; i < total; i++){
+            gpio_register_set_func_0(test_gpio[i]);
+            gpio_register_set_direction(test_gpio[i], 1);
+            gpio_register_set_value(test_gpio[i], 0);
+			//ret = gpio_register_get_value(test_gpio[i]);
+			//printf("[init]get gpio%d=%d\n", test_gpio[i], ret);
+        }
+
+
+		for(i = 0; i <= 10; i++){
+		  ret = gpio_register_get_value(118);
+		  //printf("[init]get gpi118=%d\n", ret);
+		  usleep(3000);          
+		  if(0 == (ret)){
+			  break;
+		  }
+		}
+		
+		//printf("pre set 118: %d, times: %d\n",(ret), j);
+		if(0 == (ret)){
+		  break;
+		}
+    }
+	if(0 != (ret)){
+		printf("!!!Set all low FAIL, please retest\n");
+		return -1;
+	}
+	return 0;
+}
+
+int mbtk_at_gpio(void* arg)
+{
+    const int test_gpio[] = { 
+	//GPIO	PIN		GPIO	PIN		GPIO	PIN		GPIO	PIN
+	99,		170,	8,		171,	117,	59,		21,		61,
+	22,		62,		23,		144,	24,		147,	41,		159,
+	120,	143,	19,		3,		123,	5,		58,		18,
+	59,		19,		57,		20,		56,		21,		55,		22,
+	48,		23,		125,	149,	127,	160,	36,		77,
+	35,		78,		34,		79,		33,		80,		54,		163,
+	#if 0	//Hard Ware ERROR!
+	47,		164,
+	46,		166,
+	#endif 
+	53,		165,	50,		42,		49,		43,		11,		73,
+	10,		74,		26,		65,		28,		66,		25,		67,
+	27,		68,		32,		56,		31,		57,		51,		58,
+	52,		60,		39,		192,	40,		193,	37,		194,
+	38,		195,
+	#if 0	//undefine GNSS
+	44,		161,
+	45,		151,
+	#endif
+	};
+
+    int i, n = 0, ret, total;
+    int *fail_io = (int *)arg;
+    int try_count;
+    total = (sizeof(test_gpio)/sizeof(int));
+	printf("T108 V2 Start test gpio V0.8, total gpio=%d\n", (total/2));
+	
+    for(try_count = 0; try_count < 4; try_count++){
+        n = 0;
+        /* [1 all gpio Init] */
+        gpio_test_init_test_gpio_mode();
+        
+        /* [2 GPIOTEST] */
+    	for(i = 0; i < total; i = i + 2){
+            ret = gpio_test(test_gpio[i]);
+            if(-1 == ret){
+				printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio[i]);
+				if(try_count != 3){
+					printf(" ---TEST FAILED! RETRY!--- \n");
+        			usleep(5000);
+					break;
+				}
+                fail_io[n] = test_gpio[i+1];
+                n++;
+            }else{
+                //printf("############gpio [%d] test success############\n", test_gpio[i]);
+            }
+        }
+        if(0 == n){
+			printf(" ---ALL PASS---\n");
+            break;
+        }
+    }
+    return n;
+}
+
 
 #else
 int mbtk_at_gpio(void* arg)