blob: 45f2d9db27b24122a1d6eceaaca88c9369f54e58 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001/**
2 * \file gpio-test.c
3 * \brief A Documented file.
4 *
5 * Detailed description
6 * \Author: Sniper <js.wang@mobiletek.cn>
7 * \Version: 1.0.0
8 * \Date: 2022-04-26
9 */
liubin281ac462023-07-19 14:22:54 +080010#include <stdio.h>
11#include <string.h>
12#include <sys/types.h>
13#include <sys/stat.h>
14#include <fcntl.h>
15#include <errno.h>
16#include <limits.h>
17#include <sys/wait.h>
18#include <unistd.h>
19#include <sys/mount.h>
20#include "mbtk_log.h"
21#include <sys/mman.h>
22#include <ctype.h>
23#include <cutils/properties.h>
b.liu85565a72023-11-09 16:47:40 +080024#include "mbtk_gpio_def.h"
liubin281ac462023-07-19 14:22:54 +080025#include "gpio-define.h"
26
27#define gpio_log(...) if(gpio_debug)printf(__VA_ARGS__)
wangyouqiang7850b4a2023-09-18 15:47:57 +080028#define HWMAP_DEVICE "/dev/hwmap"
29#define PAGE_OFFS_BITS(pgsz) ((unsigned int)(pgsz)-1)
30#define PAGE_MASK_BITS(pgsz) (~PAGE_OFFS_BITS(pgsz))
b.liu85565a72023-11-09 16:47:40 +080031#define STR_MAX_LEN 220
wangyouqiang7850b4a2023-09-18 15:47:57 +080032typedef enum {
33 MBTK_ADC0 = 0, /* ADC 0 */
34 MBTK_ADC1 /* ADC 1 */
35} mbtk_adc_enum;
liubin281ac462023-07-19 14:22:54 +080036
37static int gpio_debug = 0;
38
gy.tangf4d7b852024-06-27 18:50:14 +080039/**/
40#define GPIO_MAX_CURRENT (7<<10) //设置GPIO最大输出电流
41#define GPIO_STRONG_PULLUP (1<<3) //设置GPIO强上拉
42#define GPIO_PULLUP_ENABLE ((1<<15)|(1<<14)) //GPIO上拉
43#define GPIO_PULLDOWN_ENABLE ((1<<15)|1<<13) //GPIO下拉
44
45#define MBTK_GPIO_OUTPUT 1
46#define MBTK_GPIO_INPUT 0
47
wangyouqiangb1deff32024-03-05 13:31:27 +080048#if (defined(MBTK_PROJECT_L508_X6) || defined(MBTK_PROJECT_T108))
wangyouqiang7775cbf2023-12-29 16:35:24 +080049struct gpio_register_function gpio_func_register[128] = {
50 {GPIO_FUNC_GPIO_00, 0},
51 {GPIO_FUNC_GPIO_01, 0},
52 {GPIO_FUNC_GPIO_02, 0},
53 {GPIO_FUNC_GPIO_03, 0},
54 {GPIO_FUNC_GPIO_04, 0},
55 {GPIO_FUNC_GPIO_05, 0},
56 {GPIO_FUNC_GPIO_06, 0},
57 {GPIO_FUNC_GPIO_07, 0},
58 {GPIO_FUNC_GPIO_08, 0},
59 {GPIO_FUNC_GPIO_09, 0},
60 {GPIO_FUNC_GPIO_10, 0},
61 {GPIO_FUNC_GPIO_11, 0},
62 {GPIO_FUNC_GPIO_12, 0},
63 {GPIO_FUNC_GPIO_13, 0},
64 {GPIO_FUNC_GPIO_14, 0},
65 {GPIO_FUNC_GPIO_15, 0},
66 {GPIO_FUNC_GPIO_16, 0},
67 {GPIO_FUNC_GPIO_17, 0},
68 {GPIO_FUNC_GPIO_18, 0},
69 {GPIO_FUNC_GPIO_19, 0},
70 {GPIO_FUNC_GPIO_20, 0},
71 {GPIO_FUNC_GPIO_21, 0},
72 {GPIO_FUNC_GPIO_22, 0},
73 {GPIO_FUNC_GPIO_23, 0},
74 {GPIO_FUNC_GPIO_24, 0},
75 {GPIO_FUNC_GPIO_25, 0},
76 {GPIO_FUNC_GPIO_26, 0},
77 {GPIO_FUNC_GPIO_27, 0},
78 {GPIO_FUNC_GPIO_28, 0},
79 {GPIO_FUNC_GPIO_29, 0},
80 {GPIO_FUNC_GPIO_30, 0},
81 {GPIO_FUNC_GPIO_31, 0},
82 {GPIO_FUNC_GPIO_32, 0},
83 {GPIO_FUNC_GPIO_33, 0},
84 {GPIO_FUNC_GPIO_34, 0},
85 {GPIO_FUNC_GPIO_35, 0},
wangyouqiangb1deff32024-03-05 13:31:27 +080086 {GPIO_FUNC_GPIO_36, 0},
87 {GPIO_FUNC_GPIO_37, 0}, // GPIO_37
88 {GPIO_FUNC_GPIO_38, 0}, // GPIO_38
89 {GPIO_FUNC_GPIO_39, 0}, // GPIO_39
90 {GPIO_FUNC_GPIO_40, 0}, // GPIO_40
91 {GPIO_FUNC_GPIO_41, 0}, //GPIO_41
92 {GPIO_FUNC_GPIO_42, 0}, //GPIO_42
wangyouqiang7775cbf2023-12-29 16:35:24 +080093 {GPIO_FUNC_GPIO_43, 0}, //GPIO_43
94 {GPIO_FUNC_GPIO_44,0}, //GPIO_44
95 {GPIO_FUNC_GPIO_45,0}, //GPIO_45
96 {GPIO_FUNC_GPIO_46,0}, //GPIO_46
97 {GPIO_FUNC_GPIO_47,0}, //GPIO_47
98 {GPIO_FUNC_SDIO_DAT3, 1}, //GPIO_48
99 {GPIO_FUNC_GPIO_49, 0},
100 {GPIO_FUNC_GPIO_50, 0},
101 {GPIO_FUNC_GPIO_51, 0},
102 {GPIO_FUNC_GPIO_52, 0},
103 {GPIO_FUNC_GPIO_53, 0},
104 {GPIO_FUNC_GPIO_54, 0},
105 {GPIO_FUNC_SDIO_DAT2, 1}, //GPIO_55
106 {GPIO_FUNC_SDIO_DAT1, 1}, //GPIO_56
107 {GPIO_FUNC_SDIO_DAT0, 1}, //GPIO_57
108 {GPIO_FUNC_SDIO_CMD, 1}, //GPIO_58
109 {GPIO_FUNC_SDIO_CLK, 1}, //GPIO_59
110 {GPIO_FUNC_GPIO_60, 0},
111 {0,0},{0,0},{0,0},{0,0},
112 {0,0},{0,0},
113 {GPIO_FUNC_DVL_0, 1},//GPIO_67
114 {GPIO_FUNC_DVL_1, 1},//GPIO_68
115 {GPIO_FUNC_GPIO_69, 0},
116 {GPIO_FUNC_GPIO_70, 0},
117 {GPIO_FUNC_QSPI_DAT3, 1}, //GPIO_71
118 {GPIO_FUNC_QSPI_DAT2, 1}, //GPIO_72
119 {GPIO_FUNC_QSPI_DAT1, 1}, //GPIO_73
120 {GPIO_FUNC_QSPI_DAT0, 1}, //GPIO_74
121 {GPIO_FUNC_QSPI_CLK, 1}, //GPIO_75
122 {GPIO_FUNC_QSPI_CS1, 1}, //GPIO_76
123 {GPIO_FUNC_GPIO_77, 0},
124 {GPIO_FUNC_GPIO_78, 0},
125 {GPIO_FUNC_GPIO_79, 0},
126 {GPIO_FUNC_GPIO_80, 0},
127 {GPIO_FUNC_USIM_UCLK, 1},//GPIO_81
128 {GPIO_FUNC_USIM_UIO, 1},//GPIO_82
129 {GPIO_FUNC_USIM_URSTn, 1},//GPIO_83
130
131 {GPIO_FUNC_MMC1_DAT3,5}, //GPIO_84
132 {GPIO_FUNC_MMC1_DAT2,5}, //GPIO_85
133 {GPIO_FUNC_MMC1_DAT1,5}, //GPIO_86
134 {GPIO_FUNC_MMC1_DAT0,5}, //GPIO_87
135 {GPIO_FUNC_MMC1_CMD,5}, //GPIO_88
136 {GPIO_FUNC_MMC1_CLK,5}, //GPIO_89
137 {GPIO_FUNC_MMC1_CD,1}, //GPIO_90
138 {0,0},
139 {0,0},{0,0},{0,0},{0,0},
140 {0,0},{0,0},{0,0},
141 {GPIO_FUNC_USB_ID, 1},//GPIO_99
142 {0,0},{0,0},{0,0},{0,0},
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},
147 {GPIO_FUNC_PRI_TDI, 1}, //GPIO_117
148 {GPIO_FUNC_PRI_TMS, 1}, //GPIO_118
149 {GPIO_FUNC_PRI_TCK, 1}, //GPIO_119
150 {GPIO_FUNC_PRI_TDO, 1}, //GPIO_120
151 {GPIO_FUNC_QSPI_VMODE_GPIO, 1}, //GPIO_121
152 {GPIO_FUNC_VBUS_DRV, 1}, //GPIO_122
153 {GPIO_FUNC_CLK_REQ, 1}, //GPIO_123
154 {0,0},
155 {GPIO_FUNC_VCXO_REQ, 1}, //GPIO_125
156 {0,0}, //GPIO_126
157 {GPIO_FUNC_VCXO_OUT, 1}, //GPIO_127
158 {0,0},
159};
160
161#else
liubin281ac462023-07-19 14:22:54 +0800162struct gpio_register_function gpio_func_register[128] = {
163 {GPIO_FUNC_GPIO_00, 0},
164 {GPIO_FUNC_GPIO_01, 0},
165 {GPIO_FUNC_GPIO_02, 0},
166 {GPIO_FUNC_GPIO_03, 0},
167 {GPIO_FUNC_GPIO_04, 0},
168 {GPIO_FUNC_GPIO_05, 0},
169 {GPIO_FUNC_GPIO_06, 0},
170 {GPIO_FUNC_GPIO_07, 0},
171 {GPIO_FUNC_GPIO_08, 0},
172 {GPIO_FUNC_GPIO_09, 0},
173 {GPIO_FUNC_GPIO_10, 0},
174 {GPIO_FUNC_GPIO_11, 0},
175 {GPIO_FUNC_GPIO_12, 0},
176 {GPIO_FUNC_GPIO_13, 0},
177 {GPIO_FUNC_GPIO_14, 0},
178 {GPIO_FUNC_GPIO_15, 0},
179 {GPIO_FUNC_GPIO_16, 0},
180 {GPIO_FUNC_GPIO_17, 0},
181 {GPIO_FUNC_GPIO_18, 0},
182 {GPIO_FUNC_GPIO_19, 0},
183 {GPIO_FUNC_GPIO_20, 0},
184 {GPIO_FUNC_GPIO_21, 0},
185 {GPIO_FUNC_GPIO_22, 0},
186 {GPIO_FUNC_GPIO_23, 0},
187 {GPIO_FUNC_GPIO_24, 0},
188 {GPIO_FUNC_GPIO_25, 0},
189 {GPIO_FUNC_GPIO_26, 0},
190 {GPIO_FUNC_GPIO_27, 0},
191 {GPIO_FUNC_GPIO_28, 0},
192 {GPIO_FUNC_GPIO_29, 0},
193 {GPIO_FUNC_GPIO_30, 0},
194 {GPIO_FUNC_GPIO_31, 0},
195 {GPIO_FUNC_GPIO_32, 0},
196 {GPIO_FUNC_GPIO_33, 0},
197 {GPIO_FUNC_GPIO_34, 0},
198 {GPIO_FUNC_GPIO_35, 0},
199 {GPIO_FUNC_GPIO_36, 0},
200 {GPIO_FUNC_MMC1_DAT3, 5}, // GPIO_37
201 {GPIO_FUNC_MMC1_DAT2, 5}, // GPIO_38
202 {GPIO_FUNC_MMC1_DAT1, 5}, // GPIO_39
203 {GPIO_FUNC_MMC1_DAT0, 5}, // GPIO_40
204 {GPIO_FUNC_MMC1_CMD, 5}, //GPIO_41
205 {GPIO_FUNC_MMC1_CLK, 5}, //GPIO_42
206 {GPIO_FUNC_MMC1_CD , 1}, //GPIO_43
207 {0,0},{0,0},{0,0},{0,0},
208 {GPIO_FUNC_SDIO_DAT3, 1}, //GPIO_48
209 {GPIO_FUNC_GPIO_49, 0},
210 {GPIO_FUNC_GPIO_50, 0},
211 {GPIO_FUNC_GPIO_51, 0},
212 {GPIO_FUNC_GPIO_52, 0},
213 {GPIO_FUNC_GPIO_53, 0},
214 {GPIO_FUNC_GPIO_54, 0},
215 {GPIO_FUNC_SDIO_DAT2, 1}, //GPIO_55
216 {GPIO_FUNC_SDIO_DAT1, 1}, //GPIO_56
217 {GPIO_FUNC_SDIO_DAT0, 1}, //GPIO_57
218 {GPIO_FUNC_SDIO_CMD, 1}, //GPIO_58
219 {GPIO_FUNC_SDIO_CLK, 1}, //GPIO_59
220 {GPIO_FUNC_GPIO_60, 0},
221 {0,0},{0,0},{0,0},{0,0},
222 {0,0},{0,0},
223 {GPIO_FUNC_DVL_0, 1},//GPIO_67
224 {GPIO_FUNC_DVL_1, 1},//GPIO_68
225 {GPIO_FUNC_GPIO_69, 0},
226 {GPIO_FUNC_GPIO_70, 0},
227 {GPIO_FUNC_QSPI_DAT3, 1}, //GPIO_71
228 {GPIO_FUNC_QSPI_DAT2, 1}, //GPIO_72
229 {GPIO_FUNC_QSPI_DAT1, 1}, //GPIO_73
230 {GPIO_FUNC_QSPI_DAT0, 1}, //GPIO_74
231 {GPIO_FUNC_QSPI_CLK, 1}, //GPIO_75
232 {GPIO_FUNC_QSPI_CS1, 1}, //GPIO_76
233 {GPIO_FUNC_GPIO_77, 0},
234 {GPIO_FUNC_GPIO_78, 0},
235 {GPIO_FUNC_GPIO_79, 0},
236 {GPIO_FUNC_GPIO_80, 0},
237 {GPIO_FUNC_USIM_UCLK, 1},//GPIO_81
238 {GPIO_FUNC_USIM_UIO, 1},//GPIO_82
239 {GPIO_FUNC_USIM_URSTn, 1},//GPIO_83
240 {0,0},{0,0},{0,0},{0,0},
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},
244 {GPIO_FUNC_USB_ID, 1},//GPIO_99
245 {0,0},{0,0},{0,0},{0,0},
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},
250 {GPIO_FUNC_PRI_TDI, 1}, //GPIO_117
251 {GPIO_FUNC_PRI_TMS, 1}, //GPIO_118
252 {GPIO_FUNC_PRI_TCK, 1}, //GPIO_119
253 {GPIO_FUNC_PRI_TDO, 1}, //GPIO_120
254 {GPIO_FUNC_QSPI_VMODE_GPIO, 1}, //GPIO_121
255 {GPIO_FUNC_VBUS_DRV, 1}, //GPIO_122
256 {GPIO_FUNC_CLK_REQ, 1}, //GPIO_123
257 {0,0},
258 {GPIO_FUNC_VCXO_REQ, 1}, //GPIO_125
259 {GPIO_FUNC_VCXO_OUT, 1}, //GPIO_126
260 {0,0},
261};
liubin281ac462023-07-19 14:22:54 +0800262
wangyouqiang7775cbf2023-12-29 16:35:24 +0800263#endif
liubin281ac462023-07-19 14:22:54 +0800264
wangyouqiangece1c722023-12-12 17:28:53 +0800265static int mbtk_gpio_adc(channel)
liubin281ac462023-07-19 14:22:54 +0800266{
liubin281ac462023-07-19 14:22:54 +0800267 int ret = 0;
268
wangyouqiangece1c722023-12-12 17:28:53 +0800269 ret = mbtk_adc_get(channel);
270 //printf("ADC_%d_value =%d\n", channel, ret);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800271 if(ret >= 500 ){
272 ret = 1;
liubin281ac462023-07-19 14:22:54 +0800273 }
wangyouqiang7850b4a2023-09-18 15:47:57 +0800274 else if (ret>=0 && ret<=480){
275 ret = 0;
276 }
277 else{
278 ret = -1;
liubin281ac462023-07-19 14:22:54 +0800279 }
280 return ret;
281}
liubin281ac462023-07-19 14:22:54 +0800282
liubin281ac462023-07-19 14:22:54 +0800283
284static int hwacc_register(int rw, unsigned int addr, unsigned int *data)
285{
286 int fid;
287 unsigned int pagesize, len, len_aligned;
288 unsigned int addr_aligned;
289 volatile unsigned int *pa;
290 void *vpa;
291
292 len = pagesize = sysconf(_SC_PAGESIZE);
293 if((fid = open(HWMAP_DEVICE, O_RDWR)) < 0)
294 {
295 printf("Failed to open %s\n", HWMAP_DEVICE);
296 exit(-1);
297 }
298
299 // Align the length so the mapped area is page-aligned and contains the requested area
300 addr_aligned = addr & PAGE_MASK_BITS(pagesize);
301 len_aligned =((addr + len - addr_aligned) + pagesize - 1) & PAGE_MASK_BITS(pagesize);
302
303 /* Notes on flags: MAP_PRIVATE results in copy on write; MAP_SHARED allows normal write */
304 /* MAP_SHARED required O_RDWR in open above, otherwise mmap fails with errno=EACCES */
305 /* Notes on prot: PROT_WRITE allows read and write; PROT_READ allows read only */
306 /* Notes on off: an unsigned 32-bit value, should be aligned to page size according to mmap manpage */
307 if((vpa = mmap(0, len_aligned, PROT_READ|PROT_WRITE, MAP_SHARED, fid, addr_aligned)) == MAP_FAILED)
308 {
309 printf("mmap failed (%d)\n", errno);
310 }
311 else
312 {
313 pa = (volatile unsigned int *)((unsigned char *)vpa + (addr & PAGE_OFFS_BITS(pagesize)));
314 if(rw == 0)
315 {
316 *data = *pa;
317 gpio_log("Value read from 0x%.8x via MVA=0x%p is 0x%.8x\n", addr, pa, *data);
318 }
319 else if(rw == 1)
320 {
321 *pa = *data;
322 gpio_log("Value %.8x written to 0x%.8x via MVA=0x%p\n", *data, addr, pa);
323#if defined(HWACC_DEBUG)
324 {
325 unsigned int val;
326 val = *pa;
327 printf("Value read from 0x%.8x via MVA=0x%p is 0x%.8x\n", addr, pa, val);
328 }
329#endif
330 }
331 munmap(vpa, len);
332 }
333
334 close(fid);
335 return 0;
336}
337/*
338设置GPIO 模式:第一步读GPIO22默认function
339root@OpenWrt:/# hwacc r 0xd401e134
340Option = r Addr = d401e134
341Value read from 0xd401e134 via MVA=0x0xb6fc3134 is 0x0000d040 //默认GPIO功能
342Bit0~bit2值对应上面表格各function,0代表GPIO功能
343 */
344static int gpio_register_read(int reg)
345{
346 int ret = -1;
347#if 0
348 FILE * fp;
349 // "/bin/hwacc r 0xd401e134";
350 char command[36] = {0};
351 char buffer[1024];
352 int i = 0;
353
354 sprintf(command, "/bin/hwacc r 0x%x", reg);
355 fp = popen(command, "r");
356 while(1)
357 {
358 if( fgets (buffer, sizeof(buffer), fp)!=NULL ) {
359 buffer[strlen(buffer) - 1] = 0;
360 // gpio_log("out [%d]: %s\n", strlen(buffer), buffer);
361 }else{
362 break;
363 }
364 i = strstr_n(buffer, "is");
365 if(i)
366 {
367 ret = str_to_hex(&buffer[i + 2]);
368 gpio_log("read 0x%x value:%s, %x\n", reg, &buffer[i + 2], ret);
369 }
370 }
371 pclose(fp);
372#else
b.liu85565a72023-11-09 16:47:40 +0800373#ifndef MBTK_PROJECT_PN1803
374 usleep(50);
375#endif
liubin281ac462023-07-19 14:22:54 +0800376 hwacc_register(0, reg, &ret);
b.liu85565a72023-11-09 16:47:40 +0800377#ifndef MBTK_PROJECT_PN1803
378 usleep(50);
379#endif
liubin281ac462023-07-19 14:22:54 +0800380#endif
381 return ret;
382}
383/*
384设置输入输出状态,设置PDR寄存器GPIO22为output
385root@OpenWrt:/# hwacc w 0xD401900c 0x00c03800
386Option = w Addr = d401900c Data=00c03800
387Value 00c03800 written to 0xd401900c via MVA=0x0xb6f9f00c
388 */
389static void gpio_register_write(int reg, int value)
390{
391#if 0
392 FILE * fp;
393 // "/bin/hwacc w 0xD401900c 0x00c03800"
394 char command[36] = {0};
395 char buffer[1024];
396
397 sprintf(command, "/bin/hwacc w 0x%x 0x%x", reg, value);
398 gpio_log("command: %s\n", command);
399 fp = popen(command, "r");
400 while(1)
401 {
402 if( fgets (buffer, sizeof(buffer), fp)!=NULL ) {
403 gpio_log("%s\n", buffer);
404 }else{
405 break;
406 }
407 }
408 pclose(fp);
409
410#else
b.liu85565a72023-11-09 16:47:40 +0800411#ifndef MBTK_PROJECT_PN1803
412 usleep(50);
413#endif
liubin281ac462023-07-19 14:22:54 +0800414 hwacc_register(1, reg, &value);
b.liu85565a72023-11-09 16:47:40 +0800415#ifndef MBTK_PROJECT_PN1803
416 usleep(50);
417#endif
liubin281ac462023-07-19 14:22:54 +0800418#endif
419}
420/*
421AF SEL<p>This field is used for alternate function selection for a pin.
422It selects between the eight possible alternate functions for the pin.
423Alternate function 0 is always the reset case.
424
425<p>0x0 = Alternate function 0 (primary function at reset)
426<p>0x1 = Alternate function 1
427<p>0x2 = Alternate function 2
428<p>0x3 = Alternate function 3
429<p>0x4 = Alternate function 4
430<p>0x5 = Alternate function 5
431<p>0x6 = Alternate function 6
432<p>0x7 = Alternate function 7
433 */
434static int gpio_register_set_func_0(int port)
435{
436 int ret;
437 struct gpio_register_function *reg = NULL;
438 if(port > 128)
439 return -1;
440 reg = &gpio_func_register[port];
441 if(0 == reg->reg)
442 return -1;
443 ret = gpio_register_read(reg->reg);
444 if((ret & 0x7) != reg->func_gpio)
445 {
wangyouqiang7850b4a2023-09-18 15:47:57 +0800446 //printf("Gpio set func [%d] [0x%x]!\n", reg->func_gpio, (ret & 0xfffffff8) | reg->func_gpio);
liubin281ac462023-07-19 14:22:54 +0800447 gpio_register_write(reg->reg, (ret & 0xfffffff8) | reg->func_gpio);
448 }
449 return 0;
450}
451/*
452设置GPIO 方向
453读取输入输出状态,读PDR寄存器:0x0c
454root@OpenWrt:/# hwacc r 0xD401900c
455Option = r Addr = d401900c
456Value read from 0xd401900c via MVA=0x0xb6f3900c is 0x00803800 //bit22为0,代表Input
457 */
458static void gpio_register_set_direction(int port, int dir)
459{
460 int ret;
461 int reg = 0xD4019000;
462
463 if(port > (32 - 1))
464 reg = 0xD4019004;
465 if(port > (32 * 2 - 1))
466 reg = 0xD4019008;
467 if(port > (32 * 3 - 1))
468 reg = 0xD4019100;
469
470 reg += 0x0c;
471 port = port % 0x20;
472 ret = gpio_register_read(reg);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800473 //printf("[Direction] reg_value=%x\n", ret);
liubin281ac462023-07-19 14:22:54 +0800474 // 设置 输出 0 && 1
475 if(!(ret & (0x1 << port)) && dir)
476 {
gy.tangf4d7b852024-06-27 18:50:14 +0800477 gpio_register_write(reg, ret | (0x1 << port)); //set the gpio bit
wangyouqiang7850b4a2023-09-18 15:47:57 +0800478 //printf("[Direction out] reg_value=%x\n", gpio_register_read(reg));
liubin281ac462023-07-19 14:22:54 +0800479 }
480 // 设置 输入 1 && 0
481 if((ret & (0x1 << port)) && !dir)
482 {
gy.tangf4d7b852024-06-27 18:50:14 +0800483#if 1
484 gpio_register_write(reg, ret & (~(0x1 << port))); //clear the gpio bit,mbtk_tanggaoyou
485#else
486 gpio_register_write(reg, ret | !(0x1 << port)); //这是错误配置,实际没有任何作用,还是原来的值
487#endif
wangyouqiang7850b4a2023-09-18 15:47:57 +0800488 //printf("[Direction in] reg_value=%x\n", gpio_register_read(reg));
liubin281ac462023-07-19 14:22:54 +0800489 }
490}
491/*
492设置GPIO 输出电平
493
494读取电平状态,先读PLR寄存器:0x00
495root@OpenWrt:/# hwacc r 0xD4019000
496Option = r Addr = d4019000
497Value read from 0xd4019000 via MVA=0x0xb6f1c000 is 0x81e82a30
498对应下面具体BIT
4991000 0001 1110 1000 0010 1010 0011 0000 BIT22默认电平高
500
501设置输出高:设置PSR寄存器:0x18(只写寄存器)
502root@OpenWrt:/# hwacc w 0xD4019018 0x400000
503Option = w Addr = d4019018 Data=00400000
504Value 00400000 written to 0xd4019018 via MVA=0x0xb6f56018 //bit22写1,输出高
505
506设置输出低:设置PCR寄存器:0x24
507root@OpenWrt:/# hwacc w 0xD4019024 0x400000
508Option = w Addr = d4019024 Data=00400000
509Value 00400000 written to 0xd4019024 via MVA=0x0xb6faa024 //Bit22写1,GPIO22输出低
510
511 */
512static void gpio_register_set_value(int port, int value)
513{
514 int ret;
515 int reg = 0xD4019000;
516
517 if(port > (32 - 1))
518 reg = 0xD4019004;
519 if(port > (32 * 2 - 1))
520 reg = 0xD4019008;
521 if(port > (32 * 3 - 1))
522 reg = 0xD4019100;
523
524 if(value)
525 {
526 reg += 0x18;
527 }
528 else
529 reg += 0x24;
530
531 port = port % 0x20;
532 ret = gpio_register_read(reg);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800533 //printf("[Value] reg_value=%x\n", gpio_register_read(0xD4019004));
liubin281ac462023-07-19 14:22:54 +0800534 // 设置 高电平 0 && 1
535 if(value)
536 {
537 gpio_register_write(reg, ret | (0x1 << port));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800538 //printf("[Value high] reg_value=%x\n", gpio_register_read(0xD4019004));
liubin281ac462023-07-19 14:22:54 +0800539 return;
540 }
541 // 设置 低电平 1 && 0
542 if(!(ret & (0x1 << port)) && !value)
543 {
544 gpio_register_write(reg, ret | (0x1 << port));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800545 //printf("[Value low] reg_value=%x\n", gpio_register_read(0xD4019004));
liubin281ac462023-07-19 14:22:54 +0800546 }
547}
548/*
549读取电平状态,先读PLR寄存器:0x00
550root@OpenWrt:/# hwacc r 0xD4019000
551Option = r Addr = d4019000
552Value read from 0xd4019000 via MVA=0x0xb6f1c000 is 0x81e82a30
553对应下面具体BIT
5541000 0001 1110 1000 0010 1010 0011 0000 BIT22默认电平高
555 */
556static int gpio_register_get_value(int port)
557{
558 int ret = -1;
559 int reg = 0xD4019000;
560
561 if(port > (32 - 1))
562 reg = 0xD4019004;
563 if(port > (32 * 2 - 1))
564 reg = 0xD4019008;
565 if(port > (32 * 3 - 1))
566 reg = 0xD4019100;
567 port = port % 0x20;
568 ret = gpio_register_read(reg);
569 if(ret & (0x1 << port))
570 {
571 return 1;
572 }
573 return 0;
574}
575
576void gpio_debug_set(int enable)
577{
578 gpio_debug = enable;
579}
580
b.liu85565a72023-11-09 16:47:40 +0800581
582
583#if defined(MBTK_PROJECT_PN1803)
liubin281ac462023-07-19 14:22:54 +0800584int gpio_register_test_out(int port, int value)
585{
586 int ret;
wangyouqiang7850b4a2023-09-18 15:47:57 +0800587 int i;
588 int valueh = 0;
589 int valuel = 1;
590
wangyouqiang4c262fa2023-10-17 17:48:03 +0800591 //printf("Gpio port [%d] test start!\n", port);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800592 ret = gpio_register_set_func_0(port); //设功能为GPIO
593 if(ret){
liubin281ac462023-07-19 14:22:54 +0800594 printf("gpio_port can't support!\n");
595 return -1;
596 }
wangyouqiang7850b4a2023-09-18 15:47:57 +0800597 gpio_register_set_direction(port, 1); //设方向为输出
liubin281ac462023-07-19 14:22:54 +0800598 ret = gpio_register_get_value(port);
wangyouqiang4c262fa2023-10-17 17:48:03 +0800599 //printf("gpio default value is : %d.\n", ret);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800600
601
602 //[High]
603 for(i = 0; i <= 10; i++){
604 gpio_register_set_value(port, 1);
605 usleep(50);
606 valueh = gpio_register_get_value(port);
wangyouqiang4c262fa2023-10-17 17:48:03 +0800607 //printf("set high? %d\n",valueh);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800608 if(1 == valueh){
609 break;
610 }
611 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800612 //usleep(10000);
613 for(i = 0; i <= 35; i++){
wangyouqiangece1c722023-12-12 17:28:53 +0800614 ret = mbtk_gpio_adc(0);
wangyouqiang59d8dd22023-10-19 18:03:54 +0800615 usleep(3000);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800616 if(1 == ret){
617 break;
618 }
619 }
wangyouqiang7850b4a2023-09-18 15:47:57 +0800620 if(1 != ret){
621 ret=-1;
liubin281ac462023-07-19 14:22:54 +0800622 goto exit;
wangyouqiang7850b4a2023-09-18 15:47:57 +0800623 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800624 printf("******gpio should is high: %d.******\n", ret);
liubin281ac462023-07-19 14:22:54 +0800625
wangyouqiang7850b4a2023-09-18 15:47:57 +0800626
627 //[Low]
628 usleep(200);
629 for(i = 0; i <= 10; i++){
630 gpio_register_set_value(port, 0); //输出低
b.liu074771a2023-10-19 13:13:39 +0800631 usleep(50);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800632 valuel = gpio_register_get_value(port);
wangyouqiang59d8dd22023-10-19 18:03:54 +0800633 //printf("set low? %d\n",valuel);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800634 if(0 == valuel){
635 break;
636 }
637 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800638 //usleep(10000);
639 for(i = 0; i <= 35; i++){
wangyouqiangece1c722023-12-12 17:28:53 +0800640 ret = mbtk_gpio_adc(0);
wangyouqiang59d8dd22023-10-19 18:03:54 +0800641 usleep(3000);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800642 if(0 == ret){
643 break;
644 }
645 }
b.liu074771a2023-10-19 13:13:39 +0800646 printf("******gpio should is low: %d.******\n", ret);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800647 if(0 != ret){
648 ret=-1;
liubin281ac462023-07-19 14:22:54 +0800649 goto exit;
650 }
651
652exit:
wangyouqiang7850b4a2023-09-18 15:47:57 +0800653 //gpio_register_set_direction(port, 0); //设方向为输入
liubin281ac462023-07-19 14:22:54 +0800654 return ret;
655}
656
b.liu94091352024-07-02 10:38:08 +0800657int mbtk_at_gpio(bool gnss_support, void* arg)
liubin281ac462023-07-19 14:22:54 +0800658{
b.liu37e72162023-11-01 15:38:18 +0800659 int test_gpio[] = {
660 56, 55, 58, 57, 48, 59, 12, 20, 5, 43, 21,
661 49, 50, 27, 28, 26, 25, 16, 17, 15, 126, 125,
b.liu810b1a32023-11-01 22:55:07 +0800662 4, 0, 3, 18, 7, 6, 1, 2, /*54,*/ 19, 13,
663 32, 31, 23, 24, 22, /*122,*/ 33, 35, 36, 34, 14,
wangyouqiangddbcf2e2023-11-14 13:59:41 +0800664 99,/* 53,*/ 37, 38, 39, 40, 41, 42};
liubin281ac462023-07-19 14:22:54 +0800665
wangyouqiang7850b4a2023-09-18 15:47:57 +0800666 int i, j, ret, total, n = 0;
liubin281ac462023-07-19 14:22:54 +0800667 int *fail_io = (int *)arg;
668 total = (sizeof(test_gpio)/sizeof(int));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800669 //printf("Start test gpio total: %d\n", total);
670
671 /* [1 all gpio set to low] */
672 for(j = 0; j < 6 ; j++){
673 for(i = 0; i < total; i++){
674 gpio_register_set_func_0(test_gpio[i]);
675 gpio_register_set_direction(test_gpio[i], 1);
676 gpio_register_set_value(test_gpio[i], 0);
677 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800678 //usleep(10000);
679 for(i = 0; i <= 35; i++){
wangyouqiangece1c722023-12-12 17:28:53 +0800680 ret = mbtk_gpio_adc(0);
wangyouqiang59d8dd22023-10-19 18:03:54 +0800681 usleep(3000);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800682 if(0 == ret){
683 break;
684 }
685 }
b.liu074771a2023-10-19 13:13:39 +0800686 printf("pre set ADC: %d, times: %d\n",ret, j);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800687 if(0 == ret){
688 break;
689 }
690 }
wangyouqiang4c262fa2023-10-17 17:48:03 +0800691 if(0 != ret){
692 printf("Fail, please retest");
693 return -1;
694 }
695
wangyouqiang7850b4a2023-09-18 15:47:57 +0800696
697 /* [2 GPIOTEST] */
698 for(i = 0; i < total; i++){
liubin281ac462023-07-19 14:22:54 +0800699 ret = gpio_register_test_out(test_gpio[i], 0);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800700 if(-1 == ret){
701 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio[i]);
liubin281ac462023-07-19 14:22:54 +0800702 fail_io[n] = test_gpio[i];
703 n++;
704 }else{
wangyouqiang4c262fa2023-10-17 17:48:03 +0800705 //printf("############gpio [%d] test success############\n", test_gpio[i]);
liubin281ac462023-07-19 14:22:54 +0800706 }
707 }
wangyouqiang2b352b22023-10-21 15:18:43 +0800708 mbtk_adc_close();
liubin281ac462023-07-19 14:22:54 +0800709 return n;
710}
b.liu4f8e8792023-11-01 23:25:22 +0800711
712#elif defined(MBTK_PROJECT_L508)
LUOJian943fd402024-02-07 10:54:25 +0800713
b.liu85565a72023-11-09 16:47:40 +0800714int test_gpio[] =
715{
716 0,2,1,6,16,15,17,99,
717 34,36,35,33,14,49,50,12,118,120,
wangyouqiang89888612023-11-28 18:28:12 +0800718 43,19,7,3,
b.liu85565a72023-11-09 16:47:40 +0800719 20,59,126,117,125,18,56,55,58,48,57,5,4,21,
LUOJian943fd402024-02-07 10:54:25 +0800720 23,24,13,27,28,26,25,
721 32,31,
gy.tangf4d7b852024-06-27 18:50:14 +0800722 54,
LUOJian943fd402024-02-07 10:54:25 +0800723 53,123,122
b.liu85565a72023-11-09 16:47:40 +0800724};
725int total;
726total = (sizeof(test_gpio)/sizeof(int));
727
728
729int gpio_test_init(void)
730{
731 int i,j,ret;
732 //int *fail_io = (int *)arg;
LUOJian943fd402024-02-07 10:54:25 +0800733 printf("[init] L508 XX GPIOTEST v1.2 \n");
wangyouqiang89888612023-11-28 18:28:12 +0800734 system("i2cset -y -f 2 0x31 0x12 0x46");
gy.tangf4d7b852024-06-27 18:50:14 +0800735 system("echo 53 > /sys/class/gpio/export"); //gnss uart rx
736 system("echo out > /sys/class/gpio/gpio53/direction");
737 system("echo 54 > /sys/class/gpio/export");//gnss uart tx
738 system("echo out > /sys/class/gpio/gpio54/direction");
739 system("echo 22 > /sys/class/gpio/export");//check pin
LUOJian943fd402024-02-07 10:54:25 +0800740
gy.tangf4d7b852024-06-27 18:50:14 +0800741 gpio_register_set_func_0(22);
742 gpio_register_set_direction(22, 1); //输出
743
744 usleep(500);
745 /*
746 GPIO22默认是上拉,如果改成GPIO输入模式,其PIN脚电压是高电平,必须改成下拉后GPIO22才处于低电平状态,否则测试PIN拉低失败
747 */
748 gpio_register_set_direction(22, MBTK_GPIO_INPUT); //设置输入模式
749 gpio_register_write(GPIO_FUNC_GPIO_22,0xb040); //PULL DOWN
750
751
b.liu85565a72023-11-09 16:47:40 +0800752 /* [1 all gpio set to low] */
753 for(j = 0; j < 6 ; j++){
754 for(i = 0; i < total; i++){
755 gpio_register_set_func_0(test_gpio[i]);
756 gpio_register_set_direction(test_gpio[i], 1);
757 gpio_register_set_value(test_gpio[i], 0);
758 }
759
760 for(i = 0; i <= 5; i++){
761 ret = gpio_register_get_value(22);
gy.tangf4d7b852024-06-27 18:50:14 +0800762
b.liu85565a72023-11-09 16:47:40 +0800763 if(0 == ret){
764 break;
765 }
gy.tangf4d7b852024-06-27 18:50:14 +0800766
b.liu85565a72023-11-09 16:47:40 +0800767 usleep(3000);
gy.tangf4d7b852024-06-27 18:50:14 +0800768
b.liu85565a72023-11-09 16:47:40 +0800769 }
gy.tangf4d7b852024-06-27 18:50:14 +0800770
b.liu85565a72023-11-09 16:47:40 +0800771 printf("pre set ADC: %d, times: %d\n",ret, j);
gy.tangf4d7b852024-06-27 18:50:14 +0800772
b.liu85565a72023-11-09 16:47:40 +0800773 if(0 == ret){
774 break;
775 }
776 }
777
778 if(0 != ret){
779 printf("set all low Fail, please retest\n");
780 return -1;
781 }
gy.tangf4d7b852024-06-27 18:50:14 +0800782
b.liu85565a72023-11-09 16:47:40 +0800783 printf("pre set SUCCESS\n");
gy.tangf4d7b852024-06-27 18:50:14 +0800784
b.liu85565a72023-11-09 16:47:40 +0800785 return 0;
786}
787
wangyouqiang89888612023-11-28 18:28:12 +0800788int gpio_test(void* arg)
b.liu85565a72023-11-09 16:47:40 +0800789{
790 int ret,i,j;
wangyouqiang89888612023-11-28 18:28:12 +0800791 int *fail_io = (int *)arg;
b.liu85565a72023-11-09 16:47:40 +0800792 int n = 0;
gy.tangf4d7b852024-06-27 18:50:14 +0800793 int gpio_num = 0;
b.liu85565a72023-11-09 16:47:40 +0800794 for(i = 0; i < total; i++){
gy.tangf4d7b852024-06-27 18:50:14 +0800795
796 gpio_num = test_gpio[i];
797
798 //ret = gpio_register_get_value(test_gpio[i]);
799 //printf("GPIO%d default value? %d\n", test_gpio[i], ret);
800
b.liu85565a72023-11-09 16:47:40 +0800801 //set high
gy.tangf4d7b852024-06-27 18:50:14 +0800802 gpio_register_set_value(gpio_num, 1);
803
804 ret = gpio_register_get_value(gpio_num);
805
806 printf("GPIO%d set high= %d\n", gpio_num, ret);
807
b.liu85565a72023-11-09 16:47:40 +0800808 //get
809 ret = gpio_register_get_value(22);
gy.tangf4d7b852024-06-27 18:50:14 +0800810
b.liu85565a72023-11-09 16:47:40 +0800811 if(1 != ret){
gy.tangf4d7b852024-06-27 18:50:14 +0800812 printf("gpio [%d] test high failed\n", gpio_num);
813
814 fail_io[n] = gpio_num;
815
816 n++;
817 }else{
818 printf("gpio[%d] test high success\n", gpio_num);
819 }
b.liu85565a72023-11-09 16:47:40 +0800820
821 //set low
gy.tangf4d7b852024-06-27 18:50:14 +0800822 gpio_register_set_value(gpio_num, 0);
823
824 //ret = gpio_register_get_value(test_gpio[i]);
825 //printf("GPIO%d set low? %d\n", test_gpio[i], ret);
826
b.liu85565a72023-11-09 16:47:40 +0800827 //get
828 ret = gpio_register_get_value(22);
gy.tangf4d7b852024-06-27 18:50:14 +0800829
b.liu85565a72023-11-09 16:47:40 +0800830 if(0 != ret){
gy.tangf4d7b852024-06-27 18:50:14 +0800831 printf("gpio [%d] test low failed\n", gpio_num);
832
833 fail_io[n] = test_gpio[i];
834
835 n++;
836 }else{
837 printf("gpio [%d] test low success\n", gpio_num);
838 }
b.liu85565a72023-11-09 16:47:40 +0800839 }
840 return n;
841}
842
843
844
b.liu4f8e8792023-11-01 23:25:22 +0800845
b.liu94091352024-07-02 10:38:08 +0800846int mbtk_at_gpio(bool gnss_support, void* arg)
b.liu4f8e8792023-11-01 23:25:22 +0800847{
b.liu85565a72023-11-09 16:47:40 +0800848 int i, j, ret, total, n = 0;
wangyouqiang89888612023-11-28 18:28:12 +0800849 //int *fail_io = (int *)arg;
b.liu85565a72023-11-09 16:47:40 +0800850
LUOJian943fd402024-02-07 10:54:25 +0800851 n = gpio_test_init();
gy.tangf4d7b852024-06-27 18:50:14 +0800852
853 if(-1 == n)
854 {
855 //return -1;
856 }
857
wangyouqiang89888612023-11-28 18:28:12 +0800858 n = gpio_test(arg);
b.liu85565a72023-11-09 16:47:40 +0800859
gy.tangf4d7b852024-06-27 18:50:14 +0800860 system("echo 53 > /sys/class/gpio/unexport"); //unexport gnss uart rx
861 system("echo 54 > /sys/class/gpio/unexport");//unexportgnss uart tx
862 system("echo 22 > /sys/class/gpio/unexport");//unexportcheck pin
863
864 printf("\n[GPIOTEST] Finished !!\n\n");
865
b.liu85565a72023-11-09 16:47:40 +0800866 return n;
b.liu4f8e8792023-11-01 23:25:22 +0800867}
868
b.liu85565a72023-11-09 16:47:40 +0800869
870
b.liu4f8e8792023-11-01 23:25:22 +0800871#elif defined(MBTK_PROJECT_L509)
gy.tang71ea7ff2024-06-21 16:01:56 +0800872#define DEBUG_GPIO_TEST 0
873mbtk_gpio_paired_info_struct test_pin_array[] ={ //是GPIO_array
874//output_pin, output_gpio,input_gpio,input_pin,test_result
b.liu85565a72023-11-09 16:47:40 +0800875//120 5 33 这几个输入脚没法正常设置为输入
gy.tang71ea7ff2024-06-21 16:01:56 +0800876 //{?,52,51,?,0}, //拉不低 没有这一组
877 //{?,54,53,?,0},
878 //Opin,Ogpio,Igpio,Ipin,res
879 {122, 16, 17, 121, 0}, //output_pin, output_gpio,input_gpio,input_pin,test_result
880 {1, 117, 120, 2, 0},
881 //{?,118,19,13,?,0}, //usim
882 {5, 123, 126, 6, 0},
883 {23, 43, 23, 62, 0},
884 {24, 28, 27, 25, 0},
885 {26, 26, 25, 27, 0},
886 {37, 34, 33, 40, 0},
887 {38, 36, 35, 39, 0},
888 {41, 49, 50, 42, 0},
889 {64, 32, 31, 65, 0},
890 {78, 7, 2, 73, 0},
891 {77, 6, 1, 76, 0},
892 {81, 15, 3, 75, 0},
893 {74, 0, 18, 120,0},
894 {79, 4, 5, 82, 0},
895 {80, 13, 14, 84, 0},
896 {129, 48, 55, 130,0},
897 {131, 56, 57, 132,0},
898 {133, 59, 58, 134,0},
899 {135, 21, 99, 139,0},
900 //{?,20,22,?,0}, //拉不低
901 //{?,12,122,?,0}, //拉不低 122好像不该用
b.liu85565a72023-11-09 16:47:40 +0800902};
903//测低
gy.tang71ea7ff2024-06-21 16:01:56 +0800904uint16 mbtk_gpio_test_all_low(mbtk_gpio_paired_info_struct *pin_array, uint16 MAX_pin_num)
b.liu85565a72023-11-09 16:47:40 +0800905{
906 uint16 index =0;
907 uint16 OUT_pin, Read_pin;
908 uint32 read_high_value;
909 uint32 read_low_value;
910 uint16 fail_num=0;
911 int res;
912
gy.tang71ea7ff2024-06-21 16:01:56 +0800913 printf("[GPIOTEST][run test all pin low]\n");
b.liu85565a72023-11-09 16:47:40 +0800914
915
916 for(index = 0; index < MAX_pin_num; index++){
gy.tang71ea7ff2024-06-21 16:01:56 +0800917 OUT_pin = pin_array[index].output_gpio;
918 Read_pin = pin_array[index].input_gpio;
b.liu85565a72023-11-09 16:47:40 +0800919
920 //输出低
921 gpio_register_set_direction(OUT_pin,1);
922 gpio_register_set_value(OUT_pin, 0);
923
924 //输入脚设为输入
925 gpio_register_set_direction(Read_pin,0);
926 }
927
928 for(index = 0; index < MAX_pin_num; index++){
gy.tang71ea7ff2024-06-21 16:01:56 +0800929 OUT_pin = pin_array[index].output_gpio;
930 Read_pin = pin_array[index].input_gpio;
b.liu85565a72023-11-09 16:47:40 +0800931 read_low_value = gpio_register_get_value(Read_pin);
932 //结果检测
933 if(read_low_value != 0){
gy.tang71ea7ff2024-06-21 16:01:56 +0800934 printf("[GPIOTEST][!Low Failed!]: GPIO%d PIN%d\n", Read_pin,pin_array[index].input_pin);
b.liu85565a72023-11-09 16:47:40 +0800935 pin_array[index].mbtk_gpio_test_result = 1;
936 fail_num ++;
937 }
938 else{
gy.tang71ea7ff2024-06-21 16:01:56 +0800939#if DEBUG_GPIO_TEST
940 printf("[GPIOTEST][-Low Success-]: GPIO%d \n", Read_pin);
941#endif
b.liu85565a72023-11-09 16:47:40 +0800942 }
943 }
944 return fail_num;
945
946}
947
948
949
950//测高
gy.tang71ea7ff2024-06-21 16:01:56 +0800951uint16 mbtk_gpio_test_pin_high(mbtk_gpio_paired_info_struct *pin_array,uint16 MAX_pin_num)
b.liu85565a72023-11-09 16:47:40 +0800952{
953 uint16 index =0;
954 uint16 OUT_pin, Read_pin;
955 uint32 read_high_value;
956 uint32 read_low_value;
957 uint16 fail_num=0;
958 int i = 0;
959
gy.tang71ea7ff2024-06-21 16:01:56 +0800960 printf("[GPIOTEST][run test pin high]\n");
b.liu85565a72023-11-09 16:47:40 +0800961
962 for(index = 0;index<MAX_pin_num;index++)
963 {
gy.tang71ea7ff2024-06-21 16:01:56 +0800964 OUT_pin = pin_array[index].output_gpio;
965 Read_pin = pin_array[index].input_gpio;
b.liu85565a72023-11-09 16:47:40 +0800966
967 //设高
968 gpio_register_set_direction(OUT_pin,1);
969 gpio_register_set_direction(Read_pin,0);
970 gpio_register_set_value(OUT_pin, 1);
971 usleep(20000);
972 read_high_value = gpio_register_get_value(Read_pin);
gy.tang71ea7ff2024-06-21 16:01:56 +0800973#if DEBUG_GPIO_TEST
974 printf("[GPIOTEST][test_high]: PIN%d: get value =%d \n", pin_array[index].input_pin, read_high_value);
975#endif
b.liu85565a72023-11-09 16:47:40 +0800976
977 //设高后重新设低
978 usleep(20000);
979 gpio_register_set_value(OUT_pin, 0);
980 usleep(80000);
981 //read_low_value = gpio_register_get_value(Read_pin);
982 //read_low_value = gpio_register_get_value(Read_pin);
983 for(i = 0;i < 10; i++){
984 read_low_value = gpio_register_get_value(Read_pin);
985 if(0 == read_low_value){
986 break;
987 }
988 usleep(20000);
989 }
gy.tang71ea7ff2024-06-21 16:01:56 +0800990#if DEBUG_GPIO_TEST
991 printf("[GPIOTEST][test_low]: PIN%d: get value =%d \n", pin_array[index].input_pin, read_low_value);
992#endif
b.liu85565a72023-11-09 16:47:40 +0800993 if(read_high_value != 1||read_low_value !=0)
994 {
gy.tang71ea7ff2024-06-21 16:01:56 +0800995 printf("[GPIOTEST][!High Failed!]: Gpio%d PIN%d\n",Read_pin,pin_array[index].input_pin);
b.liu85565a72023-11-09 16:47:40 +0800996 pin_array[index].mbtk_gpio_test_result = 2;
997 fail_num ++;
998 }
999 else{
gy.tang71ea7ff2024-06-21 16:01:56 +08001000#if DEBUG_GPIO_TEST
1001 printf("[GPIOTEST][-High Success-]: Gpio%d \n", Read_pin);
1002#endif
b.liu85565a72023-11-09 16:47:40 +08001003 }
1004 }
1005 return fail_num;
1006
1007}
gy.tang71ea7ff2024-06-21 16:01:56 +08001008void gpio_test_init_test_gpio_mode(mbtk_gpio_paired_info_struct pin_array[],int MAX_pin_num)
b.liu85565a72023-11-09 16:47:40 +08001009{
1010 int index =0;
1011 int OUT_pin, Read_pin;
1012
gy.tangf4d7b852024-06-27 18:50:14 +08001013 printf("[GPIOTEST] L509 GPIOTEST v2.1 --init\n");
gy.tang71ea7ff2024-06-21 16:01:56 +08001014
b.liu85565a72023-11-09 16:47:40 +08001015 for(index = 0;index<MAX_pin_num;index++)
1016 {
gy.tang71ea7ff2024-06-21 16:01:56 +08001017 OUT_pin = pin_array[index].output_gpio;
1018 Read_pin = pin_array[index].input_gpio;
gy.tang5656a592024-06-19 14:04:54 +08001019 pin_array[index].mbtk_gpio_test_result = 0; //init as passed
b.liu85565a72023-11-09 16:47:40 +08001020 gpio_register_set_func_0(OUT_pin);
1021 gpio_register_set_func_0(Read_pin);
1022 }
1023
gy.tang5656a592024-06-19 14:04:54 +08001024 //gpio_register_set_func_0();
1025 system("i2cset -y -f 2 0x31 0x12 0x46");
1026 system("echo 33 > /sys/class/gpio/export");
1027 system("echo 120 > /sys/class/gpio/export");
1028 system("echo 126 > /sys/class/gpio/export");
1029 system("echo in > /sys/class/gpio/gpio126/direction");
1030 system("echo in > /sys/class/gpio/gpio120/direction");
1031 system("echo in > /sys/class/gpio/gpio5/direction");
1032 system("echo in > /sys/class/gpio/gpio33/direction");
gy.tang71ea7ff2024-06-21 16:01:56 +08001033#if DEBUG_GPIO_TEST
1034 printf("[GPIOTEST]gpio_test_init_test_gpio_mode FINISH\n");
1035#endif
b.liu85565a72023-11-09 16:47:40 +08001036
1037}
1038
b.liu4f8e8792023-11-01 23:25:22 +08001039
b.liu94091352024-07-02 10:38:08 +08001040int mbtk_at_gpio(bool gnss_support, void* arg)
b.liu4f8e8792023-11-01 23:25:22 +08001041{
gy.tang71ea7ff2024-06-21 16:01:56 +08001042 int i, num;
1043 int *fail_io = (int *)arg;
1044 /*OPEN SWITCH */
1045 uint16 test_fail_count = 0;
1046 uint16 test_MAX_pin_num = 0;
1047 char buf[STR_MAX_LEN];
1048 uint16 str_len=0;
1049 uint16 temp_len =0;
1050 uint16 Out_pin;
1051 uint16 in_pin;
1052 uint16 fail_print_num = 0;
b.liu85565a72023-11-09 16:47:40 +08001053
gy.tang71ea7ff2024-06-21 16:01:56 +08001054 gpio_register_set_func_0(125);
1055 gpio_register_set_direction(125, 1);
1056 gpio_register_set_value(125, 1); //开关使能脚
1057
b.liu85565a72023-11-09 16:47:40 +08001058
1059 /* [3]初始化待测GPIO */
gy.tang71ea7ff2024-06-21 16:01:56 +08001060 test_MAX_pin_num = sizeof(test_pin_array)/sizeof(test_pin_array[0]);
1061
1062 gpio_test_init_test_gpio_mode(test_pin_array,test_MAX_pin_num);
b.liu85565a72023-11-09 16:47:40 +08001063
1064
1065
1066 /* [4]测试过程 */
gy.tang71ea7ff2024-06-21 16:01:56 +08001067 test_fail_count = mbtk_gpio_test_all_low(test_pin_array,test_MAX_pin_num);
b.liu85565a72023-11-09 16:47:40 +08001068
gy.tang71ea7ff2024-06-21 16:01:56 +08001069 if(!test_fail_count) //set all pin low passed
1070 {
1071 printf("[GPIOTEST]set all pin low: passed\n");
1072 test_fail_count = mbtk_gpio_test_pin_high(&test_pin_array,test_MAX_pin_num);
1073 }
1074 else
1075 {
1076 printf("[GPIOTEST]set all pin low: failed num=%d!!!\n",test_fail_count);
1077 }
b.liu85565a72023-11-09 16:47:40 +08001078
gy.tang71ea7ff2024-06-21 16:01:56 +08001079
1080 //memset(buf,0,STR_MAX_LEN);
1081
b.liu85565a72023-11-09 16:47:40 +08001082 /* [5]测试结果检测 */
gy.tang71ea7ff2024-06-21 16:01:56 +08001083 if(test_fail_count )
1084 {
1085 //printf( "GPIOTEST Fail %02d PINs:\n", test_fail_count*2);
1086 sprintf(buf, "GPIOTEST Fail %02d PINs:", test_fail_count*2);
1087 temp_len = strlen(buf);
b.liu85565a72023-11-09 16:47:40 +08001088
gy.tang71ea7ff2024-06-21 16:01:56 +08001089 num = 0;
1090 for(i = 0; i< test_MAX_pin_num; i++)
1091 {
b.liu85565a72023-11-09 16:47:40 +08001092
gy.tang71ea7ff2024-06-21 16:01:56 +08001093 if(test_pin_array[i].mbtk_gpio_test_result)
1094 {
b.liu85565a72023-11-09 16:47:40 +08001095
gy.tang71ea7ff2024-06-21 16:01:56 +08001096 Out_pin = test_pin_array[i].output_pin;
1097 in_pin = test_pin_array[i].input_pin;
1098 fail_io[num++] = Out_pin;
1099 fail_io[num++] = in_pin;
b.liu85565a72023-11-09 16:47:40 +08001100
gy.tang71ea7ff2024-06-21 16:01:56 +08001101 }
1102 }
1103 }
b.liu85565a72023-11-09 16:47:40 +08001104
gy.tang71ea7ff2024-06-21 16:01:56 +08001105 else
1106 {
b.liu85565a72023-11-09 16:47:40 +08001107 printf(buf,"ALL GPIO TEST PASS\r\n");
gy.tang71ea7ff2024-06-21 16:01:56 +08001108 }
gy.tangf4d7b852024-06-27 18:50:14 +08001109 system("echo 33 > /sys/class/gpio/unexport");
1110 system("echo 120 > /sys/class/gpio/unexport");
1111 system("echo 126 > /sys/class/gpio/unexport");
1112 printf("\n[GPIOTEST] Finished !!\n\n");
wangyouqiang97a792f2024-04-01 13:28:06 +08001113 return test_fail_count * 2;
b.liu4f8e8792023-11-01 23:25:22 +08001114}
1115
wangyouqiangece1c722023-12-12 17:28:53 +08001116#elif defined(MBTK_PROJECT_L508_X6)
wangyouqiangece1c722023-12-12 17:28:53 +08001117int gpio_register_test_out_0(int port, int value)
1118{
1119 int ret;
1120 int i;
1121 int valueh = 0;
1122 int valuel = 1;
1123
1124 printf("Gpio port [%d] test start!\n", port);
1125 ret = gpio_register_set_func_0(port); //设功能为GPIO
1126 if(ret){
1127 printf("gpio_port can't support!\n");
1128 return -1;
1129 }
1130 gpio_register_set_direction(port, 1); //设方向为输出
1131 ret = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001132 //printf("gpio default value is : %d.\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001133
1134
1135 //[High]
1136 for(i = 0; i <= 10; i++){
1137 gpio_register_set_value(port, 1);
1138 usleep(50);
1139 valueh = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001140 //printf("set high? %d\n",valueh);
wangyouqiangece1c722023-12-12 17:28:53 +08001141 if(1 == valueh){
1142 break;
1143 }
1144 }
1145 //usleep(10000);
1146 for(i = 0; i <= 35; i++){
1147 ret = mbtk_gpio_adc(0);
1148 usleep(3000);
1149 if(1 == ret){
1150 break;
1151 }
1152 }
b.liuc7ffd092024-01-03 15:23:07 +08001153 //printf("******gpio should is high: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001154 if(1 != ret){
1155 ret=-1;
1156 goto exit;
1157 }
1158
1159
1160
1161 //[Low]
1162 usleep(200);
1163 for(i = 0; i <= 10; i++){
1164 gpio_register_set_value(port, 0); //输出低
1165 usleep(50);
1166 valuel = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001167 //printf("set low? %d\n",valuel);
wangyouqiangece1c722023-12-12 17:28:53 +08001168 if(0 == valuel){
1169 break;
1170 }
1171 }
1172 //usleep(10000);
1173 for(i = 0; i <= 35; i++){
1174 ret = mbtk_gpio_adc(0);
1175 usleep(3000);
1176 if(0 == ret){
1177 break;
1178 }
1179 }
b.liuc7ffd092024-01-03 15:23:07 +08001180 //printf("******gpio should is low: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001181 if(0 != ret){
1182 ret=-1;
1183 goto exit;
1184 }
1185
1186exit:
1187 gpio_register_set_value(port, 0);
1188 gpio_register_set_direction(port, 0); //设方向为输入
1189 return ret;
1190}
1191
1192int gpio_register_test_out_1(int port, int value)
1193{
1194 int ret;
1195 int i;
1196 int valueh = 0;
1197 int valuel = 1;
1198
1199 printf("Gpio port [%d] test start!\n", port);
1200 ret = gpio_register_set_func_0(port); //设功能为GPIO
1201 if(ret){
1202 printf("gpio_port can't support!\n");
1203 return -1;
1204 }
1205 gpio_register_set_direction(port, 1); //设方向为输出
1206 ret = gpio_register_get_value(port);
1207 printf("gpio default value is : %d.\n", ret);
1208
1209
1210 //[High]
1211 for(i = 0; i <= 10; i++){
1212 gpio_register_set_value(port, 1);
1213 usleep(50);
1214 valueh = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001215 //printf("set high? %d\n",valueh);
wangyouqiangece1c722023-12-12 17:28:53 +08001216 if(1 == valueh){
1217 break;
1218 }
1219 }
1220 //usleep(10000);
1221 for(i = 0; i <= 35; i++){
1222 ret = mbtk_gpio_adc(1);
1223 usleep(3000);
1224 if(1 == ret){
1225 break;
1226 }
1227 }
1228 if(1 != ret){
1229 ret=-1;
1230 goto exit;
1231 }
b.liuc7ffd092024-01-03 15:23:07 +08001232 //printf("******gpio should is high: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001233
1234
1235 //[Low]
1236 usleep(200);
1237 for(i = 0; i <= 10; i++){
1238 gpio_register_set_value(port, 0); //输出低
1239 usleep(50);
1240 valuel = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001241 //printf("set low? %d\n",valuel);
wangyouqiangece1c722023-12-12 17:28:53 +08001242 if(0 == valuel){
1243 break;
1244 }
1245 }
1246 //usleep(10000);
1247 for(i = 0; i <= 35; i++){
1248 ret = mbtk_gpio_adc(1);
1249 usleep(3000);
1250 if(0 == ret){
1251 break;
1252 }
1253 }
b.liuc7ffd092024-01-03 15:23:07 +08001254 //printf("******gpio should is low: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001255 if(0 != ret){
1256 ret=-1;
1257 goto exit;
1258 }
1259
1260exit:
wangyouqiang7775cbf2023-12-29 16:35:24 +08001261 gpio_register_set_value(port, 0);
1262 gpio_register_set_direction(port, 0); //设方向为输入
wangyouqiangece1c722023-12-12 17:28:53 +08001263 return ret;
1264}
1265
wangyouqiang7775cbf2023-12-29 16:35:24 +08001266
1267int gpio_register_test_out_2(int port_in, int port_out, int value)
1268{
1269 int ret_in;
1270 int ret_out;
1271 int i;
1272 int valueh = 0;
1273 int valuel = 1;
1274
1275 printf("Gpio port [%d] test start!\n", port_out);
1276 ret_in = gpio_register_set_func_0(port_in); //设功能为GPIO
1277 ret_out = gpio_register_set_func_0(port_out);
1278 if((ret_in+ret_out)){
1279 printf("gpio_port can't support!\n");
1280 return -1;
1281 }
1282 gpio_register_set_direction(port_out, 1); //设方向为输出
1283 gpio_register_set_direction(port_in, 0); //设方向为输入
1284 ret_in = gpio_register_get_value(port_in);
1285 printf("gpio default value is : %d.\n", ret_in);
1286
1287
1288 //[High]
1289 for(i = 0; i <= 10; i++){
1290 gpio_register_set_value(port_out, 1);
1291 usleep(50);
1292 valueh = gpio_register_get_value(port_out);
1293 printf("set high? %d\n",valueh);
1294 if(1 == valueh){
1295 break;
1296 }
1297 }
1298
1299
1300 for(i = 0; i <= 35; i++){
1301 ret_in = gpio_register_get_value(port_in);
1302 usleep(3000);
1303 if(1 == ret_in){
1304 break;
1305 }
1306 }
1307 if(1 != ret_in){
1308 ret_in=-1;
1309 printf("get high failed! \n");
1310 goto exit;
1311 }
1312 printf("******gpio should is high: %d.******\n", ret_in);
1313
1314
1315 //[Low]
1316 usleep(200);
1317 for(i = 0; i <= 10; i++){
1318 gpio_register_set_value(port_out, 0); //输出低
1319 usleep(50);
1320 valuel = gpio_register_get_value(port_out);
1321 printf("set low? %d\n",valuel);
1322 if(0 == valuel){
1323 break;
1324 }
1325 }
1326
1327 for(i = 0; i <= 35; i++){
1328 ret_in = gpio_register_get_value(port_in);
1329 usleep(3000);
1330 if(0 == ret_in){
1331 break;
1332 }
1333 }
1334 printf("******gpio should is low: %d.******\n", ret_in);
1335 if(0 != ret_in){
1336 ret_in=-1;
1337 printf("get low failed! \n");
1338 goto exit;
1339 }
1340
1341exit:
1342 gpio_register_set_value(port_out, 0);
1343 gpio_register_set_direction(port_out, 0); //设方向为输入
1344 return ret_in;
1345}
1346
1347
wangyouqiangece1c722023-12-12 17:28:53 +08001348void gpio_test_init_test_gpio_mode(void )
1349{
1350
1351
1352 int test_gpio_1[] = {
1353 40, 33, 34, 39, 99,
1354 56, 58, 55, 57, 48, 59, 123, 122, 20, 53,
1355 19, 49, 50, 32 ,31, 6, 11, 10,
1356 88, 87, 86, 85, 84, 89,
wangyouqiang7775cbf2023-12-29 16:35:24 +08001357 90, 46, 127, 117, 119,
wangyouqiangece1c722023-12-12 17:28:53 +08001358 };
1359
1360 int test_gpio_0[] = {
1361 7, 8, 27, 28, 26, 25, 15, 1, 3, 0,
1362 37, 38,35, 36, 42, 41, 21, 22, 24, 23,
1363 54, 125, 18, 13, 14, 17, 16, 5, 4, 2, 12,
1364 44, 45, 47
1365 };
1366 int i, j, ret_0, ret_1, total_1, total_0, n = 0;
1367 total_0 = (sizeof(test_gpio_0)/sizeof(int));
1368 total_1 = (sizeof(test_gpio_1)/sizeof(int));
1369
wangyouqiang7775cbf2023-12-29 16:35:24 +08001370 //system("at at+gpsinit=0");
1371 system("i2cset -y -f 2 0x32 0x0d 0x00");
1372 system("i2cset -y -f 2 0x31 0x18 0x8f");
1373 usleep(10000);
wangyouqiangece1c722023-12-12 17:28:53 +08001374
1375 //printf("[init]gpio_test_init_test_gpio_mode FINISH\n");
1376 /* [1 all gpio set to low] */
1377 for(j = 0; j < 6 ; j++){
1378 for(i = 0; i < total_1; i++){
1379 gpio_register_set_func_0(test_gpio_1[i]);
1380 gpio_register_set_direction(test_gpio_1[i], 1);
1381 gpio_register_set_value(test_gpio_1[i], 0);
1382 }
1383 for(i = 0; i < total_0; i++){
1384 gpio_register_set_func_0(test_gpio_0[i]);
1385 gpio_register_set_direction(test_gpio_0[i], 1);
1386 gpio_register_set_value(test_gpio_0[i], 0);
1387 }
1388 //usleep(10000);
1389
1390 for(i = 0; i <= 35; i++){
1391 ret_0 = mbtk_gpio_adc(0);
1392 usleep(3000);
1393 ret_1 = mbtk_gpio_adc(1);
1394
1395 if(0 == (ret_0 + ret_1)){
1396 break;
1397 }
1398 }
1399 printf("pre set ADC: %d, times: %d\n",(ret_0 + ret_1), j);
1400 if(0 == (ret_0 + ret_1)){
1401 break;
1402 }
1403 }
1404 if(0 != (ret_0 + ret_1)){
wangyouqiang7775cbf2023-12-29 16:35:24 +08001405
wangyouqiangece1c722023-12-12 17:28:53 +08001406 printf("Fail, please retest");
1407 return -1;
1408 }
wangyouqiang7775cbf2023-12-29 16:35:24 +08001409 gpio_register_set_value(119, 1);
1410 gpio_register_set_value(117, 1);
wangyouqiangece1c722023-12-12 17:28:53 +08001411
1412}
1413
b.liu94091352024-07-02 10:38:08 +08001414int mbtk_at_gpio(bool gnss_support, void* arg)
wangyouqiangece1c722023-12-12 17:28:53 +08001415{
wangyouqiang7775cbf2023-12-29 16:35:24 +08001416
1417 printf("Start test gpio V1.0\n");
wangyouqiangece1c722023-12-12 17:28:53 +08001418 int test_gpio_1[] = {
1419 40, 33, 34, 39, 99,
1420 56, 58, 55, 57, 48, 59, 20, 53,
1421 19, 49, 50, 32 ,31, 6, 11, 10,7,
wangyouqiang7775cbf2023-12-29 16:35:24 +08001422 90, 46, 127,
1423 88, 87, 86, 85, 84, 89,
wangyouqiangece1c722023-12-12 17:28:53 +08001424 };
1425
1426 int test_gpio_0[] = {
1427 35, 36, 42, 41, 21, 22, 24, 23,
1428 54, 125, 18, 13, 14, 17, 16, 5, 4, 2, 12,0,
1429 27, 28, 26, 25, 15, 1, 3,
1430 37, 38,8
wangyouqiang7775cbf2023-12-29 16:35:24 +08001431 };
1432 int test_gpio_2[] = { //GPS组
1433 43,47,
1434 //45,44
wangyouqiangece1c722023-12-12 17:28:53 +08001435 };
1436
wangyouqiang7775cbf2023-12-29 16:35:24 +08001437 int i, j, ret_0, ret_1, ret_2, total_1, total_0, total_2, n = 0;
wangyouqiangece1c722023-12-12 17:28:53 +08001438 int *fail_io = (int *)arg;
1439 total_0 = (sizeof(test_gpio_0)/sizeof(int));
1440 total_1 = (sizeof(test_gpio_1)/sizeof(int));
wangyouqiang7775cbf2023-12-29 16:35:24 +08001441 total_2 = (sizeof(test_gpio_2)/sizeof(int));
wangyouqiangece1c722023-12-12 17:28:53 +08001442
1443 /* [1 all gpio Init] */
1444 gpio_test_init_test_gpio_mode();
1445
1446
1447 /* [2 GPIOTEST] */
wangyouqiangece1c722023-12-12 17:28:53 +08001448 //Test 0
1449 for(i = 0; i < total_0; i++){
1450 ret_0 = gpio_register_test_out_0(test_gpio_0[i], 0);
1451 if(-1 == ret_0){
1452 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_0[i]);
1453 fail_io[n] = test_gpio_0[i];
1454 n++;
1455 }else{
b.liuc7ffd092024-01-03 15:23:07 +08001456 //printf("############gpio [%d] test success############\n", test_gpio_0[i]);
wangyouqiangece1c722023-12-12 17:28:53 +08001457 }
1458 }
1459
1460 //Test 1
1461 for(i = 0; i < total_1; i++){
1462 ret_1 = gpio_register_test_out_1(test_gpio_1[i], 0);
1463 if(-1 == ret_1){
1464 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_1[i]);
1465 fail_io[n] = test_gpio_1[i];
1466 n++;
1467 }else{
b.liuc7ffd092024-01-03 15:23:07 +08001468 //printf("############gpio [%d] test success############\n", test_gpio_1[i]);
wangyouqiangece1c722023-12-12 17:28:53 +08001469 }
1470 }
wangyouqiang7775cbf2023-12-29 16:35:24 +08001471
1472 //Test 2
1473 for(i = 0; i < total_2; i=i+2){
1474 ret_2 = gpio_register_test_out_2(test_gpio_2[i], test_gpio_2[i+1], 0);
1475 if(-1 == ret_2){
b.liuc7ffd092024-01-03 15:23:07 +08001476 //printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_2[i+1]);
wangyouqiang7775cbf2023-12-29 16:35:24 +08001477 fail_io[n] = test_gpio_2[i];
1478 n++;
1479 }else{
1480 printf("############gpio [%d] test success############\n", test_gpio_2[i+1]);
1481 }
1482 }
1483
wangyouqiangece1c722023-12-12 17:28:53 +08001484 mbtk_adc_close();
1485 return n;
1486}
1487
wangyouqiang48adc142024-06-24 20:07:30 +08001488#elif 0//(MBTK_PROJECT_T108)
wangyouqiangb1deff32024-03-05 13:31:27 +08001489int gpio_register_test_out(int port, int value)
1490{
1491 int ret;
1492 int i;
1493 int valueh = 0;
1494 int valuel = 1;
1495
1496 //printf("Gpio port [%d] test start!\n", port);
1497 ret = gpio_register_set_func_0(port); //设功能为GPIO
1498 if(ret){
1499 printf("gpio_port can't support!\n");
1500 return -1;
1501 }
1502 gpio_register_set_direction(port, 1); //设方向为输出
1503 //ret = gpio_register_get_value(port);
1504 //printf("gpio default value is: %d\n", ret);
1505
1506
1507 //[High]
1508 for(i = 0; i <= 9; i++){
1509 gpio_register_set_value(port, 1);
1510 usleep(50);
1511 //valueh = gpio_register_get_value(port);
1512 //printf("set high? %d\n", valueh);
1513 if(1 == valueh){
1514 break;
1515 }
1516 }
1517
1518 for(i = 0; i <= 35; i++){
1519 ret = gpio_register_get_value(118);
1520 if(1 == ret){
1521 break;
1522 }
1523 usleep(3000);
1524 }
1525 printf("******gpio should is high: %d.******\n", ret);
1526 if(1 != ret){
1527 ret=-1;
1528 goto exit;
1529 }
1530
1531 //[Low]
1532 usleep(200);
1533 for(i = 0; i <= 9; i++){
1534 gpio_register_set_value(port, 0); //输出低
1535 usleep(50);
1536 //valuel = gpio_register_get_value(port);
1537 //printf("set low? %d\n", valuel);
1538 if(0 == valuel){
1539 break;
1540 }
1541 }
1542
1543 for(i = 0; i <= 35; i++){
1544 ret = gpio_register_get_value(118);
1545 if(0 == ret){
1546 break;
1547 }
1548 usleep(3000);
1549 }
1550 //printf("******gpio should is low: %d.******\n", ret);
1551 if(0 != ret){
1552 ret=-1;
1553 goto exit;
1554 }
1555
1556exit:
1557 gpio_register_set_value(port, 0);
1558 gpio_register_set_direction(port, 0); //设方向为输入
1559 return ret;
1560}
1561
1562int gpio_test_init_test_gpio_mode(void)
1563{
wangyouqiang48adc142024-06-24 20:07:30 +08001564 const int test_gpio[] = {
wangyouqiangb1deff32024-03-05 13:31:27 +08001565 99,
1566 117,
1567 21,22,23,24,44,41,120,
1568 8,127,46,59,58,57,56,55,48,19,34,33,35,36,49,
1569 50,25,28,26,122,20,10,11,
1570 39,40,37,38,
1571 51,52,31,32,
1572 };
1573
1574 int i, j, ret, total, n = 0;
1575 total = (sizeof(test_gpio)/sizeof(int));
1576 printf("[init]gpio_test_init_test_gpio_mode BEGIN\n");
1577
1578 /* [1 all gpio set to low] */
wangyouqiangb1deff32024-03-05 13:31:27 +08001579 gpio_register_set_func_0(118);
1580 gpio_register_set_direction(118, 0);
1581 system("echo in > /sys/class/gpio/gpio118/direction");
1582
1583 for(j = 0; j < 6 ; j++){
1584 for(i = 0; i < total; i++){
1585 gpio_register_set_func_0(test_gpio[i]);
1586 gpio_register_set_direction(test_gpio[i], 1);
1587 gpio_register_set_value(test_gpio[i], 0);
1588 //ret = gpio_register_get_value(test_gpio[i]);
1589 //printf("[init]get gpio%d=%d\n", test_gpio[i], ret);
1590 }
wangyouqiangb1deff32024-03-05 13:31:27 +08001591
1592 for(i = 0; i <= 35; i++){
1593 ret = gpio_register_get_value(118);
1594 printf("[init]get gpi118=%d\n", ret);
1595 usleep(3000);
1596 if(0 == (ret)){
1597 break;
1598 }
1599 }
1600
1601 printf("pre set ADC: %d, times: %d\n",(ret), j);
1602 if(0 == (ret)){
1603 break;
1604 }
1605 }
1606 if(0 != (ret)){
1607 printf("!!!Set all low FAIL, please retest\n");
1608 return -1;
1609 }
1610 return 0;
1611}
1612
b.liu94091352024-07-02 10:38:08 +08001613int mbtk_at_gpio(bool gnss_support, void* arg)
wangyouqiangb1deff32024-03-05 13:31:27 +08001614{
wangyouqiangb1deff32024-03-05 13:31:27 +08001615
wangyouqiang48adc142024-06-24 20:07:30 +08001616 const int test_gpio[] = {
1617 //GPIO PIN GPIO PIN GPIO PIN GPIO PIN
1618 99, 170, 117, 59, 21, 61, 22, 62,
1619 23, 144, 24, 147, 44, 5, 41, 159,
1620 120, 143, 8, 171, 127, 160, 46, 149,
1621 59, 19, 58, 18, 57, 20, 56, 21,
1622 55, 22, 48, 23, 19, 3, 34, 79,
1623 33, 80, 35, 78, 36, 77, 49, 43,
1624 50, 42, 25, 67, 28, 66, 26, 65,
1625 122, 169, 20, 152, 10, 74, 11, 73,
1626 39, 166, 40, 164, 37, 165, 38, 163,
1627 51, 58, 52, 60, 31, 57, 32, 56,
wangyouqiangb1deff32024-03-05 13:31:27 +08001628 };
1629
1630 int i, n = 0, ret, total;
1631 int *fail_io = (int *)arg;
wangyouqiang48adc142024-06-24 20:07:30 +08001632 int try_count = 0;
wangyouqiangb1deff32024-03-05 13:31:27 +08001633 total = (sizeof(test_gpio)/sizeof(int));
wangyouqiang48adc142024-06-24 20:07:30 +08001634 printf("T108 Start test gpio V1.0, total gpio=%d\n", (total/2));
wangyouqiangb1deff32024-03-05 13:31:27 +08001635
wangyouqiang48adc142024-06-24 20:07:30 +08001636 for(try_count; try_count < 4; try_count++){
1637 n = 0;
1638 /* [1 all gpio Init] */
1639 gpio_test_init_test_gpio_mode();
wangyouqiangb1deff32024-03-05 13:31:27 +08001640
wangyouqiang48adc142024-06-24 20:07:30 +08001641 /* [2 GPIOTEST] */
1642 for(i = 0; i < total; i = i + 2){
1643 ret = gpio_register_test_out(test_gpio[i], 0);
1644 if(-1 == ret){
yq.wang67c76d52024-06-24 06:11:36 -07001645 n++;
wangyouqiang48adc142024-06-24 20:07:30 +08001646 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio[i]);
1647 if(try_count != 3){
1648 printf(" ---TEST FAILED! RETRY!--- \n");
1649 usleep(5000);
1650 break;
1651 }
yq.wang67c76d52024-06-24 06:11:36 -07001652 fail_io[n - 1] = test_gpio[i+1];
wangyouqiang48adc142024-06-24 20:07:30 +08001653 }else{
1654 //printf("############gpio [%d] test success############\n", test_gpio[i]);
1655 }
1656 }
1657 if(0 == n){
1658 break;
1659 }
wangyouqiangb1deff32024-03-05 13:31:27 +08001660 }
1661 return n;
1662}
1663
1664
wangyouqiang48adc142024-06-24 20:07:30 +08001665#elif (MBTK_PROJECT_T108)
1666static int gpio_test(int port)
1667{
1668 int ret;
1669 int i;
1670 int valueh = 0;
1671 int valuel = 1;
1672
1673 //printf("Gpio port [%d] test start!\n", port);
1674 ret = gpio_register_set_func_0(port);
1675 if(ret){
1676 printf("gpio_port can't support!\n");
1677 return -1;
1678 }
1679 gpio_register_set_direction(port, 1);
1680 //ret = gpio_register_get_value(port);
1681 //printf("gpio default value is: %d\n", ret);
1682
1683
1684 //[High]
1685 for(i = 0; i <= 5; i++){
1686 gpio_register_set_value(port, 1);
1687 usleep(50);
1688 //valueh = gpio_register_get_value(port);
1689 //printf("set high? %d\n", valueh);
1690 if(1 == valueh){
1691 break;
1692 }
1693 }
1694
1695 for(i = 0; i <= 5; i++){
1696 ret = gpio_register_get_value(118);
1697 if(1 == ret){
1698
1699 break;
1700 }
1701 usleep(3000);
1702 }
1703 //printf("count=%d \n", i);
1704 //printf("******gpio should is high: %d.******\n", ret);
1705 if(1 != ret){
1706 ret=-1;
1707 goto exit;
1708 }
1709
1710 //[Low]
1711 usleep(200);
1712 for(i = 0; i <= 9; i++){
1713 gpio_register_set_value(port, 0);
1714 usleep(50);
1715 //valuel = gpio_register_get_value(port);
1716 //printf("set low? %d\n", valuel);
1717 if(0 == valuel){
1718 break;
1719 }
1720 }
1721
1722 for(i = 0; i <= 5; i++){
1723 ret = gpio_register_get_value(118);
1724 if(0 == ret){
1725
1726 break;
1727 }
1728 usleep(3000);
1729 }
1730 //printf("count=%d \n", i);
1731 //printf("******gpio should is low: %d.******\n", ret);
1732 if(0 != ret){
1733 ret=-1;
1734 goto exit;
1735 }
1736
1737exit:
1738 gpio_register_set_value(port, 0);
1739 gpio_register_set_direction(port, 0);
1740 return ret;
1741
1742}
1743
1744static int gpio_test_init_test_gpio_mode(void)
1745{
1746 const int test_gpio[] = {
1747 99,8,117,21,22,23,24,41,120,19,123,
1748 58,59,57,56,55,48,125,127,36,35,34,
1749 33,54,47,53,46,50,49,11,10,26,28,25,
1750 27,32,31,51,52,39,40,37,38,
1751 44,45
1752 };
1753
1754 int i, j, ret, total, n = 0;
1755 total = (sizeof(test_gpio)/sizeof(int));
1756 //printf("[init]gpio_test_init_test_gpio_mode BEGIN\n");
1757
1758 /* [1 all gpio set to low] */
1759 gpio_register_set_func_0(118);
1760 gpio_register_set_direction(118, 0);
1761
1762 for(j = 0; j < 5 ; j++){
1763 for(i = 0; i < total; i++){
1764 gpio_register_set_func_0(test_gpio[i]);
1765 gpio_register_set_direction(test_gpio[i], 1);
1766 gpio_register_set_value(test_gpio[i], 0);
1767 //ret = gpio_register_get_value(test_gpio[i]);
1768 //printf("[init]get gpio%d=%d\n", test_gpio[i], ret);
1769 }
1770
1771
1772 for(i = 0; i <= 10; i++){
1773 ret = gpio_register_get_value(118);
1774 //printf("[init]get gpi118=%d\n", ret);
1775 usleep(3000);
1776 if(0 == (ret)){
1777 break;
1778 }
1779 }
1780
1781 //printf("pre set 118: %d, times: %d\n",(ret), j);
1782 if(0 == (ret)){
1783 break;
1784 }
1785 }
1786 if(0 != (ret)){
1787 printf("!!!Set all low FAIL, please retest\n");
1788 return -1;
1789 }
1790 return 0;
1791}
1792
b.liu94091352024-07-02 10:38:08 +08001793int mbtk_at_gpio(bool gnss_support, void* arg)
wangyouqiang48adc142024-06-24 20:07:30 +08001794{
1795 const int test_gpio[] = {
1796 //GPIO PIN GPIO PIN GPIO PIN GPIO PIN
1797 99, 170, 8, 171, 117, 59, 21, 61,
1798 22, 62, 23, 144, 24, 147, 41, 159,
1799 120, 143, 19, 3, 123, 5, 58, 18,
1800 59, 19, 57, 20, 56, 21, 55, 22,
1801 48, 23, 125, 149, 127, 160, 36, 77,
1802 35, 78, 34, 79, 33, 80, 54, 163,
1803 #if 0 //Hard Ware ERROR!
1804 47, 164,
1805 46, 166,
1806 #endif
1807 53, 165, 50, 42, 49, 43, 11, 73,
1808 10, 74, 26, 65, 28, 66, 25, 67,
1809 27, 68, 32, 56, 31, 57, 51, 58,
1810 52, 60, 39, 192, 40, 193, 37, 194,
1811 38, 195,
1812 #if 0 //undefine GNSS
1813 44, 161,
1814 45, 151,
1815 #endif
1816 };
1817
1818 int i, n = 0, ret, total;
1819 int *fail_io = (int *)arg;
1820 int try_count;
1821 total = (sizeof(test_gpio)/sizeof(int));
1822 printf("T108 V2 Start test gpio V0.8, total gpio=%d\n", (total/2));
1823
1824 for(try_count = 0; try_count < 4; try_count++){
1825 n = 0;
1826 /* [1 all gpio Init] */
1827 gpio_test_init_test_gpio_mode();
1828
1829 /* [2 GPIOTEST] */
1830 for(i = 0; i < total; i = i + 2){
1831 ret = gpio_test(test_gpio[i]);
1832 if(-1 == ret){
yq.wang67c76d52024-06-24 06:11:36 -07001833 n++;
wangyouqiang48adc142024-06-24 20:07:30 +08001834 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio[i]);
1835 if(try_count != 3){
1836 printf(" ---TEST FAILED! RETRY!--- \n");
1837 usleep(5000);
1838 break;
1839 }
yq.wang67c76d52024-06-24 06:11:36 -07001840 fail_io[n - 1] = test_gpio[i + 1];
wangyouqiang48adc142024-06-24 20:07:30 +08001841 }else{
1842 //printf("############gpio [%d] test success############\n", test_gpio[i]);
1843 }
1844 }
1845 if(0 == n){
1846 printf(" ---ALL PASS---\n");
1847 break;
1848 }
1849 }
1850 return n;
1851}
1852
wangyouqiangece1c722023-12-12 17:28:53 +08001853
b.liu4f8e8792023-11-01 23:25:22 +08001854#else
b.liu94091352024-07-02 10:38:08 +08001855int mbtk_at_gpio(bool gnss_support, void* arg)
b.liu4f8e8792023-11-01 23:25:22 +08001856{
1857 return -1;
1858}
1859
1860#endif