1 /*
2 * Copyright (c) 2016, Freescale Semiconductor, Inc.
3 * Copyright 2016-2019 NXP
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9 #include "fsl_common.h"
10 #include "fsl_lpi2c.h"
11 #include "fsl_ft5406_rt.h"
12
13 typedef struct _ft5406_rt_touch_point
14 {
15 uint8_t XH;
16 uint8_t XL;
17 uint8_t YH;
18 uint8_t YL;
19 uint8_t RESERVED[2];
20 } ft5406_rt_touch_point_t;
21
22 typedef struct _ft5406_rt_touch_data
23 {
24 uint8_t GEST_ID;
25 uint8_t TD_STATUS;
26 ft5406_rt_touch_point_t TOUCH[FT5406_RT_MAX_TOUCHES];
27 } ft5406_rt_touch_data_t;
28
29 #define TOUCH_POINT_GET_EVENT(T) ((touch_event_t)((T).XH >> 6))
30 #define TOUCH_POINT_GET_ID(T) ((T).YH >> 4)
31 #define TOUCH_POINT_GET_X(T) ((((T).XH & 0x0f) << 8) | (T).XL)
32 #define TOUCH_POINT_GET_Y(T) ((((T).YH & 0x0f) << 8) | (T).YL)
33
FT5406_RT_Init(ft5406_rt_handle_t * handle,LPI2C_Type * base)34 status_t FT5406_RT_Init(ft5406_rt_handle_t *handle, LPI2C_Type *base)
35 {
36 lpi2c_master_transfer_t *xfer = &(handle->xfer);
37 status_t status;
38 uint8_t mode;
39
40 assert(handle);
41 assert(base);
42
43 if (!handle || !base)
44 {
45 return kStatus_InvalidArgument;
46 }
47
48 handle->base = base;
49
50 /* clear transfer structure and buffer */
51 memset(xfer, 0, sizeof(*xfer));
52 memset(handle->touch_buf, 0, FT5406_RT_TOUCH_DATA_LEN);
53
54 /* set device mode to normal operation */
55 mode = 0;
56 xfer->slaveAddress = FT5406_RT_I2C_ADDRESS;
57 xfer->direction = kLPI2C_Write;
58 xfer->subaddress = 0;
59 xfer->subaddressSize = 1;
60 xfer->data = &mode;
61 xfer->dataSize = 1;
62 xfer->flags = kLPI2C_TransferDefaultFlag;
63
64 status = LPI2C_MasterTransferBlocking(handle->base, &handle->xfer);
65
66 /* prepare transfer structure for reading touch data */
67 xfer->slaveAddress = FT5406_RT_I2C_ADDRESS;
68 xfer->direction = kLPI2C_Read;
69 xfer->subaddress = 1;
70 xfer->subaddressSize = 1;
71 xfer->data = handle->touch_buf;
72 xfer->dataSize = FT5406_RT_TOUCH_DATA_LEN;
73 xfer->flags = kLPI2C_TransferDefaultFlag;
74
75 return status;
76 }
77
FT5406_RT_Denit(ft5406_rt_handle_t * handle)78 status_t FT5406_RT_Denit(ft5406_rt_handle_t *handle)
79 {
80 assert(handle);
81
82 if (!handle)
83 {
84 return kStatus_InvalidArgument;
85 }
86
87 handle->base = NULL;
88 return kStatus_Success;
89 }
90
FT5406_RT_ReadTouchData(ft5406_rt_handle_t * handle)91 status_t FT5406_RT_ReadTouchData(ft5406_rt_handle_t *handle)
92 {
93 assert(handle);
94
95 if (!handle)
96 {
97 return kStatus_InvalidArgument;
98 }
99
100 return LPI2C_MasterTransferBlocking(handle->base, &handle->xfer);
101 }
102
FT5406_RT_GetSingleTouch(ft5406_rt_handle_t * handle,touch_event_t * touch_event,int * touch_x,int * touch_y)103 status_t FT5406_RT_GetSingleTouch(ft5406_rt_handle_t *handle, touch_event_t *touch_event, int *touch_x, int *touch_y)
104 {
105 status_t status;
106 touch_event_t touch_event_local;
107
108 status = FT5406_RT_ReadTouchData(handle);
109
110 if (status == kStatus_Success)
111 {
112 ft5406_rt_touch_data_t *touch_data = (ft5406_rt_touch_data_t *)(void *)(handle->touch_buf);
113
114 if (touch_event == NULL)
115 {
116 touch_event = &touch_event_local;
117 }
118 *touch_event = TOUCH_POINT_GET_EVENT(touch_data->TOUCH[0]);
119
120 /* Update coordinates only if there is touch detected */
121 if ((*touch_event == kTouch_Down) || (*touch_event == kTouch_Contact))
122 {
123 if (touch_x)
124 {
125 *touch_x = TOUCH_POINT_GET_X(touch_data->TOUCH[0]);
126 }
127 if (touch_y)
128 {
129 *touch_y = TOUCH_POINT_GET_Y(touch_data->TOUCH[0]);
130 }
131 }
132 }
133
134 return status;
135 }
136
FT5406_RT_GetMultiTouch(ft5406_rt_handle_t * handle,int * touch_count,touch_point_t touch_array[FT5406_RT_MAX_TOUCHES])137 status_t FT5406_RT_GetMultiTouch(ft5406_rt_handle_t *handle,
138 int *touch_count,
139 touch_point_t touch_array[FT5406_RT_MAX_TOUCHES])
140 {
141 status_t status;
142
143 status = FT5406_RT_ReadTouchData(handle);
144
145 if (status == kStatus_Success)
146 {
147 ft5406_rt_touch_data_t *touch_data = (ft5406_rt_touch_data_t *)(void *)(handle->touch_buf);
148 int i;
149
150 /* Check for valid number of touches - otherwise ignore touch information */
151 if (touch_data->TD_STATUS > FT5406_RT_MAX_TOUCHES)
152 {
153 touch_data->TD_STATUS = 0;
154 }
155
156 /* Decode number of touches */
157 if (touch_count)
158 {
159 *touch_count = touch_data->TD_STATUS;
160 }
161
162 /* Decode valid touch points */
163 for (i = 0; i < touch_data->TD_STATUS; i++)
164 {
165 touch_array[i].TOUCH_ID = TOUCH_POINT_GET_ID(touch_data->TOUCH[i]);
166 touch_array[i].TOUCH_EVENT = TOUCH_POINT_GET_EVENT(touch_data->TOUCH[i]);
167 touch_array[i].TOUCH_X = TOUCH_POINT_GET_X(touch_data->TOUCH[i]);
168 touch_array[i].TOUCH_Y = TOUCH_POINT_GET_Y(touch_data->TOUCH[i]);
169 }
170
171 /* Clear vacant elements of touch_array */
172 for (; i < FT5406_RT_MAX_TOUCHES; i++)
173 {
174 touch_array[i].TOUCH_ID = 0;
175 touch_array[i].TOUCH_EVENT = kTouch_Reserved;
176 touch_array[i].TOUCH_X = 0;
177 touch_array[i].TOUCH_Y = 0;
178 }
179 }
180
181 return status;
182 }
183