blob: 80cff215f1d6283cdbe4467140490912d1cde161 [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{
b.liu85565a72023-11-09 16:47:40 +0800982 int i;
983 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
1032
1033 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;
1041 fail_io[i] = Out_pin;
1042 fail_io[i+1] = in_pin;
1043
1044 }
1045
1046 }
1047 }
1048
1049 else
1050 {
1051 printf(buf,"ALL GPIO TEST PASS\r\n");
1052 }
1053 //temp_len = strlen(buf);
1054 return test_fail_count;
b.liu4f8e8792023-11-01 23:25:22 +08001055}
1056
wangyouqiangece1c722023-12-12 17:28:53 +08001057#elif defined(MBTK_PROJECT_L508_X6)
wangyouqiangece1c722023-12-12 17:28:53 +08001058int gpio_register_test_out_0(int port, int value)
1059{
1060 int ret;
1061 int i;
1062 int valueh = 0;
1063 int valuel = 1;
1064
1065 printf("Gpio port [%d] test start!\n", port);
1066 ret = gpio_register_set_func_0(port); //设功能为GPIO
1067 if(ret){
1068 printf("gpio_port can't support!\n");
1069 return -1;
1070 }
1071 gpio_register_set_direction(port, 1); //设方向为输出
1072 ret = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001073 //printf("gpio default value is : %d.\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001074
1075
1076 //[High]
1077 for(i = 0; i <= 10; i++){
1078 gpio_register_set_value(port, 1);
1079 usleep(50);
1080 valueh = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001081 //printf("set high? %d\n",valueh);
wangyouqiangece1c722023-12-12 17:28:53 +08001082 if(1 == valueh){
1083 break;
1084 }
1085 }
1086 //usleep(10000);
1087 for(i = 0; i <= 35; i++){
1088 ret = mbtk_gpio_adc(0);
1089 usleep(3000);
1090 if(1 == ret){
1091 break;
1092 }
1093 }
b.liuc7ffd092024-01-03 15:23:07 +08001094 //printf("******gpio should is high: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001095 if(1 != ret){
1096 ret=-1;
1097 goto exit;
1098 }
1099
1100
1101
1102 //[Low]
1103 usleep(200);
1104 for(i = 0; i <= 10; i++){
1105 gpio_register_set_value(port, 0); //输出低
1106 usleep(50);
1107 valuel = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001108 //printf("set low? %d\n",valuel);
wangyouqiangece1c722023-12-12 17:28:53 +08001109 if(0 == valuel){
1110 break;
1111 }
1112 }
1113 //usleep(10000);
1114 for(i = 0; i <= 35; i++){
1115 ret = mbtk_gpio_adc(0);
1116 usleep(3000);
1117 if(0 == ret){
1118 break;
1119 }
1120 }
b.liuc7ffd092024-01-03 15:23:07 +08001121 //printf("******gpio should is low: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001122 if(0 != ret){
1123 ret=-1;
1124 goto exit;
1125 }
1126
1127exit:
1128 gpio_register_set_value(port, 0);
1129 gpio_register_set_direction(port, 0); //设方向为输入
1130 return ret;
1131}
1132
1133int gpio_register_test_out_1(int port, int value)
1134{
1135 int ret;
1136 int i;
1137 int valueh = 0;
1138 int valuel = 1;
1139
1140 printf("Gpio port [%d] test start!\n", port);
1141 ret = gpio_register_set_func_0(port); //设功能为GPIO
1142 if(ret){
1143 printf("gpio_port can't support!\n");
1144 return -1;
1145 }
1146 gpio_register_set_direction(port, 1); //设方向为输出
1147 ret = gpio_register_get_value(port);
1148 printf("gpio default value is : %d.\n", ret);
1149
1150
1151 //[High]
1152 for(i = 0; i <= 10; i++){
1153 gpio_register_set_value(port, 1);
1154 usleep(50);
1155 valueh = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001156 //printf("set high? %d\n",valueh);
wangyouqiangece1c722023-12-12 17:28:53 +08001157 if(1 == valueh){
1158 break;
1159 }
1160 }
1161 //usleep(10000);
1162 for(i = 0; i <= 35; i++){
1163 ret = mbtk_gpio_adc(1);
1164 usleep(3000);
1165 if(1 == ret){
1166 break;
1167 }
1168 }
1169 if(1 != ret){
1170 ret=-1;
1171 goto exit;
1172 }
b.liuc7ffd092024-01-03 15:23:07 +08001173 //printf("******gpio should is high: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001174
1175
1176 //[Low]
1177 usleep(200);
1178 for(i = 0; i <= 10; i++){
1179 gpio_register_set_value(port, 0); //输出低
1180 usleep(50);
1181 valuel = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001182 //printf("set low? %d\n",valuel);
wangyouqiangece1c722023-12-12 17:28:53 +08001183 if(0 == valuel){
1184 break;
1185 }
1186 }
1187 //usleep(10000);
1188 for(i = 0; i <= 35; i++){
1189 ret = mbtk_gpio_adc(1);
1190 usleep(3000);
1191 if(0 == ret){
1192 break;
1193 }
1194 }
b.liuc7ffd092024-01-03 15:23:07 +08001195 //printf("******gpio should is low: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001196 if(0 != ret){
1197 ret=-1;
1198 goto exit;
1199 }
1200
1201exit:
wangyouqiang7775cbf2023-12-29 16:35:24 +08001202 gpio_register_set_value(port, 0);
1203 gpio_register_set_direction(port, 0); //设方向为输入
wangyouqiangece1c722023-12-12 17:28:53 +08001204 return ret;
1205}
1206
wangyouqiang7775cbf2023-12-29 16:35:24 +08001207
1208int gpio_register_test_out_2(int port_in, int port_out, int value)
1209{
1210 int ret_in;
1211 int ret_out;
1212 int i;
1213 int valueh = 0;
1214 int valuel = 1;
1215
1216 printf("Gpio port [%d] test start!\n", port_out);
1217 ret_in = gpio_register_set_func_0(port_in); //设功能为GPIO
1218 ret_out = gpio_register_set_func_0(port_out);
1219 if((ret_in+ret_out)){
1220 printf("gpio_port can't support!\n");
1221 return -1;
1222 }
1223 gpio_register_set_direction(port_out, 1); //设方向为输出
1224 gpio_register_set_direction(port_in, 0); //设方向为输入
1225 ret_in = gpio_register_get_value(port_in);
1226 printf("gpio default value is : %d.\n", ret_in);
1227
1228
1229 //[High]
1230 for(i = 0; i <= 10; i++){
1231 gpio_register_set_value(port_out, 1);
1232 usleep(50);
1233 valueh = gpio_register_get_value(port_out);
1234 printf("set high? %d\n",valueh);
1235 if(1 == valueh){
1236 break;
1237 }
1238 }
1239
1240
1241 for(i = 0; i <= 35; i++){
1242 ret_in = gpio_register_get_value(port_in);
1243 usleep(3000);
1244 if(1 == ret_in){
1245 break;
1246 }
1247 }
1248 if(1 != ret_in){
1249 ret_in=-1;
1250 printf("get high failed! \n");
1251 goto exit;
1252 }
1253 printf("******gpio should is high: %d.******\n", ret_in);
1254
1255
1256 //[Low]
1257 usleep(200);
1258 for(i = 0; i <= 10; i++){
1259 gpio_register_set_value(port_out, 0); //输出低
1260 usleep(50);
1261 valuel = gpio_register_get_value(port_out);
1262 printf("set low? %d\n",valuel);
1263 if(0 == valuel){
1264 break;
1265 }
1266 }
1267
1268 for(i = 0; i <= 35; i++){
1269 ret_in = gpio_register_get_value(port_in);
1270 usleep(3000);
1271 if(0 == ret_in){
1272 break;
1273 }
1274 }
1275 printf("******gpio should is low: %d.******\n", ret_in);
1276 if(0 != ret_in){
1277 ret_in=-1;
1278 printf("get low failed! \n");
1279 goto exit;
1280 }
1281
1282exit:
1283 gpio_register_set_value(port_out, 0);
1284 gpio_register_set_direction(port_out, 0); //设方向为输入
1285 return ret_in;
1286}
1287
1288
wangyouqiangece1c722023-12-12 17:28:53 +08001289void gpio_test_init_test_gpio_mode(void )
1290{
1291
1292
1293 int test_gpio_1[] = {
1294 40, 33, 34, 39, 99,
1295 56, 58, 55, 57, 48, 59, 123, 122, 20, 53,
1296 19, 49, 50, 32 ,31, 6, 11, 10,
1297 88, 87, 86, 85, 84, 89,
wangyouqiang7775cbf2023-12-29 16:35:24 +08001298 90, 46, 127, 117, 119,
wangyouqiangece1c722023-12-12 17:28:53 +08001299 };
1300
1301 int test_gpio_0[] = {
1302 7, 8, 27, 28, 26, 25, 15, 1, 3, 0,
1303 37, 38,35, 36, 42, 41, 21, 22, 24, 23,
1304 54, 125, 18, 13, 14, 17, 16, 5, 4, 2, 12,
1305 44, 45, 47
1306 };
1307 int i, j, ret_0, ret_1, total_1, total_0, n = 0;
1308 total_0 = (sizeof(test_gpio_0)/sizeof(int));
1309 total_1 = (sizeof(test_gpio_1)/sizeof(int));
1310
wangyouqiang7775cbf2023-12-29 16:35:24 +08001311 //system("at at+gpsinit=0");
1312 system("i2cset -y -f 2 0x32 0x0d 0x00");
1313 system("i2cset -y -f 2 0x31 0x18 0x8f");
1314 usleep(10000);
wangyouqiangece1c722023-12-12 17:28:53 +08001315
1316 //printf("[init]gpio_test_init_test_gpio_mode FINISH\n");
1317 /* [1 all gpio set to low] */
1318 for(j = 0; j < 6 ; j++){
1319 for(i = 0; i < total_1; i++){
1320 gpio_register_set_func_0(test_gpio_1[i]);
1321 gpio_register_set_direction(test_gpio_1[i], 1);
1322 gpio_register_set_value(test_gpio_1[i], 0);
1323 }
1324 for(i = 0; i < total_0; i++){
1325 gpio_register_set_func_0(test_gpio_0[i]);
1326 gpio_register_set_direction(test_gpio_0[i], 1);
1327 gpio_register_set_value(test_gpio_0[i], 0);
1328 }
1329 //usleep(10000);
1330
1331 for(i = 0; i <= 35; i++){
1332 ret_0 = mbtk_gpio_adc(0);
1333 usleep(3000);
1334 ret_1 = mbtk_gpio_adc(1);
1335
1336 if(0 == (ret_0 + ret_1)){
1337 break;
1338 }
1339 }
1340 printf("pre set ADC: %d, times: %d\n",(ret_0 + ret_1), j);
1341 if(0 == (ret_0 + ret_1)){
1342 break;
1343 }
1344 }
1345 if(0 != (ret_0 + ret_1)){
wangyouqiang7775cbf2023-12-29 16:35:24 +08001346
wangyouqiangece1c722023-12-12 17:28:53 +08001347 printf("Fail, please retest");
1348 return -1;
1349 }
wangyouqiang7775cbf2023-12-29 16:35:24 +08001350 gpio_register_set_value(119, 1);
1351 gpio_register_set_value(117, 1);
wangyouqiangece1c722023-12-12 17:28:53 +08001352
1353}
1354
1355int mbtk_at_gpio(void* arg)
1356{
wangyouqiang7775cbf2023-12-29 16:35:24 +08001357
1358 printf("Start test gpio V1.0\n");
wangyouqiangece1c722023-12-12 17:28:53 +08001359 int test_gpio_1[] = {
1360 40, 33, 34, 39, 99,
1361 56, 58, 55, 57, 48, 59, 20, 53,
1362 19, 49, 50, 32 ,31, 6, 11, 10,7,
wangyouqiang7775cbf2023-12-29 16:35:24 +08001363 90, 46, 127,
1364 88, 87, 86, 85, 84, 89,
wangyouqiangece1c722023-12-12 17:28:53 +08001365 };
1366
1367 int test_gpio_0[] = {
1368 35, 36, 42, 41, 21, 22, 24, 23,
1369 54, 125, 18, 13, 14, 17, 16, 5, 4, 2, 12,0,
1370 27, 28, 26, 25, 15, 1, 3,
1371 37, 38,8
wangyouqiang7775cbf2023-12-29 16:35:24 +08001372 };
1373 int test_gpio_2[] = { //GPS组
1374 43,47,
1375 //45,44
wangyouqiangece1c722023-12-12 17:28:53 +08001376 };
1377
wangyouqiang7775cbf2023-12-29 16:35:24 +08001378 int i, j, ret_0, ret_1, ret_2, total_1, total_0, total_2, n = 0;
wangyouqiangece1c722023-12-12 17:28:53 +08001379 int *fail_io = (int *)arg;
1380 total_0 = (sizeof(test_gpio_0)/sizeof(int));
1381 total_1 = (sizeof(test_gpio_1)/sizeof(int));
wangyouqiang7775cbf2023-12-29 16:35:24 +08001382 total_2 = (sizeof(test_gpio_2)/sizeof(int));
wangyouqiangece1c722023-12-12 17:28:53 +08001383
1384 /* [1 all gpio Init] */
1385 gpio_test_init_test_gpio_mode();
1386
1387
1388 /* [2 GPIOTEST] */
wangyouqiangece1c722023-12-12 17:28:53 +08001389 //Test 0
1390 for(i = 0; i < total_0; i++){
1391 ret_0 = gpio_register_test_out_0(test_gpio_0[i], 0);
1392 if(-1 == ret_0){
1393 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_0[i]);
1394 fail_io[n] = test_gpio_0[i];
1395 n++;
1396 }else{
b.liuc7ffd092024-01-03 15:23:07 +08001397 //printf("############gpio [%d] test success############\n", test_gpio_0[i]);
wangyouqiangece1c722023-12-12 17:28:53 +08001398 }
1399 }
1400
1401 //Test 1
1402 for(i = 0; i < total_1; i++){
1403 ret_1 = gpio_register_test_out_1(test_gpio_1[i], 0);
1404 if(-1 == ret_1){
1405 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_1[i]);
1406 fail_io[n] = test_gpio_1[i];
1407 n++;
1408 }else{
b.liuc7ffd092024-01-03 15:23:07 +08001409 //printf("############gpio [%d] test success############\n", test_gpio_1[i]);
wangyouqiangece1c722023-12-12 17:28:53 +08001410 }
1411 }
wangyouqiang7775cbf2023-12-29 16:35:24 +08001412
1413 //Test 2
1414 for(i = 0; i < total_2; i=i+2){
1415 ret_2 = gpio_register_test_out_2(test_gpio_2[i], test_gpio_2[i+1], 0);
1416 if(-1 == ret_2){
b.liuc7ffd092024-01-03 15:23:07 +08001417 //printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_2[i+1]);
wangyouqiang7775cbf2023-12-29 16:35:24 +08001418 fail_io[n] = test_gpio_2[i];
1419 n++;
1420 }else{
1421 printf("############gpio [%d] test success############\n", test_gpio_2[i+1]);
1422 }
1423 }
1424
wangyouqiangece1c722023-12-12 17:28:53 +08001425 mbtk_adc_close();
1426 return n;
1427}
1428
wangyouqiangb1deff32024-03-05 13:31:27 +08001429#elif defined(MBTK_PROJECT_T108)
1430int gpio_register_test_out(int port, int value)
1431{
1432 int ret;
1433 int i;
1434 int valueh = 0;
1435 int valuel = 1;
1436
1437 //printf("Gpio port [%d] test start!\n", port);
1438 ret = gpio_register_set_func_0(port); //设功能为GPIO
1439 if(ret){
1440 printf("gpio_port can't support!\n");
1441 return -1;
1442 }
1443 gpio_register_set_direction(port, 1); //设方向为输出
1444 //ret = gpio_register_get_value(port);
1445 //printf("gpio default value is: %d\n", ret);
1446
1447
1448 //[High]
1449 for(i = 0; i <= 9; i++){
1450 gpio_register_set_value(port, 1);
1451 usleep(50);
1452 //valueh = gpio_register_get_value(port);
1453 //printf("set high? %d\n", valueh);
1454 if(1 == valueh){
1455 break;
1456 }
1457 }
1458
1459 for(i = 0; i <= 35; i++){
1460 ret = gpio_register_get_value(118);
1461 if(1 == ret){
1462 break;
1463 }
1464 usleep(3000);
1465 }
1466 printf("******gpio should is high: %d.******\n", ret);
1467 if(1 != ret){
1468 ret=-1;
1469 goto exit;
1470 }
1471
1472 //[Low]
1473 usleep(200);
1474 for(i = 0; i <= 9; i++){
1475 gpio_register_set_value(port, 0); //输出低
1476 usleep(50);
1477 //valuel = gpio_register_get_value(port);
1478 //printf("set low? %d\n", valuel);
1479 if(0 == valuel){
1480 break;
1481 }
1482 }
1483
1484 for(i = 0; i <= 35; i++){
1485 ret = gpio_register_get_value(118);
1486 if(0 == ret){
1487 break;
1488 }
1489 usleep(3000);
1490 }
1491 //printf("******gpio should is low: %d.******\n", ret);
1492 if(0 != ret){
1493 ret=-1;
1494 goto exit;
1495 }
1496
1497exit:
1498 gpio_register_set_value(port, 0);
1499 gpio_register_set_direction(port, 0); //设方向为输入
1500 return ret;
1501}
1502
1503int gpio_test_init_test_gpio_mode(void)
1504{
1505 int test_gpio[] = {
1506 99,
1507 117,
1508 21,22,23,24,44,41,120,
1509 8,127,46,59,58,57,56,55,48,19,34,33,35,36,49,
1510 50,25,28,26,122,20,10,11,
1511 39,40,37,38,
1512 51,52,31,32,
1513 };
1514
1515 int i, j, ret, total, n = 0;
1516 total = (sizeof(test_gpio)/sizeof(int));
1517 printf("[init]gpio_test_init_test_gpio_mode BEGIN\n");
1518
1519 /* [1 all gpio set to low] */
1520 //system("echo 118 > /sys/class/gpio/unexport");
1521 //system("echo 118 > /sys/class/gpio/export");
1522 gpio_register_set_func_0(118);
1523 gpio_register_set_direction(118, 0);
1524 system("echo in > /sys/class/gpio/gpio118/direction");
1525
1526 for(j = 0; j < 6 ; j++){
1527 for(i = 0; i < total; i++){
1528 gpio_register_set_func_0(test_gpio[i]);
1529 gpio_register_set_direction(test_gpio[i], 1);
1530 gpio_register_set_value(test_gpio[i], 0);
1531 //ret = gpio_register_get_value(test_gpio[i]);
1532 //printf("[init]get gpio%d=%d\n", test_gpio[i], ret);
1533 }
1534 //system("hwacc w 0xd401e1ac 0x1040"); //52
1535
1536 for(i = 0; i <= 35; i++){
1537 ret = gpio_register_get_value(118);
1538 printf("[init]get gpi118=%d\n", ret);
1539 usleep(3000);
1540 if(0 == (ret)){
1541 break;
1542 }
1543 }
1544
1545 printf("pre set ADC: %d, times: %d\n",(ret), j);
1546 if(0 == (ret)){
1547 break;
1548 }
1549 }
1550 if(0 != (ret)){
1551 printf("!!!Set all low FAIL, please retest\n");
1552 return -1;
1553 }
1554 return 0;
1555}
1556
1557int mbtk_at_gpio(void* arg)
1558{
1559
1560
1561 int test_gpio[] = {
1562 99,
1563 117,
1564 21,22,23,24,44,41,120,
1565 8,127,46,59,58,57,56,55,48,19,34,33,35,36,49,
1566 50,25,28,26,122,20,10,11,
1567 39,40,37,38,
1568 51,52,31,32,
1569
1570 };
1571
1572 int i, n = 0, ret, total;
1573 int *fail_io = (int *)arg;
1574 total = (sizeof(test_gpio)/sizeof(int));
1575 printf("T108 Start test gpio V1.0, total gpio=%d\n", total);
1576
1577 /* [1 all gpio Init] */
1578 gpio_test_init_test_gpio_mode();
1579
1580 /* [2 GPIOTEST] */
1581 for(i = 0; i < total; i++){
1582 ret = gpio_register_test_out(test_gpio[i], 0);
1583 if(-1 == ret){
1584 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio[i]);
1585 fail_io[n] = test_gpio[i];
1586 n++;
1587 }else{
1588 //printf("############gpio [%d] test success############\n", test_gpio[i]);
1589 }
1590 }
1591 return n;
1592}
1593
1594
wangyouqiangece1c722023-12-12 17:28:53 +08001595
b.liu4f8e8792023-11-01 23:25:22 +08001596#else
b.liu4f8e8792023-11-01 23:25:22 +08001597int mbtk_at_gpio(void* arg)
1598{
1599 return -1;
1600}
1601
1602#endif