blob: e4baaef94cf521dec9daa551d1b25bdbfc9c3f86 [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;
gy.tang5656a592024-06-19 14:04:54 +0800965 pin_array[index].mbtk_gpio_test_result = 0; //init as passed
b.liu85565a72023-11-09 16:47:40 +0800966 gpio_register_set_func_0(OUT_pin);
967 gpio_register_set_func_0(Read_pin);
968 }
969
gy.tang5656a592024-06-19 14:04:54 +0800970 //gpio_register_set_func_0();
971 system("i2cset -y -f 2 0x31 0x12 0x46");
972 system("echo 33 > /sys/class/gpio/export");
973 system("echo 120 > /sys/class/gpio/export");
974 system("echo 126 > /sys/class/gpio/export");
975 system("echo in > /sys/class/gpio/gpio126/direction");
976 system("echo in > /sys/class/gpio/gpio120/direction");
977 system("echo in > /sys/class/gpio/gpio5/direction");
978 system("echo in > /sys/class/gpio/gpio33/direction");
979 printf("[3init]gpio_test_init_test_gpio_mode FINISH\n");
b.liu85565a72023-11-09 16:47:40 +0800980
981}
982
b.liu4f8e8792023-11-01 23:25:22 +0800983
984int mbtk_at_gpio(void* arg)
985{
wangyouqiang97a792f2024-04-01 13:28:06 +0800986 int i, num;
b.liu85565a72023-11-09 16:47:40 +0800987 int *fail_io = (int *)arg;
988 /*OPEN SWITCH */
989 uint16 test_fail_count = 0;
990 uint16 test_MAX_pin_num = 0;
991 char buf[STR_MAX_LEN];
992 uint16 str_len=0;
993 uint16 temp_len =0;
994 uint16 Out_pin;
995 uint16 in_pin;
996 uint16 fail_print_num = 0;
997
998 gpio_register_set_func_0(125);
999 gpio_register_set_direction(125, 1);
1000 gpio_register_set_value(125, 1); //开关使能脚
1001#if 0
1002 /* [2]设置高低电压范围 */
1003 if(th->threshold_high == 0 && th->threshold_low == 0 &&th->threshold_high_low_diff== 0)
1004 { //use default threshold
1005 th->threshold_high = MBTK_GPIOTEST_THRESHOLD_DEFAULT_HIGH;
1006 th->threshold_low = MBTK_GPIOTEST_THRESHOLD_DEFAULT_LOW;
1007 th->threshold_high_low_diff = MBTK_GPIOTEST_THRESHOLD_DEFAULT_DIFF;
1008 }
1009 RTI_LOG("GPIOTEST: threshold -- %d -- %d -- %d",th->threshold_low,th->threshold_high_low_diff,th->threshold_high);
1010#endif
1011
1012 /* [3]初始化待测GPIO */
1013 test_MAX_pin_num = sizeof(test_pin_array)/sizeof(test_pin_array[0]);
1014 gpio_test_init_test_gpio_mode(test_pin_array,test_MAX_pin_num);
1015
1016
1017
1018 /* [4]测试过程 */
1019 test_fail_count = mbtk_gpio_test_all_low(test_pin_array,test_MAX_pin_num);
1020 printf("[set_low]set ALL GPIO low, fail_num=%d\n",test_fail_count);
1021 if(!test_fail_count) //set all pin low passed
1022 {
1023 printf("[set all pin low passed]\n");
1024 test_fail_count = mbtk_gpio_test_pin_high(&test_pin_array,test_MAX_pin_num);
1025 }
1026
1027 //memset(buf,0,STR_MAX_LEN);
1028
1029 /* [5]测试结果检测 */
1030 if(test_fail_count )
1031 {
1032 //printf( "GPIOTEST Fail %02d PINs:\n", test_fail_count*2);
1033 sprintf(buf, "GPIOTEST Fail %02d PINs:", test_fail_count*2);
1034 temp_len = strlen(buf);
1035
wangyouqiang97a792f2024-04-01 13:28:06 +08001036 num = 0;
b.liu85565a72023-11-09 16:47:40 +08001037 for(i = 0; i< test_MAX_pin_num; i++)
1038 {
1039
1040 if(test_pin_array[i].mbtk_gpio_test_result)
1041 {
1042
1043 Out_pin = test_pin_array[i].output_pin;
1044 in_pin = test_pin_array[i].input_pin;
wangyouqiang97a792f2024-04-01 13:28:06 +08001045 fail_io[num++] = Out_pin;
1046 fail_io[num++] = in_pin;
b.liu85565a72023-11-09 16:47:40 +08001047
1048 }
b.liu85565a72023-11-09 16:47:40 +08001049 }
1050 }
1051
1052 else
1053 {
1054 printf(buf,"ALL GPIO TEST PASS\r\n");
1055 }
1056 //temp_len = strlen(buf);
wangyouqiang97a792f2024-04-01 13:28:06 +08001057 return test_fail_count * 2;
b.liu4f8e8792023-11-01 23:25:22 +08001058}
1059
wangyouqiangece1c722023-12-12 17:28:53 +08001060#elif defined(MBTK_PROJECT_L508_X6)
wangyouqiangece1c722023-12-12 17:28:53 +08001061int gpio_register_test_out_0(int port, int value)
1062{
1063 int ret;
1064 int i;
1065 int valueh = 0;
1066 int valuel = 1;
1067
1068 printf("Gpio port [%d] test start!\n", port);
1069 ret = gpio_register_set_func_0(port); //设功能为GPIO
1070 if(ret){
1071 printf("gpio_port can't support!\n");
1072 return -1;
1073 }
1074 gpio_register_set_direction(port, 1); //设方向为输出
1075 ret = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001076 //printf("gpio default value is : %d.\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001077
1078
1079 //[High]
1080 for(i = 0; i <= 10; i++){
1081 gpio_register_set_value(port, 1);
1082 usleep(50);
1083 valueh = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001084 //printf("set high? %d\n",valueh);
wangyouqiangece1c722023-12-12 17:28:53 +08001085 if(1 == valueh){
1086 break;
1087 }
1088 }
1089 //usleep(10000);
1090 for(i = 0; i <= 35; i++){
1091 ret = mbtk_gpio_adc(0);
1092 usleep(3000);
1093 if(1 == ret){
1094 break;
1095 }
1096 }
b.liuc7ffd092024-01-03 15:23:07 +08001097 //printf("******gpio should is high: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001098 if(1 != ret){
1099 ret=-1;
1100 goto exit;
1101 }
1102
1103
1104
1105 //[Low]
1106 usleep(200);
1107 for(i = 0; i <= 10; i++){
1108 gpio_register_set_value(port, 0); //输出低
1109 usleep(50);
1110 valuel = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001111 //printf("set low? %d\n",valuel);
wangyouqiangece1c722023-12-12 17:28:53 +08001112 if(0 == valuel){
1113 break;
1114 }
1115 }
1116 //usleep(10000);
1117 for(i = 0; i <= 35; i++){
1118 ret = mbtk_gpio_adc(0);
1119 usleep(3000);
1120 if(0 == ret){
1121 break;
1122 }
1123 }
b.liuc7ffd092024-01-03 15:23:07 +08001124 //printf("******gpio should is low: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001125 if(0 != ret){
1126 ret=-1;
1127 goto exit;
1128 }
1129
1130exit:
1131 gpio_register_set_value(port, 0);
1132 gpio_register_set_direction(port, 0); //设方向为输入
1133 return ret;
1134}
1135
1136int gpio_register_test_out_1(int port, int value)
1137{
1138 int ret;
1139 int i;
1140 int valueh = 0;
1141 int valuel = 1;
1142
1143 printf("Gpio port [%d] test start!\n", port);
1144 ret = gpio_register_set_func_0(port); //设功能为GPIO
1145 if(ret){
1146 printf("gpio_port can't support!\n");
1147 return -1;
1148 }
1149 gpio_register_set_direction(port, 1); //设方向为输出
1150 ret = gpio_register_get_value(port);
1151 printf("gpio default value is : %d.\n", ret);
1152
1153
1154 //[High]
1155 for(i = 0; i <= 10; i++){
1156 gpio_register_set_value(port, 1);
1157 usleep(50);
1158 valueh = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001159 //printf("set high? %d\n",valueh);
wangyouqiangece1c722023-12-12 17:28:53 +08001160 if(1 == valueh){
1161 break;
1162 }
1163 }
1164 //usleep(10000);
1165 for(i = 0; i <= 35; i++){
1166 ret = mbtk_gpio_adc(1);
1167 usleep(3000);
1168 if(1 == ret){
1169 break;
1170 }
1171 }
1172 if(1 != ret){
1173 ret=-1;
1174 goto exit;
1175 }
b.liuc7ffd092024-01-03 15:23:07 +08001176 //printf("******gpio should is high: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001177
1178
1179 //[Low]
1180 usleep(200);
1181 for(i = 0; i <= 10; i++){
1182 gpio_register_set_value(port, 0); //输出低
1183 usleep(50);
1184 valuel = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001185 //printf("set low? %d\n",valuel);
wangyouqiangece1c722023-12-12 17:28:53 +08001186 if(0 == valuel){
1187 break;
1188 }
1189 }
1190 //usleep(10000);
1191 for(i = 0; i <= 35; i++){
1192 ret = mbtk_gpio_adc(1);
1193 usleep(3000);
1194 if(0 == ret){
1195 break;
1196 }
1197 }
b.liuc7ffd092024-01-03 15:23:07 +08001198 //printf("******gpio should is low: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001199 if(0 != ret){
1200 ret=-1;
1201 goto exit;
1202 }
1203
1204exit:
wangyouqiang7775cbf2023-12-29 16:35:24 +08001205 gpio_register_set_value(port, 0);
1206 gpio_register_set_direction(port, 0); //设方向为输入
wangyouqiangece1c722023-12-12 17:28:53 +08001207 return ret;
1208}
1209
wangyouqiang7775cbf2023-12-29 16:35:24 +08001210
1211int gpio_register_test_out_2(int port_in, int port_out, int value)
1212{
1213 int ret_in;
1214 int ret_out;
1215 int i;
1216 int valueh = 0;
1217 int valuel = 1;
1218
1219 printf("Gpio port [%d] test start!\n", port_out);
1220 ret_in = gpio_register_set_func_0(port_in); //设功能为GPIO
1221 ret_out = gpio_register_set_func_0(port_out);
1222 if((ret_in+ret_out)){
1223 printf("gpio_port can't support!\n");
1224 return -1;
1225 }
1226 gpio_register_set_direction(port_out, 1); //设方向为输出
1227 gpio_register_set_direction(port_in, 0); //设方向为输入
1228 ret_in = gpio_register_get_value(port_in);
1229 printf("gpio default value is : %d.\n", ret_in);
1230
1231
1232 //[High]
1233 for(i = 0; i <= 10; i++){
1234 gpio_register_set_value(port_out, 1);
1235 usleep(50);
1236 valueh = gpio_register_get_value(port_out);
1237 printf("set high? %d\n",valueh);
1238 if(1 == valueh){
1239 break;
1240 }
1241 }
1242
1243
1244 for(i = 0; i <= 35; i++){
1245 ret_in = gpio_register_get_value(port_in);
1246 usleep(3000);
1247 if(1 == ret_in){
1248 break;
1249 }
1250 }
1251 if(1 != ret_in){
1252 ret_in=-1;
1253 printf("get high failed! \n");
1254 goto exit;
1255 }
1256 printf("******gpio should is high: %d.******\n", ret_in);
1257
1258
1259 //[Low]
1260 usleep(200);
1261 for(i = 0; i <= 10; i++){
1262 gpio_register_set_value(port_out, 0); //输出低
1263 usleep(50);
1264 valuel = gpio_register_get_value(port_out);
1265 printf("set low? %d\n",valuel);
1266 if(0 == valuel){
1267 break;
1268 }
1269 }
1270
1271 for(i = 0; i <= 35; i++){
1272 ret_in = gpio_register_get_value(port_in);
1273 usleep(3000);
1274 if(0 == ret_in){
1275 break;
1276 }
1277 }
1278 printf("******gpio should is low: %d.******\n", ret_in);
1279 if(0 != ret_in){
1280 ret_in=-1;
1281 printf("get low failed! \n");
1282 goto exit;
1283 }
1284
1285exit:
1286 gpio_register_set_value(port_out, 0);
1287 gpio_register_set_direction(port_out, 0); //设方向为输入
1288 return ret_in;
1289}
1290
1291
wangyouqiangece1c722023-12-12 17:28:53 +08001292void gpio_test_init_test_gpio_mode(void )
1293{
1294
1295
1296 int test_gpio_1[] = {
1297 40, 33, 34, 39, 99,
1298 56, 58, 55, 57, 48, 59, 123, 122, 20, 53,
1299 19, 49, 50, 32 ,31, 6, 11, 10,
1300 88, 87, 86, 85, 84, 89,
wangyouqiang7775cbf2023-12-29 16:35:24 +08001301 90, 46, 127, 117, 119,
wangyouqiangece1c722023-12-12 17:28:53 +08001302 };
1303
1304 int test_gpio_0[] = {
1305 7, 8, 27, 28, 26, 25, 15, 1, 3, 0,
1306 37, 38,35, 36, 42, 41, 21, 22, 24, 23,
1307 54, 125, 18, 13, 14, 17, 16, 5, 4, 2, 12,
1308 44, 45, 47
1309 };
1310 int i, j, ret_0, ret_1, total_1, total_0, n = 0;
1311 total_0 = (sizeof(test_gpio_0)/sizeof(int));
1312 total_1 = (sizeof(test_gpio_1)/sizeof(int));
1313
wangyouqiang7775cbf2023-12-29 16:35:24 +08001314 //system("at at+gpsinit=0");
1315 system("i2cset -y -f 2 0x32 0x0d 0x00");
1316 system("i2cset -y -f 2 0x31 0x18 0x8f");
1317 usleep(10000);
wangyouqiangece1c722023-12-12 17:28:53 +08001318
1319 //printf("[init]gpio_test_init_test_gpio_mode FINISH\n");
1320 /* [1 all gpio set to low] */
1321 for(j = 0; j < 6 ; j++){
1322 for(i = 0; i < total_1; i++){
1323 gpio_register_set_func_0(test_gpio_1[i]);
1324 gpio_register_set_direction(test_gpio_1[i], 1);
1325 gpio_register_set_value(test_gpio_1[i], 0);
1326 }
1327 for(i = 0; i < total_0; i++){
1328 gpio_register_set_func_0(test_gpio_0[i]);
1329 gpio_register_set_direction(test_gpio_0[i], 1);
1330 gpio_register_set_value(test_gpio_0[i], 0);
1331 }
1332 //usleep(10000);
1333
1334 for(i = 0; i <= 35; i++){
1335 ret_0 = mbtk_gpio_adc(0);
1336 usleep(3000);
1337 ret_1 = mbtk_gpio_adc(1);
1338
1339 if(0 == (ret_0 + ret_1)){
1340 break;
1341 }
1342 }
1343 printf("pre set ADC: %d, times: %d\n",(ret_0 + ret_1), j);
1344 if(0 == (ret_0 + ret_1)){
1345 break;
1346 }
1347 }
1348 if(0 != (ret_0 + ret_1)){
wangyouqiang7775cbf2023-12-29 16:35:24 +08001349
wangyouqiangece1c722023-12-12 17:28:53 +08001350 printf("Fail, please retest");
1351 return -1;
1352 }
wangyouqiang7775cbf2023-12-29 16:35:24 +08001353 gpio_register_set_value(119, 1);
1354 gpio_register_set_value(117, 1);
wangyouqiangece1c722023-12-12 17:28:53 +08001355
1356}
1357
1358int mbtk_at_gpio(void* arg)
1359{
wangyouqiang7775cbf2023-12-29 16:35:24 +08001360
1361 printf("Start test gpio V1.0\n");
wangyouqiangece1c722023-12-12 17:28:53 +08001362 int test_gpio_1[] = {
1363 40, 33, 34, 39, 99,
1364 56, 58, 55, 57, 48, 59, 20, 53,
1365 19, 49, 50, 32 ,31, 6, 11, 10,7,
wangyouqiang7775cbf2023-12-29 16:35:24 +08001366 90, 46, 127,
1367 88, 87, 86, 85, 84, 89,
wangyouqiangece1c722023-12-12 17:28:53 +08001368 };
1369
1370 int test_gpio_0[] = {
1371 35, 36, 42, 41, 21, 22, 24, 23,
1372 54, 125, 18, 13, 14, 17, 16, 5, 4, 2, 12,0,
1373 27, 28, 26, 25, 15, 1, 3,
1374 37, 38,8
wangyouqiang7775cbf2023-12-29 16:35:24 +08001375 };
1376 int test_gpio_2[] = { //GPS组
1377 43,47,
1378 //45,44
wangyouqiangece1c722023-12-12 17:28:53 +08001379 };
1380
wangyouqiang7775cbf2023-12-29 16:35:24 +08001381 int i, j, ret_0, ret_1, ret_2, total_1, total_0, total_2, n = 0;
wangyouqiangece1c722023-12-12 17:28:53 +08001382 int *fail_io = (int *)arg;
1383 total_0 = (sizeof(test_gpio_0)/sizeof(int));
1384 total_1 = (sizeof(test_gpio_1)/sizeof(int));
wangyouqiang7775cbf2023-12-29 16:35:24 +08001385 total_2 = (sizeof(test_gpio_2)/sizeof(int));
wangyouqiangece1c722023-12-12 17:28:53 +08001386
1387 /* [1 all gpio Init] */
1388 gpio_test_init_test_gpio_mode();
1389
1390
1391 /* [2 GPIOTEST] */
wangyouqiangece1c722023-12-12 17:28:53 +08001392 //Test 0
1393 for(i = 0; i < total_0; i++){
1394 ret_0 = gpio_register_test_out_0(test_gpio_0[i], 0);
1395 if(-1 == ret_0){
1396 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_0[i]);
1397 fail_io[n] = test_gpio_0[i];
1398 n++;
1399 }else{
b.liuc7ffd092024-01-03 15:23:07 +08001400 //printf("############gpio [%d] test success############\n", test_gpio_0[i]);
wangyouqiangece1c722023-12-12 17:28:53 +08001401 }
1402 }
1403
1404 //Test 1
1405 for(i = 0; i < total_1; i++){
1406 ret_1 = gpio_register_test_out_1(test_gpio_1[i], 0);
1407 if(-1 == ret_1){
1408 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_1[i]);
1409 fail_io[n] = test_gpio_1[i];
1410 n++;
1411 }else{
b.liuc7ffd092024-01-03 15:23:07 +08001412 //printf("############gpio [%d] test success############\n", test_gpio_1[i]);
wangyouqiangece1c722023-12-12 17:28:53 +08001413 }
1414 }
wangyouqiang7775cbf2023-12-29 16:35:24 +08001415
1416 //Test 2
1417 for(i = 0; i < total_2; i=i+2){
1418 ret_2 = gpio_register_test_out_2(test_gpio_2[i], test_gpio_2[i+1], 0);
1419 if(-1 == ret_2){
b.liuc7ffd092024-01-03 15:23:07 +08001420 //printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_2[i+1]);
wangyouqiang7775cbf2023-12-29 16:35:24 +08001421 fail_io[n] = test_gpio_2[i];
1422 n++;
1423 }else{
1424 printf("############gpio [%d] test success############\n", test_gpio_2[i+1]);
1425 }
1426 }
1427
wangyouqiangece1c722023-12-12 17:28:53 +08001428 mbtk_adc_close();
1429 return n;
1430}
1431
wangyouqiangb1deff32024-03-05 13:31:27 +08001432#elif defined(MBTK_PROJECT_T108)
1433int gpio_register_test_out(int port, int value)
1434{
1435 int ret;
1436 int i;
1437 int valueh = 0;
1438 int valuel = 1;
1439
1440 //printf("Gpio port [%d] test start!\n", port);
1441 ret = gpio_register_set_func_0(port); //设功能为GPIO
1442 if(ret){
1443 printf("gpio_port can't support!\n");
1444 return -1;
1445 }
1446 gpio_register_set_direction(port, 1); //设方向为输出
1447 //ret = gpio_register_get_value(port);
1448 //printf("gpio default value is: %d\n", ret);
1449
1450
1451 //[High]
1452 for(i = 0; i <= 9; i++){
1453 gpio_register_set_value(port, 1);
1454 usleep(50);
1455 //valueh = gpio_register_get_value(port);
1456 //printf("set high? %d\n", valueh);
1457 if(1 == valueh){
1458 break;
1459 }
1460 }
1461
1462 for(i = 0; i <= 35; i++){
1463 ret = gpio_register_get_value(118);
1464 if(1 == ret){
1465 break;
1466 }
1467 usleep(3000);
1468 }
1469 printf("******gpio should is high: %d.******\n", ret);
1470 if(1 != ret){
1471 ret=-1;
1472 goto exit;
1473 }
1474
1475 //[Low]
1476 usleep(200);
1477 for(i = 0; i <= 9; i++){
1478 gpio_register_set_value(port, 0); //输出低
1479 usleep(50);
1480 //valuel = gpio_register_get_value(port);
1481 //printf("set low? %d\n", valuel);
1482 if(0 == valuel){
1483 break;
1484 }
1485 }
1486
1487 for(i = 0; i <= 35; i++){
1488 ret = gpio_register_get_value(118);
1489 if(0 == ret){
1490 break;
1491 }
1492 usleep(3000);
1493 }
1494 //printf("******gpio should is low: %d.******\n", ret);
1495 if(0 != ret){
1496 ret=-1;
1497 goto exit;
1498 }
1499
1500exit:
1501 gpio_register_set_value(port, 0);
1502 gpio_register_set_direction(port, 0); //设方向为输入
1503 return ret;
1504}
1505
1506int gpio_test_init_test_gpio_mode(void)
1507{
1508 int test_gpio[] = {
1509 99,
1510 117,
1511 21,22,23,24,44,41,120,
1512 8,127,46,59,58,57,56,55,48,19,34,33,35,36,49,
1513 50,25,28,26,122,20,10,11,
1514 39,40,37,38,
1515 51,52,31,32,
1516 };
1517
1518 int i, j, ret, total, n = 0;
1519 total = (sizeof(test_gpio)/sizeof(int));
1520 printf("[init]gpio_test_init_test_gpio_mode BEGIN\n");
1521
1522 /* [1 all gpio set to low] */
1523 //system("echo 118 > /sys/class/gpio/unexport");
1524 //system("echo 118 > /sys/class/gpio/export");
1525 gpio_register_set_func_0(118);
1526 gpio_register_set_direction(118, 0);
1527 system("echo in > /sys/class/gpio/gpio118/direction");
1528
1529 for(j = 0; j < 6 ; j++){
1530 for(i = 0; i < total; i++){
1531 gpio_register_set_func_0(test_gpio[i]);
1532 gpio_register_set_direction(test_gpio[i], 1);
1533 gpio_register_set_value(test_gpio[i], 0);
1534 //ret = gpio_register_get_value(test_gpio[i]);
1535 //printf("[init]get gpio%d=%d\n", test_gpio[i], ret);
1536 }
1537 //system("hwacc w 0xd401e1ac 0x1040"); //52
1538
1539 for(i = 0; i <= 35; i++){
1540 ret = gpio_register_get_value(118);
1541 printf("[init]get gpi118=%d\n", ret);
1542 usleep(3000);
1543 if(0 == (ret)){
1544 break;
1545 }
1546 }
1547
1548 printf("pre set ADC: %d, times: %d\n",(ret), j);
1549 if(0 == (ret)){
1550 break;
1551 }
1552 }
1553 if(0 != (ret)){
1554 printf("!!!Set all low FAIL, please retest\n");
1555 return -1;
1556 }
1557 return 0;
1558}
1559
1560int mbtk_at_gpio(void* arg)
1561{
1562
1563
1564 int test_gpio[] = {
1565 99,
1566 117,
1567 21,22,23,24,44,41,120,
1568 8,127,46,59,58,57,56,55,48,19,34,33,35,36,49,
1569 50,25,28,26,122,20,10,11,
1570 39,40,37,38,
1571 51,52,31,32,
1572
1573 };
1574
1575 int i, n = 0, ret, total;
1576 int *fail_io = (int *)arg;
1577 total = (sizeof(test_gpio)/sizeof(int));
1578 printf("T108 Start test gpio V1.0, total gpio=%d\n", total);
1579
1580 /* [1 all gpio Init] */
1581 gpio_test_init_test_gpio_mode();
1582
1583 /* [2 GPIOTEST] */
1584 for(i = 0; i < total; i++){
1585 ret = gpio_register_test_out(test_gpio[i], 0);
1586 if(-1 == ret){
1587 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio[i]);
1588 fail_io[n] = test_gpio[i];
1589 n++;
1590 }else{
1591 //printf("############gpio [%d] test success############\n", test_gpio[i]);
1592 }
1593 }
1594 return n;
1595}
1596
1597
wangyouqiangece1c722023-12-12 17:28:53 +08001598
b.liu4f8e8792023-11-01 23:25:22 +08001599#else
b.liu4f8e8792023-11-01 23:25:22 +08001600int mbtk_at_gpio(void* arg)
1601{
1602 return -1;
1603}
1604
1605#endif