summaryrefslogtreecommitdiff
path: root/bsp/loongson/ls1cdev/drivers/drv_touch_gt9xx.c
blob: 801ec762cbc3ef7fcff9b4cd36a2cee541182958 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-02-08     Zhangyihong  the first version
 * 2018-04-03     XY           gt9xx for 1024 * 600
 * 2018-04-14     liu2guang    optimize int and rst to pin framework
 * 2017-08-08     XY           imxrt1052
 * 2018-10-29     XY
 */

#include "drv_touch.h"
#include "string.h"

#ifdef TINA_USING_TOUCH

#define TP_INT_PIN 89
#define TP_RST_PIN 87

#ifndef TP_INT_PIN
#error "Please config touch panel INT pin."
#endif
#ifndef TP_RST_PIN
#error "Please config touch panel RST pin."
#endif

#ifndef IIC_RETRY_NUM
#define IIC_RETRY_NUM 2
#endif

#define GT9xx_TS_ADDR               (0x5D)

#define gt9xx_READ_XY_REG           (0x814E)    /* 坐标寄存器       */
#define gt9xx_CLEARBUF_REG          (0x814E)    /* 清除坐标寄存器   */
#define gt9xx_CONFIG_REG            (0x8047)    /* 配置参数寄存器   */
#define gt9xx_COMMAND_REG           (0x8040)    /* 实时命令         */
#define gt9xx_PRODUCT_ID_REG        (0x8140)    /* 产品ID           */
#define gt9xx_VENDOR_ID_REG         (0x814A)    /* 当前模组选项信息 */
#define gt9xx_CONFIG_VERSION_REG    (0x8047)    /* 配置文件版本号   */
#define gt9xx_CONFIG_CHECKSUM_REG   (0x80FF)    /* 配置文件校验码   */
#define gt9xx_FIRMWARE_VERSION_REG  (0x8144)    /* 固件版本号       */

#if 0
#define TPDEBUG     rt_kprintf
#else
#define TPDEBUG(...)
#endif

static struct touch_driver gt9xx_driver;

void gt9xx_hw_reset(rt_uint8_t address)
{
    rt_tick_t delay = rt_tick_from_millisecond(30);

    rt_pin_mode(TP_RST_PIN, PIN_MODE_OUTPUT);
    rt_pin_mode(TP_INT_PIN, PIN_MODE_OUTPUT);

    if (address == 0x5D)
    {
        rt_pin_write(TP_RST_PIN, PIN_LOW);
        rt_pin_write(TP_INT_PIN, PIN_LOW);
        rt_thread_delay(delay);
        rt_pin_write(TP_RST_PIN, PIN_HIGH);
        rt_pin_write(TP_INT_PIN, PIN_LOW);
        rt_thread_delay(delay);
        rt_pin_write(TP_INT_PIN, PIN_LOW);
        rt_thread_delay(delay);
        rt_pin_write(TP_INT_PIN, PIN_HIGH);
    }
    else
    {
        rt_pin_write(TP_RST_PIN, PIN_LOW);
        rt_pin_write(TP_INT_PIN, PIN_HIGH);
        rt_thread_delay(delay);
        rt_pin_write(TP_RST_PIN, PIN_HIGH);
        rt_pin_write(TP_INT_PIN, PIN_HIGH);
        rt_thread_delay(delay);
        rt_pin_write(TP_INT_PIN, PIN_LOW);
        rt_thread_delay(delay);
        rt_pin_write(TP_INT_PIN, PIN_HIGH);
    }
}

static void gt9xx_soft_reset(struct rt_i2c_bus_device *i2c_bus)
{
    rt_uint8_t buf[3];

    buf[0] = (rt_uint8_t)((gt9xx_COMMAND_REG >> 8) & 0xFF);
    buf[1] = (rt_uint8_t)(gt9xx_COMMAND_REG & 0xFF);
    buf[2] = 0x02;
    rt_touch_write(GT9xx_TS_ADDR, buf, 3);
}

