1 /*
2  * Copyright (c) 2006-2022, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2018-02-08     Zhangyihong  the first version
9  * 2018-04-03     XY           gt9xx for 1024 * 600
10  * 2018-04-14     liu2guang    optimize int and rst to pin framework
11  * 2017-08-08     XY           imxrt1052
12  * 2018-10-29     XY
13  */
14 #include <rtthread.h>
15 #include <rtdevice.h>
16 #include "drv_touch.h"
17 #include "string.h"
18 #include "drv_pin.h"
19 
20 #define DBG_TAG "TOUCH.gt9xx"
21 #define DBG_LVL DBG_LOG
22 #include <rtdbg.h>
23 
24 // extern tcon_panel_t _panel;
25 #if 0
26 #define TP_INT_PIN GET_PIN(GPIO_PORT_E, GPIO_PIN_10) /* GPIO_PORT_E GPIO_PIN_10 */
27 #define TP_RST_PIN GET_PIN(GPIO_PORT_E, GPIO_PIN_11) /* GPIO_PORT_E GPIO_PIN_11 */
28 #else
29 #ifdef BSP_USING_MANGOPI // mango board
30 #define TP_INT_PIN GET_PIN(GPIO_PORT_D, GPIO_PIN_22) /* GPIO_PORT_D GPIO_PIN_22 */
31 #define TP_RST_PIN GET_PIN(GPIO_PORT_G, GPIO_PIN_11) /* GPIO_PORT_G GPIO_PIN_11 */
32 #elif defined(BSP_USING_M7)
33 #define TP_INT_PIN GET_PIN(GPIO_PORT_G, GPIO_PIN_14) /* GPIO_PORT_G GPIO_PIN_14 */
34 #define TP_RST_PIN GET_PIN(GPIO_PORT_G, GPIO_PIN_12) /* GPIO_PORT_G GPIO_PIN_12 */
35 #endif
36 #endif
37 
38 #ifndef TP_INT_PIN
39 #error "Please config touch panel INT pin."
40 #endif
41 #ifndef TP_RST_PIN
42 #error "Please config touch panel RST pin."
43 #endif
44 
45 #ifndef IIC_RETRY_NUM
46 #define IIC_RETRY_NUM 2
47 #endif
48 
49 #define GT9xx_Read_Interval         (20)        /* gt9xx 读取坐标最小间隔时间 */
50 
51 /* Either 0x5D or 0x14 could be used as the address */
52 #define GT9xx_TS_ADDR1 (0x14)
53 #define GT9xx_TS_ADDR2 (0x5D)
54 static uint8_t GT9xx_TS_ADDR = GT9xx_TS_ADDR1;
55 
56 #define gt9xx_READ_XY_REG (0x814E)          /* 坐标寄存器       */
57 #define gt9xx_CLEARBUF_REG (0x814E)         /* 清除坐标寄存器   */
58 #define gt9xx_CONFIG_REG (0x8047)           /* 配置参数寄存器   */
59 #define gt9xx_COMMAND_REG (0x8040)          /* 实时命令         */
60 #define gt9xx_PRODUCT_ID_REG (0x8140)       /* 产品ID           */
61 #define gt9xx_VENDOR_ID_REG (0x814A)        /* 当前模组选项信息 */
62 #define gt9xx_CONFIG_VERSION_REG (0x8047)   /* 配置文件版本号   */
63 #define gt9xx_CONFIG_CHECKSUM_REG (0x80FF)  /* 配置文件校验码   */
64 #define gt9xx_FIRMWARE_VERSION_REG (0x8144) /* 固件版本号       */
65 
66 static struct touch_driver gt9xx_driver;
67 
gt9xx_hw_reset(rt_uint8_t address)68 void gt9xx_hw_reset(rt_uint8_t address)
69 {
70     rt_tick_t delay = rt_tick_from_millisecond(30);
71 
72     rt_pin_mode(TP_RST_PIN, PIN_MODE_OUTPUT);
73     rt_pin_mode(TP_INT_PIN, PIN_MODE_OUTPUT);
74 
75     if (address == 0x5D)
76     {
77         rt_pin_write(TP_RST_PIN, PIN_LOW);
78         rt_pin_write(TP_INT_PIN, PIN_LOW);
79         rt_thread_delay(delay);
80         rt_pin_write(TP_RST_PIN, PIN_HIGH);
81         rt_pin_write(TP_INT_PIN, PIN_LOW);
82         rt_thread_delay(delay);
83         rt_pin_write(TP_INT_PIN, PIN_LOW);
84         rt_thread_delay(delay);
85         rt_pin_write(TP_INT_PIN, PIN_HIGH);
86     }
87     else
88     {
89         rt_pin_write(TP_RST_PIN, PIN_LOW);
90         rt_pin_write(TP_INT_PIN, PIN_HIGH);
91         rt_thread_delay(delay);
92         rt_pin_write(TP_RST_PIN, PIN_HIGH);
93         rt_pin_write(TP_INT_PIN, PIN_HIGH);
94         rt_thread_delay(delay);
95         rt_pin_write(TP_INT_PIN, PIN_LOW);
96         rt_thread_delay(delay);
97         rt_pin_write(TP_INT_PIN, PIN_HIGH);
98     }
99 
100     rt_thread_mdelay(5); /* more than 5ms */
101 }
102 
gt9xx_probe(struct rt_i2c_bus_device * i2c_bus)103 static rt_bool_t gt9xx_probe(struct rt_i2c_bus_device *i2c_bus)
104 {
105     rt_uint8_t cmd[2];
106     rt_uint8_t buffer[5] = {0};
107 
108     GT9xx_TS_ADDR = GT9xx_TS_ADDR1;
109     gt9xx_hw_reset(GT9xx_TS_ADDR);
110     rt_thread_delay(RT_TICK_PER_SECOND / 5);
111 
112     cmd[0] = (rt_uint8_t)((gt9xx_PRODUCT_ID_REG >> 8) & 0xFF);
113     cmd[1] = (rt_uint8_t)(gt9xx_PRODUCT_ID_REG & 0xFF);
114     if (rt_touch_read(GT9xx_TS_ADDR1, &cmd, 2, buffer, 4) != 0)
115     {
116         if (rt_touch_read(GT9xx_TS_ADDR2, &cmd, 2, buffer, 4) != 0)
117         {
118             LOG_E("Failed to fetch GT9XX ID at the address 0x%x/0x%x", GT9xx_TS_ADDR1, GT9xx_TS_ADDR2);
119             return RT_FALSE;
120         }
121         GT9xx_TS_ADDR = GT9xx_TS_ADDR2;
122     }
123 
124     buffer[4] = '\0';
125 
126     LOG_D("%#X %#X %#X %#X %#X", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4]);
127 
128     if (!rt_strcmp((const char *)buffer, "911"))
129     {
130         LOG_I("Found chip gt911");
131         return RT_TRUE;
132     }
133     else if (!rt_strcmp((const char *)buffer, "928"))
134     {
135         LOG_I("Found chip gt928");
136         return RT_TRUE;
137     }
138     else if (!rt_strcmp((const char *)buffer, "9147"))
139     {
140         LOG_I("Found chip gt9147");
141         return RT_TRUE;
142     }
143     else if (!rt_strcmp((const char *)buffer, "9157"))
144     {
145         LOG_I("Found chip gt9157");
146         return RT_TRUE;
147     }
148     else
149     {
150         LOG_E("Uknow chip gt9xx device: [%s]", buffer);
151     }
152 
153     return RT_FALSE;
154 }
155 
gt9xx_init(struct rt_i2c_bus_device * i2c_bus)156 static void gt9xx_init(struct rt_i2c_bus_device *i2c_bus)
157 {
158     rt_uint8_t buf = 0;
159     rt_uint8_t cmd[2];
160 
161     cmd[0] = (rt_uint8_t)((gt9xx_CONFIG_VERSION_REG >> 8) & 0xFF);
162     cmd[1] = (rt_uint8_t)(gt9xx_CONFIG_VERSION_REG & 0xFF);
163     rt_touch_read(GT9xx_TS_ADDR, &cmd, 2, &buf, 1);
164     LOG_I("GT9xx Config version: 0x%02X", buf);
165 
166     cmd[0] = (rt_uint8_t)((gt9xx_VENDOR_ID_REG >> 8) & 0xFF);
167     cmd[1] = (rt_uint8_t)(gt9xx_VENDOR_ID_REG & 0xFF);
168     rt_touch_read(GT9xx_TS_ADDR, &cmd, 2, &buf, 1);
169     LOG_I("GT9xx Sensor id: 0x%02X", buf);
170 }
171 
gt9xx_deinit(void)172 static void gt9xx_deinit(void)
173 {
174 
175 }
176 
gt9xx_read_point(struct rt_touch_data * touch_data,rt_size_t touch_num)177 static rt_err_t gt9xx_read_point(struct rt_touch_data *touch_data, rt_size_t touch_num)
178 {
179     rt_uint8_t cmd[2];
180     rt_uint8_t buf[8] = {0};
181     static rt_uint8_t s_tp_down = 0;
182 
183     cmd[0] = (rt_uint8_t)((gt9xx_READ_XY_REG >> 8) & 0xFF);
184     cmd[1] = (rt_uint8_t)(gt9xx_READ_XY_REG & 0xFF);
185     rt_touch_read(GT9xx_TS_ADDR, &cmd, 2, buf, 8);
186 
187     if ((buf[0] & 0x01) == 0)
188     {
189         if (s_tp_down)
190         {
191             s_tp_down = 0;
192             touch_data->event = RT_TOUCH_EVENT_UP;
193         }
194         else
195         {
196             touch_data->event = RT_TOUCH_EVENT_NONE;
197         }
198     }
199     else
200     {
201         touch_data->x_coordinate = ((rt_uint16_t)buf[3] << 8) | buf[2];
202         touch_data->y_coordinate = ((rt_uint16_t)buf[5] << 8) | buf[4];
203 
204         // rt_kprintf("X:%d    Y:%d\n", touch_data->x_coordinate, touch_data->y_coordinate);
205 
206         if (s_tp_down)
207         {
208             touch_data->event = RT_TOUCH_EVENT_MOVE;
209             // rt_kprintf("s_tp_down\n");
210         }
211         else
212         {
213             touch_data->event = RT_TOUCH_EVENT_DOWN;
214             s_tp_down = 1;
215         }
216     }
217 
218     buf[0] = ((gt9xx_CLEARBUF_REG >> 8) & 0xFF);
219     buf[1] = (gt9xx_CLEARBUF_REG & 0xFF);
220     buf[2] = 0x00;
221     rt_touch_write(GT9xx_TS_ADDR, buf, 3);
222 
223     return RT_EOK;
224 }
225 
226 struct touch_ops gt9xx_ops =
227 {
228     .init = gt9xx_init,
229     .deinit = gt9xx_deinit,
230     .read_point = gt9xx_read_point,
231 };
232 
gt9xx_driver_register(void)233 int gt9xx_driver_register(void)
234 {
235     gt9xx_driver.probe = gt9xx_probe;
236     gt9xx_driver.ops = &gt9xx_ops;
237     gt9xx_driver.read_interval = rt_tick_from_millisecond(GT9xx_Read_Interval);
238     gt9xx_driver.check_mode = TOUCH_INT_MODE;
239     gt9xx_driver.user_data = RT_NULL;
240 
241     rt_touch_drivers_register(&gt9xx_driver);
242 
243     return RT_EOK;
244 }
245 INIT_DEVICE_EXPORT(gt9xx_driver_register);
246