[bsp][stm32f429-disco] Add touchscreen support

This commit is contained in:
xuzhuoyi 2018-10-03 15:20:09 +08:00
parent a0c848b0e7
commit b5bb1f5785
2 changed files with 278 additions and 0 deletions

View File

@ -0,0 +1,197 @@
/*
* File : drv_touch.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2018 RT-Thread Develop Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2018-10-03 xuzhuoyi first implementation.
*/
#include "drv_touch.h"
#include "drivers/i2c.h"
#define TSC_I2C_ADDR 0x41 /* 7-bit I2C address */
static struct rt_i2c_bus_device *stmpe811_i2c_bus;
/**
\fn int32_t touch_readRegister (uint8_t reg, uint8_t *val)
\brief Read register value from Touchscreen controller
\param[in] reg Register address
\param[out] val Pointer where data will be read from register
\returns
- \b 0: function succeeded
- \b -1: function failed
*/
static int32_t touch_read (uint8_t reg, uint8_t *val)
{
struct rt_i2c_msg msgs[2];
msgs[0].addr = TSC_I2C_ADDR;
msgs[0].flags = RT_I2C_WR;
msgs[0].buf = ®
msgs[0].len = 1;
msgs[1].addr = TSC_I2C_ADDR;
msgs[1].flags = RT_I2C_RD;
msgs[1].buf = val;
msgs[1].len = 1;
if (rt_i2c_transfer(stmpe811_i2c_bus, msgs, 2) == 2)
{
return RT_EOK;
}
else
{
return -RT_ERROR;
}
}
/**
\fn int32_t touch_writeData (uint8_t reg, const uint8_t *val)
\brief Write value to Touchscreen controller register
\param[in] reg Register address
\param[in] val Pointer with data to write to register
\returns
- \b 0: function succeeded
- \b -1: function failed
*/
static int32_t touch_write (uint8_t reg, uint8_t val)
{
struct rt_i2c_msg msgs;
rt_uint8_t buf[2] = {reg, val};
msgs.addr = TSC_I2C_ADDR;
msgs.flags = RT_I2C_WR;
msgs.buf = buf;
msgs.len = 2;
if (rt_i2c_transfer(stmpe811_i2c_bus, &msgs, 1) == 1)
{
return RT_EOK;
}
else
{
return -RT_ERROR;
}
}
/**
\fn int32_t Touch_Initialize (void)
\brief Initialize touchscreen
\returns
- \b 0: function succeeded
- \b -1: function failed
*/
int32_t Touch_Initialize (void)
{
stmpe811_i2c_bus = rt_i2c_bus_device_find(STMPE811_I2CBUS_NAME);
// ptrI2C->Initialize (NULL);
// ptrI2C->PowerControl(ARM_POWER_FULL);
// ptrI2C->Control (ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
// ptrI2C->Control (ARM_I2C_BUS_CLEAR, 0);
touch_write(STMPE811_SYS_CTRL1, 0x02); /* Reset Touch-screen controller */
rt_thread_mdelay(10); /* Wait 10ms */
touch_write(STMPE811_SYS_CTRL2, 0x0C); /* Enable TSC and ADC */
touch_write(STMPE811_ADC_CTRL1, 0x68); /* Set sample time , 12-bit mode */
rt_thread_mdelay(1); /* Wait 1ms */
touch_write(STMPE811_ADC_CTRL2, 0x01); /* ADC frequency 3.25 MHz */
touch_write(STMPE811_TSC_CFG, 0xC2); /* Detect delay 10us,
Settle time 500us */
touch_write(STMPE811_FIFO_TH, 0x01); /* Threshold for FIFO */
touch_write(STMPE811_FIFO_STA, 0x01); /* FIFO reset */
touch_write(STMPE811_FIFO_STA, 0x00); /* FIFO not reset */
touch_write(STMPE811_TSC_FRACTION_Z, 0x07); /* Fraction z */
touch_write(STMPE811_TSC_I_DRIVE, 0x01); /* Drive 50 mA typical */
touch_write(STMPE811_GPIO_AF, 0x00); /* Pins are used for touchscreen */
touch_write(STMPE811_TSC_CTRL, 0x01); /* Enable TSC */
return 0;
}
/**
\fn int32_t Touch_Uninitialize (void)
\brief De-initialize touchscreen
\returns
- \b 0: function succeeded
- \b -1: function failed
*/
int32_t Touch_Uninitialize (void) {
touch_write(STMPE811_SYS_CTRL1, 0x02); /* Reset Touch-screen controller */
return 0;
}
/**
\fn int32_t Touch_GetState (TOUCH_STATE *pState)
\brief Get touchscreen state
\param[out] pState pointer to TOUCH_STATE structure
\returns
- \b 0: function succeeded
- \b -1: function failed
*/
int32_t Touch_GetState (struct touch_state *pState) {
uint8_t val;
uint8_t num;
uint8_t xyz[4];
int32_t res;
uint8_t buf[4];
struct rt_i2c_msg msgs;
/* Read touch status */
res = touch_read(STMPE811_TSC_CTRL, &val);
if (res < 0) return -1;
pState->pressed = (val & (1 << 7)) ? 1 : 0;
if (pState->pressed)
{
val = STMPE811_TSC_DATA;
/* If FIFO overflow, discard all samples except the last one */
res = touch_read(STMPE811_FIFO_SIZE, &num);
if (res < 0 || num == 0) return -1;
while (num--)
{
msgs.addr = TSC_I2C_ADDR;
msgs.flags = RT_I2C_WR;
msgs.buf = buf;
msgs.len = 1;
rt_i2c_transfer(stmpe811_i2c_bus, &msgs, 1);
//ptrI2C->MasterTransmit (TSC_I2C_ADDR, &val, 1, true);
//while (ptrI2C->GetStatus().busy);
msgs.addr = TSC_I2C_ADDR;
msgs.flags = RT_I2C_RD;
msgs.buf = buf;
msgs.len = 4;
rt_i2c_transfer(stmpe811_i2c_bus, &msgs, 1);
//ptrI2C->MasterReceive (TSC_I2C_ADDR, xyz, 4, false);
//while (ptrI2C->GetStatus().busy);
}
pState->x = (int16_t)((xyz[0] << 4) | ((xyz[1] & 0xF0) >> 4));
pState->y = (int16_t) (xyz[2] | ((xyz[1] & 0x0F) << 8));
}
else
{
/* Clear all data in FIFO */
touch_write(STMPE811_FIFO_STA, 0x1);
touch_write(STMPE811_FIFO_STA, 0x0);
}
return 0;
}