static rt_bool_t gt9xx_probe(struct rt_i2c_bus_device *i2c_bus)
{
    rt_uint8_t cmd[2];
    rt_uint8_t buffer[5] = {0};

    gt9xx_hw_reset(GT9xx_TS_ADDR);
    //gt9xx_soft_reset(i2c_bus);
    rt_thread_delay(RT_TICK_PER_SECOND / 5);

    cmd[0] = (rt_uint8_t)((gt9xx_PRODUCT_ID_REG >> 8) & 0xFF);
    cmd[1] = (rt_uint8_t)(gt9xx_PRODUCT_ID_REG & 0xFF);
    if (rt_touch_read(GT9xx_TS_ADDR, &cmd, 2, buffer, 4) != 0)
    {
        TPDEBUG("[TP] %s failed!\n", __func__);
        return RT_FALSE;
    }

    buffer[4] = '\0';

    TPDEBUG("%#X %#X %#X %#X %#X\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4]);

    if(!rt_strcmp((const char*)buffer, "911"))
    {
        rt_kprintf("[TP] Found chip gt911\n");
        return RT_TRUE;
    }
    else if(!rt_strcmp((const char*)buffer, "9147"))
    {
        rt_kprintf("[TP] Found chip gt9147\n");
        return RT_TRUE;
    }
    else if(!rt_strcmp((const char*)buffer, "9157"))
    {
        rt_kprintf("[TP] Found chip gt9157\n");
        return RT_TRUE;
    }
    else
    {
        rt_kprintf("[TP] Uknow chip gt9xx device: [%s]\n", buffer);
    }

    return RT_FALSE;
}

static void gt9xx_init(struct rt_i2c_bus_device *i2c_bus)
{
    rt_uint8_t buf = 0;
    rt_uint8_t cmd[2];

    gt9xx_driver.isr_sem = rt_sem_create("gt9xx", 0, RT_IPC_FLAG_FIFO);
    RT_ASSERT(gt9xx_driver.isr_sem);

    cmd[0] = (rt_uint8_t)((gt9xx_CONFIG_VERSION_REG >> 8) & 0xFF);
    cmd[1] = (rt_uint8_t)(gt9xx_CONFIG_VERSION_REG & 0xFF);
    rt_touch_read(GT9xx_TS_ADDR, &cmd, 2, &buf, 1);
    rt_kprintf("[TP] GT9xx Config version: 0x%02X\n", buf);

    cmd[0] = (rt_uint8_t)((gt9xx_VENDOR_ID_REG >> 8) & 0xFF);
    cmd[1] = (rt_uint8_t)(gt9xx_VENDOR_ID_REG & 0xFF);
    rt_touch_read(GT9xx_TS_ADDR, &cmd, 2, &buf, 1);
    rt_kprintf("[TP] GT9xx Sensor id: 0x%02X\n", buf);

    rt_sem_release(gt9xx_driver.isr_sem);
}

static void gt9xx_deinit(void)
{
    rt_sem_delete(gt9xx_driver.isr_sem);
}

static rt_err_t gt9xx_read_point(touch_message_t msg)
{
    rt_uint8_t cmd[2];
    rt_uint8_t buf[8] = {0};
    static rt_uint8_t s_tp_down = 0;

    cmd[0] = (rt_uint8_t)((gt9xx_READ_XY_REG >> 8) & 0xFF);
    cmd[1] = (rt_uint8_t)(gt9xx_READ_XY_REG & 0xFF);
    rt_touch_read(GT9xx_TS_ADDR, &cmd, 2, buf, 8);

    if((buf[0] & 0x01) == 0)
    {
        if(s_tp_down)
        {
            s_tp_down = 0;
            msg->event = TOUCH_EVENT_UP;
        }
        else
        {
            msg->event = TOUCH_EVENT_NONE;
        }
    }
    else
    {
        msg->x = ((rt_uint16_t)buf[3] << 8) | buf[2];
        msg->y = ((rt_uint16_t)buf[5] << 8) | buf[4];

        if(s_tp_down)
        {
            msg->event = TOUCH_EVENT_MOVE;
        }
        else
        {
            msg->event = TOUCH_EVENT_DOWN;
            s_tp_down = 1;
        }
    }

    buf[0] = ((gt9xx_CLEARBUF_REG >> 8) & 0xFF);
    buf[1] = (gt9xx_CLEARBUF_REG & 0xFF);
    buf[2] = 0x00;
    rt_touch_write(GT9xx_TS_ADDR, buf, 3);

    rt_sem_release(gt9xx_driver.isr_sem);

    return RT_EOK;
}

struct touch_ops gt9xx_ops =
{
    .init       = gt9xx_init,
    .deinit     = gt9xx_deinit,
    .read_point = gt9xx_read_point,
};

static int gt9xx_driver_register(void)
{
    gt9xx_driver.probe     = gt9xx_probe;
    gt9xx_driver.ops       = &gt9xx_ops;
    gt9xx_driver.user_data = RT_NULL;

    rt_touch_drivers_register(&gt9xx_driver);

    return RT_EOK;
}
INIT_ENV_EXPORT(gt9xx_driver_register);

#endif