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 Mar 29 21:32:38 2013 +0000
Revision:
2:4c0d4b90a3ed
Parent:
1:cee45334b36a
Child:
3:828260f21de6
Updated documentation and added LogUtil class

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 2:4c0d4b90a3ed 27 #include "LogUtil.h"
sam_grove 0:42add775212a 28
sam_grove 0:42add775212a 29 /** Using the Sparkfun SEN-10250
sam_grove 0:42add775212a 30 *
sam_grove 0:42add775212a 31 * Example:
sam_grove 0:42add775212a 32 * @code
sam_grove 2:4c0d4b90a3ed 33 * #include "mbed.h"
sam_grove 2:4c0d4b90a3ed 34 * #include "MPR121.h"
sam_grove 2:4c0d4b90a3ed 35 *
sam_grove 2:4c0d4b90a3ed 36 * // TODO: put IC in low power mode when disabled
sam_grove 0:42add775212a 37 *
sam_grove 2:4c0d4b90a3ed 38 * DigitalOut myled(LED1);
sam_grove 2:4c0d4b90a3ed 39 * DigitalOut off(LED4);
sam_grove 2:4c0d4b90a3ed 40 * Timer t;
sam_grove 2:4c0d4b90a3ed 41 *
sam_grove 2:4c0d4b90a3ed 42 * LogUtil logger;
sam_grove 2:4c0d4b90a3ed 43 *
sam_grove 2:4c0d4b90a3ed 44 * I2C i2c(p28, p27);
sam_grove 2:4c0d4b90a3ed 45 * InterruptIn irq(p26);
sam_grove 2:4c0d4b90a3ed 46 * MPR121 touch_pad(i2c, irq, MPR121::ADDR_VSS);
sam_grove 0:42add775212a 47 *
sam_grove 2:4c0d4b90a3ed 48 * int main()
sam_grove 2:4c0d4b90a3ed 49 * {
sam_grove 2:4c0d4b90a3ed 50 * touch_pad.init();
sam_grove 2:4c0d4b90a3ed 51 * touch_pad.enable();
sam_grove 2:4c0d4b90a3ed 52 * t.start();
sam_grove 2:4c0d4b90a3ed 53 * while(1)
sam_grove 2:4c0d4b90a3ed 54 * {
sam_grove 2:4c0d4b90a3ed 55 * if(touch_pad.isPressed())
sam_grove 2:4c0d4b90a3ed 56 * {
sam_grove 2:4c0d4b90a3ed 57 * uint16_t button_val = touch_pad.buttonPressed();
sam_grove 2:4c0d4b90a3ed 58 * LOG("button = 0x%04x\n", button_val);
sam_grove 2:4c0d4b90a3ed 59 * myled = (button_val>0) ? 1 : 0;
sam_grove 2:4c0d4b90a3ed 60 * }
sam_grove 2:4c0d4b90a3ed 61 * if(t.read_ms() > 5000)
sam_grove 2:4c0d4b90a3ed 62 * {
sam_grove 2:4c0d4b90a3ed 63 * touch_pad.disable();
sam_grove 2:4c0d4b90a3ed 64 * off = 1;
sam_grove 2:4c0d4b90a3ed 65 * wait(5.0f);
sam_grove 2:4c0d4b90a3ed 66 * off = 0;
sam_grove 2:4c0d4b90a3ed 67 * touch_pad.enable();
sam_grove 2:4c0d4b90a3ed 68 * t.reset();
sam_grove 2:4c0d4b90a3ed 69 * }
sam_grove 2:4c0d4b90a3ed 70 *
sam_grove 2:4c0d4b90a3ed 71 * }
sam_grove 2:4c0d4b90a3ed 72 * }
sam_grove 0:42add775212a 73 * @endcode
sam_grove 0:42add775212a 74 */
sam_grove 0:42add775212a 75
sam_grove 0:42add775212a 76 /**
sam_grove 0:42add775212a 77 * @class MPR121
sam_grove 0:42add775212a 78 * @brief API abstraction for the MPR121 capacitive touch IC
sam_grove 0:42add775212a 79 */
sam_grove 0:42add775212a 80 class MPR121
sam_grove 0:42add775212a 81 {
sam_grove 0:42add775212a 82 private:
sam_grove 0:42add775212a 83
sam_grove 0:42add775212a 84 I2C *_i2c;
sam_grove 0:42add775212a 85 InterruptIn *_irq;
sam_grove 0:42add775212a 86 uint8_t _i2c_addr;
sam_grove 0:42add775212a 87 volatile uint16_t _button;
sam_grove 0:42add775212a 88 volatile uint32_t _button_has_changed;
sam_grove 0:42add775212a 89
sam_grove 0:42add775212a 90 /** The interrupt handler for the IRQ pin
sam_grove 0:42add775212a 91 */
sam_grove 0:42add775212a 92 void handler(void);
sam_grove 0:42add775212a 93
sam_grove 0:42add775212a 94 public:
sam_grove 0:42add775212a 95
sam_grove 0:42add775212a 96 /**
sam_grove 0:42add775212a 97 * @enum MPR121_ADDR
sam_grove 0:42add775212a 98 * @brief Possible terminations for the ADDR pin
sam_grove 0:42add775212a 99 */
sam_grove 0:42add775212a 100 enum MPR121_ADDR
sam_grove 0:42add775212a 101 {
sam_grove 0:42add775212a 102 ADDR_VSS = 0x5A, /*!< ADDR connected to VSS */
sam_grove 0:42add775212a 103 ADDR_VDD, /*!< ADDR connected to VDD */
sam_grove 0:42add775212a 104 ADDR_SCL, /*!< ADDR connected to SDA */
sam_grove 0:42add775212a 105 ADDR_SDA /*!< ADDR connected to SCL */
sam_grove 0:42add775212a 106 };
sam_grove 0:42add775212a 107
sam_grove 0:42add775212a 108 /**
sam_grove 0:42add775212a 109 * @enum MPR121_REGISTER
sam_grove 0:42add775212a 110 * @brief The device register map
sam_grove 0:42add775212a 111 */
sam_grove 0:42add775212a 112 enum MPR121_REGISTER
sam_grove 0:42add775212a 113 {
sam_grove 0:42add775212a 114 ELE0_7_STAT = 0x00,
sam_grove 0:42add775212a 115 ELE8_11_STAT, ELE0_7_OOR_STAT, ELE8_11_OOR_STAT, EFD0LB, EFD0HB,
sam_grove 0:42add775212a 116 EFD1LB, EFD1HB, EFD2LB, EFD2HB, EFD3LB, EFD3HB, EFD4LB, EFD4HB, EFD5LB, EFD5HB,
sam_grove 0:42add775212a 117
sam_grove 0:42add775212a 118 EFD6LB = 0x10,
sam_grove 0:42add775212a 119 EFD6HB, EFD7LB, EFD7HB, EFD8LB, EFD8HB, EFD9LB, EFD9HB, EFD10LB,
sam_grove 0:42add775212a 120 EFD10HB, EFD11LB, EFD11HB, EFDPROXLB, EFDPROXHB, E0BV, E1BV,
sam_grove 0:42add775212a 121
sam_grove 0:42add775212a 122 E2BV = 0x20,
sam_grove 0:42add775212a 123 E3BV, E4BV, E5BV, E6BV, E7BV, E8BV, E9BV, E10BV, E11BV, EPROXBV,
sam_grove 0:42add775212a 124 MHDR, NHDR, NCLR, FDLR, MHDF,
sam_grove 0:42add775212a 125
sam_grove 0:42add775212a 126 NHDF = 0x30,
sam_grove 0:42add775212a 127 NCLF, FDLF, NHDT, NCLT, FDLT, MHDPROXR, NHDPROXR, NCLPROXR,
sam_grove 0:42add775212a 128 FDLPROXR, MHDPROXF, NHDPROXF, NCLPROXF, FDLPROXF, NHDPROXT, NCLPROXT,
sam_grove 0:42add775212a 129
sam_grove 0:42add775212a 130 FDLPROXT = 0x40,
sam_grove 0:42add775212a 131 E0TTH, E0RTH, E1TTH, E1RTH, E2TTH, E2RTH, E3TTH, E3RTH,
sam_grove 0:42add775212a 132 E4TTH, E4RTH, E5TTH, E5RTH, E6TTH, E6RTH, E7TTH,
sam_grove 0:42add775212a 133
sam_grove 0:42add775212a 134 E7RTH = 0x50,
sam_grove 0:42add775212a 135 E8TTH, E8RTH, E9TTH, E9RTH, E10TTH, E10RTH, E11TTH, E11RTH,
sam_grove 0:42add775212a 136 EPROXTTH, EPROXRTH, DT_DR, CDC_CONFIG, CDT_CONFIG, ECR, CDC0,
sam_grove 0:42add775212a 137
sam_grove 0:42add775212a 138 CDC1 = 0x60,
sam_grove 0:42add775212a 139 CDC2, CDC3, CDC4, CDC5, CDC6, CDC7, CDC8, CDC9, CDC10, CDC11, CDCPROX, CDT0_CDT1,
sam_grove 0:42add775212a 140 CDT2_CDT3, CDT4_CDT5, CDT6_CDT7,
sam_grove 0:42add775212a 141
sam_grove 0:42add775212a 142 CDT8_CDT9 = 0x70,
sam_grove 0:42add775212a 143 CDT10_CDT11, CDTPROX, GPIO_CTRL0, GPIO_CTRL1, GPIO_DATA, GPIO_DIR, GPIO_EN, GPIO_SET,
sam_grove 0:42add775212a 144 GPIO_CLR, GPIO_TOGGLE, AUTO_CFG0, AUTO_CFG1, USL, LSL, TL,
sam_grove 0:42add775212a 145
sam_grove 0:42add775212a 146 SRST = 0x80
sam_grove 0:42add775212a 147 };
sam_grove 0:42add775212a 148
sam_grove 0:42add775212a 149 /** Create the MPR121 object
sam_grove 1:cee45334b36a 150 * @param i2c - A defined I2C object
sam_grove 1:cee45334b36a 151 * @param pin - A defined InterruptIn object
sam_grove 1:cee45334b36a 152 * @param i2c_addr - Connection of the address line
sam_grove 0:42add775212a 153 */
sam_grove 0:42add775212a 154 MPR121(I2C &i2c, InterruptIn &pin, MPR121_ADDR i2c_addr);
sam_grove 0:42add775212a 155
sam_grove 0:42add775212a 156 /** Clear state vars 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 0:42add775212a 181 */
sam_grove 1:cee45334b36a 182 void registerDump(void) const;
sam_grove 0:42add775212a 183
sam_grove 0:42add775212a 184 /** Write to a register (exposed for debugging reasons)
sam_grove 0:42add775212a 185 * Note: most writes are only valid in stop mode
sam_grove 1:cee45334b36a 186 * @param reg - The register to be written
sam_grove 1:cee45334b36a 187 * @param data - The data to be written
sam_grove 0:42add775212a 188 */
sam_grove 1:cee45334b36a 189 void writeRegister(uint8_t const reg, uint8_t const data) const;
sam_grove 0:42add775212a 190
sam_grove 0:42add775212a 191 /** Read from a register (exposed for debugging reasons)
sam_grove 1:cee45334b36a 192 * @param reg - The register to read from
sam_grove 0:42add775212a 193 * @return The register contents
sam_grove 0:42add775212a 194 */
sam_grove 1:cee45334b36a 195 uint8_t readRegister(uint8_t const reg) const;
sam_grove 0:42add775212a 196
sam_grove 0:42add775212a 197 };
sam_grove 0:42add775212a 198
sam_grove 0:42add775212a 199 #endif