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_i2c.h"
11 #include "fsl_ft5406.h"
12 
13 typedef struct _ft5406_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_touch_point_t;
21 
22 typedef struct _ft5406_touch_data
23 {
24     uint8_t GEST_ID;
25     uint8_t TD_STATUS;
26     ft5406_touch_point_t TOUCH[FT5406_MAX_TOUCHES];
27 } ft5406_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_Init(ft5406_handle_t * handle,I2C_Type * base)34 status_t FT5406_Init(ft5406_handle_t *handle, I2C_Type *base)
35 {
36     i2c_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_TOUCH_DATA_LEN);
53 
54     /* set device mode to normal operation */
55     mode                 = 0;
56     xfer->slaveAddress   = 0x38;
57     xfer->direction      = kI2C_Write;
58     xfer->subaddress     = 0;
59     xfer->subaddressSize = 1;
60     xfer->data           = &mode;
61     xfer->dataSize       = 1;
62     xfer->flags          = kI2C_TransferDefaultFlag;
63 
64     status = I2C_MasterTransferBlocking(handle->base, &handle->xfer);
65 
66     /* prepare transfer structure for reading touch data */
67     xfer->slaveAddress   = 0x38;
68     xfer->direction      = kI2C_Read;
69     xfer->subaddress     = 1;
70     xfer->subaddressSize = 1;
71     xfer->data           = handle->touch_buf;
72     xfer->dataSize       = FT5406_TOUCH_DATA_LEN;
73     xfer->flags          = kI2C_TransferDefaultFlag;
74 
75     return status;
76 }
77 
FT5406_Denit(ft5406_handle_t * handle)78 status_t FT5406_Denit(ft5406_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_ReadTouchData(ft5406_handle_t * handle)91 status_t FT5406_ReadTouchData(ft5406_handle_t *handle)
92 {
93     assert(handle);
94 
95     if (!handle)
96     {
97         return kStatus_InvalidArgument;
98     }
99 
100     return I2C_MasterTransferBlocking(handle->base, &handle->xfer);
101 }
102 
FT5406_GetSingleTouch(ft5406_handle_t * handle,touch_event_t * touch_event,int * touch_x,int * touch_y)103 status_t FT5406_GetSingleTouch(ft5406_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_ReadTouchData(handle);
109 
110     if (status == kStatus_Success)
111     {
112         ft5406_touch_data_t *touch_data = (ft5406_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_GetMultiTouch(ft5406_handle_t * handle,int * touch_count,touch_point_t touch_array[FT5406_MAX_TOUCHES])137 status_t FT5406_GetMultiTouch(ft5406_handle_t *handle, int *touch_count, touch_point_t touch_array[FT5406_MAX_TOUCHES])
138 {
139     status_t status;
140 
141     status = FT5406_ReadTouchData(handle);
142 
143     if (status == kStatus_Success)
144     {
145         ft5406_touch_data_t *touch_data = (ft5406_touch_data_t *)(void *)(handle->touch_buf);
146         int i;
147 
148         /* Check for valid number of touches - otherwise ignore touch information */
149         if (touch_data->TD_STATUS > FT5406_MAX_TOUCHES)
150         {
151             touch_data->TD_STATUS = 0;
152         }
153 
154         /* Decode number of touches */
155         if (touch_count)
156         {
157             *touch_count = touch_data->TD_STATUS;
158         }
159 
160         /* Decode valid touch points */
161         for (i = 0; i < touch_data->TD_STATUS; i++)
162         {
163             touch_array[i].TOUCH_ID    = TOUCH_POINT_GET_ID(touch_data->TOUCH[i]);
164             touch_array[i].TOUCH_EVENT = TOUCH_POINT_GET_EVENT(touch_data->TOUCH[i]);
165             touch_array[i].TOUCH_X     = TOUCH_POINT_GET_X(touch_data->TOUCH[i]);
166             touch_array[i].TOUCH_Y     = TOUCH_POINT_GET_Y(touch_data->TOUCH[i]);
167         }
168 
169         /* Clear vacant elements of touch_array */
170         for (; i < FT5406_MAX_TOUCHES; i++)
171         {
172             touch_array[i].TOUCH_ID    = 0;
173             touch_array[i].TOUCH_EVENT = kTouch_Reserved;
174             touch_array[i].TOUCH_X     = 0;
175             touch_array[i].TOUCH_Y     = 0;
176         }
177     }
178 
179     return status;
180 }
181