Add L509/L508 GPIO test support.

Change-Id: I1d145e8351733aa677479096ffa129646edc8b7a
diff --git a/mbtk/mbtk_lib/src/mbtk_gpio.c b/mbtk/mbtk_lib/src/mbtk_gpio.c
index a5ec0e9..7f6c128 100755
--- a/mbtk/mbtk_lib/src/mbtk_gpio.c
+++ b/mbtk/mbtk_lib/src/mbtk_gpio.c
@@ -21,13 +21,14 @@
 #include <sys/mman.h>
 #include <ctype.h>
 #include <cutils/properties.h>
+#include "mbtk_gpio_def.h"
 #include "gpio-define.h"
 
 #define gpio_log(...)     if(gpio_debug)printf(__VA_ARGS__)
 #define HWMAP_DEVICE "/dev/hwmap"
 #define PAGE_OFFS_BITS(pgsz) ((unsigned int)(pgsz)-1)
 #define PAGE_MASK_BITS(pgsz) (~PAGE_OFFS_BITS(pgsz))
-
+#define STR_MAX_LEN 220
 typedef enum {
     MBTK_ADC0 = 0,  /* ADC 0 */
     MBTK_ADC1       /* ADC 1 */
@@ -245,7 +246,13 @@
     }
     pclose(fp);
 #else
+#ifndef MBTK_PROJECT_PN1803
+    usleep(50);
+#endif
     hwacc_register(0, reg, &ret);
+#ifndef MBTK_PROJECT_PN1803
+    usleep(50);
+#endif	
 #endif
     return ret;
 }
@@ -277,9 +284,13 @@
     pclose(fp);
 
 #else
-    usleep(20);
+#ifndef MBTK_PROJECT_PN1803
+    usleep(50);
+#endif
     hwacc_register(1, reg, &value);
-    usleep(20);
+#ifndef MBTK_PROJECT_PN1803
+    usleep(50);
+#endif
 #endif
 }
 /*
@@ -439,6 +450,9 @@
     gpio_debug = enable;
 }
 
+
+
+#if defined(MBTK_PROJECT_PN1803)
 int gpio_register_test_out(int port, int value)
 {
     int ret;
@@ -475,7 +489,6 @@
             break;
         }
     }
-    //printf("------gpio should is high: %d.------\n", ret);
     if(1 != ret){
         ret=-1;
         goto exit;
@@ -513,12 +526,8 @@
     return ret;
 }
 
-#if defined(MBTK_PROJECT_PN1803)
 int mbtk_at_gpio(void* arg)
 {
-    /*
-      error:53
-    */
     int test_gpio[] = {
      56, 55, 58, 57, 48, 59, 12, 20, 5, 43, 21,
      49, 50, 27, 28, 26, 25, 16, 17, 15, 126, 125,
@@ -573,32 +582,351 @@
 }
 
 #elif defined(MBTK_PROJECT_L508)
+int test_gpio[] = 
+{ 
+	0,2,1,6,16,15,17,99,
+	34,36,35,33,14,49,50,12,118,120,
+	43,19,7,122,3,
+	20,59,126,117,125,18,56,55,58,48,57,5,4,21,
+	23,24,13,27,28,26,25,
+	54,53,123,32,31
+};
+int total;
+total = (sizeof(test_gpio)/sizeof(int));
+	
+
+int gpio_test_init(void)
+{
+	int i,j,ret;
+	//int *fail_io = (int *)arg;
+	
+	printf("[init] L508 GPIOTEST v1 \n");
+	gpio_register_set_func_0(22);
+    gpio_register_set_direction(22, 0);	//GPIO22 DTR做输入,检测电压
+	
+	#if 0
+	/* 错误点预先关闭 */
+	gpio_register_set_func_0(123);
+	gpio_register_set_func_0(32);
+	gpio_register_set_func_0(53);
+	gpio_register_set_direction(123, 1);
+	gpio_register_set_direction(32, 1);
+	gpio_register_set_direction(53, 1);
+	#endif
+	
+	/* [1 all gpio set to low] */
+	for(j = 0; j < 6 ; 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);
+		}
+
+		for(i = 0; i <= 5; i++){
+			ret = gpio_register_get_value(22);
+			if(0 == ret){
+				break;
+			}
+			usleep(3000);
+		}
+		printf("pre set ADC: %d, times: %d\n",ret, j);
+		if(0 == ret){
+			break;
+		}
+	}
+
+	if(0 != ret){
+		printf("set all low Fail, please retest\n");
+		return -1;
+	}
+	printf("pre set SUCCESS\n");
+	return 0;
+}
+
+int gpio_test(void)
+{
+	int ret,i,j;
+	int n = 0;
+	for(i = 0; i < total; i++){	
+		ret = gpio_register_get_value(test_gpio[i]);
+		printf("GPIO%d default value? %d\n", test_gpio[i], ret);
+		//set high
+		gpio_register_set_value(test_gpio[i], 1);
+		ret = gpio_register_get_value(test_gpio[i]);
+		printf("GPIO%d set high? %d\n", test_gpio[i], ret);
+		//get 
+		ret = gpio_register_get_value(22);
+		if(1 != ret){
+            printf("gpio [%d] test high failed\n", test_gpio[i]);
+            //fail_io[n] = test_gpio[i];
+            n++;
+        }else{
+            printf("gpio[%d] test high success\n", test_gpio[i]);
+        }
+
+		//set low
+	    gpio_register_set_value(test_gpio[i], 0);
+		ret = gpio_register_get_value(test_gpio[i]);
+		printf("GPIO%d set low? %d\n", test_gpio[i], ret);
+		//get 
+		ret = gpio_register_get_value(22);
+		if(0 != ret){
+            printf("gpio [%d] test low failed\n", test_gpio[i]);
+            //fail_io[n] = test_gpio[i];
+            n++;
+        }else{
+            printf("gpio [%d] test low success\n", test_gpio[i]);
+        }
+	}
+	return n;
+}
+		
+	
+
 
 int mbtk_at_gpio(void* arg)
 {
-    return -1;
+	int i, j, ret, total, n = 0;
+	int *fail_io = (int *)arg;
+
+	gpio_test_init();
+	n = gpio_test();
+
+	return n;
 }
 
