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
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2012-05-06 18:00:16 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-05-06 18:00:16 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2012-05-06 18:00:16 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-04 07:10:42 +01:00
|
|
|
/* References:
|
|
|
|
* "STMPE811 S-Touch advanced resistive touchscreen controller with 8-bit
|
|
|
|
* GPIO expander," Doc ID 14489 Rev 6, CD00186725, STMicroelectronics"
|
|
|
|
*/
|
|
|
|
|
2012-05-06 18:00:16 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* 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,
|
2022-04-12 12:16:13 +02:00
|
|
|
FAR struct stmpe811_sample_s *sample);
|
2012-05-29 03:43:51 +02:00
|
|
|
static inline int stmpe811_waitsample(FAR struct stmpe811_dev_s *priv,
|
2022-04-12 12:16:13 +02:00
|
|
|
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,
|
2022-04-12 12:16:13 +02:00
|
|
|
size_t len);
|
2012-05-29 03:43:51 +02:00
|
|
|
static int stmpe811_ioctl(FAR struct file *filep, int cmd,
|
2022-04-12 12:16:13 +02:00
|
|
|
unsigned long arg);
|
2024-08-25 01:21:12 +02:00
|
|
|
static int stmpe811_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
2022-04-12 12:16:13 +02:00
|
|
|
bool setup);
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* 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 */
|
2023-01-02 14:02:51 +01:00
|
|
|
NULL, /* mmap */
|
2023-01-02 18:06:12 +01:00
|
|
|
NULL, /* truncate */
|
2019-05-22 02:57:54 +02:00
|
|
|
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
|
|
|
{
|
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
|
|
|
*/
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(priv->fds, CONFIG_STMPE811_NPOLLWAITERS, POLLIN);
|
2020-09-26 14:25:03 +02:00
|
|
|
|
|
|
|
/* If there are threads waiting for read data, then signal one of them
|
|
|
|
* that the read data is available.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->nwaiters > 0)
|
|
|
|
{
|
|
|
|
/* After posting this semaphore, we need to exit because the STMPE811
|
|
|
|
* is no longer available.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nxsem_post(&priv->waitsem);
|
|
|
|
}
|
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;
|
2023-08-09 09:02:54 +02:00
|
|
|
irqstate_t flags;
|
2012-05-06 18:00:16 +02:00
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
2023-08-09 09:02:54 +02:00
|
|
|
flags = enter_critical_section();
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
errout:
|
2023-08-09 09:02:54 +02:00
|
|
|
/* Then re-enable interrupts. We might get interrupt here and there
|
|
|
|
* could be a new sample. But no new threads will run because we still
|
|
|
|
* have pre-emption disabled.
|
2012-05-06 18:00:16 +02:00
|
|
|
*/
|
|
|
|
|
2023-08-09 09:02:54 +02:00
|
|
|
leave_critical_section(flags);
|
2012-05-06 18:00:16 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2023-08-29 06:16:49 +02:00
|
|
|
priv = inode->i_private;
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Get exclusive access to the driver data structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
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;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2012-05-06 18:00:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
|
|
|
nxmutex_unlock(&priv->lock);
|
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;
|
|
|
|
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2023-08-29 06:16:49 +02:00
|
|
|
priv = inode->i_private;
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Get exclusive access to the driver data structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
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--;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
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
|
|
|
inode = filep->f_inode;
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2023-08-29 06:16:49 +02:00
|
|
|
priv = 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 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
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:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
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
|
|
|
inode = filep->f_inode;
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2023-08-29 06:16:49 +02:00
|
|
|
priv = inode->i_private;
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Get exclusive access to the driver data structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
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;
|
|
|
|
|
2023-07-03 10:47:50 +02:00
|
|
|
case TSIOC_GETOFFSETX: /* arg: Pointer to int offsetx config value */
|
|
|
|
{
|
|
|
|
FAR int *ptr = (FAR int *)((uintptr_t)arg);
|
|
|
|
DEBUGASSERT(ptr != NULL);
|
|
|
|
*ptr = CONFIG_STMPE811_OFFSETX;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TSIOC_GETOFFSETY: /* arg: Pointer to int offsety config value */
|
|
|
|
{
|
|
|
|
FAR int *ptr = (FAR int *)((uintptr_t)arg);
|
|
|
|
DEBUGASSERT(ptr != NULL);
|
|
|
|
*ptr = CONFIG_STMPE811_OFFSETY;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TSIOC_GETTHRESHX: /* arg: Pointer to int threshx config value */
|
|
|
|
{
|
|
|
|
FAR int *ptr = (FAR int *)((uintptr_t)arg);
|
|
|
|
DEBUGASSERT(ptr != NULL);
|
|
|
|
*ptr = CONFIG_STMPE811_THRESHX;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TSIOC_GETTHRESHY: /* arg: Pointer to int threshy config value */
|
|
|
|
{
|
|
|
|
FAR int *ptr = (FAR int *)((uintptr_t)arg);
|
|
|
|
DEBUGASSERT(ptr != NULL);
|
|
|
|
*ptr = CONFIG_STMPE811_THRESHY;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-05-06 18:00:16 +02:00
|
|
|
default:
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
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);
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(fds);
|
2012-05-06 18:00:16 +02:00
|
|
|
inode = filep->f_inode;
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2023-08-29 06:16:49 +02:00
|
|
|
priv = inode->i_private;
|
2012-05-06 18:00:16 +02:00
|
|
|
|
|
|
|
/* Are we setting up the poll? Or tearing it down? */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
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)
|
|
|
|
{
|
2022-04-01 14:59:55 +02:00
|
|
|
ierr("ERROR: Missing POLLIN: revents: %08" PRIx32 "\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);
|
2023-11-19 12:19:53 +01:00
|
|
|
fds->priv = NULL;
|
|
|
|
ret = -EBUSY;
|
2012-05-06 18:00:16 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Should we immediately notify on any of the requested events? */
|
|
|
|
|
|
|
|
if (priv->penchange)
|
|
|
|
{
|
2023-11-19 12:19:53 +01:00
|
|
|
poll_notify(&fds, 1, POLLIN);
|
2012-05-06 18:00:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (fds->priv)
|
|
|
|
{
|
|
|
|
/* This is a request to tear down the poll. */
|
|
|
|
|
2024-08-25 01:21:12 +02:00
|
|
|
FAR struct pollfd **slot = (FAR struct pollfd **)fds->priv;
|
2012-05-06 18:00:16 +02:00
|
|
|
DEBUGASSERT(slot != NULL);
|
|
|
|
|
|
|
|
/* Remove all memory of the poll setup */
|
|
|
|
|
2023-11-19 12:19:53 +01:00
|
|
|
*slot = NULL;
|
|
|
|
fds->priv = NULL;
|
2012-05-06 18:00:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
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 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
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);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
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 */
|
|
|
|
|
2022-04-12 12:16:13 +02:00
|
|
|
snprintf(devname, sizeof(devname), 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);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
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 */
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
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
|
|
|
|
2023-08-09 09:02:54 +02:00
|
|
|
/* Get exclusive access to the driver data structure */
|
|
|
|
|
|
|
|
nxmutex_lock(&priv->lock);
|
|
|
|
|
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:
|
2023-08-09 09:02:54 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
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 */
|