|
| 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 | +} |
0 commit comments