Skip to content

Commit 71f832b

Browse files
committed
boards: Add support to MPR121 Capacitive Keypad
This commit adds support to MPR121 on stm32f4discovery board and a board config example. Signed-off-by: Alan C. Assis <acassis@gmail.com>
1 parent c7ee2d1 commit 71f832b

7 files changed

Lines changed: 351 additions & 0 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/****************************************************************************
2+
* boards/arm/stm32/common/include/stm32_mpr121.h
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
#ifndef __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_MPR121_H
24+
#define __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_MPR121_H
25+
26+
/****************************************************************************
27+
* Included Files
28+
****************************************************************************/
29+
30+
#include <nuttx/config.h>
31+
32+
/****************************************************************************
33+
* Pre-processor Definitions
34+
****************************************************************************/
35+
36+
/****************************************************************************
37+
* Public Types
38+
****************************************************************************/
39+
40+
/****************************************************************************
41+
* Public Data
42+
****************************************************************************/
43+
44+
#ifdef __cplusplus
45+
#define EXTERN extern "C"
46+
extern "C"
47+
{
48+
#else
49+
#define EXTERN extern
50+
#endif
51+
52+
/****************************************************************************
53+
* Inline Functions
54+
****************************************************************************/
55+
56+
/****************************************************************************
57+
* Public Function Prototypes
58+
****************************************************************************/
59+
60+
/****************************************************************************
61+
* Name: board_mpr121_initialize
62+
*
63+
* Description:
64+
* Initialize and register the MPR121 capacitive keypad.
65+
*
66+
* Input Parameters:
67+
* devno - The device number, used to build the device path as /dev/keypadN
68+
* busno - The I2C bus number
69+
*
70+
* Returned Value:
71+
* Zero (OK) on success; a negated errno value on failure.
72+
*
73+
****************************************************************************/
74+
75+
int board_mpr121_initialize(int devno, int busno);
76+
77+
#undef EXTERN
78+
#ifdef __cplusplus
79+
}
80+
#endif
81+
82+
#endif /* __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_MPR121_H */

boards/arm/stm32/common/src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,8 @@ if(CONFIG_INPUT_KMATRIX_I2C)
166166
list(APPEND SRCS stm32_kmatrix_i2c.c)
167167
endif()
168168

169+
if(CONFIG_INPUT_MPR121_KEYPAD)
170+
list(APPEND SRCS stm32_mpr121.c)
171+
endif()
172+
169173
target_sources(board PRIVATE ${SRCS})

boards/arm/stm32/common/src/Make.defs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ ifeq ($(CONFIG_SENSORS_APDS9960),y)
8686
CSRCS += stm32_apds9960.c
8787
endif
8888

