drivers/mpr121: Add support to MPR121 Capacitive Keypad

This driver adds support to MPR121 Capacitive Keypad usually found
at Aliexpress. The MPR121 is a touch sensor chip from Freescale/NXP
that supports up to 12 sensor electrodes and 1 proximity electrode.

Signed-off-by: Alan C. Assis <acassis@gmail.com>
This commit is contained in:
Alan Carvalho de Assis 2026-04-21 17:32:01 -03:00 committed by Alan C. Assis
parent 1446961c87
commit 90ab0a8ae1
6 changed files with 905 additions and 0 deletions

View file

@ -138,5 +138,9 @@ if(CONFIG_INPUT)
list(APPEND SRCS goldfish_events.c)
endif()
if(CONFIG_INPUT_MPR121_KEYPAD)
list(APPEND SRCS mpr121.c)
endif()
target_sources(drivers PRIVATE ${SRCS})
endif()

View file

@ -815,4 +815,20 @@ endif # INPUT_KMATRIX_I2C
endif # INPUT_KMATRIX
config INPUT_MPR121_KEYPAD
bool "MPR121 Touch Keypad Driver"
default n
select INPUT_KEYBOARD
---help---
Enable support for keypad matrix powered by MPR121.
if INPUT_MPR121_KEYPAD
config INPUT_MPR121_KEYPAD_BUFSIZE
int "Keyboard matrix buffer size"
default 64
---help---
Size of the keyboard event buffer for each open file descriptor.
endif # INPUT_MPR121_KEYPAD
endif # INPUT

View file

@ -138,6 +138,10 @@ ifeq ($(CONFIG_INPUT_GOLDFISH_EVENTS),y)
CSRCS += goldfish_events.c
endif
ifeq ($(CONFIG_INPUT_MPR121_KEYPAD),y)
CSRCS += mpr121.c
endif
# Include input device driver build support
DEPPATH += --dep-path input

572
drivers/input/mpr121.c Normal file
View file

