2015-07-09 15:31:22 +02:00
|
|
|
/****************************************************************************
|
2015-12-15 15:40:34 +01:00
|
|
|
* drivers/ioexpander/pca9555.c
|
2015-07-09 15:31:22 +02:00
|
|
|
*
|
2021-03-31 10:52:24 +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
|
2015-07-09 15:31:22 +02:00
|
|
|
*
|
2021-03-31 10:52:24 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-07-09 15:31:22 +02:00
|
|
|
*
|
2021-03-31 10:52:24 +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.
|
2015-07-09 15:31:22 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-20 11:10:04 +01:00
|
|
|
/* References:
|
|
|
|
* "16-bit I2C-bus and SMBus I/O port with interrupt product datasheet",
|
|
|
|
* Rev. 08 - 22 October 2009, NXP
|
|
|
|
*/
|
|
|
|
|
2015-07-09 15:31:22 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2015-11-13 16:28:46 +01:00
|
|
|
#include <nuttx/irq.h>
|
2016-01-30 15:00:16 +01:00
|
|
|
#include <nuttx/i2c/i2c_master.h>
|
2015-11-01 20:14:48 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2015-12-15 15:40:34 +01:00
|
|
|
#include <nuttx/ioexpander/ioexpander.h>
|
2015-07-09 15:31:22 +02:00
|
|
|
|
|
|
|
#include "pca9555.h"
|
|
|
|
|
|
|
|
#if defined(CONFIG_IOEXPANDER_PCA9555)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_I2C
|
|
|
|
# warning I2C support is required (CONFIG_I2C)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-01-26 18:07:47 +01:00
|
|
|
static inline int pca9555_write(FAR struct pca9555_dev_s *pca,
|
|
|
|
FAR const uint8_t *wbuffer, int wbuflen);
|
|
|
|
static inline int pca9555_writeread(FAR struct pca9555_dev_s *pca,
|
2016-01-26 14:59:36 +01:00
|
|
|
FAR const uint8_t *wbuffer, int wbuflen, FAR uint8_t *rbuffer,
|
|
|
|
int rbuflen);
|
2016-01-22 14:08:59 +01:00
|
|
|
static int pca9555_direction(FAR struct ioexpander_dev_s *dev, uint8_t pin,
|
|
|
|
int dir);
|
|
|
|
static int pca9555_option(FAR struct ioexpander_dev_s *dev, uint8_t pin,
|
|
|
|
int opt, void *val);
|
|
|
|
static int pca9555_writepin(FAR struct ioexpander_dev_s *dev, uint8_t pin,
|
|
|
|
bool value);
|
|
|
|
static int pca9555_readpin(FAR struct ioexpander_dev_s *dev, uint8_t pin,
|
|
|
|
FAR bool *value);
|
|
|
|
static int pca9555_readbuf(FAR struct ioexpander_dev_s *dev, uint8_t pin,
|
|
|
|
FAR bool *value);
|
2015-07-10 18:33:07 +02:00
|
|
|
#ifdef CONFIG_IOEXPANDER_MULTIPIN
|
2016-01-22 14:08:59 +01:00
|
|
|
static int pca9555_multiwritepin(FAR struct ioexpander_dev_s *dev,
|
2023-06-13 17:23:58 +02:00
|
|
|
FAR const uint8_t *pins, FAR bool *values, int count);
|
2015-07-10 18:33:07 +02:00
|
|
|
static int pca9555_multireadpin(FAR struct ioexpander_dev_s *dev,
|
2023-06-13 17:23:58 +02:00
|
|
|
FAR const uint8_t *pins, FAR bool *values, int count);
|
2015-07-10 18:33:07 +02:00
|
|
|
static int pca9555_multireadbuf(FAR struct ioexpander_dev_s *dev,
|
2023-06-13 17:23:58 +02:00
|
|
|
FAR const uint8_t *pins, FAR bool *values, int count);
|
2015-07-10 18:33:07 +02:00
|
|
|
#endif
|
2016-07-31 19:09:02 +02:00
|
|
|
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
|
2016-08-01 15:26:04 +02:00
|
|
|
static FAR void *pca9555_attach(FAR struct ioexpander_dev_s *dev,
|
|
|
|
ioe_pinset_t pinset, ioe_callback_t callback, FAR void *arg);
|
|
|
|
static int pca9555_detach(FAR struct ioexpander_dev_s *dev,
|
|
|
|
FAR void *handle);
|
2016-07-31 19:09:02 +02:00
|
|
|
#endif
|
2015-07-09 15:31:22 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_PCA9555_MULTIPLE
|
|
|
|
/* If only a single PCA9555 device is supported, then the driver state
|
|
|
|
* structure may as well be pre-allocated.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct pca9555_dev_s g_pca9555;
|
|
|
|
|
|
|
|
/* Otherwise, we will need to maintain allocated driver instances in a list */
|
|
|
|
|
|
|
|
#else
|
|
|
|
static struct pca9555_dev_s *g_pca9555list;
|
|
|
|
#endif
|
|
|
|
|
2016-07-31 19:09:02 +02:00
|
|
|
/* I/O expander vtable */
|
|
|
|
|
2015-07-09 15:31:22 +02:00
|
|
|
static const struct ioexpander_ops_s g_pca9555_ops =
|
|
|
|
{
|
|
|
|
pca9555_direction,
|
|
|
|
pca9555_option,
|
2016-01-22 14:08:59 +01:00
|
|
|
pca9555_writepin,
|
2015-07-09 15:31:22 +02:00
|
|
|
pca9555_readpin,
|
2016-07-31 19:09:02 +02:00
|
|
|
pca9555_readbuf
|
2015-07-10 18:33:07 +02:00
|
|
|
#ifdef CONFIG_IOEXPANDER_MULTIPIN
|
2016-07-31 19:09:02 +02:00
|
|
|
, pca9555_multiwritepin
|
|
|
|
, pca9555_multireadpin
|
|
|
|
, pca9555_multireadbuf
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
|
|
|
|
, pca9555_attach
|
2016-08-01 15:26:04 +02:00
|
|
|
, pca9555_detach
|
2015-07-10 18:33:07 +02:00
|
|
|
#endif
|
2015-07-09 15:31:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-01-26 18:07:47 +01:00
|
|
|
/****************************************************************************
|
2016-02-01 15:57:22 +01:00
|
|
|
* Name: pca9555_write
|
2016-01-26 18:07:47 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2016-02-01 15:57:22 +01:00
|
|
|
* Write to the I2C device.
|
2016-01-26 18:07:47 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int pca9555_write(FAR struct pca9555_dev_s *pca,
|
2016-02-02 19:53:32 +01:00
|
|
|
FAR const uint8_t *wbuffer, int wbuflen)
|
2016-01-26 18:07:47 +01:00
|
|
|
{
|
2016-02-02 14:13:03 +01:00
|
|
|
struct i2c_msg_s msg;
|
2017-08-04 15:31:36 +02:00
|
|
|
int ret;
|
2016-01-26 18:07:47 +01:00
|
|
|
|
2016-02-02 14:13:03 +01:00
|
|
|
/* Setup for the transfer */
|
2016-01-26 18:07:47 +01:00
|
|
|
|
2016-02-02 14:13:03 +01:00
|
|
|
msg.frequency = pca->config->frequency;
|
|
|
|
msg.addr = pca->config->address;
|
|
|
|
msg.flags = 0;
|
|
|
|
msg.buffer = (FAR uint8_t *)wbuffer; /* Override const */
|
|
|
|
msg.length = wbuflen;
|
2016-01-26 18:07:47 +01:00
|
|
|
|
2016-02-02 14:13:03 +01:00
|
|
|
/* Then perform the transfer. */
|
|
|
|
|
2017-08-04 15:31:36 +02:00
|
|
|
ret = I2C_TRANSFER(pca->i2c, &msg, 1);
|
|
|
|
return (ret >= 0) ? OK : ret;
|
2016-01-26 18:07:47 +01:00
|
|
|
}
|
|
|
|
|
2016-01-26 14:59:36 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pca9555_writeread
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write to then read from the I2C device.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-01-26 16:58:18 +01:00
|
|
|
static inline int pca9555_writeread(FAR struct pca9555_dev_s *pca,
|
|
|
|
FAR const uint8_t *wbuffer, int wbuflen,
|
|
|
|
FAR uint8_t *rbuffer, int rbuflen)
|
2016-01-26 14:59:36 +01:00
|
|
|
{
|
2016-01-26 16:58:18 +01:00
|
|
|
struct i2c_config_s config;
|
2016-01-26 14:59:36 +01:00
|
|
|
|
2016-01-26 16:58:18 +01:00
|
|
|
/* Set up the configuration and perform the write-read operation */
|
2016-01-26 14:59:36 +01:00
|
|
|
|
2016-01-26 16:58:18 +01:00
|
|
|
config.frequency = pca->config->frequency;
|
|
|
|
config.address = pca->config->address;
|
|
|
|
config.addrlen = 7;
|
2016-01-26 14:59:36 +01:00
|
|
|
|
2020-03-30 23:06:15 +02:00
|
|
|
return i2c_writeread(pca->i2c, &config, wbuffer, wbuflen,
|
|
|
|
rbuffer, rbuflen);
|
2016-01-26 14:59:36 +01:00
|
|
|
}
|
|
|
|
|
2015-07-09 15:31:22 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pca9555_setbit
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write a bit in a register pair
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-02-03 19:39:11 +01:00
|
|
|
static int pca9555_setbit(FAR struct pca9555_dev_s *pca, uint8_t addr,
|
2022-03-01 17:18:50 +01:00
|
|
|
uint8_t pin, bool bitval)
|
2015-07-09 15:31:22 +02:00
|
|
|
{
|
|
|
|
uint8_t buf[2];
|
2016-01-26 14:59:36 +01:00
|
|
|
int ret;
|
2015-07-09 15:31:22 +02:00
|
|
|
|
2015-07-09 15:36:10 +02:00
|
|
|
if (pin > 15)
|
2015-07-09 15:31:22 +02:00
|
|
|
{
|
|
|
|
return -ENXIO;
|
|
|
|
}
|
2015-07-09 15:36:10 +02:00
|
|
|
else if (pin > 7)
|
2015-07-09 15:31:22 +02:00
|
|
|
{
|
2016-01-26 14:59:36 +01:00
|
|
|
addr++;
|
2015-07-09 15:31:22 +02:00
|
|
|
pin -= 8;
|
|
|
|
}
|
|
|
|
|
2016-01-26 14:59:36 +01:00
|
|
|
buf[0] = addr;
|
|
|
|
|
2016-08-01 19:10:11 +02:00
|
|
|
#ifdef CONFIG_PCA9555_SHADOW_MODE
|
2016-06-27 16:11:54 +02:00
|
|
|
/* Get the shadowed register value */
|
|
|
|
|
|
|
|
buf[1] = pca->sreg[addr];
|
|
|
|
|
|
|
|
#else
|
|
|
|
/* Get the register value from the IO-Expander */
|
|
|
|
|
2016-01-26 14:59:36 +01:00
|
|
|
ret = pca9555_writeread(pca, &buf[0], 1, &buf[1], 1);
|
2015-07-10 18:33:07 +02:00
|
|
|
if (ret < 0)
|
2015-07-09 15:31:22 +02:00
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2016-06-27 16:11:54 +02:00
|
|
|
#endif
|
2015-07-09 15:31:22 +02:00
|
|
|
|
2015-07-09 15:36:10 +02:00
|
|
|
if (bitval)
|
2015-07-09 15:31:22 +02:00
|
|
|
{
|
2015-07-10 18:33:07 +02:00
|
|
|
buf[1] |= (1 << pin);
|
2015-07-09 15:31:22 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-07-10 18:33:07 +02:00
|
|
|
buf[1] &= ~(1 << pin);
|
2015-07-09 15:31:22 +02:00
|
|
|
}
|
|
|
|
|
2016-08-01 19:10:11 +02:00
|
|
|
#ifdef CONFIG_PCA9555_SHADOW_MODE
|
2016-06-27 16:11:54 +02:00
|
|
|
/* Save the new register value in the shadow register */
|
|
|
|
|
|
|
|
pca->sreg[addr] = buf[1];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ret = pca9555_write(pca, buf, 2);
|
2016-08-01 19:10:11 +02:00
|
|
|
#ifdef CONFIG_PCA9555_RETRY
|
2016-06-27 16:11:54 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
/* Try again (only once) */
|
|
|
|
|
|
|
|
ret = pca9555_write(pca, buf, 2);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
2015-07-09 15:31:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pca9555_getbit
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get a bit from a register pair
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-02-03 19:39:11 +01:00
|
|
|
static int pca9555_getbit(FAR struct pca9555_dev_s *pca, uint8_t addr,
|
2015-07-10 18:33:07 +02:00
|
|
|
uint8_t pin, FAR bool *val)
|
2015-07-09 15:31:22 +02:00
|
|
|
{
|
|
|
|
uint8_t buf;
|
|
|
|
int ret;
|
|
|
|
|
2015-07-10 18:33:07 +02:00
|
|
|
if (pin > 15)
|
2015-07-09 15:31:22 +02:00
|
|
|
{
|
|
|
|
return -ENXIO;
|
|
|
|
}
|
2015-07-10 18:33:07 +02:00
|
|
|
else if (pin > 7)
|
2015-07-09 15:31:22 +02:00
|
|
|
{
|
|
|
|
addr += 1;
|
|
|
|
pin -= 8;
|
|
|
|
}
|
|
|
|
|
2016-01-26 14:59:36 +01:00
|
|
|
ret = pca9555_writeread(pca, &addr, 1, &buf, 1);
|
2015-07-10 18:33:07 +02:00
|
|
|
if (ret < 0)
|
2015-07-09 15:31:22 +02:00
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-08-01 19:10:11 +02:00
|
|
|
#ifdef CONFIG_PCA9555_SHADOW_MODE
|
2016-06-27 16:11:54 +02:00
|
|
|
/* Save the new register value in the shadow register */
|
|
|
|
|
|
|
|
pca->sreg[addr] = buf;
|
|
|
|
#endif
|
|
|
|
|
2015-07-10 18:33:07 +02:00
|
|
|
*val = (buf >> pin) & 1;
|
|
|
|
return OK;
|
2015-07-09 15:31:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pca9555_direction
|
|
|
|
*
|
|
|
|
* Description:
|
2016-07-31 23:43:36 +02:00
|
|
|
* 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
|
2015-07-09 15:31:22 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pca9555_direction(FAR struct ioexpander_dev_s *dev, uint8_t pin,
|
|
|
|
int direction)
|
|
|
|
{
|
2016-02-03 19:39:11 +01:00
|
|
|
FAR struct pca9555_dev_s *pca = (FAR struct pca9555_dev_s *)dev;
|
|
|
|
int ret;
|
|
|
|
|
2020-07-02 12:47:58 +02:00
|
|
|
if (direction != IOEXPANDER_DIRECTION_IN &&
|
|
|
|
direction != IOEXPANDER_DIRECTION_OUT)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2016-02-03 19:39:11 +01:00
|
|
|
/* Get exclusive access to the PCA555 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&pca->lock);
|
2020-03-30 23:06:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-02-03 19:39:11 +01:00
|
|
|
ret = pca9555_setbit(pca, PCA9555_REG_CONFIG, pin,
|
|
|
|
(direction == IOEXPANDER_DIRECTION_IN));
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&pca->lock);
|
2016-02-03 19:39:11 +01:00
|
|
|
return ret;
|
2015-07-09 15:31:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pca9555_option
|
|
|
|
*
|
|
|
|
* Description:
|
2016-07-31 23:43:36 +02:00
|
|
|
* 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
|
2015-07-09 15:31:22 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pca9555_option(FAR struct ioexpander_dev_s *dev, uint8_t pin,
|
2022-03-01 17:18:50 +01:00
|
|
|
int opt, FAR void *value)
|
2015-07-09 15:31:22 +02:00
|
|
|
{
|
2016-02-03 19:39:11 +01:00
|
|
|
FAR struct pca9555_dev_s *pca = (FAR struct pca9555_dev_s *)dev;
|
|
|
|
int ret = -EINVAL;
|
2015-07-09 15:31:22 +02:00
|
|
|
|
2016-08-01 02:33:04 +02:00
|
|
|
if (opt == IOEXPANDER_OPTION_INVERT)
|
2015-07-09 15:31:22 +02:00
|
|
|
{
|
2016-02-03 19:39:11 +01:00
|
|
|
/* Get exclusive access to the PCA555 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&pca->lock);
|
2020-03-30 23:06:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-01 17:18:50 +01:00
|
|
|
ret = pca9555_setbit(pca, PCA9555_REG_POLINV, pin,
|
|
|
|
((uintptr_t)value == IOEXPANDER_VAL_INVERT));
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&pca->lock);
|
2015-07-09 15:31:22 +02:00
|
|
|
}
|
2015-07-10 18:33:07 +02:00
|
|
|
|
2016-02-03 19:39:11 +01:00
|
|
|
return ret;
|
2015-07-09 15:31:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2016-01-22 14:08:59 +01:00
|
|
|
* Name: pca9555_writepin
|
2015-07-09 15:31:22 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2016-07-31 23:43:36 +02:00
|
|
|
* 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
|
2015-07-09 15:31:22 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-01-22 14:08:59 +01:00
|
|
|
static int pca9555_writepin(FAR struct ioexpander_dev_s *dev, uint8_t pin,
|
|
|
|
bool value)
|
2015-07-09 15:31:22 +02:00
|
|
|
{
|
2016-02-03 19:39:11 +01:00
|
|
|
FAR struct pca9555_dev_s *pca = (FAR struct pca9555_dev_s *)dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get exclusive access to the PCA555 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&pca->lock);
|
2020-03-30 23:06:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-02-03 19:39:11 +01:00
|
|
|
ret = pca9555_setbit(pca, PCA9555_REG_OUTPUT, pin, value);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&pca->lock);
|
2016-02-03 19:39:11 +01:00
|
|
|
return ret;
|
2015-07-09 15:31:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pca9555_readpin
|
|
|
|
*
|
|
|
|
* Description:
|
2020-03-30 23:06:15 +02:00
|
|
|
* Read the actual PIN level. This can be different from the last value
|
|
|
|
* written to this pin. Required.
|
2016-07-31 23:43:36 +02:00
|
|
|
*
|
|
|
|
* 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
|
2020-03-30 23:06:15 +02:00
|
|
|
* if the pin is high, except if OPTION_INVERT has been set on
|
|
|
|
* this pin.
|
2016-07-31 23:43:36 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success, else a negative error code
|
2015-07-09 15:31:22 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pca9555_readpin(FAR struct ioexpander_dev_s *dev, uint8_t pin,
|
2015-07-10 18:33:07 +02:00
|
|
|
FAR bool *value)
|
2015-07-09 15:31:22 +02:00
|
|
|
{
|
2016-02-03 19:39:11 +01:00
|
|
|
FAR struct pca9555_dev_s *pca = (FAR struct pca9555_dev_s *)dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get exclusive access to the PCA555 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&pca->lock);
|
2020-03-30 23:06:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-02-03 19:39:11 +01:00
|
|
|
ret = pca9555_getbit(pca, PCA9555_REG_INPUT, pin, value);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&pca->lock);
|
2016-02-03 19:39:11 +01:00
|
|
|
return ret;
|
2015-07-09 15:31:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pca9555_readbuf
|
|
|
|
*
|
|
|
|
* Description:
|
2016-07-31 23:43:36 +02:00
|
|
|
* Read the buffered pin level.
|
|
|
|
* This can be different from the actual pin state. Required.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - Device-specific state data
|
|
|
|
* pin - The index of the pin
|
|
|
|
* valptr - Pointer to a buffer where the level is stored.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success, else a negative error code
|
2015-07-09 15:31:22 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pca9555_readbuf(FAR struct ioexpander_dev_s *dev, uint8_t pin,
|
2015-07-10 18:33:07 +02:00
|
|
|
FAR bool *value)
|
2015-07-09 15:31:22 +02:00
|
|
|
{
|
2016-02-03 19:39:11 +01:00
|
|
|
FAR struct pca9555_dev_s *pca = (FAR struct pca9555_dev_s *)dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get exclusive access to the PCA555 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&pca->lock);
|
2020-03-30 23:06:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-02-03 19:39:11 +01:00
|
|
|
ret = pca9555_getbit(pca, PCA9555_REG_OUTPUT, pin, value);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&pca->lock);
|
2016-02-03 19:39:11 +01:00
|
|
|
return ret;
|
2015-07-09 15:31:22 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 18:33:07 +02:00
|
|
|
#ifdef CONFIG_IOEXPANDER_MULTIPIN
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pca9555_getmultibits
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read multiple bits from PCA9555 registers.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-02-03 19:39:11 +01:00
|
|
|
static int pca9555_getmultibits(FAR struct pca9555_dev_s *pca, uint8_t addr,
|
2023-06-13 17:23:58 +02:00
|
|
|
FAR const uint8_t *pins, FAR bool *values,
|
2015-07-10 18:33:07 +02:00
|
|
|
int count)
|
|
|
|
{
|
|
|
|
uint8_t buf[2];
|
|
|
|
int ret = OK;
|
|
|
|
int i;
|
|
|
|
int index;
|
|
|
|
int pin;
|
|
|
|
|
2016-01-26 14:59:36 +01:00
|
|
|
ret = pca9555_writeread(pca, &addr, 1, buf, 2);
|
2015-07-10 18:33:07 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-08-01 19:10:11 +02:00
|
|
|
#ifdef CONFIG_PCA9555_SHADOW_MODE
|
2016-06-27 16:11:54 +02:00
|
|
|
/* Save the new register value in the shadow register */
|
|
|
|
|
2020-03-30 23:06:15 +02:00
|
|
|
pca->sreg[addr] = buf[0];
|
|
|
|
pca->sreg[addr + 1] = buf[1];
|
2016-06-27 16:11:54 +02:00
|
|
|
#endif
|
|
|
|
|
2015-07-10 18:33:07 +02:00
|
|
|
/* Read the requested bits */
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
index = 0;
|
|
|
|
pin = pins[i];
|
|
|
|
if (pin > 15)
|
|
|
|
{
|
|
|
|
return -ENXIO;
|
|
|
|
}
|
2016-04-20 17:59:16 +02:00
|
|
|
else if (pin > 7)
|
2015-07-10 18:33:07 +02:00
|
|
|
{
|
|
|
|
index = 1;
|
|
|
|
pin -= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
values[i] = (buf[index] >> pin) & 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2016-01-22 14:08:59 +01:00
|
|
|
* Name: pca9555_multiwritepin
|
2015-07-10 18:33:07 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2016-07-31 23:43:36 +02:00
|
|
|
* 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
|
2015-07-10 18:33:07 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-01-22 14:08:59 +01:00
|
|
|
static int pca9555_multiwritepin(FAR struct ioexpander_dev_s *dev,
|
2023-06-13 17:23:58 +02:00
|
|
|
FAR const uint8_t *pins, FAR bool *values,
|
2016-01-22 14:08:59 +01:00
|
|
|
int count)
|
2015-07-10 18:33:07 +02:00
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
FAR struct pca9555_dev_s *pca = (FAR struct pca9555_dev_s *)dev;
|
2015-07-10 18:33:07 +02:00
|
|
|
uint8_t addr = PCA9555_REG_OUTPUT;
|
|
|
|
uint8_t buf[3];
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
int index;
|
|
|
|
int pin;
|
|
|
|
|
2016-02-03 19:39:11 +01:00
|
|
|
/* Get exclusive access to the PCA555 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&pca->lock);
|
2020-03-30 23:06:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2016-02-03 19:39:11 +01:00
|
|
|
|
2015-07-10 18:33:07 +02:00
|
|
|
/* Start by reading both registers, whatever the pins to change. We could
|
|
|
|
* attempt to read one port only if all pins were on the same port, but
|
2016-06-27 16:11:54 +02:00
|
|
|
* this would not save much.
|
|
|
|
*/
|
2015-07-10 18:33:07 +02:00
|
|
|
|
2016-08-01 19:10:11 +02:00
|
|
|
#ifndef CONFIG_PCA9555_SHADOW_MODE
|
2016-01-26 14:59:36 +01:00
|
|
|
ret = pca9555_writeread(pca, &addr, 1, &buf[1], 2);
|
2015-07-10 18:33:07 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&pca->lock);
|
2015-07-10 18:33:07 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2016-06-27 16:11:54 +02:00
|
|
|
#else
|
|
|
|
/* In Shadow-Mode we "read" the pin status from the shadow registers */
|
|
|
|
|
|
|
|
buf[1] = pca->sreg[addr];
|
2020-03-30 23:06:15 +02:00
|
|
|
buf[2] = pca->sreg[addr + 1];
|
2016-06-27 16:11:54 +02:00
|
|
|
#endif
|
2015-07-10 18:33:07 +02:00
|
|
|
|
|
|
|
/* Apply the user defined changes */
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
index = 1;
|
|
|
|
pin = pins[i];
|
|
|
|
if (pin > 15)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&pca->lock);
|
2015-07-10 18:33:07 +02:00
|
|
|
return -ENXIO;
|
|
|
|
}
|
2016-04-20 17:59:16 +02:00
|
|
|
else if (pin > 7)
|
2015-07-10 18:33:07 +02:00
|
|
|
{
|
|
|
|
index = 2;
|
|
|
|
pin -= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (values[i])
|
|
|
|
{
|
|
|
|
buf[index] |= (1 << pin);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf[index] &= ~(1 << pin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now write back the new pins states */
|
|
|
|
|
|
|
|
buf[0] = addr;
|
2016-08-01 19:10:11 +02:00
|
|
|
#ifdef CONFIG_PCA9555_SHADOW_MODE
|
2016-06-27 16:11:54 +02:00
|
|
|
/* Save the new register values in the shadow register */
|
2020-03-30 23:06:15 +02:00
|
|
|
|
|
|
|
pca->sreg[addr] = buf[1];
|
|
|
|
pca->sreg[addr + 1] = buf[2];
|
2016-06-27 16:11:54 +02:00
|
|
|
#endif
|
2016-02-03 19:39:11 +01:00
|
|
|
ret = pca9555_write(pca, buf, 3);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&pca->lock);
|
2016-02-03 19:39:11 +01:00
|
|
|
return ret;
|
2015-07-10 18:33:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pca9555_multireadpin
|
|
|
|
*
|
|
|
|
* Description:
|
2016-07-31 23:43:36 +02:00
|
|
|
* 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
|
2015-07-10 18:33:07 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pca9555_multireadpin(FAR struct ioexpander_dev_s *dev,
|
2023-06-13 17:23:58 +02:00
|
|
|
FAR const uint8_t *pins, FAR bool *values,
|
2015-07-10 18:33:07 +02:00
|
|
|
int count)
|
|
|
|
{
|
2016-02-03 19:39:11 +01:00
|
|
|
FAR struct pca9555_dev_s *pca = (FAR struct pca9555_dev_s *)dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get exclusive access to the PCA555 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&pca->lock);
|
2020-03-30 23:06:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-02-03 19:39:11 +01:00
|
|
|
ret = pca9555_getmultibits(pca, PCA9555_REG_INPUT,
|
|
|
|
pins, values, count);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&pca->lock);
|
2016-02-03 19:39:11 +01:00
|
|
|
return ret;
|
2015-07-10 18:33:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pca9555_multireadbuf
|
|
|
|
*
|
|
|
|
* Description:
|
2020-03-30 23:06:15 +02:00
|
|
|
* Read the buffered level of multiple pins. This routine may be faster
|
|
|
|
* than individual pin accesses. Optional.
|
2016-07-31 23:43:36 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - Device-specific state data
|
|
|
|
* pin - The index of the pin
|
|
|
|
* valptr - Pointer to a buffer where the buffered levels are stored.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success, else a negative error code
|
2015-07-10 18:33:07 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pca9555_multireadbuf(FAR struct ioexpander_dev_s *dev,
|
2023-06-13 17:23:58 +02:00
|
|
|
FAR const uint8_t *pins, FAR bool *values,
|
2015-07-10 18:33:07 +02:00
|
|
|
int count)
|
|
|
|
{
|
2016-02-03 19:39:11 +01:00
|
|
|
FAR struct pca9555_dev_s *pca = (FAR struct pca9555_dev_s *)dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get exclusive access to the PCA555 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&pca->lock);
|
2020-03-30 23:06:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-02-03 19:39:11 +01:00
|
|
|
ret = pca9555_getmultibits(pca, PCA9555_REG_OUTPUT,
|
|
|
|
pins, values, count);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&pca->lock);
|
2016-02-03 19:39:11 +01:00
|
|
|
return ret;
|
2015-07-10 18:33:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-11-17 14:09:43 +01:00
|
|
|
#ifdef CONFIG_PCA9555_INT_ENABLE
|
2015-07-09 15:31:22 +02:00
|
|
|
|
2016-07-31 19:09:02 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pca9555_attach
|
|
|
|
*
|
|
|
|
* Description:
|
2016-08-01 15:26:04 +02:00
|
|
|
* Attach and enable a pin interrupt callback function.
|
2016-07-31 19:09:02 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2016-08-01 15:26:04 +02:00
|
|
|
* arg - User-provided callback argument
|
2016-07-31 19:09:02 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2016-08-01 15:26:04 +02:00
|
|
|
* A non-NULL handle value is returned on success. This handle may be
|
|
|
|
* used later to detach and disable the pin interrupt.
|
2016-07-31 19:09:02 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-08-01 15:26:04 +02:00
|
|
|
static FAR void *pca9555_attach(FAR struct ioexpander_dev_s *dev,
|
|
|
|
ioe_pinset_t pinset, ioe_callback_t callback,
|
|
|
|
FAR void *arg)
|
2016-07-31 19:09:02 +02:00
|
|
|
{
|
|
|
|
FAR struct pca9555_dev_s *pca = (FAR struct pca9555_dev_s *)dev;
|
2016-08-01 15:26:04 +02:00
|
|
|
FAR void *handle = NULL;
|
2016-07-31 19:09:02 +02:00
|
|
|
int i;
|
2020-03-30 23:06:15 +02:00
|
|
|
int ret;
|
2016-07-31 19:09:02 +02:00
|
|
|
|
|
|
|
/* Get exclusive access to the PCA555 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&pca->lock);
|
2020-03-30 23:06:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-06-13 17:23:58 +02:00
|
|
|
return NULL;
|
2020-03-30 23:06:15 +02:00
|
|
|
}
|
2016-07-31 19:09:02 +02:00
|
|
|
|
|
|
|
/* Find and available in entry in the callback table */
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_PCA9555_INT_NCALLBACKS; i++)
|
|
|
|
{
|
2020-03-30 23:06:15 +02:00
|
|
|
/* Is this entry available (i.e., no callback attached) */
|
|
|
|
|
|
|
|
if (pca->cb[i].cbfunc == NULL)
|
|
|
|
{
|
|
|
|
/* Yes.. use this entry */
|
|
|
|
|
|
|
|
pca->cb[i].pinset = pinset;
|
|
|
|
pca->cb[i].cbfunc = callback;
|
|
|
|
pca->cb[i].cbarg = arg;
|
|
|
|
handle = &pca->cb[i];
|
|
|
|
break;
|
|
|
|
}
|
2016-07-31 19:09:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add this callback to the table */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&pca->lock);
|
2016-08-01 15:26:04 +02:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pca9555_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 pca9555_attch()
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success, else a negative error code
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pca9555_detach(FAR struct ioexpander_dev_s *dev, FAR void *handle)
|
|
|
|
{
|
|
|
|
FAR struct pca9555_dev_s *pca = (FAR struct pca9555_dev_s *)dev;
|
2020-03-30 23:06:15 +02:00
|
|
|
FAR struct pca9555_callback_s *cb =
|
|
|
|
(FAR struct pca9555_callback_s *)handle;
|
2016-08-01 15:26:04 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(pca != NULL && cb != NULL);
|
|
|
|
DEBUGASSERT((uintptr_t)cb >= (uintptr_t)&pca->cb[0] &&
|
2020-03-30 23:06:15 +02:00
|
|
|
(uintptr_t)cb <=
|
2021-05-25 16:01:27 +02:00
|
|
|
(uintptr_t)&pca->cb[CONFIG_PCA9555_INT_NCALLBACKS - 1]);
|
2016-08-01 15:26:04 +02:00
|
|
|
UNUSED(pca);
|
|
|
|
|
|
|
|
cb->pinset = 0;
|
|
|
|
cb->cbfunc = NULL;
|
|
|
|
cb->cbarg = NULL;
|
|
|
|
return OK;
|
2016-07-31 19:09:02 +02:00
|
|
|
}
|
|
|
|
|
2015-07-09 15:31:22 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pca9555_irqworker
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle GPIO interrupt events (this function actually executes in the
|
|
|
|
* context of the worker thread).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-11-13 16:28:46 +01:00
|
|
|
static void pca9555_irqworker(void *arg)
|
2015-07-09 15:31:22 +02:00
|
|
|
{
|
2016-04-20 17:59:16 +02:00
|
|
|
FAR struct pca9555_dev_s *pca = (FAR struct pca9555_dev_s *)arg;
|
2015-11-13 16:28:46 +01:00
|
|
|
uint8_t addr = PCA9555_REG_INPUT;
|
|
|
|
uint8_t buf[2];
|
2016-07-31 19:09:02 +02:00
|
|
|
ioe_pinset_t pinset;
|
2016-04-20 17:59:16 +02:00
|
|
|
int ret;
|
2016-07-31 19:09:02 +02:00
|
|
|
int i;
|
2015-11-18 00:38:46 +01:00
|
|
|
|
|
|
|
/* Read inputs */
|
2015-07-09 15:31:22 +02:00
|
|
|
|
2016-01-26 14:59:36 +01:00
|
|
|
ret = pca9555_writeread(pca, &addr, 1, buf, 2);
|
2016-04-20 17:59:16 +02:00
|
|
|
if (ret == OK)
|
2015-11-17 14:09:43 +01:00
|
|
|
{
|
2016-08-01 19:10:11 +02:00
|
|
|
#ifdef CONFIG_PCA9555_SHADOW_MODE
|
2016-06-27 16:11:54 +02:00
|
|
|
/* Don't forget to update the shadow registers at this point */
|
|
|
|
|
2020-03-30 23:06:15 +02:00
|
|
|
pca->sreg[addr] = buf[0];
|
|
|
|
pca->sreg[addr + 1] = buf[1];
|
2016-06-27 16:11:54 +02:00
|
|
|
#endif
|
2016-07-31 19:09:02 +02:00
|
|
|
/* Create a 16-bit pinset */
|
|
|
|
|
|
|
|
pinset = ((unsigned int)buf[0] << 8) | buf[1];
|
2015-11-17 14:09:43 +01:00
|
|
|
|
2016-07-31 19:09:02 +02:00
|
|
|
/* Perform pin interrupt callbacks */
|
2015-11-17 14:09:43 +01:00
|
|
|
|
2016-07-31 19:09:02 +02:00
|
|
|
for (i = 0; i < CONFIG_PCA9555_INT_NCALLBACKS; i++)
|
2016-04-20 17:59:16 +02:00
|
|
|
{
|
2016-07-31 19:09:02 +02:00
|
|
|
/* Is this entry valid (i.e., callback attached)? If so, did
|
|
|
|
* any of the requested pin interrupts occur?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (pca->cb[i].cbfunc != NULL)
|
|
|
|
{
|
|
|
|
/* Did any of the requested pin interrupts occur? */
|
|
|
|
|
|
|
|
ioe_pinset_t match = pinset & pca->cb[i].pinset;
|
|
|
|
if (match != 0)
|
|
|
|
{
|
|
|
|
/* Yes.. perform the callback */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
pca->cb[i].cbfunc(&pca->dev, match,
|
|
|
|
pca->cb[i].cbarg);
|
2016-07-31 19:09:02 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-20 17:59:16 +02:00
|
|
|
}
|
2015-11-17 14:09:43 +01:00
|
|
|
}
|
2015-11-18 00:38:46 +01:00
|
|
|
|
2016-04-20 17:59:16 +02:00
|
|
|
/* Re-enable interrupts */
|
2015-11-18 00:38:46 +01:00
|
|
|
|
2015-11-17 14:09:43 +01:00
|
|
|
pca->config->enable(pca->config, TRUE);
|
2015-11-13 16:28:46 +01:00
|
|
|
}
|
2015-07-09 15:31:22 +02:00
|
|
|
|
2015-11-17 14:09:43 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pca9555_interrupt
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle GPIO interrupt events (this function executes in the
|
|
|
|
* context of the interrupt).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-10-19 16:10:40 +02:00
|
|
|
static int pca9555_interrupt(int irq, FAR void *context, FAR void *arg)
|
2015-11-13 16:28:46 +01:00
|
|
|
{
|
2020-03-30 23:06:15 +02:00
|
|
|
FAR struct pca9555_dev_s *pca = (FAR struct pca9555_dev_s *)arg;
|
2015-07-09 15:31:22 +02:00
|
|
|
|
2015-11-13 16:28:46 +01:00
|
|
|
/* In complex environments, we cannot do I2C transfers from the interrupt
|
|
|
|
* handler because semaphores are probably used to lock the I2C bus. In
|
|
|
|
* this case, we will defer processing to the worker thread. This is also
|
|
|
|
* much kinder in the use of system resources and is, therefore, probably
|
|
|
|
* a good thing to do in any event.
|
|
|
|
*/
|
2015-07-09 15:31:22 +02:00
|
|
|
|
2015-11-13 16:28:46 +01:00
|
|
|
/* Notice that further GPIO interrupts are disabled until the work is
|
|
|
|
* actually performed. This is to prevent overrun of the worker thread.
|
2016-04-20 17:59:16 +02:00
|
|
|
* Interrupts are re-enabled in pca9555_irqworker() when the work is
|
|
|
|
* completed.
|
2015-11-13 16:28:46 +01:00
|
|
|
*/
|
|
|
|
|
2016-07-31 19:09:02 +02:00
|
|
|
if (work_available(&pca->work))
|
2016-04-20 17:59:16 +02:00
|
|
|
{
|
|
|
|
pca->config->enable(pca->config, FALSE);
|
2016-07-31 19:09:02 +02:00
|
|
|
work_queue(HPWORK, &pca->work, pca9555_irqworker,
|
2016-04-20 17:59:16 +02:00
|
|
|
(FAR void *)pca, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
2015-07-09 15:31:22 +02:00
|
|
|
}
|
2015-11-13 16:28:46 +01:00
|
|
|
|
2015-07-09 15:31:22 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pca9555_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize a PCA9555 I2C device.
|
|
|
|
*
|
|
|
|
* TODO: Actually support more than one device.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-30 23:06:15 +02:00
|
|
|
FAR struct ioexpander_dev_s *pca9555_initialize(
|
|
|
|
FAR struct i2c_master_s *i2cdev,
|
|
|
|
FAR struct pca9555_config_s *config)
|
2015-07-09 15:31:22 +02:00
|
|
|
{
|
|
|
|
FAR struct pca9555_dev_s *pcadev;
|
|
|
|
|
|
|
|
DEBUGASSERT(i2cdev != NULL && config != NULL);
|
|
|
|
|
|
|
|
#ifdef CONFIG_PCA9555_MULTIPLE
|
|
|
|
/* Allocate the device state structure */
|
|
|
|
|
2020-03-30 23:06:15 +02:00
|
|
|
pcadev = (FAR struct pca9555_dev_s *)
|
|
|
|
kmm_zalloc(sizeof(struct pca9555_dev_s));
|
2015-07-09 15:31:22 +02:00
|
|
|
if (!pcadev)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And save the device structure in the list of PCA9555 so that we can
|
|
|
|
* find it later.
|
|
|
|
*/
|
|
|
|
|
|
|
|
pcadev->flink = g_pca9555list;
|
|
|
|
g_pca9555list = pcadev;
|
|
|
|
|
|
|
|
#else
|
|
|
|
/* Use the one-and-only PCA9555 driver instance */
|
|
|
|
|
|
|
|
pcadev = &g_pca9555;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Initialize the device state structure */
|
|
|
|
|
|
|
|
pcadev->i2c = i2cdev;
|
|
|
|
pcadev->dev.ops = &g_pca9555_ops;
|
2015-11-13 16:28:46 +01:00
|
|
|
pcadev->config = config;
|
2015-07-09 15:31:22 +02:00
|
|
|
|
2015-11-17 14:09:43 +01:00
|
|
|
#ifdef CONFIG_PCA9555_INT_ENABLE
|
2017-10-19 16:10:40 +02:00
|
|
|
pcadev->config->attach(pcadev->config, pca9555_interrupt, pcadev);
|
2015-11-13 16:28:46 +01:00
|
|
|
pcadev->config->enable(pcadev->config, TRUE);
|
|
|
|
#endif
|
2016-02-03 19:39:11 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&pcadev->lock);
|
2015-07-09 15:31:22 +02:00
|
|
|
return &pcadev->dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_IOEXPANDER_PCA9555 */
|