blob: 3aa9b9260a73a3e4b8cca9037d39b388028a246c [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
wangyouqiangb1deff32024-03-05 13:31:27 +080039#if (defined(MBTK_PROJECT_L508_X6) || defined(MBTK_PROJECT_T108))
wangyouqiang7775cbf2023-12-29 16:35:24 +080040struct gpio_register_function gpio_func_register[128] = {
41 {GPIO_FUNC_GPIO_00, 0},
42 {GPIO_FUNC_GPIO_01, 0},
43 {GPIO_FUNC_GPIO_02, 0},
44 {GPIO_FUNC_GPIO_03, 0},
45 {GPIO_FUNC_GPIO_04, 0},
46 {GPIO_FUNC_GPIO_05, 0},
47 {GPIO_FUNC_GPIO_06, 0},
48 {GPIO_FUNC_GPIO_07, 0},
49 {GPIO_FUNC_GPIO_08, 0},
50 {GPIO_FUNC_GPIO_09, 0},
51 {GPIO_FUNC_GPIO_10, 0},
52 {GPIO_FUNC_GPIO_11, 0},
53 {GPIO_FUNC_GPIO_12, 0},
54 {GPIO_FUNC_GPIO_13, 0},
55 {GPIO_FUNC_GPIO_14, 0},
56 {GPIO_FUNC_GPIO_15, 0},
57 {GPIO_FUNC_GPIO_16, 0},
58 {GPIO_FUNC_GPIO_17, 0},
59 {GPIO_FUNC_GPIO_18, 0},
60 {GPIO_FUNC_GPIO_19, 0},
61 {GPIO_FUNC_GPIO_20, 0},
62 {GPIO_FUNC_GPIO_21, 0},
63 {GPIO_FUNC_GPIO_22, 0},
64 {GPIO_FUNC_GPIO_23, 0},
65 {GPIO_FUNC_GPIO_24, 0},
66 {GPIO_FUNC_GPIO_25, 0},
67 {GPIO_FUNC_GPIO_26, 0},
68 {GPIO_FUNC_GPIO_27, 0},
69 {GPIO_FUNC_GPIO_28, 0},
70 {GPIO_FUNC_GPIO_29, 0},
71 {GPIO_FUNC_GPIO_30, 0},
72 {GPIO_FUNC_GPIO_31, 0},
73 {GPIO_FUNC_GPIO_32, 0},
74 {GPIO_FUNC_GPIO_33, 0},
75 {GPIO_FUNC_GPIO_34, 0},
76 {GPIO_FUNC_GPIO_35, 0},
wangyouqiangb1deff32024-03-05 13:31:27 +080077 {GPIO_FUNC_GPIO_36, 0},
78 {GPIO_FUNC_GPIO_37, 0}, // GPIO_37
79 {GPIO_FUNC_GPIO_38, 0}, // GPIO_38
80 {GPIO_FUNC_GPIO_39, 0}, // GPIO_39
81 {GPIO_FUNC_GPIO_40, 0}, // GPIO_40
82 {GPIO_FUNC_GPIO_41, 0}, //GPIO_41
83 {GPIO_FUNC_GPIO_42, 0}, //GPIO_42
wangyouqiang7775cbf2023-12-29 16:35:24 +080084 {GPIO_FUNC_GPIO_43, 0}, //GPIO_43
85 {GPIO_FUNC_GPIO_44,0}, //GPIO_44
86 {GPIO_FUNC_GPIO_45,0}, //GPIO_45
87 {GPIO_FUNC_GPIO_46,0}, //GPIO_46
88 {GPIO_FUNC_GPIO_47,0}, //GPIO_47
89 {GPIO_FUNC_SDIO_DAT3, 1}, //GPIO_48
90 {GPIO_FUNC_GPIO_49, 0},
91 {GPIO_FUNC_GPIO_50, 0},
92 {GPIO_FUNC_GPIO_51, 0},
93 {GPIO_FUNC_GPIO_52, 0},
94 {GPIO_FUNC_GPIO_53, 0},
95 {GPIO_FUNC_GPIO_54, 0},
96 {GPIO_FUNC_SDIO_DAT2, 1}, //GPIO_55
97 {GPIO_FUNC_SDIO_DAT1, 1}, //GPIO_56
98 {GPIO_FUNC_SDIO_DAT0, 1}, //GPIO_57
99 {GPIO_FUNC_SDIO_CMD, 1}, //GPIO_58
100 {GPIO_FUNC_SDIO_CLK, 1}, //GPIO_59
101 {GPIO_FUNC_GPIO_60, 0},
102 {0,0},{0,0},{0,0},{0,0},
103 {0,0},{0,0},
104 {GPIO_FUNC_DVL_0, 1},//GPIO_67
105 {GPIO_FUNC_DVL_1, 1},//GPIO_68
106 {GPIO_FUNC_GPIO_69, 0},
107 {GPIO_FUNC_GPIO_70, 0},
108 {GPIO_FUNC_QSPI_DAT3, 1}, //GPIO_71
109 {GPIO_FUNC_QSPI_DAT2, 1}, //GPIO_72
110 {GPIO_FUNC_QSPI_DAT1, 1}, //GPIO_73
111 {GPIO_FUNC_QSPI_DAT0, 1}, //GPIO_74
112 {GPIO_FUNC_QSPI_CLK, 1}, //GPIO_75
113 {GPIO_FUNC_QSPI_CS1, 1}, //GPIO_76
114 {GPIO_FUNC_GPIO_77, 0},
115 {GPIO_FUNC_GPIO_78, 0},
116 {GPIO_FUNC_GPIO_79, 0},
117 {GPIO_FUNC_GPIO_80, 0},
118 {GPIO_FUNC_USIM_UCLK, 1},//GPIO_81
119 {GPIO_FUNC_USIM_UIO, 1},//GPIO_82
120 {GPIO_FUNC_USIM_URSTn, 1},//GPIO_83
121
122 {GPIO_FUNC_MMC1_DAT3,5}, //GPIO_84
123 {GPIO_FUNC_MMC1_DAT2,5}, //GPIO_85
124 {GPIO_FUNC_MMC1_DAT1,5}, //GPIO_86
125 {GPIO_FUNC_MMC1_DAT0,5}, //GPIO_87
126 {GPIO_FUNC_MMC1_CMD,5}, //GPIO_88
127 {GPIO_FUNC_MMC1_CLK,5}, //GPIO_89
128 {GPIO_FUNC_MMC1_CD,1}, //GPIO_90
129 {0,0},
130 {0,0},{0,0},{0,0},{0,0},
131 {0,0},{0,0},{0,0},
132 {GPIO_FUNC_USB_ID, 1},//GPIO_99
133 {0,0},{0,0},{0,0},{0,0},
134 {0,0},{0,0},{0,0},{0,0},
135 {0,0},{0,0},{0,0},{0,0},
136 {0,0},{0,0},{0,0},{0,0},
137 {0,0},
138 {GPIO_FUNC_PRI_TDI, 1}, //GPIO_117
139 {GPIO_FUNC_PRI_TMS, 1}, //GPIO_118
140 {GPIO_FUNC_PRI_TCK, 1}, //GPIO_119
141 {GPIO_FUNC_PRI_TDO, 1}, //GPIO_120
142 {GPIO_FUNC_QSPI_VMODE_GPIO, 1}, //GPIO_121
143 {GPIO_FUNC_VBUS_DRV, 1}, //GPIO_122
144 {GPIO_FUNC_CLK_REQ, 1}, //GPIO_123
145 {0,0},
146 {GPIO_FUNC_VCXO_REQ, 1}, //GPIO_125
147 {0,0}, //GPIO_126
148 {GPIO_FUNC_VCXO_OUT, 1}, //GPIO_127
149 {0,0},
150};
151
152#else
liubin281ac462023-07-19 14:22:54 +0800153struct gpio_register_function gpio_func_register[128] = {
154 {GPIO_FUNC_GPIO_00, 0},
155 {GPIO_FUNC_GPIO_01, 0},
156 {GPIO_FUNC_GPIO_02, 0},
157 {GPIO_FUNC_GPIO_03, 0},
158 {GPIO_FUNC_GPIO_04, 0},
159 {GPIO_FUNC_GPIO_05, 0},
160 {GPIO_FUNC_GPIO_06, 0},
161 {GPIO_FUNC_GPIO_07, 0},
162 {GPIO_FUNC_GPIO_08, 0},
163 {GPIO_FUNC_GPIO_09, 0},
164 {GPIO_FUNC_GPIO_10, 0},
165 {GPIO_FUNC_GPIO_11, 0},
166 {GPIO_FUNC_GPIO_12, 0},
167 {GPIO_FUNC_GPIO_13, 0},
168 {GPIO_FUNC_GPIO_14, 0},
169 {GPIO_FUNC_GPIO_15, 0},
170 {GPIO_FUNC_GPIO_16, 0},
171 {GPIO_FUNC_GPIO_17, 0},
172 {GPIO_FUNC_GPIO_18, 0},
173 {GPIO_FUNC_GPIO_19, 0},
174 {GPIO_FUNC_GPIO_20, 0},
175 {GPIO_FUNC_GPIO_21, 0},
176 {GPIO_FUNC_GPIO_22, 0},
177 {GPIO_FUNC_GPIO_23, 0},
178 {GPIO_FUNC_GPIO_24, 0},
179 {GPIO_FUNC_GPIO_25, 0},
180 {GPIO_FUNC_GPIO_26, 0},
181 {GPIO_FUNC_GPIO_27, 0},
182 {GPIO_FUNC_GPIO_28, 0},
183 {GPIO_FUNC_GPIO_29, 0},
184 {GPIO_FUNC_GPIO_30, 0},
185 {GPIO_FUNC_GPIO_31, 0},
186 {GPIO_FUNC_GPIO_32, 0},
187 {GPIO_FUNC_GPIO_33, 0},
188 {GPIO_FUNC_GPIO_34, 0},
189 {GPIO_FUNC_GPIO_35, 0},
190 {GPIO_FUNC_GPIO_36, 0},
191 {GPIO_FUNC_MMC1_DAT3, 5}, // GPIO_37
192 {GPIO_FUNC_MMC1_DAT2, 5}, // GPIO_38
193 {GPIO_FUNC_MMC1_DAT1, 5}, // GPIO_39
194 {GPIO_FUNC_MMC1_DAT0, 5}, // GPIO_40
195 {GPIO_FUNC_MMC1_CMD, 5}, //GPIO_41
196 {GPIO_FUNC_MMC1_CLK, 5}, //GPIO_42
197 {GPIO_FUNC_MMC1_CD , 1}, //GPIO_43
198 {0,0},{0,0},{0,0},{0,0},
199 {GPIO_FUNC_SDIO_DAT3, 1}, //GPIO_48
200 {GPIO_FUNC_GPIO_49, 0},
201 {GPIO_FUNC_GPIO_50, 0},
202 {GPIO_FUNC_GPIO_51, 0},
203 {GPIO_FUNC_GPIO_52, 0},
204 {GPIO_FUNC_GPIO_53, 0},
205 {GPIO_FUNC_GPIO_54, 0},
206 {GPIO_FUNC_SDIO_DAT2, 1}, //GPIO_55
207 {GPIO_FUNC_SDIO_DAT1, 1}, //GPIO_56
208 {GPIO_FUNC_SDIO_DAT0, 1}, //GPIO_57
209 {GPIO_FUNC_SDIO_CMD, 1}, //GPIO_58
210 {GPIO_FUNC_SDIO_CLK, 1}, //GPIO_59
211 {GPIO_FUNC_GPIO_60, 0},
212 {0,0},{0,0},{0,0},{0,0},
213 {0,0},{0,0},
214 {GPIO_FUNC_DVL_0, 1},//GPIO_67
215 {GPIO_FUNC_DVL_1, 1},//GPIO_68
216 {GPIO_FUNC_GPIO_69, 0},
217 {GPIO_FUNC_GPIO_70, 0},
218 {GPIO_FUNC_QSPI_DAT3, 1}, //GPIO_71
219 {GPIO_FUNC_QSPI_DAT2, 1}, //GPIO_72
220 {GPIO_FUNC_QSPI_DAT1, 1}, //GPIO_73
221 {GPIO_FUNC_QSPI_DAT0, 1}, //GPIO_74
222 {GPIO_FUNC_QSPI_CLK, 1}, //GPIO_75
223 {GPIO_FUNC_QSPI_CS1, 1}, //GPIO_76
224 {GPIO_FUNC_GPIO_77, 0},
225 {GPIO_FUNC_GPIO_78, 0},
226 {GPIO_FUNC_GPIO_79, 0},
227 {GPIO_FUNC_GPIO_80, 0},
228 {GPIO_FUNC_USIM_UCLK, 1},//GPIO_81
229 {GPIO_FUNC_USIM_UIO, 1},//GPIO_82
230 {GPIO_FUNC_USIM_URSTn, 1},//GPIO_83
231 {0,0},{0,0},{0,0},{0,0},
232 {0,0},{0,0},{0,0},{0,0},
233 {0,0},{0,0},{0,0},{0,0},
234 {0,0},{0,0},{0,0},
235 {GPIO_FUNC_USB_ID, 1},//GPIO_99
236 {0,0},{0,0},{0,0},{0,0},
237 {0,0},{0,0},{0,0},{0,0},
238 {0,0},{0,0},{0,0},{0,0},
239 {0,0},{0,0},{0,0},{0,0},
240 {0,0},
241 {GPIO_FUNC_PRI_TDI, 1}, //GPIO_117
242 {GPIO_FUNC_PRI_TMS, 1}, //GPIO_118
243 {GPIO_FUNC_PRI_TCK, 1}, //GPIO_119
244 {GPIO_FUNC_PRI_TDO, 1}, //GPIO_120
245 {GPIO_FUNC_QSPI_VMODE_GPIO, 1}, //GPIO_121
246 {GPIO_FUNC_VBUS_DRV, 1}, //GPIO_122
247 {GPIO_FUNC_CLK_REQ, 1}, //GPIO_123
248 {0,0},
249 {GPIO_FUNC_VCXO_REQ, 1}, //GPIO_125
250 {GPIO_FUNC_VCXO_OUT, 1}, //GPIO_126
251 {0,0},
252};
liubin281ac462023-07-19 14:22:54 +0800253
wangyouqiang7775cbf2023-12-29 16:35:24 +0800254#endif
liubin281ac462023-07-19 14:22:54 +0800255
wangyouqiangece1c722023-12-12 17:28:53 +0800256static int mbtk_gpio_adc(channel)
liubin281ac462023-07-19 14:22:54 +0800257{
liubin281ac462023-07-19 14:22:54 +0800258 int ret = 0;
259
wangyouqiangece1c722023-12-12 17:28:53 +0800260 ret = mbtk_adc_get(channel);
261 //printf("ADC_%d_value =%d\n", channel, ret);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800262 if(ret >= 500 ){
263 ret = 1;
liubin281ac462023-07-19 14:22:54 +0800264 }
wangyouqiang7850b4a2023-09-18 15:47:57 +0800265 else if (ret>=0 && ret<=480){
266 ret = 0;
267 }
268 else{
269 ret = -1;
liubin281ac462023-07-19 14:22:54 +0800270 }
271 return ret;
272}
liubin281ac462023-07-19 14:22:54 +0800273
liubin281ac462023-07-19 14:22:54 +0800274
275static int hwacc_register(int rw, unsigned int addr, unsigned int *data)
276{
277 int fid;
278 unsigned int pagesize, len, len_aligned;
279 unsigned int addr_aligned;
280 volatile unsigned int *pa;
281 void *vpa;
282
283 len = pagesize = sysconf(_SC_PAGESIZE);
284 if((fid = open(HWMAP_DEVICE, O_RDWR)) < 0)
285 {
286 printf("Failed to open %s\n", HWMAP_DEVICE);
287 exit(-1);
288 }
289
290 // Align the length so the mapped area is page-aligned and contains the requested area
291 addr_aligned = addr & PAGE_MASK_BITS(pagesize);
292 len_aligned =((addr + len - addr_aligned) + pagesize - 1) & PAGE_MASK_BITS(pagesize);
293
294 /* Notes on flags: MAP_PRIVATE results in copy on write; MAP_SHARED allows normal write */
295 /* MAP_SHARED required O_RDWR in open above, otherwise mmap fails with errno=EACCES */
296 /* Notes on prot: PROT_WRITE allows read and write; PROT_READ allows read only */
297 /* Notes on off: an unsigned 32-bit value, should be aligned to page size according to mmap manpage */
298 if((vpa = mmap(0, len_aligned, PROT_READ|PROT_WRITE, MAP_SHARED, fid, addr_aligned)) == MAP_FAILED)
299 {
300 printf("mmap failed (%d)\n", errno);
301 }
302 else
303 {
304 pa = (volatile unsigned int *)((unsigned char *)vpa + (addr & PAGE_OFFS_BITS(pagesize)));
305 if(rw == 0)
306 {
307 *data = *pa;
308 gpio_log("Value read from 0x%.8x via MVA=0x%p is 0x%.8x\n", addr, pa, *data);
309 }
310 else if(rw == 1)
311 {
312 *pa = *data;
313 gpio_log("Value %.8x written to 0x%.8x via MVA=0x%p\n", *data, addr, pa);
314#if defined(HWACC_DEBUG)
315 {
316 unsigned int val;
317 val = *pa;
318 printf("Value read from 0x%.8x via MVA=0x%p is 0x%.8x\n", addr, pa, val);
319 }
320#endif
321 }
322 munmap(vpa, len);
323 }
324
325 close(fid);
326 return 0;
327}
328/*
329设置GPIO 模式:第一步读GPIO22默认function
330root@OpenWrt:/# hwacc r 0xd401e134
331Option = r Addr = d401e134
332Value read from 0xd401e134 via MVA=0x0xb6fc3134 is 0x0000d040 //默认GPIO功能
333Bit0~bit2值对应上面表格各function,0代表GPIO功能
334 */
335static int gpio_register_read(int reg)
336{
337 int ret = -1;
338#if 0
339 FILE * fp;
340 // "/bin/hwacc r 0xd401e134";
341 char command[36] = {0};
342 char buffer[1024];
343 int i = 0;
344
345 sprintf(command, "/bin/hwacc r 0x%x", reg);
346 fp = popen(command, "r");
347 while(1)
348 {
349 if( fgets (buffer, sizeof(buffer), fp)!=NULL ) {
350 buffer[strlen(buffer) - 1] = 0;
351 // gpio_log("out [%d]: %s\n", strlen(buffer), buffer);
352 }else{
353 break;
354 }
355 i = strstr_n(buffer, "is");
356 if(i)
357 {
358 ret = str_to_hex(&buffer[i + 2]);
359 gpio_log("read 0x%x value:%s, %x\n", reg, &buffer[i + 2], ret);
360 }
361 }
362 pclose(fp);
363#else
b.liu85565a72023-11-09 16:47:40 +0800364#ifndef MBTK_PROJECT_PN1803
365 usleep(50);
366#endif
liubin281ac462023-07-19 14:22:54 +0800367 hwacc_register(0, reg, &ret);
b.liu85565a72023-11-09 16:47:40 +0800368#ifndef MBTK_PROJECT_PN1803
369 usleep(50);
370#endif
liubin281ac462023-07-19 14:22:54 +0800371#endif
372 return ret;
373}
374/*
375设置输入输出状态,设置PDR寄存器GPIO22为output
376root@OpenWrt:/# hwacc w 0xD401900c 0x00c03800
377Option = w Addr = d401900c Data=00c03800
378Value 00c03800 written to 0xd401900c via MVA=0x0xb6f9f00c
379 */
380static void gpio_register_write(int reg, int value)
381{
382#if 0
383 FILE * fp;
384 // "/bin/hwacc w 0xD401900c 0x00c03800"
385 char command[36] = {0};
386 char buffer[1024];
387
388 sprintf(command, "/bin/hwacc w 0x%x 0x%x", reg, value);
389 gpio_log("command: %s\n", command);
390 fp = popen(command, "r");
391 while(1)
392 {
393 if( fgets (buffer, sizeof(buffer), fp)!=NULL ) {
394 gpio_log("%s\n", buffer);
395 }else{
396 break;
397 }
398 }
399 pclose(fp);
400
401#else
b.liu85565a72023-11-09 16:47:40 +0800402#ifndef MBTK_PROJECT_PN1803
403 usleep(50);
404#endif
liubin281ac462023-07-19 14:22:54 +0800405 hwacc_register(1, reg, &value);
b.liu85565a72023-11-09 16:47:40 +0800406#ifndef MBTK_PROJECT_PN1803
407 usleep(50);
408#endif
liubin281ac462023-07-19 14:22:54 +0800409#endif
410}
411/*
412AF SEL<p>This field is used for alternate function selection for a pin.
413It selects between the eight possible alternate functions for the pin.
414Alternate function 0 is always the reset case.
415
416<p>0x0 = Alternate function 0 (primary function at reset)
417<p>0x1 = Alternate function 1
418<p>0x2 = Alternate function 2
419<p>0x3 = Alternate function 3
420<p>0x4 = Alternate function 4
421<p>0x5 = Alternate function 5
422<p>0x6 = Alternate function 6
423<p>0x7 = Alternate function 7
424 */
425static int gpio_register_set_func_0(int port)
426{
427 int ret;
428 struct gpio_register_function *reg = NULL;
429 if(port > 128)
430 return -1;
431 reg = &gpio_func_register[port];
432 if(0 == reg->reg)
433 return -1;
434 ret = gpio_register_read(reg->reg);
435 if((ret & 0x7) != reg->func_gpio)
436 {
wangyouqiang7850b4a2023-09-18 15:47:57 +0800437 //printf("Gpio set func [%d] [0x%x]!\n", reg->func_gpio, (ret & 0xfffffff8) | reg->func_gpio);
liubin281ac462023-07-19 14:22:54 +0800438 gpio_register_write(reg->reg, (ret & 0xfffffff8) | reg->func_gpio);
439 }
440 return 0;
441}
442/*
443设置GPIO 方向
444读取输入输出状态,读PDR寄存器:0x0c
445root@OpenWrt:/# hwacc r 0xD401900c
446Option = r Addr = d401900c
447Value read from 0xd401900c via MVA=0x0xb6f3900c is 0x00803800 //bit22为0,代表Input
448 */
449static void gpio_register_set_direction(int port, int dir)
450{
451 int ret;
452 int reg = 0xD4019000;
453
454 if(port > (32 - 1))
455 reg = 0xD4019004;
456 if(port > (32 * 2 - 1))
457 reg = 0xD4019008;
458 if(port > (32 * 3 - 1))
459 reg = 0xD4019100;
460
461 reg += 0x0c;
462 port = port % 0x20;
463 ret = gpio_register_read(reg);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800464 //printf("[Direction] reg_value=%x\n", ret);
liubin281ac462023-07-19 14:22:54 +0800465 // 设置 输出 0 && 1
466 if(!(ret & (0x1 << port)) && dir)
467 {
468 gpio_register_write(reg, ret | (0x1 << port));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800469 //printf("[Direction out] reg_value=%x\n", gpio_register_read(reg));
liubin281ac462023-07-19 14:22:54 +0800470 }
471 // 设置 输入 1 && 0
472 if((ret & (0x1 << port)) && !dir)
473 {
474 gpio_register_write(reg, ret | !(0x1 << port));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800475 //printf("[Direction in] reg_value=%x\n", gpio_register_read(reg));
liubin281ac462023-07-19 14:22:54 +0800476 }
477}
478/*
479设置GPIO 输出电平
480
481读取电平状态,先读PLR寄存器:0x00
482root@OpenWrt:/# hwacc r 0xD4019000
483Option = r Addr = d4019000
484Value read from 0xd4019000 via MVA=0x0xb6f1c000 is 0x81e82a30
485对应下面具体BIT
4861000 0001 1110 1000 0010 1010 0011 0000 BIT22默认电平高
487
488设置输出高:设置PSR寄存器:0x18(只写寄存器)
489root@OpenWrt:/# hwacc w 0xD4019018 0x400000
490Option = w Addr = d4019018 Data=00400000
491Value 00400000 written to 0xd4019018 via MVA=0x0xb6f56018 //bit22写1,输出高
492
493设置输出低:设置PCR寄存器:0x24
494root@OpenWrt:/# hwacc w 0xD4019024 0x400000
495Option = w Addr = d4019024 Data=00400000
496Value 00400000 written to 0xd4019024 via MVA=0x0xb6faa024 //Bit22写1,GPIO22输出低
497
498 */
499static void gpio_register_set_value(int port, int value)
500{
501 int ret;
502 int reg = 0xD4019000;
503
504 if(port > (32 - 1))
505 reg = 0xD4019004;
506 if(port > (32 * 2 - 1))
507 reg = 0xD4019008;
508 if(port > (32 * 3 - 1))
509 reg = 0xD4019100;
510
511 if(value)
512 {
513 reg += 0x18;
514 }
515 else
516 reg += 0x24;
517
518 port = port % 0x20;
519 ret = gpio_register_read(reg);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800520 //printf("[Value] reg_value=%x\n", gpio_register_read(0xD4019004));
liubin281ac462023-07-19 14:22:54 +0800521 // 设置 高电平 0 && 1
522 if(value)
523 {
524 gpio_register_write(reg, ret | (0x1 << port));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800525 //printf("[Value high] reg_value=%x\n", gpio_register_read(0xD4019004));
liubin281ac462023-07-19 14:22:54 +0800526 return;
527 }
528 // 设置 低电平 1 && 0
529 if(!(ret & (0x1 << port)) && !value)
530 {
531 gpio_register_write(reg, ret | (0x1 << port));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800532 //printf("[Value low] reg_value=%x\n", gpio_register_read(0xD4019004));
liubin281ac462023-07-19 14:22:54 +0800533 }
534}
535/*
536读取电平状态,先读PLR寄存器:0x00
537root@OpenWrt:/# hwacc r 0xD4019000
538Option = r Addr = d4019000
539Value read from 0xd4019000 via MVA=0x0xb6f1c000 is 0x81e82a30
540对应下面具体BIT
5411000 0001 1110 1000 0010 1010 0011 0000 BIT22默认电平高
542 */
543static int gpio_register_get_value(int port)
544{
545 int ret = -1;
546 int reg = 0xD4019000;
547
548 if(port > (32 - 1))
549 reg = 0xD4019004;
550 if(port > (32 * 2 - 1))
551 reg = 0xD4019008;
552 if(port > (32 * 3 - 1))
553 reg = 0xD4019100;
554 port = port % 0x20;
555 ret = gpio_register_read(reg);
556 if(ret & (0x1 << port))
557 {
558 return 1;
559 }
560 return 0;
561}
562
563void gpio_debug_set(int enable)
564{
565 gpio_debug = enable;
566}
567
b.liu85565a72023-11-09 16:47:40 +0800568
569
570#if defined(MBTK_PROJECT_PN1803)
liubin281ac462023-07-19 14:22:54 +0800571int gpio_register_test_out(int port, int value)
572{
573 int ret;
wangyouqiang7850b4a2023-09-18 15:47:57 +0800574 int i;
575 int valueh = 0;
576 int valuel = 1;
577
wangyouqiang4c262fa2023-10-17 17:48:03 +0800578 //printf("Gpio port [%d] test start!\n", port);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800579 ret = gpio_register_set_func_0(port); //设功能为GPIO
580 if(ret){
liubin281ac462023-07-19 14:22:54 +0800581 printf("gpio_port can't support!\n");
582 return -1;
583 }
wangyouqiang7850b4a2023-09-18 15:47:57 +0800584 gpio_register_set_direction(port, 1); //设方向为输出
liubin281ac462023-07-19 14:22:54 +0800585 ret = gpio_register_get_value(port);
wangyouqiang4c262fa2023-10-17 17:48:03 +0800586 //printf("gpio default value is : %d.\n", ret);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800587
588
589 //[High]
590 for(i = 0; i <= 10; i++){
591 gpio_register_set_value(port, 1);
592 usleep(50);
593 valueh = gpio_register_get_value(port);
wangyouqiang4c262fa2023-10-17 17:48:03 +0800594 //printf("set high? %d\n",valueh);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800595 if(1 == valueh){
596 break;
597 }
598 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800599 //usleep(10000);
600 for(i = 0; i <= 35; i++){
wangyouqiangece1c722023-12-12 17:28:53 +0800601 ret = mbtk_gpio_adc(0);
wangyouqiang59d8dd22023-10-19 18:03:54 +0800602 usleep(3000);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800603 if(1 == ret){
604 break;
605 }
606 }
wangyouqiang7850b4a2023-09-18 15:47:57 +0800607 if(1 != ret){
608 ret=-1;
liubin281ac462023-07-19 14:22:54 +0800609 goto exit;
wangyouqiang7850b4a2023-09-18 15:47:57 +0800610 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800611 printf("******gpio should is high: %d.******\n", ret);
liubin281ac462023-07-19 14:22:54 +0800612
wangyouqiang7850b4a2023-09-18 15:47:57 +0800613
614 //[Low]
615 usleep(200);
616 for(i = 0; i <= 10; i++){
617 gpio_register_set_value(port, 0); //输出低
b.liu074771a2023-10-19 13:13:39 +0800618 usleep(50);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800619 valuel = gpio_register_get_value(port);
wangyouqiang59d8dd22023-10-19 18:03:54 +0800620 //printf("set low? %d\n",valuel);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800621 if(0 == valuel){
622 break;
623 }
624 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800625 //usleep(10000);
626 for(i = 0; i <= 35; i++){
wangyouqiangece1c722023-12-12 17:28:53 +0800627 ret = mbtk_gpio_adc(0);
wangyouqiang59d8dd22023-10-19 18:03:54 +0800628 usleep(3000);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800629 if(0 == ret){
630 break;
631 }
632 }
b.liu074771a2023-10-19 13:13:39 +0800633 printf("******gpio should is low: %d.******\n", ret);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800634 if(0 != ret){
635 ret=-1;
liubin281ac462023-07-19 14:22:54 +0800636 goto exit;
637 }
638
639exit:
wangyouqiang7850b4a2023-09-18 15:47:57 +0800640 //gpio_register_set_direction(port, 0); //设方向为输入
liubin281ac462023-07-19 14:22:54 +0800641 return ret;
642}
643
644int mbtk_at_gpio(void* arg)
645{
b.liu37e72162023-11-01 15:38:18 +0800646 int test_gpio[] = {
647 56, 55, 58, 57, 48, 59, 12, 20, 5, 43, 21,
648 49, 50, 27, 28, 26, 25, 16, 17, 15, 126, 125,
b.liu810b1a32023-11-01 22:55:07 +0800649 4, 0, 3, 18, 7, 6, 1, 2, /*54,*/ 19, 13,
650 32, 31, 23, 24, 22, /*122,*/ 33, 35, 36, 34, 14,
wangyouqiangddbcf2e2023-11-14 13:59:41 +0800651 99,/* 53,*/ 37, 38, 39, 40, 41, 42};
liubin281ac462023-07-19 14:22:54 +0800652
wangyouqiang7850b4a2023-09-18 15:47:57 +0800653 int i, j, ret, total, n = 0;
liubin281ac462023-07-19 14:22:54 +0800654 int *fail_io = (int *)arg;
655 total = (sizeof(test_gpio)/sizeof(int));
wangyouqiang7850b4a2023-09-18 15:47:57 +0800656 //printf("Start test gpio total: %d\n", total);
657
658 /* [1 all gpio set to low] */
659 for(j = 0; j < 6 ; j++){
660 for(i = 0; i < total; i++){
661 gpio_register_set_func_0(test_gpio[i]);
662 gpio_register_set_direction(test_gpio[i], 1);
663 gpio_register_set_value(test_gpio[i], 0);
664 }
wangyouqiang59d8dd22023-10-19 18:03:54 +0800665 //usleep(10000);
666 for(i = 0; i <= 35; i++){
wangyouqiangece1c722023-12-12 17:28:53 +0800667 ret = mbtk_gpio_adc(0);
wangyouqiang59d8dd22023-10-19 18:03:54 +0800668 usleep(3000);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800669 if(0 == ret){
670 break;
671 }
672 }
b.liu074771a2023-10-19 13:13:39 +0800673 printf("pre set ADC: %d, times: %d\n",ret, j);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800674 if(0 == ret){
675 break;
676 }
677 }
wangyouqiang4c262fa2023-10-17 17:48:03 +0800678 if(0 != ret){
679 printf("Fail, please retest");
680 return -1;
681 }
682
wangyouqiang7850b4a2023-09-18 15:47:57 +0800683
684 /* [2 GPIOTEST] */
685 for(i = 0; i < total; i++){
liubin281ac462023-07-19 14:22:54 +0800686 ret = gpio_register_test_out(test_gpio[i], 0);
wangyouqiang7850b4a2023-09-18 15:47:57 +0800687 if(-1 == ret){
688 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio[i]);
liubin281ac462023-07-19 14:22:54 +0800689 fail_io[n] = test_gpio[i];
690 n++;
691 }else{
wangyouqiang4c262fa2023-10-17 17:48:03 +0800692 //printf("############gpio [%d] test success############\n", test_gpio[i]);
liubin281ac462023-07-19 14:22:54 +0800693 }
694 }
wangyouqiang2b352b22023-10-21 15:18:43 +0800695 mbtk_adc_close();
liubin281ac462023-07-19 14:22:54 +0800696 return n;
697}
b.liu4f8e8792023-11-01 23:25:22 +0800698
699#elif defined(MBTK_PROJECT_L508)
LUOJian943fd402024-02-07 10:54:25 +0800700
b.liu85565a72023-11-09 16:47:40 +0800701int test_gpio[] =
702{
703 0,2,1,6,16,15,17,99,
704 34,36,35,33,14,49,50,12,118,120,
wangyouqiang89888612023-11-28 18:28:12 +0800705 43,19,7,3,
b.liu85565a72023-11-09 16:47:40 +0800706 20,59,126,117,125,18,56,55,58,48,57,5,4,21,
LUOJian943fd402024-02-07 10:54:25 +0800707 23,24,13,27,28,26,25,
708 32,31,
709 //54,
710 53,123,122
b.liu85565a72023-11-09 16:47:40 +0800711};
712int total;
713total = (sizeof(test_gpio)/sizeof(int));
714
715
716int gpio_test_init(void)
717{
718 int i,j,ret;
719 //int *fail_io = (int *)arg;
LUOJian943fd402024-02-07 10:54:25 +0800720 printf("[init] L508 XX GPIOTEST v1.2 \n");
wangyouqiang89888612023-11-28 18:28:12 +0800721 system("i2cset -y -f 2 0x31 0x12 0x46");
b.liu85565a72023-11-09 16:47:40 +0800722
LUOJian943fd402024-02-07 10:54:25 +0800723 gpio_register_set_func_0(22);
724 gpio_register_set_direction(22, 1); //输出
725
726 usleep(500); //这里要先设置输出再设置为输入,怀疑L508TLC软件使用了GPIO22
727 gpio_register_set_direction(22, 0); //GPIO22 DTR做输入,检测电压
b.liu85565a72023-11-09 16:47:40 +0800728 #if 0
729 /* 错误点预先关闭 */
730 gpio_register_set_func_0(123);
731 gpio_register_set_func_0(32);
732 gpio_register_set_func_0(53);
733 gpio_register_set_direction(123, 1);
734 gpio_register_set_direction(32, 1);
735 gpio_register_set_direction(53, 1);
736 #endif
737
738 /* [1 all gpio set to low] */
739 for(j = 0; j < 6 ; j++){
740 for(i = 0; i < total; i++){
741 gpio_register_set_func_0(test_gpio[i]);
742 gpio_register_set_direction(test_gpio[i], 1);
743 gpio_register_set_value(test_gpio[i], 0);
744 }
745
746 for(i = 0; i <= 5; i++){
747 ret = gpio_register_get_value(22);
748 if(0 == ret){
749 break;
750 }
751 usleep(3000);
752 }
753 printf("pre set ADC: %d, times: %d\n",ret, j);
754 if(0 == ret){
755 break;
756 }
757 }
758
759 if(0 != ret){
760 printf("set all low Fail, please retest\n");
761 return -1;
762 }
763 printf("pre set SUCCESS\n");
764 return 0;
765}
766
wangyouqiang89888612023-11-28 18:28:12 +0800767int gpio_test(void* arg)
b.liu85565a72023-11-09 16:47:40 +0800768{
769 int ret,i,j;
wangyouqiang89888612023-11-28 18:28:12 +0800770 int *fail_io = (int *)arg;
b.liu85565a72023-11-09 16:47:40 +0800771 int n = 0;
772 for(i = 0; i < total; i++){
773 ret = gpio_register_get_value(test_gpio[i]);
774 printf("GPIO%d default value? %d\n", test_gpio[i], ret);
775 //set high
776 gpio_register_set_value(test_gpio[i], 1);
777 ret = gpio_register_get_value(test_gpio[i]);
778 printf("GPIO%d set high? %d\n", test_gpio[i], ret);
779 //get
780 ret = gpio_register_get_value(22);
781 if(1 != ret){
782 printf("gpio [%d] test high failed\n", test_gpio[i]);
wangyouqiang89888612023-11-28 18:28:12 +0800783 fail_io[n] = test_gpio[i];
b.liu85565a72023-11-09 16:47:40 +0800784 n++;
785 }else{
786 printf("gpio[%d] test high success\n", test_gpio[i]);
787 }
788
789 //set low
790 gpio_register_set_value(test_gpio[i], 0);
791 ret = gpio_register_get_value(test_gpio[i]);
792 printf("GPIO%d set low? %d\n", test_gpio[i], ret);
793 //get
794 ret = gpio_register_get_value(22);
795 if(0 != ret){
796 printf("gpio [%d] test low failed\n", test_gpio[i]);
wangyouqiang89888612023-11-28 18:28:12 +0800797 fail_io[n] = test_gpio[i];
b.liu85565a72023-11-09 16:47:40 +0800798 n++;
799 }else{
800 printf("gpio [%d] test low success\n", test_gpio[i]);
801 }
802 }
803 return n;
804}
805
806
807
b.liu4f8e8792023-11-01 23:25:22 +0800808
809int mbtk_at_gpio(void* arg)
810{
b.liu85565a72023-11-09 16:47:40 +0800811 int i, j, ret, total, n = 0;
wangyouqiang89888612023-11-28 18:28:12 +0800812 //int *fail_io = (int *)arg;
b.liu85565a72023-11-09 16:47:40 +0800813
LUOJian943fd402024-02-07 10:54:25 +0800814 n = gpio_test_init();
815 if(-1 == n)
816 {
817 //return -1;
818 }
wangyouqiang89888612023-11-28 18:28:12 +0800819 n = gpio_test(arg);
b.liu85565a72023-11-09 16:47:40 +0800820
821 return n;
b.liu4f8e8792023-11-01 23:25:22 +0800822}
823
b.liu85565a72023-11-09 16:47:40 +0800824
825
b.liu4f8e8792023-11-01 23:25:22 +0800826#elif defined(MBTK_PROJECT_L509)
gy.tang71ea7ff2024-06-21 16:01:56 +0800827#define DEBUG_GPIO_TEST 0
828mbtk_gpio_paired_info_struct test_pin_array[] ={ //是GPIO_array
829//output_pin, output_gpio,input_gpio,input_pin,test_result
b.liu85565a72023-11-09 16:47:40 +0800830//120 5 33 这几个输入脚没法正常设置为输入
gy.tang71ea7ff2024-06-21 16:01:56 +0800831 //{?,52,51,?,0}, //拉不低 没有这一组
832 //{?,54,53,?,0},
833 //Opin,Ogpio,Igpio,Ipin,res
834 {122, 16, 17, 121, 0}, //output_pin, output_gpio,input_gpio,input_pin,test_result
835 {1, 117, 120, 2, 0},
836 //{?,118,19,13,?,0}, //usim
837 {5, 123, 126, 6, 0},
838 {23, 43, 23, 62, 0},
839 {24, 28, 27, 25, 0},
840 {26, 26, 25, 27, 0},
841 {37, 34, 33, 40, 0},
842 {38, 36, 35, 39, 0},
843 {41, 49, 50, 42, 0},
844 {64, 32, 31, 65, 0},
845 {78, 7, 2, 73, 0},
846 {77, 6, 1, 76, 0},
847 {81, 15, 3, 75, 0},
848 {74, 0, 18, 120,0},
849 {79, 4, 5, 82, 0},
850 {80, 13, 14, 84, 0},
851 {129, 48, 55, 130,0},
852 {131, 56, 57, 132,0},
853 {133, 59, 58, 134,0},
854 {135, 21, 99, 139,0},
855 //{?,20,22,?,0}, //拉不低
856 //{?,12,122,?,0}, //拉不低 122好像不该用
b.liu85565a72023-11-09 16:47:40 +0800857};
858//测低
gy.tang71ea7ff2024-06-21 16:01:56 +0800859uint16 mbtk_gpio_test_all_low(mbtk_gpio_paired_info_struct *pin_array, uint16 MAX_pin_num)
b.liu85565a72023-11-09 16:47:40 +0800860{
861 uint16 index =0;
862 uint16 OUT_pin, Read_pin;
863 uint32 read_high_value;
864 uint32 read_low_value;
865 uint16 fail_num=0;
866 int res;
867
gy.tang71ea7ff2024-06-21 16:01:56 +0800868 printf("[GPIOTEST][run test all pin low]\n");
b.liu85565a72023-11-09 16:47:40 +0800869
870
871 for(index = 0; index < MAX_pin_num; index++){
gy.tang71ea7ff2024-06-21 16:01:56 +0800872 OUT_pin = pin_array[index].output_gpio;
873 Read_pin = pin_array[index].input_gpio;
b.liu85565a72023-11-09 16:47:40 +0800874
875 //输出低
876 gpio_register_set_direction(OUT_pin,1);
877 gpio_register_set_value(OUT_pin, 0);
878
879 //输入脚设为输入
880 gpio_register_set_direction(Read_pin,0);
881 }
882
883 for(index = 0; index < MAX_pin_num; index++){
gy.tang71ea7ff2024-06-21 16:01:56 +0800884 OUT_pin = pin_array[index].output_gpio;
885 Read_pin = pin_array[index].input_gpio;
b.liu85565a72023-11-09 16:47:40 +0800886 read_low_value = gpio_register_get_value(Read_pin);
887 //结果检测
888 if(read_low_value != 0){
gy.tang71ea7ff2024-06-21 16:01:56 +0800889 printf("[GPIOTEST][!Low Failed!]: GPIO%d PIN%d\n", Read_pin,pin_array[index].input_pin);
b.liu85565a72023-11-09 16:47:40 +0800890 pin_array[index].mbtk_gpio_test_result = 1;
891 fail_num ++;
892 }
893 else{
gy.tang71ea7ff2024-06-21 16:01:56 +0800894#if DEBUG_GPIO_TEST
895 printf("[GPIOTEST][-Low Success-]: GPIO%d \n", Read_pin);
896#endif
b.liu85565a72023-11-09 16:47:40 +0800897 }
898 }
899 return fail_num;
900
901}
902
903
904
905//测高
gy.tang71ea7ff2024-06-21 16:01:56 +0800906uint16 mbtk_gpio_test_pin_high(mbtk_gpio_paired_info_struct *pin_array,uint16 MAX_pin_num)
b.liu85565a72023-11-09 16:47:40 +0800907{
908 uint16 index =0;
909 uint16 OUT_pin, Read_pin;
910 uint32 read_high_value;
911 uint32 read_low_value;
912 uint16 fail_num=0;
913 int i = 0;
914
gy.tang71ea7ff2024-06-21 16:01:56 +0800915 printf("[GPIOTEST][run test pin high]\n");
b.liu85565a72023-11-09 16:47:40 +0800916
917 for(index = 0;index<MAX_pin_num;index++)
918 {
gy.tang71ea7ff2024-06-21 16:01:56 +0800919 OUT_pin = pin_array[index].output_gpio;
920 Read_pin = pin_array[index].input_gpio;
b.liu85565a72023-11-09 16:47:40 +0800921
922 //设高
923 gpio_register_set_direction(OUT_pin,1);
924 gpio_register_set_direction(Read_pin,0);
925 gpio_register_set_value(OUT_pin, 1);
926 usleep(20000);
927 read_high_value = gpio_register_get_value(Read_pin);
gy.tang71ea7ff2024-06-21 16:01:56 +0800928#if DEBUG_GPIO_TEST
929 printf("[GPIOTEST][test_high]: PIN%d: get value =%d \n", pin_array[index].input_pin, read_high_value);
930#endif
b.liu85565a72023-11-09 16:47:40 +0800931
932 //设高后重新设低
933 usleep(20000);
934 gpio_register_set_value(OUT_pin, 0);
935 usleep(80000);
936 //read_low_value = gpio_register_get_value(Read_pin);
937 //read_low_value = gpio_register_get_value(Read_pin);
938 for(i = 0;i < 10; i++){
939 read_low_value = gpio_register_get_value(Read_pin);
940 if(0 == read_low_value){
941 break;
942 }
943 usleep(20000);
944 }
gy.tang71ea7ff2024-06-21 16:01:56 +0800945#if DEBUG_GPIO_TEST
946 printf("[GPIOTEST][test_low]: PIN%d: get value =%d \n", pin_array[index].input_pin, read_low_value);
947#endif
b.liu85565a72023-11-09 16:47:40 +0800948 if(read_high_value != 1||read_low_value !=0)
949 {
gy.tang71ea7ff2024-06-21 16:01:56 +0800950 printf("[GPIOTEST][!High Failed!]: Gpio%d PIN%d\n",Read_pin,pin_array[index].input_pin);
b.liu85565a72023-11-09 16:47:40 +0800951 pin_array[index].mbtk_gpio_test_result = 2;
952 fail_num ++;
953 }
954 else{
gy.tang71ea7ff2024-06-21 16:01:56 +0800955#if DEBUG_GPIO_TEST
956 printf("[GPIOTEST][-High Success-]: Gpio%d \n", Read_pin);
957#endif
b.liu85565a72023-11-09 16:47:40 +0800958 }
959 }
960 return fail_num;
961
962}
gy.tang71ea7ff2024-06-21 16:01:56 +0800963void gpio_test_init_test_gpio_mode(mbtk_gpio_paired_info_struct pin_array[],int MAX_pin_num)
b.liu85565a72023-11-09 16:47:40 +0800964{
965 int index =0;
966 int OUT_pin, Read_pin;
967
gy.tang71ea7ff2024-06-21 16:01:56 +0800968 printf("[GPIOTEST] L509 GPIOTEST v2 --init\n");
969
b.liu85565a72023-11-09 16:47:40 +0800970 for(index = 0;index<MAX_pin_num;index++)
971 {
gy.tang71ea7ff2024-06-21 16:01:56 +0800972 OUT_pin = pin_array[index].output_gpio;
973 Read_pin = pin_array[index].input_gpio;
gy.tang5656a592024-06-19 14:04:54 +0800974 pin_array[index].mbtk_gpio_test_result = 0; //init as passed
b.liu85565a72023-11-09 16:47:40 +0800975 gpio_register_set_func_0(OUT_pin);
976 gpio_register_set_func_0(Read_pin);
977 }
978
gy.tang5656a592024-06-19 14:04:54 +0800979 //gpio_register_set_func_0();
980 system("i2cset -y -f 2 0x31 0x12 0x46");
981 system("echo 33 > /sys/class/gpio/export");
982 system("echo 120 > /sys/class/gpio/export");
983 system("echo 126 > /sys/class/gpio/export");
984 system("echo in > /sys/class/gpio/gpio126/direction");
985 system("echo in > /sys/class/gpio/gpio120/direction");
986 system("echo in > /sys/class/gpio/gpio5/direction");
987 system("echo in > /sys/class/gpio/gpio33/direction");
gy.tang71ea7ff2024-06-21 16:01:56 +0800988#if DEBUG_GPIO_TEST
989 printf("[GPIOTEST]gpio_test_init_test_gpio_mode FINISH\n");
990#endif
b.liu85565a72023-11-09 16:47:40 +0800991
992}
993
b.liu4f8e8792023-11-01 23:25:22 +0800994
995int mbtk_at_gpio(void* arg)
996{
gy.tang71ea7ff2024-06-21 16:01:56 +0800997 int i, num;
998 int *fail_io = (int *)arg;
999 /*OPEN SWITCH */
1000 uint16 test_fail_count = 0;
1001 uint16 test_MAX_pin_num = 0;
1002 char buf[STR_MAX_LEN];
1003 uint16 str_len=0;
1004 uint16 temp_len =0;
1005 uint16 Out_pin;
1006 uint16 in_pin;
1007 uint16 fail_print_num = 0;
b.liu85565a72023-11-09 16:47:40 +08001008
gy.tang71ea7ff2024-06-21 16:01:56 +08001009 gpio_register_set_func_0(125);
1010 gpio_register_set_direction(125, 1);
1011 gpio_register_set_value(125, 1); //开关使能脚
1012
b.liu85565a72023-11-09 16:47:40 +08001013
1014 /* [3]初始化待测GPIO */
gy.tang71ea7ff2024-06-21 16:01:56 +08001015 test_MAX_pin_num = sizeof(test_pin_array)/sizeof(test_pin_array[0]);
1016
1017 gpio_test_init_test_gpio_mode(test_pin_array,test_MAX_pin_num);
b.liu85565a72023-11-09 16:47:40 +08001018
1019
1020
1021 /* [4]测试过程 */
gy.tang71ea7ff2024-06-21 16:01:56 +08001022 test_fail_count = mbtk_gpio_test_all_low(test_pin_array,test_MAX_pin_num);
b.liu85565a72023-11-09 16:47:40 +08001023
gy.tang71ea7ff2024-06-21 16:01:56 +08001024 if(!test_fail_count) //set all pin low passed
1025 {
1026 printf("[GPIOTEST]set all pin low: passed\n");
1027 test_fail_count = mbtk_gpio_test_pin_high(&test_pin_array,test_MAX_pin_num);
1028 }
1029 else
1030 {
1031 printf("[GPIOTEST]set all pin low: failed num=%d!!!\n",test_fail_count);
1032 }
b.liu85565a72023-11-09 16:47:40 +08001033
gy.tang71ea7ff2024-06-21 16:01:56 +08001034
1035 //memset(buf,0,STR_MAX_LEN);
1036
b.liu85565a72023-11-09 16:47:40 +08001037 /* [5]测试结果检测 */
gy.tang71ea7ff2024-06-21 16:01:56 +08001038 if(test_fail_count )
1039 {
1040 //printf( "GPIOTEST Fail %02d PINs:\n", test_fail_count*2);
1041 sprintf(buf, "GPIOTEST Fail %02d PINs:", test_fail_count*2);
1042 temp_len = strlen(buf);
b.liu85565a72023-11-09 16:47:40 +08001043
gy.tang71ea7ff2024-06-21 16:01:56 +08001044 num = 0;
1045 for(i = 0; i< test_MAX_pin_num; i++)
1046 {
b.liu85565a72023-11-09 16:47:40 +08001047
gy.tang71ea7ff2024-06-21 16:01:56 +08001048 if(test_pin_array[i].mbtk_gpio_test_result)
1049 {
b.liu85565a72023-11-09 16:47:40 +08001050
gy.tang71ea7ff2024-06-21 16:01:56 +08001051 Out_pin = test_pin_array[i].output_pin;
1052 in_pin = test_pin_array[i].input_pin;
1053 fail_io[num++] = Out_pin;
1054 fail_io[num++] = in_pin;
b.liu85565a72023-11-09 16:47:40 +08001055
gy.tang71ea7ff2024-06-21 16:01:56 +08001056 }
1057 }
1058 }
b.liu85565a72023-11-09 16:47:40 +08001059
gy.tang71ea7ff2024-06-21 16:01:56 +08001060 else
1061 {
b.liu85565a72023-11-09 16:47:40 +08001062 printf(buf,"ALL GPIO TEST PASS\r\n");
gy.tang71ea7ff2024-06-21 16:01:56 +08001063 }
1064 //temp_len = strlen(buf);
wangyouqiang97a792f2024-04-01 13:28:06 +08001065 return test_fail_count * 2;
b.liu4f8e8792023-11-01 23:25:22 +08001066}
1067
wangyouqiangece1c722023-12-12 17:28:53 +08001068#elif defined(MBTK_PROJECT_L508_X6)
wangyouqiangece1c722023-12-12 17:28:53 +08001069int gpio_register_test_out_0(int port, int value)
1070{
1071 int ret;
1072 int i;
1073 int valueh = 0;
1074 int valuel = 1;
1075
1076 printf("Gpio port [%d] test start!\n", port);
1077 ret = gpio_register_set_func_0(port); //设功能为GPIO
1078 if(ret){
1079 printf("gpio_port can't support!\n");
1080 return -1;
1081 }
1082 gpio_register_set_direction(port, 1); //设方向为输出
1083 ret = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001084 //printf("gpio default value is : %d.\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001085
1086
1087 //[High]
1088 for(i = 0; i <= 10; i++){
1089 gpio_register_set_value(port, 1);
1090 usleep(50);
1091 valueh = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001092 //printf("set high? %d\n",valueh);
wangyouqiangece1c722023-12-12 17:28:53 +08001093 if(1 == valueh){
1094 break;
1095 }
1096 }
1097 //usleep(10000);
1098 for(i = 0; i <= 35; i++){
1099 ret = mbtk_gpio_adc(0);
1100 usleep(3000);
1101 if(1 == ret){
1102 break;
1103 }
1104 }
b.liuc7ffd092024-01-03 15:23:07 +08001105 //printf("******gpio should is high: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001106 if(1 != ret){
1107 ret=-1;
1108 goto exit;
1109 }
1110
1111
1112
1113 //[Low]
1114 usleep(200);
1115 for(i = 0; i <= 10; i++){
1116 gpio_register_set_value(port, 0); //输出低
1117 usleep(50);
1118 valuel = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001119 //printf("set low? %d\n",valuel);
wangyouqiangece1c722023-12-12 17:28:53 +08001120 if(0 == valuel){
1121 break;
1122 }
1123 }
1124 //usleep(10000);
1125 for(i = 0; i <= 35; i++){
1126 ret = mbtk_gpio_adc(0);
1127 usleep(3000);
1128 if(0 == ret){
1129 break;
1130 }
1131 }
b.liuc7ffd092024-01-03 15:23:07 +08001132 //printf("******gpio should is low: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001133 if(0 != ret){
1134 ret=-1;
1135 goto exit;
1136 }
1137
1138exit:
1139 gpio_register_set_value(port, 0);
1140 gpio_register_set_direction(port, 0); //设方向为输入
1141 return ret;
1142}
1143
1144int gpio_register_test_out_1(int port, int value)
1145{
1146 int ret;
1147 int i;
1148 int valueh = 0;
1149 int valuel = 1;
1150
1151 printf("Gpio port [%d] test start!\n", port);
1152 ret = gpio_register_set_func_0(port); //设功能为GPIO
1153 if(ret){
1154 printf("gpio_port can't support!\n");
1155 return -1;
1156 }
1157 gpio_register_set_direction(port, 1); //设方向为输出
1158 ret = gpio_register_get_value(port);
1159 printf("gpio default value is : %d.\n", ret);
1160
1161
1162 //[High]
1163 for(i = 0; i <= 10; i++){
1164 gpio_register_set_value(port, 1);
1165 usleep(50);
1166 valueh = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001167 //printf("set high? %d\n",valueh);
wangyouqiangece1c722023-12-12 17:28:53 +08001168 if(1 == valueh){
1169 break;
1170 }
1171 }
1172 //usleep(10000);
1173 for(i = 0; i <= 35; i++){
1174 ret = mbtk_gpio_adc(1);
1175 usleep(3000);
1176 if(1 == ret){
1177 break;
1178 }
1179 }
1180 if(1 != ret){
1181 ret=-1;
1182 goto exit;
1183 }
b.liuc7ffd092024-01-03 15:23:07 +08001184 //printf("******gpio should is high: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001185
1186
1187 //[Low]
1188 usleep(200);
1189 for(i = 0; i <= 10; i++){
1190 gpio_register_set_value(port, 0); //输出低
1191 usleep(50);
1192 valuel = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001193 //printf("set low? %d\n",valuel);
wangyouqiangece1c722023-12-12 17:28:53 +08001194 if(0 == valuel){
1195 break;
1196 }
1197 }
1198 //usleep(10000);
1199 for(i = 0; i <= 35; i++){
1200 ret = mbtk_gpio_adc(1);
1201 usleep(3000);
1202 if(0 == ret){
1203 break;
1204 }
1205 }
b.liuc7ffd092024-01-03 15:23:07 +08001206 //printf("******gpio should is low: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001207 if(0 != ret){
1208 ret=-1;
1209 goto exit;
1210 }
1211
1212exit:
wangyouqiang7775cbf2023-12-29 16:35:24 +08001213 gpio_register_set_value(port, 0);
1214 gpio_register_set_direction(port, 0); //设方向为输入
wangyouqiangece1c722023-12-12 17:28:53 +08001215 return ret;
1216}
1217
wangyouqiang7775cbf2023-12-29 16:35:24 +08001218
1219int gpio_register_test_out_2(int port_in, int port_out, int value)
1220{
1221 int ret_in;
1222 int ret_out;
1223 int i;
1224 int valueh = 0;
1225 int valuel = 1;
1226
1227 printf("Gpio port [%d] test start!\n", port_out);
1228 ret_in = gpio_register_set_func_0(port_in); //设功能为GPIO
1229 ret_out = gpio_register_set_func_0(port_out);
1230 if((ret_in+ret_out)){
1231 printf("gpio_port can't support!\n");
1232 return -1;
1233 }
1234 gpio_register_set_direction(port_out, 1); //设方向为输出
1235 gpio_register_set_direction(port_in, 0); //设方向为输入
1236 ret_in = gpio_register_get_value(port_in);
1237 printf("gpio default value is : %d.\n", ret_in);
1238
1239
1240 //[High]
1241 for(i = 0; i <= 10; i++){
1242 gpio_register_set_value(port_out, 1);
1243 usleep(50);
1244 valueh = gpio_register_get_value(port_out);
1245 printf("set high? %d\n",valueh);
1246 if(1 == valueh){
1247 break;
1248 }
1249 }
1250
1251
1252 for(i = 0; i <= 35; i++){
1253 ret_in = gpio_register_get_value(port_in);
1254 usleep(3000);
1255 if(1 == ret_in){
1256 break;
1257 }
1258 }
1259 if(1 != ret_in){
1260 ret_in=-1;
1261 printf("get high failed! \n");
1262 goto exit;
1263 }
1264 printf("******gpio should is high: %d.******\n", ret_in);
1265
1266
1267 //[Low]
1268 usleep(200);
1269 for(i = 0; i <= 10; i++){
1270 gpio_register_set_value(port_out, 0); //输出低
1271 usleep(50);
1272 valuel = gpio_register_get_value(port_out);
1273 printf("set low? %d\n",valuel);
1274 if(0 == valuel){
1275 break;
1276 }
1277 }
1278
1279 for(i = 0; i <= 35; i++){
1280 ret_in = gpio_register_get_value(port_in);
1281 usleep(3000);
1282 if(0 == ret_in){
1283 break;
1284 }
1285 }
1286 printf("******gpio should is low: %d.******\n", ret_in);
1287 if(0 != ret_in){
1288 ret_in=-1;
1289 printf("get low failed! \n");
1290 goto exit;
1291 }
1292
1293exit:
1294 gpio_register_set_value(port_out, 0);
1295 gpio_register_set_direction(port_out, 0); //设方向为输入
1296 return ret_in;
1297}
1298
1299
wangyouqiangece1c722023-12-12 17:28:53 +08001300void gpio_test_init_test_gpio_mode(void )
1301{
1302
1303
1304 int test_gpio_1[] = {
1305 40, 33, 34, 39, 99,
1306 56, 58, 55, 57, 48, 59, 123, 122, 20, 53,
1307 19, 49, 50, 32 ,31, 6, 11, 10,
1308 88, 87, 86, 85, 84, 89,
wangyouqiang7775cbf2023-12-29 16:35:24 +08001309 90, 46, 127, 117, 119,
wangyouqiangece1c722023-12-12 17:28:53 +08001310 };
1311
1312 int test_gpio_0[] = {
1313 7, 8, 27, 28, 26, 25, 15, 1, 3, 0,
1314 37, 38,35, 36, 42, 41, 21, 22, 24, 23,
1315 54, 125, 18, 13, 14, 17, 16, 5, 4, 2, 12,
1316 44, 45, 47
1317 };
1318 int i, j, ret_0, ret_1, total_1, total_0, n = 0;
1319 total_0 = (sizeof(test_gpio_0)/sizeof(int));
1320 total_1 = (sizeof(test_gpio_1)/sizeof(int));
1321
wangyouqiang7775cbf2023-12-29 16:35:24 +08001322 //system("at at+gpsinit=0");
1323 system("i2cset -y -f 2 0x32 0x0d 0x00");
1324 system("i2cset -y -f 2 0x31 0x18 0x8f");
1325 usleep(10000);
wangyouqiangece1c722023-12-12 17:28:53 +08001326
1327 //printf("[init]gpio_test_init_test_gpio_mode FINISH\n");
1328 /* [1 all gpio set to low] */
1329 for(j = 0; j < 6 ; j++){
1330 for(i = 0; i < total_1; i++){
1331 gpio_register_set_func_0(test_gpio_1[i]);
1332 gpio_register_set_direction(test_gpio_1[i], 1);
1333 gpio_register_set_value(test_gpio_1[i], 0);
1334 }
1335 for(i = 0; i < total_0; i++){
1336 gpio_register_set_func_0(test_gpio_0[i]);
1337 gpio_register_set_direction(test_gpio_0[i], 1);
1338 gpio_register_set_value(test_gpio_0[i], 0);
1339 }
1340 //usleep(10000);
1341
1342 for(i = 0; i <= 35; i++){
1343 ret_0 = mbtk_gpio_adc(0);
1344 usleep(3000);
1345 ret_1 = mbtk_gpio_adc(1);
1346
1347 if(0 == (ret_0 + ret_1)){
1348 break;
1349 }
1350 }
1351 printf("pre set ADC: %d, times: %d\n",(ret_0 + ret_1), j);
1352 if(0 == (ret_0 + ret_1)){
1353 break;
1354 }
1355 }
1356 if(0 != (ret_0 + ret_1)){
wangyouqiang7775cbf2023-12-29 16:35:24 +08001357
wangyouqiangece1c722023-12-12 17:28:53 +08001358 printf("Fail, please retest");
1359 return -1;
1360 }
wangyouqiang7775cbf2023-12-29 16:35:24 +08001361 gpio_register_set_value(119, 1);
1362 gpio_register_set_value(117, 1);
wangyouqiangece1c722023-12-12 17:28:53 +08001363
1364}
1365
1366int mbtk_at_gpio(void* arg)
1367{
wangyouqiang7775cbf2023-12-29 16:35:24 +08001368
1369 printf("Start test gpio V1.0\n");
wangyouqiangece1c722023-12-12 17:28:53 +08001370 int test_gpio_1[] = {
1371 40, 33, 34, 39, 99,
1372 56, 58, 55, 57, 48, 59, 20, 53,
1373 19, 49, 50, 32 ,31, 6, 11, 10,7,
wangyouqiang7775cbf2023-12-29 16:35:24 +08001374 90, 46, 127,
1375 88, 87, 86, 85, 84, 89,
wangyouqiangece1c722023-12-12 17:28:53 +08001376 };
1377
1378 int test_gpio_0[] = {
1379 35, 36, 42, 41, 21, 22, 24, 23,
1380 54, 125, 18, 13, 14, 17, 16, 5, 4, 2, 12,0,
1381 27, 28, 26, 25, 15, 1, 3,
1382 37, 38,8
wangyouqiang7775cbf2023-12-29 16:35:24 +08001383 };
1384 int test_gpio_2[] = { //GPS组
1385 43,47,
1386 //45,44
wangyouqiangece1c722023-12-12 17:28:53 +08001387 };
1388
wangyouqiang7775cbf2023-12-29 16:35:24 +08001389 int i, j, ret_0, ret_1, ret_2, total_1, total_0, total_2, n = 0;
wangyouqiangece1c722023-12-12 17:28:53 +08001390 int *fail_io = (int *)arg;
1391 total_0 = (sizeof(test_gpio_0)/sizeof(int));
1392 total_1 = (sizeof(test_gpio_1)/sizeof(int));
wangyouqiang7775cbf2023-12-29 16:35:24 +08001393 total_2 = (sizeof(test_gpio_2)/sizeof(int));
wangyouqiangece1c722023-12-12 17:28:53 +08001394
1395 /* [1 all gpio Init] */
1396 gpio_test_init_test_gpio_mode();
1397
1398
1399 /* [2 GPIOTEST] */
wangyouqiangece1c722023-12-12 17:28:53 +08001400 //Test 0
1401 for(i = 0; i < total_0; i++){
1402 ret_0 = gpio_register_test_out_0(test_gpio_0[i], 0);
1403 if(-1 == ret_0){
1404 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_0[i]);
1405 fail_io[n] = test_gpio_0[i];
1406 n++;
1407 }else{
b.liuc7ffd092024-01-03 15:23:07 +08001408 //printf("############gpio [%d] test success############\n", test_gpio_0[i]);
wangyouqiangece1c722023-12-12 17:28:53 +08001409 }
1410 }
1411
1412 //Test 1
1413 for(i = 0; i < total_1; i++){
1414 ret_1 = gpio_register_test_out_1(test_gpio_1[i], 0);
1415 if(-1 == ret_1){
1416 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_1[i]);
1417 fail_io[n] = test_gpio_1[i];
1418 n++;
1419 }else{
b.liuc7ffd092024-01-03 15:23:07 +08001420 //printf("############gpio [%d] test success############\n", test_gpio_1[i]);
wangyouqiangece1c722023-12-12 17:28:53 +08001421 }
1422 }
wangyouqiang7775cbf2023-12-29 16:35:24 +08001423
1424 //Test 2
1425 for(i = 0; i < total_2; i=i+2){
1426 ret_2 = gpio_register_test_out_2(test_gpio_2[i], test_gpio_2[i+1], 0);
1427 if(-1 == ret_2){
b.liuc7ffd092024-01-03 15:23:07 +08001428 //printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_2[i+1]);
wangyouqiang7775cbf2023-12-29 16:35:24 +08001429 fail_io[n] = test_gpio_2[i];
1430 n++;
1431 }else{
1432 printf("############gpio [%d] test success############\n", test_gpio_2[i+1]);
1433 }
1434 }
1435
wangyouqiangece1c722023-12-12 17:28:53 +08001436 mbtk_adc_close();
1437 return n;
1438}
1439
wangyouqiang48adc142024-06-24 20:07:30 +08001440#elif 0//(MBTK_PROJECT_T108)
wangyouqiangb1deff32024-03-05 13:31:27 +08001441int gpio_register_test_out(int port, int value)
1442{
1443 int ret;
1444 int i;
1445 int valueh = 0;
1446 int valuel = 1;
1447
1448 //printf("Gpio port [%d] test start!\n", port);
1449 ret = gpio_register_set_func_0(port); //设功能为GPIO
1450 if(ret){
1451 printf("gpio_port can't support!\n");
1452 return -1;
1453 }
1454 gpio_register_set_direction(port, 1); //设方向为输出
1455 //ret = gpio_register_get_value(port);
1456 //printf("gpio default value is: %d\n", ret);
1457
1458
1459 //[High]
1460 for(i = 0; i <= 9; i++){
1461 gpio_register_set_value(port, 1);
1462 usleep(50);
1463 //valueh = gpio_register_get_value(port);
1464 //printf("set high? %d\n", valueh);
1465 if(1 == valueh){
1466 break;
1467 }
1468 }
1469
1470 for(i = 0; i <= 35; i++){
1471 ret = gpio_register_get_value(118);
1472 if(1 == ret){
1473 break;
1474 }
1475 usleep(3000);
1476 }
1477 printf("******gpio should is high: %d.******\n", ret);
1478 if(1 != ret){
1479 ret=-1;
1480 goto exit;
1481 }
1482
1483 //[Low]
1484 usleep(200);
1485 for(i = 0; i <= 9; i++){
1486 gpio_register_set_value(port, 0); //输出低
1487 usleep(50);
1488 //valuel = gpio_register_get_value(port);
1489 //printf("set low? %d\n", valuel);
1490 if(0 == valuel){
1491 break;
1492 }
1493 }
1494
1495 for(i = 0; i <= 35; i++){
1496 ret = gpio_register_get_value(118);
1497 if(0 == ret){
1498 break;
1499 }
1500 usleep(3000);
1501 }
1502 //printf("******gpio should is low: %d.******\n", ret);
1503 if(0 != ret){
1504 ret=-1;
1505 goto exit;
1506 }
1507
1508exit:
1509 gpio_register_set_value(port, 0);
1510 gpio_register_set_direction(port, 0); //设方向为输入
1511 return ret;
1512}
1513
1514int gpio_test_init_test_gpio_mode(void)
1515{
wangyouqiang48adc142024-06-24 20:07:30 +08001516 const int test_gpio[] = {
wangyouqiangb1deff32024-03-05 13:31:27 +08001517 99,
1518 117,
1519 21,22,23,24,44,41,120,
1520 8,127,46,59,58,57,56,55,48,19,34,33,35,36,49,
1521 50,25,28,26,122,20,10,11,
1522 39,40,37,38,
1523 51,52,31,32,
1524 };
1525
1526 int i, j, ret, total, n = 0;
1527 total = (sizeof(test_gpio)/sizeof(int));
1528 printf("[init]gpio_test_init_test_gpio_mode BEGIN\n");
1529
1530 /* [1 all gpio set to low] */
wangyouqiangb1deff32024-03-05 13:31:27 +08001531 gpio_register_set_func_0(118);
1532 gpio_register_set_direction(118, 0);
1533 system("echo in > /sys/class/gpio/gpio118/direction");
1534
1535 for(j = 0; j < 6 ; j++){
1536 for(i = 0; i < total; i++){
1537 gpio_register_set_func_0(test_gpio[i]);
1538 gpio_register_set_direction(test_gpio[i], 1);
1539 gpio_register_set_value(test_gpio[i], 0);
1540 //ret = gpio_register_get_value(test_gpio[i]);
1541 //printf("[init]get gpio%d=%d\n", test_gpio[i], ret);
1542 }
wangyouqiangb1deff32024-03-05 13:31:27 +08001543
1544 for(i = 0; i <= 35; i++){
1545 ret = gpio_register_get_value(118);
1546 printf("[init]get gpi118=%d\n", ret);
1547 usleep(3000);
1548 if(0 == (ret)){
1549 break;
1550 }
1551 }
1552
1553 printf("pre set ADC: %d, times: %d\n",(ret), j);
1554 if(0 == (ret)){
1555 break;
1556 }
1557 }
1558 if(0 != (ret)){
1559 printf("!!!Set all low FAIL, please retest\n");
1560 return -1;
1561 }
1562 return 0;
1563}
1564
1565int mbtk_at_gpio(void* arg)
1566{
wangyouqiangb1deff32024-03-05 13:31:27 +08001567
wangyouqiang48adc142024-06-24 20:07:30 +08001568 const int test_gpio[] = {
1569 //GPIO PIN GPIO PIN GPIO PIN GPIO PIN
1570 99, 170, 117, 59, 21, 61, 22, 62,
1571 23, 144, 24, 147, 44, 5, 41, 159,
1572 120, 143, 8, 171, 127, 160, 46, 149,
1573 59, 19, 58, 18, 57, 20, 56, 21,
1574 55, 22, 48, 23, 19, 3, 34, 79,
1575 33, 80, 35, 78, 36, 77, 49, 43,
1576 50, 42, 25, 67, 28, 66, 26, 65,
1577 122, 169, 20, 152, 10, 74, 11, 73,
1578 39, 166, 40, 164, 37, 165, 38, 163,
1579 51, 58, 52, 60, 31, 57, 32, 56,
wangyouqiangb1deff32024-03-05 13:31:27 +08001580 };
1581
1582 int i, n = 0, ret, total;
1583 int *fail_io = (int *)arg;
wangyouqiang48adc142024-06-24 20:07:30 +08001584 int try_count = 0;
wangyouqiangb1deff32024-03-05 13:31:27 +08001585 total = (sizeof(test_gpio)/sizeof(int));
wangyouqiang48adc142024-06-24 20:07:30 +08001586 printf("T108 Start test gpio V1.0, total gpio=%d\n", (total/2));
wangyouqiangb1deff32024-03-05 13:31:27 +08001587
wangyouqiang48adc142024-06-24 20:07:30 +08001588 for(try_count; try_count < 4; try_count++){
1589 n = 0;
1590 /* [1 all gpio Init] */
1591 gpio_test_init_test_gpio_mode();
wangyouqiangb1deff32024-03-05 13:31:27 +08001592
wangyouqiang48adc142024-06-24 20:07:30 +08001593 /* [2 GPIOTEST] */
1594 for(i = 0; i < total; i = i + 2){
1595 ret = gpio_register_test_out(test_gpio[i], 0);
1596 if(-1 == ret){
yq.wang67c76d52024-06-24 06:11:36 -07001597 n++;
wangyouqiang48adc142024-06-24 20:07:30 +08001598 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio[i]);
1599 if(try_count != 3){
1600 printf(" ---TEST FAILED! RETRY!--- \n");
1601 usleep(5000);
1602 break;
1603 }
yq.wang67c76d52024-06-24 06:11:36 -07001604 fail_io[n - 1] = test_gpio[i+1];
wangyouqiang48adc142024-06-24 20:07:30 +08001605 }else{
1606 //printf("############gpio [%d] test success############\n", test_gpio[i]);
1607 }
1608 }
1609 if(0 == n){
1610 break;
1611 }
wangyouqiangb1deff32024-03-05 13:31:27 +08001612 }
1613 return n;
1614}
1615
1616
wangyouqiang48adc142024-06-24 20:07:30 +08001617#elif (MBTK_PROJECT_T108)
1618static int gpio_test(int port)
1619{
1620 int ret;
1621 int i;
1622 int valueh = 0;
1623 int valuel = 1;
1624
1625 //printf("Gpio port [%d] test start!\n", port);
1626 ret = gpio_register_set_func_0(port);
1627 if(ret){
1628 printf("gpio_port can't support!\n");
1629 return -1;
1630 }
1631 gpio_register_set_direction(port, 1);
1632 //ret = gpio_register_get_value(port);
1633 //printf("gpio default value is: %d\n", ret);
1634
1635
1636 //[High]
1637 for(i = 0; i <= 5; i++){
1638 gpio_register_set_value(port, 1);
1639 usleep(50);
1640 //valueh = gpio_register_get_value(port);
1641 //printf("set high? %d\n", valueh);
1642 if(1 == valueh){
1643 break;
1644 }
1645 }
1646
1647 for(i = 0; i <= 5; i++){
1648 ret = gpio_register_get_value(118);
1649 if(1 == ret){
1650
1651 break;
1652 }
1653 usleep(3000);
1654 }
1655 //printf("count=%d \n", i);
1656 //printf("******gpio should is high: %d.******\n", ret);
1657 if(1 != ret){
1658 ret=-1;
1659 goto exit;
1660 }
1661
1662 //[Low]
1663 usleep(200);
1664 for(i = 0; i <= 9; i++){
1665 gpio_register_set_value(port, 0);
1666 usleep(50);
1667 //valuel = gpio_register_get_value(port);
1668 //printf("set low? %d\n", valuel);
1669 if(0 == valuel){
1670 break;
1671 }
1672 }
1673
1674 for(i = 0; i <= 5; i++){
1675 ret = gpio_register_get_value(118);
1676 if(0 == ret){
1677
1678 break;
1679 }
1680 usleep(3000);
1681 }
1682 //printf("count=%d \n", i);
1683 //printf("******gpio should is low: %d.******\n", ret);
1684 if(0 != ret){
1685 ret=-1;
1686 goto exit;
1687 }
1688
1689exit:
1690 gpio_register_set_value(port, 0);
1691 gpio_register_set_direction(port, 0);
1692 return ret;
1693
1694}
1695
1696static int gpio_test_init_test_gpio_mode(void)
1697{
1698 const int test_gpio[] = {
1699 99,8,117,21,22,23,24,41,120,19,123,
1700 58,59,57,56,55,48,125,127,36,35,34,
1701 33,54,47,53,46,50,49,11,10,26,28,25,
1702 27,32,31,51,52,39,40,37,38,
1703 44,45
1704 };
1705
1706 int i, j, ret, total, n = 0;
1707 total = (sizeof(test_gpio)/sizeof(int));
1708 //printf("[init]gpio_test_init_test_gpio_mode BEGIN\n");
1709
1710 /* [1 all gpio set to low] */
1711 gpio_register_set_func_0(118);
1712 gpio_register_set_direction(118, 0);
1713
1714 for(j = 0; j < 5 ; j++){
1715 for(i = 0; i < total; i++){
1716 gpio_register_set_func_0(test_gpio[i]);
1717 gpio_register_set_direction(test_gpio[i], 1);
1718 gpio_register_set_value(test_gpio[i], 0);
1719 //ret = gpio_register_get_value(test_gpio[i]);
1720 //printf("[init]get gpio%d=%d\n", test_gpio[i], ret);
1721 }
1722
1723
1724 for(i = 0; i <= 10; i++){
1725 ret = gpio_register_get_value(118);
1726 //printf("[init]get gpi118=%d\n", ret);
1727 usleep(3000);
1728 if(0 == (ret)){
1729 break;
1730 }
1731 }
1732
1733 //printf("pre set 118: %d, times: %d\n",(ret), j);
1734 if(0 == (ret)){
1735 break;
1736 }
1737 }
1738 if(0 != (ret)){
1739 printf("!!!Set all low FAIL, please retest\n");
1740 return -1;
1741 }
1742 return 0;
1743}
1744
1745int mbtk_at_gpio(void* arg)
1746{
1747 const int test_gpio[] = {
1748 //GPIO PIN GPIO PIN GPIO PIN GPIO PIN
1749 99, 170, 8, 171, 117, 59, 21, 61,
1750 22, 62, 23, 144, 24, 147, 41, 159,
1751 120, 143, 19, 3, 123, 5, 58, 18,
1752 59, 19, 57, 20, 56, 21, 55, 22,
1753 48, 23, 125, 149, 127, 160, 36, 77,
1754 35, 78, 34, 79, 33, 80, 54, 163,
1755 #if 0 //Hard Ware ERROR!
1756 47, 164,
1757 46, 166,
1758 #endif
1759 53, 165, 50, 42, 49, 43, 11, 73,
1760 10, 74, 26, 65, 28, 66, 25, 67,
1761 27, 68, 32, 56, 31, 57, 51, 58,
1762 52, 60, 39, 192, 40, 193, 37, 194,
1763 38, 195,
1764 #if 0 //undefine GNSS
1765 44, 161,
1766 45, 151,
1767 #endif
1768 };
1769
1770 int i, n = 0, ret, total;
1771 int *fail_io = (int *)arg;
1772 int try_count;
1773 total = (sizeof(test_gpio)/sizeof(int));
1774 printf("T108 V2 Start test gpio V0.8, total gpio=%d\n", (total/2));
1775
1776 for(try_count = 0; try_count < 4; try_count++){
1777 n = 0;
1778 /* [1 all gpio Init] */
1779 gpio_test_init_test_gpio_mode();
1780
1781 /* [2 GPIOTEST] */
1782 for(i = 0; i < total; i = i + 2){
1783 ret = gpio_test(test_gpio[i]);
1784 if(-1 == ret){
yq.wang67c76d52024-06-24 06:11:36 -07001785 n++;
wangyouqiang48adc142024-06-24 20:07:30 +08001786 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio[i]);
1787 if(try_count != 3){
1788 printf(" ---TEST FAILED! RETRY!--- \n");
1789 usleep(5000);
1790 break;
1791 }
yq.wang67c76d52024-06-24 06:11:36 -07001792 fail_io[n - 1] = test_gpio[i + 1];
wangyouqiang48adc142024-06-24 20:07:30 +08001793 }else{
1794 //printf("############gpio [%d] test success############\n", test_gpio[i]);
1795 }
1796 }
1797 if(0 == n){
1798 printf(" ---ALL PASS---\n");
1799 break;
1800 }
1801 }
1802 return n;
1803}
1804
wangyouqiangece1c722023-12-12 17:28:53 +08001805
b.liu4f8e8792023-11-01 23:25:22 +08001806#else
b.liu4f8e8792023-11-01 23:25:22 +08001807int mbtk_at_gpio(void* arg)
1808{
1809 return -1;
1810}
1811
1812#endif