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 * 2022-6-27 solar first version
9 */
10
11 #include <drv_touch_xpt.h>
12 #include <drv_soft_spi.h>
13 #include <drv_spi.h>
14 #include <drv_gpio.h>
15
16 #define LOG_TAG "drv.xpt2046"
17 #include <drv_log.h>
18
19 #ifdef BSP_USING_TOUCH_RES
20
21 static const rt_uint8_t xpt2046_tx_data[21] = {0xD0, 0, 0xD0, 0, 0xD0, 0, 0xD0, 0, 0xD0, 0, 0x90, 0, 0x90, 0, 0x90, 0, 0x90, 0, 0x90, 0, 0};
22
23 /* Please calibrate the resistive touch screen before use, it is best to store the calibrated data */
xpt2046_calibration(const char * lcd_name,const char * touch_name)24 rt_err_t xpt2046_calibration(const char *lcd_name,const char *touch_name)
25 {
26 /* Find the TFT LCD device */
27 rt_device_t lcd = rt_device_find(lcd_name);
28 if (lcd == RT_NULL)
29 {
30 LOG_E(LOG_TAG " cannot find lcd device named %s", lcd_name);
31 return -RT_ERROR;
32 }
33 if (rt_device_open(lcd, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
34 {
35 LOG_E(LOG_TAG " cannot open lcd device named %s", lcd_name);
36 return 0;
37 }
38
39 /* Find the Touch device */
40 rt_xpt2046_t touch = (rt_xpt2046_t)rt_device_find(touch_name);
41 if (touch == RT_NULL)
42 {
43 LOG_E(LOG_TAG " cannot find touch device named %s", touch_name);
44 return -RT_ERROR;
45 }
46
47 /* Get TFT LCD screen information */
48 struct rt_device_graphic_info lcd_info;
49 rt_device_control(lcd, RTGRAPHIC_CTRL_GET_INFO, &lcd_info);
50
51 /* clear screen */
52 for (rt_uint32_t y = 0; y < lcd_info.height; ++y)
53 {
54 const uint32_t white = 0xFFFFFFFF;
55 rt_graphix_ops(lcd)->draw_hline((const char *)(&white), 0, lcd_info.width, y);
56 }
57 /* Set the information for the four points used for calibration */
58 rt_uint32_t cross_size = (lcd_info.width > lcd_info.height ? lcd_info.height : lcd_info.width) / 10;
59 rt_uint32_t x0 = cross_size;
60 rt_uint32_t y0 = cross_size;
61 rt_uint32_t x1 = lcd_info.width - cross_size;
62 rt_uint32_t y1 = cross_size;
63 rt_uint32_t x2 = lcd_info.width - cross_size;
64 rt_uint32_t y2 = lcd_info.height - cross_size;
65 rt_uint32_t x3 = cross_size;
66 rt_uint32_t y3 = lcd_info.height - cross_size;
67 const rt_uint32_t black = 0x0;
68 /* Upper left cross */
69 rt_graphix_ops(lcd)->draw_hline((const char *)(&black), 0, x0 + cross_size, y0);
70 rt_graphix_ops(lcd)->draw_vline((const char *)(&black), x0, 0, y0 + cross_size);
71
72 touch->min_raw_x = 0;
73 touch->min_raw_y = 0;
74 touch->max_raw_x = 4096;
75 touch->max_raw_y = 4096;
76 touch->parent.info.range_x = 4096;
77 touch->parent.info.range_y = 4096;
78
79 rt_uint16_t x_raw[4];
80 rt_uint16_t y_raw[4];
81 rt_uint8_t raw_idx = 0;
82 rt_memset(x_raw, 0, sizeof(x_raw));
83 rt_memset(y_raw, 0, sizeof(y_raw));
84 while (1)
85 {
86 struct rt_touch_data read_data;
87 rt_memset(&read_data, 0, sizeof(struct rt_touch_data));
88 if (rt_device_read((rt_device_t)touch, 0, &read_data, 1) == 1)
89 {
90 x_raw[raw_idx] = read_data.x_coordinate;
91 y_raw[raw_idx++] = read_data.y_coordinate;
92 LOG_I(" %d point capture", raw_idx - 1);
93 /* After processing a point, proceed to clear the screen */
94 for (rt_uint32_t y = 0; y < lcd_info.height; ++y)
95 {
96 const uint32_t white = 0xFFFFFFFF;
97 rt_graphix_ops(lcd)->draw_hline((const char *)(&white), 0, lcd_info.width, y);
98 }
99 rt_thread_mdelay(400);
100 if (raw_idx >= 4)
101 {
102 break;
103 }
104 switch (raw_idx)
105 {
106 case 1:
107 /* Upper right cross */
108 rt_graphix_ops(lcd)->draw_hline((const char *)(&black), x1 - cross_size, lcd_info.width, y1);
109 rt_graphix_ops(lcd)->draw_vline((const char *)(&black), x1, 0, y1 + cross_size);
110 break;
111 case 2:
112 /* lower right cross */
113 rt_graphix_ops(lcd)->draw_hline((const char *)(&black), x2 - cross_size, lcd_info.width, y2);
114 rt_graphix_ops(lcd)->draw_vline((const char *)(&black), x2, y2 - cross_size, lcd_info.height);
115 break;
116 case 3:
117 /* lower left cross */
118 rt_graphix_ops(lcd)->draw_hline((const char *)(&black), 0, x3 + cross_size, y3);
119 rt_graphix_ops(lcd)->draw_vline((const char *)(&black), x3, y3 - cross_size, lcd_info.height);
120 break;
121 default:
122 break;
123 }
124 }
125 rt_thread_mdelay(10);
126 }
127 rt_uint16_t min_x = (x_raw[0] + x_raw[3]) / 2;
128 rt_uint16_t max_x = (x_raw[1] + x_raw[2]) / 2;
129 rt_uint16_t min_y = (y_raw[0] + y_raw[1]) / 2;
130 rt_uint16_t max_y = (y_raw[2] + y_raw[3]) / 2;
131
132 rt_int32_t x_raw_cnt_per_pixel = (max_x - min_x) / (x1 - x0);
133 rt_int32_t y_raw_cnt_per_pixel = (max_y - min_y) / (y2 - y1);
134
135 min_x -= cross_size * x_raw_cnt_per_pixel;
136 max_x += cross_size * x_raw_cnt_per_pixel;
137 min_y -= cross_size * y_raw_cnt_per_pixel;
138 max_y += cross_size * y_raw_cnt_per_pixel;
139
140 touch->min_raw_x = min_x;
141 touch->min_raw_y = min_y;
142 touch->max_raw_x = max_x;
143 touch->max_raw_y = max_y;
144 touch->parent.info.range_x = lcd_info.width;
145 touch->parent.info.range_y = lcd_info.height;
146
147 LOG_I(" Calibration result, min_x:%d, min_y:%d, max_x:%d, max_y:%d", min_x, min_y, max_x, max_y);
148
149 rt_device_close(lcd);
150
151 return RT_EOK;
152 }
153
xpt2046_touch_readpoint(struct rt_touch_device * touch,void * buf,rt_size_t touch_num)154 static rt_ssize_t xpt2046_touch_readpoint(struct rt_touch_device *touch, void *buf, rt_size_t touch_num)
155 {
156 if (touch_num != 0)
157 {
158 struct rt_touch_data *result = (struct rt_touch_data *)buf;
159 rt_xpt2046_t dev = (rt_xpt2046_t)touch;
160 #ifdef RT_TOUCH_PIN_IRQ
161 if (rt_pin_read(dev->parent.config.irq_pin.pin))
162 {
163 result->event = RT_TOUCH_EVENT_NONE;
164 return 0;
165 }
166 #endif /* RT_TOUCH_PIN_IRQ */
167 rt_uint8_t rx_data[21];
168 rt_spi_transfer(dev->spi, xpt2046_tx_data, rx_data, 21);
169 rt_uint8_t x_count = 0;
170 rt_uint8_t y_count = 0;
171 rt_uint32_t x_cum = 0;
172 rt_uint32_t y_cum = 0;
173 for (rt_uint8_t i = 1; i < 11; i += 2)
174 {
175 rt_uint16_t temp = (rx_data[i] << 8 | rx_data[i + 1]) >> 4;
176 if (temp >= dev->min_raw_x && temp <= dev->max_raw_x)
177 {
178 ++x_count;
179 x_cum += temp;
180 }
181 temp = (rx_data[i + 10] << 8 | rx_data[i + 11]) >> 4;
182 if (temp >= dev->min_raw_y && temp <= dev->max_raw_y)
183 {
184 ++y_count;
185 y_cum += temp;
186 }
187 }
188 if (!x_count || !y_count)
189 {
190 result->event = RT_TOUCH_EVENT_NONE;
191 return 0;
192 }
193 result->event = RT_TOUCH_EVENT_DOWN;
194 result->x_coordinate = ((float)x_cum / x_count - dev->min_raw_x) / (dev->max_raw_x - dev->min_raw_x) * dev->parent.info.range_x;
195 result->y_coordinate = ((float)y_cum / y_count - dev->min_raw_y) / (dev->max_raw_y - dev->min_raw_y) * dev->parent.info.range_y;
196 return touch_num;
197 }
198 else
199 {
200 return 0;
201 }
202 }
203
xpt2046_touch_control(struct rt_touch_device * touch,int cmd,void * arg)204 static rt_err_t xpt2046_touch_control(struct rt_touch_device *touch, int cmd, void *arg)
205 {
206 rt_err_t result = RT_EOK;
207 RT_ASSERT(touch != RT_NULL);
208
209 /* If necessary, please implement this control function yourself */
210
211 return result;
212 }
213
214 static struct rt_touch_ops xpt2046_ops =
215 {
216 .touch_readpoint = xpt2046_touch_readpoint,
217 .touch_control = xpt2046_touch_control,
218 };
219
xpt2046_hw_init(void)220 static int xpt2046_hw_init(void)
221 {
222 rt_xpt2046_t dev_obj = rt_malloc(sizeof(struct rt_xpt2046));
223 if (dev_obj != RT_NULL)
224 {
225 rt_memset(dev_obj, 0x0, sizeof(struct rt_xpt2046));
226 dev_obj->min_raw_x = BSP_XPT2046_MIN_RAW_X;
227 dev_obj->min_raw_y = BSP_XPT2046_MIN_RAW_Y;
228 dev_obj->max_raw_x = BSP_XPT2046_MAX_RAW_X;
229 dev_obj->max_raw_y = BSP_XPT2046_MAX_RAW_Y;
230 /* spi mount and config is implemented by the user */
231 dev_obj->spi = RT_NULL;
232
233 dev_obj->parent.info.type = RT_TOUCH_TYPE_RESISTANCE;
234 dev_obj->parent.info.vendor = RT_TOUCH_VENDOR_UNKNOWN;
235 dev_obj->parent.info.point_num = 1;
236 dev_obj->parent.info.range_x = BSP_XPT2046_RANGE_X;
237 dev_obj->parent.info.range_y = BSP_XPT2046_RANGE_Y;
238 #ifdef RT_TOUCH_PIN_IRQ
239 dev_obj->parent.config.irq_pin.pin = rt_pin_get(BSP_XPT2046_IRQ_PIN);
240 dev_obj->parent.config.irq_pin.mode = PIN_MODE_INPUT_PULLUP;
241 #endif /* RT_TOUCH_PIN_IRQ */
242 dev_obj->parent.ops = &xpt2046_ops;
243
244 rt_uint8_t dev_num = 0;
245 char dev_name[RT_NAME_MAX];
246 do
247 {
248 rt_sprintf(dev_name, "xpt%d", dev_num++);
249 if (dev_num == 255)
250 {
251 rt_device_destroy((rt_device_t) & (dev_obj->parent));
252 return RT_NULL;
253 }
254 } while (rt_device_find(dev_name));
255 rt_hw_touch_register(&(dev_obj->parent), dev_name, RT_DEVICE_FLAG_INT_RX, RT_NULL);
256 return RT_EOK;
257 }
258 else
259 {
260 return -RT_ERROR;
261 }
262 }
263
264 INIT_DEVICE_EXPORT(xpt2046_hw_init);
265
266 #endif /* BSP_USING_TOUCH_RES */
267