1 /*
2 * Copyright (c) 2021 GOODIX.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "gpio_if.h"
17 #include "driver_test.h"
18
19 #define GPIO_LED1 4
20 #define GPIO_LED2 44
21 #define GPIO_BUTTON_UP 12
22 #define GPIO_BUTTON_DOWN 13
23 #define GPIO_BUTTON_LEFT 14
24 #define GPIO_BUTTON_RIGH 15
25 #define GPIO_READ_MAX 10
26 #define MS_1000 1000
27 #define IS_ODD 2
28
gpio_output_test(void)29 static void gpio_output_test(void)
30 {
31 static int cnt = 0;
32
33 LOG_I("set gpio %d output\r\n", GPIO_LED1);
34 GpioSetDir(GPIO_LED1, GPIO_DIR_OUT);
35 GpioSetDir(GPIO_LED2, GPIO_DIR_OUT);
36
37 if ((cnt++) % IS_ODD) { // check cnt is odd number
38 LOG_I("set led on\r\n");
39 GpioWrite(GPIO_LED1, GPIO_VAL_LOW);
40 GpioWrite(GPIO_LED2, GPIO_VAL_HIGH);
41 } else {
42 LOG_I("set led of\r\n");
43 GpioWrite(GPIO_LED1, GPIO_VAL_HIGH);
44 GpioWrite(GPIO_LED2, GPIO_VAL_LOW);
45 }
46 }
47
gpio_input_test(void)48 static void gpio_input_test(void)
49 {
50 int cnt = 0;
51 uint16_t val = 1;
52
53 GpioSetDir(GPIO_BUTTON_UP, GPIO_DIR_IN);
54 LOG_I("please push up key: >>> ");
55 while (cnt++ < GPIO_READ_MAX) {
56 GpioRead(GPIO_BUTTON_UP, &val);
57 if (val == 0) {
58 LOG_I("up key\r\n");
59 break;
60 }
61
62 osDelay(MS_1000);
63 }
64
65 if (val) {
66 LOG_I("none key\r\n");
67 }
68 }
69
gpio_irq_handler(uint16_t gpio,uint8_t * data)70 static int32_t gpio_irq_handler(uint16_t gpio, uint8_t *data)
71 {
72 LOG_I("%s enter, gpio = %d, data = %d\r\n", __func__, gpio, *(int *)data);
73 return 0;
74 }
75
gpio_irq_test(void)76 static void gpio_irq_test(void)
77 {
78 static bool enable = false;
79 static int val = 1234;
80
81 if (enable == false) {
82 enable = true;
83 GpioSetDir(GPIO_BUTTON_LEFT, GPIO_DIR_IN);
84 GpioSetIrq(GPIO_BUTTON_LEFT, GPIO_IRQ_TRIGGER_RISING, (GpioIrqFunc)gpio_irq_handler, &val);
85 GpioEnableIrq(GPIO_BUTTON_LEFT);
86 LOG_I("Set key_left (gpio %d) as interrupt function\r\n", GPIO_BUTTON_LEFT);
87 }
88 }
89
gpio_test(void)90 void gpio_test(void)
91 {
92 gpio_output_test();
93 gpio_input_test();
94 gpio_irq_test();
95 }