blob: 18d216d948167af8277e7ac936eb904fb9280cc5 [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
wangyouqiang7775cbf2023-12-29 16:35:24 +080039#if defined(MBTK_PROJECT_L508_X6)
40struct 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},
77 {GPIO_FUNC_GPIO_36, 0},
78 {GPIO_FUNC_MMC1_DAT3, 5}, // GPIO_37
79 {GPIO_FUNC_MMC1_DAT2, 5}, // GPIO_38
80 {GPIO_FUNC_MMC1_DAT1, 5}, // GPIO_39
81 {GPIO_FUNC_MMC1_DAT0, 5}, // GPIO_40
82 {GPIO_FUNC_MMC1_CMD, 5}, //GPIO_41
83 {GPIO_FUNC_MMC1_CLK, 5}, //GPIO_42
84 {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)
b.liu85565a72023-11-09 16:47:40 +0800700int test_gpio[] =
701{
702 0,2,1,6,16,15,17,99,
703 34,36,35,33,14,49,50,12,118,120,
wangyouqiang89888612023-11-28 18:28:12 +0800704 43,19,7,3,
b.liu85565a72023-11-09 16:47:40 +0800705 20,59,126,117,125,18,56,55,58,48,57,5,4,21,
wangyouqiang89888612023-11-28 18:28:12 +0800706 23,24,13,27,28,26,25
707 //32,31
708 //,54,53,123,122
b.liu85565a72023-11-09 16:47:40 +0800709};
710int total;
711total = (sizeof(test_gpio)/sizeof(int));
712
713
714int gpio_test_init(void)
715{
716 int i,j,ret;
717 //int *fail_io = (int *)arg;
wangyouqiang89888612023-11-28 18:28:12 +0800718 printf("[init] L508 GPIOTEST v1.2 \n");
719 system("i2cset -y -f 2 0x31 0x12 0x46");
b.liu85565a72023-11-09 16:47:40 +0800720 gpio_register_set_func_0(22);
721 gpio_register_set_direction(22, 0); //GPIO22 DTR做输入,检测电压
722
723 #if 0
724 /* 错误点预先关闭 */
725 gpio_register_set_func_0(123);
726 gpio_register_set_func_0(32);
727 gpio_register_set_func_0(53);
728 gpio_register_set_direction(123, 1);
729 gpio_register_set_direction(32, 1);
730 gpio_register_set_direction(53, 1);
731 #endif
732
733 /* [1 all gpio set to low] */
734 for(j = 0; j < 6 ; j++){
735 for(i = 0; i < total; i++){
736 gpio_register_set_func_0(test_gpio[i]);
737 gpio_register_set_direction(test_gpio[i], 1);
738 gpio_register_set_value(test_gpio[i], 0);
739 }
740
741 for(i = 0; i <= 5; i++){
742 ret = gpio_register_get_value(22);
743 if(0 == ret){
744 break;
745 }
746 usleep(3000);
747 }
748 printf("pre set ADC: %d, times: %d\n",ret, j);
749 if(0 == ret){
750 break;
751 }
752 }
753
754 if(0 != ret){
755 printf("set all low Fail, please retest\n");
756 return -1;
757 }
758 printf("pre set SUCCESS\n");
759 return 0;
760}
761
wangyouqiang89888612023-11-28 18:28:12 +0800762int gpio_test(void* arg)
b.liu85565a72023-11-09 16:47:40 +0800763{
764 int ret,i,j;
wangyouqiang89888612023-11-28 18:28:12 +0800765 int *fail_io = (int *)arg;
b.liu85565a72023-11-09 16:47:40 +0800766 int n = 0;
767 for(i = 0; i < total; i++){
768 ret = gpio_register_get_value(test_gpio[i]);
769 printf("GPIO%d default value? %d\n", test_gpio[i], ret);
770 //set high
771 gpio_register_set_value(test_gpio[i], 1);
772 ret = gpio_register_get_value(test_gpio[i]);
773 printf("GPIO%d set high? %d\n", test_gpio[i], ret);
774 //get
775 ret = gpio_register_get_value(22);
776 if(1 != ret){
777 printf("gpio [%d] test high failed\n", test_gpio[i]);
wangyouqiang89888612023-11-28 18:28:12 +0800778 fail_io[n] = test_gpio[i];
b.liu85565a72023-11-09 16:47:40 +0800779 n++;
780 }else{
781 printf("gpio[%d] test high success\n", test_gpio[i]);
782 }
783
784 //set low
785 gpio_register_set_value(test_gpio[i], 0);
786 ret = gpio_register_get_value(test_gpio[i]);
787 printf("GPIO%d set low? %d\n", test_gpio[i], ret);
788 //get
789 ret = gpio_register_get_value(22);
790 if(0 != ret){
791 printf("gpio [%d] test low failed\n", test_gpio[i]);
wangyouqiang89888612023-11-28 18:28:12 +0800792 fail_io[n] = test_gpio[i];
b.liu85565a72023-11-09 16:47:40 +0800793 n++;
794 }else{
795 printf("gpio [%d] test low success\n", test_gpio[i]);
796 }
797 }
798 return n;
799}
800
801
802
b.liu4f8e8792023-11-01 23:25:22 +0800803
804int mbtk_at_gpio(void* arg)
805{
b.liu85565a72023-11-09 16:47:40 +0800806 int i, j, ret, total, n = 0;
wangyouqiang89888612023-11-28 18:28:12 +0800807 //int *fail_io = (int *)arg;
b.liu85565a72023-11-09 16:47:40 +0800808
809 gpio_test_init();
wangyouqiang89888612023-11-28 18:28:12 +0800810 n = gpio_test(arg);
b.liu85565a72023-11-09 16:47:40 +0800811
812 return n;
b.liu4f8e8792023-11-01 23:25:22 +0800813}
814
b.liu85565a72023-11-09 16:47:40 +0800815
816
b.liu4f8e8792023-11-01 23:25:22 +0800817#elif defined(MBTK_PROJECT_L509)
b.liu85565a72023-11-09 16:47:40 +0800818mbtk_gpio_test_pin_paired test_pin_array[] ={ //是GPIO_array
819//120 5 33 这几个输入脚没法正常设置为输入
820 //{52,51,0}, //拉不低 没有这一组
b.liu006dabb2023-11-09 17:31:53 +0800821 //{54,53,0},
b.liu85565a72023-11-09 16:47:40 +0800822 {16,17,0},
823 {117,120,0},
b.liu006dabb2023-11-09 17:31:53 +0800824 //{118,19,0}, //usim
b.liu85565a72023-11-09 16:47:40 +0800825 {123,126,0},
826 {43,23,0},
827 {28,27,0},
828 {26,25,0},
829 {34,33,0},
830 {36,35,0},
831 {49,50,0},
832 {32,31,0},
833 {7,2,0},
834 {6,1,0},
835 {15,3,0},
836 {0,18,0},
837 {4,5,0},
838 {13,14,0},
839 {48,55,0},
840 {56,57,0},
841 {59,58,0},
842 {21,99,0},
843 //{20,22,0}, //拉不低
844 //{12,122,0}, //拉不低 122好像不该用
845};
846//测低
847uint16 mbtk_gpio_test_all_low(mbtk_gpio_test_pin_paired *pin_array, uint16 MAX_pin_num)
848{
849 uint16 index =0;
850 uint16 OUT_pin, Read_pin;
851 uint32 read_high_value;
852 uint32 read_low_value;
853 uint16 fail_num=0;
854 int res;
855
856 printf("[test_low_begin]\n");
857
858
859 for(index = 0; index < MAX_pin_num; index++){
860 OUT_pin = pin_array[index].output_pin;
861 Read_pin = pin_array[index].input_pin;
862
863 //输出低
864 gpio_register_set_direction(OUT_pin,1);
865 gpio_register_set_value(OUT_pin, 0);
866
867 //输入脚设为输入
868 gpio_register_set_direction(Read_pin,0);
869 }
870
871 for(index = 0; index < MAX_pin_num; index++){
872 OUT_pin = pin_array[index].output_pin;
873 Read_pin = pin_array[index].input_pin;
874 read_low_value = gpio_register_get_value(Read_pin);
875 //结果检测
876 if(read_low_value != 0){
877 printf("[!Low Failed!]: GPIO%d \n", Read_pin);
878 pin_array[index].mbtk_gpio_test_result = 1;
879 fail_num ++;
880 }
881 else{
882 printf("[-Low Success-]: GPIO%d \n", Read_pin);
883 }
884 }
885 return fail_num;
886
887}
888
889
890
891//测高
892uint16 mbtk_gpio_test_pin_high(mbtk_gpio_test_pin_paired *pin_array,uint16 MAX_pin_num)
893{
894 uint16 index =0;
895 uint16 OUT_pin, Read_pin;
896 uint32 read_high_value;
897 uint32 read_low_value;
898 uint16 fail_num=0;
899 int i = 0;
900
901 printf("[test_high_begin]\n");
902
903 for(index = 0;index<MAX_pin_num;index++)
904 {
905 OUT_pin = pin_array[index].output_pin;
906 Read_pin = pin_array[index].input_pin;
907
908 //设高
909 gpio_register_set_direction(OUT_pin,1);
910 gpio_register_set_direction(Read_pin,0);
911 gpio_register_set_value(OUT_pin, 1);
912 usleep(20000);
913 read_high_value = gpio_register_get_value(Read_pin);
914 printf("[test_high]: Gpio%d: get value =%d \n", Read_pin, read_high_value);
915
916
917 //设高后重新设低
918 usleep(20000);
919 gpio_register_set_value(OUT_pin, 0);
920 usleep(80000);
921 //read_low_value = gpio_register_get_value(Read_pin);
922 //read_low_value = gpio_register_get_value(Read_pin);
923 for(i = 0;i < 10; i++){
924 read_low_value = gpio_register_get_value(Read_pin);
925 if(0 == read_low_value){
926 break;
927 }
928 usleep(20000);
929 }
930
931 printf("[test_low]: Gpio%d: get value =%d \n", Read_pin, read_low_value);
932
933 if(read_high_value != 1||read_low_value !=0)
934 {
935 printf("[!High Failed!]: Gpio%d\n",Read_pin);
936 pin_array[index].mbtk_gpio_test_result = 2;
937 fail_num ++;
938 }
939 else{
940 printf("[-High Success-]: Gpio%d \n", Read_pin);
941 }
942 }
943 return fail_num;
944
945}
946void gpio_test_init_test_gpio_mode(mbtk_gpio_test_pin_paired pin_array[],int MAX_pin_num)
947{
948 int index =0;
949 int OUT_pin, Read_pin;
950
951 printf("[init] L509 GPIOTEST v1 \n");
952 for(index = 0;index<MAX_pin_num;index++)
953 {
954 OUT_pin = pin_array[index].output_pin;
955 Read_pin = pin_array[index].input_pin;
956 gpio_register_set_func_0(OUT_pin);
957 gpio_register_set_func_0(Read_pin);
958 }
959
960 //gpio_register_set_func_0();
b.liu006dabb2023-11-09 17:31:53 +0800961 system("i2cset -y -f 2 0x31 0x12 0x46");
b.liu85565a72023-11-09 16:47:40 +0800962 system("echo 33 > /sys/class/gpio/export");
963 system("echo in > /sys/class/gpio/gpio120/direction");
964 system("echo in > /sys/class/gpio/gpio5/direction");
965 system("echo in > /sys/class/gpio/gpio33/direction");
966 printf("[3init]gpio_test_init_test_gpio_mode FINISH\n");
967
968}
969
b.liu4f8e8792023-11-01 23:25:22 +0800970
971int mbtk_at_gpio(void* arg)
972{
b.liu85565a72023-11-09 16:47:40 +0800973 int i;
974 int *fail_io = (int *)arg;
975 /*OPEN SWITCH */
976 uint16 test_fail_count = 0;
977 uint16 test_MAX_pin_num = 0;
978 char buf[STR_MAX_LEN];
979 uint16 str_len=0;
980 uint16 temp_len =0;
981 uint16 Out_pin;
982 uint16 in_pin;
983 uint16 fail_print_num = 0;
984
985 gpio_register_set_func_0(125);
986 gpio_register_set_direction(125, 1);
987 gpio_register_set_value(125, 1); //开关使能脚
988#if 0
989 /* [2]设置高低电压范围 */
990 if(th->threshold_high == 0 && th->threshold_low == 0 &&th->threshold_high_low_diff== 0)
991 { //use default threshold
992 th->threshold_high = MBTK_GPIOTEST_THRESHOLD_DEFAULT_HIGH;
993 th->threshold_low = MBTK_GPIOTEST_THRESHOLD_DEFAULT_LOW;
994 th->threshold_high_low_diff = MBTK_GPIOTEST_THRESHOLD_DEFAULT_DIFF;
995 }
996 RTI_LOG("GPIOTEST: threshold -- %d -- %d -- %d",th->threshold_low,th->threshold_high_low_diff,th->threshold_high);
997#endif
998
999 /* [3]初始化待测GPIO */
1000 test_MAX_pin_num = sizeof(test_pin_array)/sizeof(test_pin_array[0]);
1001 gpio_test_init_test_gpio_mode(test_pin_array,test_MAX_pin_num);
1002
1003
1004
1005 /* [4]测试过程 */
1006 test_fail_count = mbtk_gpio_test_all_low(test_pin_array,test_MAX_pin_num);
1007 printf("[set_low]set ALL GPIO low, fail_num=%d\n",test_fail_count);
1008 if(!test_fail_count) //set all pin low passed
1009 {
1010 printf("[set all pin low passed]\n");
1011 test_fail_count = mbtk_gpio_test_pin_high(&test_pin_array,test_MAX_pin_num);
1012 }
1013
1014 //memset(buf,0,STR_MAX_LEN);
1015
1016 /* [5]测试结果检测 */
1017 if(test_fail_count )
1018 {
1019 //printf( "GPIOTEST Fail %02d PINs:\n", test_fail_count*2);
1020 sprintf(buf, "GPIOTEST Fail %02d PINs:", test_fail_count*2);
1021 temp_len = strlen(buf);
1022
1023
1024 for(i = 0; i< test_MAX_pin_num; i++)
1025 {
1026
1027 if(test_pin_array[i].mbtk_gpio_test_result)
1028 {
1029
1030 Out_pin = test_pin_array[i].output_pin;
1031 in_pin = test_pin_array[i].input_pin;
1032 fail_io[i] = Out_pin;
1033 fail_io[i+1] = in_pin;
1034
1035 }
1036
1037 }
1038 }
1039
1040 else
1041 {
1042 printf(buf,"ALL GPIO TEST PASS\r\n");
1043 }
1044 //temp_len = strlen(buf);
1045 return test_fail_count;
b.liu4f8e8792023-11-01 23:25:22 +08001046}
1047
wangyouqiangece1c722023-12-12 17:28:53 +08001048#elif defined(MBTK_PROJECT_L508_X6)
wangyouqiangece1c722023-12-12 17:28:53 +08001049int gpio_register_test_out_0(int port, int value)
1050{
1051 int ret;
1052 int i;
1053 int valueh = 0;
1054 int valuel = 1;
1055
1056 printf("Gpio port [%d] test start!\n", port);
1057 ret = gpio_register_set_func_0(port); //设功能为GPIO
1058 if(ret){
1059 printf("gpio_port can't support!\n");
1060 return -1;
1061 }
1062 gpio_register_set_direction(port, 1); //设方向为输出
1063 ret = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001064 //printf("gpio default value is : %d.\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001065
1066
1067 //[High]
1068 for(i = 0; i <= 10; i++){
1069 gpio_register_set_value(port, 1);
1070 usleep(50);
1071 valueh = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001072 //printf("set high? %d\n",valueh);
wangyouqiangece1c722023-12-12 17:28:53 +08001073 if(1 == valueh){
1074 break;
1075 }
1076 }
1077 //usleep(10000);
1078 for(i = 0; i <= 35; i++){
1079 ret = mbtk_gpio_adc(0);
1080 usleep(3000);
1081 if(1 == ret){
1082 break;
1083 }
1084 }
b.liuc7ffd092024-01-03 15:23:07 +08001085 //printf("******gpio should is high: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001086 if(1 != ret){
1087 ret=-1;
1088 goto exit;
1089 }
1090
1091
1092
1093 //[Low]
1094 usleep(200);
1095 for(i = 0; i <= 10; i++){
1096 gpio_register_set_value(port, 0); //输出低
1097 usleep(50);
1098 valuel = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001099 //printf("set low? %d\n",valuel);
wangyouqiangece1c722023-12-12 17:28:53 +08001100 if(0 == valuel){
1101 break;
1102 }
1103 }
1104 //usleep(10000);
1105 for(i = 0; i <= 35; i++){
1106 ret = mbtk_gpio_adc(0);
1107 usleep(3000);
1108 if(0 == ret){
1109 break;
1110 }
1111 }
b.liuc7ffd092024-01-03 15:23:07 +08001112 //printf("******gpio should is low: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001113 if(0 != ret){
1114 ret=-1;
1115 goto exit;
1116 }
1117
1118exit:
1119 gpio_register_set_value(port, 0);
1120 gpio_register_set_direction(port, 0); //设方向为输入
1121 return ret;
1122}
1123
1124int gpio_register_test_out_1(int port, int value)
1125{
1126 int ret;
1127 int i;
1128 int valueh = 0;
1129 int valuel = 1;
1130
1131 printf("Gpio port [%d] test start!\n", port);
1132 ret = gpio_register_set_func_0(port); //设功能为GPIO
1133 if(ret){
1134 printf("gpio_port can't support!\n");
1135 return -1;
1136 }
1137 gpio_register_set_direction(port, 1); //设方向为输出
1138 ret = gpio_register_get_value(port);
1139 printf("gpio default value is : %d.\n", ret);
1140
1141
1142 //[High]
1143 for(i = 0; i <= 10; i++){
1144 gpio_register_set_value(port, 1);
1145 usleep(50);
1146 valueh = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001147 //printf("set high? %d\n",valueh);
wangyouqiangece1c722023-12-12 17:28:53 +08001148 if(1 == valueh){
1149 break;
1150 }
1151 }
1152 //usleep(10000);
1153 for(i = 0; i <= 35; i++){
1154 ret = mbtk_gpio_adc(1);
1155 usleep(3000);
1156 if(1 == ret){
1157 break;
1158 }
1159 }
1160 if(1 != ret){
1161 ret=-1;
1162 goto exit;
1163 }
b.liuc7ffd092024-01-03 15:23:07 +08001164 //printf("******gpio should is high: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001165
1166
1167 //[Low]
1168 usleep(200);
1169 for(i = 0; i <= 10; i++){
1170 gpio_register_set_value(port, 0); //输出低
1171 usleep(50);
1172 valuel = gpio_register_get_value(port);
b.liuc7ffd092024-01-03 15:23:07 +08001173 //printf("set low? %d\n",valuel);
wangyouqiangece1c722023-12-12 17:28:53 +08001174 if(0 == valuel){
1175 break;
1176 }
1177 }
1178 //usleep(10000);
1179 for(i = 0; i <= 35; i++){
1180 ret = mbtk_gpio_adc(1);
1181 usleep(3000);
1182 if(0 == ret){
1183 break;
1184 }
1185 }
b.liuc7ffd092024-01-03 15:23:07 +08001186 //printf("******gpio should is low: %d.******\n", ret);
wangyouqiangece1c722023-12-12 17:28:53 +08001187 if(0 != ret){
1188 ret=-1;
1189 goto exit;
1190 }
1191
1192exit:
wangyouqiang7775cbf2023-12-29 16:35:24 +08001193 gpio_register_set_value(port, 0);
1194 gpio_register_set_direction(port, 0); //设方向为输入
wangyouqiangece1c722023-12-12 17:28:53 +08001195 return ret;
1196}
1197
wangyouqiang7775cbf2023-12-29 16:35:24 +08001198
1199int gpio_register_test_out_2(int port_in, int port_out, int value)
1200{
1201 int ret_in;
1202 int ret_out;
1203 int i;
1204 int valueh = 0;
1205 int valuel = 1;
1206
1207 printf("Gpio port [%d] test start!\n", port_out);
1208 ret_in = gpio_register_set_func_0(port_in); //设功能为GPIO
1209 ret_out = gpio_register_set_func_0(port_out);
1210 if((ret_in+ret_out)){
1211 printf("gpio_port can't support!\n");
1212 return -1;
1213 }
1214 gpio_register_set_direction(port_out, 1); //设方向为输出
1215 gpio_register_set_direction(port_in, 0); //设方向为输入
1216 ret_in = gpio_register_get_value(port_in);
1217 printf("gpio default value is : %d.\n", ret_in);
1218
1219
1220 //[High]
1221 for(i = 0; i <= 10; i++){
1222 gpio_register_set_value(port_out, 1);
1223 usleep(50);
1224 valueh = gpio_register_get_value(port_out);
1225 printf("set high? %d\n",valueh);
1226 if(1 == valueh){
1227 break;
1228 }
1229 }
1230
1231
1232 for(i = 0; i <= 35; i++){
1233 ret_in = gpio_register_get_value(port_in);
1234 usleep(3000);
1235 if(1 == ret_in){
1236 break;
1237 }
1238 }
1239 if(1 != ret_in){
1240 ret_in=-1;
1241 printf("get high failed! \n");
1242 goto exit;
1243 }
1244 printf("******gpio should is high: %d.******\n", ret_in);
1245
1246
1247 //[Low]
1248 usleep(200);
1249 for(i = 0; i <= 10; i++){
1250 gpio_register_set_value(port_out, 0); //输出低
1251 usleep(50);
1252 valuel = gpio_register_get_value(port_out);
1253 printf("set low? %d\n",valuel);
1254 if(0 == valuel){
1255 break;
1256 }
1257 }
1258
1259 for(i = 0; i <= 35; i++){
1260 ret_in = gpio_register_get_value(port_in);
1261 usleep(3000);
1262 if(0 == ret_in){
1263 break;
1264 }
1265 }
1266 printf("******gpio should is low: %d.******\n", ret_in);
1267 if(0 != ret_in){
1268 ret_in=-1;
1269 printf("get low failed! \n");
1270 goto exit;
1271 }
1272
1273exit:
1274 gpio_register_set_value(port_out, 0);
1275 gpio_register_set_direction(port_out, 0); //设方向为输入
1276 return ret_in;
1277}
1278
1279
wangyouqiangece1c722023-12-12 17:28:53 +08001280void gpio_test_init_test_gpio_mode(void )
1281{
1282
1283
1284 int test_gpio_1[] = {
1285 40, 33, 34, 39, 99,
1286 56, 58, 55, 57, 48, 59, 123, 122, 20, 53,
1287 19, 49, 50, 32 ,31, 6, 11, 10,
1288 88, 87, 86, 85, 84, 89,
wangyouqiang7775cbf2023-12-29 16:35:24 +08001289 90, 46, 127, 117, 119,
wangyouqiangece1c722023-12-12 17:28:53 +08001290 };
1291
1292 int test_gpio_0[] = {
1293 7, 8, 27, 28, 26, 25, 15, 1, 3, 0,
1294 37, 38,35, 36, 42, 41, 21, 22, 24, 23,
1295 54, 125, 18, 13, 14, 17, 16, 5, 4, 2, 12,
1296 44, 45, 47
1297 };
1298 int i, j, ret_0, ret_1, total_1, total_0, n = 0;
1299 total_0 = (sizeof(test_gpio_0)/sizeof(int));
1300 total_1 = (sizeof(test_gpio_1)/sizeof(int));
1301
wangyouqiang7775cbf2023-12-29 16:35:24 +08001302 //system("at at+gpsinit=0");
1303 system("i2cset -y -f 2 0x32 0x0d 0x00");
1304 system("i2cset -y -f 2 0x31 0x18 0x8f");
1305 usleep(10000);
wangyouqiangece1c722023-12-12 17:28:53 +08001306
1307 //printf("[init]gpio_test_init_test_gpio_mode FINISH\n");
1308 /* [1 all gpio set to low] */
1309 for(j = 0; j < 6 ; j++){
1310 for(i = 0; i < total_1; i++){
1311 gpio_register_set_func_0(test_gpio_1[i]);
1312 gpio_register_set_direction(test_gpio_1[i], 1);
1313 gpio_register_set_value(test_gpio_1[i], 0);
1314 }
1315 for(i = 0; i < total_0; i++){
1316 gpio_register_set_func_0(test_gpio_0[i]);
1317 gpio_register_set_direction(test_gpio_0[i], 1);
1318 gpio_register_set_value(test_gpio_0[i], 0);
1319 }
1320 //usleep(10000);
1321
1322 for(i = 0; i <= 35; i++){
1323 ret_0 = mbtk_gpio_adc(0);
1324 usleep(3000);
1325 ret_1 = mbtk_gpio_adc(1);
1326
1327 if(0 == (ret_0 + ret_1)){
1328 break;
1329 }
1330 }
1331 printf("pre set ADC: %d, times: %d\n",(ret_0 + ret_1), j);
1332 if(0 == (ret_0 + ret_1)){
1333 break;
1334 }
1335 }
1336 if(0 != (ret_0 + ret_1)){
wangyouqiang7775cbf2023-12-29 16:35:24 +08001337
wangyouqiangece1c722023-12-12 17:28:53 +08001338 printf("Fail, please retest");
1339 return -1;
1340 }
wangyouqiang7775cbf2023-12-29 16:35:24 +08001341 gpio_register_set_value(119, 1);
1342 gpio_register_set_value(117, 1);
wangyouqiangece1c722023-12-12 17:28:53 +08001343
1344}
1345
1346int mbtk_at_gpio(void* arg)
1347{
wangyouqiang7775cbf2023-12-29 16:35:24 +08001348
1349 printf("Start test gpio V1.0\n");
wangyouqiangece1c722023-12-12 17:28:53 +08001350 int test_gpio_1[] = {
1351 40, 33, 34, 39, 99,
1352 56, 58, 55, 57, 48, 59, 20, 53,
1353 19, 49, 50, 32 ,31, 6, 11, 10,7,
wangyouqiang7775cbf2023-12-29 16:35:24 +08001354 90, 46, 127,
1355 88, 87, 86, 85, 84, 89,
wangyouqiangece1c722023-12-12 17:28:53 +08001356 };
1357
1358 int test_gpio_0[] = {
1359 35, 36, 42, 41, 21, 22, 24, 23,
1360 54, 125, 18, 13, 14, 17, 16, 5, 4, 2, 12,0,
1361 27, 28, 26, 25, 15, 1, 3,
1362 37, 38,8
wangyouqiang7775cbf2023-12-29 16:35:24 +08001363 };
1364 int test_gpio_2[] = { //GPS组
1365 43,47,
1366 //45,44
wangyouqiangece1c722023-12-12 17:28:53 +08001367 };
1368
wangyouqiang7775cbf2023-12-29 16:35:24 +08001369 int i, j, ret_0, ret_1, ret_2, total_1, total_0, total_2, n = 0;
wangyouqiangece1c722023-12-12 17:28:53 +08001370 int *fail_io = (int *)arg;
1371 total_0 = (sizeof(test_gpio_0)/sizeof(int));
1372 total_1 = (sizeof(test_gpio_1)/sizeof(int));
wangyouqiang7775cbf2023-12-29 16:35:24 +08001373 total_2 = (sizeof(test_gpio_2)/sizeof(int));
wangyouqiangece1c722023-12-12 17:28:53 +08001374
1375 /* [1 all gpio Init] */
1376 gpio_test_init_test_gpio_mode();
1377
1378
1379 /* [2 GPIOTEST] */
wangyouqiangece1c722023-12-12 17:28:53 +08001380 //Test 0
1381 for(i = 0; i < total_0; i++){
1382 ret_0 = gpio_register_test_out_0(test_gpio_0[i], 0);
1383 if(-1 == ret_0){
1384 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_0[i]);
1385 fail_io[n] = test_gpio_0[i];
1386 n++;
1387 }else{
b.liuc7ffd092024-01-03 15:23:07 +08001388 //printf("############gpio [%d] test success############\n", test_gpio_0[i]);
wangyouqiangece1c722023-12-12 17:28:53 +08001389 }
1390 }
1391
1392 //Test 1
1393 for(i = 0; i < total_1; i++){
1394 ret_1 = gpio_register_test_out_1(test_gpio_1[i], 0);
1395 if(-1 == ret_1){
1396 printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_1[i]);
1397 fail_io[n] = test_gpio_1[i];
1398 n++;
1399 }else{
b.liuc7ffd092024-01-03 15:23:07 +08001400 //printf("############gpio [%d] test success############\n", test_gpio_1[i]);
wangyouqiangece1c722023-12-12 17:28:53 +08001401 }
1402 }
wangyouqiang7775cbf2023-12-29 16:35:24 +08001403
1404 //Test 2
1405 for(i = 0; i < total_2; i=i+2){
1406 ret_2 = gpio_register_test_out_2(test_gpio_2[i], test_gpio_2[i+1], 0);
1407 if(-1 == ret_2){
b.liuc7ffd092024-01-03 15:23:07 +08001408 //printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_2[i+1]);
wangyouqiang7775cbf2023-12-29 16:35:24 +08001409 fail_io[n] = test_gpio_2[i];
1410 n++;
1411 }else{
1412 printf("############gpio [%d] test success############\n", test_gpio_2[i+1]);
1413 }
1414 }
1415
wangyouqiangece1c722023-12-12 17:28:53 +08001416 mbtk_adc_close();
1417 return n;
1418}
1419
1420
b.liu4f8e8792023-11-01 23:25:22 +08001421#else
b.liu4f8e8792023-11-01 23:25:22 +08001422int mbtk_at_gpio(void* arg)
1423{
1424 return -1;
1425}
1426
1427#endif