Adapt lvgl for using ft6206 model touch screen[stm32f469-st-disco] (#6153)

* Adapt lvgl for using ft6206 model touch screen
This commit is contained in:
liYang~ 2022-07-08 13:55:13 +08:00 committed by GitHub
parent 7b004d9fd6
commit e36e3fda0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 18 additions and 508 deletions

View File

@ -5,19 +5,23 @@
*
* Change Logs:
* Date Author Notes
* 2022-07-07 liYony The first version
* 2022-07-07 liYony The first version (FT6336)
* 2022-07-08 liYony Add FT6206
*/
#include <lvgl.h>
#include <rtdevice.h>
#include <touch.h>
#include <lcd_port.h>
#define DBG_TAG "LVGL.port.indev"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
/* Include the package header files you are using */
/* Add the include file of the package you are using */
#ifdef BSP_USING_TOUCH_FT6X36
#include "ft6236.h"
#include <ft6236.h>
#elif defined(BSP_USING_TOUCH_FT6206)
#include <ft6206.h>
#endif /* BSP_USING_TOUCH_FT6X36 */
/* Touch chip connection information */
@ -42,6 +46,9 @@ static void input_read(lv_indev_drv_t *indev_drv, lv_indev_data_t *data)
#ifdef BSP_USING_TOUCH_FT6X36
data->point.x = read_data->y_coordinate;
data->point.y = LCD_HEIGHT - read_data->x_coordinate;
#elif defined(BSP_USING_TOUCH_FT6206)
data->point.x = read_data->x_coordinate;
data->point.y = LCD_HEIGHT - read_data->y_coordinate;
#endif /* BSP_USING_TOUCH_FT6X36 */
if (read_data->event == RT_TOUCH_EVENT_DOWN)
@ -68,9 +75,11 @@ static int lv_hw_touch_init(void)
{
struct rt_touch_config cfg;
#ifdef BSP_USING_TOUCH_FT6X36
cfg.dev_name = BSP_TOUCH_I2C_BUS_NAME;
#ifdef BSP_USING_TOUCH_FT6X36
rt_hw_ft6236_init(TOUCH_DEV_NAME, &cfg, BSP_TOUCH_I2C_RESET_PIN);
#elif defined(BSP_USING_TOUCH_FT6206)
rt_hw_ft6206_init(TOUCH_DEV_NAME, &cfg);
#endif /* BSP_USING_TOUCH_FT6X36 */
touch_dev = rt_device_find(TOUCH_DEV_NAME);

View File

@ -85,28 +85,20 @@ menu "Onboard Peripheral Drivers"
config BSP_USING_TOUCH
bool "Enable touch screen"
select BSP_USING_I2C1
select PKG_USING_TOUCH_DRIVERS
choice
prompt "Touch IC type"
depends on BSP_USING_TOUCH
default BSP_CHOICE_TOUCH_FT6X36
default BSP_USING_TOUCH_FT6X36
config BSP_USING_TOUCH_FT6206
bool "FT6206"
select BSP_USING_I2C1
if BSP_USING_TOUCH_FT6206
config BSP_TOUCH_INT_PIN
int # "Touch IC interrupt pin"
default 149
config BSP_TOUCH_I2C_NAME
string # "I2C Bus Name"
default "i2c1"
endif
bool "FT6206"
select PKG_USING_FT6206
config BSP_USING_TOUCH_FT6X36
bool "FT6x36"
select BSP_USING_I2C1
select PKG_USING_TOUCH_DRIVERS
select PKG_USING_FT6236
endchoice

View File

@ -1,14 +0,0 @@
from building import *
cwd = GetCurrentDir()
src = []
if GetDepend(['BSP_USING_TOUCH_FT6206']):
src += Glob('drv_touch.c')
src += Glob('drv_touch_ft6206.c')
CPPPATH = [cwd]
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = CPPPATH)
Return('group')

View File

@ -1,207 +0,0 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-02-08 Zhangyihong the first version
*/
#include <rtconfig.h>
#ifdef BSP_USING_TOUCH_FT6206
#include "drv_touch.h"
#include <string.h>
#ifdef PKG_USING_GUIENGINE
#include <rtgui/event.h>
#include <rtgui/rtgui_server.h>
#elif defined(PKG_USING_LITTLEVGL2RTT)
#include <littlevgl2rtt.h>
#elif defined(PKG_USING_LVGL)
#include <lvgl.h>
extern void lv_port_indev_input(rt_int16_t x, rt_int16_t y, lv_indev_state_t state);
static rt_bool_t touch_down = RT_FALSE;
#endif /* PKG_USING_GUIENGINE */
#define BSP_TOUCH_SAMPLE_HZ (50)
#define DBG_ENABLE
#define DBG_SECTION_NAME "touch"
#define DBG_LEVEL DBG_LOG
#define DBG_COLOR
#include <rtdbg.h>
static rt_list_t driver_list;
void rt_touch_drivers_register(touch_drv_t drv)
{
rt_list_insert_before(&driver_list, &drv->list);
}
static void post_down_event(rt_uint16_t x, rt_uint16_t y, rt_tick_t ts)
{
#ifdef PKG_USING_GUIENGINE
struct rtgui_event_mouse emouse;
emouse.parent.sender = RT_NULL;
emouse.wid = RT_NULL;
emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN;
emouse.x = x;
emouse.y = y;
emouse.ts = rt_tick_get();
emouse.id = ts;
rtgui_server_post_event(&emouse.parent, sizeof(emouse));
#elif defined(PKG_USING_LITTLEVGL2RTT)
littlevgl2rtt_send_input_event(x, y, LITTLEVGL2RTT_INPUT_DOWN);
#elif defined(PKG_USING_LVGL)
touch_down = RT_TRUE;
lv_port_indev_input(x, y, (touch_down == RT_TRUE) ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL);
#endif
}
static void post_motion_event(rt_uint16_t x, rt_uint16_t y, rt_tick_t ts)
{
#ifdef PKG_USING_GUIENGINE
struct rtgui_event_mouse emouse;
emouse.parent.sender = RT_NULL;
emouse.wid = RT_NULL;
emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN;
emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
emouse.x = x;
emouse.y = y;
emouse.ts = rt_tick_get();
emouse.id = ts;
rtgui_server_post_event(&emouse.parent, sizeof(emouse));
#elif defined(PKG_USING_LITTLEVGL2RTT)
littlevgl2rtt_send_input_event(x, y, LITTLEVGL2RTT_INPUT_MOVE);
#elif defined(PKG_USING_LVGL)
lv_port_indev_input(x, y, (touch_down == RT_TRUE) ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL);
#endif /* PKG_USING_GUIENGINE */
}
static void post_up_event(rt_uint16_t x, rt_uint16_t y, rt_tick_t ts)
{
#ifdef PKG_USING_GUIENGINE
struct rtgui_event_mouse emouse;
emouse.parent.sender = RT_NULL;
emouse.wid = RT_NULL;
emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
emouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP;
emouse.x = x;
emouse.y = y;
emouse.ts = rt_tick_get();
emouse.id = ts;
rtgui_server_post_event(&emouse.parent, sizeof(emouse));
#elif defined(PKG_USING_LITTLEVGL2RTT)
littlevgl2rtt_send_input_event(x, y, LITTLEVGL2RTT_INPUT_MOVE);
#elif defined(PKG_USING_LVGL)
touch_down = RT_FALSE;
lv_port_indev_input(x, y, (touch_down == RT_TRUE) ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL);
#endif /* PKG_USING_GUIENGINE */
}
static void touch_thread_entry(void *parameter)
{
touch_drv_t touch = (touch_drv_t)parameter;
struct touch_message msg;
rt_tick_t emouse_id = 0;
touch->ops->isr_enable(RT_TRUE);
while (1)
{
if (rt_sem_take(touch->isr_sem, 10) != RT_EOK)
{
continue;
}
while(touch->ops->read_point(&msg) == RT_EOK)
{
switch (msg.event)
{
case TOUCH_EVENT_UP:
post_up_event(msg.x, msg.y, emouse_id);
break;
case TOUCH_EVENT_DOWN:
emouse_id = rt_tick_get();
post_down_event(msg.x, msg.y, emouse_id);
break;
case TOUCH_EVENT_MOVE:
post_motion_event(msg.x, msg.y, emouse_id);
break;
default:
break;
}
rt_thread_delay(RT_TICK_PER_SECOND / BSP_TOUCH_SAMPLE_HZ);
}
touch->ops->isr_enable(RT_TRUE);
}
}
static int rt_touch_driver_init(void)
{
rt_list_init(&driver_list);
return 0;
}
INIT_BOARD_EXPORT(rt_touch_driver_init);
static struct rt_i2c_bus_device *i2c_bus = RT_NULL;
static int rt_touch_thread_init(void)
{
rt_list_t *l;
touch_drv_t current_driver;
rt_thread_t tid = RT_NULL;
i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(BSP_TOUCH_I2C_NAME);
RT_ASSERT(i2c_bus);
current_driver = RT_NULL;
if (rt_device_open((rt_device_t)i2c_bus, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
return -1;
for (l = driver_list.next; l != &driver_list; l = l->next)
{
if (rt_list_entry(l, struct touch_drivers, list)->probe(i2c_bus))
{
current_driver = rt_list_entry(l, struct touch_drivers, list);
break;
}
}
if (current_driver == RT_NULL)
{
LOG_E("no touch screen or do not have driver\r\n");
rt_device_close((rt_device_t)i2c_bus);
return -1;
}
current_driver->ops->init(i2c_bus);
LOG_I("touch screen found driver\r\n");
tid = rt_thread_create("touch", touch_thread_entry, current_driver, 2048, 27, 20);
if (tid == RT_NULL)
{
current_driver->ops->deinit();
rt_device_close((rt_device_t)i2c_bus);
return -1;
}
rt_thread_startup(tid);
return 0;
}
static void touch_init_thread_entry(void *parameter)
{
rt_touch_thread_init();
}
static int touc_bg_init(void)
{
rt_thread_t tid = RT_NULL;
tid = rt_thread_create("touch", touch_init_thread_entry, RT_NULL, 2048, 28, 20);
if (tid == RT_NULL)
{
return -1;
}
rt_thread_startup(tid);
return 0;
}
INIT_COMPONENT_EXPORT(touc_bg_init);
#endif /* BSP_USING_TOUCH_FT6206 */

View File

@ -1,55 +0,0 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-02-08 Zhangyihong the first version
*/
#ifndef __DRV_TOUCH_H__
#define __DRV_TOUCH_H__
#include "rtthread.h"
#include "rtdevice.h"
#define TOUCH_DBG_LEVEL DBG_LOG
#define IIC_RETRY_NUM 2
#define TOUCH_EVENT_UP (0x01)
#define TOUCH_EVENT_DOWN (0x02)
#define TOUCH_EVENT_MOVE (0x03)
#define TOUCH_EVENT_NONE (0x80)
struct touch_message
{
rt_uint16_t x;
rt_uint16_t y;
rt_uint8_t event;
};
typedef struct touch_message *touch_msg_t;
struct touch_ops
{
void (* isr_enable)(rt_bool_t);
rt_err_t (* read_point)(touch_msg_t);
void (* init)(struct rt_i2c_bus_device *);
void (* deinit)(void);
};
typedef struct touch_ops *touch_ops_t;
struct touch_drivers
{
rt_list_t list;
unsigned char address;
rt_bool_t (*probe)(struct rt_i2c_bus_device *i2c_bus);
rt_sem_t isr_sem;
touch_ops_t ops;
void *user_data;
};
typedef struct touch_drivers *touch_drv_t;
extern void rt_touch_drivers_register(touch_drv_t drv);
#endif

View File

@ -1,215 +0,0 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2017-08-08 Yang the first version
* 2019-04-23 WillianChan porting to ft6206
*/
#include <rtthread.h>
#include <rthw.h>
#include <rtdevice.h>
#include "drv_touch.h"
#include <stdint.h>
#include <string.h>
#ifdef BSP_USING_TOUCH_FT6206
#define DBG_ENABLE
#define DBG_SECTION_NAME "TOUCH.ft"
#define DBG_LEVEL TOUCH_DBG_LEVEL
#define DBG_COLOR
#include <rtdbg.h>
static struct rt_i2c_bus_device *ft_i2c_bus;
static struct touch_drivers ft_driver;
static int ft_read(struct rt_i2c_bus_device *i2c_bus, rt_uint8_t addr, rt_uint8_t *buffer, rt_size_t length)
{
int ret = -1;
int retries = 0;
struct rt_i2c_msg msgs[] =
{
{
.addr = ft_driver.address,
.flags = RT_I2C_WR,
.len = 1,
.buf = &addr,
},
{
.addr = ft_driver.address,
.flags = RT_I2C_RD,
.len = length,
.buf = buffer,
},
};
while (retries < IIC_RETRY_NUM)
{
ret = rt_i2c_transfer(i2c_bus, msgs, 2);
if (ret == 2)break;
retries++;
}
if (retries >= IIC_RETRY_NUM)
{
LOG_E("%s i2c read error: %d", __func__, ret);
return -1;
}
return ret;
}
static void ft_write(touch_drv_t driver, struct rt_i2c_bus_device *i2c_bus, rt_uint8_t addr, rt_uint8_t *buffer, rt_size_t length)
{
rt_uint8_t *send_buffer = rt_malloc(length + 1);
RT_ASSERT(send_buffer);
send_buffer[0] = addr;
memcpy(send_buffer + 1, buffer, length);
struct rt_i2c_msg msgs[] =
{
{
.addr = ft_driver.address,
.flags = RT_I2C_WR,
.len = length + 1,
.buf = send_buffer,
}
};
length = rt_i2c_transfer(i2c_bus, msgs, 1);
rt_free(send_buffer);
send_buffer = RT_NULL;
}
static void ft_isr_enable(rt_bool_t enable)
{
rt_pin_irq_enable(BSP_TOUCH_INT_PIN, enable);
}
static void ft_touch_isr(void *parameter)
{
ft_isr_enable(RT_FALSE);
rt_sem_release(ft_driver.isr_sem);
}
static rt_err_t ft_read_point(touch_msg_t msg)
{
int ret = -1;
uint8_t point_num = 0;
static uint8_t s_tp_down = 0;
uint8_t point[6];
ret = ft_read(ft_i2c_bus, 0x02, &point_num, 1);
if (ret < 0)
{
return RT_ERROR;
}
if (point_num == 0)
{
if (s_tp_down)
{
s_tp_down = 0;
msg->event = TOUCH_EVENT_UP;
return RT_EOK;
}
msg->event = TOUCH_EVENT_NONE;
return RT_ERROR;
}
ret = ft_read(ft_i2c_bus, 0x03, point, 6);
if (ret < 0)
{
return RT_ERROR;
}
msg->y = (point[0]&0x0F) << 8 | point[1];
msg->x = (point[2]&0x0F) << 8 | point[3];
if (s_tp_down)
{
msg->event = TOUCH_EVENT_MOVE;
return RT_EOK;
}
msg->event = TOUCH_EVENT_DOWN;
s_tp_down = 1;
return RT_EOK;
}
static void ft_init(struct rt_i2c_bus_device *i2c_bus)
{
if (ft_i2c_bus == RT_NULL)
{
ft_i2c_bus = i2c_bus;
}
ft_driver.isr_sem = rt_sem_create("ft", 0, RT_IPC_FLAG_FIFO);
RT_ASSERT(ft_driver.isr_sem);
rt_pin_mode(BSP_TOUCH_INT_PIN, PIN_MODE_INPUT_PULLUP);
rt_pin_attach_irq(BSP_TOUCH_INT_PIN, PIN_IRQ_MODE_FALLING, ft_touch_isr, RT_NULL);
rt_thread_mdelay(200);
}
static void ft_deinit(void)
{
if (ft_driver.isr_sem)
{
rt_sem_delete(ft_driver.isr_sem);
ft_driver.isr_sem = RT_NULL;
}
}
struct touch_ops ft_ops =
{
ft_isr_enable,
ft_read_point,
ft_init,
ft_deinit,
};
static rt_bool_t ft_probe(struct rt_i2c_bus_device *i2c_bus)
{
int err = 0;
uint8_t cid = 0xFF;
ft_i2c_bus = i2c_bus;
/* FT6206 Chip identification register address is 0xA8 */
err = ft_read(ft_i2c_bus, 0xA8, (uint8_t *)&cid, 1);
if (err < 0)
{
LOG_E("%s failed: %d", __func__, err);
return RT_FALSE;
}
LOG_I("touch CID:0x%02X", cid);
/* FT6206 ID Value is 0x11 */
if(cid == 0x11)
{
return RT_TRUE;
}
return RT_FALSE;
}
int ft_driver_register(void)
{
/* TouchScreen FT6206 Slave I2C address is 0x54
* 0x54 << 1 = 0x2A
*/
ft_driver.address = 0x2A;
ft_driver.probe = ft_probe;
ft_driver.ops = &ft_ops;
ft_driver.user_data = RT_NULL;
rt_touch_drivers_register(&ft_driver);
return 0;
}
INIT_DEVICE_EXPORT(ft_driver_register);
#endif /* BSP_USING_TOUCH_FT6206 */