blob: 29e9cd6474e2d8f337b166a81954b90e1a87f79f [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)
b.liu85565a72023-11-09 16:47:40 +0800827mbtk_gpio_test_pin_paired test_pin_array[] ={ //是GPIO_array
828//120 5 33 这几个输入脚没法正常设置为输入
829 //{52,51,0}, //拉不低 没有这一组
b.liu006dabb2023-11-09 17:31:53 +0800830 //{54,53,0},
b.liu85565a72023-11-09 16:47:40 +0800831 {16,17,0},
832 {117,120,0},
b.liu006dabb2023-11-09 17:31:53 +0800833 //{118,19,0}, //usim
b.liu85565a72023-11-09 16:47:40 +0800834 {123,126,0},
835 {43,23,0},
836 {28,27,0},
837 {26,25,0},
838 {34,33,0},
839 {36,35,0},
840 {49,50,0},
841 {32,31,0},
842 {7,2,0},
843 {6,1,0},
844 {15,3,0},
845 {0,18,0},
846 {4,5,0},
847 {13,14,0},
848 {48,55,0},
849 {56,57,0},
850 {59,58,0},
851 {21,99,0},
852 //{20,22,0}, //拉不低
853 //{12,122,0}, //拉不低 122好像不该用
854};
855//测低
856uint16 mbtk_gpio_test_all_low(mbtk_gpio_test_pin_paired *pin_array, uint16 MAX_pin_num)
857{
858 uint16 index =0;
859 uint16 OUT_pin, Read_pin;
860 uint32 read_high_value;
861 uint32 read_low_value;
862 uint16 fail_num=0;
863 int res;
864
865 printf("[test_low_begin]\n");
866
867
868 for(index = 0; index < MAX_pin_num; index++){
869 OUT_pin = pin_array[index].output_pin;
870 Read_pin = pin_array[index].input_pin;
871
872 //输出低
873 gpio_register_set_direction(OUT_pin,1);
874 gpio_register_set_value(OUT_pin, 0);
875
876 //输入脚设为输入
877 gpio_register_set_direction(Read_pin,0);
878 }
879
880 for(index = 0; index < MAX_pin_num; index++){
881 OUT_pin = pin_array[index].output_pin;
882 Read_pin = pin_array[index].input_pin;
883 read_low_value = gpio_register_get_value(Read_pin);
884 //结果检测
885 if(read_low_value != 0){
886 printf("[!Low Failed!]: GPIO%d \n", Read_pin);
887 pin_array[index].mbtk_gpio_test_result = 1;
888 fail_num ++;
889 }
890 else{
891 printf("[-Low Success-]: GPIO%d \n", Read_pin);
892 }
893 }
894 return fail_num;
895
896}
897
898
899
900//测高
901uint16 mbtk_gpio_test_pin_high(mbtk_gpio_test_pin_paired *pin_array,uint16 MAX_pin_num)
902{
903 uint16 index =0;
904 uint16 OUT_pin, Read_pin;
905 uint32 read_high_value;
906 uint32 read_low_value;
907 uint16 fail_num=0;
908 int i = 0;
909
910 printf("[test_high_begin]\n");
911
912 for(index = 0;index<MAX_pin_num;index++)
913 {
914 OUT_pin = pin_array[index].output_pin;
915 Read_pin = pin_array[index].input_pin;
916
917 //设高
918 gpio_register_set_direction(OUT_pin,1);
919 gpio_register_set_direction(Read_pin,0);
920 gpio_register_set_value(OUT_pin, 1);
921 usleep(20000);
922 read_high_value = gpio_register_get_value(Read_pin);
923 printf("[test_high]: Gpio%d: get value =%d \n", Read_pin, read_high_value);
924
925
926 //设高后重新设低
927 usleep(20000);
928 gpio_register_set_value(OUT_pin, 0);
929 usleep(80000);
930 //read_low_value = gpio_register_get_value(Read_pin);
931 //read_low_value = gpio_register_get_value(Read_pin);
932 for(i = 0;i < 10; i++){
933 read_low_value = gpio_register_get_value(Read_pin);
934 if(0 == read_low_value){
935 break;
936 }
937 usleep(20000);
938 }
939
940 printf("[test_low]: Gpio%d: get value =%d \n", Read_pin, read_low_value);
941
942 if(read_high_value != 1||read_low_value !=0)
943 {
944 printf("[!High Failed!]: Gpio%d\n",Read_pin);
945 pin_array[index].mbtk_gpio_test_result = 2;
946 fail_num ++;
947 }
948 else{
949 printf("[-High Success-]: Gpio%d \n", Read_pin);
950 }
951 }
952 return fail_num;
953
954}
955void gpio_test_init_test_gpio_mode(mbtk_gpio_test_pin_paired pin_array[],int MAX_pin_num)
956{
957 int index =0;
958 int OUT_pin, Read_pin;
959
960 printf("[init] L509 GPIOTEST v1 \n");
961 for(index = 0;index<MAX_pin_num;index++)
962 {
963 OUT_pin = pin_array[index].output_pin;
964 Read_pin = pin_array[index].input_pin;
965 gpio_register_set_func_0(OUT_pin);
966 gpio_register_set_func_0(Read_pin);
967 }
968
969 //gpio_register_set_func_0();
b.liu006dabb2023-11-09 17:31:53 +0800970 system("i2cset -y -f 2 0x31 0x12 0x46");
b.liu85565a72023-11-09 16:47:40 +0800971 system("echo 33 > /sys/class/gpio/export");
972 system("echo in > /sys/class/gpio/gpio120/direction");
973 system("echo in > /sys/class/gpio/gpio5/direction");
974 system("echo in > /sys/class/gpio/gpio33/direction");
975 printf("[3init]gpio_test_init_test_gpio_mode FINISH\n");
976
977}
978
b.liu4f8e8792023-11-01 23:25:22 +0800979
980int mbtk_at_gpio(void* arg)
981{
wangyouqiang97a792f2024-04-01 13:28:06 +0800982 int i, num;
b.liu85565a72023-11-09 16:47:40 +0800983 int *fail_io = (int *)arg;
984 /*OPEN SWITCH */
985 uint16 test_fail_count = 0;
986 uint16 test_MAX_pin_num = 0;
987 char buf[STR_MAX_LEN];
988 uint16 str_len=0;
989 uint16 temp_len =0;
990 uint16 Out_pin;
991 uint16 in_pin;
992 uint16 fail_print_num = 0;
993
994 gpio_register_set_func_0(125);
995 gpio_register_set_direction(125, 1);
996 gpio_register_set_value(125, 1); //开关使能脚
997#if 0
998 /* [2]设置高低电压范围 */
999 if(th->threshold_high == 0 && th->threshold_low == 0 &&th->threshold_high_low_diff== 0)
1000 { //use default threshold
1001 th->threshold_high = MBTK_GPIOTEST_THRESHOLD_DEFAULT_HIGH;
1002 th->threshold_low = MBTK_GPIOTEST_THRESHOLD_DEFAULT_LOW;
1003 th->threshold_high_low_diff = MBTK_GPIOTEST_THRESHOLD_DEFAULT_DIFF;
1004 }
1005 RTI_LOG("GPIOTEST: threshold -- %d -- %d -- %d",th->threshold_low,th->threshold_high_low_diff,th->threshold_high);
1006#endif
1007
1008 /* [3]初始化待测GPIO */
1009 test_MAX_pin_num = sizeof(test_pin_array)/sizeof(test_pin_array[0]);
1010 gpio_test_init_test_gpio_mode(test_pin_array,test_MAX_pin_num);
1011
1012
1013
1014 /* [4]测试过程 */
1015 test_fail_count = mbtk_gpio_test_all_low(test_pin_array,test_MAX_pin_num);
1016 printf("[set_low]set ALL GPIO low, fail_num=%d\n",test_fail_count);
1017 if(!test_fail_count) //set all pin low passed
1018 {
1019 printf("[set all pin low passed]\n");
1020 test_fail_count = mbtk_gpio_test_pin_high(&test_pin_array,test_MAX_pin_num);
1021 }
1022
1023 //memset(buf,0,STR_MAX_LEN);
1024
1025 /* [5]测试结果检测 */
1026 if(test_fail_count )
1027 {
1028 //printf( "GPIOTEST Fail %02d PINs:\n", test_fail_count*2);
1029 sprintf(buf, "GPIOTEST Fail %02d PINs:", test_fail_count*2);
1030 temp_len = strlen(buf);
1031
wangyouqiang97a792f2024-04-01 13:28:06 +08001032 num = 0;
b.liu85565a72023-11-09 16:47:40 +08001033 for(i = 0; i< test_MAX_pin_num; i++)
1034 {
1035
1036 if(test_pin_array[i].mbtk_gpio_test_result)
1037 {
1038
1039 Out_pin = test_pin_array[i].output_pin;
1040 in_pin = test_pin_array[i].input_pin;
wangyouqiang97a792f2024-04-01 13:28:06 +08001041 fail_io[num++] = Out_pin;
1042 fail_io[num++] = in_pin;
b.liu85565a72023-11-09 16:47:40 +08001043
1044 }
b.liu85565a72023-11-09 16:47:40 +08001045 }
1046 }
1047
1048 else
1049 {
1050 printf(buf,"ALL GPIO TEST PASS\r\n");
1051 }
1052 //temp_len = strlen(buf);
wangyouqiang97a792f2024-04-01 13:28:06 +08001053 return test_fail_count * 2;
b.liu4f8e8792023-11-01 23:25:22 +08001054}
1055
wangyouqiangece1c722023-12-12 17:28:53 +08001056#elif defined(MBTK_PROJECT_L508_X6)
wangyouqiangece1c722023-12-12 17:28:53 +08001057int gpio_register_test_out_0(int port, int value)
1058{
1059 int ret;
1060 int i;
1061 int valueh = 0;
1062 int valuel = 1;
1063
1064 printf("Gpio port [%d] test start!\n", port);
1065 ret = gpio_register_set_func_0(port); //设功能为GPIO
1066 if(ret){
1067 printf("gpio_port can't support!\n");
1068 return -1;
1069 }
1070 gpio_register_set_direction(port, 1); //设方向为输出
1071 ret = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001072 //printf("gpio default value is : %d.\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001073
1074
1075 //[High]
1076 for(i = 0; i <= 10; i++){
1077 gpio_register_set_value(port, 1);
1078 usleep(50);
1079 valueh = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001080 //printf("set high? %d\n",valueh);
wangyouqiangece1c722023-12-12 17:28:53 +08001081 if(1 == valueh){
1082 break;
1083 }
1084 }
1085 //usleep(10000);
1086 for(i = 0; i <= 35; i++){
1087 ret = mbtk_gpio_adc(0);
1088 usleep(3000);
1089 if(1 == ret){
1090 break;
1091 }
1092 }
b.liuc7ffd092024-01-03 15:23:07 +08001093 //printf("******gpio should is high: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001094 if(1 != ret){
1095 ret=-1;
1096 goto exit;
1097 }
1098
1099
1100
1101 //[Low]
1102 usleep(200);
1103 for(i = 0; i <= 10; i++){
1104 gpio_register_set_value(port, 0); //输出低
1105 usleep(50);
1106 valuel = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001107 //printf("set low? %d\n",valuel);
wangyouqiangece1c722023-12-12 17:28:53 +08001108 if(0 == valuel){
1109 break;
1110 }
1111 }
1112 //usleep(10000);
1113 for(i = 0; i <= 35; i++){
1114 ret = mbtk_gpio_adc(0);
1115 usleep(3000);
1116 if(0 == ret){
1117 break;
1118 }
1119 }
b.liuc7ffd092024-01-03 15:23:07 +08001120 //printf("******gpio should is low: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001121 if(0 != ret){
1122 ret=-1;
1123 goto exit;
1124 }
1125
1126exit:
1127 gpio_register_set_value(port, 0);
1128 gpio_register_set_direction(port, 0); //设方向为输入
1129 return ret;
1130}
1131
1132int gpio_register_test_out_1(int port, int value)
1133{
1134 int ret;
1135 int i;
1136 int valueh = 0;
1137 int valuel = 1;
1138
1139 printf("Gpio port [%d] test start!\n", port);
1140 ret = gpio_register_set_func_0(port); //设功能为GPIO
1141 if(ret){
1142 printf("gpio_port can't support!\n");
1143 return -1;
1144 }
1145 gpio_register_set_direction(port, 1); //设方向为输出
1146 ret = gpio_register_get_value(port);
1147 printf("gpio default value is : %d.\n", ret);
1148
1149
1150 //[High]
1151 for(i = 0; i <= 10; i++){
1152 gpio_register_set_value(port, 1);
1153 usleep(50);
1154 valueh = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001155 //printf("set high? %d\n",valueh);
wangyouqiangece1c722023-12-12 17:28:53 +08001156 if(1 == valueh){
1157 break;
1158 }
1159 }
1160 //usleep(10000);
1161 for(i = 0; i <= 35; i++){
1162 ret = mbtk_gpio_adc(1);
1163 usleep(3000);
1164 if(1 == ret){
1165 break;
1166 }
1167 }
1168 if(1 != ret){
1169 ret=-1;
1170 goto exit;
1171 }
b.liuc7ffd092024-01-03 15:23:07 +08001172 //printf("******gpio should is high: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001173
1174
1175 //[Low]
1176 usleep(200);
1177 for(i = 0; i <= 10; i++){
1178 gpio_register_set_value(port, 0); //输出低
1179 usleep(50);
1180 valuel = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001181 //printf("set low? %d\n",valuel);
wangyouqiangece1c722023-12-12 17:28:53 +08001182 if(0 == valuel){
1183 break;
1184 }
1185 }
1186 //usleep(10000);
1187 for(i = 0; i <= 35; i++){
1188 ret = mbtk_gpio_adc(1);
1189 usleep(3000);
1190 if(0 == ret){
1191 break;
1192 }
1193 }
b.liuc7ffd092024-01-03 15:23:07 +08001194 //printf("******gpio should is low: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001195 if(0 != ret){
1196 ret=-1;
1197 goto exit;
1198 }
1199
1200exit:
wangyouqiang7775cbf2023-12-29 16:35:24 +08001201 gpio_register_set_value(port, 0);
1202 gpio_register_set_direction(port, 0); //设方向为输入
wangyouqiangece1c722023-12-12 17:28:53 +08001203 return ret;
1204}
1205
wangyouqiang7775cbf2023-12-29 16:35:24 +08001206
1207int gpio_register_test_out_2(int port_in, int port_out, int value)
1208{
1209 int ret_in;
1210 int ret_out;
1211 int i;
1212 int valueh = 0;
1213 int valuel = 1;
1214
1215 printf("Gpio port [%d] test start!\n", port_out);
1216 ret_in = gpio_register_set_func_0(port_in); //设功能为GPIO
1217 ret_out = gpio_register_set_func_0(port_out);
1218 if((ret_in+ret_out)){
1219 printf("gpio_port can't support!\n");
1220 return -1;
1221 }
1222 gpio_register_set_direction(port_out, 1); //设方向为输出
1223 gpio_register_set_direction(port_in, 0); //设方向为输入
1224 ret_in = gpio_register_get_value(port_in);
1225 printf("gpio default value is : %d.\n", ret_in);
1226
1227
1228 //[High]
1229 for(i = 0; i <= 10; i++){
1230 gpio_register_set_value(port_out, 1);
1231 usleep(50);
1232 valueh = gpio_register_get_value(port_out);
1233 printf("set high? %d\n",valueh);
1234 if(1 == valueh){
1235 break;
1236 }
1237 }
1238
1239
1240 for(i = 0; i <= 35; i++){
1241 ret_in = gpio_register_get_value(port_in);
1242 usleep(3000);
1243 if(1 == ret_in){
1244 break;
1245 }
1246 }
1247 if(1 != ret_in){
1248 ret_in=-1;
1249 printf("get high failed! \n");
1250 goto exit;
1251 }
1252 printf("******gpio should is high: %d.******\n", ret_in);
1253
1254
1255 //[Low]
1256 usleep(200);
1257 for(i = 0; i <= 10; i++){
1258 gpio_register_set_value(port_out, 0); //输出低
1259 usleep(50);
1260 valuel = gpio_register_get_value(port_out);
1261 printf("set low? %d\n",valuel);
1262 if(0 == valuel){
1263 break;
1264 }
1265 }
1266
1267 for(i = 0; i <= 35; i++){
1268 ret_in = gpio_register_get_value(port_in);
1269 usleep(3000);
1270 if(0 == ret_in){
1271 break;
1272 }
1273 }
1274 printf("******gpio should is low: %d.******\n", ret_in);
1275 if(0 != ret_in){
1276 ret_in=-1;
1277 printf("get low failed! \n");
1278 goto exit;
1279 }
1280
1281exit:
1282 gpio_register_set_value(port_out, 0);
1283 gpio_register_set_direction(port_out, 0); //设方向为输入
1284 return ret_in;
1285}
1286
1287
wangyouqiangece1c722023-12-12 17:28:53 +08001288void gpio_test_init_test_gpio_mode(void )
1289{
1290
1291
1292 int test_gpio_1[] = {
1293 40, 33, 34, 39, 99,
1294 56, 58, 55, 57, 48, 59, 123, 122, 20, 53,
1295 19, 49, 50, 32 ,31, 6, 11, 10,
1296 88, 87, 86, 85, 84, 89,
wangyouqiang7775cbf2023-12-29 16:35:24 +08001297 90, 46, 127, 117, 119,
wangyouqiangece1c722023-12-12 17:28:53 +08001298 };
1299
1300 int test_gpio_0[] = {
1301 7, 8, 27, 28, 26, 25, 15, 1, 3, 0,
1302 37, 38,35, 36, 42, 41, 21, 22, 24, 23,
1303 54, 125, 18, 13, 14, 17, 16, 5, 4, 2, 12,
1304 44, 45, 47
1305 };
1306 int i, j, ret_0, ret_1, total_1, total_0, n = 0;
1307 total_0 = (sizeof(test_gpio_0)/sizeof(int));
1308 total_1 = (sizeof(test_gpio_1)/sizeof(int));
1309
wangyouqiang7775cbf2023-12-29 16:35:24 +08001310 //system("at at+gpsinit=0");
1311 system("i2cset -y -f 2 0x32 0x0d 0x00");
1312 system("i2cset -y -f 2 0x31 0x18 0x8f");
1313 usleep(10000);
wangyouqiangece1c722023-12-12 17:28:53 +08001314
1315 //printf("[init]gpio_test_init_test_gpio_mode FINISH\n");
1316 /* [1 all gpio set to low] */
1317 for(j = 0; j < 6 ; j++){
1318 for(i = 0; i < total_1; i++){
1319 gpio_register_set_func_0(test_gpio_1[i]);
1320 gpio_register_set_direction(test_gpio_1[i], 1);
1321 gpio_register_set_value(test_gpio_1[i], 0);
1322 }
1323 for(i = 0; i < total_0; i++){
1324 gpio_register_set_func_0(test_gpio_0[i]);
1325 gpio_register_set_direction(test_gpio_0[i], 1);
1326 gpio_register_set_value(test_gpio_0[i], 0);
1327 }
1328 //usleep(10000);
1329
1330 for(i = 0; i <= 35; i++){
1331 ret_0 = mbtk_gpio_adc(0);
1332 usleep(3000);
1333 ret_1 = mbtk_gpio_adc(1);
1334
1335 if(0 == (ret_0 + ret_1)){
1336 break;
1337 }
1338 }
1339 printf("pre set ADC: %d, times: %d\n",(ret_0 + ret_1), j);
1340 if(0 == (ret_0 + ret_1)){
1341 break;
1342 }
1343 }
1344 if(0 != (ret_0 + ret_1)){
wangyouqiang7775cbf2023-12-29 16:35:24 +08001345
wangyouqiangece1c722023-12-12 17:28:53 +08001346 printf("Fail, please retest");
1347 return -1;
1348 }
wangyouqiang7775cbf2023-12-29 16:35:24 +08001349 gpio_register_set_value(119, 1);
1350 gpio_register_set_value(117, 1);
wangyouqiangece1c722023-12-12 17:28:53 +08001351
1352}
1353
1354int mbtk_at_gpio(void* arg)
1355{
wangyouqiang7775cbf2023-12-29 16:35:24 +08001356
1357 printf("Start test gpio V1.0\n");
wangyouqiangece1c722023-12-12 17:28:53 +08001358 int test_gpio_1[] = {
1359 40, 33, 34, 39, 99,
1360 56, 58, 55, 57, 48, 59, 20, 53,
1361 19, 49, 50, 32 ,31, 6, 11, 10,7,
wangyouqiang7775cbf2023-12-29 16:35:24 +08001362 90, 46, 127,
1363 88, 87, 86, 85, 84, 89,
wangyouqiangece1c722023-12-12 17:28:53 +08001364 };
1365
1366 int test_gpio_0[] = {
1367 35, 36, 42, 41, 21, 22, 24, 23,
1368 54, 125, 18, 13, 14, 17, 16, 5, 4, 2, 12,0,
1369 27, 28, 26, 25, 15, 1, 3,
1370 37, 38,8
wangyouqiang7775cbf2023-12-29 16:35:24 +08001371 };
1372 int test_gpio_2[] = { //GPS组
1373 43,47,
1374 //45,44
wangyouqiangece1c722023-12-12 17:28:53 +08001375 };
1376
wangyouqiang7775cbf2023-12-29 16:35:24 +08001377 int i, j, ret_0, ret_1, ret_2, total_1, total_0, total_2, n = 0;
wangyouqiangece1c722023-12-12 17:28:53 +08001378 int *fail_io = (int *)arg;
1379 total_0 = (sizeof(test_gpio_0)/sizeof(int));
1380 total_1 = (sizeof(test_gpio_1)/sizeof(int));
wangyouqiang7775cbf2023-12-29 16:35:24 +08001381 total_2 = (sizeof(test_gpio_2)/sizeof(int));
wangyouqiangece1c722023-12-12 17:28:53 +08001382
1383 /* [1 all gpio Init] */
1384 gpio_test_init_test_gpio_mode();
1385
1386
1387 /* [2 GPIOTEST] */
wangyouqiangece1c722023-12-12 17:28:53 +08001388 //Test 0
1389 for(i = 0; i < total_0; i++){
1390 ret_0 = gpio_register_test_out_0(test_gpio_0[i], 0);
1391 if(-1 == ret_0){
1392 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_0[i]);
1393 fail_io[n] = test_gpio_0[i];
1394 n++;
1395 }else{
b.liuc7ffd092024-01-03 15:23:07 +08001396 //printf("############gpio [%d] test success############\n", test_gpio_0[i]);
wangyouqiangece1c722023-12-12 17:28:53 +08001397 }
1398 }
1399
1400 //Test 1
1401 for(i = 0; i < total_1; i++){
1402 ret_1 = gpio_register_test_out_1(test_gpio_1[i], 0);
1403 if(-1 == ret_1){
1404 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_1[i]);
1405 fail_io[n] = test_gpio_1[i];
1406 n++;
1407 }else{
b.liuc7ffd092024-01-03 15:23:07 +08001408 //printf("############gpio [%d] test success############\n", test_gpio_1[i]);
wangyouqiangece1c722023-12-12 17:28:53 +08001409 }
1410 }
wangyouqiang7775cbf2023-12-29 16:35:24 +08001411
1412 //Test 2
1413 for(i = 0; i < total_2; i=i+2){
1414 ret_2 = gpio_register_test_out_2(test_gpio_2[i], test_gpio_2[i+1], 0);
1415 if(-1 == ret_2){
b.liuc7ffd092024-01-03 15:23:07 +08001416 //printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_2[i+1]);
wangyouqiang7775cbf2023-12-29 16:35:24 +08001417 fail_io[n] = test_gpio_2[i];
1418 n++;
1419 }else{
1420 printf("############gpio [%d] test success############\n", test_gpio_2[i+1]);
1421 }
1422 }
1423
wangyouqiangece1c722023-12-12 17:28:53 +08001424 mbtk_adc_close();
1425 return n;
1426}
1427
wangyouqiangb1deff32024-03-05 13:31:27 +08001428#elif defined(MBTK_PROJECT_T108)
1429int gpio_register_test_out(int port, int value)
1430{
1431 int ret;
1432 int i;
1433 int valueh = 0;
1434 int valuel = 1;
1435
1436 //printf("Gpio port [%d] test start!\n", port);
1437 ret = gpio_register_set_func_0(port); //设功能为GPIO
1438 if(ret){
1439 printf("gpio_port can't support!\n");
1440 return -1;
1441 }
1442 gpio_register_set_direction(port, 1); //设方向为输出
1443 //ret = gpio_register_get_value(port);
1444 //printf("gpio default value is: %d\n", ret);
1445
1446
1447 //[High]
1448 for(i = 0; i <= 9; i++){
1449 gpio_register_set_value(port, 1);
1450 usleep(50);
1451 //valueh = gpio_register_get_value(port);
1452 //printf("set high? %d\n", valueh);
1453 if(1 == valueh){
1454 break;
1455 }
1456 }
1457
1458 for(i = 0; i <= 35; i++){
1459 ret = gpio_register_get_value(118);
1460 if(1 == ret){
1461 break;
1462 }
1463 usleep(3000);
1464 }
1465 printf("******gpio should is high: %d.******\n", ret);
1466 if(1 != ret){
1467 ret=-1;
1468 goto exit;
1469 }
1470
1471 //[Low]
1472 usleep(200);
1473 for(i = 0; i <= 9; i++){
1474 gpio_register_set_value(port, 0); //输出低
1475 usleep(50);
1476 //valuel = gpio_register_get_value(port);
1477 //printf("set low? %d\n", valuel);
1478 if(0 == valuel){
1479 break;
1480 }
1481 }
1482
1483 for(i = 0; i <= 35; i++){
1484 ret = gpio_register_get_value(118);
1485 if(0 == ret){
1486 break;
1487 }
1488 usleep(3000);
1489 }
1490 //printf("******gpio should is low: %d.******\n", ret);
1491 if(0 != ret){
1492 ret=-1;
1493 goto exit;
1494 }
1495
1496exit:
1497 gpio_register_set_value(port, 0);
1498 gpio_register_set_direction(port, 0); //设方向为输入
1499 return ret;
1500}
1501
1502int gpio_test_init_test_gpio_mode(void)
1503{
1504 int test_gpio[] = {
1505 99,
1506 117,
1507 21,22,23,24,44,41,120,
1508 8,127,46,59,58,57,56,55,48,19,34,33,35,36,49,
1509 50,25,28,26,122,20,10,11,
1510 39,40,37,38,
1511 51,52,31,32,
1512 };
1513
1514 int i, j, ret, total, n = 0;
1515 total = (sizeof(test_gpio)/sizeof(int));
1516 printf("[init]gpio_test_init_test_gpio_mode BEGIN\n");
1517
1518 /* [1 all gpio set to low] */
1519 //system("echo 118 > /sys/class/gpio/unexport");
1520 //system("echo 118 > /sys/class/gpio/export");
1521 gpio_register_set_func_0(118);
1522 gpio_register_set_direction(118, 0);
1523 system("echo in > /sys/class/gpio/gpio118/direction");
1524
1525 for(j = 0; j < 6 ; j++){
1526 for(i = 0; i < total; i++){
1527 gpio_register_set_func_0(test_gpio[i]);
1528 gpio_register_set_direction(test_gpio[i], 1);
1529 gpio_register_set_value(test_gpio[i], 0);
1530 //ret = gpio_register_get_value(test_gpio[i]);
1531 //printf("[init]get gpio%d=%d\n", test_gpio[i], ret);
1532 }
1533 //system("hwacc w 0xd401e1ac 0x1040"); //52
1534
1535 for(i = 0; i <= 35; i++){
1536 ret = gpio_register_get_value(118);
1537 printf("[init]get gpi118=%d\n", ret);
1538 usleep(3000);
1539 if(0 == (ret)){
1540 break;
1541 }
1542 }
1543
1544 printf("pre set ADC: %d, times: %d\n",(ret), j);
1545 if(0 == (ret)){
1546 break;
1547 }
1548 }
1549 if(0 != (ret)){
1550 printf("!!!Set all low FAIL, please retest\n");
1551 return -1;
1552 }
1553 return 0;
1554}
1555
1556int mbtk_at_gpio(void* arg)
1557{
1558
1559
1560 int test_gpio[] = {
1561 99,
1562 117,
1563 21,22,23,24,44,41,120,
1564 8,127,46,59,58,57,56,55,48,19,34,33,35,36,49,
1565 50,25,28,26,122,20,10,11,
1566 39,40,37,38,
1567 51,52,31,32,
1568
1569 };
1570
1571 int i, n = 0, ret, total;
1572 int *fail_io = (int *)arg;
1573 total = (sizeof(test_gpio)/sizeof(int));
1574 printf("T108 Start test gpio V1.0, total gpio=%d\n", total);
1575
1576 /* [1 all gpio Init] */
1577 gpio_test_init_test_gpio_mode();
1578
1579 /* [2 GPIOTEST] */
1580 for(i = 0; i < total; i++){
1581 ret = gpio_register_test_out(test_gpio[i], 0);
1582 if(-1 == ret){
1583 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio[i]);
1584 fail_io[n] = test_gpio[i];
1585 n++;
1586 }else{
1587 //printf("############gpio [%d] test success############\n", test_gpio[i]);
1588 }
1589 }
1590 return n;
1591}
1592
1593
wangyouqiangece1c722023-12-12 17:28:53 +08001594
b.liu4f8e8792023-11-01 23:25:22 +08001595#else
b.liu4f8e8792023-11-01 23:25:22 +08001596int mbtk_at_gpio(void* arg)
1597{
1598 return -1;
1599}
1600
1601#endif