mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-06-07 09:13:32 +08:00
Add GPIO support to STMPE11 driver; NFS update
git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@4701 7fd9a85b-ad96-42d3-883c-3090e2eb8679
This commit is contained in:
@@ -49,6 +49,9 @@ endif
|
||||
|
||||
ifeq ($(CONFIG_INPUT_STMPE11),y)
|
||||
CSRCS += stmpe11_base.c
|
||||
ifneq ($(CONFIG_INPUT_STMPE11_GPIO_DISABLE),y)
|
||||
CSRCS += stmpe11_gpio.c
|
||||
endif
|
||||
endif
|
||||
|
||||
# Include input device driver build support
|
||||
|
||||
@@ -107,28 +107,36 @@ struct stmpe11_dev_s
|
||||
#ifdef CONFIG_STMPE11_MULTIPLE
|
||||
FAR struct stmpe11_dev_s *flink; /* Supports a singly linked list of drivers */
|
||||
#endif
|
||||
#ifdef CONFIG_STMPE11_REFCNT
|
||||
uint8_t crefs; /* Number of times the device has been opened */
|
||||
#endif
|
||||
uint8_t nwaiters; /* Number of threads waiting for STMPE11 data */
|
||||
uint8_t inuse; /* SMTPE11 pins in use */
|
||||
uint8_t id; /* Current touch point ID (TSC only) */
|
||||
uint8_t minor; /* Touchscreen minor device number (TSC only) */
|
||||
volatile bool penchange; /* An unreported event is buffered (TSC only) */
|
||||
sem_t devsem; /* Manages exclusive access to this structure */
|
||||
sem_t waitsem; /* Used to wait for the availability of data */
|
||||
uint32_t threshx; /* Thresholded X value (TSC only) */
|
||||
uint32_t threshy; /* Thresholded Y value (TSC only) */
|
||||
|
||||
/* Common fields */
|
||||
|
||||
FAR struct stmpe11_config_s *config; /* Board configuration data */
|
||||
sem_t exclsem; /* Manages exclusive access to this structure */
|
||||
#ifdef CONFIG_STMPE11_SPI
|
||||
FAR struct spi_dev_s *spi; /* Saved SPI driver instance */
|
||||
#else
|
||||
FAR struct i2c_dev_s *i2c; /* Saved I2C driver instance */
|
||||
#endif
|
||||
|
||||
FAR struct stmpe11_config_s *config; /* Board configuration data (TSC only) */
|
||||
uint8_t inuse; /* SMTPE11 pins in use */
|
||||
|
||||
/* Fields that may be disabled to save size if touchscreen support is not used. */
|
||||
|
||||
#ifndef CONFIG_STMPE11_TSC_DISABLE
|
||||
#ifdef CONFIG_STMPE11_REFCNT
|
||||
uint8_t crefs; /* Number of times the device has been opened */
|
||||
#endif
|
||||
uint8_t nwaiters; /* Number of threads waiting for STMPE11 data */
|
||||
uint8_t id; /* Current touch point ID */
|
||||
uint8_t minor; /* Touchscreen minor device number */
|
||||
volatile bool penchange; /* An unreported event is buffered */
|
||||
|
||||
uint32_t threshx; /* Thresholded X value */
|
||||
uint32_t threshy; /* Thresholded Y value */
|
||||
sem_t waitsem; /* Used to wait for the availability of data */
|
||||
|
||||
struct work_s work; /* Supports the interrupt handling "bottom half" */
|
||||
struct stmpe11_sample_s sample; /* Last sampled touch point data (TSC only) */
|
||||
struct stmpe11_sample_s sample; /* Last sampled touch point data */
|
||||
|
||||
/* The following is a list if poll structures of threads waiting for
|
||||
* driver events. The 'struct pollfd' reference for each open is also
|
||||
@@ -138,6 +146,14 @@ struct stmpe11_dev_s
|
||||
#ifndef CONFIG_DISABLE_POLL
|
||||
struct pollfd *fds[CONFIG_STMPE11_NPOLLWAITERS];
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* Fields that may be disabled to save size of GPIO support is not used */
|
||||
|
||||
#if !defined(CONFIG_STMPE11_GPIO_DISABLE) && !defined(CONFIG_STMPE11_GPIOINT_DISABLE)
|
||||
bool initialized; /* True if GPIO interrupt subsystem has been initialized */
|
||||
stmpe11_handler_t handlers[8]; /* GPIO "interrupt handlers" */
|
||||
#endif
|
||||
};
|
||||
|
||||
/********************************************************************************************
|
||||
@@ -152,7 +168,7 @@ struct stmpe11_dev_s
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
static uint8_t stmpe11_getreg8(FAR struct stmpe11_dev_s *priv, uint8_t regaddr);
|
||||
uint8_t stmpe11_getreg8(FAR struct stmpe11_dev_s *priv, uint8_t regaddr);
|
||||
|
||||
/********************************************************************************************
|
||||
* Name: stmpe11_putreg8
|
||||
@@ -174,5 +190,31 @@ void stmpe11_putreg8(FAR struct stmpe11_dev_s *priv, uint8_t regaddr, uint8_t re
|
||||
|
||||
uint16_t stmpe11_getreg16(FAR struct stmpe11_dev_s *priv, uint8_t regaddr);
|
||||
|
||||
/********************************************************************************************
|
||||
* Name: stmpe11_tscint
|
||||
*
|
||||
* Description:
|
||||
* Handle touchscreen interrupt events (this function actually executes in the context of
|
||||
* the worker thread).
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#ifndef CONFIG_STMPE11_TSC_DISABLE
|
||||
void stmpe11_tscint(FAR struct stmpe11_dev_s *priv) weak_function;
|
||||
#endif
|
||||
|
||||
/********************************************************************************************
|
||||
* Name: stmpe11_gpioint
|
||||
*
|
||||
* Description:
|
||||
* Handle GPIO interrupt events (this function actually executes in the context of the
|
||||
* worker thread).
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#if !defined(CONFIG_STMPE11_GPIO_DISABLE) && !defined(CONFIG_STMPE11_GPIOINT_DISABLE)
|
||||
void stmpe11_gpioint(FAR struct stmpe11_dev_s *priv) weak_function;
|
||||
#endif
|
||||
|
||||
#endif /* CONFIG_INPUT && CONFIG_INPUT_STMPE11 */
|
||||
#endif /* __DRIVERS_INPUT_STMPE11_H */
|
||||
|
||||
@@ -78,6 +78,122 @@ static struct stmpe11_dev_s *g_stmpe11list;
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: stmpe11_worker
|
||||
*
|
||||
* Description:
|
||||
* This is the "bottom half" of the STMPE11 interrupt handler
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static void stmpe11_worker(FAR void *arg)
|
||||
{
|
||||
FAR struct stmpe11_dev_s *priv = (FAR struct stmpe11_dev_s *)arg;
|
||||
uint8_t regval;
|
||||
|
||||
/* Get the globl interrupt status */
|
||||
|
||||
regval = stmpe11_getreg8(priv, STMPE11_INT_STA);
|
||||
|
||||
/* Check for a touchscreen interrupt */
|
||||
|
||||
#ifndef CONFIG_STMPE11_TSC_DISABLE
|
||||
if ((regval & (INT_TOUCH_DET|INT_FIFO_TH|INT_FIFO_OFLOW)) != 0)
|
||||
{
|
||||
/* Dispatch the touchscreen interrupt if it was brought into the link */
|
||||
|
||||
if (stmpe11_tscint)
|
||||
{
|
||||
stmpe11_tscint(priv);
|
||||
}
|
||||
|
||||
stmpe11_putreg8(priv, STMPE11_INT_STA, (INT_TOUCH_DET|INT_FIFO_TH|INT_FIFO_OFLOW));
|
||||
regval &= ~(INT_TOUCH_DET|INT_FIFO_TH|INT_FIFO_OFLOW);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !defined(CONFIG_STMPE11_GPIO_DISABLE) && !defined(CONFIG_STMPE11_GPIOINT_DISABLE)
|
||||
if ((regval & INT_GPIO) != 0)
|
||||
{
|
||||
/* Dispatch the GPIO interrupt if it was brought into the link */
|
||||
|
||||
if (stmpe11_gpioint)
|
||||
{
|
||||
stmpe11_gpioint(priv);
|
||||
}
|
||||
|
||||
stmpe11_putreg8(priv, STMPE11_INT_STA, INT_GPIO);
|
||||
regval &= ~INT_GPIO;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Clear any other residual, unhandled pending interrupt */
|
||||
|
||||
if (regval != 0)
|
||||
{
|
||||
stmpe11_putreg8(priv, STMPE11_INT_STA, regval);
|
||||
}
|
||||
|
||||
/* Clear the STMPE11 global interrupt */
|
||||
|
||||
priv->config->clear(priv->config);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: stmpe11_interrupt
|
||||
*
|
||||
* Description:
|
||||
* The STMPE11 interrupt handler
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static int stmpe11_interrupt(int irq, FAR void *context)
|
||||
{
|
||||
FAR struct stmpe11_dev_s *priv;
|
||||
FAR struct stmpe11_config_s *config;
|
||||
int ret;
|
||||
|
||||
/* Which STMPE11 device caused the interrupt? */
|
||||
|
||||
#ifndef CONFIG_STMPE11_MULTIPLE
|
||||
priv = &g_stmpe11;
|
||||
#else
|
||||
for (priv = g_stmpe11list;
|
||||
priv && priv->configs->irq != irq;
|
||||
priv = priv->flink);
|
||||
|
||||
ASSERT(priv != NULL);
|
||||
#endif
|
||||
|
||||
/* Get a pointer the callbacks for convenience (and so the code is not so
|
||||
* ugly).
|
||||
*/
|
||||
|
||||
config = priv->config;
|
||||
DEBUGASSERT(config != NULL);
|
||||
|
||||
/* Disable further interrupts */
|
||||
|
||||
config->enable(config, false);
|
||||
|
||||
/* Transfer processing to the worker thread. Since STMPE11 interrupts are
|
||||
* disabled while the work is pending, no special action should be required
|
||||
* to protected the work queue.
|
||||
*/
|
||||
|
||||
DEBUGASSERT(priv->work.worker == NULL);
|
||||
ret = work_queue(&priv->work, stmpe11_worker, priv, 0);
|
||||
if (ret != 0)
|
||||
{
|
||||
illdbg("Failed to queue work: %d\n", ret);
|
||||
}
|
||||
|
||||
/* Clear any pending interrupts and return success */
|
||||
|
||||
config->clear(config);
|
||||
return OK;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: stmpe11_checkid
|
||||
*
|
||||
@@ -191,6 +307,7 @@ STMPE11_HANDLE stmpe11_instantiate(FAR struct i2c_dev_s *dev,
|
||||
|
||||
/* Initialize the device state structure */
|
||||
|
||||
sem_init(&priv->exclsem, 0, 1);
|
||||
#ifdef CONFIG_STMPE11_SPI
|
||||
priv->spi = dev;
|
||||
#else
|
||||
@@ -211,6 +328,20 @@ STMPE11_HANDLE stmpe11_instantiate(FAR struct i2c_dev_s *dev,
|
||||
/* Generate STMPE11 Software reset */
|
||||
|
||||
stmpe11_reset(priv);
|
||||
|
||||
/* Attach the STMPE11 interrupt handler * yet).
|
||||
*/
|
||||
|
||||
config->attach(config, stmpe11_interrupt);
|
||||
|
||||
/* Clear any pending interrupts */
|
||||
|
||||
stmpe11_putreg8(priv, STMPE11_INT_STA, INT_ALL);
|
||||
config->clear(config);
|
||||
config->enable(config, true);
|
||||
|
||||
/* Return our private data structure as an opaque handle */
|
||||
|
||||
return (STMPE11_HANDLE)priv;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,437 @@
|
||||
/****************************************************************************
|
||||
* drivers/input/stmpe11_gpio.c
|
||||
*
|
||||
* Copyright (C) 2012 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* References:
|
||||
* "STMPE811 S-Touch® advanced resistive touchscreen controller with 8-bit
|
||||
* GPIO expander," Doc ID 14489 Rev 6, CD00186725, STMicroelectronics"
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/input/stmpe11.h>
|
||||
|
||||
#include "stmpe11.h"
|
||||
|
||||
#if defined(CONFIG_INPUT) && defined(CONFIG_INPUT_STMPE11) && !defined(CONFIG_STMPE11_GPIO_DISABLE)
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: stmpe11_gpioinit
|
||||
*
|
||||
* Description:
|
||||
* Initialize the GPIO interrupt subsystem
|
||||
*
|
||||
* Input Parameters:
|
||||
* handle - The handle previously returned by stmpe11_instantiate
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero is returned on success. Otherwise, a negated errno value is
|
||||
* returned to indicate the nature of the failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_STMPE11_GPIOINT_DISABLE
|
||||
static inline void stmpe11_gpioinit(FAR struct stmpe11_dev_s *priv)
|
||||
{
|
||||
uint8_t regval;
|
||||
|
||||
if (!priv->initialized)
|
||||
{
|
||||
/* Disable all GPIO interrupts */
|
||||
|
||||
stmpe11_putreg8(priv, STMPE11_GPIO_EN, 0);
|
||||
|
||||
/* Enable global GPIO interrupts */
|
||||
|
||||
regval = stmpe11_getreg8(priv, STMPE11_INT_EN);
|
||||
regval |= INT_GPIO;
|
||||
stmpe11_putreg8(priv, STMPE11_INT_EN, regval);
|
||||
|
||||
priv->initialized = true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: stmpe11_gpioconfig
|
||||
*
|
||||
* Description:
|
||||
* Configure an STMPE11 GPIO pin
|
||||
*
|
||||
* Input Parameters:
|
||||
* handle - The handle previously returned by stmpe11_instantiate
|
||||
* pinconfig - Bit-encoded pin configuration
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero is returned on success. Otherwise, a negated errno value is
|
||||
* returned to indicate the nature of the failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int stmpe11_gpioconfig(STMPE11_HANDLE handle, uint8_t pinconfig)
|
||||
{
|
||||
FAR struct stmpe11_dev_s *priv = (FAR struct stmpe11_dev_s *)handle;
|
||||
int pin = (pinconfig & STMPE11_GPIO_PIN_MASK) >> STMPE11_GPIO_PIN_SHIFT;
|
||||
uint8_t pinmask = (1 << pin);
|
||||
uint8_t regval;
|
||||
int ret;
|
||||
|
||||
DEBUGASSERT(handle && (unsigned)pin < 8);
|
||||
|
||||
/* Get exclusive access to the device structure */
|
||||
|
||||
ret = sem_wait(&priv->exclsem);
|
||||
if (ret < 0)
|
||||
{
|
||||
int errval = errno;
|
||||
idbg("sem_wait failed: %d\n", errval);
|
||||
return -errval;
|
||||
}
|
||||
|
||||
/* Make sure that the pin is not already in use */
|
||||
|
||||
if ((priv->inuse & pinmask) != 0)
|
||||
{
|
||||
idbg("PIN%d is already in-use\n", pin);
|
||||
sem_post(&priv->exclsem);
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
/* Is the pin an input or an output? */
|
||||
|
||||
if ((pinconfig & STMPE11_GPIO_DIR) == STMPE11_GPIO_OUTPUT)
|
||||
{
|
||||
/* The pin is an output */
|
||||
|
||||
regval = stmpe11_getreg8(priv, STMPE11_GPIO_DIR);
|
||||
regval &= ~pinmask;
|
||||
stmpe11_putreg8(priv, STMPE11_GPIO_DIR, regval);
|
||||
|
||||
/* Set its initial output value */
|
||||
|
||||
stmpe11_gpiowrite(handle, pinconfig,
|
||||
(pinconfig & STMPE11_GPIO_VALUE) != STMPE11_GPIO_ZERO);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* It is an input */
|
||||
|
||||
regval = stmpe11_getreg8(priv, STMPE11_GPIO_DIR);
|
||||
regval |= pinmask;
|
||||
stmpe11_putreg8(priv, STMPE11_GPIO_DIR, regval);
|
||||
|
||||
/* Set up the falling edge detection */
|
||||
|
||||
regval = stmpe11_getreg8(priv, STMPE11_GPIO_FE);
|
||||
if ((pinconfig & STMPE11_GPIO_FALLING) != 0)
|
||||
{
|
||||
regval |= pinmask;
|
||||
}
|
||||
else
|
||||
{
|
||||
regval &= pinmask;
|
||||
}
|
||||
stmpe11_putreg8(priv, STMPE11_GPIO_FE, regval);
|
||||
|
||||
/* Set up the rising edge detection */
|
||||
|
||||
regval = stmpe11_getreg8(priv, STMPE11_GPIO_RE);
|
||||
if ((pinconfig & STMPE11_GPIO_FALLING) != 0)
|
||||
{
|
||||
regval |= pinmask;
|
||||
}
|
||||
else
|
||||
{
|
||||
regval &= pinmask;
|
||||
}
|
||||
stmpe11_putreg8(priv, STMPE11_GPIO_RE, regval);
|
||||
|
||||
/* Disable interrupts for now */
|
||||
|
||||
regval = stmpe11_getreg8(priv, STMPE11_GPIO_EN);
|
||||
regval &= ~pinmask;
|
||||
stmpe11_putreg8(priv, STMPE11_GPIO_EN, regval);
|
||||
}
|
||||
|
||||
/* Mark the pin as 'in use' */
|
||||
|
||||
priv->inuse |= pinmask;
|
||||
sem_post(&priv->exclsem);
|
||||
return OK;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: stmpe11_gpiowrite
|
||||
*
|
||||
* Description:
|
||||
* Set or clear the GPIO output
|
||||
*
|
||||
* Input Parameters:
|
||||
* handle - The handle previously returned by stmpe11_instantiate
|
||||
* pinconfig - Bit-encoded pin configuration
|
||||
* value = true: write logic '1'; false: write logic '0;
|
||||
*
|
||||
* Returned Value:
|
||||
* None
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void stmpe11_gpiowrite(STMPE11_HANDLE handle, uint8_t pinconfig, bool value)
|
||||
{
|
||||
FAR struct stmpe11_dev_s *priv = (FAR struct stmpe11_dev_s *)handle;
|
||||
int pin = (pinconfig & STMPE11_GPIO_PIN_MASK) >> STMPE11_GPIO_PIN_SHIFT;
|
||||
int ret;
|
||||
|
||||
DEBUGASSERT(handle && (unsigned)pin < 8);
|
||||
|
||||
/* Get exclusive access to the device structure */
|
||||
|
||||
ret = sem_wait(&priv->exclsem);
|
||||
if (ret < 0)
|
||||
{
|
||||
idbg("sem_wait failed: %d\n", errno);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Are we setting or clearing outputs? */
|
||||
|
||||
if (value)
|
||||
{
|
||||
/* Set the output valu(s)e by writing to the SET register */
|
||||
|
||||
stmpe11_putreg8(priv, STMPE11_GPIO_SETPIN, (1 << pin));
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Clear the output value(s) by writing to the CLR register */
|
||||
|
||||
stmpe11_putreg8(priv, STMPE11_GPIO_CLRPIN, (1 << pin));
|
||||
}
|
||||
|
||||
sem_post(&priv->exclsem);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: stmpe11_gpioread
|
||||
*
|
||||
* Description:
|
||||
* Set or clear the GPIO output
|
||||
*
|
||||
* Input Parameters:
|
||||
* handle - The handle previously returned by stmpe11_instantiate
|
||||
* pinconfig - Bit-encoded pin configuration
|
||||
* value - The location to return the state of the GPIO pin
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero is returned on success. Otherwise, a negated errno value is
|
||||
* returned to indicate the nature of the failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int stmpe11_gpioread(STMPE11_HANDLE handle, uint8_t pinconfig, bool *value)
|
||||
{
|
||||
FAR struct stmpe11_dev_s *priv = (FAR struct stmpe11_dev_s *)handle;
|
||||
int pin = (pinconfig & STMPE11_GPIO_PIN_MASK) >> STMPE11_GPIO_PIN_SHIFT;
|
||||
uint8_t regval;
|
||||
int ret;
|
||||
|
||||
DEBUGASSERT(handle && (unsigned)pin < 8);
|
||||
|
||||
/* Get exclusive access to the device structure */
|
||||
|
||||
ret = sem_wait(&priv->exclsem);
|
||||
if (ret < 0)
|
||||
{
|
||||
int errval = errno;
|
||||
idbg("sem_wait failed: %d\n", errval);
|
||||
return -errval;
|
||||
}
|
||||
|
||||
regval = stmpe11_getreg8(priv, STMPE11_GPIO_MPSTA);
|
||||
*value = ((regval & GPIO_PIN(pin)) != 0);
|
||||
sem_post(&priv->exclsem);
|
||||
return OK;
|
||||
}
|
||||
|
||||
/***********************************************************************************
|
||||
* Name: stmpe11_gpioattach
|
||||
*
|
||||
* Description:
|
||||
* Attach to a GPIO interrupt input pin and enable interrupts on the pin. Using
|
||||
* the value NULL for the handler address will disable interrupts from the pin and
|
||||
* detach the handler.
|
||||
*
|
||||
* NOTE: Callbacks do not occur from an interrupt handler but rather from the
|
||||
* context of the worker thread.
|
||||
*
|
||||
* Input Parameters:
|
||||
* handle - The handle previously returned by stmpe11_instantiate
|
||||
* pinconfig - Bit-encoded pin configuration
|
||||
* handler - The handler that will be called when the interrupt occurs.
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero is returned on success. Otherwise, a negated errno value is returned
|
||||
* to indicate the nature of the failure.
|
||||
*
|
||||
************************************************************************************/
|
||||
|
||||
#ifndef CONFIG_STMPE11_GPIOINT_DISABLE
|
||||
int stmpe11_gpioattach(STMPE11_HANDLE handle, uint8_t pinconfig,
|
||||
stmpe11_handler_t handler)
|
||||
{
|
||||
FAR struct stmpe11_dev_s *priv = (FAR struct stmpe11_dev_s *)handle;
|
||||
int pin = (pinconfig & STMPE11_GPIO_PIN_MASK) >> STMPE11_GPIO_PIN_SHIFT;
|
||||
uint8_t regval;
|
||||
int ret;
|
||||
|
||||
DEBUGASSERT(handle && (unsigned)pin < 8);
|
||||
|
||||
/* Get exclusive access to the device structure */
|
||||
|
||||
ret = sem_wait(&priv->exclsem);
|
||||
if (ret < 0)
|
||||
{
|
||||
int errval = errno;
|
||||
idbg("sem_wait failed: %d\n", errval);
|
||||
return -errval;
|
||||
}
|
||||
|
||||
/* Make sure that the GPIO interrupt system has been initialized */
|
||||
|
||||
stmpe11_gpioinit(priv);
|
||||
|
||||
/* Set/clear the handler */
|
||||
|
||||
priv->handlers[pin] = handler;
|
||||
|
||||
/* If an handler has provided, then we are enabling interrupts */
|
||||
|
||||
regval = stmpe11_getreg8(priv, STMPE11_GPIO_EN);
|
||||
if (handler)
|
||||
{
|
||||
/* Enable interrupts for this GPIO */
|
||||
|
||||
regval &= ~GPIO_PIN(pin);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Disable interrupts for this GPIO */
|
||||
|
||||
regval &= ~GPIO_PIN(pin);
|
||||
}
|
||||
stmpe11_putreg8(priv, STMPE11_GPIO_EN, regval);
|
||||
|
||||
sem_post(&priv->exclsem);
|
||||
return OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: stmpe11_gpioint
|
||||
*
|
||||
* Description:
|
||||
* Handle GPIO interrupt events (this function actually executes in the
|
||||
* context of the worker thread).
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_STMPE11_GPIOINT_DISABLE
|
||||
void stmpe11_gpioint(FAR struct stmpe11_dev_s *priv)
|
||||
{
|
||||
uint8_t regval;
|
||||
uint8_t pinmask;
|
||||
int pin;
|
||||
|
||||
/* Get the set of pending GPIO interrupts */
|
||||
|
||||
regval = stmpe11_getreg8(priv, STMPE11_GPIO_INTSTA);
|
||||
|
||||
/* Look at each pin */
|
||||
|
||||
for (pin = 0; pin < 8; pin++)
|
||||
{
|
||||
pinmask = GPIO_INT(pin);
|
||||
if ((regval & pinmask) != 0)
|
||||
{
|
||||
/* Check if we have a handler for this interrupt (there should
|
||||
* be one)
|
||||
*/
|
||||
|
||||
if (priv->handlers[pin])
|
||||
{
|
||||
/* Interrupt is pending... dispatch the interrupt to the
|
||||
* callback
|
||||
*/
|
||||
|
||||
priv->handlers[pin](pin);
|
||||
}
|
||||
else
|
||||
{
|
||||
illdbg("No handler for PIN%d, GPIO_INTSTA: %02x\n", pin, regval);
|
||||
}
|
||||
|
||||
/* Clear the pending GPIO interrupt by writing a '1' to the
|
||||
* pin position in the status register.
|
||||
*/
|
||||
|
||||
stmpe11_putreg8(priv, STMPE11_GPIO_INTSTA, pinmask);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CONFIG_INPUT && CONFIG_INPUT_STMPE11 */
|
||||
|
||||
Reference in New Issue
Block a user