A device driver for the Freescale MPR121 capactive touch IC. Not optimized for any particular system, just a starting point to get the chip up in running in no time. Changes to registers init() method will tailor the library for end system use.

Dependents:   Seeed_Grove_I2C_Touch_Example MPR121_HelloWorld mbed_petbottle_holder_shikake test_DEV-10508 ... more

Datasheet:

http://cache.freescale.com/files/sensors/doc/data_sheet/MPR121.pdf

Information

Must add pull-ups to the I2C bus!!

Committer:
sam_grove
Date:
Fri Aug 15 23:00:13 2014 +0000
Revision:
5:3934358ec2b7
Parent:
3:828260f21de6
Updated library to work without the IRQ pin being connected

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 0:42add775212a 1 /**
sam_grove 0:42add775212a 2 * @file MPR121.h
sam_grove 0:42add775212a 3 * @brief Device driver - MPR121 capactiive touch IC
sam_grove 0:42add775212a 4 * @author sam grove
sam_grove 0:42add775212a 5 * @version 1.0
sam_grove 0:42add775212a 6 * @see http://cache.freescale.com/files/sensors/doc/data_sheet/MPR121.pdf
sam_grove 0:42add775212a 7 *
sam_grove 0:42add775212a 8 * Copyright (c) 2013
sam_grove 0:42add775212a 9 *
sam_grove 0:42add775212a 10 * Licensed under the Apache License, Version 2.0 (the "License");
sam_grove 0:42add775212a 11 * you may not use this file except in compliance with the License.
sam_grove 0:42add775212a 12 * You may obtain a copy of the License at
sam_grove 0:42add775212a 13 *
sam_grove 0:42add775212a 14 * http://www.apache.org/licenses/LICENSE-2.0
sam_grove 0:42add775212a 15 *
sam_grove 0:42add775212a 16 * Unless required by applicable law or agreed to in writing, software
sam_grove 0:42add775212a 17 * distributed under the License is distributed on an "AS IS" BASIS,
sam_grove 0:42add775212a 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sam_grove 0:42add775212a 19 * See the License for the specific language governing permissions and
sam_grove 0:42add775212a 20 * limitations under the License.
sam_grove 0:42add775212a 21 */
sam_grove 0:42add775212a 22
sam_grove 0:42add775212a 23 #ifndef MPR121_H
sam_grove 0:42add775212a 24 #define MPR121_H
sam_grove 0:42add775212a 25
sam_grove 0:42add775212a 26 #include "mbed.h"
sam_grove 0:42add775212a 27
sam_grove 3:828260f21de6 28 /** Using the Sparkfun SEN-10250 BoB
sam_grove 0:42add775212a 29 *
sam_grove 0:42add775212a 30 * Example:
sam_grove 0:42add775212a 31 * @code
sam_grove 3:828260f21de6 32 * #include "mbed.h"
sam_grove 3:828260f21de6 33 * #include "MPR121.h"
sam_grove 3:828260f21de6 34 *
sam_grove 3:828260f21de6 35 * Serial pc(USBTX, USBRX);
sam_grove 3:828260f21de6 36 * DigitalOut myled(LED1);
sam_grove 3:828260f21de6 37 *
sam_grove 3:828260f21de6 38 * #if defined TARGET_LPC1768 || TARGET_LPC11U24
sam_grove 3:828260f21de6 39 * I2C i2c(p28, p27);
sam_grove 3:828260f21de6 40 * InterruptIn irq(p26);
sam_grove 3:828260f21de6 41 * MPR121 touch_pad(i2c, irq, MPR121::ADDR_VSS);
sam_grove 3:828260f21de6 42 *
sam_grove 3:828260f21de6 43 * #elif defined TARGET_KL25Z
sam_grove 3:828260f21de6 44 * I2C i2c(PTC9, PTC8);
sam_grove 3:828260f21de6 45 * InterruptIn irq(PTA5);
sam_grove 3:828260f21de6 46 * MPR121 touch_pad(i2c, irq, MPR121::ADDR_VSS);
sam_grove 3:828260f21de6 47 *
sam_grove 3:828260f21de6 48 * #else
sam_grove 3:828260f21de6 49 * #error TARGET NOT TESTED
sam_grove 3:828260f21de6 50 * #endif
sam_grove 3:828260f21de6 51 *
sam_grove 3:828260f21de6 52 * int main()
sam_grove 3:828260f21de6 53 * {
sam_grove 3:828260f21de6 54 * touch_pad.init();
sam_grove 3:828260f21de6 55 * touch_pad.enable();
sam_grove 3:828260f21de6 56 *
sam_grove 3:828260f21de6 57 * while(1)
sam_grove 3:828260f21de6 58 * {
sam_grove 3:828260f21de6 59 * if(touch_pad.isPressed())
sam_grove 3:828260f21de6 60 * {
sam_grove 3:828260f21de6 61 * uint16_t button_val = touch_pad.buttonPressed();
sam_grove 3:828260f21de6 62 * printf("button = 0x%04x\n", button_val);
sam_grove 3:828260f21de6 63 * myled = (button_val>0) ? 1 : 0;
sam_grove 3:828260f21de6 64 * }
sam_grove 3:828260f21de6 65 * }
sam_grove 3:828260f21de6 66 * }
sam_grove 0:42add775212a 67 * @endcode
sam_grove 0:42add775212a 68 */
sam_grove 0:42add775212a 69
sam_grove 0:42add775212a 70 /**
sam_grove 0:42add775212a 71 * @class MPR121
sam_grove 3:828260f21de6 72 * @brief API for the MPR121 capacitive touch IC
sam_grove 0:42add775212a 73 */
sam_grove 0:42add775212a 74 class MPR121
sam_grove 0:42add775212a 75 {
sam_grove 0:42add775212a 76 private:
sam_grove 0:42add775212a 77
sam_grove 0:42add775212a 78 I2C *_i2c;
sam_grove 0:42add775212a 79 InterruptIn *_irq;
sam_grove 0:42add775212a 80 uint8_t _i2c_addr;
sam_grove 0:42add775212a 81 volatile uint16_t _button;
sam_grove 0:42add775212a 82 volatile uint32_t _button_has_changed;
sam_grove 0:42add775212a 83
sam_grove 0:42add775212a 84 /** The interrupt handler for the IRQ pin
sam_grove 0:42add775212a 85 */
sam_grove 0:42add775212a 86 void handler(void);
sam_grove 0:42add775212a 87
sam_grove 0:42add775212a 88 public:
sam_grove 0:42add775212a 89
sam_grove 0:42add775212a 90 /**
sam_grove 0:42add775212a 91 * @enum MPR121_ADDR
sam_grove 0:42add775212a 92 * @brief Possible terminations for the ADDR pin
sam_grove 0:42add775212a 93 */
sam_grove 0:42add775212a 94 enum MPR121_ADDR
sam_grove 0:42add775212a 95 {
sam_grove 0:42add775212a 96 ADDR_VSS = 0x5A, /*!< ADDR connected to VSS */
sam_grove 0:42add775212a 97 ADDR_VDD, /*!< ADDR connected to VDD */
sam_grove 0:42add775212a 98 ADDR_SCL, /*!< ADDR connected to SDA */
sam_grove 0:42add775212a 99 ADDR_SDA /*!< ADDR connected to SCL */
sam_grove 0:42add775212a 100 };
sam_grove 0:42add775212a 101
sam_grove 0:42add775212a 102 /**
sam_grove 0:42add775212a 103 * @enum MPR121_REGISTER
sam_grove 0:42add775212a 104 * @brief The device register map
sam_grove 0:42add775212a 105 */
sam_grove 0:42add775212a 106 enum MPR121_REGISTER
sam_grove 0:42add775212a 107 {
sam_grove 0:42add775212a 108 ELE0_7_STAT = 0x00,
sam_grove 0:42add775212a 109 ELE8_11_STAT, ELE0_7_OOR_STAT, ELE8_11_OOR_STAT, EFD0LB, EFD0HB,
sam_grove 0:42add775212a 110 EFD1LB, EFD1HB, EFD2LB, EFD2HB, EFD3LB, EFD3HB, EFD4LB, EFD4HB, EFD5LB, EFD5HB,
sam_grove 0:42add775212a 111
sam_grove 0:42add775212a 112 EFD6LB = 0x10,
sam_grove 0:42add775212a 113 EFD6HB, EFD7LB, EFD7HB, EFD8LB, EFD8HB, EFD9LB, EFD9HB, EFD10LB,
sam_grove 0:42add775212a 114 EFD10HB, EFD11LB, EFD11HB, EFDPROXLB, EFDPROXHB, E0BV, E1BV,
sam_grove 0:42add775212a 115
sam_grove 0:42add775212a 116 E2BV = 0x20,
sam_grove 0:42add775212a 117 E3BV, E4BV, E5BV, E6BV, E7BV, E8BV, E9BV, E10BV, E11BV, EPROXBV,
sam_grove 0:42add775212a 118 MHDR, NHDR, NCLR, FDLR, MHDF,
sam_grove 0:42add775212a 119
sam_grove 0:42add775212a 120 NHDF = 0x30,
sam_grove 0:42add775212a 121 NCLF, FDLF, NHDT, NCLT, FDLT, MHDPROXR, NHDPROXR, NCLPROXR,
sam_grove 0:42add775212a 122 FDLPROXR, MHDPROXF, NHDPROXF, NCLPROXF, FDLPROXF, NHDPROXT, NCLPROXT,
sam_grove 0:42add775212a 123
sam_grove 0:42add775212a 124 FDLPROXT = 0x40,
sam_grove 0:42add775212a 125 E0TTH, E0RTH, E1TTH, E1RTH, E2TTH, E2RTH, E3TTH, E3RTH,
sam_grove 0:42add775212a 126 E4TTH, E4RTH, E5TTH, E5RTH, E6TTH, E6RTH, E7TTH,
sam_grove 0:42add775212a 127
sam_grove 0:42add775212a 128 E7RTH = 0x50,
sam_grove 0:42add775212a 129 E8TTH, E8RTH, E9TTH, E9RTH, E10TTH, E10RTH, E11TTH, E11RTH,
sam_grove 0:42add775212a 130 EPROXTTH, EPROXRTH, DT_DR, CDC_CONFIG, CDT_CONFIG, ECR, CDC0,
sam_grove 0:42add775212a 131
sam_grove 0:42add775212a 132 CDC1 = 0x60,
sam_grove 0:42add775212a 133 CDC2, CDC3, CDC4, CDC5, CDC6, CDC7, CDC8, CDC9, CDC10, CDC11, CDCPROX, CDT0_CDT1,
sam_grove 0:42add775212a 134 CDT2_CDT3, CDT4_CDT5, CDT6_CDT7,
sam_grove 0:42add775212a 135
sam_grove 0:42add775212a 136 CDT8_CDT9 = 0x70,
sam_grove 0:42add775212a 137 CDT10_CDT11, CDTPROX, GPIO_CTRL0, GPIO_CTRL1, GPIO_DATA, GPIO_DIR, GPIO_EN, GPIO_SET,
sam_grove 0:42add775212a 138 GPIO_CLR, GPIO_TOGGLE, AUTO_CFG0, AUTO_CFG1, USL, LSL, TL,
sam_grove 0:42add775212a 139
sam_grove 0:42add775212a 140 SRST = 0x80
sam_grove 0:42add775212a 141 };
sam_grove 0:42add775212a 142
sam_grove 0:42add775212a 143 /** Create the MPR121 object
sam_grove 1:cee45334b36a 144 * @param i2c - A defined I2C object
sam_grove 1:cee45334b36a 145 * @param pin - A defined InterruptIn object
sam_grove 1:cee45334b36a 146 * @param i2c_addr - Connection of the address line
sam_grove 0:42add775212a 147 */
sam_grove 0:42add775212a 148 MPR121(I2C &i2c, InterruptIn &pin, MPR121_ADDR i2c_addr);
sam_grove 0:42add775212a 149
sam_grove 5:3934358ec2b7 150 /** Create the MPR121 object
sam_grove 5:3934358ec2b7 151 * @param i2c - A defined I2C object
sam_grove 5:3934358ec2b7 152 * @param i2c_addr - Connection of the address line
sam_grove 5:3934358ec2b7 153 */
sam_grove 5:3934358ec2b7 154 MPR121(I2C &i2c, MPR121_ADDR i2c_addr);
sam_grove 5:3934358ec2b7 155
sam_grove 3:828260f21de6 156 /** Clear state variables and initilize the dependant objects
sam_grove 0:42add775212a 157 */
sam_grove 0:42add775212a 158 void init(void);
sam_grove 0:42add775212a 159
sam_grove 0:42add775212a 160 /** Allow the IC to run and collect user input
sam_grove 0:42add775212a 161 */
sam_grove 0:42add775212a 162 void enable(void);
sam_grove 0:42add775212a 163
sam_grove 0:42add775212a 164 /** Stop the IC and put into low power mode
sam_grove 0:42add775212a 165 */
sam_grove 0:42add775212a 166 void disable(void);
sam_grove 0:42add775212a 167
sam_grove 0:42add775212a 168 /** Determine if a new button press event occured
sam_grove 0:42add775212a 169 * Upon calling the state is cleared until another press is detected
sam_grove 0:42add775212a 170 * @return 1 if a press has been detected since the last call, 0 otherwise
sam_grove 0:42add775212a 171 */
sam_grove 0:42add775212a 172 uint32_t isPressed(void);
sam_grove 0:42add775212a 173
sam_grove 0:42add775212a 174 /** Get the electrode status (ELE12 ... ELE0 -> b15 xxx b11 ... b0
sam_grove 0:42add775212a 175 * The buttons are bit mapped. ELE0 = b0 ... ELE11 = b11 b12 ... b15 undefined
sam_grove 0:42add775212a 176 * @return The state of all buttons
sam_grove 0:42add775212a 177 */
sam_grove 0:42add775212a 178 uint16_t buttonPressed(void);
sam_grove 0:42add775212a 179
sam_grove 0:42add775212a 180 /** print the register map and values to the console
sam_grove 3:828260f21de6 181 * @param obj - a Serial object that prints to a console
sam_grove 0:42add775212a 182 */
sam_grove 3:828260f21de6 183 void registerDump(Serial &obj) const;
sam_grove 0:42add775212a 184
sam_grove 5:3934358ec2b7 185 /** print the register map and values to the console
sam_grove 5:3934358ec2b7 186 */
sam_grove 5:3934358ec2b7 187 void registerDump(void) const;
sam_grove 5:3934358ec2b7 188
sam_grove 0:42add775212a 189 /** Write to a register (exposed for debugging reasons)
sam_grove 0:42add775212a 190 * Note: most writes are only valid in stop mode
sam_grove 1:cee45334b36a 191 * @param reg - The register to be written
sam_grove 1:cee45334b36a 192 * @param data - The data to be written
sam_grove 0:42add775212a 193 */
sam_grove 3:828260f21de6 194 void writeRegister(MPR121_REGISTER const reg, uint8_t const data) const;
sam_grove 0:42add775212a 195
sam_grove 0:42add775212a 196 /** Read from a register (exposed for debugging reasons)
sam_grove 1:cee45334b36a 197 * @param reg - The register to read from
sam_grove 0:42add775212a 198 * @return The register contents
sam_grove 0:42add775212a 199 */
sam_grove 3:828260f21de6 200 uint8_t readRegister(MPR121_REGISTER const reg) const;
sam_grove 0:42add775212a 201
sam_grove 0:42add775212a 202 };
sam_grove 0:42add775212a 203
sam_grove 0:42add775212a 204 #endif