driver/ioexpander: adds driver for the PCA9538 I2C ioexpander

This commit is contained in:
Nicholas Chin
2020-02-19 10:09:52 -05:00
committed by Gregory Nutt
parent 5b750fdda0
commit 0f284c3025
5 changed files with 1224 additions and 0 deletions
+51
View File
@@ -65,6 +65,57 @@ config PCA9555_RETRY
endif # IOEXPANDER_PCA9555 endif # IOEXPANDER_PCA9555
config IOEXPANDER_PCA9538
bool "PCA9538 I2C IO expander"
default n
depends on I2C
---help---
Enable support for the NXP PCA9538 IO Expander
if IOEXPANDER_PCA9538
config PCA9538_MULTIPLE
bool "Multiple PCA9538 Devices"
default n
---help---
Can be defined to support multiple PCA9538 devices on board.
config PCA9538_INT_ENABLE
bool "Enable PCA9538 Interrupt Support"
default n
select IOEXPANDER_INT_ENABLE
---help---
Enable driver interrupt functionality
config PCA9538_INT_NCALLBACKS
int "Max number of interrupt callbacks"
default 4
depends on PCA9538_INT_ENABLE
---help---
This is the maximum number of interrupt callbacks supported
config PCA9538_SHADOW_MODE
bool "Use Shadow Mode instead of Read-Modify-Write Operations"
default n
---help---
This setting enables a mode where the output and pin
configuration registers are held in RAM.
With this for example we do not need to read back the
output-register every time we want to change one pin.
We do instead change the bit in the internal register
and then just write this register to the IO-Expander.
This reduces bus traffic and eliminates the problem of
EMC-caused toggling of output pins.
config PCA9538_RETRY
bool "Retry to send commands and data at I2C communication errors"
default n
---help---
Retry to send commands and data if a I2C-communication
error occurs (eg. caused by EMC).
endif # IOEXPANDER_PCA9538
config IOEXPANDER_TCA64XX config IOEXPANDER_TCA64XX
bool "TCA64XX I2C IO expander" bool "TCA64XX I2C IO expander"
default n default n
+4
View File
@@ -44,6 +44,10 @@ ifeq ($(CONFIG_IOEXPANDER_PCA9555),y)
CSRCS += pca9555.c CSRCS += pca9555.c
endif endif
ifeq ($(CONFIG_IOEXPANDER_PCA9538),y)
CSRCS += pca9538.c
endif
ifeq ($(CONFIG_IOEXPANDER_TCA64XX),y) ifeq ($(CONFIG_IOEXPANDER_TCA64XX),y)
CSRCS += tca64xx.c CSRCS += tca64xx.c
endif endif
File diff suppressed because it is too large Load Diff
+139
View File
@@ -0,0 +1,139 @@
/****************************************************************************
* drivers/ioexpander/pca9538.h
*
* 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.
*
****************************************************************************/
#ifndef __DRIVERS_IOEXPANDER_PCA9538_H
#define __DRIVERS_IOEXPANDER_PCA9538_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/wdog.h>
#include <nuttx/clock.h>
#include <nuttx/semaphore.h>
#include <nuttx/wqueue.h>
#include <nuttx/ioexpander/ioexpander.h>
#include <nuttx/ioexpander/pca9538.h>
#include <nuttx/i2c/i2c_master.h>
#include <nuttx/irq.h>
#if defined(CONFIG_IOEXPANDER) && defined(CONFIG_IOEXPANDER_PCA9538)
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* Prerequisites:
* CONFIG_I2C
* I2C support is required
* CONFIG_IOEXPANDER
* Enables support for the PCA9538 I/O expander
*
* CONFIG_IOEXPANDER_PCA9538
* Enables support for the PCA9538 driver (Needs CONFIG_INPUT)
* CONFIG_PCA9538_MULTIPLE
* Can be defined to support multiple PCA9538 devices on board.
* CONFIG_PCA9538_INT_NCALLBACKS
* Maximum number of supported pin interrupt callbacks.
*/
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
# ifndef CONFIG_PCA9538_INT_NCALLBACKS
# define CONFIG_PCA9538_INT_NCALLBACKS 4
# endif
#endif
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
# ifndef CONFIG_SCHED_WORKQUEUE
# error Work queue support required. CONFIG_SCHED_WORKQUEUE must be selected.
# endif
#endif
#undef CONFIG_PCA9538_REFCNT
/* PCA9538 Resources ********************************************************/
#define PCA9538_GPIO_NPINS 8
#ifndef CONFIG_I2C
#error "CONFIG_I2C is required by pca9538"
#endif
#define PCA9538_MAXDEVS 4
/* I2C frequency */
#define PCA9538_I2C_MAXFREQUENCY 400000 /* 400KHz */
/* PCA9538 Registers ********************************************************/
#define PCA9538_REG_INPUT 0x00
#define PCA9538_REG_OUTPUT 0x01
#define PCA9538_REG_POLINV 0x02
#define PCA9538_REG_CONFIG 0x03
/****************************************************************************
* Public Types
****************************************************************************/
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
/* This type represents on registered pin interrupt callback */
struct pca9538_callback_s
{
ioe_pinset_t pinset; /* Set of pin interrupts that will generate
* the callback. */
ioe_callback_t cbfunc; /* The saved callback function pointer */
FAR void *cbarg; /* Callback argument */
};
#endif
/* This structure represents the state of the PCA9538 driver */
struct pca9538_dev_s
{
struct ioexpander_dev_s dev; /* Nested structure to allow casting
* as public gpio expander. */
#ifdef CONFIG_PCA9538_SHADOW_MODE
uint8_t sreg[8]; /* Shadowed registers of the PCA9538 */
#endif
#ifdef CONFIG_PCA9538_MULTIPLE
FAR struct pca9538_dev_s *flink; /* Supports a singly linked list of drivers */
#endif
FAR struct pca9538_config_s *config; /* Board configuration data */
FAR struct i2c_master_s *i2c; /* Saved I2C driver instance */
sem_t exclsem; /* Mutual exclusion */
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
struct work_s work; /* Supports the interrupt handling "bottom half" */
/* Saved callback information for each I/O expander client */
struct pca9538_callback_s cb[CONFIG_PCA9538_INT_NCALLBACKS];
#endif
};
#endif /* CONFIG_IOEXPANDER && CONFIG_IOEXPANDER_PCA9538 */
#endif /* __DRIVERS_IOEXPANDER_PCA9538_H */
+115
View File
@@ -0,0 +1,115 @@
/****************************************************************************
* include/nuttx/ioexpander/pca9538.h
*
* 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.
*
****************************************************************************/
#ifndef __INCLUDE_NUTTX_IOEXPANDER_PCA9538_H
#define __INCLUDE_NUTTX_IOEXPANDER_PCA9538_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/i2c/i2c_master.h>
#include <stdbool.h>
/****************************************************************************
* Public Types
****************************************************************************/
/* A reference to a structure of this type must be passed to the pca9538
* driver when the driver is instantiated. This structure provides
* information about the configuration of the pca9538 and provides some
* board-specific hooks.
*
* Memory for this structure is provided by the caller. It is not copied by
* the driver and is presumed to persist while the driver is active. The
* memory must be writeable because, under certain circumstances, the driver
* may modify the frequency.
*/
struct pca9538_config_s
{
/* Device characterization */
uint8_t address; /* 7-bit I2C address (only bits 0-6 used) */
uint32_t frequency; /* I2C or SPI frequency */
/* Sets the state of the PCA9538's nReset pin */
CODE void (*set_nreset_pin)(bool state);
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
/* If multiple pca9538 devices are supported, then an IRQ number must
* be provided for each so that their interrupts can be distinguished.
*/
/* IRQ/GPIO access callbacks. These operations all hidden behind
* callbacks to isolate the pca9538 driver from differences in GPIO
* interrupt handling by varying boards and MCUs.
*
* attach - Attach the pca9538 interrupt handler to the GPIO interrupt
* enable - Enable or disable the GPIO interrupt
*/
CODE int (*attach)(FAR struct pca9538_config_s *state, xcpt_t isr,
FAR void *arg);
CODE void (*enable)(FAR struct pca9538_config_s *state, bool enable);
#endif
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Name: pca9538_initialize
*
* Description:
* Instantiate and configure the pca9538 device driver to use the provided
* I2C device
* instance.
*
* Input Parameters:
* dev - 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 *pca9538_initialize(FAR struct i2c_master_s *dev,
FAR struct pca9538_config_s *config);
#ifdef __cplusplus
}
#endif
#endif /* __INCLUDE_NUTTX_IOEXPANDER_PCA9538_H */