b.liu | d440f9f | 2025-04-18 10:44:31 +0800 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <errno.h> |
| 5 | #include "ql_gpio.h" |
| 6 | #include "mbtk_log.h" |
| 7 | |
| 8 | int gpio_g = -1; |
| 9 | |
| 10 | int main(int argc, char *argv[]) |
| 11 | { |
| 12 | char operator[10]; |
| 13 | int opt; |
| 14 | int ret; |
| 15 | int gpio; |
| 16 | int direction; |
| 17 | int value; |
| 18 | // int pullsel; |
| 19 | |
| 20 | mbtk_log_init("radio", "GPIO_TEST"); |
| 21 | |
| 22 | printf("=========ql gpio main=========\n" |
| 23 | "\t0 exit\n" |
| 24 | "\t1 gpio init\n" |
| 25 | "\t2 gpio uninit\n" |
| 26 | "\t3 gpio set direction\n" |
| 27 | "\t4 gpio set level\n" |
| 28 | "\t5 gpio get level\n" |
| 29 | "operator: >> \n"); |
| 30 | |
| 31 | while(1) |
| 32 | { |
| 33 | memset(operator, 0, sizeof(operator)); |
| 34 | if(NULL != fgets(operator, sizeof(operator), stdin)) |
| 35 | break; |
| 36 | fflush(stdin); |
| 37 | opt = atoi(operator); |
| 38 | switch (opt) |
| 39 | { |
| 40 | case 0: |
| 41 | printf("main exit\n"); |
| 42 | return 0; |
| 43 | case 1: |
| 44 | { |
| 45 | printf("INPUT gpio \n"); |
| 46 | memset(operator, 0, sizeof(operator)); |
| 47 | if(NULL == fgets(operator, sizeof(operator), stdin)) |
| 48 | break; |
| 49 | fflush(stdin); |
| 50 | gpio = atoi(operator); |
| 51 | printf("gpio is %d\n", gpio); |
| 52 | ret = ql_gpio_init(gpio, 1, 1, 0); |
| 53 | if(ret != 0) |
| 54 | { |
| 55 | printf("ql_gpio_init fail\n"); |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | printf("ql_gpio_init success\n"); |
| 60 | gpio_g = gpio; |
| 61 | } |
| 62 | } |
| 63 | break; |
| 64 | case 2: |
| 65 | { |
| 66 | printf(">>>>>gpio uninit\n"); |
| 67 | if (gpio_g != -1) |
| 68 | { |
| 69 | ret = ql_gpio_uninit(gpio_g); |
| 70 | if(ret != 0) |
| 71 | { |
| 72 | printf("ql_gpio_uninit fail\n"); |
| 73 | printf("ret=%d\n", ret); |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | printf("ql_gpio_uninit success\n"); |
| 78 | gpio_g = -1; |
| 79 | } |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | printf(">>>>>pleas gpio init<<<<<\n"); |
| 84 | } |
| 85 | } |
| 86 | break; |
| 87 | case 3: |
| 88 | { |
| 89 | printf(">>>>>Input set direction<<<<<\n"); |
| 90 | memset(operator, 0, sizeof(operator)); |
| 91 | if(NULL == fgets(operator, sizeof(operator), stdin)) |
| 92 | break; |
| 93 | fflush(stdin); |
| 94 | direction = atoi(operator); |
| 95 | if (gpio_g != -1) |
| 96 | { |
| 97 | ret = ql_gpio_set_direction(gpio_g, direction); |
| 98 | if(ret != 0) |
| 99 | { |
| 100 | printf("ql_gpio_set_direction fail\n"); |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | printf("ql_gpio_set_direction success\n"); |
| 105 | } |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | printf(">>>>>pleas gpio init<<<<<\n"); |
| 110 | } |
| 111 | } |
| 112 | break; |
| 113 | case 4: |
| 114 | { |
| 115 | printf(">>>>>Input set level<<<<<\n"); |
| 116 | memset(operator, 0, sizeof(operator)); |
| 117 | if(NULL == fgets(operator, sizeof(operator), stdin)) |
| 118 | break; |
| 119 | fflush(stdin); |
| 120 | value = atoi(operator); |
| 121 | if (gpio_g != -1) |
| 122 | { |
| 123 | ret = ql_gpio_set_level(gpio_g, value); |
| 124 | if(ret < 0) |
| 125 | { |
| 126 | printf("ql_gpio_set_level fail\n"); |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | printf("ql_gpio_set_level success\n"); |
| 131 | } |
| 132 | } |
| 133 | else |
| 134 | { |
| 135 | printf(">>>>>pleas gpio init<<<<<\n"); |
| 136 | } |
| 137 | } |
| 138 | break; |
| 139 | case 5: |
| 140 | { |
| 141 | printf(">>>>>Input get level<<<<<\n"); |
| 142 | if (gpio_g != -1) |
| 143 | { |
| 144 | ret = ql_gpio_get_level(gpio_g); |
| 145 | if(ret < 0) |
| 146 | { |
| 147 | printf("ql_gpio_get_level fail\n"); |
| 148 | printf("ret=%d\n", ret); |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | printf("ql_gpio_get_level success\n"); |
| 153 | printf("ret=%d\n", ret); |
| 154 | } |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | printf(">>>>>pleas gpio init<<<<<\n"); |
| 159 | } |
| 160 | } |
| 161 | break; |
| 162 | |
| 163 | |
| 164 | default: |
| 165 | break; |
| 166 | } |
| 167 | |
| 168 | } |
| 169 | |
| 170 | return 0; |
| 171 | |
| 172 | } |