89+
ifeq ($(CONFIG_INPUT_MPR121_KEYPAD),y)
90+
CSRCS += stm32_mpr121.c
91+
endif
92+
8993
ifeq ($(CONFIG_SENSORS_ZEROCROSS),y)
9094
CSRCS += stm32_zerocross.c
9195
endif
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/****************************************************************************
2+
* boards/arm/stm32/common/src/stm32_mpr121.c
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
/****************************************************************************
24+
* Included Files
25+
****************************************************************************/
26+
27+
#include <nuttx/config.h>
28+
29+
#include <errno.h>
30+
#include <nuttx/debug.h>
31+
#include <stdio.h>
32+
33+
#include <nuttx/input/mpr121.h>
34+
#include <arch/board/board.h>
35+
36+
#include "stm32.h"
37+
#include "stm32_i2c.h"
38+
39+
/****************************************************************************
40+
* Pre-processor Definitions
41+
****************************************************************************/
42+
43+
/* Device I2C Address of MPR121 Capacitive Keypad */
44+
45+
#define MPR121_I2C_ADDR 0x5a
46+
47+
/****************************************************************************
48+
* Public Functions
49+
****************************************************************************/
50+
51+
struct stm32_mpr121config_s
52+
{
53+
/* Configuration structure as seen by the MPR121 driver */
54+
55+
struct mpr121_config_s config;
56+
57+
/* Additional private definitions only known to this driver */
58+
59+
void *arg; /* Argument to pass to the interrupt handler */
60+
xcpt_t isr; /* ISR Handler */
61+
};
62+
63+
/****************************************************************************
64+
* Private Function Prototypes
65+
****************************************************************************/
66+
67+
static int mpr121_irq_attach(const struct mpr121_config_s *state,
68+
xcpt_t isr, void *arg);
69+
70+
/****************************************************************************
71+
* Private Data
72+
****************************************************************************/
73+
74+
/* Keymap for 4x3 MPR121 Capacitive Keypad
75+
* Keys named 11 and 10 were replaced with 'B' a 'A'
76+
*/
77+
78+
static const uint32_t g_mpr121_keymap[] =
79+
{
80+
'0', '1', '2', '3',
81+
'4', '5', '6', '7',
82+
'8', '9', 'A', 'B',
83+
};
84+
85+
/* A reference to a structure of this type must be passed to the MPR121
86+
* driver. This structure provides information about the configuration
87+
* of the MPR121 and provides some board-specific hooks.
88+
*
89+
* Memory for this structure is provided by the caller. It is not copied
90+
* by the driver and is presumed to persist while the driver is active. The
91+
* memory must be writable because, under certain circumstances, the driver
92+
* may modify frequency or other values.
93+
*/
94+
95+
static struct stm32_mpr121config_s g_mpr121config =
96+
{
97+
.config =
98+
{
99+
.irq_attach = mpr121_irq_attach,
100+
.keymap = g_mpr121_keymap,
101+
},
102+
};
103+
104+
/****************************************************************************
105+
* Private Functions
106+
****************************************************************************/
107+
108+
/* Attach the MPR121 interrupt handler to the GPIO interrupt */
109+
110+
static int mpr121_irq_attach(const struct mpr121_config_s *state,
111+
xcpt_t isr, void *arg)
112+
{
113+
irqstate_t flags;
114+
115+
iinfo("mpr121_irq_attach\n");
116+
117+
flags = enter_critical_section();
118+
119+
/* Setup interrupt for Falling Edge */
120+
121+
stm32_gpiosetevent(BOARD_MPR121_GPIO_INT, false, true, true, isr, arg);
122+
123+
leave_critical_section(flags);
124+
125+
return OK;
126+
}
127+
128+
/****************************************************************************
129+
* Public Functions
130+
****************************************************************************/
131+
132+
/****************************************************************************
133+
* Name: board_mpr121_initialize
134+
*
135+
* Description:
136+
* Initialize and register the MPR121 gesture sensor.
137+
*
138+
* Input Parameters:
139+
* devno - The device number, used to build the device path as /dev/gestN
140+
* busno - The I2C bus number
141+
*
142+
* Returned Value:
143+
* Zero (OK) on success; a negated errno value on failure.
144+
*
145+
****************************************************************************/
146+
147+
int board_mpr121_initialize(int devno, int busno)
148+
{
149+
struct i2c_master_s *i2c;
150+
char devpath[14];
151+
int ret;
152+
153+
iinfo("Initializing MPR121!\n");
154+
155+
/* Configure the GPIO interrupt */
156+
157+
stm32_configgpio(BOARD_MPR121_GPIO_INT);
158+
159+
/* Initialize I2C */
160+
161+
i2c = stm32_i2cbus_initialize(busno);
162+
if (i2c == NULL)
163+
{
164+
return -ENODEV;
165+
}
166+
167+
/* Save this i2c in the config */
168+
169+
g_mpr121config.config.i2c_dev = i2c;
170+
g_mpr121config.config.i2c_addr = MPR121_I2C_ADDR;
171+
172+
/* Then register the capacitive keypad */
173+
174+
snprintf(devpath, sizeof(devpath), "/dev/keypad%d", devno);
175+
ret = mpr121_register(&g_mpr121config.config, devpath);
176+
if (ret < 0)
177+
{
178+
ierr("ERROR: Failed registering APDS-9960!\n");
179+
}
180+
181+
return ret;
182+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#
2+
# This file is autogenerated: PLEASE DO NOT EDIT IT.
3+
#
4+
# You can use "make menuconfig" to make any modifications to the installed .config file.
5+
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
6+
# modifications.
7+
#
8+
# CONFIG_ARCH_FPU is not set
9+
# CONFIG_NSH_ARGCAT is not set
10+
# CONFIG_NSH_CMDOPT_HEXDUMP is not set
11+
CONFIG_ARCH="arm"
12+
CONFIG_ARCH_BOARD="stm32f4discovery"
13+
CONFIG_ARCH_BOARD_COMMON=y
14+
CONFIG_ARCH_BOARD_STM32F4_DISCOVERY=y
15+
CONFIG_ARCH_BUTTONS=y
16+
CONFIG_ARCH_CHIP="stm32"
17+
CONFIG_ARCH_CHIP_STM32=y
18+
CONFIG_ARCH_CHIP_STM32F407VG=y
19+
CONFIG_ARCH_STACKDUMP=y
20+
CONFIG_BOARD_LATE_INITIALIZE=y
21+
CONFIG_BOARD_LOOPSPERMSEC=16717
22+
CONFIG_BUILTIN=y
23+
CONFIG_DEBUG_FEATURES=y
24+
CONFIG_EXAMPLES_HELLO=y
25+
CONFIG_EXAMPLES_KEYBOARD=y
26+
CONFIG_EXAMPLES_KEYBOARD_DEVPATH="/dev/keypad0"
27+
CONFIG_FS_PROCFS=y
28+
CONFIG_HAVE_CXX=y
29+
CONFIG_HAVE_CXXINITIALIZE=y
30+
CONFIG_I2C=y
31+
CONFIG_INIT_ENTRYPOINT="nsh_main"
32+
CONFIG_INPUT=y
33+
CONFIG_INPUT_MPR121_KEYPAD=y
34+
CONFIG_INTELHEX_BINARY=y
35+
CONFIG_LINE_MAX=64
36+
CONFIG_MM_REGIONS=2
37+
CONFIG_NSH_BUILTIN_APPS=y
38+
CONFIG_NSH_FILEIOSIZE=512
39+
CONFIG_NSH_READLINE=y
40+
CONFIG_PREALLOC_TIMERS=4
41+
CONFIG_RAM_SIZE=114688
42+
CONFIG_RAM_START=0x20000000
43+
CONFIG_RAW_BINARY=y
44+
CONFIG_RR_INTERVAL=200
45+
CONFIG_SCHED_LPWORK=y
46+
CONFIG_SCHED_WAITPID=y
47+
CONFIG_START_DAY=6
48+
CONFIG_START_MONTH=12
49+
CONFIG_START_YEAR=2011
50+
CONFIG_STM32_I2C1=y
51+
CONFIG_STM32_JTAG_SW_ENABLE=y
52+
CONFIG_STM32_PWR=y
53+
CONFIG_STM32_SPI1=y
54+
CONFIG_STM32_USART2=y
55+
CONFIG_SYSTEM_NSH=y
56+
CONFIG_USART2_RXBUFSIZE=128
57+
CONFIG_USART2_SERIAL_CONSOLE=y
58+
CONFIG_USART2_TXBUFSIZE=128

boards/arm/stm32/stm32f4discovery/include/board.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,13 @@
448448

449449
#define BOARD_APDS9960_GPIO_INT GPIO_APDS9960_INT
450450

451+
/* IRQ Pin for MPR121 Capacitive Keypad */
452+
453+
#define GPIO_MPR121_INT \
454+
(GPIO_INPUT|GPIO_PULLUP|GPIO_EXTI|GPIO_PORTB|GPIO_PIN0)
455+
456+
#define BOARD_MPR121_GPIO_INT GPIO_MPR121_INT
457+
451458
/* LIS3DSH */
452459

453460
#define GPIO_LIS3DSH_EXT0 \

boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@
6060
#include "stm32_apds9960.h"
6161
#endif
6262

63+
#ifdef CONFIG_INPUT_MPR121_KEYPAD
64+
#include "stm32_mpr121.h"
65+
#endif
66+
6367
#ifdef CONFIG_CL_MFRC522
6468
#include "stm32_mfrc522.h"
6569
#endif
@@ -272,6 +276,16 @@ int stm32_bringup(void)
272276
}
273277
#endif
274278

279+
#ifdef CONFIG_INPUT_MPR121_KEYPAD
280+
/* Initialize MPR121 using I2C1 bus to /dev/keypad0 */
281+
282+
ret = board_mpr121_initialize(0, 1);
283+
if (ret < 0)
284+
{
285+
syslog(LOG_ERR, "ERROR: board_mpr121_initialize failed: %d\n", ret);
286+
}
287+
#endif
288+
275289
#ifdef CONFIG_LCD_ST7032
276290
ret = stm32_st7032init("/dev/slcd0");
277291
if (ret < 0)

0 commit comments

Comments
 (0)