+
+
 #elif defined(MBTK_PROJECT_L509)
+mbtk_gpio_test_pin_paired  test_pin_array[] ={  //是GPIO_array
+//120 5 33 这几个输入脚没法正常设置为输入
+    //{52,51,0},  //拉不低    没有这一组
+    {54,53,0},
+    {16,17,0},
+    {117,120,0},  
+    {118,19,0},
+    {123,126,0},
+    {43,23,0},
+    {28,27,0},
+    {26,25,0},
+    {34,33,0},    
+    {36,35,0},
+    {49,50,0},
+    {32,31,0},
+    {7,2,0},
+    {6,1,0},      
+    {15,3,0},      
+    {0,18,0},      
+    {4,5,0},        
+    {13,14,0},
+    {48,55,0},
+    {56,57,0},
+    {59,58,0},
+    {21,99,0},    
+    //{20,22,0},  //拉不低
+    //{12,122,0},  //拉不低    122好像不该用
+};
+//测低
+uint16 mbtk_gpio_test_all_low(mbtk_gpio_test_pin_paired *pin_array, uint16 MAX_pin_num)
+{
+    uint16 index =0;
+    uint16 OUT_pin, Read_pin;
+    uint32 read_high_value;
+    uint32 read_low_value;
+    uint16 fail_num=0;
+    int res;
+
+    printf("[test_low_begin]\n");
+
+   
+    for(index = 0; index < MAX_pin_num; index++){
+       OUT_pin = pin_array[index].output_pin;
+       Read_pin = pin_array[index].input_pin;
+    
+       //输出低
+       gpio_register_set_direction(OUT_pin,1);
+       gpio_register_set_value(OUT_pin, 0);
+
+       //输入脚设为输入
+       gpio_register_set_direction(Read_pin,0);
+   }
+    
+    for(index = 0; index < MAX_pin_num; index++){
+        OUT_pin = pin_array[index].output_pin;
+        Read_pin = pin_array[index].input_pin;
+        read_low_value = gpio_register_get_value(Read_pin);
+        //结果检测
+        if(read_low_value != 0){
+           printf("[!Low Failed!]: GPIO%d \n", Read_pin);
+           pin_array[index].mbtk_gpio_test_result = 1;
+           fail_num ++;
+        }
+        else{
+            printf("[-Low Success-]: GPIO%d \n", Read_pin);
+        }
+    }
+    return fail_num;
+
+}
+
+
+
+//测高
+uint16 mbtk_gpio_test_pin_high(mbtk_gpio_test_pin_paired *pin_array,uint16 MAX_pin_num)
+{
+    uint16 index =0;
+    uint16 OUT_pin, Read_pin;
+    uint32 read_high_value;
+    uint32 read_low_value;
+    uint16 fail_num=0;
+    int i = 0;
+
+    printf("[test_high_begin]\n");
+
+    for(index = 0;index<MAX_pin_num;index++)
+    {
+        OUT_pin = pin_array[index].output_pin;
+        Read_pin = pin_array[index].input_pin;
+
+        //设高
+        gpio_register_set_direction(OUT_pin,1);
+        gpio_register_set_direction(Read_pin,0);
+        gpio_register_set_value(OUT_pin, 1);
+        usleep(20000);
+        read_high_value = gpio_register_get_value(Read_pin);
+        printf("[test_high]: Gpio%d: get value =%d \n", Read_pin, read_high_value);
+
+
+        //设高后重新设低
+        usleep(20000);
+        gpio_register_set_value(OUT_pin, 0);
+        usleep(80000);
+        //read_low_value = gpio_register_get_value(Read_pin);
+        //read_low_value = gpio_register_get_value(Read_pin);
+        for(i = 0;i < 10; i++){
+            read_low_value = gpio_register_get_value(Read_pin);
+            if(0 == read_low_value){
+                break;
+            }
+            usleep(20000);
+        }
+        
+        printf("[test_low]: Gpio%d: get value =%d  \n", Read_pin, read_low_value);
+
+       if(read_high_value != 1||read_low_value !=0)
+        {
+               printf("[!High Failed!]: Gpio%d\n",Read_pin);
+               pin_array[index].mbtk_gpio_test_result = 2;
+               fail_num ++;
+        }
+        else{
+            printf("[-High Success-]: Gpio%d \n", Read_pin);
+        }
+    }
+ return fail_num;
+
+}
+void gpio_test_init_test_gpio_mode(mbtk_gpio_test_pin_paired pin_array[],int MAX_pin_num)
+{
+       int  index =0;
+       int OUT_pin, Read_pin;
+
+	   printf("[init] L509 GPIOTEST v1 \n");
+       for(index = 0;index<MAX_pin_num;index++)
+        {
+            OUT_pin = pin_array[index].output_pin;
+            Read_pin = pin_array[index].input_pin;
+            gpio_register_set_func_0(OUT_pin);
+            gpio_register_set_func_0(Read_pin);
+        }
+        
+        //gpio_register_set_func_0();
+        system("echo 33 > /sys/class/gpio/export");
+        system("echo in > /sys/class/gpio/gpio120/direction");
+        system("echo in > /sys/class/gpio/gpio5/direction");
+        system("echo in > /sys/class/gpio/gpio33/direction");
+        printf("[3init]gpio_test_init_test_gpio_mode FINISH\n");
+
+}
+
 
 int mbtk_at_gpio(void* arg)
 {
-    return -1;
+    int i;
+    int *fail_io = (int *)arg;
+     /*OPEN SWITCH */
+    uint16 test_fail_count = 0;
+    uint16 test_MAX_pin_num = 0;
+    char buf[STR_MAX_LEN];
+    uint16 str_len=0;
+    uint16 temp_len =0;
+    uint16 Out_pin;
+    uint16 in_pin;
+    uint16 fail_print_num = 0;
+
+    gpio_register_set_func_0(125);
+    gpio_register_set_direction(125, 1);
+    gpio_register_set_value(125, 1);			//开关使能脚
+#if 0
+	/* [2]设置高低电压范围 */
+	 if(th->threshold_high == 0 && th->threshold_low == 0 &&th->threshold_high_low_diff== 0)
+	 {  //use default threshold
+		    th->threshold_high          = MBTK_GPIOTEST_THRESHOLD_DEFAULT_HIGH;
+		    th->threshold_low           = MBTK_GPIOTEST_THRESHOLD_DEFAULT_LOW;
+			th->threshold_high_low_diff = MBTK_GPIOTEST_THRESHOLD_DEFAULT_DIFF;
+	 }
+	 RTI_LOG("GPIOTEST: threshold -- %d -- %d -- %d",th->threshold_low,th->threshold_high_low_diff,th->threshold_high);
+#endif
+
+	/* [3]初始化待测GPIO */
+    test_MAX_pin_num = sizeof(test_pin_array)/sizeof(test_pin_array[0]);
+    gpio_test_init_test_gpio_mode(test_pin_array,test_MAX_pin_num);
+
+
+
+	/* [4]测试过程 */
+    test_fail_count = mbtk_gpio_test_all_low(test_pin_array,test_MAX_pin_num);
+    printf("[set_low]set ALL GPIO  low,  fail_num=%d\n",test_fail_count);
+    if(!test_fail_count) //set all pin low passed
+    {
+        printf("[set all pin low passed]\n");
+        test_fail_count = mbtk_gpio_test_pin_high(&test_pin_array,test_MAX_pin_num);
+    }
+
+    //memset(buf,0,STR_MAX_LEN);
+	
+	/* [5]测试结果检测 */
+     if(test_fail_count )
+     {
+         //printf( "GPIOTEST Fail %02d PINs:\n", test_fail_count*2);
+         sprintf(buf, "GPIOTEST Fail %02d PINs:", test_fail_count*2);
+         temp_len = strlen(buf);
+
+
+           for(i = 0; i< test_MAX_pin_num; i++)
+           {
+
+                if(test_pin_array[i].mbtk_gpio_test_result)
+                {
+
+                     Out_pin = test_pin_array[i].output_pin;
+                     in_pin = test_pin_array[i].input_pin;
+                     fail_io[i] = Out_pin;
+                     fail_io[i+1] = in_pin;
+
+                }
+
+           }
+       }
+
+	 else
+	 {
+		printf(buf,"ALL GPIO TEST PASS\r\n");
+	 }
+	 //temp_len = strlen(buf);
+	return  test_fail_count;
 }
 
-#elif defined(MBTK_PROJECT_L508_X6)
-
-int mbtk_at_gpio(void* arg)
-{
-    return -1;
-}
-
-#elif defined(MBTK_PROJECT_T108)
-
-int mbtk_at_gpio(void* arg)
-{
-    return -1;
-}
 
 #else