View File

@ -0,0 +1,81 @@
/*
* File : drv_touch.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2018 RT-Thread Develop Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2018-10-03 xuzhuoyi first implementation.
*/
#ifndef __DRV_TOUCH_H__
#define __DRV_TOUCH_H__
#include <stdint.h>
/* Register addresses */
#define STMPE811_CHIP_ID 0x00
#define STMPE811_ID_VER 0x02
#define STMPE811_SYS_CTRL1 0x03
#define STMPE811_SYS_CTRL2 0x04
#define STMPE811_SPI_CFG 0x08
#define STMPE811_INT_CTRL 0x09
#define STMPE811_INT_EN 0x0A
#define STMPE811_INT_STA 0x0B
#define STMPE811_GPIO_EN 0x0C
#define STMPE811_GPIO_INT_STA 0x0D
#define STMPE811_ADC_INT_EN 0x0E
#define STMPE811_ADC_INT_STA 0x0F
#define STMPE811_GPIO_SET_PIN 0x10
#define STMPE811_GPIO_CLR_PIN 0x11
#define STMPE811_GPIO_MP_STA 0x12
#define STMPE811_GPIO_DIR 0x13
#define STMPE811_GPIO_ED 0x14
#define STMPE811_GPIO_RE 0x15
#define STMPE811_GPIO_FE 0x16
#define STMPE811_GPIO_AF 0x17
#define STMPE811_ADC_CTRL1 0x20
#define STMPE811_ADC_CTRL2 0x21
#define STMPE811_ADC_CAPT 0x22
#define STMPE811_ADC_DATA_CH0 0x30
#define STMPE811_ADC_DATA_CH1 0x32
#define STMPE811_ADC_DATA_CH2 0x34
#define STMPE811_ADC_DATA_CH3 0x36
#define STMPE811_ADC_DATA_CH4 0x38
#define STMPE811_ADC_DATA_CH5 0x3A
#define STMPE811_ADC_DATA_CH6 0x3C
#define STMPE811_ADC_DATA_CH7 0x3E
#define STMPE811_TSC_CTRL 0x40
#define STMPE811_TSC_CFG 0x41
#define STMPE811_WDW_TR_X 0x42
#define STMPE811_WDW_TR_Y 0x44
#define STMPE811_WDW_BL_X 0x46
#define STMPE811_WDW_BL_Y 0x48
#define STMPE811_FIFO_TH 0x4A
#define STMPE811_FIFO_STA 0x4B
#define STMPE811_FIFO_SIZE 0x4C
#define STMPE811_TSC_DATA_X 0x4D
#define STMPE811_TSC_DATA_Y 0x4F
#define STMPE811_TSC_DATA_Z 0x51
#define STMPE811_TSC_FRACTION_Z 0x56
#define STMPE811_TSC_DATA_XYZ 0x57
#define STMPE811_TSC_DATA 0xD7
#define STMPE811_TSC_I_DRIVE 0x58
#define STMPE811_TSC_SHIELD 0x59
#define STMPE811_TEMP_CTRL 0x60
#define STMPE811_TEMP_DATA 0x61
#define STMPE811_TEMP_TH 0x62
/* Touch state */
struct touch_state {
int16_t x; ///< Position X
int16_t y; ///< Position Y
uint8_t pressed; ///< Pressed flag
uint8_t padding;
};
#endif /* __DRV_TOUCH_H__ */