blob: eeeaeb56e2379d6c7e1464a048d1c0fe8a823e4f [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;
39
gy.tangf4d7b852024-06-27 18:50:14 +080040/**/
41#define GPIO_MAX_CURRENT (7<<10) //设置GPIO最大输出电流
42#define GPIO_STRONG_PULLUP (1<<3) //设置GPIO强上拉
43#define GPIO_PULLUP_ENABLE ((1<<15)|(1<<14)) //GPIO上拉
44#define GPIO_PULLDOWN_ENABLE ((1<<15)|1<<13) //GPIO下拉
45
46#define MBTK_GPIO_OUTPUT 1
47#define MBTK_GPIO_INPUT 0
48
wangyouqiangb1deff32024-03-05 13:31:27 +080049#if (defined(MBTK_PROJECT_L508_X6) || defined(MBTK_PROJECT_T108))
wangyouqiang7775cbf2023-12-29 16:35:24 +080050struct gpio_register_function gpio_func_register[128] = {
51 {GPIO_FUNC_GPIO_00, 0},
52 {GPIO_FUNC_GPIO_01, 0},
53 {GPIO_FUNC_GPIO_02, 0},
54 {GPIO_FUNC_GPIO_03, 0},
55 {GPIO_FUNC_GPIO_04, 0},
56 {GPIO_FUNC_GPIO_05, 0},
57 {GPIO_FUNC_GPIO_06, 0},
58 {GPIO_FUNC_GPIO_07, 0},
59 {GPIO_FUNC_GPIO_08, 0},
60 {GPIO_FUNC_GPIO_09, 0},
61 {GPIO_FUNC_GPIO_10, 0},
62 {GPIO_FUNC_GPIO_11, 0},
63 {GPIO_FUNC_GPIO_12, 0},
64 {GPIO_FUNC_GPIO_13, 0},
65 {GPIO_FUNC_GPIO_14, 0},
66 {GPIO_FUNC_GPIO_15, 0},
67 {GPIO_FUNC_GPIO_16, 0},
68 {GPIO_FUNC_GPIO_17, 0},
69 {GPIO_FUNC_GPIO_18, 0},
70 {GPIO_FUNC_GPIO_19, 0},
71 {GPIO_FUNC_GPIO_20, 0},
72 {GPIO_FUNC_GPIO_21, 0},
73 {GPIO_FUNC_GPIO_22, 0},
74 {GPIO_FUNC_GPIO_23, 0},
75 {GPIO_FUNC_GPIO_24, 0},
76 {GPIO_FUNC_GPIO_25, 0},
77 {GPIO_FUNC_GPIO_26, 0},
78 {GPIO_FUNC_GPIO_27, 0},
79 {GPIO_FUNC_GPIO_28, 0},
80 {GPIO_FUNC_GPIO_29, 0},
81 {GPIO_FUNC_GPIO_30, 0},
82 {GPIO_FUNC_GPIO_31, 0},
83 {GPIO_FUNC_GPIO_32, 0},
84 {GPIO_FUNC_GPIO_33, 0},
85 {GPIO_FUNC_GPIO_34, 0},
86 {GPIO_FUNC_GPIO_35, 0},
wangyouqiangb1deff32024-03-05 13:31:27 +080087 {GPIO_FUNC_GPIO_36, 0},
88 {GPIO_FUNC_GPIO_37, 0}, // GPIO_37
89 {GPIO_FUNC_GPIO_38, 0}, // GPIO_38
90 {GPIO_FUNC_GPIO_39, 0}, // GPIO_39
91 {GPIO_FUNC_GPIO_40, 0}, // GPIO_40
92 {GPIO_FUNC_GPIO_41, 0}, //GPIO_41
93 {GPIO_FUNC_GPIO_42, 0}, //GPIO_42
wangyouqiang7775cbf2023-12-29 16:35:24 +080094 {GPIO_FUNC_GPIO_43, 0}, //GPIO_43
95 {GPIO_FUNC_GPIO_44,0}, //GPIO_44
96 {GPIO_FUNC_GPIO_45,0}, //GPIO_45
97 {GPIO_FUNC_GPIO_46,0}, //GPIO_46
98 {GPIO_FUNC_GPIO_47,0}, //GPIO_47
99 {GPIO_FUNC_SDIO_DAT3, 1}, //GPIO_48
100 {GPIO_FUNC_GPIO_49, 0},
101 {GPIO_FUNC_GPIO_50, 0},
102 {GPIO_FUNC_GPIO_51, 0},
103 {GPIO_FUNC_GPIO_52, 0},
104 {GPIO_FUNC_GPIO_53, 0},
105 {GPIO_FUNC_GPIO_54, 0},
106 {GPIO_FUNC_SDIO_DAT2, 1}, //GPIO_55
107 {GPIO_FUNC_SDIO_DAT1, 1}, //GPIO_56
108 {GPIO_FUNC_SDIO_DAT0, 1}, //GPIO_57
109 {GPIO_FUNC_SDIO_CMD, 1}, //GPIO_58
110 {GPIO_FUNC_SDIO_CLK, 1}, //GPIO_59
111 {GPIO_FUNC_GPIO_60, 0},
112 {0,0},{0,0},{0,0},{0,0},
113 {0,0},{0,0},
114 {GPIO_FUNC_DVL_0, 1},//GPIO_67
115 {GPIO_FUNC_DVL_1, 1},//GPIO_68
116 {GPIO_FUNC_GPIO_69, 0},
117 {GPIO_FUNC_GPIO_70, 0},
118 {GPIO_FUNC_QSPI_DAT3, 1}, //GPIO_71
119 {GPIO_FUNC_QSPI_DAT2, 1}, //GPIO_72
120 {GPIO_FUNC_QSPI_DAT1, 1}, //GPIO_73
121 {GPIO_FUNC_QSPI_DAT0, 1}, //GPIO_74
122 {GPIO_FUNC_QSPI_CLK, 1}, //GPIO_75
123 {GPIO_FUNC_QSPI_CS1, 1}, //GPIO_76
124 {GPIO_FUNC_GPIO_77, 0},
125 {GPIO_FUNC_GPIO_78, 0},
126 {GPIO_FUNC_GPIO_79, 0},
127 {GPIO_FUNC_GPIO_80, 0},
128 {GPIO_FUNC_USIM_UCLK, 1},//GPIO_81
129 {GPIO_FUNC_USIM_UIO, 1},//GPIO_82
130 {GPIO_FUNC_USIM_URSTn, 1},//GPIO_83
131
132 {GPIO_FUNC_MMC1_DAT3,5}, //GPIO_84
133 {GPIO_FUNC_MMC1_DAT2,5}, //GPIO_85
134 {GPIO_FUNC_MMC1_DAT1,5}, //GPIO_86
135 {GPIO_FUNC_MMC1_DAT0,5}, //GPIO_87
136 {GPIO_FUNC_MMC1_CMD,5}, //GPIO_88
137 {GPIO_FUNC_MMC1_CLK,5}, //GPIO_89
138 {GPIO_FUNC_MMC1_CD,1}, //GPIO_90
139 {0,0},
140 {0,0},{0,0},{0,0},{0,0},
141 {0,0},{0,0},{0,0},
142 {GPIO_FUNC_USB_ID, 1},//GPIO_99
143 {0,0},{0,0},{0,0},{0,0},
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},
148 {GPIO_FUNC_PRI_TDI, 1}, //GPIO_117
149 {GPIO_FUNC_PRI_TMS, 1}, //GPIO_118
150 {GPIO_FUNC_PRI_TCK, 1}, //GPIO_119
151 {GPIO_FUNC_PRI_TDO, 1}, //GPIO_120
152 {GPIO_FUNC_QSPI_VMODE_GPIO, 1}, //GPIO_121
153 {GPIO_FUNC_VBUS_DRV, 1}, //GPIO_122
154 {GPIO_FUNC_CLK_REQ, 1}, //GPIO_123
155 {0,0},
156 {GPIO_FUNC_VCXO_REQ, 1}, //GPIO_125
157 {0,0}, //GPIO_126
158 {GPIO_FUNC_VCXO_OUT, 1}, //GPIO_127
159 {0,0},
160};
161
162#else
liubin281ac462023-07-19 14:22:54 +0800163struct gpio_register_function gpio_func_register[128] = {
164 {GPIO_FUNC_GPIO_00, 0},
165 {GPIO_FUNC_GPIO_01, 0},
166 {GPIO_FUNC_GPIO_02, 0},
167 {GPIO_FUNC_GPIO_03, 0},
168 {GPIO_FUNC_GPIO_04, 0},
169 {GPIO_FUNC_GPIO_05, 0},
170 {GPIO_FUNC_GPIO_06, 0},
171 {GPIO_FUNC_GPIO_07, 0},
172 {GPIO_FUNC_GPIO_08, 0},
173 {GPIO_FUNC_GPIO_09, 0},
174 {GPIO_FUNC_GPIO_10, 0},
175 {GPIO_FUNC_GPIO_11, 0},
176 {GPIO_FUNC_GPIO_12, 0},
177 {GPIO_FUNC_GPIO_13, 0},
178 {GPIO_FUNC_GPIO_14, 0},
179 {GPIO_FUNC_GPIO_15, 0},
180 {GPIO_FUNC_GPIO_16, 0},
181 {GPIO_FUNC_GPIO_17, 0},
182 {GPIO_FUNC_GPIO_18, 0},
183 {GPIO_FUNC_GPIO_19, 0},
184 {GPIO_FUNC_GPIO_20, 0},
185 {GPIO_FUNC_GPIO_21, 0},
186 {GPIO_FUNC_GPIO_22, 0},
187 {GPIO_FUNC_GPIO_23, 0},
188 {GPIO_FUNC_GPIO_24, 0},
189 {GPIO_FUNC_GPIO_25, 0},
190 {GPIO_FUNC_GPIO_26, 0},
191 {GPIO_FUNC_GPIO_27, 0},
192 {GPIO_FUNC_GPIO_28, 0},
193 {GPIO_FUNC_GPIO_29, 0},
194 {GPIO_FUNC_GPIO_30, 0},
195 {GPIO_FUNC_GPIO_31, 0},
196 {GPIO_FUNC_GPIO_32, 0},
197 {GPIO_FUNC_GPIO_33, 0},
198 {GPIO_FUNC_GPIO_34, 0},
199 {GPIO_FUNC_GPIO_35, 0},
200 {GPIO_FUNC_GPIO_36, 0},
201 {GPIO_FUNC_MMC1_DAT3, 5}, // GPIO_37
202 {GPIO_FUNC_MMC1_DAT2, 5}, // GPIO_38
203 {GPIO_FUNC_MMC1_DAT1, 5}, // GPIO_39
204 {GPIO_FUNC_MMC1_DAT0, 5}, // GPIO_40
205 {GPIO_FUNC_MMC1_CMD, 5}, //GPIO_41
206 {GPIO_FUNC_MMC1_CLK, 5}, //GPIO_42
207 {GPIO_FUNC_MMC1_CD , 1}, //GPIO_43
208 {0,0},{0,0},{0,0},{0,0},
209 {GPIO_FUNC_SDIO_DAT3, 1}, //GPIO_48
210 {GPIO_FUNC_GPIO_49, 0},
211 {GPIO_FUNC_GPIO_50, 0},
212 {GPIO_FUNC_GPIO_51, 0},
213 {GPIO_FUNC_GPIO_52, 0},
214 {GPIO_FUNC_GPIO_53, 0},
215 {GPIO_FUNC_GPIO_54, 0},
216 {GPIO_FUNC_SDIO_DAT2, 1}, //GPIO_55
217 {GPIO_FUNC_SDIO_DAT1, 1}, //GPIO_56
218 {GPIO_FUNC_SDIO_DAT0, 1}, //GPIO_57
219 {GPIO_FUNC_SDIO_CMD, 1}, //GPIO_58
220 {GPIO_FUNC_SDIO_CLK, 1}, //GPIO_59
221 {GPIO_FUNC_GPIO_60, 0},
222 {0,0},{0,0},{0,0},{0,0},
223 {0,0},{0,0},
224 {GPIO_FUNC_DVL_0, 1},//GPIO_67
225 {GPIO_FUNC_DVL_1, 1},//GPIO_68
226 {GPIO_FUNC_GPIO_69, 0},
227 {GPIO_FUNC_GPIO_70, 0},
228 {GPIO_FUNC_QSPI_DAT3, 1}, //GPIO_71
229 {GPIO_FUNC_QSPI_DAT2, 1}, //GPIO_72
230 {GPIO_FUNC_QSPI_DAT1, 1}, //GPIO_73
231 {GPIO_FUNC_QSPI_DAT0, 1}, //GPIO_74
232 {GPIO_FUNC_QSPI_CLK, 1}, //GPIO_75
233 {GPIO_FUNC_QSPI_CS1, 1}, //GPIO_76
234 {GPIO_FUNC_GPIO_77, 0},
235 {GPIO_FUNC_GPIO_78, 0},
236 {GPIO_FUNC_GPIO_79, 0},
237 {GPIO_FUNC_GPIO_80, 0},
238 {GPIO_FUNC_USIM_UCLK, 1},//GPIO_81
239 {GPIO_FUNC_USIM_UIO, 1},//GPIO_82
240 {GPIO_FUNC_USIM_URSTn, 1},//GPIO_83
241 {0,0},{0,0},{0,0},{0,0},
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},
245 {GPIO_FUNC_USB_ID, 1},//GPIO_99
246 {0,0},{0,0},{0,0},{0,0},
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},
251 {GPIO_FUNC_PRI_TDI, 1}, //GPIO_117
252 {GPIO_FUNC_PRI_TMS, 1}, //GPIO_118
253 {GPIO_FUNC_PRI_TCK, 1}, //GPIO_119
254 {GPIO_FUNC_PRI_TDO, 1}, //GPIO_120
255 {GPIO_FUNC_QSPI_VMODE_GPIO, 1}, //GPIO_121
256 {GPIO_FUNC_VBUS_DRV, 1}, //GPIO_122
257 {GPIO_FUNC_CLK_REQ, 1}, //GPIO_123
258 {0,0},
259 {GPIO_FUNC_VCXO_REQ, 1}, //GPIO_125
260 {GPIO_FUNC_VCXO_OUT, 1}, //GPIO_126
261 {0,0},
262};
liubin281ac462023-07-19 14:22:54 +0800263
wangyouqiang7775cbf2023-12-29 16:35:24 +0800264#endif
liubin281ac462023-07-19 14:22:54 +0800265
wangyouqiangece1c722023-12-12 17:28:53 +0800266static int mbtk_gpio_adc(channel)
liubin281ac462023-07-19 14:22:54 +0800267{
liubin281ac462023-07-19 14:22:54 +0800268 int ret = 0;
269
wangyouqiangece1c722023-12-12 17:28:53 +0800270 ret = mbtk_adc_get(channel);
271 //printf("ADC_%d_value =%d\n", channel, ret);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800272 if(ret >= 500 ){
273 ret = 1;
liubin281ac462023-07-19 14:22:54 +0800274 }
wangyouqiang7850b4a2023-09-18 15:47:57 +0800275 else if (ret>=0 && ret<=480){
276 ret = 0;
277 }
278 else{
279 ret = -1;
liubin281ac462023-07-19 14:22:54 +0800280 }
281 return ret;
282}
liubin281ac462023-07-19 14:22:54 +0800283
liubin281ac462023-07-19 14:22:54 +0800284
285static int hwacc_register(int rw, unsigned int addr, unsigned int *data)
286{
287 int fid;
288 unsigned int pagesize, len, len_aligned;
289 unsigned int addr_aligned;
290 volatile unsigned int *pa;
291 void *vpa;
292
293 len = pagesize = sysconf(_SC_PAGESIZE);
294 if((fid = open(HWMAP_DEVICE, O_RDWR)) < 0)
295 {
296 printf("Failed to open %s\n", HWMAP_DEVICE);
297 exit(-1);
298 }
299
300 // Align the length so the mapped area is page-aligned and contains the requested area
301 addr_aligned = addr & PAGE_MASK_BITS(pagesize);
302 len_aligned =((addr + len - addr_aligned) + pagesize - 1) & PAGE_MASK_BITS(pagesize);
303
304 /* Notes on flags: MAP_PRIVATE results in copy on write; MAP_SHARED allows normal write */
305 /* MAP_SHARED required O_RDWR in open above, otherwise mmap fails with errno=EACCES */
306 /* Notes on prot: PROT_WRITE allows read and write; PROT_READ allows read only */
307 /* Notes on off: an unsigned 32-bit value, should be aligned to page size according to mmap manpage */
308 if((vpa = mmap(0, len_aligned, PROT_READ|PROT_WRITE, MAP_SHARED, fid, addr_aligned)) == MAP_FAILED)
309 {
310 printf("mmap failed (%d)\n", errno);
311 }
312 else
313 {
314 pa = (volatile unsigned int *)((unsigned char *)vpa + (addr & PAGE_OFFS_BITS(pagesize)));
315 if(rw == 0)
316 {
317 *data = *pa;
318 gpio_log("Value read from 0x%.8x via MVA=0x%p is 0x%.8x\n", addr, pa, *data);
319 }
320 else if(rw == 1)
321 {
322 *pa = *data;
323 gpio_log("Value %.8x written to 0x%.8x via MVA=0x%p\n", *data, addr, pa);
324#if defined(HWACC_DEBUG)
325 {
326 unsigned int val;
327 val = *pa;
328 printf("Value read from 0x%.8x via MVA=0x%p is 0x%.8x\n", addr, pa, val);
329 }
330#endif
331 }
332 munmap(vpa, len);
333 }
334
335 close(fid);
336 return 0;
337}
338/*
339设置GPIO 模式:第一步读GPIO22默认function
340root@OpenWrt:/# hwacc r 0xd401e134
341Option = r Addr = d401e134
342Value read from 0xd401e134 via MVA=0x0xb6fc3134 is 0x0000d040 //默认GPIO功能
343Bit0~bit2值对应上面表格各function,0代表GPIO功能
344 */
345static int gpio_register_read(int reg)
346{
347 int ret = -1;
348#if 0
349 FILE * fp;
350 // "/bin/hwacc r 0xd401e134";
351 char command[36] = {0};
352 char buffer[1024];
353 int i = 0;
354
355 sprintf(command, "/bin/hwacc r 0x%x", reg);
356 fp = popen(command, "r");
357 while(1)
358 {
359 if( fgets (buffer, sizeof(buffer), fp)!=NULL ) {
360 buffer[strlen(buffer) - 1] = 0;
361 // gpio_log("out [%d]: %s\n", strlen(buffer), buffer);
362 }else{
363 break;
364 }
365 i = strstr_n(buffer, "is");
366 if(i)
367 {
368 ret = str_to_hex(&buffer[i + 2]);
369 gpio_log("read 0x%x value:%s, %x\n", reg, &buffer[i + 2], ret);
370 }
371 }
372 pclose(fp);
373#else
b.liu85565a72023-11-09 16:47:40 +0800374#ifndef MBTK_PROJECT_PN1803
375 usleep(50);
376#endif
liubin281ac462023-07-19 14:22:54 +0800377 hwacc_register(0, reg, &ret);
b.liu85565a72023-11-09 16:47:40 +0800378#ifndef MBTK_PROJECT_PN1803
379 usleep(50);
380#endif
liubin281ac462023-07-19 14:22:54 +0800381#endif
382 return ret;
383}
384/*
385设置输入输出状态,设置PDR寄存器GPIO22为output
386root@OpenWrt:/# hwacc w 0xD401900c 0x00c03800
387Option = w Addr = d401900c Data=00c03800
388Value 00c03800 written to 0xd401900c via MVA=0x0xb6f9f00c
389 */
390static void gpio_register_write(int reg, int value)
391{
392#if 0
393 FILE * fp;
394 // "/bin/hwacc w 0xD401900c 0x00c03800"
395 char command[36] = {0};
396 char buffer[1024];
397
398 sprintf(command, "/bin/hwacc w 0x%x 0x%x", reg, value);
399 gpio_log("command: %s\n", command);
400 fp = popen(command, "r");
401 while(1)
402 {
403 if( fgets (buffer, sizeof(buffer), fp)!=NULL ) {
404 gpio_log("%s\n", buffer);
405 }else{
406 break;
407 }
408 }
409 pclose(fp);
410
411#else
b.liu85565a72023-11-09 16:47:40 +0800412#ifndef MBTK_PROJECT_PN1803
413 usleep(50);
414#endif
liubin281ac462023-07-19 14:22:54 +0800415 hwacc_register(1, reg, &value);
b.liu85565a72023-11-09 16:47:40 +0800416#ifndef MBTK_PROJECT_PN1803
417 usleep(50);
418#endif
liubin281ac462023-07-19 14:22:54 +0800419#endif
420}
421/*
422AF SEL<p>This field is used for alternate function selection for a pin.
423It selects between the eight possible alternate functions for the pin.
424Alternate function 0 is always the reset case.
425
426<p>0x0 = Alternate function 0 (primary function at reset)
427<p>0x1 = Alternate function 1
428<p>0x2 = Alternate function 2
429<p>0x3 = Alternate function 3
430<p>0x4 = Alternate function 4
431<p>0x5 = Alternate function 5
432<p>0x6 = Alternate function 6
433<p>0x7 = Alternate function 7
434 */
435static int gpio_register_set_func_0(int port)
436{
437 int ret;
438 struct gpio_register_function *reg = NULL;
439 if(port > 128)
440 return -1;
441 reg = &gpio_func_register[port];
442 if(0 == reg->reg)
443 return -1;
444 ret = gpio_register_read(reg->reg);
445 if((ret & 0x7) != reg->func_gpio)
446 {
wangyouqiang7850b4a2023-09-18 15:47:57 +0800447 //printf("Gpio set func [%d] [0x%x]!\n", reg->func_gpio, (ret & 0xfffffff8) | reg->func_gpio);
liubin281ac462023-07-19 14:22:54 +0800448 gpio_register_write(reg->reg, (ret & 0xfffffff8) | reg->func_gpio);
449 }
450 return 0;
451}
452/*
453设置GPIO 方向
454读取输入输出状态,读PDR寄存器:0x0c
455root@OpenWrt:/# hwacc r 0xD401900c
456Option = r Addr = d401900c
457Value read from 0xd401900c via MVA=0x0xb6f3900c is 0x00803800 //bit22为0,代表Input
458 */
459static void gpio_register_set_direction(int port, int dir)
460{
461 int ret;
462 int reg = 0xD4019000;
463
464 if(port > (32 - 1))
465 reg = 0xD4019004;
466 if(port > (32 * 2 - 1))
467 reg = 0xD4019008;
468 if(port > (32 * 3 - 1))
469 reg = 0xD4019100;
470
471 reg += 0x0c;
472 port = port % 0x20;
473 ret = gpio_register_read(reg);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800474 //printf("[Direction] reg_value=%x\n", ret);
liubin281ac462023-07-19 14:22:54 +0800475 // 设置 输出 0 && 1
476 if(!(ret & (0x1 << port)) && dir)
477 {
gy.tangf4d7b852024-06-27 18:50:14 +0800478 gpio_register_write(reg, ret | (0x1 << port)); //set the gpio bit
wangyouqiang7850b4a2023-09-18 15:47:57 +0800479 //printf("[Direction out] reg_value=%x\n", gpio_register_read(reg));
liubin281ac462023-07-19 14:22:54 +0800480 }
481 // 设置 输入 1 && 0
482 if((ret & (0x1 << port)) && !dir)
483 {
gy.tangf4d7b852024-06-27 18:50:14 +0800484#if 1
485 gpio_register_write(reg, ret & (~(0x1 << port))); //clear the gpio bit,mbtk_tanggaoyou
486#else
487 gpio_register_write(reg, ret | !(0x1 << port)); //这是错误配置,实际没有任何作用,还是原来的值
488#endif
wangyouqiang7850b4a2023-09-18 15:47:57 +0800489 //printf("[Direction in] reg_value=%x\n", gpio_register_read(reg));
liubin281ac462023-07-19 14:22:54 +0800490 }
491}
492/*
493设置GPIO 输出电平
494
495读取电平状态,先读PLR寄存器:0x00
496root@OpenWrt:/# hwacc r 0xD4019000
497Option = r Addr = d4019000
498Value read from 0xd4019000 via MVA=0x0xb6f1c000 is 0x81e82a30
499对应下面具体BIT
5001000 0001 1110 1000 0010 1010 0011 0000 BIT22默认电平高
501
502设置输出高:设置PSR寄存器:0x18(只写寄存器)
503root@OpenWrt:/# hwacc w 0xD4019018 0x400000
504Option = w Addr = d4019018 Data=00400000
505Value 00400000 written to 0xd4019018 via MVA=0x0xb6f56018 //bit22写1,输出高
506
507设置输出低:设置PCR寄存器:0x24
508root@OpenWrt:/# hwacc w 0xD4019024 0x400000
509Option = w Addr = d4019024 Data=00400000
510Value 00400000 written to 0xd4019024 via MVA=0x0xb6faa024 //Bit22写1,GPIO22输出低
511
512 */
513static void gpio_register_set_value(int port, int value)
514{
515 int ret;
516 int reg = 0xD4019000;
517
518 if(port > (32 - 1))
519 reg = 0xD4019004;
520 if(port > (32 * 2 - 1))
521 reg = 0xD4019008;
522 if(port > (32 * 3 - 1))
523 reg = 0xD4019100;
524
525 if(value)
526 {
527 reg += 0x18;
528 }
529 else
530 reg += 0x24;
531
532 port = port % 0x20;
533 ret = gpio_register_read(reg);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800534 //printf("[Value] reg_value=%x\n", gpio_register_read(0xD4019004));
liubin281ac462023-07-19 14:22:54 +0800535 // 设置 高电平 0 && 1
536 if(value)
537 {
538 gpio_register_write(reg, ret | (0x1 << port));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800539 //printf("[Value high] reg_value=%x\n", gpio_register_read(0xD4019004));
liubin281ac462023-07-19 14:22:54 +0800540 return;
541 }
542 // 设置 低电平 1 && 0
543 if(!(ret & (0x1 << port)) && !value)
544 {
545 gpio_register_write(reg, ret | (0x1 << port));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800546 //printf("[Value low] reg_value=%x\n", gpio_register_read(0xD4019004));
liubin281ac462023-07-19 14:22:54 +0800547 }
548}
549/*
550读取电平状态,先读PLR寄存器:0x00
551root@OpenWrt:/# hwacc r 0xD4019000
552Option = r Addr = d4019000
553Value read from 0xd4019000 via MVA=0x0xb6f1c000 is 0x81e82a30
554对应下面具体BIT
5551000 0001 1110 1000 0010 1010 0011 0000 BIT22默认电平高
556 */
557static int gpio_register_get_value(int port)
558{
559 int ret = -1;
560 int reg = 0xD4019000;
561
562 if(port > (32 - 1))
563 reg = 0xD4019004;
564 if(port > (32 * 2 - 1))
565 reg = 0xD4019008;
566 if(port > (32 * 3 - 1))
567 reg = 0xD4019100;
568 port = port % 0x20;
569 ret = gpio_register_read(reg);
570 if(ret & (0x1 << port))
571 {
572 return 1;
573 }
574 return 0;
575}
576
577void gpio_debug_set(int enable)
578{
579 gpio_debug = enable;
580}
581
b.liu85565a72023-11-09 16:47:40 +0800582
583
584#if defined(MBTK_PROJECT_PN1803)
liubin281ac462023-07-19 14:22:54 +0800585int gpio_register_test_out(int port, int value)
586{
587 int ret;
wangyouqiang7850b4a2023-09-18 15:47:57 +0800588 int i;
589 int valueh = 0;
590 int valuel = 1;
591
wangyouqiang4c262fa2023-10-17 17:48:03 +0800592 //printf("Gpio port [%d] test start!\n", port);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800593 ret = gpio_register_set_func_0(port); //设功能为GPIO
594 if(ret){
liubin281ac462023-07-19 14:22:54 +0800595 printf("gpio_port can't support!\n");
596 return -1;
597 }
wangyouqiang7850b4a2023-09-18 15:47:57 +0800598 gpio_register_set_direction(port, 1); //设方向为输出
liubin281ac462023-07-19 14:22:54 +0800599 ret = gpio_register_get_value(port);
wangyouqiang4c262fa2023-10-17 17:48:03 +0800600 //printf("gpio default value is : %d.\n", ret);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800601
602
603 //[High]
604 for(i = 0; i <= 10; i++){
605 gpio_register_set_value(port, 1);
606 usleep(50);
607 valueh = gpio_register_get_value(port);
wangyouqiang4c262fa2023-10-17 17:48:03 +0800608 //printf("set high? %d\n",valueh);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800609 if(1 == valueh){
610 break;
611 }
612 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800613 //usleep(10000);
614 for(i = 0; i <= 35; i++){
wangyouqiangece1c722023-12-12 17:28:53 +0800615 ret = mbtk_gpio_adc(0);
wangyouqiang59d8dd22023-10-19 18:03:54 +0800616 usleep(3000);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800617 if(1 == ret){
618 break;
619 }
620 }
wangyouqiang7850b4a2023-09-18 15:47:57 +0800621 if(1 != ret){
622 ret=-1;
liubin281ac462023-07-19 14:22:54 +0800623 goto exit;
wangyouqiang7850b4a2023-09-18 15:47:57 +0800624 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800625 printf("******gpio should is high: %d.******\n", ret);
liubin281ac462023-07-19 14:22:54 +0800626
wangyouqiang7850b4a2023-09-18 15:47:57 +0800627
628 //[Low]
629 usleep(200);
630 for(i = 0; i <= 10; i++){
631 gpio_register_set_value(port, 0); //输出低
b.liu074771a2023-10-19 13:13:39 +0800632 usleep(50);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800633 valuel = gpio_register_get_value(port);
wangyouqiang59d8dd22023-10-19 18:03:54 +0800634 //printf("set low? %d\n",valuel);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800635 if(0 == valuel){
636 break;
637 }
638 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800639 //usleep(10000);
640 for(i = 0; i <= 35; i++){
wangyouqiangece1c722023-12-12 17:28:53 +0800641 ret = mbtk_gpio_adc(0);
wangyouqiang59d8dd22023-10-19 18:03:54 +0800642 usleep(3000);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800643 if(0 == ret){
644 break;
645 }
646 }
b.liu074771a2023-10-19 13:13:39 +0800647 printf("******gpio should is low: %d.******\n", ret);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800648 if(0 != ret){
649 ret=-1;
liubin281ac462023-07-19 14:22:54 +0800650 goto exit;
651 }
652
653exit:
wangyouqiang7850b4a2023-09-18 15:47:57 +0800654 //gpio_register_set_direction(port, 0); //设方向为输入
liubin281ac462023-07-19 14:22:54 +0800655 return ret;
656}
657
b.liu94091352024-07-02 10:38:08 +0800658int mbtk_at_gpio(bool gnss_support, void* arg)
liubin281ac462023-07-19 14:22:54 +0800659{
b.liu37e72162023-11-01 15:38:18 +0800660 int test_gpio[] = {
661 56, 55, 58, 57, 48, 59, 12, 20, 5, 43, 21,
662 49, 50, 27, 28, 26, 25, 16, 17, 15, 126, 125,
b.liu810b1a32023-11-01 22:55:07 +0800663 4, 0, 3, 18, 7, 6, 1, 2, /*54,*/ 19, 13,
664 32, 31, 23, 24, 22, /*122,*/ 33, 35, 36, 34, 14,
wangyouqiangddbcf2e2023-11-14 13:59:41 +0800665 99,/* 53,*/ 37, 38, 39, 40, 41, 42};
liubin281ac462023-07-19 14:22:54 +0800666
wangyouqiang7850b4a2023-09-18 15:47:57 +0800667 int i, j, ret, total, n = 0;
liubin281ac462023-07-19 14:22:54 +0800668 int *fail_io = (int *)arg;
669 total = (sizeof(test_gpio)/sizeof(int));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800670 //printf("Start test gpio total: %d\n", total);
671
672 /* [1 all gpio set to low] */
673 for(j = 0; j < 6 ; j++){
674 for(i = 0; i < total; i++){
675 gpio_register_set_func_0(test_gpio[i]);
676 gpio_register_set_direction(test_gpio[i], 1);
677 gpio_register_set_value(test_gpio[i], 0);
678 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800679 //usleep(10000);
680 for(i = 0; i <= 35; i++){
wangyouqiangece1c722023-12-12 17:28:53 +0800681 ret = mbtk_gpio_adc(0);
wangyouqiang59d8dd22023-10-19 18:03:54 +0800682 usleep(3000);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800683 if(0 == ret){
684 break;
685 }
686 }
b.liu074771a2023-10-19 13:13:39 +0800687 printf("pre set ADC: %d, times: %d\n",ret, j);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800688 if(0 == ret){
689 break;
690 }
691 }
wangyouqiang4c262fa2023-10-17 17:48:03 +0800692 if(0 != ret){
693 printf("Fail, please retest");
694 return -1;
695 }
696
wangyouqiang7850b4a2023-09-18 15:47:57 +0800697
698 /* [2 GPIOTEST] */
699 for(i = 0; i < total; i++){
liubin281ac462023-07-19 14:22:54 +0800700 ret = gpio_register_test_out(test_gpio[i], 0);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800701 if(-1 == ret){
702 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio[i]);
liubin281ac462023-07-19 14:22:54 +0800703 fail_io[n] = test_gpio[i];
704 n++;
705 }else{
wangyouqiang4c262fa2023-10-17 17:48:03 +0800706 //printf("############gpio [%d] test success############\n", test_gpio[i]);
liubin281ac462023-07-19 14:22:54 +0800707 }
708 }
wangyouqiang2b352b22023-10-21 15:18:43 +0800709 mbtk_adc_close();
liubin281ac462023-07-19 14:22:54 +0800710 return n;
711}
b.liu4f8e8792023-11-01 23:25:22 +0800712
713#elif defined(MBTK_PROJECT_L508)
LUOJian943fd402024-02-07 10:54:25 +0800714
gy.tang141ce612024-07-03 11:01:58 +0800715
716#define READ_GPIO_NUM 22
717
718#define DEBUG_GPIO_TEST 1
719
720mbtk_gpio_test_pin_and_gpio *p_test_gpio =NULL;
721mbtk_gpio_test_pin_and_gpio *p_gnss_gpio = NULL;
722int test_gpio_group_total_pin = 0;
723int gnss_gpio_group_total_pin = 0;
724
725int gpio_test_set_group_pin_to_low(mbtk_gpio_test_pin_and_gpio *p_pin_group_array, int total_num)
726{
727 int i;
728 int gpio_num = 0;
729 int ret = 0;
730
731 if(p_pin_group_array == NULL)
732 return -1;
733
734 for(i=0; i < total_num; i++)
735 {
736 gpio_num = p_pin_group_array[i].gpio_num;
737 ret += gpio_register_set_func_0(gpio_num);
738 gpio_register_set_direction(gpio_num, MBTK_GPIO_OUTPUT);
739 gpio_register_set_value(gpio_num, 0);
740 }
741 return ret;
742}
b.liu85565a72023-11-09 16:47:40 +0800743
744
745int gpio_test_init(void)
746{
747 int i,j,ret;
gy.tang141ce612024-07-03 11:01:58 +0800748 int gpio_num = 0;
749
750
751
752
wangyouqiang89888612023-11-28 18:28:12 +0800753 system("i2cset -y -f 2 0x31 0x12 0x46");
gy.tangf4d7b852024-06-27 18:50:14 +0800754 system("echo 53 > /sys/class/gpio/export"); //gnss uart rx
755 system("echo out > /sys/class/gpio/gpio53/direction");
756 system("echo 54 > /sys/class/gpio/export");//gnss uart tx
757 system("echo out > /sys/class/gpio/gpio54/direction");
758 system("echo 22 > /sys/class/gpio/export");//check pin
gy.tang141ce612024-07-03 11:01:58 +0800759
760 gpio_register_set_func_0(READ_GPIO_NUM);
761 gpio_register_set_direction(READ_GPIO_NUM, 1); //输出
gy.tangf4d7b852024-06-27 18:50:14 +0800762
763 usleep(500);
gy.tang141ce612024-07-03 11:01:58 +0800764/*
765 GPIO22默认是上拉,如果改成GPIO输入模式,其PIN脚电压是高电平,必须改成下拉后GPIO22才处于低电平状态,否则测试PIN拉低失败
766*/
767 gpio_register_set_direction(READ_GPIO_NUM, MBTK_GPIO_INPUT); //设置输入模式
gy.tangf4d7b852024-06-27 18:50:14 +0800768 gpio_register_write(GPIO_FUNC_GPIO_22,0xb040); //PULL DOWN
769
770
gy.tang141ce612024-07-03 11:01:58 +0800771 gpio_test_set_group_pin_to_low(p_gnss_gpio,gnss_gpio_group_total_pin); //set all gnss reserver pin to low
772
773
b.liu85565a72023-11-09 16:47:40 +0800774 /* [1 all gpio set to low] */
gy.tang141ce612024-07-03 11:01:58 +0800775 for(j = 0; j < 6 ; j++){
776 gpio_test_set_group_pin_to_low(p_test_gpio,test_gpio_group_total_pin);
b.liu85565a72023-11-09 16:47:40 +0800777
778 for(i = 0; i <= 5; i++){
gy.tang141ce612024-07-03 11:01:58 +0800779 ret = gpio_register_get_value(READ_GPIO_NUM);
b.liu85565a72023-11-09 16:47:40 +0800780 if(0 == ret){
781 break;
782 }
783 usleep(3000);
784 }
gy.tang141ce612024-07-03 11:01:58 +0800785
b.liu85565a72023-11-09 16:47:40 +0800786 if(0 == ret){
787 break;
788 }
789 }
790
791 if(0 != ret){
gy.tang141ce612024-07-03 11:01:58 +0800792 printf("[GPIOTEST]set all low Fail, please retest\n");
b.liu85565a72023-11-09 16:47:40 +0800793 return -1;
794 }
gy.tang141ce612024-07-03 11:01:58 +0800795 printf("[GPIOTEST]pre set SUCCESS\n");
b.liu85565a72023-11-09 16:47:40 +0800796 return 0;
797}
798
gy.tang141ce612024-07-03 11:01:58 +0800799int 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 +0800800{
gy.tang141ce612024-07-03 11:01:58 +0800801 int ret,i;
wangyouqiang89888612023-11-28 18:28:12 +0800802 int *fail_io = (int *)arg;
gy.tang141ce612024-07-03 11:01:58 +0800803 int failed_num = index;
gy.tangf4d7b852024-06-27 18:50:14 +0800804 int gpio_num = 0;
gy.tang141ce612024-07-03 11:01:58 +0800805 int pin_num = 0;
806 bool falied_flag = FALSE;
807
b.liu85565a72023-11-09 16:47:40 +0800808 for(i = 0; i < total; i++){
gy.tang141ce612024-07-03 11:01:58 +0800809 gpio_num = p_pin_group_array[i].gpio_num;
810 pin_num = p_pin_group_array[i].pin_num;
811 falied_flag = FALSE;
812
813#if DEBUG_GPIO_TEST
814 ret = gpio_register_get_value(gpio_num);
815 printf("[GPIOTEST]pin_num%d default value= %d\n", pin_num, ret);
816#endif
817
b.liu85565a72023-11-09 16:47:40 +0800818 //set high
gy.tangf4d7b852024-06-27 18:50:14 +0800819 gpio_register_set_value(gpio_num, 1);
gy.tang141ce612024-07-03 11:01:58 +0800820
821#if DEBUG_GPIO_TEST
gy.tangf4d7b852024-06-27 18:50:14 +0800822 ret = gpio_register_get_value(gpio_num);
gy.tang141ce612024-07-03 11:01:58 +0800823 printf("[GPIOTEST]pin_num%d set high= %d\n", pin_num, ret);
824#endif
b.liu85565a72023-11-09 16:47:40 +0800825 //get
gy.tang141ce612024-07-03 11:01:58 +0800826 ret = gpio_register_get_value(READ_GPIO_NUM);
b.liu85565a72023-11-09 16:47:40 +0800827 if(1 != ret){
gy.tang141ce612024-07-03 11:01:58 +0800828 printf("[GPIOTEST]pin%d test high failed\n", pin_num);
829 falied_flag = TRUE;
830 }else{
831 printf("[GPIOTEST]pin[%d] test high success\n", pin_num);
832 }
b.liu85565a72023-11-09 16:47:40 +0800833
834 //set low
gy.tang141ce612024-07-03 11:01:58 +0800835 gpio_register_set_value(gpio_num, 0);
836#if DEBUG_GPIO_TEST
837 ret = gpio_register_get_value(gpio_num);
838 printf("[GPIOTEST]pin_num%d set low= %d\n", pin_num, ret);
839#endif
b.liu85565a72023-11-09 16:47:40 +0800840 //get
gy.tang141ce612024-07-03 11:01:58 +0800841 ret = gpio_register_get_value(READ_GPIO_NUM);
b.liu85565a72023-11-09 16:47:40 +0800842 if(0 != ret){
gy.tang141ce612024-07-03 11:01:58 +0800843 printf("[GPIOTEST]pin_num%d test low failed\n", pin_num);
844 falied_flag = TRUE;
845
846 }else{
847 printf("[GPIOTEST]pin_num%d test low success\n", pin_num);
848 }
849
850 if(falied_flag)
851 {
852 fail_io[failed_num] = p_pin_group_array[i].pin_num;
853#if DEBUG_GPIO_TEST
854 printf("[GPIOTEST]NO.%d pin_num%d failed %d\n",i,pin_num, failed_num);
855#endif
856 failed_num++;
gy.tangf4d7b852024-06-27 18:50:14 +0800857 }
b.liu85565a72023-11-09 16:47:40 +0800858 }
gy.tang141ce612024-07-03 11:01:58 +0800859 return failed_num;
b.liu85565a72023-11-09 16:47:40 +0800860}
b.liu85565a72023-11-09 16:47:40 +0800861
b.liu4f8e8792023-11-01 23:25:22 +0800862
gy.tang141ce612024-07-03 11:01:58 +0800863int mbtk_at_gpio(bool gnss_support,void* arg)
b.liu4f8e8792023-11-01 23:25:22 +0800864{
gy.tang141ce612024-07-03 11:01:58 +0800865 int n = 0;
866 mbtk_gpio_test_pin_and_gpio test_gpio[] =
867 {
868 //{GPIO , PIN}
869 {0,100}, //RMII_RX-DV
870 {1,101}, //RMII_RXD0
871 {2,102},//RMII_RXD1
872 {3,103},//RMII_CLK
873 {6,104},//RMII_TXD0
874 {7,105},//RMII_TXD1
875 {15,106},//RMII_TX_EN
876 {16,109},//RMII_MDC
877 {17,110},//RMII_MDIO
878 {18,107},//RMII_INT
879 {99,16},//USB_ID
880 {34,9}, //SPI0_CS
881 {36,8},//SPI0_TXD
882 {35,7},//SPI0_RXD
883 {33,6},//SPI0_CLK
884 {14,51}, //NET_LIGHT
885 {13,49}, //STATUS LED
886 {12,50},//WAKE_IN
887 {49,55}, //I2C SCL
888 {50,56}, //I2C SDA
889 {19,53}, //USIM_DET
890 {43,48}, //SD_DET
891 {118,52}, //WAKE OUT
892 {120,54}, //FLIGHT MODE
893
894
895 {20,45}, //NORMAL GPIO
896 {59,122}, //SDIO_CLK
897 {56,117}, //SDIO_DATA1
898 {55,118},//SDIO_DATA2
899 {58,119},//SDIO_CMD
900 {48,120},//SDIO_DATA3
901 {57,121},//SDIO_DATA0
902 {5,84}, //NORMAL GPIO
903 {4,30},//NORMAL GPIO
904 {21,28}, //NORMAL GPIO
905 {23,69}, //UART_RING
906 {24,70}, //UART_DCD
907 {27,73}, //PCM_OUT
908 {28,74}, //PCM_IN
909 {26,75}, //PCM_SYNC
910 {25,76}, //PCM_CLK
911
912 {126,112},//NORMAL GPIO or pwm3
913 {117,123}, //PWM
914 {125,116}, //CLK_REQ
915
916 //{54,114}, //debug uart2, no need be tested
917 //{53,115}, //debug uart2, no need be tested
918
919 };
920
921 mbtk_gpio_test_pin_and_gpio GNSS_test_gpio[] = {
922 {54,114}, //GPS UTXD, UART1_TXD,L508LAN+L508CN(D)+L508EN+L508LEN+L508TLCN NOT CONNECTED
923 {53,115}, //GPS URXD,UART1-RXD,L508LAN+L508CN(D)+L508EN+L508LEN+L508TLCN NOT CONNECTED
924 {32,66}, //PIN66,host wake GPS, RTS, L508TLCN_V2+L508LEN_V2+L508EN_V2 NOT CONNECTED
925 {31,67}, //PIN67,GPS wake Host,CTS, L508TLCN_V2+L508LEN_V2+L508EN_V2 NOT CONNECTED
926 {123,42},//GPS_EN, L508TLCN_V2+L508LEN_V2+L508EN_V2 NOT CONNECTED
927 {122,35}, //32K OUT, L508TLCN_V2+L508LEN_V2+L508EN_V2 not connected
928 };
929 p_gnss_gpio = GNSS_test_gpio;
930 gnss_gpio_group_total_pin = (sizeof(GNSS_test_gpio)/sizeof(GNSS_test_gpio[0]));
931
932 p_test_gpio = test_gpio;
933 test_gpio_group_total_pin = (sizeof(test_gpio)/sizeof(test_gpio[0]));
934
935 if(!gnss_support)
936 n = test_gpio_group_total_pin+gnss_gpio_group_total_pin;
937 else
938 n = test_gpio_group_total_pin;
939
940 printf("[init] L508 XX GPIOTEST v1.3 total pin=%d\n",n);
b.liu85565a72023-11-09 16:47:40 +0800941
LUOJian943fd402024-02-07 10:54:25 +0800942 n = gpio_test_init();
gy.tangf4d7b852024-06-27 18:50:14 +0800943 if(-1 == n)
944 {
gy.tang141ce612024-07-03 11:01:58 +0800945 goto gpiotest_finished;
gy.tangf4d7b852024-06-27 18:50:14 +0800946 }
gy.tang141ce612024-07-03 11:01:58 +0800947 n = 0;
b.liu85565a72023-11-09 16:47:40 +0800948
gy.tang141ce612024-07-03 11:01:58 +0800949 n = gpio_test(arg,0,p_test_gpio,test_gpio_group_total_pin);
950
951 //if the module has gnss chip, don't test the gnss's pins
952 if(!gnss_support)
953 {
954 n = gpio_test(arg,n,p_gnss_gpio,gnss_gpio_group_total_pin);
955 }
956
957gpiotest_finished:
gy.tangf4d7b852024-06-27 18:50:14 +0800958 system("echo 53 > /sys/class/gpio/unexport"); //unexport gnss uart rx
959 system("echo 54 > /sys/class/gpio/unexport");//unexportgnss uart tx
960 system("echo 22 > /sys/class/gpio/unexport");//unexportcheck pin
961
gy.tang141ce612024-07-03 11:01:58 +0800962 printf("\n[GPIOTEST] Finished %d!!\n\n",n);
b.liu85565a72023-11-09 16:47:40 +0800963 return n;
b.liu4f8e8792023-11-01 23:25:22 +0800964}
965
b.liu85565a72023-11-09 16:47:40 +0800966
967
b.liu4f8e8792023-11-01 23:25:22 +0800968#elif defined(MBTK_PROJECT_L509)
gy.tang71ea7ff2024-06-21 16:01:56 +0800969#define DEBUG_GPIO_TEST 0
970mbtk_gpio_paired_info_struct test_pin_array[] ={ //是GPIO_array
971//output_pin, output_gpio,input_gpio,input_pin,test_result
b.liu85565a72023-11-09 16:47:40 +0800972//120 5 33 这几个输入脚没法正常设置为输入
gy.tang71ea7ff2024-06-21 16:01:56 +0800973 //{?,52,51,?,0}, //拉不低 没有这一组
974 //{?,54,53,?,0},
975 //Opin,Ogpio,Igpio,Ipin,res
976 {122, 16, 17, 121, 0}, //output_pin, output_gpio,input_gpio,input_pin,test_result
977 {1, 117, 120, 2, 0},
978 //{?,118,19,13,?,0}, //usim
979 {5, 123, 126, 6, 0},
980 {23, 43, 23, 62, 0},
981 {24, 28, 27, 25, 0},
982 {26, 26, 25, 27, 0},
983 {37, 34, 33, 40, 0},
984 {38, 36, 35, 39, 0},
985 {41, 49, 50, 42, 0},
986 {64, 32, 31, 65, 0},
987 {78, 7, 2, 73, 0},
988 {77, 6, 1, 76, 0},
989 {81, 15, 3, 75, 0},
990 {74, 0, 18, 120,0},
991 {79, 4, 5, 82, 0},
992 {80, 13, 14, 84, 0},
993 {129, 48, 55, 130,0},
994 {131, 56, 57, 132,0},
995 {133, 59, 58, 134,0},
996 {135, 21, 99, 139,0},
997 //{?,20,22,?,0}, //拉不低
998 //{?,12,122,?,0}, //拉不低 122好像不该用
b.liu85565a72023-11-09 16:47:40 +0800999};
1000//测低
gy.tang71ea7ff2024-06-21 16:01:56 +08001001uint16 mbtk_gpio_test_all_low(mbtk_gpio_paired_info_struct *pin_array, uint16 MAX_pin_num)
b.liu85565a72023-11-09 16:47:40 +08001002{
1003 uint16 index =0;
1004 uint16 OUT_pin, Read_pin;
1005 uint32 read_high_value;
1006 uint32 read_low_value;
1007 uint16 fail_num=0;
1008 int res;
1009
gy.tang71ea7ff2024-06-21 16:01:56 +08001010 printf("[GPIOTEST][run test all pin low]\n");
b.liu85565a72023-11-09 16:47:40 +08001011
1012
1013 for(index = 0; index < MAX_pin_num; index++){
gy.tang71ea7ff2024-06-21 16:01:56 +08001014 OUT_pin = pin_array[index].output_gpio;
1015 Read_pin = pin_array[index].input_gpio;
b.liu85565a72023-11-09 16:47:40 +08001016
1017 //输出低
1018 gpio_register_set_direction(OUT_pin,1);
1019 gpio_register_set_value(OUT_pin, 0);
1020
1021 //输入脚设为输入
1022 gpio_register_set_direction(Read_pin,0);
1023 }
1024
1025 for(index = 0; index < MAX_pin_num; index++){
gy.tang71ea7ff2024-06-21 16:01:56 +08001026 OUT_pin = pin_array[index].output_gpio;
1027 Read_pin = pin_array[index].input_gpio;
b.liu85565a72023-11-09 16:47:40 +08001028 read_low_value = gpio_register_get_value(Read_pin);
1029 //结果检测
1030 if(read_low_value != 0){
gy.tang71ea7ff2024-06-21 16:01:56 +08001031 printf("[GPIOTEST][!Low Failed!]: GPIO%d PIN%d\n", Read_pin,pin_array[index].input_pin);
b.liu85565a72023-11-09 16:47:40 +08001032 pin_array[index].mbtk_gpio_test_result = 1;
1033 fail_num ++;
1034 }
1035 else{
gy.tang71ea7ff2024-06-21 16:01:56 +08001036#if DEBUG_GPIO_TEST
1037 printf("[GPIOTEST][-Low Success-]: GPIO%d \n", Read_pin);
1038#endif
b.liu85565a72023-11-09 16:47:40 +08001039 }
1040 }
1041 return fail_num;
1042
1043}
1044
1045
1046
1047//测高
gy.tang71ea7ff2024-06-21 16:01:56 +08001048uint16 mbtk_gpio_test_pin_high(mbtk_gpio_paired_info_struct *pin_array,uint16 MAX_pin_num)
b.liu85565a72023-11-09 16:47:40 +08001049{
1050 uint16 index =0;
1051 uint16 OUT_pin, Read_pin;
1052 uint32 read_high_value;
1053 uint32 read_low_value;
1054 uint16 fail_num=0;
1055 int i = 0;
1056
gy.tang71ea7ff2024-06-21 16:01:56 +08001057 printf("[GPIOTEST][run test pin high]\n");
b.liu85565a72023-11-09 16:47:40 +08001058
1059 for(index = 0;index<MAX_pin_num;index++)
1060 {
gy.tang71ea7ff2024-06-21 16:01:56 +08001061 OUT_pin = pin_array[index].output_gpio;
1062 Read_pin = pin_array[index].input_gpio;
b.liu85565a72023-11-09 16:47:40 +08001063
1064 //设高
1065 gpio_register_set_direction(OUT_pin,1);
1066 gpio_register_set_direction(Read_pin,0);
1067 gpio_register_set_value(OUT_pin, 1);
1068 usleep(20000);
1069 read_high_value = gpio_register_get_value(Read_pin);
gy.tang71ea7ff2024-06-21 16:01:56 +08001070#if DEBUG_GPIO_TEST
1071 printf("[GPIOTEST][test_high]: PIN%d: get value =%d \n", pin_array[index].input_pin, read_high_value);
1072#endif
b.liu85565a72023-11-09 16:47:40 +08001073
1074 //设高后重新设低
1075 usleep(20000);
1076 gpio_register_set_value(OUT_pin, 0);
1077 usleep(80000);
1078 //read_low_value = gpio_register_get_value(Read_pin);
1079 //read_low_value = gpio_register_get_value(Read_pin);
1080 for(i = 0;i < 10; i++){
1081 read_low_value = gpio_register_get_value(Read_pin);
1082 if(0 == read_low_value){
1083 break;
1084 }
1085 usleep(20000);
1086 }
gy.tang71ea7ff2024-06-21 16:01:56 +08001087#if DEBUG_GPIO_TEST
1088 printf("[GPIOTEST][test_low]: PIN%d: get value =%d \n", pin_array[index].input_pin, read_low_value);
1089#endif
b.liu85565a72023-11-09 16:47:40 +08001090 if(read_high_value != 1||read_low_value !=0)
1091 {
gy.tang71ea7ff2024-06-21 16:01:56 +08001092 printf("[GPIOTEST][!High Failed!]: Gpio%d PIN%d\n",Read_pin,pin_array[index].input_pin);
b.liu85565a72023-11-09 16:47:40 +08001093 pin_array[index].mbtk_gpio_test_result = 2;
1094 fail_num ++;
1095 }
1096 else{
gy.tang71ea7ff2024-06-21 16:01:56 +08001097#if DEBUG_GPIO_TEST
1098 printf("[GPIOTEST][-High Success-]: Gpio%d \n", Read_pin);
1099#endif
b.liu85565a72023-11-09 16:47:40 +08001100 }
1101 }
1102 return fail_num;
1103
1104}
gy.tang71ea7ff2024-06-21 16:01:56 +08001105void gpio_test_init_test_gpio_mode(mbtk_gpio_paired_info_struct pin_array[],int MAX_pin_num)
b.liu85565a72023-11-09 16:47:40 +08001106{
1107 int index =0;
1108 int OUT_pin, Read_pin;
1109
gy.tangf4d7b852024-06-27 18:50:14 +08001110 printf("[GPIOTEST] L509 GPIOTEST v2.1 --init\n");
gy.tang71ea7ff2024-06-21 16:01:56 +08001111
b.liu85565a72023-11-09 16:47:40 +08001112 for(index = 0;index<MAX_pin_num;index++)
1113 {
gy.tang71ea7ff2024-06-21 16:01:56 +08001114 OUT_pin = pin_array[index].output_gpio;
1115 Read_pin = pin_array[index].input_gpio;
gy.tang5656a592024-06-19 14:04:54 +08001116 pin_array[index].mbtk_gpio_test_result = 0; //init as passed
b.liu85565a72023-11-09 16:47:40 +08001117 gpio_register_set_func_0(OUT_pin);
1118 gpio_register_set_func_0(Read_pin);
1119 }
1120
gy.tang5656a592024-06-19 14:04:54 +08001121 //gpio_register_set_func_0();
1122 system("i2cset -y -f 2 0x31 0x12 0x46");
1123 system("echo 33 > /sys/class/gpio/export");
1124 system("echo 120 > /sys/class/gpio/export");
1125 system("echo 126 > /sys/class/gpio/export");
1126 system("echo in > /sys/class/gpio/gpio126/direction");
1127 system("echo in > /sys/class/gpio/gpio120/direction");
1128 system("echo in > /sys/class/gpio/gpio5/direction");
1129 system("echo in > /sys/class/gpio/gpio33/direction");
gy.tang71ea7ff2024-06-21 16:01:56 +08001130#if DEBUG_GPIO_TEST
1131 printf("[GPIOTEST]gpio_test_init_test_gpio_mode FINISH\n");
1132#endif
b.liu85565a72023-11-09 16:47:40 +08001133
1134}
1135
b.liu4f8e8792023-11-01 23:25:22 +08001136
b.liu94091352024-07-02 10:38:08 +08001137int mbtk_at_gpio(bool gnss_support, void* arg)
b.liu4f8e8792023-11-01 23:25:22 +08001138{
gy.tang71ea7ff2024-06-21 16:01:56 +08001139 int i, num;
1140 int *fail_io = (int *)arg;
1141 /*OPEN SWITCH */
1142 uint16 test_fail_count = 0;
1143 uint16 test_MAX_pin_num = 0;
1144 char buf[STR_MAX_LEN];
1145 uint16 str_len=0;
1146 uint16 temp_len =0;
1147 uint16 Out_pin;
1148 uint16 in_pin;
1149 uint16 fail_print_num = 0;
b.liu85565a72023-11-09 16:47:40 +08001150
gy.tang71ea7ff2024-06-21 16:01:56 +08001151 gpio_register_set_func_0(125);
1152 gpio_register_set_direction(125, 1);
1153 gpio_register_set_value(125, 1); //开关使能脚
1154
b.liu85565a72023-11-09 16:47:40 +08001155
1156 /* [3]初始化待测GPIO */
gy.tang71ea7ff2024-06-21 16:01:56 +08001157 test_MAX_pin_num = sizeof(test_pin_array)/sizeof(test_pin_array[0]);
1158
1159 gpio_test_init_test_gpio_mode(test_pin_array,test_MAX_pin_num);
b.liu85565a72023-11-09 16:47:40 +08001160
1161
1162
1163 /* [4]测试过程 */
gy.tang71ea7ff2024-06-21 16:01:56 +08001164 test_fail_count = mbtk_gpio_test_all_low(test_pin_array,test_MAX_pin_num);
b.liu85565a72023-11-09 16:47:40 +08001165
gy.tang71ea7ff2024-06-21 16:01:56 +08001166 if(!test_fail_count) //set all pin low passed
1167 {
1168 printf("[GPIOTEST]set all pin low: passed\n");
1169 test_fail_count = mbtk_gpio_test_pin_high(&test_pin_array,test_MAX_pin_num);
1170 }
1171 else
1172 {
1173 printf("[GPIOTEST]set all pin low: failed num=%d!!!\n",test_fail_count);
1174 }
b.liu85565a72023-11-09 16:47:40 +08001175
gy.tang71ea7ff2024-06-21 16:01:56 +08001176
1177 //memset(buf,0,STR_MAX_LEN);
1178
b.liu85565a72023-11-09 16:47:40 +08001179 /* [5]测试结果检测 */
gy.tang71ea7ff2024-06-21 16:01:56 +08001180 if(test_fail_count )
1181 {
1182 //printf( "GPIOTEST Fail %02d PINs:\n", test_fail_count*2);
1183 sprintf(buf, "GPIOTEST Fail %02d PINs:", test_fail_count*2);
1184 temp_len = strlen(buf);
b.liu85565a72023-11-09 16:47:40 +08001185
gy.tang71ea7ff2024-06-21 16:01:56 +08001186 num = 0;
1187 for(i = 0; i< test_MAX_pin_num; i++)
1188 {
b.liu85565a72023-11-09 16:47:40 +08001189
gy.tang71ea7ff2024-06-21 16:01:56 +08001190 if(test_pin_array[i].mbtk_gpio_test_result)
1191 {
b.liu85565a72023-11-09 16:47:40 +08001192
gy.tang71ea7ff2024-06-21 16:01:56 +08001193 Out_pin = test_pin_array[i].output_pin;
1194 in_pin = test_pin_array[i].input_pin;
1195 fail_io[num++] = Out_pin;
1196 fail_io[num++] = in_pin;
b.liu85565a72023-11-09 16:47:40 +08001197
gy.tang71ea7ff2024-06-21 16:01:56 +08001198 }
1199 }
1200 }
b.liu85565a72023-11-09 16:47:40 +08001201
gy.tang71ea7ff2024-06-21 16:01:56 +08001202 else
1203 {
b.liu85565a72023-11-09 16:47:40 +08001204 printf(buf,"ALL GPIO TEST PASS\r\n");
gy.tang71ea7ff2024-06-21 16:01:56 +08001205 }
gy.tangf4d7b852024-06-27 18:50:14 +08001206 system("echo 33 > /sys/class/gpio/unexport");
1207 system("echo 120 > /sys/class/gpio/unexport");
1208 system("echo 126 > /sys/class/gpio/unexport");
1209 printf("\n[GPIOTEST] Finished !!\n\n");
wangyouqiang97a792f2024-04-01 13:28:06 +08001210 return test_fail_count * 2;
b.liu4f8e8792023-11-01 23:25:22 +08001211}
1212
wangyouqiangece1c722023-12-12 17:28:53 +08001213#elif defined(MBTK_PROJECT_L508_X6)
wangyouqiangece1c722023-12-12 17:28:53 +08001214int gpio_register_test_out_0(int port, int value)
1215{
1216 int ret;
1217 int i;
1218 int valueh = 0;
1219 int valuel = 1;
1220
1221 printf("Gpio port [%d] test start!\n", port);
1222 ret = gpio_register_set_func_0(port); //设功能为GPIO
1223 if(ret){
1224 printf("gpio_port can't support!\n");
1225 return -1;
1226 }
1227 gpio_register_set_direction(port, 1); //设方向为输出
1228 ret = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001229 //printf("gpio default value is : %d.\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001230
1231
1232 //[High]
1233 for(i = 0; i <= 10; i++){
1234 gpio_register_set_value(port, 1);
1235 usleep(50);
1236 valueh = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001237 //printf("set high? %d\n",valueh);
wangyouqiangece1c722023-12-12 17:28:53 +08001238 if(1 == valueh){
1239 break;
1240 }
1241 }
1242 //usleep(10000);
1243 for(i = 0; i <= 35; i++){
1244 ret = mbtk_gpio_adc(0);
1245 usleep(3000);
1246 if(1 == ret){
1247 break;
1248 }
1249 }
b.liuc7ffd092024-01-03 15:23:07 +08001250 //printf("******gpio should is high: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001251 if(1 != ret){
1252 ret=-1;
1253 goto exit;
1254 }
1255
1256
1257
1258 //[Low]
1259 usleep(200);
1260 for(i = 0; i <= 10; i++){
1261 gpio_register_set_value(port, 0); //输出低
1262 usleep(50);
1263 valuel = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001264 //printf("set low? %d\n",valuel);
wangyouqiangece1c722023-12-12 17:28:53 +08001265 if(0 == valuel){
1266 break;
1267 }
1268 }
1269 //usleep(10000);
1270 for(i = 0; i <= 35; i++){
1271 ret = mbtk_gpio_adc(0);
1272 usleep(3000);
1273 if(0 == ret){
1274 break;
1275 }
1276 }
b.liuc7ffd092024-01-03 15:23:07 +08001277 //printf("******gpio should is low: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001278 if(0 != ret){
1279 ret=-1;
1280 goto exit;
1281 }
1282
1283exit:
1284 gpio_register_set_value(port, 0);
1285 gpio_register_set_direction(port, 0); //设方向为输入
1286 return ret;
1287}
1288
1289int gpio_register_test_out_1(int port, int value)
1290{
1291 int ret;
1292 int i;
1293 int valueh = 0;
1294 int valuel = 1;
1295
1296 printf("Gpio port [%d] test start!\n", port);
1297 ret = gpio_register_set_func_0(port); //设功能为GPIO
1298 if(ret){
1299 printf("gpio_port can't support!\n");
1300 return -1;
1301 }
1302 gpio_register_set_direction(port, 1); //设方向为输出
1303 ret = gpio_register_get_value(port);
1304 printf("gpio default value is : %d.\n", ret);
1305
1306
1307 //[High]
1308 for(i = 0; i <= 10; i++){
1309 gpio_register_set_value(port, 1);
1310 usleep(50);
1311 valueh = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001312 //printf("set high? %d\n",valueh);
wangyouqiangece1c722023-12-12 17:28:53 +08001313 if(1 == valueh){
1314 break;
1315 }
1316 }
1317 //usleep(10000);
1318 for(i = 0; i <= 35; i++){
1319 ret = mbtk_gpio_adc(1);
1320 usleep(3000);
1321 if(1 == ret){
1322 break;
1323 }
1324 }
1325 if(1 != ret){
1326 ret=-1;
1327 goto exit;
1328 }
b.liuc7ffd092024-01-03 15:23:07 +08001329 //printf("******gpio should is high: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001330
1331
1332 //[Low]
1333 usleep(200);
1334 for(i = 0; i <= 10; i++){
1335 gpio_register_set_value(port, 0); //输出低
1336 usleep(50);
1337 valuel = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001338 //printf("set low? %d\n",valuel);
wangyouqiangece1c722023-12-12 17:28:53 +08001339 if(0 == valuel){
1340 break;
1341 }
1342 }
1343 //usleep(10000);
1344 for(i = 0; i <= 35; i++){
1345 ret = mbtk_gpio_adc(1);
1346 usleep(3000);
1347 if(0 == ret){
1348 break;
1349 }
1350 }
b.liuc7ffd092024-01-03 15:23:07 +08001351 //printf("******gpio should is low: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001352 if(0 != ret){
1353 ret=-1;
1354 goto exit;
1355 }
1356
1357exit:
wangyouqiang7775cbf2023-12-29 16:35:24 +08001358 gpio_register_set_value(port, 0);
1359 gpio_register_set_direction(port, 0); //设方向为输入
wangyouqiangece1c722023-12-12 17:28:53 +08001360 return ret;
1361}
1362
wangyouqiang7775cbf2023-12-29 16:35:24 +08001363
1364int gpio_register_test_out_2(int port_in, int port_out, int value)
1365{
1366 int ret_in;
1367 int ret_out;
1368 int i;
1369 int valueh = 0;
1370 int valuel = 1;
1371
1372 printf("Gpio port [%d] test start!\n", port_out);
1373 ret_in = gpio_register_set_func_0(port_in); //设功能为GPIO
1374 ret_out = gpio_register_set_func_0(port_out);
1375 if((ret_in+ret_out)){
1376 printf("gpio_port can't support!\n");
1377 return -1;
1378 }
1379 gpio_register_set_direction(port_out, 1); //设方向为输出
1380 gpio_register_set_direction(port_in, 0); //设方向为输入
1381 ret_in = gpio_register_get_value(port_in);
1382 printf("gpio default value is : %d.\n", ret_in);
1383
1384
1385 //[High]
1386 for(i = 0; i <= 10; i++){
1387 gpio_register_set_value(port_out, 1);
1388 usleep(50);
1389 valueh = gpio_register_get_value(port_out);
1390 printf("set high? %d\n",valueh);
1391 if(1 == valueh){
1392 break;
1393 }
1394 }
1395
1396
1397 for(i = 0; i <= 35; i++){
1398 ret_in = gpio_register_get_value(port_in);
1399 usleep(3000);
1400 if(1 == ret_in){
1401 break;
1402 }
1403 }
1404 if(1 != ret_in){
1405 ret_in=-1;
1406 printf("get high failed! \n");
1407 goto exit;
1408 }
1409 printf("******gpio should is high: %d.******\n", ret_in);
1410
1411
1412 //[Low]
1413 usleep(200);
1414 for(i = 0; i <= 10; i++){
1415 gpio_register_set_value(port_out, 0); //输出低
1416 usleep(50);
1417 valuel = gpio_register_get_value(port_out);
1418 printf("set low? %d\n",valuel);
1419 if(0 == valuel){
1420 break;
1421 }
1422 }
1423
1424 for(i = 0; i <= 35; i++){
1425 ret_in = gpio_register_get_value(port_in);
1426 usleep(3000);
1427 if(0 == ret_in){
1428 break;
1429 }
1430 }
1431 printf("******gpio should is low: %d.******\n", ret_in);
1432 if(0 != ret_in){
1433 ret_in=-1;
1434 printf("get low failed! \n");
1435 goto exit;
1436 }
1437
1438exit:
1439 gpio_register_set_value(port_out, 0);
1440 gpio_register_set_direction(port_out, 0); //设方向为输入
1441 return ret_in;
1442}
1443
1444
wangyouqiangece1c722023-12-12 17:28:53 +08001445void gpio_test_init_test_gpio_mode(void )
1446{
1447
1448
1449 int test_gpio_1[] = {
1450 40, 33, 34, 39, 99,
1451 56, 58, 55, 57, 48, 59, 123, 122, 20, 53,
1452 19, 49, 50, 32 ,31, 6, 11, 10,
1453 88, 87, 86, 85, 84, 89,
wangyouqiang7775cbf2023-12-29 16:35:24 +08001454 90, 46, 127, 117, 119,
wangyouqiangece1c722023-12-12 17:28:53 +08001455 };
1456
1457 int test_gpio_0[] = {
1458 7, 8, 27, 28, 26, 25, 15, 1, 3, 0,
1459 37, 38,35, 36, 42, 41, 21, 22, 24, 23,
1460 54, 125, 18, 13, 14, 17, 16, 5, 4, 2, 12,
1461 44, 45, 47
1462 };
1463 int i, j, ret_0, ret_1, total_1, total_0, n = 0;
1464 total_0 = (sizeof(test_gpio_0)/sizeof(int));
1465 total_1 = (sizeof(test_gpio_1)/sizeof(int));
1466
wangyouqiang7775cbf2023-12-29 16:35:24 +08001467 //system("at at+gpsinit=0");
1468 system("i2cset -y -f 2 0x32 0x0d 0x00");
1469 system("i2cset -y -f 2 0x31 0x18 0x8f");
1470 usleep(10000);
wangyouqiangece1c722023-12-12 17:28:53 +08001471
1472 //printf("[init]gpio_test_init_test_gpio_mode FINISH\n");
1473 /* [1 all gpio set to low] */
1474 for(j = 0; j < 6 ; j++){
1475 for(i = 0; i < total_1; i++){
1476 gpio_register_set_func_0(test_gpio_1[i]);
1477 gpio_register_set_direction(test_gpio_1[i], 1);
1478 gpio_register_set_value(test_gpio_1[i], 0);
1479 }
1480 for(i = 0; i < total_0; i++){
1481 gpio_register_set_func_0(test_gpio_0[i]);
1482 gpio_register_set_direction(test_gpio_0[i], 1);
1483 gpio_register_set_value(test_gpio_0[i], 0);
1484 }
1485 //usleep(10000);
1486
1487 for(i = 0; i <= 35; i++){
1488 ret_0 = mbtk_gpio_adc(0);
1489 usleep(3000);
1490 ret_1 = mbtk_gpio_adc(1);
1491
1492 if(0 == (ret_0 + ret_1)){
1493 break;
1494 }
1495 }
1496 printf("pre set ADC: %d, times: %d\n",(ret_0 + ret_1), j);
1497 if(0 == (ret_0 + ret_1)){
1498 break;
1499 }
1500 }
1501 if(0 != (ret_0 + ret_1)){
wangyouqiang7775cbf2023-12-29 16:35:24 +08001502
wangyouqiangece1c722023-12-12 17:28:53 +08001503 printf("Fail, please retest");
1504 return -1;
1505 }
wangyouqiang7775cbf2023-12-29 16:35:24 +08001506 gpio_register_set_value(119, 1);
1507 gpio_register_set_value(117, 1);
wangyouqiangece1c722023-12-12 17:28:53 +08001508
1509}
1510
b.liu94091352024-07-02 10:38:08 +08001511int mbtk_at_gpio(bool gnss_support, void* arg)
wangyouqiangece1c722023-12-12 17:28:53 +08001512{
wangyouqiang7775cbf2023-12-29 16:35:24 +08001513
1514 printf("Start test gpio V1.0\n");
wangyouqiangece1c722023-12-12 17:28:53 +08001515 int test_gpio_1[] = {
1516 40, 33, 34, 39, 99,
1517 56, 58, 55, 57, 48, 59, 20, 53,
1518 19, 49, 50, 32 ,31, 6, 11, 10,7,
wangyouqiang7775cbf2023-12-29 16:35:24 +08001519 90, 46, 127,
1520 88, 87, 86, 85, 84, 89,
wangyouqiangece1c722023-12-12 17:28:53 +08001521 };
1522
1523 int test_gpio_0[] = {
1524 35, 36, 42, 41, 21, 22, 24, 23,
1525 54, 125, 18, 13, 14, 17, 16, 5, 4, 2, 12,0,
1526 27, 28, 26, 25, 15, 1, 3,
1527 37, 38,8
wangyouqiang7775cbf2023-12-29 16:35:24 +08001528 };
1529 int test_gpio_2[] = { //GPS组
1530 43,47,
1531 //45,44
wangyouqiangece1c722023-12-12 17:28:53 +08001532 };
1533
wangyouqiang7775cbf2023-12-29 16:35:24 +08001534 int i, j, ret_0, ret_1, ret_2, total_1, total_0, total_2, n = 0;
wangyouqiangece1c722023-12-12 17:28:53 +08001535 int *fail_io = (int *)arg;
1536 total_0 = (sizeof(test_gpio_0)/sizeof(int));
1537 total_1 = (sizeof(test_gpio_1)/sizeof(int));
wangyouqiang7775cbf2023-12-29 16:35:24 +08001538 total_2 = (sizeof(test_gpio_2)/sizeof(int));
wangyouqiangece1c722023-12-12 17:28:53 +08001539
1540 /* [1 all gpio Init] */
1541 gpio_test_init_test_gpio_mode();
1542
1543
1544 /* [2 GPIOTEST] */
wangyouqiangece1c722023-12-12 17:28:53 +08001545 //Test 0
1546 for(i = 0; i < total_0; i++){
1547 ret_0 = gpio_register_test_out_0(test_gpio_0[i], 0);
1548 if(-1 == ret_0){
1549 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_0[i]);
1550 fail_io[n] = test_gpio_0[i];
1551 n++;
1552 }else{
b.liuc7ffd092024-01-03 15:23:07 +08001553 //printf("############gpio [%d] test success############\n", test_gpio_0[i]);
wangyouqiangece1c722023-12-12 17:28:53 +08001554 }
1555 }
1556
1557 //Test 1
1558 for(i = 0; i < total_1; i++){
1559 ret_1 = gpio_register_test_out_1(test_gpio_1[i], 0);
1560 if(-1 == ret_1){
1561 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_1[i]);
1562 fail_io[n] = test_gpio_1[i];
1563 n++;
1564 }else{
b.liuc7ffd092024-01-03 15:23:07 +08001565 //printf("############gpio [%d] test success############\n", test_gpio_1[i]);
wangyouqiangece1c722023-12-12 17:28:53 +08001566 }
1567 }
wangyouqiang7775cbf2023-12-29 16:35:24 +08001568
1569 //Test 2
1570 for(i = 0; i < total_2; i=i+2){
1571 ret_2 = gpio_register_test_out_2(test_gpio_2[i], test_gpio_2[i+1], 0);
1572 if(-1 == ret_2){
b.liuc7ffd092024-01-03 15:23:07 +08001573 //printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_2[i+1]);
wangyouqiang7775cbf2023-12-29 16:35:24 +08001574 fail_io[n] = test_gpio_2[i];
1575 n++;
1576 }else{
1577 printf("############gpio [%d] test success############\n", test_gpio_2[i+1]);
1578 }
1579 }
1580
wangyouqiangece1c722023-12-12 17:28:53 +08001581 mbtk_adc_close();
1582 return n;
1583}
1584
wangyouqiang48adc142024-06-24 20:07:30 +08001585#elif 0//(MBTK_PROJECT_T108)
wangyouqiangb1deff32024-03-05 13:31:27 +08001586int gpio_register_test_out(int port, int value)
1587{
1588 int ret;
1589 int i;
1590 int valueh = 0;
1591 int valuel = 1;
1592
1593 //printf("Gpio port [%d] test start!\n", port);
1594 ret = gpio_register_set_func_0(port); //设功能为GPIO
1595 if(ret){
1596 printf("gpio_port can't support!\n");
1597 return -1;
1598 }
1599 gpio_register_set_direction(port, 1); //设方向为输出
1600 //ret = gpio_register_get_value(port);
1601 //printf("gpio default value is: %d\n", ret);
1602
1603
1604 //[High]
1605 for(i = 0; i <= 9; i++){
1606 gpio_register_set_value(port, 1);
1607 usleep(50);
1608 //valueh = gpio_register_get_value(port);
1609 //printf("set high? %d\n", valueh);
1610 if(1 == valueh){
1611 break;
1612 }
1613 }
1614
1615 for(i = 0; i <= 35; i++){
1616 ret = gpio_register_get_value(118);
1617 if(1 == ret){
1618 break;
1619 }
1620 usleep(3000);
1621 }
1622 printf("******gpio should is high: %d.******\n", ret);
1623 if(1 != ret){
1624 ret=-1;
1625 goto exit;
1626 }
1627
1628 //[Low]
1629 usleep(200);
1630 for(i = 0; i <= 9; i++){
1631 gpio_register_set_value(port, 0); //输出低
1632 usleep(50);
1633 //valuel = gpio_register_get_value(port);
1634 //printf("set low? %d\n", valuel);
1635 if(0 == valuel){
1636 break;
1637 }
1638 }
1639
1640 for(i = 0; i <= 35; i++){
1641 ret = gpio_register_get_value(118);
1642 if(0 == ret){
1643 break;
1644 }
1645 usleep(3000);
1646 }
1647 //printf("******gpio should is low: %d.******\n", ret);
1648 if(0 != ret){
1649 ret=-1;
1650 goto exit;
1651 }
1652
1653exit:
1654 gpio_register_set_value(port, 0);
1655 gpio_register_set_direction(port, 0); //设方向为输入
1656 return ret;
1657}
1658
1659int gpio_test_init_test_gpio_mode(void)
1660{
wangyouqiang48adc142024-06-24 20:07:30 +08001661 const int test_gpio[] = {
wangyouqiangb1deff32024-03-05 13:31:27 +08001662 99,
1663 117,
1664 21,22,23,24,44,41,120,
1665 8,127,46,59,58,57,56,55,48,19,34,33,35,36,49,
1666 50,25,28,26,122,20,10,11,
1667 39,40,37,38,
1668 51,52,31,32,
1669 };
1670
1671 int i, j, ret, total, n = 0;
1672 total = (sizeof(test_gpio)/sizeof(int));
1673 printf("[init]gpio_test_init_test_gpio_mode BEGIN\n");
1674
1675 /* [1 all gpio set to low] */
wangyouqiangb1deff32024-03-05 13:31:27 +08001676 gpio_register_set_func_0(118);
1677 gpio_register_set_direction(118, 0);
1678 system("echo in > /sys/class/gpio/gpio118/direction");
1679
1680 for(j = 0; j < 6 ; j++){
1681 for(i = 0; i < total; i++){
1682 gpio_register_set_func_0(test_gpio[i]);
1683 gpio_register_set_direction(test_gpio[i], 1);
1684 gpio_register_set_value(test_gpio[i], 0);
1685 //ret = gpio_register_get_value(test_gpio[i]);
1686 //printf("[init]get gpio%d=%d\n", test_gpio[i], ret);
1687 }
wangyouqiangb1deff32024-03-05 13:31:27 +08001688
1689 for(i = 0; i <= 35; i++){
1690 ret = gpio_register_get_value(118);
1691 printf("[init]get gpi118=%d\n", ret);
1692 usleep(3000);
1693 if(0 == (ret)){
1694 break;
1695 }
1696 }
1697
1698 printf("pre set ADC: %d, times: %d\n",(ret), j);
1699 if(0 == (ret)){
1700 break;
1701 }
1702 }
1703 if(0 != (ret)){
1704 printf("!!!Set all low FAIL, please retest\n");
1705 return -1;
1706 }
1707 return 0;
1708}
1709
b.liu94091352024-07-02 10:38:08 +08001710int mbtk_at_gpio(bool gnss_support, void* arg)
wangyouqiangb1deff32024-03-05 13:31:27 +08001711{
wangyouqiangb1deff32024-03-05 13:31:27 +08001712
wangyouqiang48adc142024-06-24 20:07:30 +08001713 const int test_gpio[] = {
1714 //GPIO PIN GPIO PIN GPIO PIN GPIO PIN
1715 99, 170, 117, 59, 21, 61, 22, 62,
1716 23, 144, 24, 147, 44, 5, 41, 159,
1717 120, 143, 8, 171, 127, 160, 46, 149,
1718 59, 19, 58, 18, 57, 20, 56, 21,
1719 55, 22, 48, 23, 19, 3, 34, 79,
1720 33, 80, 35, 78, 36, 77, 49, 43,
1721 50, 42, 25, 67, 28, 66, 26, 65,
1722 122, 169, 20, 152, 10, 74, 11, 73,
1723 39, 166, 40, 164, 37, 165, 38, 163,
1724 51, 58, 52, 60, 31, 57, 32, 56,
wangyouqiangb1deff32024-03-05 13:31:27 +08001725 };
1726
1727 int i, n = 0, ret, total;
1728 int *fail_io = (int *)arg;
wangyouqiang48adc142024-06-24 20:07:30 +08001729 int try_count = 0;
wangyouqiangb1deff32024-03-05 13:31:27 +08001730 total = (sizeof(test_gpio)/sizeof(int));
wangyouqiang48adc142024-06-24 20:07:30 +08001731 printf("T108 Start test gpio V1.0, total gpio=%d\n", (total/2));
wangyouqiangb1deff32024-03-05 13:31:27 +08001732
wangyouqiang48adc142024-06-24 20:07:30 +08001733 for(try_count; try_count < 4; try_count++){
1734 n = 0;
1735 /* [1 all gpio Init] */
1736 gpio_test_init_test_gpio_mode();
wangyouqiangb1deff32024-03-05 13:31:27 +08001737
wangyouqiang48adc142024-06-24 20:07:30 +08001738 /* [2 GPIOTEST] */
1739 for(i = 0; i < total; i = i + 2){
1740 ret = gpio_register_test_out(test_gpio[i], 0);
1741 if(-1 == ret){
yq.wang67c76d52024-06-24 06:11:36 -07001742 n++;
wangyouqiang48adc142024-06-24 20:07:30 +08001743 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio[i]);
1744 if(try_count != 3){
1745 printf(" ---TEST FAILED! RETRY!--- \n");
1746 usleep(5000);
1747 break;
1748 }
yq.wang67c76d52024-06-24 06:11:36 -07001749 fail_io[n - 1] = test_gpio[i+1];
wangyouqiang48adc142024-06-24 20:07:30 +08001750 }else{
1751 //printf("############gpio [%d] test success############\n", test_gpio[i]);
1752 }
1753 }
1754 if(0 == n){
1755 break;
1756 }
wangyouqiangb1deff32024-03-05 13:31:27 +08001757 }
1758 return n;
1759}
1760
1761
wangyouqiang48adc142024-06-24 20:07:30 +08001762#elif (MBTK_PROJECT_T108)
1763static int gpio_test(int port)
1764{
1765 int ret;
1766 int i;
1767 int valueh = 0;
1768 int valuel = 1;
1769
1770 //printf("Gpio port [%d] test start!\n", port);
1771 ret = gpio_register_set_func_0(port);
1772 if(ret){
1773 printf("gpio_port can't support!\n");
1774 return -1;
1775 }
1776 gpio_register_set_direction(port, 1);
1777 //ret = gpio_register_get_value(port);
1778 //printf("gpio default value is: %d\n", ret);
1779
1780
1781 //[High]
1782 for(i = 0; i <= 5; i++){
1783 gpio_register_set_value(port, 1);
1784 usleep(50);
1785 //valueh = gpio_register_get_value(port);
1786 //printf("set high? %d\n", valueh);
1787 if(1 == valueh){
1788 break;
1789 }
1790 }
1791
1792 for(i = 0; i <= 5; i++){
1793 ret = gpio_register_get_value(118);
1794 if(1 == ret){
1795
1796 break;
1797 }
1798 usleep(3000);
1799 }
1800 //printf("count=%d \n", i);
1801 //printf("******gpio should is high: %d.******\n", ret);
1802 if(1 != ret){
1803 ret=-1;
1804 goto exit;
1805 }
1806
1807 //[Low]
1808 usleep(200);
1809 for(i = 0; i <= 9; i++){
1810 gpio_register_set_value(port, 0);
1811 usleep(50);
1812 //valuel = gpio_register_get_value(port);
1813 //printf("set low? %d\n", valuel);
1814 if(0 == valuel){
1815 break;
1816 }
1817 }
1818
1819 for(i = 0; i <= 5; i++){
1820 ret = gpio_register_get_value(118);
1821 if(0 == ret){
1822
1823 break;
1824 }
1825 usleep(3000);
1826 }
1827 //printf("count=%d \n", i);
1828 //printf("******gpio should is low: %d.******\n", ret);
1829 if(0 != ret){
1830 ret=-1;
1831 goto exit;
1832 }
1833
1834exit:
1835 gpio_register_set_value(port, 0);
1836 gpio_register_set_direction(port, 0);
1837 return ret;
1838
1839}
1840
1841static int gpio_test_init_test_gpio_mode(void)
1842{
1843 const int test_gpio[] = {
1844 99,8,117,21,22,23,24,41,120,19,123,
1845 58,59,57,56,55,48,125,127,36,35,34,
1846 33,54,47,53,46,50,49,11,10,26,28,25,
1847 27,32,31,51,52,39,40,37,38,
1848 44,45
1849 };
1850
1851 int i, j, ret, total, n = 0;
1852 total = (sizeof(test_gpio)/sizeof(int));
1853 //printf("[init]gpio_test_init_test_gpio_mode BEGIN\n");
1854
1855 /* [1 all gpio set to low] */
1856 gpio_register_set_func_0(118);
1857 gpio_register_set_direction(118, 0);
1858
1859 for(j = 0; j < 5 ; j++){
1860 for(i = 0; i < total; i++){
1861 gpio_register_set_func_0(test_gpio[i]);
1862 gpio_register_set_direction(test_gpio[i], 1);
1863 gpio_register_set_value(test_gpio[i], 0);
1864 //ret = gpio_register_get_value(test_gpio[i]);
1865 //printf("[init]get gpio%d=%d\n", test_gpio[i], ret);
1866 }
1867
1868
1869 for(i = 0; i <= 10; i++){
1870 ret = gpio_register_get_value(118);
1871 //printf("[init]get gpi118=%d\n", ret);
1872 usleep(3000);
1873 if(0 == (ret)){
1874 break;
1875 }
1876 }
1877
1878 //printf("pre set 118: %d, times: %d\n",(ret), j);
1879 if(0 == (ret)){
1880 break;
1881 }
1882 }
1883 if(0 != (ret)){
1884 printf("!!!Set all low FAIL, please retest\n");
1885 return -1;
1886 }
1887 return 0;
1888}
1889
b.liu94091352024-07-02 10:38:08 +08001890int mbtk_at_gpio(bool gnss_support, void* arg)
wangyouqiang48adc142024-06-24 20:07:30 +08001891{
1892 const int test_gpio[] = {
1893 //GPIO PIN GPIO PIN GPIO PIN GPIO PIN
1894 99, 170, 8, 171, 117, 59, 21, 61,
1895 22, 62, 23, 144, 24, 147, 41, 159,
1896 120, 143, 19, 3, 123, 5, 58, 18,
1897 59, 19, 57, 20, 56, 21, 55, 22,
1898 48, 23, 125, 149, 127, 160, 36, 77,
1899 35, 78, 34, 79, 33, 80, 54, 163,
1900 #if 0 //Hard Ware ERROR!
1901 47, 164,
1902 46, 166,
1903 #endif
1904 53, 165, 50, 42, 49, 43, 11, 73,
1905 10, 74, 26, 65, 28, 66, 25, 67,
1906 27, 68, 32, 56, 31, 57, 51, 58,
1907 52, 60, 39, 192, 40, 193, 37, 194,
1908 38, 195,
1909 #if 0 //undefine GNSS
1910 44, 161,
1911 45, 151,
1912 #endif
1913 };
1914
1915 int i, n = 0, ret, total;
1916 int *fail_io = (int *)arg;
1917 int try_count;
1918 total = (sizeof(test_gpio)/sizeof(int));
1919 printf("T108 V2 Start test gpio V0.8, total gpio=%d\n", (total/2));
1920
1921 for(try_count = 0; try_count < 4; try_count++){
1922 n = 0;
1923 /* [1 all gpio Init] */
1924 gpio_test_init_test_gpio_mode();
1925
1926 /* [2 GPIOTEST] */
1927 for(i = 0; i < total; i = i + 2){
1928 ret = gpio_test(test_gpio[i]);
1929 if(-1 == ret){
yq.wang67c76d52024-06-24 06:11:36 -07001930 n++;
wangyouqiang48adc142024-06-24 20:07:30 +08001931 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio[i]);
1932 if(try_count != 3){
1933 printf(" ---TEST FAILED! RETRY!--- \n");
1934 usleep(5000);
1935 break;
1936 }
yq.wang67c76d52024-06-24 06:11:36 -07001937 fail_io[n - 1] = test_gpio[i + 1];
wangyouqiang48adc142024-06-24 20:07:30 +08001938 }else{
1939 //printf("############gpio [%d] test success############\n", test_gpio[i]);
1940 }
1941 }
1942 if(0 == n){
1943 printf(" ---ALL PASS---\n");
1944 break;
1945 }
1946 }
1947 return n;
1948}
1949
wangyouqiangece1c722023-12-12 17:28:53 +08001950
b.liu4f8e8792023-11-01 23:25:22 +08001951#else
b.liu94091352024-07-02 10:38:08 +08001952int mbtk_at_gpio(bool gnss_support, void* arg)
b.liu4f8e8792023-11-01 23:25:22 +08001953{
1954 return -1;
1955}
1956
1957#endif