blob: 8b7396a0266e0ab90709cbc2d5cfe5361751b304 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001/**
2 * \file gpio-test.c
3 * \brief A Documented file.
4 *
5 * Detailed description
6 * \Author: Sniper <js.wang@mobiletek.cn>
7 * \Version: 1.0.0
8 * \Date: 2022-04-26
9 */
liubin281ac462023-07-19 14:22:54 +080010#include <stdio.h>
11#include <string.h>
12#include <sys/types.h>
13#include <sys/stat.h>
14#include <fcntl.h>
15#include <errno.h>
16#include <limits.h>
17#include <sys/wait.h>
18#include <unistd.h>
19#include <sys/mount.h>
20#include "mbtk_log.h"
21#include <sys/mman.h>
22#include <ctype.h>
23#include <cutils/properties.h>
24#include "gpio-define.h"
25
26#define gpio_log(...) if(gpio_debug)printf(__VA_ARGS__)
wangyouqiang7850b4a2023-09-18 15:47:57 +080027#define HWMAP_DEVICE "/dev/hwmap"
28#define PAGE_OFFS_BITS(pgsz) ((unsigned int)(pgsz)-1)
29#define PAGE_MASK_BITS(pgsz) (~PAGE_OFFS_BITS(pgsz))
30
31typedef enum {
32 MBTK_ADC0 = 0, /* ADC 0 */
33 MBTK_ADC1 /* ADC 1 */
34} mbtk_adc_enum;
liubin281ac462023-07-19 14:22:54 +080035
36static int gpio_debug = 0;
37
38struct gpio_register_function gpio_func_register[128] = {
39 {GPIO_FUNC_GPIO_00, 0},
40 {GPIO_FUNC_GPIO_01, 0},
41 {GPIO_FUNC_GPIO_02, 0},
42 {GPIO_FUNC_GPIO_03, 0},
43 {GPIO_FUNC_GPIO_04, 0},
44 {GPIO_FUNC_GPIO_05, 0},
45 {GPIO_FUNC_GPIO_06, 0},
46 {GPIO_FUNC_GPIO_07, 0},
47 {GPIO_FUNC_GPIO_08, 0},
48 {GPIO_FUNC_GPIO_09, 0},
49 {GPIO_FUNC_GPIO_10, 0},
50 {GPIO_FUNC_GPIO_11, 0},
51 {GPIO_FUNC_GPIO_12, 0},
52 {GPIO_FUNC_GPIO_13, 0},
53 {GPIO_FUNC_GPIO_14, 0},
54 {GPIO_FUNC_GPIO_15, 0},
55 {GPIO_FUNC_GPIO_16, 0},
56 {GPIO_FUNC_GPIO_17, 0},
57 {GPIO_FUNC_GPIO_18, 0},
58 {GPIO_FUNC_GPIO_19, 0},
59 {GPIO_FUNC_GPIO_20, 0},
60 {GPIO_FUNC_GPIO_21, 0},
61 {GPIO_FUNC_GPIO_22, 0},
62 {GPIO_FUNC_GPIO_23, 0},
63 {GPIO_FUNC_GPIO_24, 0},
64 {GPIO_FUNC_GPIO_25, 0},
65 {GPIO_FUNC_GPIO_26, 0},
66 {GPIO_FUNC_GPIO_27, 0},
67 {GPIO_FUNC_GPIO_28, 0},
68 {GPIO_FUNC_GPIO_29, 0},
69 {GPIO_FUNC_GPIO_30, 0},
70 {GPIO_FUNC_GPIO_31, 0},
71 {GPIO_FUNC_GPIO_32, 0},
72 {GPIO_FUNC_GPIO_33, 0},
73 {GPIO_FUNC_GPIO_34, 0},
74 {GPIO_FUNC_GPIO_35, 0},
75 {GPIO_FUNC_GPIO_36, 0},
76 {GPIO_FUNC_MMC1_DAT3, 5}, // GPIO_37
77 {GPIO_FUNC_MMC1_DAT2, 5}, // GPIO_38
78 {GPIO_FUNC_MMC1_DAT1, 5}, // GPIO_39
79 {GPIO_FUNC_MMC1_DAT0, 5}, // GPIO_40
80 {GPIO_FUNC_MMC1_CMD, 5}, //GPIO_41
81 {GPIO_FUNC_MMC1_CLK, 5}, //GPIO_42
82 {GPIO_FUNC_MMC1_CD , 1}, //GPIO_43
83 {0,0},{0,0},{0,0},{0,0},
84 {GPIO_FUNC_SDIO_DAT3, 1}, //GPIO_48
85 {GPIO_FUNC_GPIO_49, 0},
86 {GPIO_FUNC_GPIO_50, 0},
87 {GPIO_FUNC_GPIO_51, 0},
88 {GPIO_FUNC_GPIO_52, 0},
89 {GPIO_FUNC_GPIO_53, 0},
90 {GPIO_FUNC_GPIO_54, 0},
91 {GPIO_FUNC_SDIO_DAT2, 1}, //GPIO_55
92 {GPIO_FUNC_SDIO_DAT1, 1}, //GPIO_56
93 {GPIO_FUNC_SDIO_DAT0, 1}, //GPIO_57
94 {GPIO_FUNC_SDIO_CMD, 1}, //GPIO_58
95 {GPIO_FUNC_SDIO_CLK, 1}, //GPIO_59
96 {GPIO_FUNC_GPIO_60, 0},
97 {0,0},{0,0},{0,0},{0,0},
98 {0,0},{0,0},
99 {GPIO_FUNC_DVL_0, 1},//GPIO_67
100 {GPIO_FUNC_DVL_1, 1},//GPIO_68
101 {GPIO_FUNC_GPIO_69, 0},
102 {GPIO_FUNC_GPIO_70, 0},
103 {GPIO_FUNC_QSPI_DAT3, 1}, //GPIO_71
104 {GPIO_FUNC_QSPI_DAT2, 1}, //GPIO_72
105 {GPIO_FUNC_QSPI_DAT1, 1}, //GPIO_73
106 {GPIO_FUNC_QSPI_DAT0, 1}, //GPIO_74
107 {GPIO_FUNC_QSPI_CLK, 1}, //GPIO_75
108 {GPIO_FUNC_QSPI_CS1, 1}, //GPIO_76
109 {GPIO_FUNC_GPIO_77, 0},
110 {GPIO_FUNC_GPIO_78, 0},
111 {GPIO_FUNC_GPIO_79, 0},
112 {GPIO_FUNC_GPIO_80, 0},
113 {GPIO_FUNC_USIM_UCLK, 1},//GPIO_81
114 {GPIO_FUNC_USIM_UIO, 1},//GPIO_82
115 {GPIO_FUNC_USIM_URSTn, 1},//GPIO_83
116 {0,0},{0,0},{0,0},{0,0},
117 {0,0},{0,0},{0,0},{0,0},
118 {0,0},{0,0},{0,0},{0,0},
119 {0,0},{0,0},{0,0},
120 {GPIO_FUNC_USB_ID, 1},//GPIO_99
121 {0,0},{0,0},{0,0},{0,0},
122 {0,0},{0,0},{0,0},{0,0},
123 {0,0},{0,0},{0,0},{0,0},
124 {0,0},{0,0},{0,0},{0,0},
125 {0,0},
126 {GPIO_FUNC_PRI_TDI, 1}, //GPIO_117
127 {GPIO_FUNC_PRI_TMS, 1}, //GPIO_118
128 {GPIO_FUNC_PRI_TCK, 1}, //GPIO_119
129 {GPIO_FUNC_PRI_TDO, 1}, //GPIO_120
130 {GPIO_FUNC_QSPI_VMODE_GPIO, 1}, //GPIO_121
131 {GPIO_FUNC_VBUS_DRV, 1}, //GPIO_122
132 {GPIO_FUNC_CLK_REQ, 1}, //GPIO_123
133 {0,0},
134 {GPIO_FUNC_VCXO_REQ, 1}, //GPIO_125
135 {GPIO_FUNC_VCXO_OUT, 1}, //GPIO_126
136 {0,0},
137};
liubin281ac462023-07-19 14:22:54 +0800138
liubin281ac462023-07-19 14:22:54 +0800139
wangyouqiang7850b4a2023-09-18 15:47:57 +0800140static int mbtk_gpio_adc()
liubin281ac462023-07-19 14:22:54 +0800141{
liubin281ac462023-07-19 14:22:54 +0800142 int ret = 0;
143
wangyouqiang7850b4a2023-09-18 15:47:57 +0800144 ret = mbtk_adc_get(0);
wangyouqiang59d8dd22023-10-19 18:03:54 +0800145 //printf("ADC_value =%d\n", ret);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800146 if(ret >= 500 ){
147 ret = 1;
liubin281ac462023-07-19 14:22:54 +0800148 }
wangyouqiang7850b4a2023-09-18 15:47:57 +0800149 else if (ret>=0 && ret<=480){
150 ret = 0;
151 }
152 else{
153 ret = -1;
liubin281ac462023-07-19 14:22:54 +0800154 }
155 return ret;
156}
liubin281ac462023-07-19 14:22:54 +0800157
liubin281ac462023-07-19 14:22:54 +0800158
159static int hwacc_register(int rw, unsigned int addr, unsigned int *data)
160{
161 int fid;
162 unsigned int pagesize, len, len_aligned;
163 unsigned int addr_aligned;
164 volatile unsigned int *pa;
165 void *vpa;
166
167 len = pagesize = sysconf(_SC_PAGESIZE);
168 if((fid = open(HWMAP_DEVICE, O_RDWR)) < 0)
169 {
170 printf("Failed to open %s\n", HWMAP_DEVICE);
171 exit(-1);
172 }
173
174 // Align the length so the mapped area is page-aligned and contains the requested area
175 addr_aligned = addr & PAGE_MASK_BITS(pagesize);
176 len_aligned =((addr + len - addr_aligned) + pagesize - 1) & PAGE_MASK_BITS(pagesize);
177
178 /* Notes on flags: MAP_PRIVATE results in copy on write; MAP_SHARED allows normal write */
179 /* MAP_SHARED required O_RDWR in open above, otherwise mmap fails with errno=EACCES */
180 /* Notes on prot: PROT_WRITE allows read and write; PROT_READ allows read only */
181 /* Notes on off: an unsigned 32-bit value, should be aligned to page size according to mmap manpage */
182 if((vpa = mmap(0, len_aligned, PROT_READ|PROT_WRITE, MAP_SHARED, fid, addr_aligned)) == MAP_FAILED)
183 {
184 printf("mmap failed (%d)\n", errno);
185 }
186 else
187 {
188 pa = (volatile unsigned int *)((unsigned char *)vpa + (addr & PAGE_OFFS_BITS(pagesize)));
189 if(rw == 0)
190 {
191 *data = *pa;
192 gpio_log("Value read from 0x%.8x via MVA=0x%p is 0x%.8x\n", addr, pa, *data);
193 }
194 else if(rw == 1)
195 {
196 *pa = *data;
197 gpio_log("Value %.8x written to 0x%.8x via MVA=0x%p\n", *data, addr, pa);
198#if defined(HWACC_DEBUG)
199 {
200 unsigned int val;
201 val = *pa;
202 printf("Value read from 0x%.8x via MVA=0x%p is 0x%.8x\n", addr, pa, val);
203 }
204#endif
205 }
206 munmap(vpa, len);
207 }
208
209 close(fid);
210 return 0;
211}
212/*
213设置GPIO 模式:第一步读GPIO22默认function
214root@OpenWrt:/# hwacc r 0xd401e134
215Option = r Addr = d401e134
216Value read from 0xd401e134 via MVA=0x0xb6fc3134 is 0x0000d040 //默认GPIO功能
217Bit0~bit2值对应上面表格各function,0代表GPIO功能
218 */
219static int gpio_register_read(int reg)
220{
221 int ret = -1;
222#if 0
223 FILE * fp;
224 // "/bin/hwacc r 0xd401e134";
225 char command[36] = {0};
226 char buffer[1024];
227 int i = 0;
228
229 sprintf(command, "/bin/hwacc r 0x%x", reg);
230 fp = popen(command, "r");
231 while(1)
232 {
233 if( fgets (buffer, sizeof(buffer), fp)!=NULL ) {
234 buffer[strlen(buffer) - 1] = 0;
235 // gpio_log("out [%d]: %s\n", strlen(buffer), buffer);
236 }else{
237 break;
238 }
239 i = strstr_n(buffer, "is");
240 if(i)
241 {
242 ret = str_to_hex(&buffer[i + 2]);
243 gpio_log("read 0x%x value:%s, %x\n", reg, &buffer[i + 2], ret);
244 }
245 }
246 pclose(fp);
247#else
248 hwacc_register(0, reg, &ret);
249#endif
250 return ret;
251}
252/*
253设置输入输出状态,设置PDR寄存器GPIO22为output
254root@OpenWrt:/# hwacc w 0xD401900c 0x00c03800
255Option = w Addr = d401900c Data=00c03800
256Value 00c03800 written to 0xd401900c via MVA=0x0xb6f9f00c
257 */
258static void gpio_register_write(int reg, int value)
259{
260#if 0
261 FILE * fp;
262 // "/bin/hwacc w 0xD401900c 0x00c03800"
263 char command[36] = {0};
264 char buffer[1024];
265
266 sprintf(command, "/bin/hwacc w 0x%x 0x%x", reg, value);
267 gpio_log("command: %s\n", command);
268 fp = popen(command, "r");
269 while(1)
270 {
271 if( fgets (buffer, sizeof(buffer), fp)!=NULL ) {
272 gpio_log("%s\n", buffer);
273 }else{
274 break;
275 }
276 }
277 pclose(fp);
278
279#else
wangyouqiang4c262fa2023-10-17 17:48:03 +0800280 usleep(20);
liubin281ac462023-07-19 14:22:54 +0800281 hwacc_register(1, reg, &value);
wangyouqiang4c262fa2023-10-17 17:48:03 +0800282 usleep(20);
liubin281ac462023-07-19 14:22:54 +0800283#endif
284}
285/*
286AF SEL<p>This field is used for alternate function selection for a pin.
287It selects between the eight possible alternate functions for the pin.
288Alternate function 0 is always the reset case.
289
290<p>0x0 = Alternate function 0 (primary function at reset)
291<p>0x1 = Alternate function 1
292<p>0x2 = Alternate function 2
293<p>0x3 = Alternate function 3
294<p>0x4 = Alternate function 4
295<p>0x5 = Alternate function 5
296<p>0x6 = Alternate function 6
297<p>0x7 = Alternate function 7
298 */
299static int gpio_register_set_func_0(int port)
300{
301 int ret;
302 struct gpio_register_function *reg = NULL;
303 if(port > 128)
304 return -1;
305 reg = &gpio_func_register[port];
306 if(0 == reg->reg)
307 return -1;
308 ret = gpio_register_read(reg->reg);
309 if((ret & 0x7) != reg->func_gpio)
310 {
wangyouqiang7850b4a2023-09-18 15:47:57 +0800311 //printf("Gpio set func [%d] [0x%x]!\n", reg->func_gpio, (ret & 0xfffffff8) | reg->func_gpio);
liubin281ac462023-07-19 14:22:54 +0800312 gpio_register_write(reg->reg, (ret & 0xfffffff8) | reg->func_gpio);
313 }
314 return 0;
315}
316/*
317设置GPIO 方向
318读取输入输出状态,读PDR寄存器:0x0c
319root@OpenWrt:/# hwacc r 0xD401900c
320Option = r Addr = d401900c
321Value read from 0xd401900c via MVA=0x0xb6f3900c is 0x00803800 //bit22为0,代表Input
322 */
323static void gpio_register_set_direction(int port, int dir)
324{
325 int ret;
326 int reg = 0xD4019000;
327
328 if(port > (32 - 1))
329 reg = 0xD4019004;
330 if(port > (32 * 2 - 1))
331 reg = 0xD4019008;
332 if(port > (32 * 3 - 1))
333 reg = 0xD4019100;
334
335 reg += 0x0c;
336 port = port % 0x20;
337 ret = gpio_register_read(reg);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800338 //printf("[Direction] reg_value=%x\n", ret);
liubin281ac462023-07-19 14:22:54 +0800339 // 设置 输出 0 && 1
340 if(!(ret & (0x1 << port)) && dir)
341 {
342 gpio_register_write(reg, ret | (0x1 << port));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800343 //printf("[Direction out] reg_value=%x\n", gpio_register_read(reg));
liubin281ac462023-07-19 14:22:54 +0800344 }
345 // 设置 输入 1 && 0
346 if((ret & (0x1 << port)) && !dir)
347 {
348 gpio_register_write(reg, ret | !(0x1 << port));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800349 //printf("[Direction in] reg_value=%x\n", gpio_register_read(reg));
liubin281ac462023-07-19 14:22:54 +0800350 }
351}
352/*
353设置GPIO 输出电平
354
355读取电平状态,先读PLR寄存器:0x00
356root@OpenWrt:/# hwacc r 0xD4019000
357Option = r Addr = d4019000
358Value read from 0xd4019000 via MVA=0x0xb6f1c000 is 0x81e82a30
359对应下面具体BIT
3601000 0001 1110 1000 0010 1010 0011 0000 BIT22默认电平高
361
362设置输出高:设置PSR寄存器:0x18(只写寄存器)
363root@OpenWrt:/# hwacc w 0xD4019018 0x400000
364Option = w Addr = d4019018 Data=00400000
365Value 00400000 written to 0xd4019018 via MVA=0x0xb6f56018 //bit22写1,输出高
366
367设置输出低:设置PCR寄存器:0x24
368root@OpenWrt:/# hwacc w 0xD4019024 0x400000
369Option = w Addr = d4019024 Data=00400000
370Value 00400000 written to 0xd4019024 via MVA=0x0xb6faa024 //Bit22写1,GPIO22输出低
371
372 */
373static void gpio_register_set_value(int port, int value)
374{
375 int ret;
376 int reg = 0xD4019000;
377
378 if(port > (32 - 1))
379 reg = 0xD4019004;
380 if(port > (32 * 2 - 1))
381 reg = 0xD4019008;
382 if(port > (32 * 3 - 1))
383 reg = 0xD4019100;
384
385 if(value)
386 {
387 reg += 0x18;
388 }
389 else
390 reg += 0x24;
391
392 port = port % 0x20;
393 ret = gpio_register_read(reg);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800394 //printf("[Value] reg_value=%x\n", gpio_register_read(0xD4019004));
liubin281ac462023-07-19 14:22:54 +0800395 // 设置 高电平 0 && 1
396 if(value)
397 {
398 gpio_register_write(reg, ret | (0x1 << port));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800399 //printf("[Value high] reg_value=%x\n", gpio_register_read(0xD4019004));
liubin281ac462023-07-19 14:22:54 +0800400 return;
401 }
402 // 设置 低电平 1 && 0
403 if(!(ret & (0x1 << port)) && !value)
404 {
405 gpio_register_write(reg, ret | (0x1 << port));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800406 //printf("[Value low] reg_value=%x\n", gpio_register_read(0xD4019004));
liubin281ac462023-07-19 14:22:54 +0800407 }
408}
409/*
410读取电平状态,先读PLR寄存器:0x00
411root@OpenWrt:/# hwacc r 0xD4019000
412Option = r Addr = d4019000
413Value read from 0xd4019000 via MVA=0x0xb6f1c000 is 0x81e82a30
414对应下面具体BIT
4151000 0001 1110 1000 0010 1010 0011 0000 BIT22默认电平高
416 */
417static int gpio_register_get_value(int port)
418{
419 int ret = -1;
420 int reg = 0xD4019000;
421
422 if(port > (32 - 1))
423 reg = 0xD4019004;
424 if(port > (32 * 2 - 1))
425 reg = 0xD4019008;
426 if(port > (32 * 3 - 1))
427 reg = 0xD4019100;
428 port = port % 0x20;
429 ret = gpio_register_read(reg);
430 if(ret & (0x1 << port))
431 {
432 return 1;
433 }
434 return 0;
435}
436
437void gpio_debug_set(int enable)
438{
439 gpio_debug = enable;
440}
441
442int gpio_register_test_out(int port, int value)
443{
444 int ret;
wangyouqiang7850b4a2023-09-18 15:47:57 +0800445 int i;
446 int valueh = 0;
447 int valuel = 1;
448
wangyouqiang4c262fa2023-10-17 17:48:03 +0800449 //printf("Gpio port [%d] test start!\n", port);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800450 ret = gpio_register_set_func_0(port); //设功能为GPIO
451 if(ret){
liubin281ac462023-07-19 14:22:54 +0800452 printf("gpio_port can't support!\n");
453 return -1;
454 }
wangyouqiang7850b4a2023-09-18 15:47:57 +0800455 gpio_register_set_direction(port, 1); //设方向为输出
liubin281ac462023-07-19 14:22:54 +0800456 ret = gpio_register_get_value(port);
wangyouqiang4c262fa2023-10-17 17:48:03 +0800457 //printf("gpio default value is : %d.\n", ret);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800458
459
460 //[High]
461 for(i = 0; i <= 10; i++){
462 gpio_register_set_value(port, 1);
463 usleep(50);
464 valueh = gpio_register_get_value(port);
wangyouqiang4c262fa2023-10-17 17:48:03 +0800465 //printf("set high? %d\n",valueh);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800466 if(1 == valueh){
467 break;
468 }
469 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800470 //usleep(10000);
471 for(i = 0; i <= 35; i++){
wangyouqiang7850b4a2023-09-18 15:47:57 +0800472 ret = mbtk_gpio_adc();
wangyouqiang59d8dd22023-10-19 18:03:54 +0800473 usleep(3000);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800474 if(1 == ret){
475 break;
476 }
477 }
wangyouqiang4c262fa2023-10-17 17:48:03 +0800478 //printf("------gpio should is high: %d.------\n", ret);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800479 if(1 != ret){
480 ret=-1;
liubin281ac462023-07-19 14:22:54 +0800481 goto exit;
wangyouqiang7850b4a2023-09-18 15:47:57 +0800482 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800483 printf("******gpio should is high: %d.******\n", ret);
liubin281ac462023-07-19 14:22:54 +0800484
wangyouqiang7850b4a2023-09-18 15:47:57 +0800485
486 //[Low]
487 usleep(200);
488 for(i = 0; i <= 10; i++){
489 gpio_register_set_value(port, 0); //输出低
b.liu074771a2023-10-19 13:13:39 +0800490 usleep(50);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800491 valuel = gpio_register_get_value(port);
wangyouqiang59d8dd22023-10-19 18:03:54 +0800492 //printf("set low? %d\n",valuel);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800493 if(0 == valuel){
494 break;
495 }
496 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800497 //usleep(10000);
498 for(i = 0; i <= 35; i++){
wangyouqiang7850b4a2023-09-18 15:47:57 +0800499 ret = mbtk_gpio_adc();
wangyouqiang59d8dd22023-10-19 18:03:54 +0800500 usleep(3000);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800501 if(0 == ret){
502 break;
503 }
504 }
b.liu074771a2023-10-19 13:13:39 +0800505 printf("******gpio should is low: %d.******\n", ret);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800506 if(0 != ret){
507 ret=-1;
liubin281ac462023-07-19 14:22:54 +0800508 goto exit;
509 }
510
511exit:
wangyouqiang7850b4a2023-09-18 15:47:57 +0800512 //gpio_register_set_direction(port, 0); //设方向为输入
liubin281ac462023-07-19 14:22:54 +0800513 return ret;
514}
515
516int mbtk_at_gpio(void* arg)
517{
wangyouqiang7850b4a2023-09-18 15:47:57 +0800518 /*
519 error:53
520 */
521 int test_gpio[] =
522 {
523 56, 55, 58, 57, 48, 59, 12, 20, 5, 43, 21,
524 49, 50, 27, 28, 26, 25, 16, 17, 15, 126, 125,
525 4, 0, 3, 18, 7, 6, 1, 2, 54, 19, 13,
526 32, 31, 23, 24, 22, 122, 33, 35, 36, 34, 14,
527 99,/*53,*/37,38,39,40,41,42};
liubin281ac462023-07-19 14:22:54 +0800528
wangyouqiang7850b4a2023-09-18 15:47:57 +0800529 int i, j, ret, total, n = 0;
liubin281ac462023-07-19 14:22:54 +0800530 int *fail_io = (int *)arg;
531 total = (sizeof(test_gpio)/sizeof(int));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800532 //printf("Start test gpio total: %d\n", total);
533
534 /* [1 all gpio set to low] */
535 for(j = 0; j < 6 ; j++){
536 for(i = 0; i < total; i++){
537 gpio_register_set_func_0(test_gpio[i]);
538 gpio_register_set_direction(test_gpio[i], 1);
539 gpio_register_set_value(test_gpio[i], 0);
540 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800541 //usleep(10000);
542 for(i = 0; i <= 35; i++){
b.liu074771a2023-10-19 13:13:39 +0800543 ret = mbtk_gpio_adc();
wangyouqiang59d8dd22023-10-19 18:03:54 +0800544 usleep(3000);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800545 if(0 == ret){
546 break;
547 }
548 }
b.liu074771a2023-10-19 13:13:39 +0800549 printf("pre set ADC: %d, times: %d\n",ret, j);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800550 if(0 == ret){
551 break;
552 }
553 }
wangyouqiang4c262fa2023-10-17 17:48:03 +0800554 if(0 != ret){
555 printf("Fail, please retest");
556 return -1;
557 }
558
wangyouqiang7850b4a2023-09-18 15:47:57 +0800559
560 /* [2 GPIOTEST] */
561 for(i = 0; i < total; i++){
liubin281ac462023-07-19 14:22:54 +0800562 ret = gpio_register_test_out(test_gpio[i], 0);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800563 if(-1 == ret){
564 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio[i]);
liubin281ac462023-07-19 14:22:54 +0800565 fail_io[n] = test_gpio[i];
566 n++;
567 }else{
wangyouqiang4c262fa2023-10-17 17:48:03 +0800568 //printf("############gpio [%d] test success############\n", test_gpio[i]);
liubin281ac462023-07-19 14:22:54 +0800569 }
570 }
wangyouqiang2b352b22023-10-21 15:18:43 +0800571 mbtk_adc_close();
liubin281ac462023-07-19 14:22:54 +0800572 return n;
573}