2012-05-06 18:00:16 +02:00
|
|
|
/****************************************************************************
|
2012-05-29 03:43:51 +02:00
|
|
|
* drivers/input/stmpe811_tsc.c
|
2012-05-06 18:00:16 +02:00
|
|
|
*
|
2017-10-04 23:22:27 +02:00
|
|
|
* Copyright (C) 2012, 2017 Gregory Nutt. All rights reserved.
|
2012-05-06 18:00:16 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* References:
|
2020-08-06 19:41:45 +02:00
|
|
|
* "STMPE811 S-Touch advanced resistive touchscreen controller with 8-bit
|
2012-05-06 18:00:16 +02:00
|
|
|
* GPIO expander," Doc ID 14489 Rev 6, CD00186725, STMicroelectronics"
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <poll.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
2016-01-30 15:00:16 +01:00
|
|
|
#include <nuttx/i2c/i2c_master.h>
|
2012-05-06 18:00:16 +02:00
|
|
|
#include <nuttx/wqueue.h>
|
2017-03-30 15:38:37 +02:00
|
|
|
#include <nuttx/random.h>
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/input/touchscreen.h>
|
2012-05-29 03:43:51 +02:00
|
|
|
#include <nuttx/input/stmpe811.h>
|
2012-05-06 18:00:16 +02:00
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
#include "stmpe811.h"
|
2012-05-06 18:00:16 +02:00
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
#if defined(CONFIG_INPUT) && defined(CONFIG_INPUT_STMPE811) && !defined(CONFIG_STMPE811_TSC_DISABLE)
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define IO_IT_0 0x01
|
|
|
|
#define IO_IT_1 0x02
|
|
|
|
#define IO_IT_2 0x04
|
|
|
|
#define IO_IT_3 0x08
|
|
|
|
#define IO_IT_4 0x10
|
|
|
|
#define IO_IT_5 0x20
|
|
|
|
#define IO_IT_6 0x40
|
|
|
|
#define IO_IT_7 0x80
|
|
|
|
#define ALL_IT 0xFF
|
|
|
|
#define IOE_JOY_IT (uint8_t)(IO_IT_3 | IO_IT_4 | IO_IT_5 | IO_IT_6 | IO_IT_7)
|
|
|
|
#define IOE_TS_IT (uint8_t)(IO_IT_0 | IO_IT_1 | IO_IT_2)
|
|
|
|
#define IOE_INMEMS_IT (uint8_t)(IO_IT_2 | IO_IT_3)
|
|
|
|
|
2020-08-06 19:41:45 +02:00
|
|
|
#define EDGE_FALLING 0x01
|
|
|
|
#define EDGE_RISING 0x02
|
|
|
|
/* <! The value of the maximal timeout for I2C waiting loops */
|
|
|
|
#define TIMEOUT_MAX 0x3000
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
2020-08-06 19:41:45 +02:00
|
|
|
|
2012-05-06 18:00:16 +02:00
|
|
|
/* Internal logic */
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
static void stmpe811_notify(FAR struct stmpe811_dev_s *priv);
|
|
|
|
static int stmpe811_sample(FAR struct stmpe811_dev_s *priv,
|
|
|
|
FAR struct stmpe811_sample_s *sample);
|
|
|
|
static inline int stmpe811_waitsample(FAR struct stmpe811_dev_s *priv,
|
|
|
|
FAR struct stmpe811_sample_s *sample);
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Character driver methods */
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
static int stmpe811_open(FAR struct file *filep);
|
|
|
|
static int stmpe811_close(FAR struct file *filep);
|
|
|
|
static ssize_t stmpe811_read(FAR struct file *filep, FAR char *buffer,
|
2012-05-06 18:00:16 +02:00
|
|
|
size_t len);
|
2012-05-29 03:43:51 +02:00
|
|
|
static int stmpe811_ioctl(FAR struct file *filep, int cmd,
|
2012-05-06 18:00:16 +02:00
|
|
|
unsigned long arg);
|
2012-05-29 03:43:51 +02:00
|
|
|
static int stmpe811_poll(FAR struct file *filep, struct pollfd *fds,
|
2012-05-06 18:00:16 +02:00
|
|
|
bool setup);
|
|
|
|
|
|
|
|
/* Initialization logic */
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
static inline void stmpe811_tscinitialize(FAR struct stmpe811_dev_s *priv);
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-08-27 17:40:19 +02:00
|
|
|
/* This the vtable that supports the character driver interface */
|
2012-05-06 18:00:16 +02:00
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
static const struct file_operations g_stmpe811fops =
|
2012-05-06 18:00:16 +02:00
|
|
|
{
|
2012-05-29 03:43:51 +02:00
|
|
|
stmpe811_open, /* open */
|
|
|
|
stmpe811_close, /* close */
|
|
|
|
stmpe811_read, /* read */
|
2019-05-22 02:57:54 +02:00
|
|
|
NULL, /* write */
|
|
|
|
NULL, /* seek */
|
|
|
|
stmpe811_ioctl, /* ioctl */
|
|
|
|
stmpe811_poll /* poll */
|
2012-05-06 18:00:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
2020-08-06 19:41:45 +02:00
|
|
|
|
2012-05-06 18:00:16 +02:00
|
|
|
/****************************************************************************
|
2012-05-29 03:43:51 +02:00
|
|
|
* Name: stmpe811_notify
|
2012-05-06 18:00:16 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Notify any threads waiting on touchscreen data that data is now
|
|
|
|
* available for reading.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
static void stmpe811_notify(FAR struct stmpe811_dev_s *priv)
|
2012-05-06 18:00:16 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* If there are threads waiting for read data, then signal one of them
|
|
|
|
* that the read data is available.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->nwaiters > 0)
|
|
|
|
{
|
2012-05-29 03:43:51 +02:00
|
|
|
/* After posting this semaphore, we need to exit because the STMPE811
|
2012-05-06 18:00:16 +02:00
|
|
|
* is no longer available.
|
|
|
|
*/
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->waitsem);
|
2012-05-06 18:00:16 +02:00
|
|
|
}
|
|
|
|
|
2020-08-06 19:41:45 +02:00
|
|
|
/* If there are threads waiting on poll() for STMPE811 data to become
|
|
|
|
* available, then wake them up now. NOTE: we wake up all waiting threads
|
|
|
|
* because we do not know that they are going to do. If they all try to
|
|
|
|
* read the data, then some make end up blocking after all.
|
2012-05-06 18:00:16 +02:00
|
|
|
*/
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
for (i = 0; i < CONFIG_STMPE811_NPOLLWAITERS; i++)
|
2012-05-06 18:00:16 +02:00
|
|
|
{
|
|
|
|
struct pollfd *fds = priv->fds[i];
|
|
|
|
if (fds)
|
|
|
|
{
|
|
|
|
fds->revents |= POLLIN;
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("Report events: %02x\n", fds->revents);
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(fds->sem);
|
2012-05-06 18:00:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-05-29 03:43:51 +02:00
|
|
|
* Name: stmpe811_sample
|
2012-05-06 18:00:16 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if touchscreen sample data is available now and, if so, return
|
2012-05-29 03:43:51 +02:00
|
|
|
* the sample data. This is part of the stmpe811_read logic.
|
2012-05-06 18:00:16 +02:00
|
|
|
*
|
2012-05-12 00:07:06 +02:00
|
|
|
* Assumption:
|
|
|
|
* Pre-emption is disable to prevent the worker thread from running.
|
|
|
|
* Otherwise, sampled data may continue to change.
|
|
|
|
*
|
2012-05-06 18:00:16 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
static int stmpe811_sample(FAR struct stmpe811_dev_s *priv,
|
|
|
|
FAR struct stmpe811_sample_s *sample)
|
2012-05-06 18:00:16 +02:00
|
|
|
{
|
|
|
|
int ret = -EAGAIN;
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
/* Is there new STMPE811 sample data available? */
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
if (priv->penchange)
|
|
|
|
{
|
|
|
|
/* Yes.. the state has changed in some way. Return a copy of the
|
|
|
|
* sampled data.
|
|
|
|
*/
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
memcpy(sample, &priv->sample, sizeof(struct stmpe811_sample_s));
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Now manage state transitions */
|
|
|
|
|
|
|
|
if (sample->contact == CONTACT_UP)
|
|
|
|
{
|
2012-05-11 20:27:46 +02:00
|
|
|
/* The sampling logic has detected pen-up in some condition other
|
|
|
|
* than CONTACT_NONE. Set the next state to CONTACT_NONE: Further
|
|
|
|
* pen-down reports will be ignored. Increment the ID so that
|
|
|
|
* next contact ID will be unique
|
|
|
|
*/
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
priv->sample.contact = CONTACT_NONE;
|
2012-05-13 20:28:43 +02:00
|
|
|
priv->sample.valid = false;
|
2012-05-06 18:00:16 +02:00
|
|
|
priv->id++;
|
|
|
|
}
|
|
|
|
else if (sample->contact == CONTACT_DOWN)
|
2020-08-06 19:41:45 +02:00
|
|
|
{
|
2012-05-11 20:27:46 +02:00
|
|
|
/* The sampling logic has detected pen-up in some condition other
|
2020-08-06 19:41:45 +02:00
|
|
|
* than CONTACT_MOVE. Set the next state to CONTACT_MOVE: Further
|
|
|
|
* samples collected while the pen is down will reported as
|
|
|
|
* movement events.
|
2012-05-11 20:27:46 +02:00
|
|
|
*/
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
priv->sample.contact = CONTACT_MOVE;
|
2020-08-06 19:41:45 +02:00
|
|
|
}
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
priv->penchange = false;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-05-29 03:43:51 +02:00
|
|
|
* Name: stmpe811_waitsample
|
2012-05-06 18:00:16 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Wait for a sample to become available (this is really part of the
|
2012-05-29 03:43:51 +02:00
|
|
|
* stmpe811_read logic).
|
2012-05-06 18:00:16 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
static inline int stmpe811_waitsample(FAR struct stmpe811_dev_s *priv,
|
|
|
|
FAR struct stmpe811_sample_s *sample)
|
2012-05-06 18:00:16 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2012-05-12 00:07:06 +02:00
|
|
|
/* Disable pre-emption to prevent the worker thread from running
|
|
|
|
* asynchronously.
|
2012-05-06 18:00:16 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
|
|
|
|
/* Now release the semaphore that manages mutually exclusive access to
|
|
|
|
* the device structure. This may cause other tasks to become ready to
|
|
|
|
* run, but they cannot run yet because pre-emption is disabled.
|
|
|
|
*/
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->exclsem);
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Try to get the a sample... if we cannot, then wait on the semaphore
|
2014-10-12 01:02:35 +02:00
|
|
|
* that is posted when new sample data is available.
|
2012-05-06 18:00:16 +02:00
|
|
|
*/
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
while (stmpe811_sample(priv, sample) < 0)
|
2012-05-06 18:00:16 +02:00
|
|
|
{
|
2012-05-29 03:43:51 +02:00
|
|
|
/* Wait for a change in the STMPE811 state */
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2012-05-06 18:00:16 +02:00
|
|
|
priv->nwaiters++;
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&priv->waitsem);
|
2012-05-06 18:00:16 +02:00
|
|
|
priv->nwaiters--;
|
|
|
|
|
2012-05-12 00:07:06 +02:00
|
|
|
/* When we are re-awakened, pre-emption will again be disabled */
|
|
|
|
|
2012-05-06 18:00:16 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
ierr("ERROR: nxsem_wait failed: %d\n", ret);
|
2012-05-06 18:00:16 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-27 17:40:19 +02:00
|
|
|
/* Re-acquire the semaphore that manages mutually exclusive access to
|
2020-08-06 19:41:45 +02:00
|
|
|
* the device structure. We may have to wait here. But we have our sample.
|
2012-05-06 18:00:16 +02:00
|
|
|
* Interrupts and pre-emption will be re-enabled while we wait.
|
|
|
|
*/
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&priv->exclsem);
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
errout:
|
|
|
|
/* Restore pre-emption. We might get suspended here but that is okay
|
|
|
|
* because we already have our sample. Note: this means that if there
|
2012-05-29 03:43:51 +02:00
|
|
|
* were two threads reading from the STMPE811 for some reason, the data
|
2012-05-06 18:00:16 +02:00
|
|
|
* might be read out of order.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sched_unlock();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-05-29 03:43:51 +02:00
|
|
|
* Name: stmpe811_open
|
2012-05-06 18:00:16 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Standard character driver open method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
static int stmpe811_open(FAR struct file *filep)
|
2012-05-06 18:00:16 +02:00
|
|
|
{
|
2012-05-29 03:43:51 +02:00
|
|
|
#ifdef CONFIG_STMPE811_REFCNT
|
2012-05-06 18:00:16 +02:00
|
|
|
FAR struct inode *inode;
|
2012-05-29 03:43:51 +02:00
|
|
|
FAR struct stmpe811_dev_s *priv;
|
2012-05-06 18:00:16 +02:00
|
|
|
uint8_t tmp;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(filep);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
2012-05-29 03:43:51 +02:00
|
|
|
priv = (FAR struct stmpe811_dev_s *)inode->i_private;
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Get exclusive access to the driver data structure */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&priv->exclsem);
|
2012-05-06 18:00:16 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
ierr("ERROR: nxsem_wait failed: %d\n", ret);
|
|
|
|
return ret;
|
2012-05-06 18:00:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Increment the reference count */
|
|
|
|
|
|
|
|
tmp = priv->crefs + 1;
|
|
|
|
if (tmp == 0)
|
|
|
|
{
|
|
|
|
/* More than 255 opens; uint8_t overflows to zero */
|
|
|
|
|
|
|
|
ret = -EMFILE;
|
|
|
|
goto errout_with_sem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* When the reference increments to 1, this is the first open event
|
|
|
|
* on the driver.. and an opportunity to do any one-time initialization.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Save the new open count on success */
|
|
|
|
|
|
|
|
priv->crefs = tmp;
|
|
|
|
|
|
|
|
errout_with_sem:
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->exclsem);
|
2012-05-06 18:00:16 +02:00
|
|
|
return ret;
|
|
|
|
#else
|
|
|
|
return OK;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-05-29 03:43:51 +02:00
|
|
|
* Name: stmpe811_close
|
2012-05-06 18:00:16 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Standard character driver close method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
static int stmpe811_close(FAR struct file *filep)
|
2012-05-06 18:00:16 +02:00
|
|
|
{
|
2012-05-29 03:43:51 +02:00
|
|
|
#ifdef CONFIG_STMPE811_REFCNT
|
2012-05-06 18:00:16 +02:00
|
|
|
FAR struct inode *inode;
|
2012-05-29 03:43:51 +02:00
|
|
|
FAR struct stmpe811_dev_s *priv;
|
2012-05-06 18:00:16 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(filep);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
2012-05-29 03:43:51 +02:00
|
|
|
priv = (FAR struct stmpe811_dev_s *)inode->i_private;
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Get exclusive access to the driver data structure */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&priv->exclsem);
|
2012-05-06 18:00:16 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
ierr("ERROR: nxsem_wait failed: %d\n", ret);
|
|
|
|
return ret;
|
2012-05-06 18:00:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Decrement the reference count unless it would decrement a negative
|
|
|
|
* value. When the count decrements to zero, there are no further
|
|
|
|
* open references to the driver.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->crefs >= 1)
|
|
|
|
{
|
|
|
|
priv->crefs--;
|
|
|
|
}
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->exclsem);
|
2012-05-06 18:00:16 +02:00
|
|
|
#endif
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-05-29 03:43:51 +02:00
|
|
|
* Name: stmpe811_read
|
2012-05-06 18:00:16 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Standard character driver read method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-08-06 19:41:45 +02:00
|
|
|
static ssize_t stmpe811_read(FAR struct file *filep,
|
|
|
|
FAR char *buffer, size_t len)
|
2012-05-06 18:00:16 +02:00
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
2012-05-29 03:43:51 +02:00
|
|
|
FAR struct stmpe811_dev_s *priv;
|
2012-05-06 18:00:16 +02:00
|
|
|
FAR struct touch_sample_s *report;
|
2012-05-29 03:43:51 +02:00
|
|
|
struct stmpe811_sample_s sample;
|
2012-05-06 18:00:16 +02:00
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("len=%d\n", len);
|
2012-05-06 18:00:16 +02:00
|
|
|
DEBUGASSERT(filep);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
2012-05-29 03:43:51 +02:00
|
|
|
priv = (FAR struct stmpe811_dev_s *)inode->i_private;
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Verify that the caller has provided a buffer large enough to receive
|
|
|
|
* the touch data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (len < SIZEOF_TOUCH_SAMPLE_S(1))
|
|
|
|
{
|
|
|
|
/* We could provide logic to break up a touch report into segments and
|
|
|
|
* handle smaller reads... but why?
|
|
|
|
*/
|
|
|
|
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get exclusive access to the driver data structure */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&priv->exclsem);
|
2012-05-06 18:00:16 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
ierr("ERROR: nxsem_wait failed: %d\n", ret);
|
|
|
|
return ret;
|
2012-05-06 18:00:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to read sample data. */
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
ret = stmpe811_sample(priv, &sample);
|
2012-05-06 18:00:16 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Sample data is not available now. We would ave to wait to get
|
|
|
|
* receive sample data. If the user has specified the O_NONBLOCK
|
|
|
|
* option, then just return an error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (filep->f_oflags & O_NONBLOCK)
|
|
|
|
{
|
|
|
|
ret = -EAGAIN;
|
|
|
|
goto errout;
|
2020-08-06 19:41:45 +02:00
|
|
|
}
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Wait for sample data */
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
ret = stmpe811_waitsample(priv, &sample);
|
2012-05-06 18:00:16 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* We might have been awakened by a signal */
|
|
|
|
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
/* In any event, we now have sampled STMPE811 data that we can report
|
2012-05-06 18:00:16 +02:00
|
|
|
* to the caller.
|
|
|
|
*/
|
|
|
|
|
|
|
|
report = (FAR struct touch_sample_s *)buffer;
|
|
|
|
memset(report, 0, SIZEOF_TOUCH_SAMPLE_S(1));
|
|
|
|
report->npoints = 1;
|
2012-05-12 00:07:06 +02:00
|
|
|
report->point[0].id = sample.id;
|
2012-05-06 18:00:16 +02:00
|
|
|
report->point[0].x = sample.x;
|
|
|
|
report->point[0].y = sample.y;
|
|
|
|
report->point[0].pressure = sample.z;
|
|
|
|
|
2017-03-30 15:38:37 +02:00
|
|
|
add_ui_randomness((sample.x << 16) ^ (sample.y << 8) ^ sample.z);
|
|
|
|
|
2012-05-06 18:00:16 +02:00
|
|
|
/* Report the appropriate flags */
|
|
|
|
|
|
|
|
if (sample.contact == CONTACT_UP)
|
|
|
|
{
|
2012-05-13 20:28:43 +02:00
|
|
|
/* Pen is now up. Is the positional data valid? This is important to
|
|
|
|
* know because the release will be sent to the window based on its
|
|
|
|
* last positional data.
|
|
|
|
*/
|
2012-05-06 18:00:16 +02:00
|
|
|
|
2012-05-13 20:28:43 +02:00
|
|
|
if (sample.valid)
|
2012-05-06 18:00:16 +02:00
|
|
|
{
|
2012-05-13 20:28:43 +02:00
|
|
|
report->point[0].flags = TOUCH_UP | TOUCH_ID_VALID |
|
2012-05-11 20:27:46 +02:00
|
|
|
TOUCH_POS_VALID | TOUCH_PRESSURE_VALID;
|
2012-05-06 18:00:16 +02:00
|
|
|
}
|
2012-05-13 20:28:43 +02:00
|
|
|
else
|
2012-05-06 18:00:16 +02:00
|
|
|
{
|
2012-05-13 20:28:43 +02:00
|
|
|
report->point[0].flags = TOUCH_UP | TOUCH_ID_VALID;
|
2012-05-06 18:00:16 +02:00
|
|
|
}
|
|
|
|
}
|
2012-05-13 20:28:43 +02:00
|
|
|
else if (sample.contact == CONTACT_DOWN)
|
|
|
|
{
|
|
|
|
/* First contact */
|
|
|
|
|
|
|
|
report->point[0].flags = TOUCH_DOWN | TOUCH_ID_VALID |
|
|
|
|
TOUCH_POS_VALID | TOUCH_PRESSURE_VALID;
|
|
|
|
}
|
|
|
|
else /* if (sample->contact == CONTACT_MOVE) */
|
|
|
|
{
|
|
|
|
/* Movement of the same contact */
|
|
|
|
|
|
|
|
report->point[0].flags = TOUCH_MOVE | TOUCH_ID_VALID |
|
|
|
|
TOUCH_POS_VALID | TOUCH_PRESSURE_VALID;
|
|
|
|
}
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
ret = SIZEOF_TOUCH_SAMPLE_S(1);
|
|
|
|
|
|
|
|
errout:
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->exclsem);
|
2012-05-06 18:00:16 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-05-29 03:43:51 +02:00
|
|
|
* Name: stmpe811_ioctl
|
2015-10-10 18:41:00 +02:00
|
|
|
*
|
2012-05-06 18:00:16 +02:00
|
|
|
* Description:
|
|
|
|
* Standard character driver ioctl method.
|
|
|
|
*
|
2015-10-10 18:41:00 +02:00
|
|
|
****************************************************************************/
|
2012-05-06 18:00:16 +02:00
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
static int stmpe811_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
2012-05-06 18:00:16 +02:00
|
|
|
{
|
2012-05-29 03:43:51 +02:00
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct stmpe811_dev_s *priv;
|
|
|
|
int ret;
|
2012-05-06 18:00:16 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("cmd: %d arg: %ld\n", cmd, arg);
|
2012-05-06 18:00:16 +02:00
|
|
|
DEBUGASSERT(filep);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
2012-05-29 03:43:51 +02:00
|
|
|
priv = (FAR struct stmpe811_dev_s *)inode->i_private;
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Get exclusive access to the driver data structure */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&priv->exclsem);
|
2012-05-06 18:00:16 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
ierr("ERROR: nxsem_wait failed: %d\n", ret);
|
|
|
|
return ret;
|
2012-05-06 18:00:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process the IOCTL by command */
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case TSIOC_SETFREQUENCY: /* arg: Pointer to uint32_t frequency value */
|
|
|
|
{
|
|
|
|
FAR uint32_t *ptr = (FAR uint32_t *)((uintptr_t)arg);
|
|
|
|
DEBUGASSERT(priv->config != NULL && ptr != NULL);
|
2016-02-01 23:31:13 +01:00
|
|
|
priv->config->frequency = *ptr;
|
2012-05-06 18:00:16 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TSIOC_GETFREQUENCY: /* arg: Pointer to uint32_t frequency value */
|
|
|
|
{
|
|
|
|
FAR uint32_t *ptr = (FAR uint32_t *)((uintptr_t)arg);
|
|
|
|
DEBUGASSERT(priv->config != NULL && ptr != NULL);
|
|
|
|
*ptr = priv->config->frequency;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->exclsem);
|
2012-05-06 18:00:16 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-05-29 03:43:51 +02:00
|
|
|
* Name: stmpe811_poll
|
2012-05-06 18:00:16 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Standard character driver poll method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
static int stmpe811_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup)
|
2012-05-06 18:00:16 +02:00
|
|
|
{
|
2012-05-29 03:43:51 +02:00
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct stmpe811_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
int i;
|
2012-05-06 18:00:16 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("setup: %d\n", (int)setup);
|
2012-05-06 18:00:16 +02:00
|
|
|
DEBUGASSERT(filep && fds);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
2012-05-29 03:43:51 +02:00
|
|
|
priv = (FAR struct stmpe811_dev_s *)inode->i_private;
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Are we setting up the poll? Or tearing it down? */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&priv->exclsem);
|
2012-05-06 18:00:16 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* This should only happen if the wait was canceled by an signal */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ierr("ERROR: nxsem_wait failed: %d\n", ret);
|
|
|
|
return ret;
|
2012-05-06 18:00:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (setup)
|
|
|
|
{
|
|
|
|
/* Ignore waits that do not include POLLIN */
|
|
|
|
|
|
|
|
if ((fds->events & POLLIN) == 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ierr("ERROR: Missing POLLIN: revents: %08x\n", fds->revents);
|
2012-05-06 18:00:16 +02:00
|
|
|
ret = -EDEADLK;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is a request to set up the poll. Find an available
|
|
|
|
* slot for the poll structure reference
|
|
|
|
*/
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
for (i = 0; i < CONFIG_STMPE811_NPOLLWAITERS; i++)
|
2012-05-06 18:00:16 +02:00
|
|
|
{
|
|
|
|
/* Find an available slot */
|
|
|
|
|
|
|
|
if (!priv->fds[i])
|
|
|
|
{
|
|
|
|
/* Bind the poll structure and this slot */
|
|
|
|
|
|
|
|
priv->fds[i] = fds;
|
|
|
|
fds->priv = &priv->fds[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
if (i >= CONFIG_STMPE811_NPOLLWAITERS)
|
2012-05-06 18:00:16 +02:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ierr("ERROR: No available slot found: %d\n", i);
|
2012-05-06 18:00:16 +02:00
|
|
|
fds->priv = NULL;
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Should we immediately notify on any of the requested events? */
|
|
|
|
|
|
|
|
if (priv->penchange)
|
|
|
|
{
|
2012-05-29 03:43:51 +02:00
|
|
|
stmpe811_notify(priv);
|
2012-05-06 18:00:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (fds->priv)
|
|
|
|
{
|
|
|
|
/* This is a request to tear down the poll. */
|
|
|
|
|
|
|
|
struct pollfd **slot = (struct pollfd **)fds->priv;
|
|
|
|
DEBUGASSERT(slot != NULL);
|
|
|
|
|
|
|
|
/* Remove all memory of the poll setup */
|
|
|
|
|
|
|
|
*slot = NULL;
|
|
|
|
fds->priv = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->exclsem);
|
2012-05-06 18:00:16 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-05-21 19:36:26 +02:00
|
|
|
/****************************************************************************
|
2012-05-29 03:43:51 +02:00
|
|
|
* Name: stmpe811_timeoutworker
|
2012-05-21 19:36:26 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* A timer has expired without receiving a pen up event. Check again.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
static void stmpe811_timeoutworker(FAR void *arg)
|
2012-05-21 19:36:26 +02:00
|
|
|
{
|
2012-05-29 03:43:51 +02:00
|
|
|
FAR struct stmpe811_dev_s *priv = (FAR struct stmpe811_dev_s *)arg;
|
2012-05-21 19:36:26 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Treat the timeout just like an interrupt occurred */
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
stmpe811_tscworker(priv, stmpe811_getreg8(priv, STMPE811_INT_STA));
|
2012-05-21 19:36:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-05-29 03:43:51 +02:00
|
|
|
* Name: stmpe811_timeout
|
2012-05-21 19:36:26 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* A timer has expired without receiving a pen up event. Schedule work
|
|
|
|
* to check again.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-08-09 20:29:35 +02:00
|
|
|
static void stmpe811_timeout(wdparm_t arg)
|
2012-05-21 19:36:26 +02:00
|
|
|
{
|
2020-08-09 20:29:35 +02:00
|
|
|
FAR struct stmpe811_dev_s *priv = (FAR struct stmpe811_dev_s *)arg;
|
2012-05-21 19:36:26 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Are we still stuck in the pen down state? */
|
|
|
|
|
2014-02-11 01:08:49 +01:00
|
|
|
if (priv->sample.contact == CONTACT_DOWN ||
|
2012-05-21 19:36:26 +02:00
|
|
|
priv->sample.contact == CONTACT_MOVE)
|
|
|
|
{
|
|
|
|
/* Yes... is the worker thread available? If not, then apparently
|
|
|
|
* we have work already pending?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (work_available(&priv->timeout))
|
|
|
|
{
|
2012-05-29 03:43:51 +02:00
|
|
|
/* Yes.. Transfer processing to the worker thread. Since STMPE811
|
2012-05-21 19:36:26 +02:00
|
|
|
* interrupts are disabled while the work is pending, no special
|
|
|
|
* action should be required to protect the work queue.
|
|
|
|
*/
|
|
|
|
|
2020-08-06 19:41:45 +02:00
|
|
|
ret = work_queue(HPWORK, &priv->timeout,
|
|
|
|
stmpe811_timeoutworker, priv, 0);
|
2012-05-21 19:36:26 +02:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-20 20:44:38 +02:00
|
|
|
ierr("ERROR: Failed to queue work: %d\n", ret);
|
2012-05-21 19:36:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-06 18:00:16 +02:00
|
|
|
/****************************************************************************
|
2012-05-29 03:43:51 +02:00
|
|
|
* Name: stmpe811_tscinitialize
|
2012-05-06 18:00:16 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the touchscreen controller. This is really a part of the
|
2012-05-29 03:43:51 +02:00
|
|
|
* stmpe811_register logic,
|
2012-05-06 18:00:16 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
static inline void stmpe811_tscinitialize(FAR struct stmpe811_dev_s *priv)
|
2012-05-06 18:00:16 +02:00
|
|
|
{
|
2012-05-09 00:10:29 +02:00
|
|
|
uint8_t regval;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("Initializing touchscreen controller\n");
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Enable TSC and ADC functions */
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
regval = stmpe811_getreg8(priv, STMPE811_SYS_CTRL2);
|
2012-05-06 18:00:16 +02:00
|
|
|
regval &= ~(SYS_CTRL2_TSC_OFF | SYS_CTRL2_ADC_OFF);
|
2012-05-29 03:43:51 +02:00
|
|
|
stmpe811_putreg8(priv, STMPE811_SYS_CTRL2, regval);
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Enable the TSC global interrupts */
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
regval = stmpe811_getreg8(priv, STMPE811_INT_EN);
|
2012-05-06 18:00:16 +02:00
|
|
|
regval |= (uint32_t)(INT_TOUCH_DET | INT_FIFO_TH | INT_FIFO_OFLOW);
|
2012-05-29 03:43:51 +02:00
|
|
|
stmpe811_putreg8(priv, STMPE811_INT_EN, regval);
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Select Sample Time, bit number and ADC Reference */
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
stmpe811_putreg8(priv, STMPE811_ADC_CTRL1, priv->config->ctrl1);
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Wait for 20 ms */
|
|
|
|
|
|
|
|
up_mdelay(20);
|
|
|
|
|
|
|
|
/* Select the ADC clock speed */
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
stmpe811_putreg8(priv, STMPE811_ADC_CTRL2, priv->config->ctrl2);
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Select TSC pins in non-GPIO mode (AF=0) */
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
regval = stmpe811_getreg8(priv, STMPE811_GPIO_AF);
|
2012-05-06 18:00:16 +02:00
|
|
|
regval &= ~(uint8_t)TSC_PIN_SET;
|
2012-05-29 03:43:51 +02:00
|
|
|
stmpe811_putreg8(priv, STMPE811_GPIO_AF, regval);
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Select 2 nF filter capacitor */
|
|
|
|
|
2020-08-06 19:41:45 +02:00
|
|
|
stmpe811_putreg8(priv, STMPE811_TSC_CFG, TSC_CFG_AVE_CTRL_4SAMPLES |
|
|
|
|
TSC_CFG_TOUCH_DELAY_500US | TSC_CFG_SETTLING_500US);
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Select single point reading */
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
stmpe811_putreg8(priv, STMPE811_FIFO_TH, 1);
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Reset and clear the FIFO. */
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
stmpe811_putreg8(priv, STMPE811_FIFO_STA, FIFO_STA_FIFO_RESET);
|
|
|
|
stmpe811_putreg8(priv, STMPE811_FIFO_STA, 0);
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* set the data format for Z value: 7 fractional part and 1 whole part */
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
stmpe811_putreg8(priv, STMPE811_TSC_FRACTIONZ, 0x01);
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Set the driving capability of the device for TSC pins: 50mA */
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
stmpe811_putreg8(priv, STMPE811_TSC_IDRIVE, TSC_IDRIVE_50MA);
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Enable the TSC. Use no tracking index, touch-screen controller
|
2015-10-10 18:41:00 +02:00
|
|
|
* operation mode (XYZ).
|
2012-05-06 18:00:16 +02:00
|
|
|
*/
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
stmpe811_putreg8(priv, STMPE811_TSC_CTRL, TSC_CTRL_EN);
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Clear all the status pending bits */
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
stmpe811_putreg8(priv, STMPE811_INT_STA, INT_ALL);
|
2012-05-06 18:00:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-05-29 03:43:51 +02:00
|
|
|
* Name: stmpe811_register
|
2012-05-06 18:00:16 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Enable TSC functionality. GPIO4-7 must be available. This function
|
2014-10-12 01:02:35 +02:00
|
|
|
* will register the touchscreen driver as /dev/inputN where N is the minor
|
2012-05-06 18:00:16 +02:00
|
|
|
* device number
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2012-05-29 03:43:51 +02:00
|
|
|
* handle - The handle previously returned by stmpe811_register
|
2012-05-06 18:00:16 +02:00
|
|
|
* minor - The input device minor number
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned on success. Otherwise, a negated errno value is
|
|
|
|
* returned to indicate the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
int stmpe811_register(STMPE811_HANDLE handle, int minor)
|
2012-05-06 18:00:16 +02:00
|
|
|
{
|
2012-05-29 03:43:51 +02:00
|
|
|
FAR struct stmpe811_dev_s *priv = (FAR struct stmpe811_dev_s *)handle;
|
2012-05-09 00:10:29 +02:00
|
|
|
char devname[DEV_NAMELEN];
|
2012-05-06 18:00:16 +02:00
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("handle=%p minor=%d\n", handle, minor);
|
2012-05-06 18:00:16 +02:00
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Get exclusive access to the device structure */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&priv->exclsem);
|
2012-05-06 18:00:16 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
ierr("ERROR: nxsem_wait failed: %d\n", ret);
|
|
|
|
return ret;
|
2012-05-06 18:00:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that the pins (4-7) need by the TSC are not already in use */
|
|
|
|
|
|
|
|
if ((priv->inuse & TSC_PIN_SET) != 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ierr("ERROR: TSC pins is already in-use: %02x\n", priv->inuse);
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->exclsem);
|
2012-05-06 18:00:16 +02:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2012-05-21 19:36:26 +02:00
|
|
|
/* Initialize the TS structure fields to their default values */
|
2012-05-06 18:00:16 +02:00
|
|
|
|
2012-05-09 00:10:29 +02:00
|
|
|
priv->minor = minor;
|
|
|
|
priv->penchange = false;
|
|
|
|
priv->threshx = 0;
|
|
|
|
priv->threshy = 0;
|
|
|
|
|
|
|
|
/* Register the character driver */
|
|
|
|
|
|
|
|
snprintf(devname, DEV_NAMELEN, DEV_FORMAT, minor);
|
2012-05-29 03:43:51 +02:00
|
|
|
ret = register_driver(devname, &g_stmpe811fops, 0666, priv);
|
2012-05-09 00:10:29 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ierr("ERROR: Failed to register driver %s: %d\n", devname, ret);
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->exclsem);
|
2012-05-09 00:10:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Initialize the touchscreen controller */
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
stmpe811_tscinitialize(priv);
|
2012-05-06 18:00:16 +02:00
|
|
|
|
2012-05-09 00:10:29 +02:00
|
|
|
/* Inidicate that the touchscreen controller was successfully initialized */
|
2012-05-06 18:00:16 +02:00
|
|
|
|
2012-05-09 00:10:29 +02:00
|
|
|
priv->inuse |= TSC_PIN_SET; /* Pins 4-7 are now in-use */
|
2020-08-06 19:41:45 +02:00
|
|
|
priv->flags |= STMPE811_FLAGS_TSC_INITIALIZED; /* TSC function is initialized */
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->exclsem);
|
2012-05-09 00:10:29 +02:00
|
|
|
return ret;
|
2012-05-06 18:00:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-05-29 03:43:51 +02:00
|
|
|
* Name: stmpe811_tscworker
|
2012-05-06 18:00:16 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is called to handle a TSC interrupt. It is not really
|
2012-05-29 03:43:51 +02:00
|
|
|
* an interrupt handle because it is called from the STMPE811 "bottom half"
|
2012-05-06 18:00:16 +02:00
|
|
|
* logic that runs on the worker thread.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
void stmpe811_tscworker(FAR struct stmpe811_dev_s *priv, uint8_t intsta)
|
2012-05-06 18:00:16 +02:00
|
|
|
{
|
2014-10-12 01:02:35 +02:00
|
|
|
uint16_t xdiff; /* X difference used in thresholding */
|
|
|
|
uint16_t ydiff; /* Y difference used in thresholding */
|
|
|
|
uint16_t x; /* X position */
|
|
|
|
uint16_t y; /* Y position */
|
|
|
|
bool pendown; /* true: pen is down */
|
2012-05-06 18:00:16 +02:00
|
|
|
|
2018-08-24 14:58:30 +02:00
|
|
|
DEBUGASSERT(priv != NULL);
|
2012-05-06 18:00:16 +02:00
|
|
|
|
2012-05-21 19:36:26 +02:00
|
|
|
/* Cancel the missing pen up timer */
|
|
|
|
|
2020-08-04 12:31:31 +02:00
|
|
|
wd_cancel(&priv->wdog);
|
2012-05-21 19:36:26 +02:00
|
|
|
|
2020-08-06 19:41:45 +02:00
|
|
|
/* Check for pen up or down from the TSC_STA bit in STMPE811_TSC_CTRL. */
|
2012-05-06 18:00:16 +02:00
|
|
|
|
2020-08-06 19:41:45 +02:00
|
|
|
pendown = !!(stmpe811_getreg8(priv, STMPE811_TSC_CTRL) & TSC_CTRL_TSC_STA);
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Handle the change from pen down to pen up */
|
|
|
|
|
|
|
|
if (!pendown)
|
|
|
|
{
|
2020-08-06 19:41:45 +02:00
|
|
|
/* The pen is up.. reset thresholding variables. FIFOs will read zero
|
|
|
|
* if there is no data available (hence the choice of (0,0))
|
2012-05-06 18:00:16 +02:00
|
|
|
*/
|
|
|
|
|
2012-05-11 20:27:46 +02:00
|
|
|
priv->threshx = 0;
|
|
|
|
priv->threshy = 0;
|
|
|
|
|
2020-08-06 19:41:45 +02:00
|
|
|
/* Ignore the interrupt if the pen was already up (CONTACT_NONE == pen
|
|
|
|
* up and already reported; CONTACT_UP == pen up, but not reported)
|
2012-05-11 20:27:46 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->sample.contact == CONTACT_NONE ||
|
|
|
|
priv->sample.contact == CONTACT_UP)
|
2012-05-06 18:00:16 +02:00
|
|
|
{
|
2012-05-11 20:27:46 +02:00
|
|
|
goto ignored;
|
2012-05-06 18:00:16 +02:00
|
|
|
}
|
2012-05-11 20:27:46 +02:00
|
|
|
|
2020-08-06 19:41:45 +02:00
|
|
|
/* A pen-down to up transition has been detected. CONTACT_UP indicates
|
|
|
|
* the initial loss of contact. The state will be changed to
|
|
|
|
* CONTACT_NONE after the loss of contact is sampled.
|
2012-05-11 20:27:46 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
priv->sample.contact = CONTACT_UP;
|
2012-05-06 18:00:16 +02:00
|
|
|
}
|
2012-05-11 20:27:46 +02:00
|
|
|
|
|
|
|
/* The pen is down... check for data in the FIFO */
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
else if ((intsta & (INT_FIFO_TH | INT_FIFO_OFLOW)) != 0)
|
2012-05-06 18:00:16 +02:00
|
|
|
{
|
2012-05-12 00:07:06 +02:00
|
|
|
/* Read the next x and y positions from the FIFO. */
|
2012-05-06 18:00:16 +02:00
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
#ifdef CONFIG_STMPE811_SWAPXY
|
|
|
|
x = stmpe811_getreg16(priv, STMPE811_TSC_DATAX);
|
|
|
|
y = stmpe811_getreg16(priv, STMPE811_TSC_DATAY);
|
2012-05-09 00:10:29 +02:00
|
|
|
#else
|
2012-05-29 03:43:51 +02:00
|
|
|
x = stmpe811_getreg16(priv, STMPE811_TSC_DATAY);
|
|
|
|
y = stmpe811_getreg16(priv, STMPE811_TSC_DATAX);
|
2012-05-09 00:10:29 +02:00
|
|
|
#endif
|
2012-05-06 18:00:16 +02:00
|
|
|
|
2012-05-12 00:07:06 +02:00
|
|
|
/* If we have not yet processed the last pen up event, then we
|
|
|
|
* cannot handle this pen down event. We will have to discard it. That
|
|
|
|
* should be okay because there will be another FIFO event right behind
|
|
|
|
* this one. Other kinds of data overruns are not harmful.
|
|
|
|
*
|
|
|
|
* Hmm.. a better design might be to disable FIFO interrupts when we
|
|
|
|
* detect pen up. Then re-enable them when CONTACT_UP is reported.
|
|
|
|
* That would save processing interrupts just to discard the data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->sample.contact == CONTACT_UP)
|
|
|
|
{
|
|
|
|
/* We have not closed the loop on the last touch ... don't report
|
|
|
|
* anything.
|
|
|
|
*/
|
|
|
|
|
|
|
|
goto ignored;
|
|
|
|
}
|
|
|
|
|
2020-08-06 19:41:45 +02:00
|
|
|
/* Perform a thresholding operation so that the results will be more
|
|
|
|
* stable. If the difference from the last sample is small, then ignore
|
|
|
|
* the event. REVISIT: Should a large change in pressure also generate
|
|
|
|
* a event?
|
2012-05-14 23:57:38 +02:00
|
|
|
*/
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
xdiff = x > priv->threshx ? (x - priv->threshx) : (priv->threshx - x);
|
|
|
|
ydiff = y > priv->threshy ? (y - priv->threshy) : (priv->threshy - y);
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
if (xdiff < CONFIG_STMPE811_THRESHX && ydiff < CONFIG_STMPE811_THRESHY)
|
2012-05-06 18:00:16 +02:00
|
|
|
{
|
2020-08-06 19:41:45 +02:00
|
|
|
/* Little or no change in either direction ...
|
|
|
|
* don't report anything.
|
|
|
|
*/
|
2012-05-11 20:27:46 +02:00
|
|
|
|
|
|
|
goto ignored;
|
2012-05-06 18:00:16 +02:00
|
|
|
}
|
|
|
|
|
2012-05-11 20:27:46 +02:00
|
|
|
/* When we see a big difference, snap to the new x/y thresholds */
|
|
|
|
|
2012-05-13 20:28:43 +02:00
|
|
|
priv->threshx = x;
|
|
|
|
priv->threshy = y;
|
2012-05-11 20:27:46 +02:00
|
|
|
|
|
|
|
/* Update the x/y position in the sample data */
|
2012-05-06 18:00:16 +02:00
|
|
|
|
2012-05-13 20:28:43 +02:00
|
|
|
priv->sample.x = priv->threshx;
|
|
|
|
priv->sample.y = priv->threshy;
|
2012-05-06 18:00:16 +02:00
|
|
|
|
2012-05-11 20:27:46 +02:00
|
|
|
/* Update the Z pressure index */
|
2012-05-06 18:00:16 +02:00
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
priv->sample.z = stmpe811_getreg8(priv, STMPE811_TSC_DATAZ);
|
2012-05-13 20:28:43 +02:00
|
|
|
priv->sample.valid = true;
|
2012-05-06 18:00:16 +02:00
|
|
|
|
2012-05-11 20:27:46 +02:00
|
|
|
/* If this is the first (acknowledged) pen down report, then report
|
2012-05-06 18:00:16 +02:00
|
|
|
* this as the first contact. If contact == CONTACT_DOWN, it will be
|
|
|
|
* set to set to CONTACT_MOVE after the contact is first sampled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->sample.contact != CONTACT_MOVE)
|
|
|
|
{
|
|
|
|
/* First contact */
|
|
|
|
|
|
|
|
priv->sample.contact = CONTACT_DOWN;
|
|
|
|
}
|
|
|
|
}
|
2012-05-11 20:27:46 +02:00
|
|
|
|
|
|
|
/* Pen down, but no data in FIFO */
|
|
|
|
|
|
|
|
else
|
2012-05-06 18:00:16 +02:00
|
|
|
{
|
2012-05-11 20:27:46 +02:00
|
|
|
/* Ignore the interrupt... wait until there is data in the FIFO */
|
2012-05-06 18:00:16 +02:00
|
|
|
|
2012-05-11 20:27:46 +02:00
|
|
|
goto ignored;
|
2012-05-06 18:00:16 +02:00
|
|
|
}
|
|
|
|
|
2012-05-11 20:27:46 +02:00
|
|
|
/* We get here if (1) we just went from a pen down to a pen up state OR (2)
|
|
|
|
* We just get a measurement from the FIFO in a pen down state. Indicate
|
|
|
|
* the availability of new sample data for this ID.
|
|
|
|
*/
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
priv->sample.id = priv->id;
|
|
|
|
priv->penchange = true;
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
/* Notify any waiters that new STMPE811 data is available */
|
2012-05-06 18:00:16 +02:00
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
stmpe811_notify(priv);
|
2012-05-11 20:27:46 +02:00
|
|
|
|
2014-10-12 01:02:35 +02:00
|
|
|
/* If we think that the pen is still down, the start/re-start the pen up
|
2012-05-21 19:36:26 +02:00
|
|
|
* timer.
|
|
|
|
*/
|
2012-05-11 20:27:46 +02:00
|
|
|
|
|
|
|
ignored:
|
2014-02-11 01:08:49 +01:00
|
|
|
if (priv->sample.contact == CONTACT_DOWN ||
|
2012-05-21 19:36:26 +02:00
|
|
|
priv->sample.contact == CONTACT_MOVE)
|
|
|
|
{
|
2020-08-04 12:31:31 +02:00
|
|
|
wd_start(&priv->wdog, STMPE811_PENUP_TICKS,
|
2020-08-09 20:29:35 +02:00
|
|
|
stmpe811_timeout, (wdparm_t)priv);
|
2012-05-21 19:36:26 +02:00
|
|
|
}
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2012-05-21 19:36:26 +02:00
|
|
|
/* Reset and clear all data in the FIFO */
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
stmpe811_putreg8(priv, STMPE811_FIFO_STA, FIFO_STA_FIFO_RESET);
|
|
|
|
stmpe811_putreg8(priv, STMPE811_FIFO_STA, 0);
|
2012-05-06 18:00:16 +02:00
|
|
|
}
|
|
|
|
|
2012-05-29 03:43:51 +02:00
|
|
|
#endif /* CONFIG_INPUT && CONFIG_INPUT_STMPE811 && !CONFIG_STMPE811_TSC_DISABLE */
|