@ -0,0 +1,572 @@
/****************************************************************************
* drivers/input/mpr121.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 <stdbool.h>
#include <stdio.h>
#include <assert.h>
#include <nuttx/debug.h>
#include <errno.h>
#include <poll.h>
#include <fcntl.h>
#include <nuttx/input/mpr121.h>
#include <nuttx/fs/fs.h>
#include <nuttx/clock.h>
#include <nuttx/kmalloc.h>
#include <nuttx/mutex.h>
#include <nuttx/wqueue.h>
#include <nuttx/semaphore.h>
#include <nuttx/i2c/i2c_master.h>
#include <nuttx/input/keyboard.h>
#include <nuttx/input/kbd_codec.h>
#include "mpr121.h"
/* This driver is for MPR121 Capacitive Keypad
* usually found at Aliexpress. This keypad has
* 12 keys numbered from 0 to 11, starting from
* 0 at bottom left and ending with 11 at top
* right:
*
* +---+---+---+
* | 3 | 7 | 11|
* +---+---+---+
* | 2 | 6 | 10|
* +---+---+---+
* | 1 | 5 | 9 |
* +---+---+---+
* | 0 | 4 | 8 |
* +---+---+---+
* o o o o o
* | | | | |
* | | | | +- VCC
* | | | +--- IRQ
* | | +----- SCL
* | +------- SDA
* +--------- GND
*
*/
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Types
****************************************************************************/
struct mpr121_dev_s
{
FAR const struct mpr121_config_s *config; /* Board configuration data */
mutex_t lock; /* Exclusive access to device */
struct work_s work; /* Work queue to process keys after IRQ */
/* Current and previous state of the matrix (bitfield) */
FAR uint8_t *state; /* Current state bitmap */
/* Keyboard lower-half registration */
struct keyboard_lowerhalf_s lower;
};
/****************************************************************************
* Private Functions
****************************************************************************/
/* Work queue */
static void mpr121_worker(FAR void *arg);
/****************************************************************************
* Name: mpr121_int_handler
*
* Description:
* Interrupt handler (ISR) for MPR121 IRQ pin.
*
****************************************************************************/
static int mpr121_int_handler(int irq, FAR void *context, FAR void *arg)
{
int ret;
FAR struct mpr121_dev_s *priv = (FAR struct mpr121_dev_s *)arg;
DEBUGASSERT(priv != NULL);
/* Transfer processing to the worker thread. Since MPR121 interrupts
* are disabled while the work is pending, no special action should be
* required to protect the work queue.
*/
DEBUGASSERT(priv->work.worker == NULL);
ret = work_queue(LPWORK, &priv->work, mpr121_worker, priv, 0);
if (ret != 0)
{
ierr("ERROR: Failed to queue work: %d\n", ret);
}
return OK;
}
/****************************************************************************
* Name: mpr121_getreg8
*
* Description:
* Read from an 8-bit mpr121 register
*
****************************************************************************/
static uint8_t mpr121_getreg8(FAR struct mpr121_dev_s *priv,
uint8_t regaddr)
{
struct i2c_msg_s msg[2];
uint8_t regval;
int ret;
/* Setup 8-bit mpr121 address write message */
msg[0].frequency = 400000; /* I2C frequency 400 KHz */
msg[0].addr = priv->config->i2c_addr; /* 7-bit address */
msg[0].flags = 0; /* Write transaction, START */
msg[0].buffer = &regaddr; /* Transfer from this addr */
msg[0].length = 1; /* Send 1 byte following the addr
* (no STOP) */
/* Set up the 8-bit mpr121 data read message */
msg[1].frequency = 400000; /* I2C frequency 400 KHz */
msg[1].addr = priv->config->i2c_addr; /* 7-bit address */
msg[1].flags = I2C_M_READ; /* Read transac., Re-START */
msg[1].buffer = &regval; /* Transfer to this address */
msg[1].length = 1; /* Recv 1 byte following the addr
* (then STOP) */
/* Perform the transfer */
ret = I2C_TRANSFER(priv->config->i2c_dev, msg, 2);
if (ret < 0)
{
ierr("ERROR: I2C_TRANSFER failed: %d\n", ret);
return 0;
}
#ifdef CONFIG_MPR121_KEYPAD_REGDEBUG
_err("%02x->%02x\n", regaddr, regval);
#endif
return regval;
}
/****************************************************************************
* Name: mpr121_putreg8
*
* Description:
* Write a value to an 8-bit mpr121 register
*
****************************************************************************/
static int mpr121_putreg8(FAR struct mpr121_dev_s *priv,
uint8_t regaddr, uint8_t regval)
{
/* 8-bit data read sequence:
*
* Start-I2C_Write_Address-MPR121_Reg_Address-MPR121_Write_Data-STOP
*/
struct i2c_msg_s msg;
uint8_t txbuffer[2];
int ret;
#ifdef CONFIG_MPR121_REGDEBUG
_err("%02x<-%02x\n", regaddr, regval);
#endif
/* Setup to the data to be transferred. Two bytes: The mpr121 register
* address followed by one byte of data.
*/
txbuffer[0] = regaddr;
txbuffer[1] = regval;
/* Setup 8-bit mpr121 address write message */
msg.frequency = 400000; /* I2C frequency 400 KHz */
msg.addr = priv->config->i2c_addr; /* 7-bit address */
msg.flags = 0; /* Write transaction, beginning with START */
msg.buffer = txbuffer; /* Transfer from this address */
msg.length = 2; /* Send two byte following the address
* (then STOP) */
/* Perform the transfer */
ret = I2C_TRANSFER(priv->config->i2c_dev, &msg, 1);
if (ret < 0)
{
ierr("ERROR: I2C_TRANSFER failed: %d\n", ret);
return -EIO;
}
return OK;
}
/****************************************************************************
* Name: mpr121_get_state
*
* Description:
* Get the current state of a key at position (key_idx)
*
****************************************************************************/
static bool mpr121_get_state(FAR struct mpr121_dev_s *priv,
uint8_t key_idx)
{
uint8_t byte_idx = key_idx / 8;
uint8_t bit_idx = key_idx % 8;
return (priv->state[byte_idx] >> bit_idx) & 1;
}
/****************************************************************************
* Name: mpr121_set_state
*
* Description:
* Set the current state of a key at position (key_idx)
*
****************************************************************************/
static void mpr121_set_state(FAR struct mpr121_dev_s *priv,
uint8_t key_idx, bool pressed)
{
uint8_t byte_idx = key_idx / 8;
uint8_t bit_idx = key_idx % 8;
if (pressed)
{
priv->state[byte_idx] |= (1 << bit_idx);
}
else
{
priv->state[byte_idx] &= ~(1 << bit_idx);
}
}
/****************************************************************************
* Name: mpr121_worker
*
* Description:
* The worker is executed every time the MPR121 issue an IRQ (goes low)
*
****************************************************************************/
static void mpr121_worker(FAR void *arg)
{
FAR struct mpr121_dev_s *priv = (FAR struct mpr121_dev_s *)arg;
uint32_t keycode;
uint16_t status;
uint8_t lo;
uint8_t hi;
int ret;
int i;
DEBUGASSERT(priv != NULL);
ret = nxmutex_lock(&priv->lock);
if (ret < 0)
{
return;
}
/* Read the status touched keys */
lo = mpr121_getreg8(priv, MPR121_ELE0_7_TS);
hi = mpr121_getreg8(priv, MPR121_ELE8_11_TS);
iinfo("Pressed: LO = 0x%02X | HI = 0x%02X", lo, hi);
/* Only lower 4 bits of hi byte are valid (ELE8..ELE11) */
status = (uint16_t)lo | ((uint16_t)(hi & 0x0f) << 8);
/* Sweep all the previous saved keys and generate event
* for each new key pressed or released
*/
for (i = 0; i < MPR121_NUMKEY; i++)
{
if (mpr121_get_state(priv, i) != (bool)(status & 1 << i))
{
/* Generate keyboard event */
keycode = priv->config->keymap[i];
keyboard_event(&priv->lower, (uint16_t)keycode,
(status & 1 << i) ? KEYBOARD_PRESS : KEYBOARD_RELEASE);
iinfo("Key [%d]: %s (code %lu)\n", i,
(status & 1 << i) ? "PRESS" : "RELEASE",
(unsigned long)keycode);
}
mpr121_set_state(priv, i, (bool)(status & 1 << i));
}
nxmutex_unlock(&priv->lock);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: mpr121_register
*
* Description:
* Configure and register a keyboard matrix device.
*
****************************************************************************/
int mpr121_register(FAR const struct mpr121_config_s *config,
FAR const char *devpath)
{
FAR struct mpr121_dev_s *priv;
int i;
int ret;
uint8_t state_size;
uint8_t regval;
/* Validate configuration */
DEBUGASSERT(config != NULL);
DEBUGASSERT(devpath != NULL);
DEBUGASSERT(config->keymap != NULL);
iinfo("Registering MPR121 Capacitive Keypad as %s\n", devpath);
/* Allocate driver instance */
priv = kmm_zalloc(sizeof(struct mpr121_dev_s));
if (!priv)
{
ierr("ERROR: kmm_zalloc(%zu) failed\n", sizeof(struct mpr121_dev_s));
return -ENOMEM;
}
/* Calculate bitmap sizes */
state_size = (MPR121_NUMKEY + 7) / 8;
/* Allocate state bitmap */
priv->state = kmm_zalloc(state_size);
if (!priv->state)
{
ierr("ERROR: Failed to allocate state bitmap\n");
kmm_free(priv);
return -ENOMEM;
}
/* Initialize device structure */
priv->config = config;
nxmutex_init(&priv->lock);
/* Soft Reset the MPR121 */
ret = mpr121_putreg8(priv, MPR121_SRST, 0x63);
if (ret < 0)
{
ierr("Failed to reset MPR121\n");
goto errout_with_priv;
}
/* Force Stop Mode, although it should be in this mode after reset */
ret = mpr121_putreg8(priv, MPR121_ECR, 0x00);
if (ret < 0)
{
ierr("Failed to force MPR121 into Stop Mode\n");
goto errout_with_priv;
}
/* Probe the device, AFE Conf1 = 0x10 and Conf2 = 0x24 */
regval = mpr121_getreg8(priv, MPR121_AFE1);
if (regval != 0x10)
{
ierr("Error MPR121_AFE1 = 0x%02X instead of 0x10\n", regval);
goto errout_with_priv;
}
regval = mpr121_getreg8(priv, MPR121_AFE2);
if (regval != 0x24)
{
ierr("Error MPR121_AFE2 = 0x%02X instead of 0x24\n", regval);
goto errout_with_priv;
}
/* Setup the Baseline Tracking: MHD / NHD / NCL / FDL (see AN3944) */
/* Rising */
ret = mpr121_putreg8(priv, MPR121_MHDR, 0x01);
ret |= mpr121_putreg8(priv, MPR121_NHDR, 0x01);
ret |= mpr121_putreg8(priv, MPR121_NCLR, 0x0e);
ret |= mpr121_putreg8(priv, MPR121_FDLR, 0x00);
/* Falling */
ret |= mpr121_putreg8(priv, MPR121_MHDF, 0x01);
ret |= mpr121_putreg8(priv, MPR121_NHDF, 0x05);
ret |= mpr121_putreg8(priv, MPR121_NCLF, 0x01);
ret |= mpr121_putreg8(priv, MPR121_FDLF, 0x00);
/* Touched */
ret |= mpr121_putreg8(priv, MPR121_NHDT, 0x00);
ret |= mpr121_putreg8(priv, MPR121_NCLT, 0x00);
ret |= mpr121_putreg8(priv, MPR121_FDLT, 0x00);
if (ret < 0)
{
ierr("Failed to setup the baseline tracking\n");
goto errout_with_priv;
}
/* Configure each electrode Touch / Release thresholds (all 12 channels)
*
* Touch threshold > Release threshold always, with hysteresis recommended
* at ~50 % of touch threshold.
*/
for (i = 0; i < MPR121_NUMKEY; i++)
{
ret = mpr121_putreg8(priv, MPR121_REG_TOUCH_THRESH(i),
MPR121_TOUCH_THRESHOLD);
if (ret < 0)
{
ierr("Failed to set the touch threshold\n");
goto errout_with_priv;
}
ret = mpr121_putreg8(priv, MPR121_REG_REL_THRESH(i),
MPR121_RELEASE_THRESHOLD);
if (ret < 0)
{
ierr("Failed to set the release threshold\n");
goto errout_with_priv;
}
}
/* Configure Debounce = 2 consecutive samples required for touch / release
*
* Bits [2:0] = touch debounce
* bits [6:4] = release debounce
*/
ret = mpr121_putreg8(priv, MPR121_DT_DR, 0x22);
if (ret < 0)
{
ierr("Failed to setup the debounce\n");
goto errout_with_priv;
}
/* Configure AFE (Analog Front End) & Filter configuration
*
* We are keeping the original values here:
* AFE1: FFI = 6 samples, CDC = 16 uA
* AFE2: CDT = 0.5 us, SFI = 4 samples, ESI = 1 ms
* Note: Increase ESI to reduce noise, but also reduces response time
*/
ret = mpr121_putreg8(priv, MPR121_AFE1, MPR121_AFE1_VALUE);
ret |= mpr121_putreg8(priv, MPR121_AFE2, MPR121_AFE2_VALUE);
if (ret < 0)
{
ierr("Failed to configure the Analog Front End and Filter\n");
goto errout_with_priv;
}
/* Auto-Configuration (see AN3890), optional but recommended!
*
* When we know the Vdd we can automatically tunes CDC/CDT per electrode:
*
* AUTOCONF0: ACE=1 (enable), ARE=1 (retry), BVA=11 (baseline=5MSB)
* FFI same as AFE1 bits [7:6] => 0x0B
* AUTOCONF1: ACFIE=0, ARFIE=0, OORIE=0, SCTS=0 => 0x00
*/
ret = mpr121_putreg8(priv, MPR121_AUTOCONF0, 0x0b);
ret |= mpr121_putreg8(priv, MPR121_AUTOCONF1, 0x00);
ret |= mpr121_putreg8(priv, MPR121_UPLIMIT, MPR121_UPLIMIT_VALUE);
ret |= mpr121_putreg8(priv, MPR121_LOWLIMIT, MPR121_LOWLIMIT_VALUE);
ret |= mpr121_putreg8(priv, MPR121_TARGETLIMIT, MPR121_TARGETLIMIT_VALUE);
if (ret < 0)
{
ierr("Failed to setup the Auto Configuration limits\n");
goto errout_with_priv;
}
/* Enter Run Mode writing the ECR register
*
* ECR:
* CL [7:6] = 10 => baseline loaded from 5 upper bits data
* ELEPROX_EN [5:4] = 00 => proximity disabled
* ELE_EN [3:0] = 1100 => ELE0..ELE11 (12 electrodes active)
*/
ret = mpr121_putreg8(priv, MPR121_ECR, MPR121_ECR_VALUE);
if (ret < 0)
{
ierr("Failed to enter in Run Mode\n");
goto errout_with_priv;
}
/* Register as keyboard device */
ret = keyboard_register(&priv->lower, devpath,
CONFIG_INPUT_MPR121_KEYPAD_BUFSIZE);
if (ret < 0)
{
ierr("ERROR: keyboard_register() failed: %d\n", ret);
goto errout_with_priv;
}
iinfo("MPR121 Capacitive Keypad registered as %s\n", devpath);
/* Attach to the interrupt */
priv->config->irq_attach(priv->config, mpr121_int_handler, priv);
return OK;
errout_with_priv:
nxmutex_destroy(&priv->lock);
kmm_free(priv->state);
kmm_free(priv);
return ret;
}

213
drivers/input/mpr121.h Normal file
View file

@ -0,0 +1,213 @@
/****************************************************************************
* drivers/input/mpr121.h
*
* 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.
*
****************************************************************************/
#ifndef __DRIVERS_INPUT_MPR121_H
#define __DRIVERS_INPUT_MPR121_H
/****************************************************************************
* Included Files
****************************************************************************/
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* MPR121 Address */
#define MPR121_ELE0_7_TS (0x00) /* Bits 0-7: E(n) Touch Status */
#define MPR121_ELE8_11_TS (0x01) /* Bits 0-7: E(n)TS, OVCF */
#define MPR121_ELE0_7_OOR (0x02) /* Bits 0-7: E(n)OOR status */
#define MPR121_ELE8_11_OOR (0x03) /* Bits 0-7: E(n)OOR, ACFF, ARFF */
#define MPR121_ELE0_FDL (0x04) /* Bits 0-7: ELE0 Filtered Data LSB */
#define MPR121_ELE0_FDM (0x05) /* Bits 0-1: ELE0 Filtered Data MSB */
#define MPR121_ELE1_FDL (0x06) /* Bits 0-7: ELE1 Filtered Data LSB */
#define MPR121_ELE1_FDM (0x07) /* Bits 0-1: ELE1 Filtered Data MSB */
#define MPR121_ELE2_FDL (0x08) /* Bits 0-7: ELE2 Filtered Data LSB */
#define MPR121_ELE2_FDM (0x09) /* Bits 0-1: ELE2 Filtered Data MSB */
#define MPR121_ELE3_FDL (0x0a) /* Bits 0-7: ELE3 Filtered Data LSB */
#define MPR121_ELE3_FDM (0x0b) /* Bits 0-1: ELE3 Filtered Data MSB */
#define MPR121_ELE4_FDL (0x0c) /* Bits 0-7: ELE4 Filtered Data LSB */
#define MPR121_ELE4_FDM (0x0d) /* Bits 0-1: ELE4 Filtered Data MSB */
#define MPR121_ELE5_FDL (0x0e) /* Bits 0-7: ELE5 Filtered Data LSB */
#define MPR121_ELE5_FDM (0x0f) /* Bits 0-1: ELE5 Filtered Data MSB */
#define MPR121_ELE6_FDL (0x10) /* Bits 0-7: ELE6 Filtered Data LSB */
#define MPR121_ELE6_FDM (0x11) /* Bits 0-1: ELE6 Filtered Data MSB */
#define MPR121_ELE7_FDL (0x12) /* Bits 0-7: ELE7 Filtered Data LSB */
#define MPR121_ELE7_FDM (0x13) /* Bits 0-1: ELE7 Filtered Data MSB */
#define MPR121_ELE8_FDL (0x14) /* Bits 0-7: ELE8 Filtered Data LSB */
#define MPR121_ELE8_FDM (0x15) /* Bits 0-1: ELE8 Filtered Data MSB */
#define MPR121_ELE9_FDL (0x16) /* Bits 0-7: ELE9 Filtered Data LSB */
#define MPR121_ELE9_FDM (0x17) /* Bits 0-1: ELE9 Filtered Data MSB */
#define MPR121_ELE10_FDL (0x18) /* Bits 0-7: ELE10 Filtered Data LSB */
#define MPR121_ELE10_FDM (0x19) /* Bits 0-1: ELE10 Filtered Data MSB */
#define MPR121_ELE11_FDL (0x1a) /* Bits 0-7: ELE11 Filtered Data LSB */
#define MPR121_ELE11_FDM (0x1b) /* Bits 0-1: ELE11 Filtered Data MSB */
#define MPR121_ELE12_FDL (0x1c) /* Bits 0-7: ELEPROX Filtered Data LSB */
#define MPR121_ELE12_FDM (0x1d) /* Bits 0-1: ELEPROX Filtered Data MSB */
#define MPR121_ELE0_BV (0x1e) /* Bits 0-7: ELE0 Baseline Value */
#define MPR121_ELE1_BV (0x1f) /* Bits 0-7: ELE1 Baseline Value */
#define MPR121_ELE2_BV (0x20) /* Bits 0-7: ELE2 Baseline Value */
#define MPR121_ELE3_BV (0x21) /* Bits 0-7: ELE3 Baseline Value */
#define MPR121_ELE4_BV (0x22) /* Bits 0-7: ELE4 Baseline Value */
#define MPR121_ELE5_BV (0x23) /* Bits 0-7: ELE5 Baseline Value */
#define MPR121_ELE6_BV (0x24) /* Bits 0-7: ELE6 Baseline Value */
#define MPR121_ELE7_BV (0x25) /* Bits 0-7: ELE7 Baseline Value */
#define MPR121_ELE8_BV (0x26) /* Bits 0-7: ELE8 Baseline Value */
#define MPR121_ELE9_BV (0x27) /* Bits 0-7: ELE9 Baseline Value */
#define MPR121_ELE10_BV (0x28) /* Bits 0-7: ELE10 Baseline Value */
#define MPR121_ELE11_BV (0x29) /* Bits 0-7: ELE11 Baseline Value */
#define MPR121_ELE12_BV (0x2a) /* Bits 0-7: ELEPROX Baseline Value */
#define MPR121_MHDR (0x2b) /* Bits 0-5: MHD Rising */
#define MPR121_NHDR (0x2c) /* Bits 0-5: NHD Rising */
#define MPR121_NCLR (0x2d) /* Bits 0-7: NCL Rising */
#define MPR121_FDLR (0x2e) /* Bits 0-7: FDL Rising */
#define MPR121_MHDF (0x2f) /* Bits 0-5: MHD Falling */
#define MPR121_NHDF (0x30) /* Bits 0-5: NHD Falling */
#define MPR121_NCLF (0x31) /* Bits 0-7: NCL Falling */
#define MPR121_FDLF (0x32) /* Bits 0-7: FDL Falling */
#define MPR121_NHDT (0x33) /* Bits 0-5: NHD Touched */
#define MPR121_NCLT (0x34) /* Bits 0-7: NCL Touched */
#define MPR121_FDLT (0x35) /* Bits 0-7: FDL Touched */
#define MPR121_MHDPROXR (0x36) /* Bits 0-5: ELEPROX MHD Rising */
#define MPR121_NHDPROXR (0x37) /* Bits 0-5: ELEPROX NHD Rising */
#define MPR121_NCLPROXR (0x38) /* Bits 0-7: ELEPROX NCL Rising */
#define MPR121_FDLPROXR (0x39) /* Bits 0-7: ELEPROX FDL Rising */
#define MPR121_MHDPROXF (0x3a) /* Bits 0-5: ELEPROX MHD Falling */
#define MPR121_NHDPROXF (0x3b) /* Bits 0-5: ELEPROX NHD Falling */
#define MPR121_NCLPROXF (0x3c) /* Bits 0-7: ELEPROX NCL Falling */
#define MPR121_FDLPROXF (0x3d) /* Bits 0-7: ELEPROX FDL Falling */
#define MPR121_NHDPROXT (0x3e) /* Bits 0-5: ELEPROX NHD Touched */
#define MPR121_NCLPROXT (0x3f) /* Bits 0-7: ELEPROX NCL Touched */
#define MPR121_FDLPROXT (0x40) /* Bits 0-7: ELEPROX FDL Touched */
#define MPR121_E0TTH (0x41) /* Bits 0-7: ELE0 Touch Threshold */
#define MPR121_E0RTH (0x42) /* Bits 0-7: ELE0 Release Threshold */
#define MPR121_E1TTH (0x43) /* Bits 0-7: ELE1 Touch Threshold */
#define MPR121_E1RTH (0x44) /* Bits 0-7: ELE1 Release Threshold */
#define MPR121_E2TTH (0x45) /* Bits 0-7: ELE2 Touch Threshold */
#define MPR121_E2RTH (0x46) /* Bits 0-7: ELE2 Release Threshold */
#define MPR121_E3TTH (0x47) /* Bits 0-7: ELE3 Touch Threshold */
#define MPR121_E3RTH (0x48) /* Bits 0-7: ELE3 Release Threshold */
#define MPR121_E4TTH (0x49) /* Bits 0-7: ELE4 Touch Threshold */
#define MPR121_E4RTH (0x4a) /* Bits 0-7: ELE4 Release Threshold */
#define MPR121_E5TTH (0x4b) /* Bits 0-7: ELE5 Touch Threshold */
#define MPR121_E5RTH (0x4c) /* Bits 0-7: ELE5 Release Threshold */
#define MPR121_E6TTH (0x4d) /* Bits 0-7: ELE6 Touch Threshold */
#define MPR121_E6RTH (0x4e) /* Bits 0-7: ELE6 Release Threshold */
#define MPR121_E7TTH (0x4f) /* Bits 0-7: ELE7 Touch Threshold */
#define MPR121_E7RTH (0x50) /* Bits 0-7: ELE7 Release Threshold */
#define MPR121_E8TTH (0x51) /* Bits 0-7: ELE8 Touch Threshold */
#define MPR121_E8RTH (0x52) /* Bits 0-7: ELE8 Release Threshold */
#define MPR121_E9TTH (0x53) /* Bits 0-7: ELE9 Touch Threshold */
#define MPR121_E9RTH (0x54) /* Bits 0-7: ELE9 Release Threshold */
#define MPR121_E10TTH (0x55) /* Bits 0-7: ELE10 Touch Threshold */
#define MPR121_E10RTH (0x56) /* Bits 0-7: ELE10 Release Threshold */
#define MPR121_E11TTH (0x57) /* Bits 0-7: ELE11 Touch Threshold */
#define MPR121_E11RTH (0x58) /* Bits 0-7: ELE11 Release Threshold */
#define MPR121_E12TTH (0x59) /* Bits 0-7: ELE12 Touch Threshold */
#define MPR121_E12RTH (0x5a) /* Bits 0-7: ELE12 Release Threshold */
#define MPR121_DT_DR (0x5b) /* Bits 0-2,4-6: Debounce Touch & Release */
#define MPR121_AFE1 (0x5c) /* Bits 0-5,6-7: CDC and FFI */
#define MPR121_AFE2 (0x5d) /* Bits 0-2,3-4,5-7: ESI, SFI, CDT */
#define MPR121_ECR (0x5e) /* Bits 0-4,4-5,6-7: ELE, ELEPROX, CL */
#define MPR121_CDC0 (0x5f) /* Bits 0-5: ELE0 Electrode Current */
#define MPR121_CDC1 (0x60) /* Bits 0-5: ELE1 Electrode Current */
#define MPR121_CDC2 (0x61) /* Bits 0-5: ELE2 Electrode Current */
#define MPR121_CDC3 (0x62) /* Bits 0-5: ELE3 Electrode Current */
#define MPR121_CDC4 (0x63) /* Bits 0-5: ELE4 Electrode Current */
#define MPR121_CDC5 (0x64) /* Bits 0-5: ELE5 Electrode Current */
#define MPR121_CDC6 (0x65) /* Bits 0-5: ELE6 Electrode Current */
#define MPR121_CDC7 (0x66) /* Bits 0-5: ELE7 Electrode Current */
#define MPR121_CDC8 (0x67) /* Bits 0-5: ELE8 Electrode Current */
#define MPR121_CDC9 (0x68) /* Bits 0-5: ELE9 Electrode Current */
#define MPR121_CDC10 (0x69) /* Bits 0-5: ELE10 Electrode Current */
#define MPR121_CDC11 (0x6a) /* Bits 0-5: ELE11 Electrode Current */
#define MPR121_CDC12 (0x6b) /* Bits 0-5: ELE12 Electrode Current */
#define MPR121_CDT0_CDT1 (0x6c) /* Bits 0-2,4-6: E0 & E1 Charge time */
#define MPR121_CDT2_CDT3 (0x6d) /* Bits 0-2,4-6: E2 & E3 Charge time */
#define MPR121_CDT4_CDT5 (0x6e) /* Bits 0-2,4-6: E4 & E5 Charge time */
#define MPR121_CDT6_CDT7 (0x6f) /* Bits 0-2,4-6: E6 & E7 Charge time */
#define MPR121_CDT8_CDT9 (0x70) /* Bits 0-2,4-6: E8 & E9 Charge time */
#define MPR121_CDT10_CDT11 (0x71) /* Bits 0-2,4-6: E10 & E11 Charge time */
#define MPR121_CDT12 (0x72) /* Bits 0-2: E12 Charge time */
#define MPR121_CTL0 (0x73) /* Bits 0-7: GPIO Control Register 0 */
#define MPR121_CTL1 (0x74) /* Bits 0-7: GPIO Control Register 0 */
#define MPR121_DAT (0x75) /* Bits 0-7: GPIO Data Register */
#define MPR121_DIR (0x76) /* Bits 0-7: GPIO Direction Control Register */
#define MPR121_EN (0x77) /* Bits 0-7: GPIO Enable Register */
#define MPR121_SET (0x78) /* Bits 0-7: GPIO Data Set Register */
#define MPR121_CLR (0x79) /* Bits 0-7: GPIO Data Clear Register */
#define MPR121_TOG (0x7a) /* Bits 0-7: GPIO Data Toggle Register */
#define MPR121_AUTOCONF0 (0x7b) /* Bits 0,1,2-3,4-5,6-7: ACE, ARE, BVA, RETRY, AFES */
#define MPR121_AUTOCONF1 (0x7c) /* Bits 0,1,3,7: ACFIE, ARFIE, OORIE, SCTS */
#define MPR121_UPLIMIT (0x7d) /* Bits 0-7: Auto Config USL Register */
#define MPR121_LOWLIMIT (0x7e) /* Bits 0-7: Auto Config LSL Register */
#define MPR121_TARGETLIMIT (0x7f) /* Bits 0-7: Auto Config Target Level Register */
#define MPR121_SRST (0x80) /* Bits 0-7: Soft Reset Register */
/* Factory Reserved Region (0x81~0xff) */
/* Tuning defaults for a standard 3.3 V keypad */
#define MPR121_TOUCH_THRESHOLD 12 /* Lower means more sensitive */
#define MPR121_RELEASE_THRESHOLD 6 /* Must be < touch threshold */
/* Touch / Release thresholds (0x41..0x64, 2 bytes each) */
#define MPR121_REG_TOUCH_THRESH(n) (0x41 + (n) * 2)
#define MPR121_REG_REL_THRESH(n) (0x42 + (n) * 2)
/* AFE1: FFI=6 samples (00), CDC=16uA charge (10000) => 0x10 */
#define MPR121_AFE1_VALUE 0x10
/* AFE2: CDT=0.5 us (001), SFI=4 samples (00), ESI=1 ms (001) => 0x24 */
#define MPR121_AFE2_VALUE 0x24
/* Auto-config: limit registers (Vdd = 3.3 V, target ~70 % of Vdd) */
#define MPR121_UPLIMIT_VALUE 200 /* (Vdd - 0.7) / Vdd * 256 */
#define MPR121_LOWLIMIT_VALUE 130 /* UPLIMIT * 0.65 */
#define MPR121_TARGETLIMIT_VALUE 180 /* UPLIMIT * 0.9 */
/* ECR: CL=10 (use 5 MSB of baseline), ELEPROX_EN=0, ELE_EN=1100 (ELE0-11) */
#define MPR121_ECR_VALUE 0xCC /* Run mode, all 12 electrodes */
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* __DRIVERS_INPUT_MPR121_H */

View file

@ -0,0 +1,96 @@
/****************************************************************************
* include/nuttx/input/mpr121.h
*
* 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.
*
****************************************************************************/
#ifndef __INCLUDE_NUTTX_INPUT_MPR121_H
#define __INCLUDE_NUTTX_INPUT_MPR121_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/input/keyboard.h>
#include <stdint.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Number of keys used by MPR121 */
#define MPR121_NUMKEY 12
/****************************************************************************
* Public Types
****************************************************************************/
typedef uint32_t mpr121_pin_t;
/* Keypad matrix configuration structure passed to mpr121_register() */
struct mpr121_config_s
{
CODE int (*irq_attach)(FAR const struct mpr121_config_s *state,
xcpt_t isr, FAR void *arg);
FAR struct i2c_master_s *i2c_dev;
uint8_t i2c_addr;
/* Keymap: keycode[MPR121_NUMKEY] to map the keys of MPR121 keypad */
FAR const uint32_t *keymap;
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef __cplusplus
extern "C"
{
#endif
/****************************************************************************
* Name: mpr121_register
*
* Description:
* Configure and register a keypad matrix device. This will create the
* /dev/keypadN device node and enable keyboard scanning.
*
* Input Parameters:
* config - The keyboard matrix configuration. This structure is not
* copied; it must persist for the lifetime of the driver.
* devpath - The device path for the /dev/keypadN device.
*
* Returned Value:
* Zero is returned on success. Otherwise, a negated errno value is
* returned to indicate the nature of the failure.
*
****************************************************************************/
int mpr121_register(FAR const struct mpr121_config_s *config,
FAR const char *devpath);
#ifdef __cplusplus
}
#endif
#endif /* __INCLUDE_NUTTX_INPUT_MPR121_H */