blob: e2f0cd2588c369fdb520e1e635f50e656e2daad3 [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"
gy.tang141ce612024-07-03 11:01:58 +080026#include "mbtk_type.h"
liubin281ac462023-07-19 14:22:54 +080027
28#define gpio_log(...) if(gpio_debug)printf(__VA_ARGS__)
wangyouqiang7850b4a2023-09-18 15:47:57 +080029#define HWMAP_DEVICE "/dev/hwmap"
30#define PAGE_OFFS_BITS(pgsz) ((unsigned int)(pgsz)-1)
31#define PAGE_MASK_BITS(pgsz) (~PAGE_OFFS_BITS(pgsz))
b.liu85565a72023-11-09 16:47:40 +080032#define STR_MAX_LEN 220
wangyouqiang7850b4a2023-09-18 15:47:57 +080033typedef enum {
34 MBTK_ADC0 = 0, /* ADC 0 */
35 MBTK_ADC1 /* ADC 1 */
36} mbtk_adc_enum;
liubin281ac462023-07-19 14:22:54 +080037
38static int gpio_debug = 0;
luojianc08d4e62024-07-05 17:12:02 +080039#define DEBUG_GPIO_TEST 1
liubin281ac462023-07-19 14:22:54 +080040
gy.tangf4d7b852024-06-27 18:50:14 +080041/**/
42#define GPIO_MAX_CURRENT (7<<10) //设置GPIO最大输出电流
43#define GPIO_STRONG_PULLUP (1<<3) //设置GPIO强上拉
44#define GPIO_PULLUP_ENABLE ((1<<15)|(1<<14)) //GPIO上拉
45#define GPIO_PULLDOWN_ENABLE ((1<<15)|1<<13) //GPIO下拉
46
47#define MBTK_GPIO_OUTPUT 1
48#define MBTK_GPIO_INPUT 0
49
wangyouqiangb1deff32024-03-05 13:31:27 +080050#if (defined(MBTK_PROJECT_L508_X6) || defined(MBTK_PROJECT_T108))
wangyouqiang7775cbf2023-12-29 16:35:24 +080051struct gpio_register_function gpio_func_register[128] = {
52 {GPIO_FUNC_GPIO_00, 0},
53 {GPIO_FUNC_GPIO_01, 0},
54 {GPIO_FUNC_GPIO_02, 0},
55 {GPIO_FUNC_GPIO_03, 0},
56 {GPIO_FUNC_GPIO_04, 0},
57 {GPIO_FUNC_GPIO_05, 0},
58 {GPIO_FUNC_GPIO_06, 0},
59 {GPIO_FUNC_GPIO_07, 0},
60 {GPIO_FUNC_GPIO_08, 0},
61 {GPIO_FUNC_GPIO_09, 0},
62 {GPIO_FUNC_GPIO_10, 0},
63 {GPIO_FUNC_GPIO_11, 0},
64 {GPIO_FUNC_GPIO_12, 0},
65 {GPIO_FUNC_GPIO_13, 0},
66 {GPIO_FUNC_GPIO_14, 0},
67 {GPIO_FUNC_GPIO_15, 0},
68 {GPIO_FUNC_GPIO_16, 0},
69 {GPIO_FUNC_GPIO_17, 0},
70 {GPIO_FUNC_GPIO_18, 0},
71 {GPIO_FUNC_GPIO_19, 0},
72 {GPIO_FUNC_GPIO_20, 0},
73 {GPIO_FUNC_GPIO_21, 0},
74 {GPIO_FUNC_GPIO_22, 0},
75 {GPIO_FUNC_GPIO_23, 0},
76 {GPIO_FUNC_GPIO_24, 0},
77 {GPIO_FUNC_GPIO_25, 0},
78 {GPIO_FUNC_GPIO_26, 0},
79 {GPIO_FUNC_GPIO_27, 0},
80 {GPIO_FUNC_GPIO_28, 0},
81 {GPIO_FUNC_GPIO_29, 0},
82 {GPIO_FUNC_GPIO_30, 0},
83 {GPIO_FUNC_GPIO_31, 0},
84 {GPIO_FUNC_GPIO_32, 0},
85 {GPIO_FUNC_GPIO_33, 0},
86 {GPIO_FUNC_GPIO_34, 0},
87 {GPIO_FUNC_GPIO_35, 0},
wangyouqiangb1deff32024-03-05 13:31:27 +080088 {GPIO_FUNC_GPIO_36, 0},
89 {GPIO_FUNC_GPIO_37, 0}, // GPIO_37
90 {GPIO_FUNC_GPIO_38, 0}, // GPIO_38
91 {GPIO_FUNC_GPIO_39, 0}, // GPIO_39
92 {GPIO_FUNC_GPIO_40, 0}, // GPIO_40
93 {GPIO_FUNC_GPIO_41, 0}, //GPIO_41
94 {GPIO_FUNC_GPIO_42, 0}, //GPIO_42
wangyouqiang7775cbf2023-12-29 16:35:24 +080095 {GPIO_FUNC_GPIO_43, 0}, //GPIO_43
96 {GPIO_FUNC_GPIO_44,0}, //GPIO_44
97 {GPIO_FUNC_GPIO_45,0}, //GPIO_45
98 {GPIO_FUNC_GPIO_46,0}, //GPIO_46
99 {GPIO_FUNC_GPIO_47,0}, //GPIO_47
100 {GPIO_FUNC_SDIO_DAT3, 1}, //GPIO_48
101 {GPIO_FUNC_GPIO_49, 0},
102 {GPIO_FUNC_GPIO_50, 0},
103 {GPIO_FUNC_GPIO_51, 0},
104 {GPIO_FUNC_GPIO_52, 0},
105 {GPIO_FUNC_GPIO_53, 0},
106 {GPIO_FUNC_GPIO_54, 0},
107 {GPIO_FUNC_SDIO_DAT2, 1}, //GPIO_55
108 {GPIO_FUNC_SDIO_DAT1, 1}, //GPIO_56
109 {GPIO_FUNC_SDIO_DAT0, 1}, //GPIO_57
110 {GPIO_FUNC_SDIO_CMD, 1}, //GPIO_58
111 {GPIO_FUNC_SDIO_CLK, 1}, //GPIO_59
112 {GPIO_FUNC_GPIO_60, 0},
113 {0,0},{0,0},{0,0},{0,0},
114 {0,0},{0,0},
115 {GPIO_FUNC_DVL_0, 1},//GPIO_67
116 {GPIO_FUNC_DVL_1, 1},//GPIO_68
117 {GPIO_FUNC_GPIO_69, 0},
118 {GPIO_FUNC_GPIO_70, 0},
119 {GPIO_FUNC_QSPI_DAT3, 1}, //GPIO_71
120 {GPIO_FUNC_QSPI_DAT2, 1}, //GPIO_72
121 {GPIO_FUNC_QSPI_DAT1, 1}, //GPIO_73
122 {GPIO_FUNC_QSPI_DAT0, 1}, //GPIO_74
123 {GPIO_FUNC_QSPI_CLK, 1}, //GPIO_75
124 {GPIO_FUNC_QSPI_CS1, 1}, //GPIO_76
125 {GPIO_FUNC_GPIO_77, 0},
126 {GPIO_FUNC_GPIO_78, 0},
127 {GPIO_FUNC_GPIO_79, 0},
128 {GPIO_FUNC_GPIO_80, 0},
129 {GPIO_FUNC_USIM_UCLK, 1},//GPIO_81
130 {GPIO_FUNC_USIM_UIO, 1},//GPIO_82
131 {GPIO_FUNC_USIM_URSTn, 1},//GPIO_83
132
133 {GPIO_FUNC_MMC1_DAT3,5}, //GPIO_84
134 {GPIO_FUNC_MMC1_DAT2,5}, //GPIO_85
135 {GPIO_FUNC_MMC1_DAT1,5}, //GPIO_86
136 {GPIO_FUNC_MMC1_DAT0,5}, //GPIO_87
137 {GPIO_FUNC_MMC1_CMD,5}, //GPIO_88
138 {GPIO_FUNC_MMC1_CLK,5}, //GPIO_89
139 {GPIO_FUNC_MMC1_CD,1}, //GPIO_90
140 {0,0},
141 {0,0},{0,0},{0,0},{0,0},
142 {0,0},{0,0},{0,0},
143 {GPIO_FUNC_USB_ID, 1},//GPIO_99
144 {0,0},{0,0},{0,0},{0,0},
145 {0,0},{0,0},{0,0},{0,0},
146 {0,0},{0,0},{0,0},{0,0},
147 {0,0},{0,0},{0,0},{0,0},
148 {0,0},
149 {GPIO_FUNC_PRI_TDI, 1}, //GPIO_117
150 {GPIO_FUNC_PRI_TMS, 1}, //GPIO_118
151 {GPIO_FUNC_PRI_TCK, 1}, //GPIO_119
152 {GPIO_FUNC_PRI_TDO, 1}, //GPIO_120
153 {GPIO_FUNC_QSPI_VMODE_GPIO, 1}, //GPIO_121
154 {GPIO_FUNC_VBUS_DRV, 1}, //GPIO_122
155 {GPIO_FUNC_CLK_REQ, 1}, //GPIO_123
156 {0,0},
157 {GPIO_FUNC_VCXO_REQ, 1}, //GPIO_125
158 {0,0}, //GPIO_126
159 {GPIO_FUNC_VCXO_OUT, 1}, //GPIO_127
160 {0,0},
161};
162
163#else
liubin281ac462023-07-19 14:22:54 +0800164struct gpio_register_function gpio_func_register[128] = {
165 {GPIO_FUNC_GPIO_00, 0},
166 {GPIO_FUNC_GPIO_01, 0},
167 {GPIO_FUNC_GPIO_02, 0},
168 {GPIO_FUNC_GPIO_03, 0},
169 {GPIO_FUNC_GPIO_04, 0},
170 {GPIO_FUNC_GPIO_05, 0},
171 {GPIO_FUNC_GPIO_06, 0},
172 {GPIO_FUNC_GPIO_07, 0},
173 {GPIO_FUNC_GPIO_08, 0},
174 {GPIO_FUNC_GPIO_09, 0},
175 {GPIO_FUNC_GPIO_10, 0},
176 {GPIO_FUNC_GPIO_11, 0},
177 {GPIO_FUNC_GPIO_12, 0},
178 {GPIO_FUNC_GPIO_13, 0},
179 {GPIO_FUNC_GPIO_14, 0},
180 {GPIO_FUNC_GPIO_15, 0},
181 {GPIO_FUNC_GPIO_16, 0},
182 {GPIO_FUNC_GPIO_17, 0},
183 {GPIO_FUNC_GPIO_18, 0},
184 {GPIO_FUNC_GPIO_19, 0},
185 {GPIO_FUNC_GPIO_20, 0},
186 {GPIO_FUNC_GPIO_21, 0},
187 {GPIO_FUNC_GPIO_22, 0},
188 {GPIO_FUNC_GPIO_23, 0},
189 {GPIO_FUNC_GPIO_24, 0},
190 {GPIO_FUNC_GPIO_25, 0},
191 {GPIO_FUNC_GPIO_26, 0},
192 {GPIO_FUNC_GPIO_27, 0},
193 {GPIO_FUNC_GPIO_28, 0},
194 {GPIO_FUNC_GPIO_29, 0},
195 {GPIO_FUNC_GPIO_30, 0},
196 {GPIO_FUNC_GPIO_31, 0},
197 {GPIO_FUNC_GPIO_32, 0},
198 {GPIO_FUNC_GPIO_33, 0},
199 {GPIO_FUNC_GPIO_34, 0},
200 {GPIO_FUNC_GPIO_35, 0},
201 {GPIO_FUNC_GPIO_36, 0},
202 {GPIO_FUNC_MMC1_DAT3, 5}, // GPIO_37
203 {GPIO_FUNC_MMC1_DAT2, 5}, // GPIO_38
204 {GPIO_FUNC_MMC1_DAT1, 5}, // GPIO_39
205 {GPIO_FUNC_MMC1_DAT0, 5}, // GPIO_40
206 {GPIO_FUNC_MMC1_CMD, 5}, //GPIO_41
207 {GPIO_FUNC_MMC1_CLK, 5}, //GPIO_42
208 {GPIO_FUNC_MMC1_CD , 1}, //GPIO_43
209 {0,0},{0,0},{0,0},{0,0},
210 {GPIO_FUNC_SDIO_DAT3, 1}, //GPIO_48
211 {GPIO_FUNC_GPIO_49, 0},
212 {GPIO_FUNC_GPIO_50, 0},
213 {GPIO_FUNC_GPIO_51, 0},
214 {GPIO_FUNC_GPIO_52, 0},
215 {GPIO_FUNC_GPIO_53, 0},
216 {GPIO_FUNC_GPIO_54, 0},
217 {GPIO_FUNC_SDIO_DAT2, 1}, //GPIO_55
218 {GPIO_FUNC_SDIO_DAT1, 1}, //GPIO_56
219 {GPIO_FUNC_SDIO_DAT0, 1}, //GPIO_57
220 {GPIO_FUNC_SDIO_CMD, 1}, //GPIO_58
221 {GPIO_FUNC_SDIO_CLK, 1}, //GPIO_59
222 {GPIO_FUNC_GPIO_60, 0},
223 {0,0},{0,0},{0,0},{0,0},
224 {0,0},{0,0},
225 {GPIO_FUNC_DVL_0, 1},//GPIO_67
226 {GPIO_FUNC_DVL_1, 1},//GPIO_68
227 {GPIO_FUNC_GPIO_69, 0},
228 {GPIO_FUNC_GPIO_70, 0},
229 {GPIO_FUNC_QSPI_DAT3, 1}, //GPIO_71
230 {GPIO_FUNC_QSPI_DAT2, 1}, //GPIO_72
231 {GPIO_FUNC_QSPI_DAT1, 1}, //GPIO_73
232 {GPIO_FUNC_QSPI_DAT0, 1}, //GPIO_74
233 {GPIO_FUNC_QSPI_CLK, 1}, //GPIO_75
234 {GPIO_FUNC_QSPI_CS1, 1}, //GPIO_76
235 {GPIO_FUNC_GPIO_77, 0},
236 {GPIO_FUNC_GPIO_78, 0},
237 {GPIO_FUNC_GPIO_79, 0},
238 {GPIO_FUNC_GPIO_80, 0},
239 {GPIO_FUNC_USIM_UCLK, 1},//GPIO_81
240 {GPIO_FUNC_USIM_UIO, 1},//GPIO_82
241 {GPIO_FUNC_USIM_URSTn, 1},//GPIO_83
242 {0,0},{0,0},{0,0},{0,0},
243 {0,0},{0,0},{0,0},{0,0},
244 {0,0},{0,0},{0,0},{0,0},
245 {0,0},{0,0},{0,0},
246 {GPIO_FUNC_USB_ID, 1},//GPIO_99
247 {0,0},{0,0},{0,0},{0,0},
248 {0,0},{0,0},{0,0},{0,0},
249 {0,0},{0,0},{0,0},{0,0},
250 {0,0},{0,0},{0,0},{0,0},
251 {0,0},
252 {GPIO_FUNC_PRI_TDI, 1}, //GPIO_117
253 {GPIO_FUNC_PRI_TMS, 1}, //GPIO_118
254 {GPIO_FUNC_PRI_TCK, 1}, //GPIO_119
255 {GPIO_FUNC_PRI_TDO, 1}, //GPIO_120
256 {GPIO_FUNC_QSPI_VMODE_GPIO, 1}, //GPIO_121
257 {GPIO_FUNC_VBUS_DRV, 1}, //GPIO_122
258 {GPIO_FUNC_CLK_REQ, 1}, //GPIO_123
259 {0,0},
260 {GPIO_FUNC_VCXO_REQ, 1}, //GPIO_125
261 {GPIO_FUNC_VCXO_OUT, 1}, //GPIO_126
262 {0,0},
263};
liubin281ac462023-07-19 14:22:54 +0800264
wangyouqiang7775cbf2023-12-29 16:35:24 +0800265#endif
liubin281ac462023-07-19 14:22:54 +0800266
wangyouqiangece1c722023-12-12 17:28:53 +0800267static int mbtk_gpio_adc(channel)
liubin281ac462023-07-19 14:22:54 +0800268{
liubin281ac462023-07-19 14:22:54 +0800269 int ret = 0;
270
wangyouqiangece1c722023-12-12 17:28:53 +0800271 ret = mbtk_adc_get(channel);
luojianc08d4e62024-07-05 17:12:02 +0800272#if DEBUG_GPIO_TEST
273 printf("ADC_%d_value =%d\n", channel, ret);
274#endif
275 if(ret >= 500 ){ //TODO: 各项目高电平标准?
wangyouqiang7850b4a2023-09-18 15:47:57 +0800276 ret = 1;
liubin281ac462023-07-19 14:22:54 +0800277 }
wangyouqiang7850b4a2023-09-18 15:47:57 +0800278 else if (ret>=0 && ret<=480){
279 ret = 0;
280 }
281 else{
282 ret = -1;
liubin281ac462023-07-19 14:22:54 +0800283 }
284 return ret;
285}
liubin281ac462023-07-19 14:22:54 +0800286
liubin281ac462023-07-19 14:22:54 +0800287
288static int hwacc_register(int rw, unsigned int addr, unsigned int *data)
289{
290 int fid;
291 unsigned int pagesize, len, len_aligned;
292 unsigned int addr_aligned;
293 volatile unsigned int *pa;
294 void *vpa;
295
296 len = pagesize = sysconf(_SC_PAGESIZE);
297 if((fid = open(HWMAP_DEVICE, O_RDWR)) < 0)
298 {
299 printf("Failed to open %s\n", HWMAP_DEVICE);
300 exit(-1);
301 }
302
303 // Align the length so the mapped area is page-aligned and contains the requested area
304 addr_aligned = addr & PAGE_MASK_BITS(pagesize);
305 len_aligned =((addr + len - addr_aligned) + pagesize - 1) & PAGE_MASK_BITS(pagesize);
306
307 /* Notes on flags: MAP_PRIVATE results in copy on write; MAP_SHARED allows normal write */
308 /* MAP_SHARED required O_RDWR in open above, otherwise mmap fails with errno=EACCES */
309 /* Notes on prot: PROT_WRITE allows read and write; PROT_READ allows read only */
310 /* Notes on off: an unsigned 32-bit value, should be aligned to page size according to mmap manpage */
311 if((vpa = mmap(0, len_aligned, PROT_READ|PROT_WRITE, MAP_SHARED, fid, addr_aligned)) == MAP_FAILED)
312 {
313 printf("mmap failed (%d)\n", errno);
314 }
315 else
316 {
317 pa = (volatile unsigned int *)((unsigned char *)vpa + (addr & PAGE_OFFS_BITS(pagesize)));
318 if(rw == 0)
319 {
320 *data = *pa;
321 gpio_log("Value read from 0x%.8x via MVA=0x%p is 0x%.8x\n", addr, pa, *data);
322 }
323 else if(rw == 1)
324 {
325 *pa = *data;
326 gpio_log("Value %.8x written to 0x%.8x via MVA=0x%p\n", *data, addr, pa);
327#if defined(HWACC_DEBUG)
328 {
329 unsigned int val;
330 val = *pa;
331 printf("Value read from 0x%.8x via MVA=0x%p is 0x%.8x\n", addr, pa, val);
332 }
333#endif
334 }
335 munmap(vpa, len);
336 }
337
338 close(fid);
339 return 0;
340}
341/*
342设置GPIO 模式:第一步读GPIO22默认function
343root@OpenWrt:/# hwacc r 0xd401e134
344Option = r Addr = d401e134
345Value read from 0xd401e134 via MVA=0x0xb6fc3134 is 0x0000d040 //默认GPIO功能
346Bit0~bit2值对应上面表格各function,0代表GPIO功能
347 */
348static int gpio_register_read(int reg)
349{
350 int ret = -1;
351#if 0
352 FILE * fp;
353 // "/bin/hwacc r 0xd401e134";
354 char command[36] = {0};
355 char buffer[1024];
356 int i = 0;
357
358 sprintf(command, "/bin/hwacc r 0x%x", reg);
359 fp = popen(command, "r");
360 while(1)
361 {
362 if( fgets (buffer, sizeof(buffer), fp)!=NULL ) {
363 buffer[strlen(buffer) - 1] = 0;
364 // gpio_log("out [%d]: %s\n", strlen(buffer), buffer);
365 }else{
366 break;
367 }
368 i = strstr_n(buffer, "is");
369 if(i)
370 {
371 ret = str_to_hex(&buffer[i + 2]);
372 gpio_log("read 0x%x value:%s, %x\n", reg, &buffer[i + 2], ret);
373 }
374 }
375 pclose(fp);
376#else
b.liu85565a72023-11-09 16:47:40 +0800377#ifndef MBTK_PROJECT_PN1803
378 usleep(50);
379#endif
liubin281ac462023-07-19 14:22:54 +0800380 hwacc_register(0, reg, &ret);
b.liu85565a72023-11-09 16:47:40 +0800381#ifndef MBTK_PROJECT_PN1803
382 usleep(50);
383#endif
liubin281ac462023-07-19 14:22:54 +0800384#endif
385 return ret;
386}
387/*
388设置输入输出状态,设置PDR寄存器GPIO22为output
389root@OpenWrt:/# hwacc w 0xD401900c 0x00c03800
390Option = w Addr = d401900c Data=00c03800
391Value 00c03800 written to 0xd401900c via MVA=0x0xb6f9f00c
392 */
393static void gpio_register_write(int reg, int value)
394{
395#if 0
396 FILE * fp;
397 // "/bin/hwacc w 0xD401900c 0x00c03800"
398 char command[36] = {0};
399 char buffer[1024];
400
401 sprintf(command, "/bin/hwacc w 0x%x 0x%x", reg, value);
402 gpio_log("command: %s\n", command);
403 fp = popen(command, "r");
404 while(1)
405 {
406 if( fgets (buffer, sizeof(buffer), fp)!=NULL ) {
407 gpio_log("%s\n", buffer);
408 }else{
409 break;
410 }
411 }
412 pclose(fp);
413
414#else
b.liu85565a72023-11-09 16:47:40 +0800415#ifndef MBTK_PROJECT_PN1803
416 usleep(50);
417#endif
liubin281ac462023-07-19 14:22:54 +0800418 hwacc_register(1, reg, &value);
b.liu85565a72023-11-09 16:47:40 +0800419#ifndef MBTK_PROJECT_PN1803
420 usleep(50);
421#endif
liubin281ac462023-07-19 14:22:54 +0800422#endif
423}
424/*
425AF SEL<p>This field is used for alternate function selection for a pin.
426It selects between the eight possible alternate functions for the pin.
427Alternate function 0 is always the reset case.
428
429<p>0x0 = Alternate function 0 (primary function at reset)
430<p>0x1 = Alternate function 1
431<p>0x2 = Alternate function 2
432<p>0x3 = Alternate function 3
433<p>0x4 = Alternate function 4
434<p>0x5 = Alternate function 5
435<p>0x6 = Alternate function 6
436<p>0x7 = Alternate function 7
437 */
438static int gpio_register_set_func_0(int port)
439{
440 int ret;
441 struct gpio_register_function *reg = NULL;
442 if(port > 128)
443 return -1;
444 reg = &gpio_func_register[port];
445 if(0 == reg->reg)
446 return -1;
447 ret = gpio_register_read(reg->reg);
448 if((ret & 0x7) != reg->func_gpio)
449 {
wangyouqiang7850b4a2023-09-18 15:47:57 +0800450 //printf("Gpio set func [%d] [0x%x]!\n", reg->func_gpio, (ret & 0xfffffff8) | reg->func_gpio);
liubin281ac462023-07-19 14:22:54 +0800451 gpio_register_write(reg->reg, (ret & 0xfffffff8) | reg->func_gpio);
452 }
453 return 0;
454}
455/*
456设置GPIO 方向
457读取输入输出状态,读PDR寄存器:0x0c
458root@OpenWrt:/# hwacc r 0xD401900c
459Option = r Addr = d401900c
460Value read from 0xd401900c via MVA=0x0xb6f3900c is 0x00803800 //bit22为0,代表Input
461 */
462static void gpio_register_set_direction(int port, int dir)
463{
464 int ret;
465 int reg = 0xD4019000;
466
467 if(port > (32 - 1))
468 reg = 0xD4019004;
469 if(port > (32 * 2 - 1))
470 reg = 0xD4019008;
471 if(port > (32 * 3 - 1))
472 reg = 0xD4019100;
473
474 reg += 0x0c;
475 port = port % 0x20;
476 ret = gpio_register_read(reg);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800477 //printf("[Direction] reg_value=%x\n", ret);
liubin281ac462023-07-19 14:22:54 +0800478 // 设置 输出 0 && 1
479 if(!(ret & (0x1 << port)) && dir)
480 {
gy.tangf4d7b852024-06-27 18:50:14 +0800481 gpio_register_write(reg, ret | (0x1 << port)); //set the gpio bit
wangyouqiang7850b4a2023-09-18 15:47:57 +0800482 //printf("[Direction out] reg_value=%x\n", gpio_register_read(reg));
liubin281ac462023-07-19 14:22:54 +0800483 }
484 // 设置 输入 1 && 0
485 if((ret & (0x1 << port)) && !dir)
486 {
gy.tangf4d7b852024-06-27 18:50:14 +0800487#if 1
488 gpio_register_write(reg, ret & (~(0x1 << port))); //clear the gpio bit,mbtk_tanggaoyou
489#else
490 gpio_register_write(reg, ret | !(0x1 << port)); //这是错误配置,实际没有任何作用,还是原来的值
491#endif
wangyouqiang7850b4a2023-09-18 15:47:57 +0800492 //printf("[Direction in] reg_value=%x\n", gpio_register_read(reg));
liubin281ac462023-07-19 14:22:54 +0800493 }
494}
495/*
496设置GPIO 输出电平
497
498读取电平状态,先读PLR寄存器:0x00
499root@OpenWrt:/# hwacc r 0xD4019000
500Option = r Addr = d4019000
501Value read from 0xd4019000 via MVA=0x0xb6f1c000 is 0x81e82a30
502对应下面具体BIT
5031000 0001 1110 1000 0010 1010 0011 0000 BIT22默认电平高
504
505设置输出高:设置PSR寄存器:0x18(只写寄存器)
506root@OpenWrt:/# hwacc w 0xD4019018 0x400000
507Option = w Addr = d4019018 Data=00400000
508Value 00400000 written to 0xd4019018 via MVA=0x0xb6f56018 //bit22写1,输出高
509
510设置输出低:设置PCR寄存器:0x24
511root@OpenWrt:/# hwacc w 0xD4019024 0x400000
512Option = w Addr = d4019024 Data=00400000
513Value 00400000 written to 0xd4019024 via MVA=0x0xb6faa024 //Bit22写1,GPIO22输出低
514
515 */
516static void gpio_register_set_value(int port, int value)
517{
518 int ret;
519 int reg = 0xD4019000;
520
521 if(port > (32 - 1))
522 reg = 0xD4019004;
523 if(port > (32 * 2 - 1))
524 reg = 0xD4019008;
525 if(port > (32 * 3 - 1))
526 reg = 0xD4019100;
527
528 if(value)
529 {
530 reg += 0x18;
531 }
532 else
533 reg += 0x24;
534
535 port = port % 0x20;
536 ret = gpio_register_read(reg);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800537 //printf("[Value] reg_value=%x\n", gpio_register_read(0xD4019004));
liubin281ac462023-07-19 14:22:54 +0800538 // 设置 高电平 0 && 1
539 if(value)
540 {
541 gpio_register_write(reg, ret | (0x1 << port));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800542 //printf("[Value high] reg_value=%x\n", gpio_register_read(0xD4019004));
liubin281ac462023-07-19 14:22:54 +0800543 return;
544 }
545 // 设置 低电平 1 && 0
546 if(!(ret & (0x1 << port)) && !value)
547 {
548 gpio_register_write(reg, ret | (0x1 << port));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800549 //printf("[Value low] reg_value=%x\n", gpio_register_read(0xD4019004));
liubin281ac462023-07-19 14:22:54 +0800550 }
551}
552/*
553读取电平状态,先读PLR寄存器:0x00
554root@OpenWrt:/# hwacc r 0xD4019000
555Option = r Addr = d4019000
556Value read from 0xd4019000 via MVA=0x0xb6f1c000 is 0x81e82a30
557对应下面具体BIT
5581000 0001 1110 1000 0010 1010 0011 0000 BIT22默认电平高
559 */
560static int gpio_register_get_value(int port)
561{
562 int ret = -1;
563 int reg = 0xD4019000;
564
565 if(port > (32 - 1))
566 reg = 0xD4019004;
567 if(port > (32 * 2 - 1))
568 reg = 0xD4019008;
569 if(port > (32 * 3 - 1))
570 reg = 0xD4019100;
571 port = port % 0x20;
572 ret = gpio_register_read(reg);
573 if(ret & (0x1 << port))
574 {
575 return 1;
576 }
577 return 0;
578}
579
580void gpio_debug_set(int enable)
581{
582 gpio_debug = enable;
583}
584
b.liu85565a72023-11-09 16:47:40 +0800585
586
587#if defined(MBTK_PROJECT_PN1803)
liubin281ac462023-07-19 14:22:54 +0800588int gpio_register_test_out(int port, int value)
589{
590 int ret;
wangyouqiang7850b4a2023-09-18 15:47:57 +0800591 int i;
592 int valueh = 0;
593 int valuel = 1;
594
wangyouqiang4c262fa2023-10-17 17:48:03 +0800595 //printf("Gpio port [%d] test start!\n", port);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800596 ret = gpio_register_set_func_0(port); //设功能为GPIO
597 if(ret){
liubin281ac462023-07-19 14:22:54 +0800598 printf("gpio_port can't support!\n");
599 return -1;
600 }
wangyouqiang7850b4a2023-09-18 15:47:57 +0800601 gpio_register_set_direction(port, 1); //设方向为输出
liubin281ac462023-07-19 14:22:54 +0800602 ret = gpio_register_get_value(port);
wangyouqiang4c262fa2023-10-17 17:48:03 +0800603 //printf("gpio default value is : %d.\n", ret);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800604
605
606 //[High]
607 for(i = 0; i <= 10; i++){
608 gpio_register_set_value(port, 1);
609 usleep(50);
610 valueh = gpio_register_get_value(port);
wangyouqiang4c262fa2023-10-17 17:48:03 +0800611 //printf("set high? %d\n",valueh);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800612 if(1 == valueh){
613 break;
614 }
615 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800616 //usleep(10000);
617 for(i = 0; i <= 35; i++){
wangyouqiangece1c722023-12-12 17:28:53 +0800618 ret = mbtk_gpio_adc(0);
wangyouqiang59d8dd22023-10-19 18:03:54 +0800619 usleep(3000);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800620 if(1 == ret){
621 break;
622 }
623 }
wangyouqiang7850b4a2023-09-18 15:47:57 +0800624 if(1 != ret){
625 ret=-1;
liubin281ac462023-07-19 14:22:54 +0800626 goto exit;
wangyouqiang7850b4a2023-09-18 15:47:57 +0800627 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800628 printf("******gpio should is high: %d.******\n", ret);
liubin281ac462023-07-19 14:22:54 +0800629
wangyouqiang7850b4a2023-09-18 15:47:57 +0800630
631 //[Low]
632 usleep(200);
633 for(i = 0; i <= 10; i++){
634 gpio_register_set_value(port, 0); //输出低
b.liu074771a2023-10-19 13:13:39 +0800635 usleep(50);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800636 valuel = gpio_register_get_value(port);
wangyouqiang59d8dd22023-10-19 18:03:54 +0800637 //printf("set low? %d\n",valuel);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800638 if(0 == valuel){
639 break;
640 }
641 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800642 //usleep(10000);
643 for(i = 0; i <= 35; i++){
wangyouqiangece1c722023-12-12 17:28:53 +0800644 ret = mbtk_gpio_adc(0);
wangyouqiang59d8dd22023-10-19 18:03:54 +0800645 usleep(3000);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800646 if(0 == ret){
647 break;
648 }
649 }
b.liu074771a2023-10-19 13:13:39 +0800650 printf("******gpio should is low: %d.******\n", ret);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800651 if(0 != ret){
652 ret=-1;
liubin281ac462023-07-19 14:22:54 +0800653 goto exit;
654 }
655
656exit:
wangyouqiang7850b4a2023-09-18 15:47:57 +0800657 //gpio_register_set_direction(port, 0); //设方向为输入
liubin281ac462023-07-19 14:22:54 +0800658 return ret;
659}
660
b.liu94091352024-07-02 10:38:08 +0800661int mbtk_at_gpio(bool gnss_support, void* arg)
liubin281ac462023-07-19 14:22:54 +0800662{
b.liu37e72162023-11-01 15:38:18 +0800663 int test_gpio[] = {
664 56, 55, 58, 57, 48, 59, 12, 20, 5, 43, 21,
665 49, 50, 27, 28, 26, 25, 16, 17, 15, 126, 125,
b.liu810b1a32023-11-01 22:55:07 +0800666 4, 0, 3, 18, 7, 6, 1, 2, /*54,*/ 19, 13,
667 32, 31, 23, 24, 22, /*122,*/ 33, 35, 36, 34, 14,
wangyouqiangddbcf2e2023-11-14 13:59:41 +0800668 99,/* 53,*/ 37, 38, 39, 40, 41, 42};
liubin281ac462023-07-19 14:22:54 +0800669
wangyouqiang7850b4a2023-09-18 15:47:57 +0800670 int i, j, ret, total, n = 0;
liubin281ac462023-07-19 14:22:54 +0800671 int *fail_io = (int *)arg;
672 total = (sizeof(test_gpio)/sizeof(int));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800673 //printf("Start test gpio total: %d\n", total);
674
675 /* [1 all gpio set to low] */
676 for(j = 0; j < 6 ; j++){
677 for(i = 0; i < total; i++){
678 gpio_register_set_func_0(test_gpio[i]);
679 gpio_register_set_direction(test_gpio[i], 1);
680 gpio_register_set_value(test_gpio[i], 0);
681 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800682 //usleep(10000);
683 for(i = 0; i <= 35; i++){
wangyouqiangece1c722023-12-12 17:28:53 +0800684 ret = mbtk_gpio_adc(0);
wangyouqiang59d8dd22023-10-19 18:03:54 +0800685 usleep(3000);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800686 if(0 == ret){
687 break;
688 }
689 }
b.liu074771a2023-10-19 13:13:39 +0800690 printf("pre set ADC: %d, times: %d\n",ret, j);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800691 if(0 == ret){
692 break;
693 }
694 }
wangyouqiang4c262fa2023-10-17 17:48:03 +0800695 if(0 != ret){
696 printf("Fail, please retest");
697 return -1;
698 }
699
wangyouqiang7850b4a2023-09-18 15:47:57 +0800700
701 /* [2 GPIOTEST] */
702 for(i = 0; i < total; i++){
liubin281ac462023-07-19 14:22:54 +0800703 ret = gpio_register_test_out(test_gpio[i], 0);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800704 if(-1 == ret){
705 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio[i]);
liubin281ac462023-07-19 14:22:54 +0800706 fail_io[n] = test_gpio[i];
707 n++;
708 }else{
wangyouqiang4c262fa2023-10-17 17:48:03 +0800709 //printf("############gpio [%d] test success############\n", test_gpio[i]);
liubin281ac462023-07-19 14:22:54 +0800710 }
711 }
wangyouqiang2b352b22023-10-21 15:18:43 +0800712 mbtk_adc_close();
liubin281ac462023-07-19 14:22:54 +0800713 return n;
714}
b.liu4f8e8792023-11-01 23:25:22 +0800715
716#elif defined(MBTK_PROJECT_L508)
LUOJian943fd402024-02-07 10:54:25 +0800717
gy.tang141ce612024-07-03 11:01:58 +0800718
719#define READ_GPIO_NUM 22
720
721#define DEBUG_GPIO_TEST 1
722
723mbtk_gpio_test_pin_and_gpio *p_test_gpio =NULL;
724mbtk_gpio_test_pin_and_gpio *p_gnss_gpio = NULL;
725int test_gpio_group_total_pin = 0;
726int gnss_gpio_group_total_pin = 0;
727
728int gpio_test_set_group_pin_to_low(mbtk_gpio_test_pin_and_gpio *p_pin_group_array, int total_num)
729{
730 int i;
731 int gpio_num = 0;
732 int ret = 0;
733
734 if(p_pin_group_array == NULL)
735 return -1;
736
737 for(i=0; i < total_num; i++)
738 {
739 gpio_num = p_pin_group_array[i].gpio_num;
740 ret += gpio_register_set_func_0(gpio_num);
741 gpio_register_set_direction(gpio_num, MBTK_GPIO_OUTPUT);
742 gpio_register_set_value(gpio_num, 0);
743 }
744 return ret;
745}
b.liu85565a72023-11-09 16:47:40 +0800746
747
748int gpio_test_init(void)
749{
750 int i,j,ret;
gy.tang141ce612024-07-03 11:01:58 +0800751 int gpio_num = 0;
752
753
754
755
wangyouqiang89888612023-11-28 18:28:12 +0800756 system("i2cset -y -f 2 0x31 0x12 0x46");
gy.tangf4d7b852024-06-27 18:50:14 +0800757 system("echo 53 > /sys/class/gpio/export"); //gnss uart rx
758 system("echo out > /sys/class/gpio/gpio53/direction");
759 system("echo 54 > /sys/class/gpio/export");//gnss uart tx
760 system("echo out > /sys/class/gpio/gpio54/direction");
761 system("echo 22 > /sys/class/gpio/export");//check pin
gy.tang141ce612024-07-03 11:01:58 +0800762
763 gpio_register_set_func_0(READ_GPIO_NUM);
764 gpio_register_set_direction(READ_GPIO_NUM, 1); //输出
gy.tangf4d7b852024-06-27 18:50:14 +0800765
766 usleep(500);
gy.tang141ce612024-07-03 11:01:58 +0800767/*
768 GPIO22默认是上拉,如果改成GPIO输入模式,其PIN脚电压是高电平,必须改成下拉后GPIO22才处于低电平状态,否则测试PIN拉低失败
769*/
770 gpio_register_set_direction(READ_GPIO_NUM, MBTK_GPIO_INPUT); //设置输入模式
gy.tangf4d7b852024-06-27 18:50:14 +0800771 gpio_register_write(GPIO_FUNC_GPIO_22,0xb040); //PULL DOWN
772
773
gy.tang141ce612024-07-03 11:01:58 +0800774 gpio_test_set_group_pin_to_low(p_gnss_gpio,gnss_gpio_group_total_pin); //set all gnss reserver pin to low
775
776
b.liu85565a72023-11-09 16:47:40 +0800777 /* [1 all gpio set to low] */
gy.tang141ce612024-07-03 11:01:58 +0800778 for(j = 0; j < 6 ; j++){
779 gpio_test_set_group_pin_to_low(p_test_gpio,test_gpio_group_total_pin);
b.liu85565a72023-11-09 16:47:40 +0800780
781 for(i = 0; i <= 5; i++){
gy.tang141ce612024-07-03 11:01:58 +0800782 ret = gpio_register_get_value(READ_GPIO_NUM);
b.liu85565a72023-11-09 16:47:40 +0800783 if(0 == ret){
784 break;
785 }
786 usleep(3000);
787 }
gy.tang141ce612024-07-03 11:01:58 +0800788
b.liu85565a72023-11-09 16:47:40 +0800789 if(0 == ret){
790 break;
791 }
792 }
793
794 if(0 != ret){
gy.tang141ce612024-07-03 11:01:58 +0800795 printf("[GPIOTEST]set all low Fail, please retest\n");
b.liu85565a72023-11-09 16:47:40 +0800796 return -1;
797 }
gy.tang141ce612024-07-03 11:01:58 +0800798 printf("[GPIOTEST]pre set SUCCESS\n");
b.liu85565a72023-11-09 16:47:40 +0800799 return 0;
800}
801
gy.tang141ce612024-07-03 11:01:58 +0800802int gpio_test(void* arg,int index,mbtk_gpio_test_pin_and_gpio *p_pin_group_array,int total)
b.liu85565a72023-11-09 16:47:40 +0800803{
gy.tang141ce612024-07-03 11:01:58 +0800804 int ret,i;
wangyouqiang89888612023-11-28 18:28:12 +0800805 int *fail_io = (int *)arg;
gy.tang141ce612024-07-03 11:01:58 +0800806 int failed_num = index;
gy.tangf4d7b852024-06-27 18:50:14 +0800807 int gpio_num = 0;
gy.tang141ce612024-07-03 11:01:58 +0800808 int pin_num = 0;
809 bool falied_flag = FALSE;
810
b.liu85565a72023-11-09 16:47:40 +0800811 for(i = 0; i < total; i++){
gy.tang141ce612024-07-03 11:01:58 +0800812 gpio_num = p_pin_group_array[i].gpio_num;
813 pin_num = p_pin_group_array[i].pin_num;
814 falied_flag = FALSE;
815
816#if DEBUG_GPIO_TEST
817 ret = gpio_register_get_value(gpio_num);
818 printf("[GPIOTEST]pin_num%d default value= %d\n", pin_num, ret);
819#endif
820
b.liu85565a72023-11-09 16:47:40 +0800821 //set high
gy.tangf4d7b852024-06-27 18:50:14 +0800822 gpio_register_set_value(gpio_num, 1);
gy.tang141ce612024-07-03 11:01:58 +0800823
824#if DEBUG_GPIO_TEST
gy.tangf4d7b852024-06-27 18:50:14 +0800825 ret = gpio_register_get_value(gpio_num);
gy.tang141ce612024-07-03 11:01:58 +0800826 printf("[GPIOTEST]pin_num%d set high= %d\n", pin_num, ret);
827#endif
b.liu85565a72023-11-09 16:47:40 +0800828 //get
gy.tang141ce612024-07-03 11:01:58 +0800829 ret = gpio_register_get_value(READ_GPIO_NUM);
b.liu85565a72023-11-09 16:47:40 +0800830 if(1 != ret){
gy.tang141ce612024-07-03 11:01:58 +0800831 printf("[GPIOTEST]pin%d test high failed\n", pin_num);
832 falied_flag = TRUE;
833 }else{
834 printf("[GPIOTEST]pin[%d] test high success\n", pin_num);
835 }
b.liu85565a72023-11-09 16:47:40 +0800836
837 //set low
gy.tang141ce612024-07-03 11:01:58 +0800838 gpio_register_set_value(gpio_num, 0);
839#if DEBUG_GPIO_TEST
840 ret = gpio_register_get_value(gpio_num);
841 printf("[GPIOTEST]pin_num%d set low= %d\n", pin_num, ret);
842#endif
b.liu85565a72023-11-09 16:47:40 +0800843 //get
gy.tang141ce612024-07-03 11:01:58 +0800844 ret = gpio_register_get_value(READ_GPIO_NUM);
b.liu85565a72023-11-09 16:47:40 +0800845 if(0 != ret){
gy.tang141ce612024-07-03 11:01:58 +0800846 printf("[GPIOTEST]pin_num%d test low failed\n", pin_num);
847 falied_flag = TRUE;
848
849 }else{
850 printf("[GPIOTEST]pin_num%d test low success\n", pin_num);
851 }
852
853 if(falied_flag)
854 {
855 fail_io[failed_num] = p_pin_group_array[i].pin_num;
856#if DEBUG_GPIO_TEST
857 printf("[GPIOTEST]NO.%d pin_num%d failed %d\n",i,pin_num, failed_num);
858#endif
859 failed_num++;
gy.tangf4d7b852024-06-27 18:50:14 +0800860 }
b.liu85565a72023-11-09 16:47:40 +0800861 }
gy.tang141ce612024-07-03 11:01:58 +0800862 return failed_num;
b.liu85565a72023-11-09 16:47:40 +0800863}
b.liu85565a72023-11-09 16:47:40 +0800864
b.liu4f8e8792023-11-01 23:25:22 +0800865
gy.tang141ce612024-07-03 11:01:58 +0800866int mbtk_at_gpio(bool gnss_support,void* arg)
b.liu4f8e8792023-11-01 23:25:22 +0800867{
gy.tang141ce612024-07-03 11:01:58 +0800868 int n = 0;
869 mbtk_gpio_test_pin_and_gpio test_gpio[] =
870 {
871 //{GPIO , PIN}
872 {0,100}, //RMII_RX-DV
873 {1,101}, //RMII_RXD0
874 {2,102},//RMII_RXD1
875 {3,103},//RMII_CLK
876 {6,104},//RMII_TXD0
877 {7,105},//RMII_TXD1
878 {15,106},//RMII_TX_EN
879 {16,109},//RMII_MDC
880 {17,110},//RMII_MDIO
881 {18,107},//RMII_INT
882 {99,16},//USB_ID
883 {34,9}, //SPI0_CS
884 {36,8},//SPI0_TXD
885 {35,7},//SPI0_RXD
886 {33,6},//SPI0_CLK
887 {14,51}, //NET_LIGHT
888 {13,49}, //STATUS LED
889 {12,50},//WAKE_IN
890 {49,55}, //I2C SCL
891 {50,56}, //I2C SDA
892 {19,53}, //USIM_DET
893 {43,48}, //SD_DET
894 {118,52}, //WAKE OUT
895 {120,54}, //FLIGHT MODE
896
897
898 {20,45}, //NORMAL GPIO
899 {59,122}, //SDIO_CLK
900 {56,117}, //SDIO_DATA1
901 {55,118},//SDIO_DATA2
902 {58,119},//SDIO_CMD
903 {48,120},//SDIO_DATA3
904 {57,121},//SDIO_DATA0
905 {5,84}, //NORMAL GPIO
906 {4,30},//NORMAL GPIO
907 {21,28}, //NORMAL GPIO
908 {23,69}, //UART_RING
909 {24,70}, //UART_DCD
910 {27,73}, //PCM_OUT
911 {28,74}, //PCM_IN
912 {26,75}, //PCM_SYNC
913 {25,76}, //PCM_CLK
914
915 {126,112},//NORMAL GPIO or pwm3
916 {117,123}, //PWM
917 {125,116}, //CLK_REQ
918
919 //{54,114}, //debug uart2, no need be tested
920 //{53,115}, //debug uart2, no need be tested
921
922 };
923
924 mbtk_gpio_test_pin_and_gpio GNSS_test_gpio[] = {
925 {54,114}, //GPS UTXD, UART1_TXD,L508LAN+L508CN(D)+L508EN+L508LEN+L508TLCN NOT CONNECTED
926 {53,115}, //GPS URXD,UART1-RXD,L508LAN+L508CN(D)+L508EN+L508LEN+L508TLCN NOT CONNECTED
927 {32,66}, //PIN66,host wake GPS, RTS, L508TLCN_V2+L508LEN_V2+L508EN_V2 NOT CONNECTED
928 {31,67}, //PIN67,GPS wake Host,CTS, L508TLCN_V2+L508LEN_V2+L508EN_V2 NOT CONNECTED
929 {123,42},//GPS_EN, L508TLCN_V2+L508LEN_V2+L508EN_V2 NOT CONNECTED
930 {122,35}, //32K OUT, L508TLCN_V2+L508LEN_V2+L508EN_V2 not connected
931 };
932 p_gnss_gpio = GNSS_test_gpio;
933 gnss_gpio_group_total_pin = (sizeof(GNSS_test_gpio)/sizeof(GNSS_test_gpio[0]));
934
935 p_test_gpio = test_gpio;
936 test_gpio_group_total_pin = (sizeof(test_gpio)/sizeof(test_gpio[0]));
937
938 if(!gnss_support)
939 n = test_gpio_group_total_pin+gnss_gpio_group_total_pin;
940 else
941 n = test_gpio_group_total_pin;
942
943 printf("[init] L508 XX GPIOTEST v1.3 total pin=%d\n",n);
b.liu85565a72023-11-09 16:47:40 +0800944
LUOJian943fd402024-02-07 10:54:25 +0800945 n = gpio_test_init();
gy.tangf4d7b852024-06-27 18:50:14 +0800946 if(-1 == n)
947 {
gy.tang141ce612024-07-03 11:01:58 +0800948 goto gpiotest_finished;
gy.tangf4d7b852024-06-27 18:50:14 +0800949 }
gy.tang141ce612024-07-03 11:01:58 +0800950 n = 0;
b.liu85565a72023-11-09 16:47:40 +0800951
gy.tang141ce612024-07-03 11:01:58 +0800952 n = gpio_test(arg,0,p_test_gpio,test_gpio_group_total_pin);
953
954 //if the module has gnss chip, don't test the gnss's pins
955 if(!gnss_support)
956 {
957 n = gpio_test(arg,n,p_gnss_gpio,gnss_gpio_group_total_pin);
958 }
959
960gpiotest_finished:
gy.tangf4d7b852024-06-27 18:50:14 +0800961 system("echo 53 > /sys/class/gpio/unexport"); //unexport gnss uart rx
962 system("echo 54 > /sys/class/gpio/unexport");//unexportgnss uart tx
963 system("echo 22 > /sys/class/gpio/unexport");//unexportcheck pin
964
gy.tang141ce612024-07-03 11:01:58 +0800965 printf("\n[GPIOTEST] Finished %d!!\n\n",n);
b.liu85565a72023-11-09 16:47:40 +0800966 return n;
b.liu4f8e8792023-11-01 23:25:22 +0800967}
968
b.liu85565a72023-11-09 16:47:40 +0800969
970
b.liu4f8e8792023-11-01 23:25:22 +0800971#elif defined(MBTK_PROJECT_L509)
b.liu85565a72023-11-09 16:47:40 +0800972//测低
luojianc08d4e62024-07-05 17:12:02 +0800973static uint16 mbtk_gpio_test_all_low(mbtk_gpio_paired_info_struct *pin_array, uint16 MAX_pin_num)
b.liu85565a72023-11-09 16:47:40 +0800974{
luojianc08d4e62024-07-05 17:12:02 +0800975 uint16 index = 0;
b.liu85565a72023-11-09 16:47:40 +0800976 uint16 OUT_pin, Read_pin;
977 uint32 read_high_value;
978 uint32 read_low_value;
luojianc08d4e62024-07-05 17:12:02 +0800979 uint16 fail_num = 0;
980 //int res;
b.liu85565a72023-11-09 16:47:40 +0800981
gy.tang71ea7ff2024-06-21 16:01:56 +0800982 printf("[GPIOTEST][run test all pin low]\n");
b.liu85565a72023-11-09 16:47:40 +0800983
b.liu85565a72023-11-09 16:47:40 +0800984 for(index = 0; index < MAX_pin_num; index++){
gy.tang71ea7ff2024-06-21 16:01:56 +0800985 OUT_pin = pin_array[index].output_gpio;
986 Read_pin = pin_array[index].input_gpio;
b.liu85565a72023-11-09 16:47:40 +0800987
988 //输出低
luojianc08d4e62024-07-05 17:12:02 +0800989 gpio_register_set_direction(OUT_pin, 1);
b.liu85565a72023-11-09 16:47:40 +0800990 gpio_register_set_value(OUT_pin, 0);
991
992 //输入脚设为输入
993 gpio_register_set_direction(Read_pin,0);
994 }
995
996 for(index = 0; index < MAX_pin_num; index++){
gy.tang71ea7ff2024-06-21 16:01:56 +0800997 OUT_pin = pin_array[index].output_gpio;
998 Read_pin = pin_array[index].input_gpio;
b.liu85565a72023-11-09 16:47:40 +0800999 read_low_value = gpio_register_get_value(Read_pin);
luojianc08d4e62024-07-05 17:12:02 +08001000
b.liu85565a72023-11-09 16:47:40 +08001001 //结果检测
1002 if(read_low_value != 0){
luojianc08d4e62024-07-05 17:12:02 +08001003 printf("[GPIOTEST][!Low Failed!]: GPIO%d PIN%d\n", Read_pin, pin_array[index].input_pin);
b.liu85565a72023-11-09 16:47:40 +08001004 pin_array[index].mbtk_gpio_test_result = 1;
1005 fail_num ++;
1006 }
gy.tang71ea7ff2024-06-21 16:01:56 +08001007#if DEBUG_GPIO_TEST
luojianc08d4e62024-07-05 17:12:02 +08001008 else{
1009 printf("[GPIOTEST][-Low Success-]: GPIO%d \n", Read_pin);
b.liu85565a72023-11-09 16:47:40 +08001010 }
luojianc08d4e62024-07-05 17:12:02 +08001011#endif
b.liu85565a72023-11-09 16:47:40 +08001012 }
1013 return fail_num;
b.liu85565a72023-11-09 16:47:40 +08001014}
1015
1016
b.liu85565a72023-11-09 16:47:40 +08001017//测高
luojianc08d4e62024-07-05 17:12:02 +08001018static uint16 mbtk_gpio_test_pin_high(mbtk_gpio_paired_info_struct *pin_array, uint16 MAX_pin_num)
b.liu85565a72023-11-09 16:47:40 +08001019{
luojianc08d4e62024-07-05 17:12:02 +08001020 uint16 index = 0;
b.liu85565a72023-11-09 16:47:40 +08001021 uint16 OUT_pin, Read_pin;
1022 uint32 read_high_value;
1023 uint32 read_low_value;
luojianc08d4e62024-07-05 17:12:02 +08001024 uint16 fail_num = 0;
b.liu85565a72023-11-09 16:47:40 +08001025 int i = 0;
1026
gy.tang71ea7ff2024-06-21 16:01:56 +08001027 printf("[GPIOTEST][run test pin high]\n");
b.liu85565a72023-11-09 16:47:40 +08001028
luojianc08d4e62024-07-05 17:12:02 +08001029 for(index = 0; index < MAX_pin_num; index++){
gy.tang71ea7ff2024-06-21 16:01:56 +08001030 OUT_pin = pin_array[index].output_gpio;
1031 Read_pin = pin_array[index].input_gpio;
b.liu85565a72023-11-09 16:47:40 +08001032
1033 //设高
1034 gpio_register_set_direction(OUT_pin,1);
1035 gpio_register_set_direction(Read_pin,0);
1036 gpio_register_set_value(OUT_pin, 1);
luojianc08d4e62024-07-05 17:12:02 +08001037 usleep(10000);
b.liu85565a72023-11-09 16:47:40 +08001038 read_high_value = gpio_register_get_value(Read_pin);
gy.tang71ea7ff2024-06-21 16:01:56 +08001039#if DEBUG_GPIO_TEST
1040 printf("[GPIOTEST][test_high]: PIN%d: get value =%d \n", pin_array[index].input_pin, read_high_value);
1041#endif
b.liu85565a72023-11-09 16:47:40 +08001042
1043 //设高后重新设低
luojianc08d4e62024-07-05 17:12:02 +08001044 usleep(10000);
b.liu85565a72023-11-09 16:47:40 +08001045 gpio_register_set_value(OUT_pin, 0);
luojianc08d4e62024-07-05 17:12:02 +08001046 usleep(10000);
1047 for(i = 0; i < 10; i++){
b.liu85565a72023-11-09 16:47:40 +08001048 read_low_value = gpio_register_get_value(Read_pin);
1049 if(0 == read_low_value){
1050 break;
1051 }
1052 usleep(20000);
1053 }
gy.tang71ea7ff2024-06-21 16:01:56 +08001054#if DEBUG_GPIO_TEST
luojianc08d4e62024-07-05 17:12:02 +08001055 printf("[GPIOTEST][test_low]: PIN%d: get value =%d \n", pin_array[index].input_pin, read_low_value);
gy.tang71ea7ff2024-06-21 16:01:56 +08001056#endif
luojianc08d4e62024-07-05 17:12:02 +08001057 if(read_high_value != 1 || read_low_value != 0){
1058 printf("[GPIOTEST][!High Failed!]: Gpio%d PIN%d\n", Read_pin,pin_array[index].input_pin);
1059 pin_array[index].mbtk_gpio_test_result = 2;
1060 fail_num ++;
b.liu85565a72023-11-09 16:47:40 +08001061 }
gy.tang71ea7ff2024-06-21 16:01:56 +08001062#if DEBUG_GPIO_TEST
luojianc08d4e62024-07-05 17:12:02 +08001063 else{
gy.tang71ea7ff2024-06-21 16:01:56 +08001064 printf("[GPIOTEST][-High Success-]: Gpio%d \n", Read_pin);
luojianc08d4e62024-07-05 17:12:02 +08001065 }
gy.tang71ea7ff2024-06-21 16:01:56 +08001066#endif
b.liu85565a72023-11-09 16:47:40 +08001067 }
luojianc08d4e62024-07-05 17:12:02 +08001068 return fail_num;
b.liu85565a72023-11-09 16:47:40 +08001069}
luojianc08d4e62024-07-05 17:12:02 +08001070
1071
1072static void gpio_test_init_test_gpio_mode(mbtk_gpio_paired_info_struct pin_array[], int MAX_pin_num)
b.liu85565a72023-11-09 16:47:40 +08001073{
luojianc08d4e62024-07-05 17:12:02 +08001074 int index = 0;
1075 int OUT_pin, Read_pin;
b.liu85565a72023-11-09 16:47:40 +08001076
luojianc08d4e62024-07-05 17:12:02 +08001077 printf("[GPIOTEST] L509 GPIOTEST v2.2 --init\n");
1078
1079 gpio_register_set_func_0(125);
1080 gpio_register_set_direction(125, 1);
1081 gpio_register_set_value(125, 1); //RS2299开关的使能脚
gy.tang71ea7ff2024-06-21 16:01:56 +08001082
luojianc08d4e62024-07-05 17:12:02 +08001083 system("i2cset -y -f 2 0x31 0x12 0x46"); //使能0x12地址的VLDO, 某组GPIO的供电
1084
1085 gpio_register_write(GPIO_FUNC_GPIO_53, 0xb040); //GPIO53 DTR set PULL DOWN
1086
1087 for(index = 0; index < MAX_pin_num; index++){
1088 OUT_pin = pin_array[index].output_gpio;
1089 Read_pin = pin_array[index].input_gpio;
1090 pin_array[index].mbtk_gpio_test_result = 0; //init as passed
1091 gpio_register_set_func_0(OUT_pin);
1092 gpio_register_set_func_0(Read_pin);
1093 gpio_register_set_value(OUT_pin, 0);
1094 }
1095
gy.tang71ea7ff2024-06-21 16:01:56 +08001096#if DEBUG_GPIO_TEST
1097 printf("[GPIOTEST]gpio_test_init_test_gpio_mode FINISH\n");
1098#endif
b.liu85565a72023-11-09 16:47:40 +08001099
1100}
1101
b.liu4f8e8792023-11-01 23:25:22 +08001102
b.liu94091352024-07-02 10:38:08 +08001103int mbtk_at_gpio(bool gnss_support, void* arg)
b.liu4f8e8792023-11-01 23:25:22 +08001104{
gy.tang71ea7ff2024-06-21 16:01:56 +08001105 int i, num;
1106 int *fail_io = (int *)arg;
1107 /*OPEN SWITCH */
1108 uint16 test_fail_count = 0;
1109 uint16 test_MAX_pin_num = 0;
1110 char buf[STR_MAX_LEN];
luojianc08d4e62024-07-05 17:12:02 +08001111 //uint16 str_len = 0;
gy.tang71ea7ff2024-06-21 16:01:56 +08001112 uint16 temp_len =0;
1113 uint16 Out_pin;
1114 uint16 in_pin;
luojianc08d4e62024-07-05 17:12:02 +08001115 //uint16 fail_print_num = 0;
1116
1117 mbtk_gpio_paired_info_struct test_pin_array[] ={
1118 //output_pin, output_gpio, input_gpio, input_pin, test_result
1119 {122, 16, 17, 121, 0}, /* RGMII/RMII_MD_CLK <---SW RS2299---> RGMII/RMII_MD_IO */
1120
1121 {78, 7, 2, 73, 0}, /* RGMII/RMII_TX_1 <---SW RS2299---> RGMII/RMII_RXD1 */
1122 {77, 6, 1, 76, 0}, /* RGMII/RMII_TXD0 <---SW RS2299---> RGMII/RMII_RXD0 */
1123 {81, 15, 3, 75, 0}, /* RGMII/RMII_TX_EN <---SW RS2299---> RGMII/RMII_CLK */
1124 {74, 0, 18, 120, 0}, /* RGMII/RMII_RX_DV <---SW RS2299---> RGMII/RMII_INT */
1125
1126 {1, 117, 120, 2, 0}, /* WAKEUP_IN <---SW RS2299---> AP_READY */
1127 {4, 118, 19, 13, 0}, /* FLIGHTMODE <---SW RS2299---> USIM_CD */
1128 {5, 123, 126, 6, 0}, /* NET_MODE <---SW RS2299---> NET_STATUS */
1129 {23, 43, 23, 62, 0}, /* MMC1_CD <---SW RS2299---> UART_RI */
1130
1131 {79, 4, 5, 82, 0}, /* RGMII_RX_2 <---SW RS2299---> RGMII_RX_3 */
1132 {80, 13, 14, 84, 0}, /* RGMII_TX_2 <---SW RS2299---> RGMII_TX_3 */
1133 {129, 48, 55, 130, 0}, /* WLAN_DAT3 <---SW RS2299---> WLAN_DAT2 */
1134 {131, 56, 57, 132, 0}, /* WLAN_DAT1 <---SW RS2299---> WLAN_DAT0 */
1135
1136 {24, 28, 27, 25, 0}, /* PCM_IN <---SW RS2299---> PCM_OUT */
1137 {26, 26, 25, 27, 0}, /* PCM_SYNC <---SW RS2299---> PCM_CLK */
1138 {37, 34, 33, 40, 0}, /* SPI_CS <---SW RS2299---> SPI_CLK */
1139 {38, 36, 35, 39, 0}, /* SPI_DOUT <---SW RS2299---> SPI_DIN */
1140
1141 {133, 59, 58, 134, 0}, /* WLAN_CLK/PCIE_RSTN <---SW RS2299---> WLAN_CMD/PCIE_WK */
1142 {135, 21, 99, 139, 0}, /* WLAN_WAKE_HOST <---SW RS2299---> USB_ID */
1143 {119, 20, 22, 136, 0}, /* RGMII/RMII_RST_N <---SW RS2299---> WLAN_EN */
1144 {83, 12, 122, 118, 0}, /* RGMII_CLK_TX <---SW RS2299---> WLAN_SLP_CLK */
1145
1146 {41, 49, 50, 42, 0}, /* I2C_SCL <---SW RS2299---> I2C_SDA */
1147 {143, 10, 11, 144, 0}, /* GRFC1 <---SW RS2299---> GRFC2 */
1148 {64, 32, 31, 65, 0}, /* UART_CTS <---SW RS2299---> UART_RTS */
1149 {63, 54, 53, 66, 0}, /* UART_DCD <---SW RS2299---> UART_DTR */
1150 };
gy.tang71ea7ff2024-06-21 16:01:56 +08001151
b.liu85565a72023-11-09 16:47:40 +08001152
luojianc08d4e62024-07-05 17:12:02 +08001153 /* [1]初始化待测GPIO */
1154 test_MAX_pin_num = sizeof(test_pin_array) / sizeof(test_pin_array[0]);
1155 if(!gnss_support){ //TODO: When the hardware does not have GNSS, do not test GPIO53, 54
1156 //bad implement, need modify
1157 test_MAX_pin_num = test_MAX_pin_num - 1;
1158 }
1159 gpio_test_init_test_gpio_mode(test_pin_array, test_MAX_pin_num);
b.liu85565a72023-11-09 16:47:40 +08001160
1161
luojianc08d4e62024-07-05 17:12:02 +08001162 /* [2]测试过程 */
1163 test_fail_count = mbtk_gpio_test_all_low(test_pin_array, test_MAX_pin_num);
b.liu85565a72023-11-09 16:47:40 +08001164
luojianc08d4e62024-07-05 17:12:02 +08001165 if(!test_fail_count){ //set all pin to low success
1166 printf("[GPIOTEST]set all pin to low: success\n");
1167 test_fail_count = mbtk_gpio_test_pin_high(&test_pin_array, test_MAX_pin_num);
gy.tang71ea7ff2024-06-21 16:01:56 +08001168 }
luojianc08d4e62024-07-05 17:12:02 +08001169 else{
1170 printf("[GPIOTEST]set all pin low: failed num=%d!!!\n", test_fail_count);
gy.tang71ea7ff2024-06-21 16:01:56 +08001171 }
b.liu85565a72023-11-09 16:47:40 +08001172
luojianc08d4e62024-07-05 17:12:02 +08001173
gy.tang71ea7ff2024-06-21 16:01:56 +08001174 //memset(buf,0,STR_MAX_LEN);
luojianc08d4e62024-07-05 17:12:02 +08001175 /* [3]测试结果检测与上报 */
1176 if(test_fail_count ){
gy.tang71ea7ff2024-06-21 16:01:56 +08001177 //printf( "GPIOTEST Fail %02d PINs:\n", test_fail_count*2);
luojianc08d4e62024-07-05 17:12:02 +08001178 sprintf(buf, "GPIOTEST Fail %02d PINs:", test_fail_count * 2);
gy.tang71ea7ff2024-06-21 16:01:56 +08001179 temp_len = strlen(buf);
b.liu85565a72023-11-09 16:47:40 +08001180
gy.tang71ea7ff2024-06-21 16:01:56 +08001181 num = 0;
luojianc08d4e62024-07-05 17:12:02 +08001182 for(i = 0; i < test_MAX_pin_num; i++){
1183 if(test_pin_array[i].mbtk_gpio_test_result){
gy.tang71ea7ff2024-06-21 16:01:56 +08001184 Out_pin = test_pin_array[i].output_pin;
1185 in_pin = test_pin_array[i].input_pin;
1186 fail_io[num++] = Out_pin;
1187 fail_io[num++] = in_pin;
gy.tang71ea7ff2024-06-21 16:01:56 +08001188 }
1189 }
1190 }
b.liu85565a72023-11-09 16:47:40 +08001191
luojianc08d4e62024-07-05 17:12:02 +08001192 else{
1193 printf(buf, "ALL GPIO TEST PASS\r\n");
gy.tang71ea7ff2024-06-21 16:01:56 +08001194 }
luojianc08d4e62024-07-05 17:12:02 +08001195
1196 printf("\n[GPIOTEST] Finished !!\n\n");
wangyouqiang97a792f2024-04-01 13:28:06 +08001197 return test_fail_count * 2;
b.liu4f8e8792023-11-01 23:25:22 +08001198}
1199
luojianc08d4e62024-07-05 17:12:02 +08001200
wangyouqiangece1c722023-12-12 17:28:53 +08001201#elif defined(MBTK_PROJECT_L508_X6)
r.xiaoaeb54ee2024-07-12 02:00:37 -07001202int gpio_register_test_out_0(int port)
wangyouqiangece1c722023-12-12 17:28:53 +08001203{
1204 int ret;
1205 int i;
1206 int valueh = 0;
1207 int valuel = 1;
1208
r.xiaoaeb54ee2024-07-12 02:00:37 -07001209 //printf("Gpio port [%d] test start!\n", port);
wangyouqiangece1c722023-12-12 17:28:53 +08001210 ret = gpio_register_set_func_0(port); //设功能为GPIO
1211 if(ret){
1212 printf("gpio_port can't support!\n");
1213 return -1;
1214 }
1215 gpio_register_set_direction(port, 1); //设方向为输出
r.xiaoaeb54ee2024-07-12 02:00:37 -07001216 //ret = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001217 //printf("gpio default value is : %d.\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001218
1219
1220 //[High]
1221 for(i = 0; i <= 10; i++){
1222 gpio_register_set_value(port, 1);
1223 usleep(50);
1224 valueh = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001225 //printf("set high? %d\n",valueh);
wangyouqiangece1c722023-12-12 17:28:53 +08001226 if(1 == valueh){
1227 break;
1228 }
1229 }
r.xiaoaeb54ee2024-07-12 02:00:37 -07001230 usleep(5000);
1231 for(i = 0; i <= 10; i++){
wangyouqiangece1c722023-12-12 17:28:53 +08001232 ret = mbtk_gpio_adc(0);
1233 usleep(3000);
1234 if(1 == ret){
1235 break;
1236 }
1237 }
b.liuc7ffd092024-01-03 15:23:07 +08001238 //printf("******gpio should is high: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001239 if(1 != ret){
1240 ret=-1;
1241 goto exit;
1242 }
1243
1244
1245
1246 //[Low]
1247 usleep(200);
1248 for(i = 0; i <= 10; i++){
1249 gpio_register_set_value(port, 0); //输出低
1250 usleep(50);
1251 valuel = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001252 //printf("set low? %d\n",valuel);
wangyouqiangece1c722023-12-12 17:28:53 +08001253 if(0 == valuel){
1254 break;
1255 }
1256 }
r.xiaoaeb54ee2024-07-12 02:00:37 -07001257 usleep(5000);
1258 for(i = 0; i <= 10; i++){
wangyouqiangece1c722023-12-12 17:28:53 +08001259 ret = mbtk_gpio_adc(0);
1260 usleep(3000);
1261 if(0 == ret){
1262 break;
1263 }
1264 }
r.xiaoaeb54ee2024-07-12 02:00:37 -07001265 printf("******gpio should is low: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001266 if(0 != ret){
1267 ret=-1;
1268 goto exit;
1269 }
1270
1271exit:
1272 gpio_register_set_value(port, 0);
r.xiaoaeb54ee2024-07-12 02:00:37 -07001273 //gpio_register_set_direction(port, 0); //设方向为输入
wangyouqiangece1c722023-12-12 17:28:53 +08001274 return ret;
1275}
1276
r.xiaoaeb54ee2024-07-12 02:00:37 -07001277int gpio_register_test_out_1(int port)
wangyouqiangece1c722023-12-12 17:28:53 +08001278{
1279 int ret;
1280 int i;
1281 int valueh = 0;
1282 int valuel = 1;
1283
r.xiaoaeb54ee2024-07-12 02:00:37 -07001284 //printf("Gpio port [%d] test start!\n", port);
wangyouqiangece1c722023-12-12 17:28:53 +08001285 ret = gpio_register_set_func_0(port); //设功能为GPIO
1286 if(ret){
1287 printf("gpio_port can't support!\n");
1288 return -1;
1289 }
1290 gpio_register_set_direction(port, 1); //设方向为输出
r.xiaoaeb54ee2024-07-12 02:00:37 -07001291 //ret = gpio_register_get_value(port);
1292 //printf("gpio default value is : %d.\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001293
1294
1295 //[High]
1296 for(i = 0; i <= 10; i++){
1297 gpio_register_set_value(port, 1);
1298 usleep(50);
1299 valueh = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001300 //printf("set high? %d\n",valueh);
wangyouqiangece1c722023-12-12 17:28:53 +08001301 if(1 == valueh){
1302 break;
1303 }
1304 }
r.xiaoaeb54ee2024-07-12 02:00:37 -07001305 usleep(5000);
1306 for(i = 0; i <= 10; i++){
wangyouqiangece1c722023-12-12 17:28:53 +08001307 ret = mbtk_gpio_adc(1);
1308 usleep(3000);
1309 if(1 == ret){
1310 break;
1311 }
1312 }
1313 if(1 != ret){
1314 ret=-1;
1315 goto exit;
1316 }
b.liuc7ffd092024-01-03 15:23:07 +08001317 //printf("******gpio should is high: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001318
1319
1320 //[Low]
1321 usleep(200);
1322 for(i = 0; i <= 10; i++){
1323 gpio_register_set_value(port, 0); //输出低
1324 usleep(50);
1325 valuel = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001326 //printf("set low? %d\n",valuel);
wangyouqiangece1c722023-12-12 17:28:53 +08001327 if(0 == valuel){
1328 break;
1329 }
1330 }
r.xiaoaeb54ee2024-07-12 02:00:37 -07001331 usleep(10000);
1332 for(i = 0; i <= 10; i++){
wangyouqiangece1c722023-12-12 17:28:53 +08001333 ret = mbtk_gpio_adc(1);
1334 usleep(3000);
1335 if(0 == ret){
1336 break;
1337 }
1338 }
b.liuc7ffd092024-01-03 15:23:07 +08001339 //printf("******gpio should is low: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001340 if(0 != ret){
1341 ret=-1;
1342 goto exit;
1343 }
1344
1345exit:
wangyouqiang7775cbf2023-12-29 16:35:24 +08001346 gpio_register_set_value(port, 0);
r.xiaoaeb54ee2024-07-12 02:00:37 -07001347 //error: no need set direction in gpio_register_set_direction(port, 0);
wangyouqiangece1c722023-12-12 17:28:53 +08001348 return ret;
1349}
1350
wangyouqiang7775cbf2023-12-29 16:35:24 +08001351
r.xiaoaeb54ee2024-07-12 02:00:37 -07001352int gpio_register_test_out_2(int port_in, int port_out)
wangyouqiang7775cbf2023-12-29 16:35:24 +08001353{
1354 int ret_in;
1355 int ret_out;
1356 int i;
1357 int valueh = 0;
1358 int valuel = 1;
1359
r.xiaoaeb54ee2024-07-12 02:00:37 -07001360 //printf("Gpio port [%d] test start!\n", port_out);
wangyouqiang7775cbf2023-12-29 16:35:24 +08001361 ret_in = gpio_register_set_func_0(port_in); //设功能为GPIO
1362 ret_out = gpio_register_set_func_0(port_out);
1363 if((ret_in+ret_out)){
1364 printf("gpio_port can't support!\n");
1365 return -1;
1366 }
1367 gpio_register_set_direction(port_out, 1); //设方向为输出
1368 gpio_register_set_direction(port_in, 0); //设方向为输入
r.xiaoaeb54ee2024-07-12 02:00:37 -07001369 //ret_in = gpio_register_get_value(port_in);
1370 //printf("gpio default value is : %d.\n", ret_in);
wangyouqiang7775cbf2023-12-29 16:35:24 +08001371
1372
1373 //[High]
1374 for(i = 0; i <= 10; i++){
1375 gpio_register_set_value(port_out, 1);
1376 usleep(50);
1377 valueh = gpio_register_get_value(port_out);
r.xiaoaeb54ee2024-07-12 02:00:37 -07001378 //printf("set high? %d\n",valueh);
wangyouqiang7775cbf2023-12-29 16:35:24 +08001379 if(1 == valueh){
1380 break;
1381 }
1382 }
1383
r.xiaoaeb54ee2024-07-12 02:00:37 -07001384 usleep(5000);
1385 for(i = 0; i <= 10; i++){
wangyouqiang7775cbf2023-12-29 16:35:24 +08001386 ret_in = gpio_register_get_value(port_in);
1387 usleep(3000);
1388 if(1 == ret_in){
1389 break;
1390 }
1391 }
1392 if(1 != ret_in){
1393 ret_in=-1;
1394 printf("get high failed! \n");
1395 goto exit;
1396 }
r.xiaoaeb54ee2024-07-12 02:00:37 -07001397 //printf("******gpio should is high: %d.******\n", ret_in);
wangyouqiang7775cbf2023-12-29 16:35:24 +08001398
1399
1400 //[Low]
1401 usleep(200);
1402 for(i = 0; i <= 10; i++){
1403 gpio_register_set_value(port_out, 0); //输出低
1404 usleep(50);
1405 valuel = gpio_register_get_value(port_out);
r.xiaoaeb54ee2024-07-12 02:00:37 -07001406 //printf("set low? %d\n",valuel);
wangyouqiang7775cbf2023-12-29 16:35:24 +08001407 if(0 == valuel){
1408 break;
1409 }
1410 }
1411
r.xiaoaeb54ee2024-07-12 02:00:37 -07001412 usleep(5000);
1413 for(i = 0; i <= 10; i++){
wangyouqiang7775cbf2023-12-29 16:35:24 +08001414 ret_in = gpio_register_get_value(port_in);
1415 usleep(3000);
1416 if(0 == ret_in){
1417 break;
1418 }
1419 }
r.xiaoaeb54ee2024-07-12 02:00:37 -07001420 //printf("******gpio should is low: %d.******\n", ret_in);
wangyouqiang7775cbf2023-12-29 16:35:24 +08001421 if(0 != ret_in){
1422 ret_in=-1;
1423 printf("get low failed! \n");
1424 goto exit;
1425 }
1426
1427exit:
1428 gpio_register_set_value(port_out, 0);
r.xiaoaeb54ee2024-07-12 02:00:37 -07001429 //gpio_register_set_direction(port_out, 0);
wangyouqiang7775cbf2023-12-29 16:35:24 +08001430 return ret_in;
1431}
1432
1433
r.xiaoaeb54ee2024-07-12 02:00:37 -07001434static int gpio_test_init_test_gpio_mode(void )
wangyouqiangece1c722023-12-12 17:28:53 +08001435{
r.xiaoaeb54ee2024-07-12 02:00:37 -07001436 const int test_gpio_1[] = {
1437 40, 33, 34, 39, 99, 10, 11, 88, 87, 86, 85, 84, 89,
1438 56, 55, 58, 57, 48, 59, 10, 90, 20, 53, 19, 46, 127,
1439 49, 50, 32, 31, 6, 7
wangyouqiangece1c722023-12-12 17:28:53 +08001440 };
1441
r.xiaoaeb54ee2024-07-12 02:00:37 -07001442 const int test_gpio_0[] = {
1443 8, 27, 28, 26, 25, 15, 1, 3, 0,
wangyouqiangece1c722023-12-12 17:28:53 +08001444 37, 38,35, 36, 42, 41, 21, 22, 24, 23,
1445 54, 125, 18, 13, 14, 17, 16, 5, 4, 2, 12,
r.xiaoaeb54ee2024-07-12 02:00:37 -07001446 43, 44, 45, 47, 117, 119
wangyouqiangece1c722023-12-12 17:28:53 +08001447 };
1448 int i, j, ret_0, ret_1, total_1, total_0, n = 0;
1449 total_0 = (sizeof(test_gpio_0)/sizeof(int));
1450 total_1 = (sizeof(test_gpio_1)/sizeof(int));
1451
wangyouqiang7775cbf2023-12-29 16:35:24 +08001452 //system("at at+gpsinit=0");
1453 system("i2cset -y -f 2 0x32 0x0d 0x00");
1454 system("i2cset -y -f 2 0x31 0x18 0x8f");
1455 usleep(10000);
wangyouqiangece1c722023-12-12 17:28:53 +08001456
1457 //printf("[init]gpio_test_init_test_gpio_mode FINISH\n");
1458 /* [1 all gpio set to low] */
1459 for(j = 0; j < 6 ; j++){
r.xiaoaeb54ee2024-07-12 02:00:37 -07001460 for(i = 0; i < total_0; i++){
1461 gpio_register_set_func_0(test_gpio_0[i]);
1462 gpio_register_set_direction(test_gpio_0[i], 1);
1463 gpio_register_set_value(test_gpio_0[i], 0);
1464 }
wangyouqiangece1c722023-12-12 17:28:53 +08001465 for(i = 0; i < total_1; i++){
1466 gpio_register_set_func_0(test_gpio_1[i]);
1467 gpio_register_set_direction(test_gpio_1[i], 1);
1468 gpio_register_set_value(test_gpio_1[i], 0);
1469 }
r.xiaoaeb54ee2024-07-12 02:00:37 -07001470
1471 usleep(50000);
1472 for(i = 0; i <= 10; i++){
1473 ret_0 = mbtk_gpio_adc(0);
1474 usleep(3000);
1475 ret_1 = mbtk_gpio_adc(1);
1476
1477 if(ret_0 ==0 && ret_1 == 0){
1478 break;
1479 }
wangyouqiangece1c722023-12-12 17:28:53 +08001480 }
r.xiaoaeb54ee2024-07-12 02:00:37 -07001481 printf("pre set ADC0: %d, ADC1: %d, times: %d\n", ret_0, ret_1, j);
1482 if(ret_0 ==0 && ret_1 == 0){
1483 break;
1484 }
wangyouqiangece1c722023-12-12 17:28:53 +08001485 }
r.xiaoaeb54ee2024-07-12 02:00:37 -07001486 if(!(ret_0 ==0 && ret_1 == 0)){
wangyouqiang7775cbf2023-12-29 16:35:24 +08001487
wangyouqiangece1c722023-12-12 17:28:53 +08001488 printf("Fail, please retest");
1489 return -1;
1490 }
wangyouqiang7775cbf2023-12-29 16:35:24 +08001491 gpio_register_set_value(119, 1);
1492 gpio_register_set_value(117, 1);
r.xiaoaeb54ee2024-07-12 02:00:37 -07001493 return 0;
wangyouqiangece1c722023-12-12 17:28:53 +08001494}
1495
b.liu94091352024-07-02 10:38:08 +08001496int mbtk_at_gpio(bool gnss_support, void* arg)
wangyouqiangece1c722023-12-12 17:28:53 +08001497{
r.xiaoaeb54ee2024-07-12 02:00:37 -07001498 const int test_gpio_0[] = {
1499 35, 93,
1500 36, 94,
1501 42, 97,
1502 41, 98,
1503 21, 100,
1504 22, 101,
1505 24, 102,
1506 23, 103,
1507 54, 107,
1508 125, 108,
1509 18, 109,
1510 13, 114,
1511 14, 115,
1512 17, 116,
1513 16, 117,
1514 5, 119,
1515 4, 127,
1516 2, 120,
1517 12, 126,
1518 0, 90,
1519 27, 73,
1520 28, 74,
1521 26, 75,
1522 25, 76,
1523 15, 86,
1524 1, 88,
1525 3, 89,
1526 37, 91,
1527 38, 92,
1528 8, 72
wangyouqiangece1c722023-12-12 17:28:53 +08001529 };
1530
r.xiaoaeb54ee2024-07-12 02:00:37 -07001531 const int test_gpio_1[] = {
1532 40, 6,
1533 33, 7,
1534 34, 8,
1535 39, 9,
1536 99, 16,
1537 56, 27,
1538 58, 29,
1539 55, 28,
1540 57, 30,
1541 48, 31,
1542 59, 32,
1543 20, 49,
1544 53, 50,
1545 19, 51,
1546 49, 55,
1547 50, 56,
1548 32, 66,
1549 31, 67,
1550 6, 68,
1551 11, 83,
1552 10, 84,
1553 7, 69,
1554 90, 48,
1555 46, 52,
1556 127, 54,
1557 88, 21,
1558 87, 22,
1559 86, 23,
1560 85, 24,
1561 84, 25,
1562 89, 26,
wangyouqiang7775cbf2023-12-29 16:35:24 +08001563 };
r.xiaoaeb54ee2024-07-12 02:00:37 -07001564
1565 const int test_gpio_2[] = { //GPS组
1566 43, 53,
1567 47, 95,
1568 45, 71,
1569 44, 70,
wangyouqiangece1c722023-12-12 17:28:53 +08001570 };
1571
wangyouqiang7775cbf2023-12-29 16:35:24 +08001572 int i, j, ret_0, ret_1, ret_2, total_1, total_0, total_2, n = 0;
wangyouqiangece1c722023-12-12 17:28:53 +08001573 int *fail_io = (int *)arg;
r.xiaoaeb54ee2024-07-12 02:00:37 -07001574 total_0 = (sizeof(test_gpio_0)/sizeof(int)); //GPIO+PIN
wangyouqiangece1c722023-12-12 17:28:53 +08001575 total_1 = (sizeof(test_gpio_1)/sizeof(int));
wangyouqiang7775cbf2023-12-29 16:35:24 +08001576 total_2 = (sizeof(test_gpio_2)/sizeof(int));
r.xiaoaeb54ee2024-07-12 02:00:37 -07001577 if(gnss_support == 1){
1578 total_2 = total_2 - 4;
1579 }
1580 printf("L508-X6 Start test gpio V1.1\n");
wangyouqiangece1c722023-12-12 17:28:53 +08001581 /* [1 all gpio Init] */
1582 gpio_test_init_test_gpio_mode();
1583
1584
1585 /* [2 GPIOTEST] */
wangyouqiangece1c722023-12-12 17:28:53 +08001586 //Test 0
r.xiaoaeb54ee2024-07-12 02:00:37 -07001587 for(i = 0; i < total_0; i=i+2){
1588 ret_0 = gpio_register_test_out_0(test_gpio_0[i]);
wangyouqiangece1c722023-12-12 17:28:53 +08001589 if(-1 == ret_0){
1590 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_0[i]);
r.xiaoaeb54ee2024-07-12 02:00:37 -07001591 fail_io[n] = test_gpio_0[i+1];
wangyouqiangece1c722023-12-12 17:28:53 +08001592 n++;
1593 }else{
b.liuc7ffd092024-01-03 15:23:07 +08001594 //printf("############gpio [%d] test success############\n", test_gpio_0[i]);
wangyouqiangece1c722023-12-12 17:28:53 +08001595 }
1596 }
1597
1598 //Test 1
r.xiaoaeb54ee2024-07-12 02:00:37 -07001599 for(i = 0; i < total_1; i=i+2){
1600 ret_1 = gpio_register_test_out_1(test_gpio_1[i]);
wangyouqiangece1c722023-12-12 17:28:53 +08001601 if(-1 == ret_1){
1602 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_1[i]);
r.xiaoaeb54ee2024-07-12 02:00:37 -07001603 fail_io[n] = test_gpio_1[i+1];
wangyouqiangece1c722023-12-12 17:28:53 +08001604 n++;
1605 }else{
b.liuc7ffd092024-01-03 15:23:07 +08001606 //printf("############gpio [%d] test success############\n", test_gpio_1[i]);
wangyouqiangece1c722023-12-12 17:28:53 +08001607 }
1608 }
wangyouqiang7775cbf2023-12-29 16:35:24 +08001609
1610 //Test 2
r.xiaoaeb54ee2024-07-12 02:00:37 -07001611 for(i = 0; i < total_2; i=i+4){
1612 ret_2 = gpio_register_test_out_2(test_gpio_2[i], test_gpio_2[i+2]);
wangyouqiang7775cbf2023-12-29 16:35:24 +08001613 if(-1 == ret_2){
r.xiaoaeb54ee2024-07-12 02:00:37 -07001614 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_2[i+2]);
1615 fail_io[n] = test_gpio_2[i+3];
wangyouqiang7775cbf2023-12-29 16:35:24 +08001616 n++;
1617 }else{
r.xiaoaeb54ee2024-07-12 02:00:37 -07001618 //printf("############gpio [%d] test success############\n", test_gpio_2[i+2]);
wangyouqiang7775cbf2023-12-29 16:35:24 +08001619 }
1620 }
1621
wangyouqiangece1c722023-12-12 17:28:53 +08001622 mbtk_adc_close();
1623 return n;
1624}
1625
wangyouqiang48adc142024-06-24 20:07:30 +08001626#elif 0//(MBTK_PROJECT_T108)
wangyouqiangb1deff32024-03-05 13:31:27 +08001627int gpio_register_test_out(int port, int value)
1628{
1629 int ret;
1630 int i;
1631 int valueh = 0;
1632 int valuel = 1;
1633
1634 //printf("Gpio port [%d] test start!\n", port);
1635 ret = gpio_register_set_func_0(port); //设功能为GPIO
1636 if(ret){
1637 printf("gpio_port can't support!\n");
1638 return -1;
1639 }
1640 gpio_register_set_direction(port, 1); //设方向为输出
1641 //ret = gpio_register_get_value(port);
1642 //printf("gpio default value is: %d\n", ret);
1643
1644
1645 //[High]
1646 for(i = 0; i <= 9; i++){
1647 gpio_register_set_value(port, 1);
1648 usleep(50);
1649 //valueh = gpio_register_get_value(port);
1650 //printf("set high? %d\n", valueh);
1651 if(1 == valueh){
1652 break;
1653 }
1654 }
1655
1656 for(i = 0; i <= 35; i++){
1657 ret = gpio_register_get_value(118);
1658 if(1 == ret){
1659 break;
1660 }
1661 usleep(3000);
1662 }
1663 printf("******gpio should is high: %d.******\n", ret);
1664 if(1 != ret){
1665 ret=-1;
1666 goto exit;
1667 }
1668
1669 //[Low]
1670 usleep(200);
1671 for(i = 0; i <= 9; i++){
1672 gpio_register_set_value(port, 0); //输出低
1673 usleep(50);
1674 //valuel = gpio_register_get_value(port);
1675 //printf("set low? %d\n", valuel);
1676 if(0 == valuel){
1677 break;
1678 }
1679 }
1680
1681 for(i = 0; i <= 35; i++){
1682 ret = gpio_register_get_value(118);
1683 if(0 == ret){
1684 break;
1685 }
1686 usleep(3000);
1687 }
1688 //printf("******gpio should is low: %d.******\n", ret);
1689 if(0 != ret){
1690 ret=-1;
1691 goto exit;
1692 }
1693
1694exit:
1695 gpio_register_set_value(port, 0);
1696 gpio_register_set_direction(port, 0); //设方向为输入
1697 return ret;
1698}
1699
1700int gpio_test_init_test_gpio_mode(void)
1701{
wangyouqiang48adc142024-06-24 20:07:30 +08001702 const int test_gpio[] = {
wangyouqiangb1deff32024-03-05 13:31:27 +08001703 99,
1704 117,
1705 21,22,23,24,44,41,120,
1706 8,127,46,59,58,57,56,55,48,19,34,33,35,36,49,
1707 50,25,28,26,122,20,10,11,
1708 39,40,37,38,
1709 51,52,31,32,
1710 };
1711
1712 int i, j, ret, total, n = 0;
1713 total = (sizeof(test_gpio)/sizeof(int));
1714 printf("[init]gpio_test_init_test_gpio_mode BEGIN\n");
1715
1716 /* [1 all gpio set to low] */
wangyouqiangb1deff32024-03-05 13:31:27 +08001717 gpio_register_set_func_0(118);
1718 gpio_register_set_direction(118, 0);
1719 system("echo in > /sys/class/gpio/gpio118/direction");
1720
1721 for(j = 0; j < 6 ; j++){
1722 for(i = 0; i < total; i++){
1723 gpio_register_set_func_0(test_gpio[i]);
1724 gpio_register_set_direction(test_gpio[i], 1);
1725 gpio_register_set_value(test_gpio[i], 0);
1726 //ret = gpio_register_get_value(test_gpio[i]);
1727 //printf("[init]get gpio%d=%d\n", test_gpio[i], ret);
1728 }
wangyouqiangb1deff32024-03-05 13:31:27 +08001729
1730 for(i = 0; i <= 35; i++){
1731 ret = gpio_register_get_value(118);
1732 printf("[init]get gpi118=%d\n", ret);
1733 usleep(3000);
1734 if(0 == (ret)){
1735 break;
1736 }
1737 }
1738
1739 printf("pre set ADC: %d, times: %d\n",(ret), j);
1740 if(0 == (ret)){
1741 break;
1742 }
1743 }
1744 if(0 != (ret)){
1745 printf("!!!Set all low FAIL, please retest\n");
1746 return -1;
1747 }
1748 return 0;
1749}
1750
b.liu94091352024-07-02 10:38:08 +08001751int mbtk_at_gpio(bool gnss_support, void* arg)
wangyouqiangb1deff32024-03-05 13:31:27 +08001752{
wangyouqiangb1deff32024-03-05 13:31:27 +08001753
wangyouqiang48adc142024-06-24 20:07:30 +08001754 const int test_gpio[] = {
1755 //GPIO PIN GPIO PIN GPIO PIN GPIO PIN
1756 99, 170, 117, 59, 21, 61, 22, 62,
1757 23, 144, 24, 147, 44, 5, 41, 159,
1758 120, 143, 8, 171, 127, 160, 46, 149,
1759 59, 19, 58, 18, 57, 20, 56, 21,
1760 55, 22, 48, 23, 19, 3, 34, 79,
1761 33, 80, 35, 78, 36, 77, 49, 43,
1762 50, 42, 25, 67, 28, 66, 26, 65,
1763 122, 169, 20, 152, 10, 74, 11, 73,
1764 39, 166, 40, 164, 37, 165, 38, 163,
1765 51, 58, 52, 60, 31, 57, 32, 56,
wangyouqiangb1deff32024-03-05 13:31:27 +08001766 };
1767
1768 int i, n = 0, ret, total;
1769 int *fail_io = (int *)arg;
wangyouqiang48adc142024-06-24 20:07:30 +08001770 int try_count = 0;
wangyouqiangb1deff32024-03-05 13:31:27 +08001771 total = (sizeof(test_gpio)/sizeof(int));
wangyouqiang48adc142024-06-24 20:07:30 +08001772 printf("T108 Start test gpio V1.0, total gpio=%d\n", (total/2));
wangyouqiangb1deff32024-03-05 13:31:27 +08001773
wangyouqiang48adc142024-06-24 20:07:30 +08001774 for(try_count; try_count < 4; try_count++){
1775 n = 0;
1776 /* [1 all gpio Init] */
1777 gpio_test_init_test_gpio_mode();
wangyouqiangb1deff32024-03-05 13:31:27 +08001778
wangyouqiang48adc142024-06-24 20:07:30 +08001779 /* [2 GPIOTEST] */
1780 for(i = 0; i < total; i = i + 2){
1781 ret = gpio_register_test_out(test_gpio[i], 0);
1782 if(-1 == ret){
yq.wang67c76d52024-06-24 06:11:36 -07001783 n++;
wangyouqiang48adc142024-06-24 20:07:30 +08001784 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio[i]);
1785 if(try_count != 3){
1786 printf(" ---TEST FAILED! RETRY!--- \n");
1787 usleep(5000);
1788 break;
1789 }
yq.wang67c76d52024-06-24 06:11:36 -07001790 fail_io[n - 1] = test_gpio[i+1];
wangyouqiang48adc142024-06-24 20:07:30 +08001791 }else{
1792 //printf("############gpio [%d] test success############\n", test_gpio[i]);
1793 }
1794 }
1795 if(0 == n){
1796 break;
1797 }
wangyouqiangb1deff32024-03-05 13:31:27 +08001798 }
1799 return n;
1800}
1801
1802
wangyouqiang48adc142024-06-24 20:07:30 +08001803#elif (MBTK_PROJECT_T108)
1804static int gpio_test(int port)
1805{
1806 int ret;
1807 int i;
1808 int valueh = 0;
1809 int valuel = 1;
1810
1811 //printf("Gpio port [%d] test start!\n", port);
1812 ret = gpio_register_set_func_0(port);
1813 if(ret){
1814 printf("gpio_port can't support!\n");
1815 return -1;
1816 }
1817 gpio_register_set_direction(port, 1);
1818 //ret = gpio_register_get_value(port);
1819 //printf("gpio default value is: %d\n", ret);
1820
1821
1822 //[High]
1823 for(i = 0; i <= 5; i++){
1824 gpio_register_set_value(port, 1);
1825 usleep(50);
1826 //valueh = gpio_register_get_value(port);
1827 //printf("set high? %d\n", valueh);
1828 if(1 == valueh){
1829 break;
1830 }
1831 }
1832
1833 for(i = 0; i <= 5; i++){
1834 ret = gpio_register_get_value(118);
1835 if(1 == ret){
1836
1837 break;
1838 }
1839 usleep(3000);
1840 }
1841 //printf("count=%d \n", i);
1842 //printf("******gpio should is high: %d.******\n", ret);
1843 if(1 != ret){
1844 ret=-1;
1845 goto exit;
1846 }
1847
1848 //[Low]
1849 usleep(200);
1850 for(i = 0; i <= 9; i++){
1851 gpio_register_set_value(port, 0);
1852 usleep(50);
1853 //valuel = gpio_register_get_value(port);
1854 //printf("set low? %d\n", valuel);
1855 if(0 == valuel){
1856 break;
1857 }
1858 }
1859
1860 for(i = 0; i <= 5; i++){
1861 ret = gpio_register_get_value(118);
1862 if(0 == ret){
1863
1864 break;
1865 }
1866 usleep(3000);
1867 }
1868 //printf("count=%d \n", i);
1869 //printf("******gpio should is low: %d.******\n", ret);
1870 if(0 != ret){
1871 ret=-1;
1872 goto exit;
1873 }
1874
1875exit:
1876 gpio_register_set_value(port, 0);
1877 gpio_register_set_direction(port, 0);
1878 return ret;
1879
1880}
1881
1882static int gpio_test_init_test_gpio_mode(void)
1883{
1884 const int test_gpio[] = {
1885 99,8,117,21,22,23,24,41,120,19,123,
1886 58,59,57,56,55,48,125,127,36,35,34,
1887 33,54,47,53,46,50,49,11,10,26,28,25,
1888 27,32,31,51,52,39,40,37,38,
1889 44,45
1890 };
1891
1892 int i, j, ret, total, n = 0;
1893 total = (sizeof(test_gpio)/sizeof(int));
1894 //printf("[init]gpio_test_init_test_gpio_mode BEGIN\n");
1895
1896 /* [1 all gpio set to low] */
1897 gpio_register_set_func_0(118);
1898 gpio_register_set_direction(118, 0);
1899
1900 for(j = 0; j < 5 ; j++){
1901 for(i = 0; i < total; i++){
1902 gpio_register_set_func_0(test_gpio[i]);
1903 gpio_register_set_direction(test_gpio[i], 1);
1904 gpio_register_set_value(test_gpio[i], 0);
1905 //ret = gpio_register_get_value(test_gpio[i]);
1906 //printf("[init]get gpio%d=%d\n", test_gpio[i], ret);
1907 }
1908
1909
1910 for(i = 0; i <= 10; i++){
1911 ret = gpio_register_get_value(118);
1912 //printf("[init]get gpi118=%d\n", ret);
1913 usleep(3000);
1914 if(0 == (ret)){
1915 break;
1916 }
1917 }
1918
1919 //printf("pre set 118: %d, times: %d\n",(ret), j);
1920 if(0 == (ret)){
1921 break;
1922 }
1923 }
1924 if(0 != (ret)){
1925 printf("!!!Set all low FAIL, please retest\n");
1926 return -1;
1927 }
1928 return 0;
1929}
1930
b.liu94091352024-07-02 10:38:08 +08001931int mbtk_at_gpio(bool gnss_support, void* arg)
wangyouqiang48adc142024-06-24 20:07:30 +08001932{
1933 const int test_gpio[] = {
1934 //GPIO PIN GPIO PIN GPIO PIN GPIO PIN
1935 99, 170, 8, 171, 117, 59, 21, 61,
1936 22, 62, 23, 144, 24, 147, 41, 159,
1937 120, 143, 19, 3, 123, 5, 58, 18,
1938 59, 19, 57, 20, 56, 21, 55, 22,
1939 48, 23, 125, 149, 127, 160, 36, 77,
1940 35, 78, 34, 79, 33, 80, 54, 163,
1941 #if 0 //Hard Ware ERROR!
1942 47, 164,
1943 46, 166,
1944 #endif
1945 53, 165, 50, 42, 49, 43, 11, 73,
1946 10, 74, 26, 65, 28, 66, 25, 67,
1947 27, 68, 32, 56, 31, 57, 51, 58,
1948 52, 60, 39, 192, 40, 193, 37, 194,
1949 38, 195,
1950 #if 0 //undefine GNSS
1951 44, 161,
1952 45, 151,
1953 #endif
1954 };
1955
1956 int i, n = 0, ret, total;
1957 int *fail_io = (int *)arg;
1958 int try_count;
1959 total = (sizeof(test_gpio)/sizeof(int));
1960 printf("T108 V2 Start test gpio V0.8, total gpio=%d\n", (total/2));
1961
1962 for(try_count = 0; try_count < 4; try_count++){
1963 n = 0;
1964 /* [1 all gpio Init] */
1965 gpio_test_init_test_gpio_mode();
1966
1967 /* [2 GPIOTEST] */
1968 for(i = 0; i < total; i = i + 2){
1969 ret = gpio_test(test_gpio[i]);
1970 if(-1 == ret){
yq.wang67c76d52024-06-24 06:11:36 -07001971 n++;
wangyouqiang48adc142024-06-24 20:07:30 +08001972 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio[i]);
1973 if(try_count != 3){
1974 printf(" ---TEST FAILED! RETRY!--- \n");
1975 usleep(5000);
1976 break;
1977 }
yq.wang67c76d52024-06-24 06:11:36 -07001978 fail_io[n - 1] = test_gpio[i + 1];
wangyouqiang48adc142024-06-24 20:07:30 +08001979 }else{
1980 //printf("############gpio [%d] test success############\n", test_gpio[i]);
1981 }
1982 }
1983 if(0 == n){
1984 printf(" ---ALL PASS---\n");
1985 break;
1986 }
1987 }
1988 return n;
1989}
1990
wangyouqiangece1c722023-12-12 17:28:53 +08001991
b.liu4f8e8792023-11-01 23:25:22 +08001992#else
b.liu94091352024-07-02 10:38:08 +08001993int mbtk_at_gpio(bool gnss_support, void* arg)
b.liu4f8e8792023-11-01 23:25:22 +08001994{
1995 return -1;
1996}
1997
1998#endif
luojianc08d4e62024-07-05 17:12:02 +08001999