blob: bd641e100adf846212ec8bfaadea41659824c75d [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>
b.liu85565a72023-11-09 16:47:40 +080024#include "mbtk_gpio_def.h"
liubin281ac462023-07-19 14:22:54 +080025#include "gpio-define.h"
26
27#define gpio_log(...) if(gpio_debug)printf(__VA_ARGS__)
wangyouqiang7850b4a2023-09-18 15:47:57 +080028#define HWMAP_DEVICE "/dev/hwmap"
29#define PAGE_OFFS_BITS(pgsz) ((unsigned int)(pgsz)-1)
30#define PAGE_MASK_BITS(pgsz) (~PAGE_OFFS_BITS(pgsz))
b.liu85565a72023-11-09 16:47:40 +080031#define STR_MAX_LEN 220
wangyouqiang7850b4a2023-09-18 15:47:57 +080032typedef enum {
33 MBTK_ADC0 = 0, /* ADC 0 */
34 MBTK_ADC1 /* ADC 1 */
35} mbtk_adc_enum;
liubin281ac462023-07-19 14:22:54 +080036
37static int gpio_debug = 0;
38
39struct gpio_register_function gpio_func_register[128] = {
40 {GPIO_FUNC_GPIO_00, 0},
41 {GPIO_FUNC_GPIO_01, 0},
42 {GPIO_FUNC_GPIO_02, 0},
43 {GPIO_FUNC_GPIO_03, 0},
44 {GPIO_FUNC_GPIO_04, 0},
45 {GPIO_FUNC_GPIO_05, 0},
46 {GPIO_FUNC_GPIO_06, 0},
47 {GPIO_FUNC_GPIO_07, 0},
48 {GPIO_FUNC_GPIO_08, 0},
49 {GPIO_FUNC_GPIO_09, 0},
50 {GPIO_FUNC_GPIO_10, 0},
51 {GPIO_FUNC_GPIO_11, 0},
52 {GPIO_FUNC_GPIO_12, 0},
53 {GPIO_FUNC_GPIO_13, 0},
54 {GPIO_FUNC_GPIO_14, 0},
55 {GPIO_FUNC_GPIO_15, 0},
56 {GPIO_FUNC_GPIO_16, 0},
57 {GPIO_FUNC_GPIO_17, 0},
58 {GPIO_FUNC_GPIO_18, 0},
59 {GPIO_FUNC_GPIO_19, 0},
60 {GPIO_FUNC_GPIO_20, 0},
61 {GPIO_FUNC_GPIO_21, 0},
62 {GPIO_FUNC_GPIO_22, 0},
63 {GPIO_FUNC_GPIO_23, 0},
64 {GPIO_FUNC_GPIO_24, 0},
65 {GPIO_FUNC_GPIO_25, 0},
66 {GPIO_FUNC_GPIO_26, 0},
67 {GPIO_FUNC_GPIO_27, 0},
68 {GPIO_FUNC_GPIO_28, 0},
69 {GPIO_FUNC_GPIO_29, 0},
70 {GPIO_FUNC_GPIO_30, 0},
71 {GPIO_FUNC_GPIO_31, 0},
72 {GPIO_FUNC_GPIO_32, 0},
73 {GPIO_FUNC_GPIO_33, 0},
74 {GPIO_FUNC_GPIO_34, 0},
75 {GPIO_FUNC_GPIO_35, 0},
76 {GPIO_FUNC_GPIO_36, 0},
77 {GPIO_FUNC_MMC1_DAT3, 5}, // GPIO_37
78 {GPIO_FUNC_MMC1_DAT2, 5}, // GPIO_38
79 {GPIO_FUNC_MMC1_DAT1, 5}, // GPIO_39
80 {GPIO_FUNC_MMC1_DAT0, 5}, // GPIO_40
81 {GPIO_FUNC_MMC1_CMD, 5}, //GPIO_41
82 {GPIO_FUNC_MMC1_CLK, 5}, //GPIO_42
83 {GPIO_FUNC_MMC1_CD , 1}, //GPIO_43
84 {0,0},{0,0},{0,0},{0,0},
85 {GPIO_FUNC_SDIO_DAT3, 1}, //GPIO_48
86 {GPIO_FUNC_GPIO_49, 0},
87 {GPIO_FUNC_GPIO_50, 0},
88 {GPIO_FUNC_GPIO_51, 0},
89 {GPIO_FUNC_GPIO_52, 0},
90 {GPIO_FUNC_GPIO_53, 0},
91 {GPIO_FUNC_GPIO_54, 0},
92 {GPIO_FUNC_SDIO_DAT2, 1}, //GPIO_55
93 {GPIO_FUNC_SDIO_DAT1, 1}, //GPIO_56
94 {GPIO_FUNC_SDIO_DAT0, 1}, //GPIO_57
95 {GPIO_FUNC_SDIO_CMD, 1}, //GPIO_58
96 {GPIO_FUNC_SDIO_CLK, 1}, //GPIO_59
97 {GPIO_FUNC_GPIO_60, 0},
98 {0,0},{0,0},{0,0},{0,0},
99 {0,0},{0,0},
100 {GPIO_FUNC_DVL_0, 1},//GPIO_67
101 {GPIO_FUNC_DVL_1, 1},//GPIO_68
102 {GPIO_FUNC_GPIO_69, 0},
103 {GPIO_FUNC_GPIO_70, 0},
104 {GPIO_FUNC_QSPI_DAT3, 1}, //GPIO_71
105 {GPIO_FUNC_QSPI_DAT2, 1}, //GPIO_72
106 {GPIO_FUNC_QSPI_DAT1, 1}, //GPIO_73
107 {GPIO_FUNC_QSPI_DAT0, 1}, //GPIO_74
108 {GPIO_FUNC_QSPI_CLK, 1}, //GPIO_75
109 {GPIO_FUNC_QSPI_CS1, 1}, //GPIO_76
110 {GPIO_FUNC_GPIO_77, 0},
111 {GPIO_FUNC_GPIO_78, 0},
112 {GPIO_FUNC_GPIO_79, 0},
113 {GPIO_FUNC_GPIO_80, 0},
114 {GPIO_FUNC_USIM_UCLK, 1},//GPIO_81
115 {GPIO_FUNC_USIM_UIO, 1},//GPIO_82
116 {GPIO_FUNC_USIM_URSTn, 1},//GPIO_83
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},{0,0},
120 {0,0},{0,0},{0,0},
121 {GPIO_FUNC_USB_ID, 1},//GPIO_99
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},{0,0},{0,0},{0,0},
126 {0,0},
127 {GPIO_FUNC_PRI_TDI, 1}, //GPIO_117
128 {GPIO_FUNC_PRI_TMS, 1}, //GPIO_118
129 {GPIO_FUNC_PRI_TCK, 1}, //GPIO_119
130 {GPIO_FUNC_PRI_TDO, 1}, //GPIO_120
131 {GPIO_FUNC_QSPI_VMODE_GPIO, 1}, //GPIO_121
132 {GPIO_FUNC_VBUS_DRV, 1}, //GPIO_122
133 {GPIO_FUNC_CLK_REQ, 1}, //GPIO_123
134 {0,0},
135 {GPIO_FUNC_VCXO_REQ, 1}, //GPIO_125
136 {GPIO_FUNC_VCXO_OUT, 1}, //GPIO_126
137 {0,0},
138};
liubin281ac462023-07-19 14:22:54 +0800139
liubin281ac462023-07-19 14:22:54 +0800140
wangyouqiang7850b4a2023-09-18 15:47:57 +0800141static int mbtk_gpio_adc()
liubin281ac462023-07-19 14:22:54 +0800142{
liubin281ac462023-07-19 14:22:54 +0800143 int ret = 0;
144
wangyouqiang7850b4a2023-09-18 15:47:57 +0800145 ret = mbtk_adc_get(0);
wangyouqiang59d8dd22023-10-19 18:03:54 +0800146 //printf("ADC_value =%d\n", ret);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800147 if(ret >= 500 ){
148 ret = 1;
liubin281ac462023-07-19 14:22:54 +0800149 }
wangyouqiang7850b4a2023-09-18 15:47:57 +0800150 else if (ret>=0 && ret<=480){
151 ret = 0;
152 }
153 else{
154 ret = -1;
liubin281ac462023-07-19 14:22:54 +0800155 }
156 return ret;
157}
liubin281ac462023-07-19 14:22:54 +0800158
liubin281ac462023-07-19 14:22:54 +0800159
160static int hwacc_register(int rw, unsigned int addr, unsigned int *data)
161{
162 int fid;
163 unsigned int pagesize, len, len_aligned;
164 unsigned int addr_aligned;
165 volatile unsigned int *pa;
166 void *vpa;
167
168 len = pagesize = sysconf(_SC_PAGESIZE);
169 if((fid = open(HWMAP_DEVICE, O_RDWR)) < 0)
170 {
171 printf("Failed to open %s\n", HWMAP_DEVICE);
172 exit(-1);
173 }
174
175 // Align the length so the mapped area is page-aligned and contains the requested area
176 addr_aligned = addr & PAGE_MASK_BITS(pagesize);
177 len_aligned =((addr + len - addr_aligned) + pagesize - 1) & PAGE_MASK_BITS(pagesize);
178
179 /* Notes on flags: MAP_PRIVATE results in copy on write; MAP_SHARED allows normal write */
180 /* MAP_SHARED required O_RDWR in open above, otherwise mmap fails with errno=EACCES */
181 /* Notes on prot: PROT_WRITE allows read and write; PROT_READ allows read only */
182 /* Notes on off: an unsigned 32-bit value, should be aligned to page size according to mmap manpage */
183 if((vpa = mmap(0, len_aligned, PROT_READ|PROT_WRITE, MAP_SHARED, fid, addr_aligned)) == MAP_FAILED)
184 {
185 printf("mmap failed (%d)\n", errno);
186 }
187 else
188 {
189 pa = (volatile unsigned int *)((unsigned char *)vpa + (addr & PAGE_OFFS_BITS(pagesize)));
190 if(rw == 0)
191 {
192 *data = *pa;
193 gpio_log("Value read from 0x%.8x via MVA=0x%p is 0x%.8x\n", addr, pa, *data);
194 }
195 else if(rw == 1)
196 {
197 *pa = *data;
198 gpio_log("Value %.8x written to 0x%.8x via MVA=0x%p\n", *data, addr, pa);
199#if defined(HWACC_DEBUG)
200 {
201 unsigned int val;
202 val = *pa;
203 printf("Value read from 0x%.8x via MVA=0x%p is 0x%.8x\n", addr, pa, val);
204 }
205#endif
206 }
207 munmap(vpa, len);
208 }
209
210 close(fid);
211 return 0;
212}
213/*
214设置GPIO 模式:第一步读GPIO22默认function
215root@OpenWrt:/# hwacc r 0xd401e134
216Option = r Addr = d401e134
217Value read from 0xd401e134 via MVA=0x0xb6fc3134 is 0x0000d040 //默认GPIO功能
218Bit0~bit2值对应上面表格各function,0代表GPIO功能
219 */
220static int gpio_register_read(int reg)
221{
222 int ret = -1;
223#if 0
224 FILE * fp;
225 // "/bin/hwacc r 0xd401e134";
226 char command[36] = {0};
227 char buffer[1024];
228 int i = 0;
229
230 sprintf(command, "/bin/hwacc r 0x%x", reg);
231 fp = popen(command, "r");
232 while(1)
233 {
234 if( fgets (buffer, sizeof(buffer), fp)!=NULL ) {
235 buffer[strlen(buffer) - 1] = 0;
236 // gpio_log("out [%d]: %s\n", strlen(buffer), buffer);
237 }else{
238 break;
239 }
240 i = strstr_n(buffer, "is");
241 if(i)
242 {
243 ret = str_to_hex(&buffer[i + 2]);
244 gpio_log("read 0x%x value:%s, %x\n", reg, &buffer[i + 2], ret);
245 }
246 }
247 pclose(fp);
248#else
b.liu85565a72023-11-09 16:47:40 +0800249#ifndef MBTK_PROJECT_PN1803
250 usleep(50);
251#endif
liubin281ac462023-07-19 14:22:54 +0800252 hwacc_register(0, reg, &ret);
b.liu85565a72023-11-09 16:47:40 +0800253#ifndef MBTK_PROJECT_PN1803
254 usleep(50);
255#endif
liubin281ac462023-07-19 14:22:54 +0800256#endif
257 return ret;
258}
259/*
260设置输入输出状态,设置PDR寄存器GPIO22为output
261root@OpenWrt:/# hwacc w 0xD401900c 0x00c03800
262Option = w Addr = d401900c Data=00c03800
263Value 00c03800 written to 0xd401900c via MVA=0x0xb6f9f00c
264 */
265static void gpio_register_write(int reg, int value)
266{
267#if 0
268 FILE * fp;
269 // "/bin/hwacc w 0xD401900c 0x00c03800"
270 char command[36] = {0};
271 char buffer[1024];
272
273 sprintf(command, "/bin/hwacc w 0x%x 0x%x", reg, value);
274 gpio_log("command: %s\n", command);
275 fp = popen(command, "r");
276 while(1)
277 {
278 if( fgets (buffer, sizeof(buffer), fp)!=NULL ) {
279 gpio_log("%s\n", buffer);
280 }else{
281 break;
282 }
283 }
284 pclose(fp);
285
286#else
b.liu85565a72023-11-09 16:47:40 +0800287#ifndef MBTK_PROJECT_PN1803
288 usleep(50);
289#endif
liubin281ac462023-07-19 14:22:54 +0800290 hwacc_register(1, reg, &value);
b.liu85565a72023-11-09 16:47:40 +0800291#ifndef MBTK_PROJECT_PN1803
292 usleep(50);
293#endif
liubin281ac462023-07-19 14:22:54 +0800294#endif
295}
296/*
297AF SEL<p>This field is used for alternate function selection for a pin.
298It selects between the eight possible alternate functions for the pin.
299Alternate function 0 is always the reset case.
300
301<p>0x0 = Alternate function 0 (primary function at reset)
302<p>0x1 = Alternate function 1
303<p>0x2 = Alternate function 2
304<p>0x3 = Alternate function 3
305<p>0x4 = Alternate function 4
306<p>0x5 = Alternate function 5
307<p>0x6 = Alternate function 6
308<p>0x7 = Alternate function 7
309 */
310static int gpio_register_set_func_0(int port)
311{
312 int ret;
313 struct gpio_register_function *reg = NULL;
314 if(port > 128)
315 return -1;
316 reg = &gpio_func_register[port];
317 if(0 == reg->reg)
318 return -1;
319 ret = gpio_register_read(reg->reg);
320 if((ret & 0x7) != reg->func_gpio)
321 {
wangyouqiang7850b4a2023-09-18 15:47:57 +0800322 //printf("Gpio set func [%d] [0x%x]!\n", reg->func_gpio, (ret & 0xfffffff8) | reg->func_gpio);
liubin281ac462023-07-19 14:22:54 +0800323 gpio_register_write(reg->reg, (ret & 0xfffffff8) | reg->func_gpio);
324 }
325 return 0;
326}
327/*
328设置GPIO 方向
329读取输入输出状态,读PDR寄存器:0x0c
330root@OpenWrt:/# hwacc r 0xD401900c
331Option = r Addr = d401900c
332Value read from 0xd401900c via MVA=0x0xb6f3900c is 0x00803800 //bit22为0,代表Input
333 */
334static void gpio_register_set_direction(int port, int dir)
335{
336 int ret;
337 int reg = 0xD4019000;
338
339 if(port > (32 - 1))
340 reg = 0xD4019004;
341 if(port > (32 * 2 - 1))
342 reg = 0xD4019008;
343 if(port > (32 * 3 - 1))
344 reg = 0xD4019100;
345
346 reg += 0x0c;
347 port = port % 0x20;
348 ret = gpio_register_read(reg);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800349 //printf("[Direction] reg_value=%x\n", ret);
liubin281ac462023-07-19 14:22:54 +0800350 // 设置 输出 0 && 1
351 if(!(ret & (0x1 << port)) && dir)
352 {
353 gpio_register_write(reg, ret | (0x1 << port));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800354 //printf("[Direction out] reg_value=%x\n", gpio_register_read(reg));
liubin281ac462023-07-19 14:22:54 +0800355 }
356 // 设置 输入 1 && 0
357 if((ret & (0x1 << port)) && !dir)
358 {
359 gpio_register_write(reg, ret | !(0x1 << port));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800360 //printf("[Direction in] reg_value=%x\n", gpio_register_read(reg));
liubin281ac462023-07-19 14:22:54 +0800361 }
362}
363/*
364设置GPIO 输出电平
365
366读取电平状态,先读PLR寄存器:0x00
367root@OpenWrt:/# hwacc r 0xD4019000
368Option = r Addr = d4019000
369Value read from 0xd4019000 via MVA=0x0xb6f1c000 is 0x81e82a30
370对应下面具体BIT
3711000 0001 1110 1000 0010 1010 0011 0000 BIT22默认电平高
372
373设置输出高:设置PSR寄存器:0x18(只写寄存器)
374root@OpenWrt:/# hwacc w 0xD4019018 0x400000
375Option = w Addr = d4019018 Data=00400000
376Value 00400000 written to 0xd4019018 via MVA=0x0xb6f56018 //bit22写1,输出高
377
378设置输出低:设置PCR寄存器:0x24
379root@OpenWrt:/# hwacc w 0xD4019024 0x400000
380Option = w Addr = d4019024 Data=00400000
381Value 00400000 written to 0xd4019024 via MVA=0x0xb6faa024 //Bit22写1,GPIO22输出低
382
383 */
384static void gpio_register_set_value(int port, int value)
385{
386 int ret;
387 int reg = 0xD4019000;
388
389 if(port > (32 - 1))
390 reg = 0xD4019004;
391 if(port > (32 * 2 - 1))
392 reg = 0xD4019008;
393 if(port > (32 * 3 - 1))
394 reg = 0xD4019100;
395
396 if(value)
397 {
398 reg += 0x18;
399 }
400 else
401 reg += 0x24;
402
403 port = port % 0x20;
404 ret = gpio_register_read(reg);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800405 //printf("[Value] reg_value=%x\n", gpio_register_read(0xD4019004));
liubin281ac462023-07-19 14:22:54 +0800406 // 设置 高电平 0 && 1
407 if(value)
408 {
409 gpio_register_write(reg, ret | (0x1 << port));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800410 //printf("[Value high] reg_value=%x\n", gpio_register_read(0xD4019004));
liubin281ac462023-07-19 14:22:54 +0800411 return;
412 }
413 // 设置 低电平 1 && 0
414 if(!(ret & (0x1 << port)) && !value)
415 {
416 gpio_register_write(reg, ret | (0x1 << port));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800417 //printf("[Value low] reg_value=%x\n", gpio_register_read(0xD4019004));
liubin281ac462023-07-19 14:22:54 +0800418 }
419}
420/*
421读取电平状态,先读PLR寄存器:0x00
422root@OpenWrt:/# hwacc r 0xD4019000
423Option = r Addr = d4019000
424Value read from 0xd4019000 via MVA=0x0xb6f1c000 is 0x81e82a30
425对应下面具体BIT
4261000 0001 1110 1000 0010 1010 0011 0000 BIT22默认电平高
427 */
428static int gpio_register_get_value(int port)
429{
430 int ret = -1;
431 int reg = 0xD4019000;
432
433 if(port > (32 - 1))
434 reg = 0xD4019004;
435 if(port > (32 * 2 - 1))
436 reg = 0xD4019008;
437 if(port > (32 * 3 - 1))
438 reg = 0xD4019100;
439 port = port % 0x20;
440 ret = gpio_register_read(reg);
441 if(ret & (0x1 << port))
442 {
443 return 1;
444 }
445 return 0;
446}
447
448void gpio_debug_set(int enable)
449{
450 gpio_debug = enable;
451}
452
b.liu85565a72023-11-09 16:47:40 +0800453
454
455#if defined(MBTK_PROJECT_PN1803)
liubin281ac462023-07-19 14:22:54 +0800456int gpio_register_test_out(int port, int value)
457{
458 int ret;
wangyouqiang7850b4a2023-09-18 15:47:57 +0800459 int i;
460 int valueh = 0;
461 int valuel = 1;
462
wangyouqiang4c262fa2023-10-17 17:48:03 +0800463 //printf("Gpio port [%d] test start!\n", port);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800464 ret = gpio_register_set_func_0(port); //设功能为GPIO
465 if(ret){
liubin281ac462023-07-19 14:22:54 +0800466 printf("gpio_port can't support!\n");
467 return -1;
468 }
wangyouqiang7850b4a2023-09-18 15:47:57 +0800469 gpio_register_set_direction(port, 1); //设方向为输出
liubin281ac462023-07-19 14:22:54 +0800470 ret = gpio_register_get_value(port);
wangyouqiang4c262fa2023-10-17 17:48:03 +0800471 //printf("gpio default value is : %d.\n", ret);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800472
473
474 //[High]
475 for(i = 0; i <= 10; i++){
476 gpio_register_set_value(port, 1);
477 usleep(50);
478 valueh = gpio_register_get_value(port);
wangyouqiang4c262fa2023-10-17 17:48:03 +0800479 //printf("set high? %d\n",valueh);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800480 if(1 == valueh){
481 break;
482 }
483 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800484 //usleep(10000);
485 for(i = 0; i <= 35; i++){
wangyouqiang7850b4a2023-09-18 15:47:57 +0800486 ret = mbtk_gpio_adc();
wangyouqiang59d8dd22023-10-19 18:03:54 +0800487 usleep(3000);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800488 if(1 == ret){
489 break;
490 }
491 }
wangyouqiang7850b4a2023-09-18 15:47:57 +0800492 if(1 != ret){
493 ret=-1;
liubin281ac462023-07-19 14:22:54 +0800494 goto exit;
wangyouqiang7850b4a2023-09-18 15:47:57 +0800495 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800496 printf("******gpio should is high: %d.******\n", ret);
liubin281ac462023-07-19 14:22:54 +0800497
wangyouqiang7850b4a2023-09-18 15:47:57 +0800498
499 //[Low]
500 usleep(200);
501 for(i = 0; i <= 10; i++){
502 gpio_register_set_value(port, 0); //输出低
b.liu074771a2023-10-19 13:13:39 +0800503 usleep(50);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800504 valuel = gpio_register_get_value(port);
wangyouqiang59d8dd22023-10-19 18:03:54 +0800505 //printf("set low? %d\n",valuel);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800506 if(0 == valuel){
507 break;
508 }
509 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800510 //usleep(10000);
511 for(i = 0; i <= 35; i++){
wangyouqiang7850b4a2023-09-18 15:47:57 +0800512 ret = mbtk_gpio_adc();
wangyouqiang59d8dd22023-10-19 18:03:54 +0800513 usleep(3000);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800514 if(0 == ret){
515 break;
516 }
517 }
b.liu074771a2023-10-19 13:13:39 +0800518 printf("******gpio should is low: %d.******\n", ret);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800519 if(0 != ret){
520 ret=-1;
liubin281ac462023-07-19 14:22:54 +0800521 goto exit;
522 }
523
524exit:
wangyouqiang7850b4a2023-09-18 15:47:57 +0800525 //gpio_register_set_direction(port, 0); //设方向为输入
liubin281ac462023-07-19 14:22:54 +0800526 return ret;
527}
528
529int mbtk_at_gpio(void* arg)
530{
b.liu37e72162023-11-01 15:38:18 +0800531 int test_gpio[] = {
532 56, 55, 58, 57, 48, 59, 12, 20, 5, 43, 21,
533 49, 50, 27, 28, 26, 25, 16, 17, 15, 126, 125,
b.liu810b1a32023-11-01 22:55:07 +0800534 4, 0, 3, 18, 7, 6, 1, 2, /*54,*/ 19, 13,
535 32, 31, 23, 24, 22, /*122,*/ 33, 35, 36, 34, 14,
wangyouqiangddbcf2e2023-11-14 13:59:41 +0800536 99,/* 53,*/ 37, 38, 39, 40, 41, 42};
liubin281ac462023-07-19 14:22:54 +0800537
wangyouqiang7850b4a2023-09-18 15:47:57 +0800538 int i, j, ret, total, n = 0;
liubin281ac462023-07-19 14:22:54 +0800539 int *fail_io = (int *)arg;
540 total = (sizeof(test_gpio)/sizeof(int));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800541 //printf("Start test gpio total: %d\n", total);
542
543 /* [1 all gpio set to low] */
544 for(j = 0; j < 6 ; j++){
545 for(i = 0; i < total; i++){
546 gpio_register_set_func_0(test_gpio[i]);
547 gpio_register_set_direction(test_gpio[i], 1);
548 gpio_register_set_value(test_gpio[i], 0);
549 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800550 //usleep(10000);
551 for(i = 0; i <= 35; i++){
b.liu074771a2023-10-19 13:13:39 +0800552 ret = mbtk_gpio_adc();
wangyouqiang59d8dd22023-10-19 18:03:54 +0800553 usleep(3000);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800554 if(0 == ret){
555 break;
556 }
557 }
b.liu074771a2023-10-19 13:13:39 +0800558 printf("pre set ADC: %d, times: %d\n",ret, j);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800559 if(0 == ret){
560 break;
561 }
562 }
wangyouqiang4c262fa2023-10-17 17:48:03 +0800563 if(0 != ret){
564 printf("Fail, please retest");
565 return -1;
566 }
567
wangyouqiang7850b4a2023-09-18 15:47:57 +0800568
569 /* [2 GPIOTEST] */
570 for(i = 0; i < total; i++){
liubin281ac462023-07-19 14:22:54 +0800571 ret = gpio_register_test_out(test_gpio[i], 0);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800572 if(-1 == ret){
573 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio[i]);
liubin281ac462023-07-19 14:22:54 +0800574 fail_io[n] = test_gpio[i];
575 n++;
576 }else{
wangyouqiang4c262fa2023-10-17 17:48:03 +0800577 //printf("############gpio [%d] test success############\n", test_gpio[i]);
liubin281ac462023-07-19 14:22:54 +0800578 }
579 }
wangyouqiang2b352b22023-10-21 15:18:43 +0800580 mbtk_adc_close();
liubin281ac462023-07-19 14:22:54 +0800581 return n;
582}
b.liu4f8e8792023-11-01 23:25:22 +0800583
584#elif defined(MBTK_PROJECT_L508)
b.liu85565a72023-11-09 16:47:40 +0800585int test_gpio[] =
586{
587 0,2,1,6,16,15,17,99,
588 34,36,35,33,14,49,50,12,118,120,
wangyouqiang89888612023-11-28 18:28:12 +0800589 43,19,7,3,
b.liu85565a72023-11-09 16:47:40 +0800590 20,59,126,117,125,18,56,55,58,48,57,5,4,21,
wangyouqiang89888612023-11-28 18:28:12 +0800591 23,24,13,27,28,26,25
592 //32,31
593 //,54,53,123,122
b.liu85565a72023-11-09 16:47:40 +0800594};
595int total;
596total = (sizeof(test_gpio)/sizeof(int));
597
598
599int gpio_test_init(void)
600{
601 int i,j,ret;
602 //int *fail_io = (int *)arg;
wangyouqiang89888612023-11-28 18:28:12 +0800603 printf("[init] L508 GPIOTEST v1.2 \n");
604 system("i2cset -y -f 2 0x31 0x12 0x46");
b.liu85565a72023-11-09 16:47:40 +0800605 gpio_register_set_func_0(22);
606 gpio_register_set_direction(22, 0); //GPIO22 DTR做输入,检测电压
607
608 #if 0
609 /* 错误点预先关闭 */
610 gpio_register_set_func_0(123);
611 gpio_register_set_func_0(32);
612 gpio_register_set_func_0(53);
613 gpio_register_set_direction(123, 1);
614 gpio_register_set_direction(32, 1);
615 gpio_register_set_direction(53, 1);
616 #endif
617
618 /* [1 all gpio set to low] */
619 for(j = 0; j < 6 ; j++){
620 for(i = 0; i < total; i++){
621 gpio_register_set_func_0(test_gpio[i]);
622 gpio_register_set_direction(test_gpio[i], 1);
623 gpio_register_set_value(test_gpio[i], 0);
624 }
625
626 for(i = 0; i <= 5; i++){
627 ret = gpio_register_get_value(22);
628 if(0 == ret){
629 break;
630 }
631 usleep(3000);
632 }
633 printf("pre set ADC: %d, times: %d\n",ret, j);
634 if(0 == ret){
635 break;
636 }
637 }
638
639 if(0 != ret){
640 printf("set all low Fail, please retest\n");
641 return -1;
642 }
643 printf("pre set SUCCESS\n");
644 return 0;
645}
646
wangyouqiang89888612023-11-28 18:28:12 +0800647int gpio_test(void* arg)
b.liu85565a72023-11-09 16:47:40 +0800648{
649 int ret,i,j;
wangyouqiang89888612023-11-28 18:28:12 +0800650 int *fail_io = (int *)arg;
b.liu85565a72023-11-09 16:47:40 +0800651 int n = 0;
652 for(i = 0; i < total; i++){
653 ret = gpio_register_get_value(test_gpio[i]);
654 printf("GPIO%d default value? %d\n", test_gpio[i], ret);
655 //set high
656 gpio_register_set_value(test_gpio[i], 1);
657 ret = gpio_register_get_value(test_gpio[i]);
658 printf("GPIO%d set high? %d\n", test_gpio[i], ret);
659 //get
660 ret = gpio_register_get_value(22);
661 if(1 != ret){
662 printf("gpio [%d] test high failed\n", test_gpio[i]);
wangyouqiang89888612023-11-28 18:28:12 +0800663 fail_io[n] = test_gpio[i];
b.liu85565a72023-11-09 16:47:40 +0800664 n++;
665 }else{
666 printf("gpio[%d] test high success\n", test_gpio[i]);
667 }
668
669 //set low
670 gpio_register_set_value(test_gpio[i], 0);
671 ret = gpio_register_get_value(test_gpio[i]);
672 printf("GPIO%d set low? %d\n", test_gpio[i], ret);
673 //get
674 ret = gpio_register_get_value(22);
675 if(0 != ret){
676 printf("gpio [%d] test low failed\n", test_gpio[i]);
wangyouqiang89888612023-11-28 18:28:12 +0800677 fail_io[n] = test_gpio[i];
b.liu85565a72023-11-09 16:47:40 +0800678 n++;
679 }else{
680 printf("gpio [%d] test low success\n", test_gpio[i]);
681 }
682 }
683 return n;
684}
685
686
687
b.liu4f8e8792023-11-01 23:25:22 +0800688
689int mbtk_at_gpio(void* arg)
690{
b.liu85565a72023-11-09 16:47:40 +0800691 int i, j, ret, total, n = 0;
wangyouqiang89888612023-11-28 18:28:12 +0800692 //int *fail_io = (int *)arg;
b.liu85565a72023-11-09 16:47:40 +0800693
694 gpio_test_init();
wangyouqiang89888612023-11-28 18:28:12 +0800695 n = gpio_test(arg);
b.liu85565a72023-11-09 16:47:40 +0800696
697 return n;
b.liu4f8e8792023-11-01 23:25:22 +0800698}
699
b.liu85565a72023-11-09 16:47:40 +0800700
701
b.liu4f8e8792023-11-01 23:25:22 +0800702#elif defined(MBTK_PROJECT_L509)
b.liu85565a72023-11-09 16:47:40 +0800703mbtk_gpio_test_pin_paired test_pin_array[] ={ //是GPIO_array
704//120 5 33 这几个输入脚没法正常设置为输入
705 //{52,51,0}, //拉不低 没有这一组
b.liu006dabb2023-11-09 17:31:53 +0800706 //{54,53,0},
b.liu85565a72023-11-09 16:47:40 +0800707 {16,17,0},
708 {117,120,0},
b.liu006dabb2023-11-09 17:31:53 +0800709 //{118,19,0}, //usim
b.liu85565a72023-11-09 16:47:40 +0800710 {123,126,0},
711 {43,23,0},
712 {28,27,0},
713 {26,25,0},
714 {34,33,0},
715 {36,35,0},
716 {49,50,0},
717 {32,31,0},
718 {7,2,0},
719 {6,1,0},
720 {15,3,0},
721 {0,18,0},
722 {4,5,0},
723 {13,14,0},
724 {48,55,0},
725 {56,57,0},
726 {59,58,0},
727 {21,99,0},
728 //{20,22,0}, //拉不低
729 //{12,122,0}, //拉不低 122好像不该用
730};
731//测低
732uint16 mbtk_gpio_test_all_low(mbtk_gpio_test_pin_paired *pin_array, uint16 MAX_pin_num)
733{
734 uint16 index =0;
735 uint16 OUT_pin, Read_pin;
736 uint32 read_high_value;
737 uint32 read_low_value;
738 uint16 fail_num=0;
739 int res;
740
741 printf("[test_low_begin]\n");
742
743
744 for(index = 0; index < MAX_pin_num; index++){
745 OUT_pin = pin_array[index].output_pin;
746 Read_pin = pin_array[index].input_pin;
747
748 //输出低
749 gpio_register_set_direction(OUT_pin,1);
750 gpio_register_set_value(OUT_pin, 0);
751
752 //输入脚设为输入
753 gpio_register_set_direction(Read_pin,0);
754 }
755
756 for(index = 0; index < MAX_pin_num; index++){
757 OUT_pin = pin_array[index].output_pin;
758 Read_pin = pin_array[index].input_pin;
759 read_low_value = gpio_register_get_value(Read_pin);
760 //结果检测
761 if(read_low_value != 0){
762 printf("[!Low Failed!]: GPIO%d \n", Read_pin);
763 pin_array[index].mbtk_gpio_test_result = 1;
764 fail_num ++;
765 }
766 else{
767 printf("[-Low Success-]: GPIO%d \n", Read_pin);
768 }
769 }
770 return fail_num;
771
772}
773
774
775
776//测高
777uint16 mbtk_gpio_test_pin_high(mbtk_gpio_test_pin_paired *pin_array,uint16 MAX_pin_num)
778{
779 uint16 index =0;
780 uint16 OUT_pin, Read_pin;
781 uint32 read_high_value;
782 uint32 read_low_value;
783 uint16 fail_num=0;
784 int i = 0;
785
786 printf("[test_high_begin]\n");
787
788 for(index = 0;index<MAX_pin_num;index++)
789 {
790 OUT_pin = pin_array[index].output_pin;
791 Read_pin = pin_array[index].input_pin;
792
793 //设高
794 gpio_register_set_direction(OUT_pin,1);
795 gpio_register_set_direction(Read_pin,0);
796 gpio_register_set_value(OUT_pin, 1);
797 usleep(20000);
798 read_high_value = gpio_register_get_value(Read_pin);
799 printf("[test_high]: Gpio%d: get value =%d \n", Read_pin, read_high_value);
800
801
802 //设高后重新设低
803 usleep(20000);
804 gpio_register_set_value(OUT_pin, 0);
805 usleep(80000);
806 //read_low_value = gpio_register_get_value(Read_pin);
807 //read_low_value = gpio_register_get_value(Read_pin);
808 for(i = 0;i < 10; i++){
809 read_low_value = gpio_register_get_value(Read_pin);
810 if(0 == read_low_value){
811 break;
812 }
813 usleep(20000);
814 }
815
816 printf("[test_low]: Gpio%d: get value =%d \n", Read_pin, read_low_value);
817
818 if(read_high_value != 1||read_low_value !=0)
819 {
820 printf("[!High Failed!]: Gpio%d\n",Read_pin);
821 pin_array[index].mbtk_gpio_test_result = 2;
822 fail_num ++;
823 }
824 else{
825 printf("[-High Success-]: Gpio%d \n", Read_pin);
826 }
827 }
828 return fail_num;
829
830}
831void gpio_test_init_test_gpio_mode(mbtk_gpio_test_pin_paired pin_array[],int MAX_pin_num)
832{
833 int index =0;
834 int OUT_pin, Read_pin;
835
836 printf("[init] L509 GPIOTEST v1 \n");
837 for(index = 0;index<MAX_pin_num;index++)
838 {
839 OUT_pin = pin_array[index].output_pin;
840 Read_pin = pin_array[index].input_pin;
841 gpio_register_set_func_0(OUT_pin);
842 gpio_register_set_func_0(Read_pin);
843 }
844
845 //gpio_register_set_func_0();
b.liu006dabb2023-11-09 17:31:53 +0800846 system("i2cset -y -f 2 0x31 0x12 0x46");
b.liu85565a72023-11-09 16:47:40 +0800847 system("echo 33 > /sys/class/gpio/export");
848 system("echo in > /sys/class/gpio/gpio120/direction");
849 system("echo in > /sys/class/gpio/gpio5/direction");
850 system("echo in > /sys/class/gpio/gpio33/direction");
851 printf("[3init]gpio_test_init_test_gpio_mode FINISH\n");
852
853}
854
b.liu4f8e8792023-11-01 23:25:22 +0800855
856int mbtk_at_gpio(void* arg)
857{
b.liu85565a72023-11-09 16:47:40 +0800858 int i;
859 int *fail_io = (int *)arg;
860 /*OPEN SWITCH */
861 uint16 test_fail_count = 0;
862 uint16 test_MAX_pin_num = 0;
863 char buf[STR_MAX_LEN];
864 uint16 str_len=0;
865 uint16 temp_len =0;
866 uint16 Out_pin;
867 uint16 in_pin;
868 uint16 fail_print_num = 0;
869
870 gpio_register_set_func_0(125);
871 gpio_register_set_direction(125, 1);
872 gpio_register_set_value(125, 1); //开关使能脚
873#if 0
874 /* [2]设置高低电压范围 */
875 if(th->threshold_high == 0 && th->threshold_low == 0 &&th->threshold_high_low_diff== 0)
876 { //use default threshold
877 th->threshold_high = MBTK_GPIOTEST_THRESHOLD_DEFAULT_HIGH;
878 th->threshold_low = MBTK_GPIOTEST_THRESHOLD_DEFAULT_LOW;
879 th->threshold_high_low_diff = MBTK_GPIOTEST_THRESHOLD_DEFAULT_DIFF;
880 }
881 RTI_LOG("GPIOTEST: threshold -- %d -- %d -- %d",th->threshold_low,th->threshold_high_low_diff,th->threshold_high);
882#endif
883
884 /* [3]初始化待测GPIO */
885 test_MAX_pin_num = sizeof(test_pin_array)/sizeof(test_pin_array[0]);
886 gpio_test_init_test_gpio_mode(test_pin_array,test_MAX_pin_num);
887
888
889
890 /* [4]测试过程 */
891 test_fail_count = mbtk_gpio_test_all_low(test_pin_array,test_MAX_pin_num);
892 printf("[set_low]set ALL GPIO low, fail_num=%d\n",test_fail_count);
893 if(!test_fail_count) //set all pin low passed
894 {
895 printf("[set all pin low passed]\n");
896 test_fail_count = mbtk_gpio_test_pin_high(&test_pin_array,test_MAX_pin_num);
897 }
898
899 //memset(buf,0,STR_MAX_LEN);
900
901 /* [5]测试结果检测 */
902 if(test_fail_count )
903 {
904 //printf( "GPIOTEST Fail %02d PINs:\n", test_fail_count*2);
905 sprintf(buf, "GPIOTEST Fail %02d PINs:", test_fail_count*2);
906 temp_len = strlen(buf);
907
908
909 for(i = 0; i< test_MAX_pin_num; i++)
910 {
911
912 if(test_pin_array[i].mbtk_gpio_test_result)
913 {
914
915 Out_pin = test_pin_array[i].output_pin;
916 in_pin = test_pin_array[i].input_pin;
917 fail_io[i] = Out_pin;
918 fail_io[i+1] = in_pin;
919
920 }
921
922 }
923 }
924
925 else
926 {
927 printf(buf,"ALL GPIO TEST PASS\r\n");
928 }
929 //temp_len = strlen(buf);
930 return test_fail_count;
b.liu4f8e8792023-11-01 23:25:22 +0800931}
932
b.liu43da3412023-11-08 15:58:39 +0800933
b.liu4f8e8792023-11-01 23:25:22 +0800934#else
935
936int mbtk_at_gpio(void* arg)
937{
938 return -1;
939}
940
941#endif