2011-10-03 18:02:22 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/input/ads7843e.c
|
|
|
|
*
|
2021-05-27 11:12:43 +02: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
|
2011-10-03 18:02:22 +02:00
|
|
|
*
|
2021-05-27 11:12:43 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-10-03 18:02:22 +02:00
|
|
|
*
|
2021-05-27 11:12:43 +02: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.
|
2011-10-03 18:02:22 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-04 07:10:42 +01:00
|
|
|
/* References:
|
|
|
|
* "Touch Screen Controller, ADS7843," Burr-Brown Products from Texas
|
|
|
|
* Instruments, SBAS090B, September 2000, Revised May 2002"
|
|
|
|
*
|
|
|
|
* See also:
|
|
|
|
* "Low Voltage I/O Touch Screen Controller, TSC2046," Burr-Brown Products
|
|
|
|
* from Texas Instruments, SBAS265F, October 2002, Revised August 2007.
|
|
|
|
*
|
|
|
|
* "XPT2046 Data Sheet," Shenzhen XPTek Technology Co., Ltd, 2007
|
|
|
|
*/
|
|
|
|
|
2011-10-03 18:02:22 +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>
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
#include <nuttx/irq.h>
|
2011-10-03 18:02:22 +02:00
|
|
|
#include <nuttx/arch.h>
|
2014-08-21 19:16:55 +02:00
|
|
|
#include <nuttx/wdog.h>
|
|
|
|
#include <nuttx/kmalloc.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2013-07-01 16:11:54 +02:00
|
|
|
#include <nuttx/spi/spi.h>
|
2011-10-03 18:02:22 +02:00
|
|
|
#include <nuttx/wqueue.h>
|
2017-03-30 15:38:37 +02:00
|
|
|
#include <nuttx/random.h>
|
2011-10-03 18:02:22 +02:00
|
|
|
|
|
|
|
#include <nuttx/input/touchscreen.h>
|
|
|
|
#include <nuttx/input/ads7843e.h>
|
|
|
|
|
|
|
|
#include "ads7843e.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-09 20:43:43 +01:00
|
|
|
/* This is a value for the threshold that guarantees a big difference on the
|
2012-09-27 21:26:18 +02:00
|
|
|
* first pendown (but can't overflow).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define INVALID_THRESHOLD 0x1000
|
|
|
|
|
2011-10-03 18:02:22 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
2020-01-03 15:34:44 +01:00
|
|
|
|
2011-10-03 18:47:23 +02:00
|
|
|
/* Low-level SPI helpers */
|
|
|
|
|
2012-09-27 21:26:18 +02:00
|
|
|
static void ads7843e_lock(FAR struct spi_dev_s *spi);
|
|
|
|
static void ads7843e_unlock(FAR struct spi_dev_s *spi);
|
2011-10-03 18:47:23 +02:00
|
|
|
|
2020-03-31 00:32:20 +02:00
|
|
|
static uint16_t ads7843e_sendcmd(FAR struct ads7843e_dev_s *priv,
|
2022-04-12 12:16:13 +02:00
|
|
|
uint8_t cmd);
|
2011-10-04 19:08:59 +02:00
|
|
|
|
2011-10-03 18:47:23 +02:00
|
|
|
/* Interrupts and data sampling */
|
2011-10-03 18:02:22 +02:00
|
|
|
|
|
|
|
static void ads7843e_notify(FAR struct ads7843e_dev_s *priv);
|
2020-03-31 00:32:20 +02:00
|
|
|
static int ads7843e_sample(FAR struct ads7843e_dev_s *priv,
|
2022-04-12 12:16:13 +02:00
|
|
|
FAR struct ads7843e_sample_s *sample);
|
2020-03-31 00:32:20 +02:00
|
|
|
static int ads7843e_waitsample(FAR struct ads7843e_dev_s *priv,
|
2022-04-12 12:16:13 +02:00
|
|
|
FAR struct ads7843e_sample_s *sample);
|
2011-10-03 18:02:22 +02:00
|
|
|
static void ads7843e_worker(FAR void *arg);
|
2020-03-31 00:32:20 +02:00
|
|
|
static int ads7843e_interrupt(int irq, FAR void *context, FAR void *arg);
|
2011-10-03 18:02:22 +02:00
|
|
|
|
|
|
|
/* Character driver methods */
|
|
|
|
|
2020-03-31 00:32:20 +02:00
|
|
|
static int ads7843e_open(FAR struct file *filep);
|
|
|
|
static int ads7843e_close(FAR struct file *filep);
|
|
|
|
static ssize_t ads7843e_read(FAR struct file *filep, FAR char *buffer,
|
2022-04-12 12:16:13 +02:00
|
|
|
size_t len);
|
2020-03-31 00:32:20 +02:00
|
|
|
static int ads7843e_ioctl(FAR struct file *filep, int cmd,
|
2022-04-12 12:16:13 +02:00
|
|
|
unsigned long arg);
|
2020-03-31 00:32:20 +02:00
|
|
|
static int ads7843e_poll(FAR struct file *filep, struct pollfd *fds,
|
2022-04-12 12:16:13 +02:00
|
|
|
bool setup);
|
2011-10-03 18:02:22 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-08-27 17:40:19 +02:00
|
|
|
/* This the vtable that supports the character driver interface */
|
2011-10-03 18:02:22 +02:00
|
|
|
|
|
|
|
static const struct file_operations ads7843e_fops =
|
|
|
|
{
|
|
|
|
ads7843e_open, /* open */
|
|
|
|
ads7843e_close, /* close */
|
|
|
|
ads7843e_read, /* read */
|
2022-01-10 15:51:17 +01:00
|
|
|
NULL, /* write */
|
|
|
|
NULL, /* seek */
|
2019-05-22 02:57:54 +02:00
|
|
|
ads7843e_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
|
|
|
ads7843e_poll /* poll */
|
2011-10-03 18:02:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* If only a single ADS7843E device is supported, then the driver state
|
|
|
|
* structure may as well be pre-allocated.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_ADS7843E_MULTIPLE
|
|
|
|
static struct ads7843e_dev_s g_ads7843e;
|
|
|
|
|
|
|
|
/* Otherwise, we will need to maintain allocated driver instances in a list */
|
|
|
|
|
|
|
|
#else
|
|
|
|
static struct ads7843e_dev_s *g_ads7843elist;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-10-03 18:47:23 +02:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: ads7843e_lock
|
2011-10-03 18:47:23 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2012-09-27 21:26:18 +02:00
|
|
|
* Lock the SPI bus and re-configure as necessary. This function must be
|
|
|
|
* to assure: (1) exclusive access to the SPI bus, and (2) to assure that
|
|
|
|
* the shared bus is properly configured for the touchscreen controller.
|
2011-10-03 18:47:23 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2011-10-03 18:47:23 +02:00
|
|
|
* spi - Reference to the SPI driver structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-09-27 21:26:18 +02:00
|
|
|
static void ads7843e_lock(FAR struct spi_dev_s *spi)
|
2011-10-03 18:47:23 +02:00
|
|
|
{
|
2012-09-27 21:26:18 +02:00
|
|
|
/* Lock the SPI bus because there are multiple devices competing for the
|
|
|
|
* SPI bus
|
2011-10-03 18:47:23 +02:00
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(spi, true);
|
2011-10-03 18:47:23 +02:00
|
|
|
|
2012-09-27 21:26:18 +02:00
|
|
|
/* We have the lock. Now make sure that the SPI bus is configured for the
|
|
|
|
* ADS7843 (it might have gotten configured for a different device while
|
|
|
|
* unlocked)
|
2011-10-03 18:47:23 +02:00
|
|
|
*/
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_TOUCHSCREEN(0), true);
|
2011-10-03 18:47:23 +02:00
|
|
|
SPI_SETMODE(spi, CONFIG_ADS7843E_SPIMODE);
|
|
|
|
SPI_SETBITS(spi, 8);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_HWFEATURES(spi, 0);
|
|
|
|
SPI_SETFREQUENCY(spi, CONFIG_ADS7843E_FREQUENCY);
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_TOUCHSCREEN(0), false);
|
2011-10-03 18:47:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: ads7843e_unlock
|
2011-10-03 18:47:23 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2016-01-24 01:54:36 +01:00
|
|
|
* Un-lock the SPI bus after each transfer, possibly losing the current
|
|
|
|
* configuration if we are sharing the bus with other devices.
|
2011-10-03 18:47:23 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2011-10-03 18:47:23 +02:00
|
|
|
* spi - Reference to the SPI driver structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-09-27 21:26:18 +02:00
|
|
|
static void ads7843e_unlock(FAR struct spi_dev_s *spi)
|
2011-10-03 18:47:23 +02:00
|
|
|
{
|
2012-09-27 21:26:18 +02:00
|
|
|
/* Relinquish the SPI bus. */
|
2011-10-03 18:47:23 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(spi, false);
|
2011-10-03 18:47:23 +02:00
|
|
|
}
|
2011-10-04 19:08:59 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ads7843e_sendcmd
|
2013-06-16 18:09:21 +02:00
|
|
|
*
|
2018-02-01 19:03:55 +01:00
|
|
|
* Description:
|
2013-06-16 18:09:21 +02:00
|
|
|
* The command/data sequences is as follows:
|
|
|
|
*
|
|
|
|
* DCLK
|
|
|
|
* 1 2 3 4 5 6 7 8 1 2 3 4 ...
|
|
|
|
* S A2 A1 A0 MODE SER PD1 PD0
|
|
|
|
* DFR
|
|
|
|
* START CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
|
|
|
|
* CMD
|
2020-02-22 19:31:14 +01:00
|
|
|
* Acquisition AAAAAAAAAAA
|
2013-06-16 18:09:21 +02:00
|
|
|
* TIME
|
|
|
|
* BUSY BBBBBBBB
|
|
|
|
* Reported
|
|
|
|
* 12-bit DDDDDDDDDDDD...
|
|
|
|
* response
|
|
|
|
*
|
2014-10-11 23:20:21 +02:00
|
|
|
* The BUSY output is high impedance when /CS is high. BUSY goes low when
|
2013-06-16 18:09:21 +02:00
|
|
|
* /CS goes low (within 200ns). BUSY goes high on the falling edge of the
|
|
|
|
* 8th clock (within 200ns); BUSY goes low again after the falling edge of
|
|
|
|
* first clock of the 12-bit data read, at the leading edge of the MS bit
|
|
|
|
* 11 of the 12-bit data response.
|
|
|
|
*
|
|
|
|
* The acquisition time is 3 clock cycles and so should be complete at the
|
|
|
|
* end of the command transfer. Other places say that this time is
|
|
|
|
* nominally 2 microseconds.
|
|
|
|
*
|
|
|
|
* So what good is this BUSY? Many boards do not even bother to bring it
|
2013-06-16 22:31:18 +02:00
|
|
|
* to the MCU. Busy will stick high until we read the data so you cannot
|
|
|
|
* wait on it before reading.
|
2013-06-16 18:09:21 +02:00
|
|
|
*
|
2011-10-04 19:08:59 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-31 00:32:20 +02:00
|
|
|
static uint16_t ads7843e_sendcmd(FAR struct ads7843e_dev_s *priv,
|
|
|
|
uint8_t cmd)
|
2011-10-04 19:08:59 +02:00
|
|
|
{
|
|
|
|
uint8_t buffer[2];
|
|
|
|
uint16_t result;
|
|
|
|
|
|
|
|
/* Select the ADS7843E */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN(0), true);
|
2011-10-04 19:08:59 +02:00
|
|
|
|
|
|
|
/* Send the command */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, cmd);
|
2013-06-16 18:09:21 +02:00
|
|
|
|
2014-10-11 23:20:21 +02:00
|
|
|
/* Wait a tiny amount to make sure that the acquisition time is complete */
|
2013-06-16 18:09:21 +02:00
|
|
|
|
2020-01-03 15:34:44 +01:00
|
|
|
up_udelay(3); /* 3 microseconds */
|
2011-10-04 19:08:59 +02:00
|
|
|
|
2013-06-16 18:09:21 +02:00
|
|
|
/* Read the 12-bit data (LS 4 bits will be padded with zero) */
|
2011-10-04 19:08:59 +02:00
|
|
|
|
|
|
|
SPI_RECVBLOCK(priv->spi, buffer, 2);
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_TOUCHSCREEN(0), false);
|
2011-10-04 19:08:59 +02:00
|
|
|
|
|
|
|
result = ((uint16_t)buffer[0] << 8) | (uint16_t)buffer[1];
|
|
|
|
result = result >> 4;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("cmd:%02x response:%04x\n", cmd, result);
|
2011-10-04 19:08:59 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-10-03 18:02:22 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ads7843e_notify
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void ads7843e_notify(FAR struct ads7843e_dev_s *priv)
|
|
|
|
{
|
2020-03-31 00:32:20 +02:00
|
|
|
/* If there are threads waiting on poll() for ADS7843E 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.
|
2011-10-03 18:02:22 +02:00
|
|
|
*/
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(priv->fds, CONFIG_ADS7843E_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 ADS7843E
|
|
|
|
* is no longer available.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nxsem_post(&priv->waitsem);
|
|
|
|
}
|
2011-10-03 18:02:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ads7843e_sample
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ads7843e_sample(FAR struct ads7843e_dev_s *priv,
|
2022-04-12 12:16:13 +02:00
|
|
|
FAR struct ads7843e_sample_s *sample)
|
2011-10-03 18:02:22 +02:00
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
int ret = -EAGAIN;
|
|
|
|
|
|
|
|
/* Interrupts me be disabled when this is called to (1) prevent posting
|
2011-12-17 21:07:22 +01:00
|
|
|
* of semaphores from interrupt handlers, and (2) to prevent sampled data
|
2011-10-03 18:02:22 +02:00
|
|
|
* from changing until it has been reported.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2011-10-03 18:02:22 +02:00
|
|
|
|
|
|
|
/* Is there new ADS7843E sample data available? */
|
|
|
|
|
|
|
|
if (priv->penchange)
|
|
|
|
{
|
|
|
|
/* Yes.. the state has changed in some way. Return a copy of the
|
|
|
|
* sampled data.
|
|
|
|
*/
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
memcpy(sample, &priv->sample, sizeof(struct ads7843e_sample_s));
|
2011-10-03 18:02:22 +02:00
|
|
|
|
|
|
|
/* Now manage state transitions */
|
|
|
|
|
|
|
|
if (sample->contact == CONTACT_UP)
|
|
|
|
{
|
2012-05-13 20:28:43 +02:00
|
|
|
/* Next.. no contact. Increment the ID so that next contact ID
|
|
|
|
* will be unique. X/Y positions are no longer valid.
|
|
|
|
*/
|
2011-10-03 18:02:22 +02:00
|
|
|
|
|
|
|
priv->sample.contact = CONTACT_NONE;
|
2012-05-13 20:28:43 +02:00
|
|
|
priv->sample.valid = false;
|
2011-10-03 18:02:22 +02:00
|
|
|
priv->id++;
|
|
|
|
}
|
|
|
|
else if (sample->contact == CONTACT_DOWN)
|
2020-01-03 15:34:44 +01:00
|
|
|
{
|
2011-10-03 18:02:22 +02:00
|
|
|
/* First report -- next report will be a movement */
|
|
|
|
|
2020-01-03 15:34:44 +01:00
|
|
|
priv->sample.contact = CONTACT_MOVE;
|
|
|
|
}
|
2011-10-03 18:02:22 +02:00
|
|
|
|
|
|
|
priv->penchange = false;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2011-10-03 18:02:22 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ads7843e_waitsample
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ads7843e_waitsample(FAR struct ads7843e_dev_s *priv,
|
|
|
|
FAR struct ads7843e_sample_s *sample)
|
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Interrupts me be disabled when this is called to (1) prevent posting
|
2011-12-17 21:07:22 +01:00
|
|
|
* of semaphores from interrupt handlers, and (2) to prevent sampled data
|
2011-10-03 18:02:22 +02:00
|
|
|
* from changing until it has been reported.
|
|
|
|
*
|
|
|
|
* In addition, we will also disable pre-emption to prevent other threads
|
|
|
|
* from getting control while we muck with the semaphores.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sched_lock();
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2011-10-03 18:02:22 +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->devlock);
|
2011-10-03 18:02:22 +02:00
|
|
|
|
|
|
|
/* Try to get the a sample... if we cannot, then wait on the semaphore
|
2011-12-17 21:07:22 +01:00
|
|
|
* that is posted when new sample data is available.
|
2011-10-03 18:02:22 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
while (ads7843e_sample(priv, sample) < 0)
|
|
|
|
{
|
|
|
|
/* Wait for a change in the ADS7843E state */
|
2013-06-16 22:31:18 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("Waiting..\n");
|
2011-10-03 18:02:22 +02:00
|
|
|
priv->nwaiters++;
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&priv->waitsem);
|
2011-10-03 18:02:22 +02:00
|
|
|
priv->nwaiters--;
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
ierr("ERROR: nxsem_wait: %d\n", ret);
|
2011-10-03 18:02:22 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("Sampled\n");
|
2011-10-04 19:08:59 +02:00
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
/* Re-acquire the semaphore that manages mutually exclusive access to
|
2020-03-31 00:32:20 +02:00
|
|
|
* the device structure. We may have to wait here. But we have our
|
|
|
|
* sample. Interrupts and pre-emption will be re-enabled while we wait.
|
2011-10-03 18:02:22 +02:00
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->devlock);
|
2011-10-03 18:02:22 +02:00
|
|
|
|
|
|
|
errout:
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2011-10-03 18:02:22 +02:00
|
|
|
|
|
|
|
/* Restore pre-emption. We might get suspended here but that is okay
|
|
|
|
* because we already have our sample. Note: this means that if there
|
|
|
|
* were two threads reading from the ADS7843E for some reason, the data
|
|
|
|
* might be read out of order.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sched_unlock();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2011-10-04 19:08:59 +02:00
|
|
|
* Name: ads7843e_schedule
|
2011-10-03 18:02:22 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2011-10-04 19:08:59 +02:00
|
|
|
static int ads7843e_schedule(FAR struct ads7843e_dev_s *priv)
|
2011-10-03 18:02:22 +02:00
|
|
|
{
|
2011-10-04 19:08:59 +02:00
|
|
|
FAR struct ads7843e_config_s *config;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get a pointer the callbacks for convenience (and so the code is not so
|
|
|
|
* ugly).
|
|
|
|
*/
|
|
|
|
|
|
|
|
config = priv->config;
|
|
|
|
DEBUGASSERT(config != NULL);
|
|
|
|
|
|
|
|
/* Disable further interrupts. ADS7843E interrupts will be re-enabled
|
2011-12-17 21:07:22 +01:00
|
|
|
* after the worker thread executes.
|
2011-10-04 19:08:59 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
config->enable(config, false);
|
|
|
|
|
|
|
|
/* Disable the watchdog timer. It will be re-enabled in the worker thread
|
|
|
|
* while the pen remains down.
|
|
|
|
*/
|
|
|
|
|
2020-08-04 12:31:31 +02:00
|
|
|
wd_cancel(&priv->wdog);
|
2011-10-04 19:08:59 +02:00
|
|
|
|
|
|
|
/* Transfer processing to the worker thread. Since ADS7843E interrupts are
|
|
|
|
* disabled while the work is pending, no special action should be required
|
|
|
|
* to protected the work queue.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(priv->work.worker == NULL);
|
2012-09-04 02:54:09 +02:00
|
|
|
ret = work_queue(HPWORK, &priv->work, ads7843e_worker, priv, 0);
|
2011-10-04 19:08:59 +02:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-20 20:44:38 +02:00
|
|
|
ierr("ERROR: Failed to queue work: %d\n", ret);
|
2011-10-04 19:08:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ads7843e_wdog
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-08-09 20:29:35 +02:00
|
|
|
static void ads7843e_wdog(wdparm_t arg)
|
2011-10-04 19:08:59 +02:00
|
|
|
{
|
2020-03-31 00:32:20 +02:00
|
|
|
FAR struct ads7843e_dev_s *priv =
|
2020-08-09 20:29:35 +02:00
|
|
|
(FAR struct ads7843e_dev_s *)arg;
|
2020-03-31 00:32:20 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
ads7843e_schedule(priv);
|
2011-10-03 18:02:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ads7843e_worker
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void ads7843e_worker(FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct ads7843e_dev_s *priv = (FAR struct ads7843e_dev_s *)arg;
|
2011-10-04 19:08:59 +02:00
|
|
|
FAR struct ads7843e_config_s *config;
|
2012-09-27 21:26:18 +02:00
|
|
|
uint16_t x;
|
|
|
|
uint16_t y;
|
|
|
|
uint16_t xdiff;
|
|
|
|
uint16_t ydiff;
|
2011-10-04 19:08:59 +02:00
|
|
|
bool pendown;
|
2011-10-03 18:02:22 +02:00
|
|
|
|
2018-08-24 14:58:30 +02:00
|
|
|
DEBUGASSERT(priv != NULL);
|
2011-10-03 18:02:22 +02:00
|
|
|
|
|
|
|
/* Get a pointer the callbacks for convenience (and so the code is not so
|
|
|
|
* ugly).
|
|
|
|
*/
|
|
|
|
|
|
|
|
config = priv->config;
|
|
|
|
DEBUGASSERT(config != NULL);
|
|
|
|
|
2012-09-27 17:29:53 +02:00
|
|
|
/* Disable the watchdog timer. This is safe because it is started only
|
|
|
|
* by this function and this function is serialized on the worker thread.
|
|
|
|
*/
|
2011-10-04 19:08:59 +02:00
|
|
|
|
2020-08-04 12:31:31 +02:00
|
|
|
wd_cancel(&priv->wdog);
|
2011-10-04 19:08:59 +02:00
|
|
|
|
2012-09-27 21:26:18 +02:00
|
|
|
/* Lock the SPI bus so that we have exclusive access */
|
|
|
|
|
|
|
|
ads7843e_lock(priv->spi);
|
|
|
|
|
2012-09-27 17:29:53 +02:00
|
|
|
/* Get exclusive access to the driver data structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&priv->devlock);
|
2012-09-27 17:29:53 +02:00
|
|
|
|
2011-10-03 18:02:22 +02:00
|
|
|
/* Check for pen up or down by reading the PENIRQ GPIO. */
|
|
|
|
|
|
|
|
pendown = config->pendown(config);
|
2012-09-27 05:13:50 +02:00
|
|
|
|
2011-10-03 18:02:22 +02:00
|
|
|
/* Handle the change from pen down to pen up */
|
|
|
|
|
|
|
|
if (!pendown)
|
|
|
|
{
|
2012-09-27 21:26:18 +02:00
|
|
|
/* The pen is up.. reset thresholding variables. */
|
|
|
|
|
|
|
|
priv->threshx = INVALID_THRESHOLD;
|
|
|
|
priv->threshy = INVALID_THRESHOLD;
|
|
|
|
|
2020-03-31 00:32:20 +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)
|
2011-10-03 18:02:22 +02:00
|
|
|
*/
|
|
|
|
|
2012-09-27 21:26:18 +02:00
|
|
|
if (priv->sample.contact == CONTACT_NONE ||
|
|
|
|
priv->sample.contact == CONTACT_UP)
|
2011-10-03 18:02:22 +02:00
|
|
|
{
|
2012-09-27 21:26:18 +02:00
|
|
|
goto ignored;
|
2011-10-03 18:02:22 +02:00
|
|
|
}
|
2011-10-04 19:08:59 +02:00
|
|
|
|
|
|
|
/* The pen is up. NOTE: We know from a previous test, that this is a
|
|
|
|
* loss of contact condition. This will be changed to CONTACT_NONE
|
|
|
|
* after the loss of contact is sampled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->sample.contact = CONTACT_UP;
|
2011-10-03 18:02:22 +02:00
|
|
|
}
|
2012-05-13 20:28:43 +02:00
|
|
|
|
|
|
|
/* It is a pen down event. If the last loss-of-contact event has not been
|
2020-03-31 00:32:20 +02:00
|
|
|
* processed yet, then we have to ignore the pen down event (or else it
|
|
|
|
* will look like a drag event)
|
2012-05-13 20:28:43 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
else if (priv->sample.contact == CONTACT_UP)
|
|
|
|
{
|
2012-09-27 21:26:18 +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
|
2019-09-29 20:52:20 +02:00
|
|
|
* should be okay because we will set the timer to sample again
|
2012-09-27 21:26:18 +02:00
|
|
|
* later.
|
|
|
|
*/
|
|
|
|
|
2020-08-04 12:31:31 +02:00
|
|
|
wd_start(&priv->wdog, ADS7843E_WDOG_DELAY,
|
2020-08-09 20:29:35 +02:00
|
|
|
ads7843e_wdog, (wdparm_t)priv);
|
2020-01-03 15:34:44 +01:00
|
|
|
goto ignored;
|
2012-05-13 20:28:43 +02:00
|
|
|
}
|
2011-10-03 18:02:22 +02:00
|
|
|
else
|
|
|
|
{
|
2013-04-15 19:25:08 +02:00
|
|
|
/* Handle pen down events. First, sample positional values. NOTE:
|
|
|
|
* that these commands have the side-effect of disabling the PENIRQ.
|
|
|
|
*/
|
2011-10-03 18:02:22 +02:00
|
|
|
|
2012-09-27 21:26:18 +02:00
|
|
|
#ifdef CONFIG_ADS7843E_SWAPXY
|
|
|
|
x = ads7843e_sendcmd(priv, ADS7843_CMD_YPOSITION);
|
|
|
|
y = ads7843e_sendcmd(priv, ADS7843_CMD_XPOSITION);
|
|
|
|
#else
|
|
|
|
x = ads7843e_sendcmd(priv, ADS7843_CMD_XPOSITION);
|
|
|
|
y = ads7843e_sendcmd(priv, ADS7843_CMD_YPOSITION);
|
|
|
|
#endif
|
|
|
|
|
2017-03-30 15:38:37 +02:00
|
|
|
add_ui_randomness((x << 16) | y);
|
|
|
|
|
2020-03-31 00:32:20 +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-09-27 21:26:18 +02:00
|
|
|
*/
|
|
|
|
|
2020-03-31 00:32:20 +02:00
|
|
|
xdiff = x > priv->threshx ?
|
|
|
|
(x - priv->threshx) :
|
|
|
|
(priv->threshx - x);
|
|
|
|
ydiff = y > priv->threshy ?
|
|
|
|
(y - priv->threshy) :
|
|
|
|
(priv->threshy - y);
|
2012-09-27 21:26:18 +02:00
|
|
|
|
|
|
|
/* Continue to sample the position while the pen is down */
|
|
|
|
|
2020-08-04 12:31:31 +02:00
|
|
|
wd_start(&priv->wdog, ADS7843E_WDOG_DELAY,
|
2020-08-09 20:29:35 +02:00
|
|
|
ads7843e_wdog, (wdparm_t)priv);
|
2012-09-27 21:26:18 +02:00
|
|
|
|
|
|
|
/* Check the thresholds. Bail if there is no significant difference */
|
|
|
|
|
|
|
|
if (xdiff < CONFIG_ADS7843E_THRESHX && ydiff < CONFIG_ADS7843E_THRESHY)
|
|
|
|
{
|
2020-05-17 16:47:40 +02:00
|
|
|
/* Little or no change in either direction ... don't report
|
|
|
|
* anything.
|
|
|
|
*/
|
2012-09-27 21:26:18 +02:00
|
|
|
|
|
|
|
goto ignored;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* When we see a big difference, snap to the new x/y thresholds */
|
|
|
|
|
|
|
|
priv->threshx = x;
|
|
|
|
priv->threshy = y;
|
|
|
|
|
|
|
|
/* Update the x/y position in the sample data */
|
|
|
|
|
|
|
|
priv->sample.x = priv->threshx;
|
|
|
|
priv->sample.y = priv->threshy;
|
2011-10-03 18:02:22 +02:00
|
|
|
|
2012-05-13 20:28:43 +02:00
|
|
|
/* The X/Y positional data is now valid */
|
|
|
|
|
|
|
|
priv->sample.valid = true;
|
|
|
|
|
|
|
|
/* If this is the first (acknowledged) pen down report, then report
|
2011-10-03 18:02:22 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Indicate the availability of new sample data for this ID */
|
|
|
|
|
|
|
|
priv->sample.id = priv->id;
|
|
|
|
priv->penchange = true;
|
|
|
|
|
2011-12-17 21:07:22 +01:00
|
|
|
/* Notify any waiters that new ADS7843E data is available */
|
2011-10-03 18:02:22 +02:00
|
|
|
|
|
|
|
ads7843e_notify(priv);
|
|
|
|
|
|
|
|
/* Exit, re-enabling ADS7843E interrupts */
|
|
|
|
|
2012-09-27 21:26:18 +02:00
|
|
|
ignored:
|
|
|
|
|
2013-04-15 19:25:08 +02:00
|
|
|
/* Re-enable the PENIRQ interrupt at the ADS7843E */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
ads7843e_sendcmd(priv, ADS7843_CMD_ENABPENIRQ);
|
2013-04-15 19:25:08 +02:00
|
|
|
|
|
|
|
/* Re-enable the PENIRQ interrupt at the MCU's interrupt controller */
|
|
|
|
|
2011-10-03 18:02:22 +02:00
|
|
|
config->enable(config, true);
|
2012-09-27 17:29:53 +02:00
|
|
|
|
2012-09-27 21:26:18 +02:00
|
|
|
/* Release our lock on the state structure and unlock the SPI bus */
|
2012-09-27 17:29:53 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->devlock);
|
2012-09-27 21:26:18 +02:00
|
|
|
ads7843e_unlock(priv->spi);
|
2011-10-03 18:02:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ads7843e_interrupt
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-28 02:28:24 +01:00
|
|
|
static int ads7843e_interrupt(int irq, FAR void *context, FAR void *arg)
|
2011-10-03 18:02:22 +02:00
|
|
|
{
|
|
|
|
FAR struct ads7843e_dev_s *priv;
|
|
|
|
FAR struct ads7843e_config_s *config;
|
2011-10-04 19:08:59 +02:00
|
|
|
int ret;
|
2011-10-03 18:02:22 +02:00
|
|
|
|
|
|
|
/* Which ADS7843E device caused the interrupt? */
|
|
|
|
|
|
|
|
#ifndef CONFIG_ADS7843E_MULTIPLE
|
|
|
|
priv = &g_ads7843e;
|
|
|
|
#else
|
|
|
|
for (priv = g_ads7843elist;
|
|
|
|
priv && priv->configs->irq != irq;
|
|
|
|
priv = priv->flink);
|
|
|
|
|
2018-08-24 14:58:30 +02:00
|
|
|
DEBUGASSERT(priv != NULL);
|
2011-10-03 18:02:22 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Get a pointer the callbacks for convenience (and so the code is not so
|
|
|
|
* ugly).
|
|
|
|
*/
|
|
|
|
|
|
|
|
config = priv->config;
|
|
|
|
DEBUGASSERT(config != NULL);
|
|
|
|
|
2011-10-04 19:08:59 +02:00
|
|
|
/* Schedule sampling to occur on the worker thread */
|
2011-10-03 18:02:22 +02:00
|
|
|
|
2011-10-04 19:08:59 +02:00
|
|
|
ret = ads7843e_schedule(priv);
|
2011-10-03 18:02:22 +02:00
|
|
|
|
|
|
|
/* Clear any pending interrupts and return success */
|
|
|
|
|
|
|
|
config->clear(config);
|
2011-10-04 19:08:59 +02:00
|
|
|
return ret;
|
2011-10-03 18:02:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ads7843e_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ads7843e_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_ADS7843E_REFCNT
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR struct inode *inode;
|
2011-10-03 18:02:22 +02:00
|
|
|
FAR struct ads7843e_dev_s *priv;
|
|
|
|
uint8_t tmp;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("Opening\n");
|
2011-10-04 19:08:59 +02:00
|
|
|
|
2011-10-03 18:02:22 +02:00
|
|
|
DEBUGASSERT(filep);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
priv = (FAR struct ads7843e_dev_s *)inode->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the driver data structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->devlock);
|
2011-10-03 18:02:22 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2011-10-03 18:02:22 +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;
|
2011-10-03 18:02:22 +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->devlock);
|
2011-10-03 18:02:22 +02:00
|
|
|
return ret;
|
|
|
|
#else
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("Opening\n");
|
2011-10-03 18:02:22 +02:00
|
|
|
return OK;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ads7843e_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ads7843e_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_ADS7843E_REFCNT
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR struct inode *inode;
|
2011-10-03 18:02:22 +02:00
|
|
|
FAR struct ads7843e_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("Closing\n");
|
2011-10-03 18:02:22 +02:00
|
|
|
DEBUGASSERT(filep);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
priv = (FAR struct ads7843e_dev_s *)inode->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the driver data structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->devlock);
|
2011-10-03 18:02:22 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2011-10-03 18:02:22 +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->devlock);
|
2011-10-03 18:02:22 +02:00
|
|
|
#endif
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("Closing\n");
|
2011-10-03 18:02:22 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ads7843e_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-31 00:32:20 +02:00
|
|
|
static ssize_t ads7843e_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len)
|
2011-10-03 18:02:22 +02:00
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
2011-10-04 19:08:59 +02:00
|
|
|
FAR struct ads7843e_dev_s *priv;
|
2011-10-03 18:02:22 +02:00
|
|
|
FAR struct touch_sample_s *report;
|
2011-10-04 19:08:59 +02:00
|
|
|
struct ads7843e_sample_s sample;
|
2011-10-03 18:02:22 +02:00
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("buffer:%p len:%d\n", buffer, len);
|
2011-10-03 18:02:22 +02:00
|
|
|
DEBUGASSERT(filep);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
priv = (FAR struct ads7843e_dev_s *)inode->i_private;
|
|
|
|
|
|
|
|
/* 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?
|
|
|
|
*/
|
|
|
|
|
2016-06-12 17:26:00 +02:00
|
|
|
ierr("ERROR: Unsupported read size: %d\n", len);
|
2011-10-03 18:02:22 +02:00
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get exclusive access to the driver data structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->devlock);
|
2011-10-03 18:02:22 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
ierr("ERROR: nxsem_wait: %d\n", ret);
|
|
|
|
return ret;
|
2011-10-03 18:02:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to read sample data. */
|
|
|
|
|
|
|
|
ret = ads7843e_sample(priv, &sample);
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("Sample data is not available\n");
|
2011-10-03 18:02:22 +02:00
|
|
|
if (filep->f_oflags & O_NONBLOCK)
|
|
|
|
{
|
|
|
|
ret = -EAGAIN;
|
|
|
|
goto errout;
|
2020-01-03 15:34:44 +01:00
|
|
|
}
|
2011-10-03 18:02:22 +02:00
|
|
|
|
|
|
|
/* Wait for sample data */
|
|
|
|
|
|
|
|
ret = ads7843e_waitsample(priv, &sample);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* We might have been awakened by a signal */
|
|
|
|
|
2016-06-12 17:26:00 +02:00
|
|
|
ierr("ERROR: ads7843e_waitsample: %d\n", ret);
|
2011-10-03 18:02:22 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In any event, we now have sampled ADS7843E data that we can report
|
|
|
|
* to the caller.
|
|
|
|
*/
|
|
|
|
|
|
|
|
report = (FAR struct touch_sample_s *)buffer;
|
|
|
|
memset(report, 0, SIZEOF_TOUCH_SAMPLE_S(1));
|
|
|
|
report->npoints = 1;
|
2012-09-27 22:55:15 +02:00
|
|
|
report->point[0].id = sample.id;
|
2011-10-03 18:02:22 +02:00
|
|
|
report->point[0].x = sample.x;
|
|
|
|
report->point[0].y = sample.y;
|
|
|
|
|
|
|
|
/* Report the appropriate flags */
|
|
|
|
|
|
|
|
if (sample.contact == CONTACT_UP)
|
|
|
|
{
|
2015-10-10 18:41:00 +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.
|
|
|
|
*/
|
2011-10-03 18:02:22 +02:00
|
|
|
|
2012-05-13 20:28:43 +02:00
|
|
|
if (sample.valid)
|
|
|
|
{
|
2020-03-31 00:32:20 +02:00
|
|
|
report->point[0].flags = TOUCH_UP | TOUCH_ID_VALID |
|
|
|
|
TOUCH_POS_VALID;
|
2012-05-13 20:28:43 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
report->point[0].flags = TOUCH_UP | TOUCH_ID_VALID;
|
|
|
|
}
|
2011-10-03 18:02:22 +02:00
|
|
|
}
|
|
|
|
else if (sample.contact == CONTACT_DOWN)
|
|
|
|
{
|
|
|
|
/* First contact */
|
|
|
|
|
2020-03-31 00:32:20 +02:00
|
|
|
report->point[0].flags = TOUCH_DOWN | TOUCH_ID_VALID |
|
|
|
|
TOUCH_POS_VALID;
|
2011-10-03 18:02:22 +02:00
|
|
|
}
|
|
|
|
else /* if (sample->contact == CONTACT_MOVE) */
|
|
|
|
{
|
|
|
|
/* Movement of the same contact */
|
|
|
|
|
2020-03-31 00:32:20 +02:00
|
|
|
report->point[0].flags = TOUCH_MOVE | TOUCH_ID_VALID |
|
|
|
|
TOUCH_POS_VALID;
|
2011-10-03 18:02:22 +02:00
|
|
|
}
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo(" id: %d\n", report->point[0].id);
|
|
|
|
iinfo(" flags: %02x\n", report->point[0].flags);
|
|
|
|
iinfo(" x: %d\n", report->point[0].x);
|
|
|
|
iinfo(" y: %d\n", report->point[0].y);
|
2011-10-04 19:08:59 +02:00
|
|
|
|
2011-10-03 18:02:22 +02:00
|
|
|
ret = SIZEOF_TOUCH_SAMPLE_S(1);
|
|
|
|
|
|
|
|
errout:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->devlock);
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("Returning: %d\n", ret);
|
2011-10-03 18:02:22 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-02-01 19:17:03 +01:00
|
|
|
* Name: ads7843e_ioctl
|
2011-10-03 18:02:22 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ads7843e_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR struct inode *inode;
|
2011-10-03 18:02:22 +02:00
|
|
|
FAR struct ads7843e_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("cmd: %d arg: %ld\n", cmd, arg);
|
2011-10-03 18:02:22 +02:00
|
|
|
DEBUGASSERT(filep);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
priv = (FAR struct ads7843e_dev_s *)inode->i_private;
|
|
|
|
|
|
|
|
/* Get exclusive access to the driver data structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->devlock);
|
2011-10-03 18:02:22 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2011-10-03 18:02:22 +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);
|
|
|
|
priv->config->frequency = SPI_SETFREQUENCY(priv->spi, *ptr);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->devlock);
|
2011-10-03 18:02:22 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ads7843e_poll
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ads7843e_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
2022-04-12 12:16:13 +02:00
|
|
|
bool setup)
|
2011-10-03 18:02:22 +02:00
|
|
|
{
|
2014-02-11 01:08:49 +01:00
|
|
|
FAR struct inode *inode;
|
2011-10-03 18:02:22 +02:00
|
|
|
FAR struct ads7843e_dev_s *priv;
|
2014-02-11 01:08:49 +01:00
|
|
|
int ret;
|
|
|
|
int i;
|
2011-10-03 18:02:22 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("setup: %d\n", (int)setup);
|
2011-10-03 18:02:22 +02:00
|
|
|
DEBUGASSERT(filep && fds);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
priv = (FAR struct ads7843e_dev_s *)inode->i_private;
|
|
|
|
|
|
|
|
/* Are we setting up the poll? Or tearing it down? */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->devlock);
|
2011-10-03 18:02:22 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2011-10-03 18:02:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (setup)
|
|
|
|
{
|
|
|
|
/* Ignore waits that do not include POLLIN */
|
|
|
|
|
2011-12-03 17:13:40 +01:00
|
|
|
if ((fds->events & POLLIN) == 0)
|
2011-10-03 18:02:22 +02:00
|
|
|
{
|
|
|
|
ret = -EDEADLK;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is a request to set up the poll. Find an available
|
|
|
|
* slot for the poll structure reference
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_ADS7843E_NPOLLWAITERS; i++)
|
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= CONFIG_ADS7843E_NPOLLWAITERS)
|
|
|
|
{
|
|
|
|
fds->priv = NULL;
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Should we immediately notify on any of the requested events? */
|
|
|
|
|
|
|
|
if (priv->penchange)
|
|
|
|
{
|
|
|
|
ads7843e_notify(priv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->devlock);
|
2011-10-03 18:02:22 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ads7843e_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Configure the ADS7843E to use the provided SPI device instance. This
|
|
|
|
* will register the driver as /dev/inputN where N is the minor device
|
|
|
|
* number
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - An SPI driver instance
|
2011-12-17 21:07:22 +01:00
|
|
|
* config - Persistent board configuration data
|
2011-10-03 18:02:22 +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-09-27 21:26:18 +02:00
|
|
|
int ads7843e_register(FAR struct spi_dev_s *spi,
|
2011-10-03 18:02:22 +02:00
|
|
|
FAR struct ads7843e_config_s *config, int minor)
|
|
|
|
{
|
|
|
|
FAR struct ads7843e_dev_s *priv;
|
|
|
|
char devname[DEV_NAMELEN];
|
|
|
|
#ifdef CONFIG_ADS7843E_MULTIPLE
|
|
|
|
irqstate_t flags;
|
|
|
|
#endif
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("spi: %p minor: %d\n", spi, minor);
|
2011-10-03 18:02:22 +02:00
|
|
|
|
|
|
|
/* Debug-only sanity checks */
|
|
|
|
|
2012-09-27 21:26:18 +02:00
|
|
|
DEBUGASSERT(spi != NULL && config != NULL && minor >= 0 && minor < 100);
|
2011-10-03 18:02:22 +02:00
|
|
|
|
|
|
|
/* Create and initialize a ADS7843E device driver instance */
|
|
|
|
|
|
|
|
#ifndef CONFIG_ADS7843E_MULTIPLE
|
|
|
|
priv = &g_ads7843e;
|
|
|
|
#else
|
2020-03-31 00:32:20 +02:00
|
|
|
priv = (FAR struct ads7843e_dev_s *)
|
|
|
|
kmm_malloc(sizeof(struct ads7843e_dev_s));
|
2011-10-03 18:02:22 +02:00
|
|
|
if (!priv)
|
|
|
|
{
|
2016-06-12 17:26:00 +02:00
|
|
|
ierr("ERROR: kmm_malloc(%d) failed\n", sizeof(struct ads7843e_dev_s));
|
2011-10-03 18:02:22 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Initialize the ADS7843E device driver instance */
|
|
|
|
|
|
|
|
memset(priv, 0, sizeof(struct ads7843e_dev_s));
|
2012-09-27 21:26:18 +02:00
|
|
|
priv->spi = spi; /* Save the SPI device handle */
|
|
|
|
priv->config = config; /* Save the board configuration */
|
|
|
|
priv->threshx = INVALID_THRESHOLD; /* Initialize thresholding logic */
|
|
|
|
priv->threshy = INVALID_THRESHOLD; /* Initialize thresholding logic */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Initialize mutex & semaphores */
|
2016-11-03 18:00:47 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&priv->devlock); /* Initialize device structure mutex */
|
|
|
|
nxsem_init(&priv->waitsem, 0, 0); /* Initialize pen event wait semaphore */
|
2011-10-03 18:02:22 +02:00
|
|
|
|
|
|
|
/* Make sure that interrupts are disabled */
|
|
|
|
|
|
|
|
config->clear(config);
|
|
|
|
config->enable(config, false);
|
|
|
|
|
|
|
|
/* Attach the interrupt handler */
|
|
|
|
|
|
|
|
ret = config->attach(config, ads7843e_interrupt);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 17:26:00 +02:00
|
|
|
ierr("ERROR: Failed to attach interrupt\n");
|
2011-10-03 18:02:22 +02:00
|
|
|
goto errout_with_priv;
|
|
|
|
}
|
|
|
|
|
2016-06-12 17:26:00 +02:00
|
|
|
iinfo("Mode: %d Bits: 8 Frequency: %d\n",
|
|
|
|
CONFIG_ADS7843E_SPIMODE, CONFIG_ADS7843E_FREQUENCY);
|
2012-09-27 21:26:18 +02:00
|
|
|
|
|
|
|
/* Lock the SPI bus so that we have exclusive access */
|
|
|
|
|
|
|
|
ads7843e_lock(spi);
|
|
|
|
|
2011-10-04 19:08:59 +02:00
|
|
|
/* Enable the PEN IRQ */
|
2013-06-16 22:31:18 +02:00
|
|
|
|
2013-04-15 19:25:08 +02:00
|
|
|
ads7843e_sendcmd(priv, ADS7843_CMD_ENABPENIRQ);
|
2011-10-03 18:02:22 +02:00
|
|
|
|
2012-09-27 21:26:18 +02:00
|
|
|
/* Unlock the bus */
|
|
|
|
|
|
|
|
ads7843e_unlock(spi);
|
|
|
|
|
2011-10-03 18:02:22 +02:00
|
|
|
/* Register the device as an input device */
|
|
|
|
|
2022-04-12 12:16:13 +02:00
|
|
|
snprintf(devname, sizeof(devname), DEV_FORMAT, minor);
|
2016-06-11 19:59:51 +02:00
|
|
|
iinfo("Registering %s\n", devname);
|
2011-10-03 18:02:22 +02:00
|
|
|
|
|
|
|
ret = register_driver(devname, &ads7843e_fops, 0666, priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 17:26:00 +02:00
|
|
|
ierr("ERROR: register_driver() failed: %d\n", ret);
|
2011-10-03 18:02:22 +02:00
|
|
|
goto errout_with_priv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If multiple ADS7843E devices are supported, then we will need to add
|
|
|
|
* this new instance to a list of device instances so that it can be
|
2014-10-11 23:20:21 +02:00
|
|
|
* found by the interrupt handler based on the received IRQ number.
|
2011-10-03 18:02:22 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_ADS7843E_MULTIPLE
|
2021-12-20 17:10:54 +01:00
|
|
|
flags = enter_critical_section();
|
2011-10-03 18:02:22 +02:00
|
|
|
priv->flink = g_ads7843elist;
|
|
|
|
g_ads7843elist = priv;
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2011-10-03 18:02:22 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Schedule work to perform the initial sampling and to set the data
|
|
|
|
* availability conditions.
|
|
|
|
*/
|
|
|
|
|
2012-09-04 02:54:09 +02:00
|
|
|
ret = work_queue(HPWORK, &priv->work, ads7843e_worker, priv, 0);
|
2011-10-03 18:02:22 +02:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-12 17:26:00 +02:00
|
|
|
ierr("ERROR: Failed to queue work: %d\n", ret);
|
2011-10-03 18:02:22 +02:00
|
|
|
goto errout_with_priv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And return success (?) */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_priv:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_destroy(&priv->devlock);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxsem_destroy(&priv->waitsem);
|
2011-10-03 18:02:22 +02:00
|
|
|
#ifdef CONFIG_ADS7843E_MULTIPLE
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(priv);
|
2011-10-03 18:02:22 +02:00
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|