* drivers/ioexpander/ioe_dummy.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/kmalloc.h>
#include <nuttx/wdog.h>
#include <nuttx/wqueue.h>
#include <nuttx/ioexpander/ioe_dummy.h>
* Pre-processor Definitions
****************************************************************************/
#define IOE_DUMMY_POLLDELAY \
(CONFIG_IOEXPANDER_DUMMY_INT_POLLDELAY / USEC_PER_TICK)
#define IOE_DUMMY_INT_ENABLED(d,p) \
(((d)->intenab & ((ioe_pinset_t)1 << (p))) != 0)
#define IOE_DUMMY_INT_DISABLED(d,p) \
(((d)->intenab & ((ioe_pinset_t)1 << (p))) == 0)
#define IOE_DUMMY_LEVEL_SENSITIVE(d,p) \
(((d)->trigger & ((ioe_pinset_t)1 << (p))) == 0)
#define IOE_DUMMY_LEVEL_HIGH(d,p) \
(((d)->level[0] & ((ioe_pinset_t)1 << (p))) != 0)
#define IOE_DUMMY_LEVEL_LOW(d,p) \
(((d)->level[1] & ((ioe_pinset_t)1 << (p))) != 0)
#define IOE_DUMMY_EDGE_SENSITIVE(d,p) \
(((d)->trigger & ((ioe_pinset_t)1 << (p))) != 0)
#define IOE_DUMMY_EDGE_RISING(d,p) \
(((d)->level[0] & ((ioe_pinset_t)1 << (p))) != 0)
#define IOE_DUMMY_EDGE_FALLING(d,p) \
(((d)->level[1] & ((ioe_pinset_t)1 << (p))) != 0)
#define IOE_DUMMY_EDGE_BOTH(d,p) \
(IOE_DUMMY_LEVEL_RISING(d,p) && IOE_DUMMY_LEVEL_FALLING(d,p))
* Private Types
****************************************************************************/
struct ioe_dummy_callback_s
{
ioe_pinset_t pinset;
* the callback. */
ioe_callback_t cbfunc;
FAR void *cbarg;
};
struct ioe_dummy_dev_s
{
struct ioexpander_dev_s dev;
* public GPIO expander. */
ioe_pinset_t inpins;
ioe_pinset_t invert;
ioe_pinset_t outval;
ioe_pinset_t inval;
ioe_pinset_t intenab;
ioe_pinset_t last;
* changes) */
ioe_pinset_t trigger;
ioe_pinset_t level[2];
* 10 low/falling, 11 both */
struct wdog_s wdog;
* simulation */
struct work_s work;
* "bottom half" */
struct ioe_dummy_callback_s cb[CONFIG_IOEXPANDER_DUMMY_INT_NCALLBACKS];
};
* Private Function Prototypes
****************************************************************************/
static int ioe_dummy_direction(FAR struct ioexpander_dev_s *dev,
uint8_t pin, int dir);
static int ioe_dummy_option(FAR struct ioexpander_dev_s *dev,
uint8_t pin, int opt, void *regval);
static int ioe_dummy_writepin(FAR struct ioexpander_dev_s *dev,
uint8_t pin, bool value);
static int ioe_dummy_readpin(FAR struct ioexpander_dev_s *dev,
uint8_t pin, FAR bool *value);
#ifdef CONFIG_IOEXPANDER_MULTIPIN
static int ioe_dummy_multiwritepin(FAR struct ioexpander_dev_s *dev,
FAR const uint8_t *pins,
FAR const bool *values, int count);
static int ioe_dummy_multireadpin(FAR struct ioexpander_dev_s *dev,
FAR const uint8_t *pins,
FAR bool *values, int count);
#endif
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
static FAR void *ioe_dummy_attach(FAR struct ioexpander_dev_s *dev,
ioe_pinset_t pinset,
ioe_callback_t callback, FAR void *arg);
static int ioe_dummy_detach(FAR struct ioexpander_dev_s *dev,
FAR void *handle);
#endif
static ioe_pinset_t ioe_dummy_int_update(FAR struct ioe_dummy_dev_s *priv);
static void ioe_dummy_interrupt_work(void *arg);
static void ioe_dummy_interrupt(wdparm_t arg);
* Private Data
****************************************************************************/
* well be pre-allocated.
*/
static struct ioe_dummy_dev_s g_ioexpander;
static const struct ioexpander_ops_s g_ioe_dummy_ops =
{
ioe_dummy_direction,
ioe_dummy_option,
ioe_dummy_writepin,
ioe_dummy_readpin,
ioe_dummy_readpin
#ifdef CONFIG_IOEXPANDER_MULTIPIN
, ioe_dummy_multiwritepin
, ioe_dummy_multireadpin
, ioe_dummy_multireadpin
#endif
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
, ioe_dummy_attach
, ioe_dummy_detach
#endif
};
* Private Functions
****************************************************************************/
* Name: ioe_dummy_direction
*
* Description:
* Set the direction of an ioexpander pin. Required.
*
* Input Parameters:
* dev - Device-specific state data
* pin - The index of the pin to alter in this call
* dir - One of the IOEXPANDER_DIRECTION_ macros
*
* Returned Value:
* 0 on success, else a negative error code
*
****************************************************************************/
static int ioe_dummy_direction(FAR struct ioexpander_dev_s *dev,
uint8_t pin, int direction)
{
FAR struct ioe_dummy_dev_s *priv = (FAR struct ioe_dummy_dev_s *)dev;
if (direction != IOEXPANDER_DIRECTION_IN &&
direction != IOEXPANDER_DIRECTION_OUT)
{
return -EINVAL;
}
DEBUGASSERT(priv != NULL && pin < CONFIG_IOEXPANDER_NPINS);
gpioinfo("pin=%u direction=%s\n",
pin, (direction == IOEXPANDER_DIRECTION_IN) ? "IN" : "OUT");
if (direction == IOEXPANDER_DIRECTION_IN)
{
priv->inpins |= ((ioe_pinset_t)1 << pin);
}
else
{
* 0, the corresponding port pin is enabled as an output.
*
* REVISIT: The value of output has not been selected! This might
* put a glitch on the output.
*/
priv->inpins &= ~((ioe_pinset_t)1 << pin);
}
return OK;
}
* Name: ioe_dummy_option
*
* Description:
* Set pin options. Required.
* Since all IO expanders have various pin options, this API allows setting
* pin options in a flexible way.
*
* Input Parameters:
* dev - Device-specific state data
* pin - The index of the pin to alter in this call
* opt - One of the IOEXPANDER_OPTION_ macros
* val - The option's value
*
* Returned Value:
* 0 on success, else a negative error code
*
****************************************************************************/
static int ioe_dummy_option(FAR struct ioexpander_dev_s *dev, uint8_t pin,
int opt, FAR void *value)
{
FAR struct ioe_dummy_dev_s *priv = (FAR struct ioe_dummy_dev_s *)dev;
int ret = -ENOSYS;
DEBUGASSERT(priv != NULL);
gpioinfo("pin=%u option=%u\n", pin, opt);
* allows polarity inversion of pins defined as inputs by the
* Configuration Register. If a bit in this register is set, the
* corresponding port pin's polarity is inverted. If a bit in this
* register is cleared, the corresponding port pin's original polarity
* is retained.
*/
if (opt == IOEXPANDER_OPTION_INVERT)
{
if ((uintptr_t)value == IOEXPANDER_VAL_INVERT)
{
priv->invert |= ((ioe_pinset_t)1 << pin);
}
else
{
priv->invert &= ~((ioe_pinset_t)1 << pin);
}
}
else if (opt == IOEXPANDER_OPTION_INTCFG)
{
ioe_pinset_t bit = ((ioe_pinset_t)1 << pin);
ret = OK;
switch ((uintptr_t)value)
{
case IOEXPANDER_VAL_HIGH:
priv->intenab |= bit;
priv->trigger &= ~bit;
priv->level[0] |= bit;
priv->level[1] &= ~bit;
break;
case IOEXPANDER_VAL_LOW:
priv->intenab |= bit;
priv->trigger &= ~bit;
priv->level[0] &= ~bit;
priv->level[1] |= bit;
break;
case IOEXPANDER_VAL_RISING:
priv->intenab |= bit;
priv->trigger |= bit;
priv->level[0] |= bit;
priv->level[1] &= ~bit;
break;
case IOEXPANDER_VAL_FALLING:
priv->intenab |= bit;
priv->trigger |= bit;
priv->level[0] &= ~bit;
priv->level[1] |= bit;
break;
case IOEXPANDER_VAL_BOTH:
priv->intenab |= bit;
priv->trigger |= bit;
priv->level[0] |= bit;
priv->level[1] |= bit;
break;
case IOEXPANDER_VAL_DISABLE:
priv->trigger &= ~bit;
break;
default:
ret = -EINVAL;
break;
}
}
return ret;
}
* Name: ioe_dummy_writepin
*
* Description:
* Set the pin level. Required.
*
* Input Parameters:
* dev - Device-specific state data
* pin - The index of the pin to alter in this call
* val - The pin level. Usually TRUE will set the pin high,
* except if OPTION_INVERT has been set on this pin.
*
* Returned Value:
* 0 on success, else a negative error code
*
****************************************************************************/
static int ioe_dummy_writepin(FAR struct ioexpander_dev_s *dev,
uint8_t pin, bool value)
{
FAR struct ioe_dummy_dev_s *priv = (FAR struct ioe_dummy_dev_s *)dev;
DEBUGASSERT(priv != NULL && pin < CONFIG_IOEXPANDER_NPINS);
gpioinfo("pin=%u value=%u\n", pin, (unsigned int)value);
* Output Port Register shows the outgoing logic levels of the pins
* defined as outputs by the Configuration Register.
*/
if (value == (((priv->invert >> pin) & 1) == 0))
{
priv->outval |= ((ioe_pinset_t)1 << pin);
}
else
{
priv->outval &= ~((ioe_pinset_t)1 << pin);
}
return OK;
}
* Name: ioe_dummy_readpin
*
* Description:
* Read the actual PIN level. This can be different from the last value
* written to this pin. Required.
*
* Input Parameters:
* dev - Device-specific state data
* pin - The index of the pin
* valptr - Pointer to a buffer where the pin level is stored. Usually TRUE
* if the pin is high, except if OPTION_INVERT has been set on
* this pin.
*
* Returned Value:
* 0 on success, else a negative error code
*
****************************************************************************/
static int ioe_dummy_readpin(FAR struct ioexpander_dev_s *dev,
uint8_t pin, FAR bool *value)
{
FAR struct ioe_dummy_dev_s *priv = (FAR struct ioe_dummy_dev_s *)dev;
ioe_pinset_t inval;
DEBUGASSERT(priv != NULL && pin < CONFIG_IOEXPANDER_NPINS &&
value != NULL);
gpioinfo("pin=%u\n", pin);
if (((priv->inpins >> pin) & 1) != 0)
{
inval = priv->inval;
}
else
{
inval = priv->outval;
}
*value = ((((inval ^ priv->invert) >> pin) & 1) != 0);
return OK;
}
* Name: ioe_dummy_multiwritepin
*
* Description:
* Set the pin level for multiple pins. This routine may be faster than
* individual pin accesses. Optional.
*
* Input Parameters:
* dev - Device-specific state data
* pins - The list of pin indexes to alter in this call
* val - The list of pin levels.
*
* Returned Value:
* 0 on success, else a negative error code
*
****************************************************************************/
#ifdef CONFIG_IOEXPANDER_MULTIPIN
static int ioe_dummy_multiwritepin(FAR struct ioexpander_dev_s *dev,
FAR const uint8_t *pins,
FAR const bool *values, int count)
{
FAR struct ioe_dummy_dev_s *priv = (FAR struct ioe_dummy_dev_s *)dev;
uint8_t pin;
int i;
gpioinfo("count=%d\n", count);
DEBUGASSERT(priv != NULL && pins != NULL && values != NULL && count > 0);
for (i = 0; i < count; i++)
{
pin = pins[i];
DEBUGASSERT(pin < CONFIG_IOEXPANDER_NPINS);
if (values[i] == (((priv->invert >> pin) & 1) == 0))
{
priv->outval |= ((ioe_pinset_t)1 << pin);
}
else
{
priv->outval &= ~((ioe_pinset_t)1 << pin);
}
}
return OK;
}
#endif
* Name: ioe_dummy_multireadpin
*
* Description:
* Read the actual level for multiple pins. This routine may be faster than
* individual pin accesses. Optional.
*
* Input Parameters:
* dev - Device-specific state data
* pin - The list of pin indexes to read
* valptr - Pointer to a buffer where the pin levels are stored.
*
* Returned Value:
* 0 on success, else a negative error code
*
****************************************************************************/
#ifdef CONFIG_IOEXPANDER_MULTIPIN
static int ioe_dummy_multireadpin(FAR struct ioexpander_dev_s *dev,
FAR const uint8_t *pins,
FAR bool *values, int count)
{
FAR struct ioe_dummy_dev_s *priv = (FAR struct ioe_dummy_dev_s *)dev;
ioe_pinset_t inval;
uint8_t pin;
int i;
gpioinfo("count=%d\n", count);
DEBUGASSERT(priv != NULL && pins != NULL && values != NULL && count > 0);
for (i = 0; i < count; i++)
{
pin = pins[i];
DEBUGASSERT(pin < CONFIG_IOEXPANDER_NPINS);
if (((priv->inpins >> pin) & 1) != 0)
{
inval = priv->inval;
}
else
{
inval = priv->outval;
}
values[i] = ((((inval ^ priv->invert) >> pin) & 1) != 0);
}
return OK;
}
#endif
* Name: ioe_dummy_attach
*
* Description:
* Attach and enable a pin interrupt callback function.
*
* Input Parameters:
* dev - Device-specific state data
* pinset - The set of pin events that will generate the callback
* callback - The pointer to callback function. NULL will detach the
* callback.
* arg - User-provided callback argument
*
* Returned Value:
* A non-NULL handle value is returned on success. This handle may be
* used later to detach and disable the pin interrupt.
*
****************************************************************************/
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
static FAR void *ioe_dummy_attach(FAR struct ioexpander_dev_s *dev,
ioe_pinset_t pinset,
ioe_callback_t callback, FAR void *arg)
{
FAR struct ioe_dummy_dev_s *priv = (FAR struct ioe_dummy_dev_s *)dev;
FAR void *handle = NULL;
int i;
gpioinfo("pinset=%lx callback=%p arg=%p\n",
(unsigned long)pinset, callback, arg);
for (i = 0; i < CONFIG_IOEXPANDER_DUMMY_INT_NCALLBACKS; i++)
{
if (priv->cb[i].cbfunc == NULL)
{
priv->cb[i].pinset = pinset;
priv->cb[i].cbfunc = callback;
priv->cb[i].cbarg = arg;
handle = &priv->cb[i];
break;
}
}
return handle;
}
#endif
* Name: ioe_dummy_detach
*
* Description:
* Detach and disable a pin interrupt callback function.
*
* Input Parameters:
* dev - Device-specific state data
* handle - The non-NULL opaque value return by ioe_dummy_attch()
*
* Returned Value:
* 0 on success, else a negative error code
*
****************************************************************************/
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
static int ioe_dummy_detach(FAR struct ioexpander_dev_s *dev,
FAR void *handle)
{
FAR struct ioe_dummy_dev_s *priv = (FAR struct ioe_dummy_dev_s *)dev;
FAR struct ioe_dummy_callback_s *cb =
(FAR struct ioe_dummy_callback_s *)handle;
gpioinfo("handle=%p\n", handle);
DEBUGASSERT(priv != NULL && cb != NULL);
DEBUGASSERT((uintptr_t)cb >= (uintptr_t)&priv->cb[0] && (uintptr_t)cb <=
(uintptr_t)&priv->cb[CONFIG_IOEXPANDER_DUMMY_INT_NCALLBACKS - 1]);
UNUSED(priv);
cb->pinset = 0;
cb->cbfunc = NULL;
cb->cbarg = NULL;
return OK;
}
#endif
* Name: ioe_dummy_int_update
*
* Description:
* Check for pending interrupts.
*
****************************************************************************/
static ioe_pinset_t ioe_dummy_int_update(FAR struct ioe_dummy_dev_s *priv)
{
ioe_pinset_t toggles;
ioe_pinset_t diff;
ioe_pinset_t input;
ioe_pinset_t intstat;
bool pinval;
int pin;
int i;
* handler. This is a crude simulation for toggle interrupt inputs.
*/
toggles = 0;
for (i = 0; i < CONFIG_IOEXPANDER_DUMMY_INT_NCALLBACKS; i++)
{
if (priv->cb[i].cbfunc != NULL)
{
toggles |= (priv->cb[i].pinset & priv->inpins);
}
}
priv->inval = (priv->inval & ~toggles) | (~priv->inval & toggles);
input = priv->inval;
diff = priv->last ^ input;
if (diff != 0)
{
gpioinfo("toggles=%lx inval=%lx last=%lx diff=%lx\n",
(unsigned long)toggles, (unsigned long)priv->inval,
(unsigned long)priv->last, (unsigned long)diff);
}
priv->last = input;
intstat = 0;
for (pin = 0; pin < CONFIG_IOEXPANDER_NPINS; pin++)
{
pinval = ((((input ^ priv->invert) >> pin) & 1) != 0);
if (IOE_DUMMY_INT_DISABLED(priv, pin))
{
* next pin.
*/
}
else if (IOE_DUMMY_EDGE_SENSITIVE(priv, pin))
{
if ((diff & 1) != 0)
{
if ((!pinval && IOE_DUMMY_EDGE_FALLING(priv, pin)) ||
(pinval && IOE_DUMMY_EDGE_RISING(priv, pin)))
{
intstat |= ((ioe_pinset_t)1 << pin);
}
}
}
else
{
if ((pinval && IOE_DUMMY_LEVEL_HIGH(priv, pin)) ||
(!pinval && IOE_DUMMY_LEVEL_LOW(priv, pin)))
{
intstat |= ((ioe_pinset_t)1 << pin);
}
}
diff >>= 1;
}
return intstat;
}
* Name: ioe_dummy_interrupt_work
*
* Description:
* Handle GPIO interrupt events (this function actually executes in the
* context of the worker thread).
*
****************************************************************************/
static void ioe_dummy_interrupt_work(void *arg)
{
FAR struct ioe_dummy_dev_s *priv = (FAR struct ioe_dummy_dev_s *)arg;
ioe_pinset_t intstat;
int ret;
int i;
DEBUGASSERT(priv != NULL);
intstat = ioe_dummy_int_update(priv);
if (intstat != 0)
{
gpioinfo("intstat=%lx\n", (unsigned long)intstat);
for (i = 0; i < CONFIG_IOEXPANDER_DUMMY_INT_NCALLBACKS; i++)
{
if (priv->cb[i].cbfunc != NULL)
{
ioe_pinset_t match = intstat & priv->cb[i].pinset;
if (match != 0)
{
priv->cb[i].cbfunc(&priv->dev, match, priv->cb[i].cbarg);
}
}
}
}
ret = wd_start(&priv->wdog, IOE_DUMMY_POLLDELAY,
ioe_dummy_interrupt, (wdparm_t)priv);
if (ret < 0)
{
gpioerr("ERROR: Failed to start poll timer\n");
}
}
* Name: ioe_dummy_interrupt
*
* Description:
* The poll timer has expired; check for missed interrupts
*
* Input Parameters:
* Standard wdog expiration arguments.
*
****************************************************************************/
static void ioe_dummy_interrupt(wdparm_t arg)
{
FAR struct ioe_dummy_dev_s *priv;
priv = (FAR struct ioe_dummy_dev_s *)arg;
DEBUGASSERT(priv != NULL);
* much kinder in the use of system resources but is probably necessary
* to access the I/O expander device.
*
* Notice that further GPIO interrupts are disabled until the work is
* actually performed. This is to prevent overrun of the worker thread.
* Interrupts are re-enabled in ioe_dummy_interrupt_work() when the work is
* completed.
*/
if (work_available(&priv->work))
{
* thread.
*/
work_queue(HPWORK, &priv->work, ioe_dummy_interrupt_work,
(FAR void *)priv, 0);
}
}
* Public Functions
****************************************************************************/
* Name: ioe_dummy_initialize
*
* Description:
* Instantiate and configure the I/O Expander device driver to use the
* provided I2C device instance.
*
* Input Parameters:
* i2c - An I2C driver instance
* minor - The device i2c address
* config - Persistent board configuration data
*
* Returned Value:
* an ioexpander_dev_s instance on success, NULL on failure.
*
****************************************************************************/
FAR struct ioexpander_dev_s *ioe_dummy_initialize(void)
{
FAR struct ioe_dummy_dev_s *priv = &g_ioexpander;
int ret;
priv->dev.ops = &g_ioe_dummy_ops;
priv->trigger = PINSET_ALL;
priv->level[0] = PINSET_ALL;
priv->level[1] = PINSET_ALL;
ret = wd_start(&priv->wdog, IOE_DUMMY_POLLDELAY,
ioe_dummy_interrupt, (wdparm_t)priv);
if (ret < 0)
{
gpioerr("ERROR: Failed to start poll timer\n");
}
return &priv->dev;
}