liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1 | /** |
| 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 | */ |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 10 | #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> |
| 24 | #include "gpio-define.h" |
| 25 | |
| 26 | #define gpio_log(...) if(gpio_debug)printf(__VA_ARGS__) |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 27 | #define HWMAP_DEVICE "/dev/hwmap" |
| 28 | #define PAGE_OFFS_BITS(pgsz) ((unsigned int)(pgsz)-1) |
| 29 | #define PAGE_MASK_BITS(pgsz) (~PAGE_OFFS_BITS(pgsz)) |
| 30 | |
| 31 | typedef enum { |
| 32 | MBTK_ADC0 = 0, /* ADC 0 */ |
| 33 | MBTK_ADC1 /* ADC 1 */ |
| 34 | } mbtk_adc_enum; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 35 | |
| 36 | static int gpio_debug = 0; |
| 37 | |
| 38 | struct gpio_register_function gpio_func_register[128] = { |
| 39 | {GPIO_FUNC_GPIO_00, 0}, |
| 40 | {GPIO_FUNC_GPIO_01, 0}, |
| 41 | {GPIO_FUNC_GPIO_02, 0}, |
| 42 | {GPIO_FUNC_GPIO_03, 0}, |
| 43 | {GPIO_FUNC_GPIO_04, 0}, |
| 44 | {GPIO_FUNC_GPIO_05, 0}, |
| 45 | {GPIO_FUNC_GPIO_06, 0}, |
| 46 | {GPIO_FUNC_GPIO_07, 0}, |
| 47 | {GPIO_FUNC_GPIO_08, 0}, |
| 48 | {GPIO_FUNC_GPIO_09, 0}, |
| 49 | {GPIO_FUNC_GPIO_10, 0}, |
| 50 | {GPIO_FUNC_GPIO_11, 0}, |
| 51 | {GPIO_FUNC_GPIO_12, 0}, |
| 52 | {GPIO_FUNC_GPIO_13, 0}, |
| 53 | {GPIO_FUNC_GPIO_14, 0}, |
| 54 | {GPIO_FUNC_GPIO_15, 0}, |
| 55 | {GPIO_FUNC_GPIO_16, 0}, |
| 56 | {GPIO_FUNC_GPIO_17, 0}, |
| 57 | {GPIO_FUNC_GPIO_18, 0}, |
| 58 | {GPIO_FUNC_GPIO_19, 0}, |
| 59 | {GPIO_FUNC_GPIO_20, 0}, |
| 60 | {GPIO_FUNC_GPIO_21, 0}, |
| 61 | {GPIO_FUNC_GPIO_22, 0}, |
| 62 | {GPIO_FUNC_GPIO_23, 0}, |
| 63 | {GPIO_FUNC_GPIO_24, 0}, |
| 64 | {GPIO_FUNC_GPIO_25, 0}, |
| 65 | {GPIO_FUNC_GPIO_26, 0}, |
| 66 | {GPIO_FUNC_GPIO_27, 0}, |
| 67 | {GPIO_FUNC_GPIO_28, 0}, |
| 68 | {GPIO_FUNC_GPIO_29, 0}, |
| 69 | {GPIO_FUNC_GPIO_30, 0}, |
| 70 | {GPIO_FUNC_GPIO_31, 0}, |
| 71 | {GPIO_FUNC_GPIO_32, 0}, |
| 72 | {GPIO_FUNC_GPIO_33, 0}, |
| 73 | {GPIO_FUNC_GPIO_34, 0}, |
| 74 | {GPIO_FUNC_GPIO_35, 0}, |
| 75 | {GPIO_FUNC_GPIO_36, 0}, |
| 76 | {GPIO_FUNC_MMC1_DAT3, 5}, // GPIO_37 |
| 77 | {GPIO_FUNC_MMC1_DAT2, 5}, // GPIO_38 |
| 78 | {GPIO_FUNC_MMC1_DAT1, 5}, // GPIO_39 |
| 79 | {GPIO_FUNC_MMC1_DAT0, 5}, // GPIO_40 |
| 80 | {GPIO_FUNC_MMC1_CMD, 5}, //GPIO_41 |
| 81 | {GPIO_FUNC_MMC1_CLK, 5}, //GPIO_42 |
| 82 | {GPIO_FUNC_MMC1_CD , 1}, //GPIO_43 |
| 83 | {0,0},{0,0},{0,0},{0,0}, |
| 84 | {GPIO_FUNC_SDIO_DAT3, 1}, //GPIO_48 |
| 85 | {GPIO_FUNC_GPIO_49, 0}, |
| 86 | {GPIO_FUNC_GPIO_50, 0}, |
| 87 | {GPIO_FUNC_GPIO_51, 0}, |
| 88 | {GPIO_FUNC_GPIO_52, 0}, |
| 89 | {GPIO_FUNC_GPIO_53, 0}, |
| 90 | {GPIO_FUNC_GPIO_54, 0}, |
| 91 | {GPIO_FUNC_SDIO_DAT2, 1}, //GPIO_55 |
| 92 | {GPIO_FUNC_SDIO_DAT1, 1}, //GPIO_56 |
| 93 | {GPIO_FUNC_SDIO_DAT0, 1}, //GPIO_57 |
| 94 | {GPIO_FUNC_SDIO_CMD, 1}, //GPIO_58 |
| 95 | {GPIO_FUNC_SDIO_CLK, 1}, //GPIO_59 |
| 96 | {GPIO_FUNC_GPIO_60, 0}, |
| 97 | {0,0},{0,0},{0,0},{0,0}, |
| 98 | {0,0},{0,0}, |
| 99 | {GPIO_FUNC_DVL_0, 1},//GPIO_67 |
| 100 | {GPIO_FUNC_DVL_1, 1},//GPIO_68 |
| 101 | {GPIO_FUNC_GPIO_69, 0}, |
| 102 | {GPIO_FUNC_GPIO_70, 0}, |
| 103 | {GPIO_FUNC_QSPI_DAT3, 1}, //GPIO_71 |
| 104 | {GPIO_FUNC_QSPI_DAT2, 1}, //GPIO_72 |
| 105 | {GPIO_FUNC_QSPI_DAT1, 1}, //GPIO_73 |
| 106 | {GPIO_FUNC_QSPI_DAT0, 1}, //GPIO_74 |
| 107 | {GPIO_FUNC_QSPI_CLK, 1}, //GPIO_75 |
| 108 | {GPIO_FUNC_QSPI_CS1, 1}, //GPIO_76 |
| 109 | {GPIO_FUNC_GPIO_77, 0}, |
| 110 | {GPIO_FUNC_GPIO_78, 0}, |
| 111 | {GPIO_FUNC_GPIO_79, 0}, |
| 112 | {GPIO_FUNC_GPIO_80, 0}, |
| 113 | {GPIO_FUNC_USIM_UCLK, 1},//GPIO_81 |
| 114 | {GPIO_FUNC_USIM_UIO, 1},//GPIO_82 |
| 115 | {GPIO_FUNC_USIM_URSTn, 1},//GPIO_83 |
| 116 | {0,0},{0,0},{0,0},{0,0}, |
| 117 | {0,0},{0,0},{0,0},{0,0}, |
| 118 | {0,0},{0,0},{0,0},{0,0}, |
| 119 | {0,0},{0,0},{0,0}, |
| 120 | {GPIO_FUNC_USB_ID, 1},//GPIO_99 |
| 121 | {0,0},{0,0},{0,0},{0,0}, |
| 122 | {0,0},{0,0},{0,0},{0,0}, |
| 123 | {0,0},{0,0},{0,0},{0,0}, |
| 124 | {0,0},{0,0},{0,0},{0,0}, |
| 125 | {0,0}, |
| 126 | {GPIO_FUNC_PRI_TDI, 1}, //GPIO_117 |
| 127 | {GPIO_FUNC_PRI_TMS, 1}, //GPIO_118 |
| 128 | {GPIO_FUNC_PRI_TCK, 1}, //GPIO_119 |
| 129 | {GPIO_FUNC_PRI_TDO, 1}, //GPIO_120 |
| 130 | {GPIO_FUNC_QSPI_VMODE_GPIO, 1}, //GPIO_121 |
| 131 | {GPIO_FUNC_VBUS_DRV, 1}, //GPIO_122 |
| 132 | {GPIO_FUNC_CLK_REQ, 1}, //GPIO_123 |
| 133 | {0,0}, |
| 134 | {GPIO_FUNC_VCXO_REQ, 1}, //GPIO_125 |
| 135 | {GPIO_FUNC_VCXO_OUT, 1}, //GPIO_126 |
| 136 | {0,0}, |
| 137 | }; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 138 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 139 | |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 140 | static int mbtk_gpio_adc() |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 141 | { |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 142 | int ret = 0; |
| 143 | |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 144 | ret = mbtk_adc_get(0); |
wangyouqiang | 59d8dd2 | 2023-10-19 18:03:54 +0800 | [diff] [blame] | 145 | //printf("ADC_value =%d\n", ret); |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 146 | if(ret >= 500 ){ |
| 147 | ret = 1; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 148 | } |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 149 | else if (ret>=0 && ret<=480){ |
| 150 | ret = 0; |
| 151 | } |
| 152 | else{ |
| 153 | ret = -1; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 154 | } |
| 155 | return ret; |
| 156 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 157 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 158 | |
| 159 | static int hwacc_register(int rw, unsigned int addr, unsigned int *data) |
| 160 | { |
| 161 | int fid; |
| 162 | unsigned int pagesize, len, len_aligned; |
| 163 | unsigned int addr_aligned; |
| 164 | volatile unsigned int *pa; |
| 165 | void *vpa; |
| 166 | |
| 167 | len = pagesize = sysconf(_SC_PAGESIZE); |
| 168 | if((fid = open(HWMAP_DEVICE, O_RDWR)) < 0) |
| 169 | { |
| 170 | printf("Failed to open %s\n", HWMAP_DEVICE); |
| 171 | exit(-1); |
| 172 | } |
| 173 | |
| 174 | // Align the length so the mapped area is page-aligned and contains the requested area |
| 175 | addr_aligned = addr & PAGE_MASK_BITS(pagesize); |
| 176 | len_aligned =((addr + len - addr_aligned) + pagesize - 1) & PAGE_MASK_BITS(pagesize); |
| 177 | |
| 178 | /* Notes on flags: MAP_PRIVATE results in copy on write; MAP_SHARED allows normal write */ |
| 179 | /* MAP_SHARED required O_RDWR in open above, otherwise mmap fails with errno=EACCES */ |
| 180 | /* Notes on prot: PROT_WRITE allows read and write; PROT_READ allows read only */ |
| 181 | /* Notes on off: an unsigned 32-bit value, should be aligned to page size according to mmap manpage */ |
| 182 | if((vpa = mmap(0, len_aligned, PROT_READ|PROT_WRITE, MAP_SHARED, fid, addr_aligned)) == MAP_FAILED) |
| 183 | { |
| 184 | printf("mmap failed (%d)\n", errno); |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | pa = (volatile unsigned int *)((unsigned char *)vpa + (addr & PAGE_OFFS_BITS(pagesize))); |
| 189 | if(rw == 0) |
| 190 | { |
| 191 | *data = *pa; |
| 192 | gpio_log("Value read from 0x%.8x via MVA=0x%p is 0x%.8x\n", addr, pa, *data); |
| 193 | } |
| 194 | else if(rw == 1) |
| 195 | { |
| 196 | *pa = *data; |
| 197 | gpio_log("Value %.8x written to 0x%.8x via MVA=0x%p\n", *data, addr, pa); |
| 198 | #if defined(HWACC_DEBUG) |
| 199 | { |
| 200 | unsigned int val; |
| 201 | val = *pa; |
| 202 | printf("Value read from 0x%.8x via MVA=0x%p is 0x%.8x\n", addr, pa, val); |
| 203 | } |
| 204 | #endif |
| 205 | } |
| 206 | munmap(vpa, len); |
| 207 | } |
| 208 | |
| 209 | close(fid); |
| 210 | return 0; |
| 211 | } |
| 212 | /* |
| 213 | 设置GPIO 模式:第一步读GPIO22默认function |
| 214 | root@OpenWrt:/# hwacc r 0xd401e134 |
| 215 | Option = r Addr = d401e134 |
| 216 | Value read from 0xd401e134 via MVA=0x0xb6fc3134 is 0x0000d040 //默认GPIO功能 |
| 217 | Bit0~bit2值对应上面表格各function,0代表GPIO功能 |
| 218 | */ |
| 219 | static int gpio_register_read(int reg) |
| 220 | { |
| 221 | int ret = -1; |
| 222 | #if 0 |
| 223 | FILE * fp; |
| 224 | // "/bin/hwacc r 0xd401e134"; |
| 225 | char command[36] = {0}; |
| 226 | char buffer[1024]; |
| 227 | int i = 0; |
| 228 | |
| 229 | sprintf(command, "/bin/hwacc r 0x%x", reg); |
| 230 | fp = popen(command, "r"); |
| 231 | while(1) |
| 232 | { |
| 233 | if( fgets (buffer, sizeof(buffer), fp)!=NULL ) { |
| 234 | buffer[strlen(buffer) - 1] = 0; |
| 235 | // gpio_log("out [%d]: %s\n", strlen(buffer), buffer); |
| 236 | }else{ |
| 237 | break; |
| 238 | } |
| 239 | i = strstr_n(buffer, "is"); |
| 240 | if(i) |
| 241 | { |
| 242 | ret = str_to_hex(&buffer[i + 2]); |
| 243 | gpio_log("read 0x%x value:%s, %x\n", reg, &buffer[i + 2], ret); |
| 244 | } |
| 245 | } |
| 246 | pclose(fp); |
| 247 | #else |
| 248 | hwacc_register(0, reg, &ret); |
| 249 | #endif |
| 250 | return ret; |
| 251 | } |
| 252 | /* |
| 253 | 设置输入输出状态,设置PDR寄存器GPIO22为output |
| 254 | root@OpenWrt:/# hwacc w 0xD401900c 0x00c03800 |
| 255 | Option = w Addr = d401900c Data=00c03800 |
| 256 | Value 00c03800 written to 0xd401900c via MVA=0x0xb6f9f00c |
| 257 | */ |
| 258 | static void gpio_register_write(int reg, int value) |
| 259 | { |
| 260 | #if 0 |
| 261 | FILE * fp; |
| 262 | // "/bin/hwacc w 0xD401900c 0x00c03800" |
| 263 | char command[36] = {0}; |
| 264 | char buffer[1024]; |
| 265 | |
| 266 | sprintf(command, "/bin/hwacc w 0x%x 0x%x", reg, value); |
| 267 | gpio_log("command: %s\n", command); |
| 268 | fp = popen(command, "r"); |
| 269 | while(1) |
| 270 | { |
| 271 | if( fgets (buffer, sizeof(buffer), fp)!=NULL ) { |
| 272 | gpio_log("%s\n", buffer); |
| 273 | }else{ |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | pclose(fp); |
| 278 | |
| 279 | #else |
wangyouqiang | 4c262fa | 2023-10-17 17:48:03 +0800 | [diff] [blame] | 280 | usleep(20); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 281 | hwacc_register(1, reg, &value); |
wangyouqiang | 4c262fa | 2023-10-17 17:48:03 +0800 | [diff] [blame] | 282 | usleep(20); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 283 | #endif |
| 284 | } |
| 285 | /* |
| 286 | AF SEL<p>This field is used for alternate function selection for a pin. |
| 287 | It selects between the eight possible alternate functions for the pin. |
| 288 | Alternate function 0 is always the reset case. |
| 289 | |
| 290 | <p>0x0 = Alternate function 0 (primary function at reset) |
| 291 | <p>0x1 = Alternate function 1 |
| 292 | <p>0x2 = Alternate function 2 |
| 293 | <p>0x3 = Alternate function 3 |
| 294 | <p>0x4 = Alternate function 4 |
| 295 | <p>0x5 = Alternate function 5 |
| 296 | <p>0x6 = Alternate function 6 |
| 297 | <p>0x7 = Alternate function 7 |
| 298 | */ |
| 299 | static int gpio_register_set_func_0(int port) |
| 300 | { |
| 301 | int ret; |
| 302 | struct gpio_register_function *reg = NULL; |
| 303 | if(port > 128) |
| 304 | return -1; |
| 305 | reg = &gpio_func_register[port]; |
| 306 | if(0 == reg->reg) |
| 307 | return -1; |
| 308 | ret = gpio_register_read(reg->reg); |
| 309 | if((ret & 0x7) != reg->func_gpio) |
| 310 | { |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 311 | //printf("Gpio set func [%d] [0x%x]!\n", reg->func_gpio, (ret & 0xfffffff8) | reg->func_gpio); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 312 | gpio_register_write(reg->reg, (ret & 0xfffffff8) | reg->func_gpio); |
| 313 | } |
| 314 | return 0; |
| 315 | } |
| 316 | /* |
| 317 | 设置GPIO 方向 |
| 318 | 读取输入输出状态,读PDR寄存器:0x0c |
| 319 | root@OpenWrt:/# hwacc r 0xD401900c |
| 320 | Option = r Addr = d401900c |
| 321 | Value read from 0xd401900c via MVA=0x0xb6f3900c is 0x00803800 //bit22为0,代表Input |
| 322 | */ |
| 323 | static void gpio_register_set_direction(int port, int dir) |
| 324 | { |
| 325 | int ret; |
| 326 | int reg = 0xD4019000; |
| 327 | |
| 328 | if(port > (32 - 1)) |
| 329 | reg = 0xD4019004; |
| 330 | if(port > (32 * 2 - 1)) |
| 331 | reg = 0xD4019008; |
| 332 | if(port > (32 * 3 - 1)) |
| 333 | reg = 0xD4019100; |
| 334 | |
| 335 | reg += 0x0c; |
| 336 | port = port % 0x20; |
| 337 | ret = gpio_register_read(reg); |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 338 | //printf("[Direction] reg_value=%x\n", ret); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 339 | // 设置 输出 0 && 1 |
| 340 | if(!(ret & (0x1 << port)) && dir) |
| 341 | { |
| 342 | gpio_register_write(reg, ret | (0x1 << port)); |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 343 | //printf("[Direction out] reg_value=%x\n", gpio_register_read(reg)); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 344 | } |
| 345 | // 设置 输入 1 && 0 |
| 346 | if((ret & (0x1 << port)) && !dir) |
| 347 | { |
| 348 | gpio_register_write(reg, ret | !(0x1 << port)); |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 349 | //printf("[Direction in] reg_value=%x\n", gpio_register_read(reg)); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | /* |
| 353 | 设置GPIO 输出电平 |
| 354 | |
| 355 | 读取电平状态,先读PLR寄存器:0x00 |
| 356 | root@OpenWrt:/# hwacc r 0xD4019000 |
| 357 | Option = r Addr = d4019000 |
| 358 | Value read from 0xd4019000 via MVA=0x0xb6f1c000 is 0x81e82a30 |
| 359 | 对应下面具体BIT |
| 360 | 1000 0001 1110 1000 0010 1010 0011 0000 BIT22默认电平高 |
| 361 | |
| 362 | 设置输出高:设置PSR寄存器:0x18(只写寄存器) |
| 363 | root@OpenWrt:/# hwacc w 0xD4019018 0x400000 |
| 364 | Option = w Addr = d4019018 Data=00400000 |
| 365 | Value 00400000 written to 0xd4019018 via MVA=0x0xb6f56018 //bit22写1,输出高 |
| 366 | |
| 367 | 设置输出低:设置PCR寄存器:0x24 |
| 368 | root@OpenWrt:/# hwacc w 0xD4019024 0x400000 |
| 369 | Option = w Addr = d4019024 Data=00400000 |
| 370 | Value 00400000 written to 0xd4019024 via MVA=0x0xb6faa024 //Bit22写1,GPIO22输出低 |
| 371 | |
| 372 | */ |
| 373 | static void gpio_register_set_value(int port, int value) |
| 374 | { |
| 375 | int ret; |
| 376 | int reg = 0xD4019000; |
| 377 | |
| 378 | if(port > (32 - 1)) |
| 379 | reg = 0xD4019004; |
| 380 | if(port > (32 * 2 - 1)) |
| 381 | reg = 0xD4019008; |
| 382 | if(port > (32 * 3 - 1)) |
| 383 | reg = 0xD4019100; |
| 384 | |
| 385 | if(value) |
| 386 | { |
| 387 | reg += 0x18; |
| 388 | } |
| 389 | else |
| 390 | reg += 0x24; |
| 391 | |
| 392 | port = port % 0x20; |
| 393 | ret = gpio_register_read(reg); |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 394 | //printf("[Value] reg_value=%x\n", gpio_register_read(0xD4019004)); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 395 | // 设置 高电平 0 && 1 |
| 396 | if(value) |
| 397 | { |
| 398 | gpio_register_write(reg, ret | (0x1 << port)); |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 399 | //printf("[Value high] reg_value=%x\n", gpio_register_read(0xD4019004)); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 400 | return; |
| 401 | } |
| 402 | // 设置 低电平 1 && 0 |
| 403 | if(!(ret & (0x1 << port)) && !value) |
| 404 | { |
| 405 | gpio_register_write(reg, ret | (0x1 << port)); |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 406 | //printf("[Value low] reg_value=%x\n", gpio_register_read(0xD4019004)); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | /* |
| 410 | 读取电平状态,先读PLR寄存器:0x00 |
| 411 | root@OpenWrt:/# hwacc r 0xD4019000 |
| 412 | Option = r Addr = d4019000 |
| 413 | Value read from 0xd4019000 via MVA=0x0xb6f1c000 is 0x81e82a30 |
| 414 | 对应下面具体BIT |
| 415 | 1000 0001 1110 1000 0010 1010 0011 0000 BIT22默认电平高 |
| 416 | */ |
| 417 | static int gpio_register_get_value(int port) |
| 418 | { |
| 419 | int ret = -1; |
| 420 | int reg = 0xD4019000; |
| 421 | |
| 422 | if(port > (32 - 1)) |
| 423 | reg = 0xD4019004; |
| 424 | if(port > (32 * 2 - 1)) |
| 425 | reg = 0xD4019008; |
| 426 | if(port > (32 * 3 - 1)) |
| 427 | reg = 0xD4019100; |
| 428 | port = port % 0x20; |
| 429 | ret = gpio_register_read(reg); |
| 430 | if(ret & (0x1 << port)) |
| 431 | { |
| 432 | return 1; |
| 433 | } |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | void gpio_debug_set(int enable) |
| 438 | { |
| 439 | gpio_debug = enable; |
| 440 | } |
| 441 | |
| 442 | int gpio_register_test_out(int port, int value) |
| 443 | { |
| 444 | int ret; |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 445 | int i; |
| 446 | int valueh = 0; |
| 447 | int valuel = 1; |
| 448 | |
wangyouqiang | 4c262fa | 2023-10-17 17:48:03 +0800 | [diff] [blame] | 449 | //printf("Gpio port [%d] test start!\n", port); |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 450 | ret = gpio_register_set_func_0(port); //设功能为GPIO |
| 451 | if(ret){ |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 452 | printf("gpio_port can't support!\n"); |
| 453 | return -1; |
| 454 | } |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 455 | gpio_register_set_direction(port, 1); //设方向为输出 |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 456 | ret = gpio_register_get_value(port); |
wangyouqiang | 4c262fa | 2023-10-17 17:48:03 +0800 | [diff] [blame] | 457 | //printf("gpio default value is : %d.\n", ret); |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 458 | |
| 459 | |
| 460 | //[High] |
| 461 | for(i = 0; i <= 10; i++){ |
| 462 | gpio_register_set_value(port, 1); |
| 463 | usleep(50); |
| 464 | valueh = gpio_register_get_value(port); |
wangyouqiang | 4c262fa | 2023-10-17 17:48:03 +0800 | [diff] [blame] | 465 | //printf("set high? %d\n",valueh); |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 466 | if(1 == valueh){ |
| 467 | break; |
| 468 | } |
| 469 | } |
wangyouqiang | 59d8dd2 | 2023-10-19 18:03:54 +0800 | [diff] [blame] | 470 | //usleep(10000); |
| 471 | for(i = 0; i <= 35; i++){ |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 472 | ret = mbtk_gpio_adc(); |
wangyouqiang | 59d8dd2 | 2023-10-19 18:03:54 +0800 | [diff] [blame] | 473 | usleep(3000); |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 474 | if(1 == ret){ |
| 475 | break; |
| 476 | } |
| 477 | } |
wangyouqiang | 4c262fa | 2023-10-17 17:48:03 +0800 | [diff] [blame] | 478 | //printf("------gpio should is high: %d.------\n", ret); |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 479 | if(1 != ret){ |
| 480 | ret=-1; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 481 | goto exit; |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 482 | } |
wangyouqiang | 59d8dd2 | 2023-10-19 18:03:54 +0800 | [diff] [blame] | 483 | printf("******gpio should is high: %d.******\n", ret); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 484 | |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 485 | |
| 486 | //[Low] |
| 487 | usleep(200); |
| 488 | for(i = 0; i <= 10; i++){ |
| 489 | gpio_register_set_value(port, 0); //输出低 |
b.liu | 074771a | 2023-10-19 13:13:39 +0800 | [diff] [blame] | 490 | usleep(50); |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 491 | valuel = gpio_register_get_value(port); |
wangyouqiang | 59d8dd2 | 2023-10-19 18:03:54 +0800 | [diff] [blame] | 492 | //printf("set low? %d\n",valuel); |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 493 | if(0 == valuel){ |
| 494 | break; |
| 495 | } |
| 496 | } |
wangyouqiang | 59d8dd2 | 2023-10-19 18:03:54 +0800 | [diff] [blame] | 497 | //usleep(10000); |
| 498 | for(i = 0; i <= 35; i++){ |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 499 | ret = mbtk_gpio_adc(); |
wangyouqiang | 59d8dd2 | 2023-10-19 18:03:54 +0800 | [diff] [blame] | 500 | usleep(3000); |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 501 | if(0 == ret){ |
| 502 | break; |
| 503 | } |
| 504 | } |
b.liu | 074771a | 2023-10-19 13:13:39 +0800 | [diff] [blame] | 505 | printf("******gpio should is low: %d.******\n", ret); |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 506 | if(0 != ret){ |
| 507 | ret=-1; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 508 | goto exit; |
| 509 | } |
| 510 | |
| 511 | exit: |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 512 | //gpio_register_set_direction(port, 0); //设方向为输入 |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 513 | return ret; |
| 514 | } |
| 515 | |
| 516 | int mbtk_at_gpio(void* arg) |
| 517 | { |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 518 | /* |
| 519 | error:53 |
| 520 | */ |
| 521 | int test_gpio[] = |
| 522 | { |
| 523 | 56, 55, 58, 57, 48, 59, 12, 20, 5, 43, 21, |
| 524 | 49, 50, 27, 28, 26, 25, 16, 17, 15, 126, 125, |
| 525 | 4, 0, 3, 18, 7, 6, 1, 2, 54, 19, 13, |
| 526 | 32, 31, 23, 24, 22, 122, 33, 35, 36, 34, 14, |
| 527 | 99,/*53,*/37,38,39,40,41,42}; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 528 | |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 529 | int i, j, ret, total, n = 0; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 530 | int *fail_io = (int *)arg; |
| 531 | total = (sizeof(test_gpio)/sizeof(int)); |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 532 | //printf("Start test gpio total: %d\n", total); |
| 533 | |
| 534 | /* [1 all gpio set to low] */ |
| 535 | for(j = 0; j < 6 ; j++){ |
| 536 | for(i = 0; i < total; i++){ |
| 537 | gpio_register_set_func_0(test_gpio[i]); |
| 538 | gpio_register_set_direction(test_gpio[i], 1); |
| 539 | gpio_register_set_value(test_gpio[i], 0); |
| 540 | } |
wangyouqiang | 59d8dd2 | 2023-10-19 18:03:54 +0800 | [diff] [blame] | 541 | //usleep(10000); |
| 542 | for(i = 0; i <= 35; i++){ |
b.liu | 074771a | 2023-10-19 13:13:39 +0800 | [diff] [blame] | 543 | ret = mbtk_gpio_adc(); |
wangyouqiang | 59d8dd2 | 2023-10-19 18:03:54 +0800 | [diff] [blame] | 544 | usleep(3000); |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 545 | if(0 == ret){ |
| 546 | break; |
| 547 | } |
| 548 | } |
b.liu | 074771a | 2023-10-19 13:13:39 +0800 | [diff] [blame] | 549 | printf("pre set ADC: %d, times: %d\n",ret, j); |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 550 | if(0 == ret){ |
| 551 | break; |
| 552 | } |
| 553 | } |
wangyouqiang | 4c262fa | 2023-10-17 17:48:03 +0800 | [diff] [blame] | 554 | if(0 != ret){ |
| 555 | printf("Fail, please retest"); |
| 556 | return -1; |
| 557 | } |
| 558 | |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 559 | |
| 560 | /* [2 GPIOTEST] */ |
| 561 | for(i = 0; i < total; i++){ |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 562 | ret = gpio_register_test_out(test_gpio[i], 0); |
wangyouqiang | 7850b4a | 2023-09-18 15:47:57 +0800 | [diff] [blame] | 563 | if(-1 == ret){ |
| 564 | printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio[i]); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 565 | fail_io[n] = test_gpio[i]; |
| 566 | n++; |
| 567 | }else{ |
wangyouqiang | 4c262fa | 2023-10-17 17:48:03 +0800 | [diff] [blame] | 568 | //printf("############gpio [%d] test success############\n", test_gpio[i]); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 569 | } |
| 570 | } |
wangyouqiang | 2b352b2 | 2023-10-21 15:18:43 +0800 | [diff] [blame] | 571 | mbtk_adc_close(); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 572 | return n; |
| 573 | } |